Skip to content

Commit

Permalink
lib/chkname.c: login_name_max_size(): Put limits for LOGIN_NAME_MAX a…
Browse files Browse the repository at this point in the history
…nd sysconf(_SC_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>
Co-developed-by: Chris Hofstaedtler <[email protected]>
Cc: Tobias Stoeckmann <[email protected]>
Cc: Samuel Thibault <[email protected]>
Signed-off-by: Alejandro Colomar <[email protected]>
  • Loading branch information
alejandro-colomar committed Jan 4, 2025
1 parent 2bbe1af commit 4db388c
Showing 1 changed file with 7 additions and 5 deletions.
12 changes: 7 additions & 5 deletions lib/chkname.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,18 @@
#include <limits.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <sys/param.h>
#include <unistd.h>

#include "defines.h"
#include "chkname.h"
#include "string/strcmp/streq.h"


#ifndef LOGIN_NAME_MAX
# define LOGIN_NAME_MAX 256
#endif


int allow_bad_names = false;


Expand All @@ -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;
}


Expand Down

0 comments on commit 4db388c

Please sign in to comment.