From 1e6087ae7cf4707f40b5f70e8288a59d0e4d9775 Mon Sep 17 00:00:00 2001 From: Alejandro Colomar Date: Tue, 3 Mar 2026 15:33:34 +0100 Subject: [PATCH 1/3] lib/nss.c: Fix incorrect handling of white space Having trailing white space in a line doesn't remove the need for a trailing '\n'. Let's fail if a line doesn't have it, regardless of how much trailing white space there is. Fixes: 8492dee6 (2021-04-16; "subids: support nsswitch") Cc: Serge Hallyn Signed-off-by: Alejandro Colomar --- lib/nss.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/nss.c b/lib/nss.c index 5957390741..4423f1fb8d 100644 --- a/lib/nss.c +++ b/lib/nss.c @@ -94,11 +94,12 @@ nss_init(const char *nsswitch_path) { if (p == NULL) { goto null_subid; } - if (stpsep(p, " \t\n") == NULL) { + if (stpsep(p, "\n") == NULL) { fprintf(log_get_logfd(), "No usable subid NSS module found, using files\n"); // subid_nss has to be null here, but to ease reviews: goto null_subid; } + stpsep(p, " \t"); if (streq(p, "files")) { goto null_subid; } From 0528c3c24d14c48d92cba815bfa6d065e451d48b Mon Sep 17 00:00:00 2001 From: Alejandro Colomar Date: Tue, 3 Mar 2026 16:14:50 +0100 Subject: [PATCH 2/3] lib/nss.c: Fix error handling This error message didn't make much sense. After inspecting the commit in which it was introduced, it seems the intention was to diagnose if the line was empty after ignoring white space. It was incorrectly written then, so fix it now. Rewrite it in the following way: - If there's not a '\n', the entire line is bogus. Fail, and report an appropriate diagnostic. - Then, break the string at the first white space, as we were doing before. No error handling is appropriate here. - Then, diagnose if the remaining string is empty. Fixes: 8492dee6 (2021-04-16; "subids: support nsswitch") Link: Cc: Serge Hallyn Signed-off-by: Alejandro Colomar --- lib/nss.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/lib/nss.c b/lib/nss.c index 4423f1fb8d..6954120420 100644 --- a/lib/nss.c +++ b/lib/nss.c @@ -95,11 +95,15 @@ nss_init(const char *nsswitch_path) { goto null_subid; } if (stpsep(p, "\n") == NULL) { + fprintf(log_get_logfd(), "%s: Non-text file.\n", nsswitch_path); + goto null_subid; + } + stpsep(p, " \t"); + if (streq(p, "")) { fprintf(log_get_logfd(), "No usable subid NSS module found, using files\n"); // subid_nss has to be null here, but to ease reviews: goto null_subid; } - stpsep(p, " \t"); if (streq(p, "files")) { goto null_subid; } From 81cb1e748fe3015d159f1437aff08c178f09a92b Mon Sep 17 00:00:00 2001 From: Alejandro Colomar Date: Tue, 3 Mar 2026 12:29:56 +0100 Subject: [PATCH 3/3] lib/nss.c: Move '\n' check earlier If a line doesn't have a '\n', this is a problem, and the line should be rejected, immediately. Also, remove the '\n' during the check (with stpsep()), because we don't want it there when handling the string. Link: Cc: Serge Hallyn Signed-off-by: Alejandro Colomar --- lib/nss.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/lib/nss.c b/lib/nss.c index 6954120420..fb739ff073 100644 --- a/lib/nss.c +++ b/lib/nss.c @@ -79,14 +79,18 @@ nss_init(const char *nsswitch_path) { } p = NULL; while (getline(&line, &len, nssfp) != -1) { + if (stpsep(line, "\n") == NULL) { + fprintf(log_get_logfd(), "%s: Non-text file.\n", nsswitch_path); + goto null_subid; + } if (strprefix(line, "#")) continue; - if (strlen(line) < 8) + if (strlen(line) < 7) continue; if (!strcaseprefix(line, "subid:")) continue; p = &line[6]; - p = stpspn(p, " \t\n"); + p = stpspn(p, " \t"); if (!streq(p, "")) break; p = NULL; @@ -94,10 +98,6 @@ nss_init(const char *nsswitch_path) { if (p == NULL) { goto null_subid; } - if (stpsep(p, "\n") == NULL) { - fprintf(log_get_logfd(), "%s: Non-text file.\n", nsswitch_path); - goto null_subid; - } stpsep(p, " \t"); if (streq(p, "")) { fprintf(log_get_logfd(), "No usable subid NSS module found, using files\n");