From 8b36662205ed6ab0ec93a7e07f9fea66a8b4fd6d Mon Sep 17 00:00:00 2001 From: Alejandro Colomar Date: Sat, 4 Jan 2025 13:10:48 +0100 Subject: [PATCH] lib/chkname.c: login_name_max_size(): Put limits for LOGIN_NAME_MAX and sysconf(_SC_LOGIN_NAME_MAX) GNU Hurd doesn't define LOGIN_NAME_MAX. GNU Hurd recommends having no system limits. When a program needs a limit, because it needs to validate user input, it is recommended that each program defines its own limit macros. The rationale is that this avoids hard-coded limits in ABIs, which cannot be modified ever. However, that doesn't mean that programs should have no limits at all. We use this limit for validating user input, and so we shouldn't allow anything just because the system doesn't want to set a limit. So, when sysconf(2) returns -1, either due to an error or due to a claim for no limits, we must fall back to the LOGIN_NAME_MAX value. And if the system doesn't define that value, we must define it ourselves (we're more or less free to choose any value, so let's pick the one that glibc provides nowadays). Fixes: 6a1f45d932c8 (2024-02-04; "lib/chkname.c: Support unlimited user name lengths") Closes: Cc: Chris Hofstaedtler Reviewed-by: Samuel Thibault Reviewed-by: Tobias Stoeckmann Reviewed-by: Iker Pedrosa Signed-off-by: Alejandro Colomar --- lib/chkname.c | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/lib/chkname.c b/lib/chkname.c index 98f791706..bee2e6bd7 100644 --- a/lib/chkname.c +++ b/lib/chkname.c @@ -2,7 +2,7 @@ // SPDX-FileCopyrightText: 1996-2000, Marek Michałkiewicz // SPDX-FileCopyrightText: 2001-2005, Tomasz Kłoczko // SPDX-FileCopyrightText: 2005-2008, Nicolas François -// SPDX-FileCopyrightText: 2023-2024, Alejandro Colomar +// SPDX-FileCopyrightText: 2023-2025, Alejandro Colomar // SPDX-License-Identifier: BSD-3-Clause @@ -27,8 +27,6 @@ #include #include #include -#include -#include #include #include "defines.h" @@ -36,6 +34,11 @@ #include "string/strcmp/streq.h" +#ifndef LOGIN_NAME_MAX +# define LOGIN_NAME_MAX 256 +#endif + + int allow_bad_names = false; @@ -44,12 +47,11 @@ login_name_max_size(void) { long conf; - errno = 0; conf = sysconf(_SC_LOGIN_NAME_MAX); - if (conf == -1 && errno != 0) + if (conf == -1) return LOGIN_NAME_MAX; - return MIN(conf, PTRDIFF_MAX); + return conf; } -- 2.47.2