Skip to content

Commit d6c575e

Browse files
committed
nss: when we encounter an invalid user/group name or UID/GID, don't return EINVAL
It's not our business to validate invalid user/group names or UID/GID. Ideally, libc would filter these out, but they don't, hence we have to filter, but let's not propagate this as error, but simply as "not found" to the caller. User name rules are pretty vaguely defined, and the rules defined by POSIX clash with reality quite heavily (for example, utmp doesn't offer enough room for user name length, and /usr/bin/chown permits separating user/group names by a single dot, even though POSIX allows dots being used in user/group names themselves.) We enforce stricter rules than POSIX for good reason, and hence in doing so we should not categorically return EINVAL on stuff we don't consider valid, but other components might. Fixes: systemd#4983
1 parent 1429dfe commit d6c575e

File tree

2 files changed

+14
-24
lines changed

2 files changed

+14
-24
lines changed

src/nss-mymachines/nss-mymachines.c

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -512,10 +512,8 @@ enum nss_status _nss_mymachines_getpwuid_r(
512512

513513
BLOCK_SIGNALS(NSS_SIGNALS_BLOCK);
514514

515-
if (!uid_is_valid(uid)) {
516-
r = -EINVAL;
517-
goto fail;
518-
}
515+
if (!uid_is_valid(uid))
516+
goto not_found;
519517

520518
/* We consider all uids < 65536 host uids */
521519
if (uid < HOST_UID_LIMIT)
@@ -686,10 +684,8 @@ enum nss_status _nss_mymachines_getgrgid_r(
686684

687685
BLOCK_SIGNALS(NSS_SIGNALS_BLOCK);
688686

689-
if (!gid_is_valid(gid)) {
690-
r = -EINVAL;
691-
goto fail;
692-
}
687+
if (!gid_is_valid(gid))
688+
goto not_found;
693689

694690
/* We consider all gids < 65536 host gids */
695691
if (gid < HOST_GID_LIMIT)

src/nss-systemd/nss-systemd.c

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -123,10 +123,10 @@ enum nss_status _nss_systemd_getpwnam_r(
123123
assert(name);
124124
assert(pwd);
125125

126-
if (!valid_user_group_name(name)) {
127-
r = -EINVAL;
128-
goto fail;
129-
}
126+
/* If the username is not valid, then we don't know it. Ideally libc would filter these for us anyway. We don't
127+
* generate EINVAL here, because it isn't really out business to complain about invalid user names. */
128+
if (!valid_user_group_name(name))
129+
goto not_found;
130130

131131
/* Synthesize entries for the root and nobody users, in case they are missing in /etc/passwd */
132132
if (streq(name, root_passwd.pw_name)) {
@@ -227,10 +227,8 @@ enum nss_status _nss_systemd_getpwuid_r(
227227

228228
BLOCK_SIGNALS(NSS_SIGNALS_BLOCK);
229229

230-
if (!uid_is_valid(uid)) {
231-
r = -EINVAL;
232-
goto fail;
233-
}
230+
if (!uid_is_valid(uid))
231+
goto not_found;
234232

235233
/* Synthesize data for the root user and for nobody in case they are missing from /etc/passwd */
236234
if (uid == root_passwd.pw_uid) {
@@ -329,10 +327,8 @@ enum nss_status _nss_systemd_getgrnam_r(
329327
assert(name);
330328
assert(gr);
331329

332-
if (!valid_user_group_name(name)) {
333-
r = -EINVAL;
334-
goto fail;
335-
}
330+
if (!valid_user_group_name(name))
331+
goto not_found;
336332

337333
/* Synthesize records for root and nobody, in case they are missing form /etc/group */
338334
if (streq(name, root_group.gr_name)) {
@@ -430,10 +426,8 @@ enum nss_status _nss_systemd_getgrgid_r(
430426

431427
BLOCK_SIGNALS(NSS_SIGNALS_BLOCK);
432428

433-
if (!gid_is_valid(gid)) {
434-
r = -EINVAL;
435-
goto fail;
436-
}
429+
if (!gid_is_valid(gid))
430+
goto not_found;
437431

438432
/* Synthesize records for root and nobody, in case they are missing from /etc/group */
439433
if (gid == root_group.gr_gid) {

0 commit comments

Comments
 (0)