Skip to content

Commit f269895

Browse files
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: 6a1f45d (2024-02-04; "lib/chkname.c: Support unlimited user name lengths") Closes: <#1166> Cc: Chris Hofstaedtler <[email protected]> Cc: Tobias Stoeckmann <[email protected]> Cc: Samuel Thibault <[email protected]> Signed-off-by: Alejandro Colomar <[email protected]>
1 parent 2bbe1af commit f269895

File tree

1 file changed

+8
-6
lines changed

1 file changed

+8
-6
lines changed

lib/chkname.c

+8-6
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// SPDX-FileCopyrightText: 1996-2000, Marek Michałkiewicz
33
// SPDX-FileCopyrightText: 2001-2005, Tomasz Kłoczko
44
// SPDX-FileCopyrightText: 2005-2008, Nicolas François
5-
// SPDX-FileCopyrightText: 2023-2024, Alejandro Colomar <[email protected]>
5+
// SPDX-FileCopyrightText: 2023-2025, Alejandro Colomar <[email protected]>
66
// SPDX-License-Identifier: BSD-3-Clause
77

88

@@ -27,15 +27,18 @@
2727
#include <limits.h>
2828
#include <stdbool.h>
2929
#include <stddef.h>
30-
#include <stdint.h>
31-
#include <sys/param.h>
3230
#include <unistd.h>
3331

3432
#include "defines.h"
3533
#include "chkname.h"
3634
#include "string/strcmp/streq.h"
3735

3836

37+
#ifndef LOGIN_NAME_MAX
38+
# define LOGIN_NAME_MAX 256
39+
#endif
40+
41+
3942
int allow_bad_names = false;
4043

4144

@@ -44,12 +47,11 @@ login_name_max_size(void)
4447
{
4548
long conf;
4649

47-
errno = 0;
4850
conf = sysconf(_SC_LOGIN_NAME_MAX);
49-
if (conf == -1 && errno != 0)
51+
if (conf == -1)
5052
return LOGIN_NAME_MAX;
5153

52-
return MIN(conf, PTRDIFF_MAX);
54+
return conf;
5355
}
5456

5557

0 commit comments

Comments
 (0)