From 9bdc24477505bf2c93f1ecb021cd648693b21ef3 Mon Sep 17 00:00:00 2001 From: Alejandro Colomar Date: Tue, 14 Jul 2026 23:42:15 +0200 Subject: [PATCH 01/14] lib/tz.c, etc/, man/, tests/: Default to TZ=UTC CST6CDT seems to be a deprecated name, and also it feels weird to have an offset by default. The world defaults to UTC these days for most stuff. Signed-off-by: Alejandro Colomar --- etc/login.defs | 2 +- lib/tz.c | 2 +- man/login.defs.d/ENV_TZ.xml | 4 ++-- tests/newusers/62_create_user_no_aging/config/etc/login.defs | 2 +- tests/system/etc/login.defs | 2 +- 5 files changed, 6 insertions(+), 6 deletions(-) diff --git a/etc/login.defs b/etc/login.defs index 1b81afd82e..c05bb3ba59 100644 --- a/etc/login.defs +++ b/etc/login.defs @@ -143,7 +143,7 @@ HUSHLOGIN_FILE .hushlogin # If defined, either a TZ environment parameter spec or the # fully-rooted pathname of a file containing such a spec. # -#ENV_TZ TZ=CST6CDT +#ENV_TZ TZ=UTC #ENV_TZ /etc/tzname # diff --git a/lib/tz.c b/lib/tz.c index b2d9531e20..afcb952373 100644 --- a/lib/tz.c +++ b/lib/tz.c @@ -39,7 +39,7 @@ fp = fopen (fname, "r"); if ( (NULL == fp) || (fgets_a(tzbuf, fp) == NULL)) { - result = "TZ=CST6CDT"; + result = "TZ=UTC"; } else { stpsep(tzbuf, "\n"); result = tzbuf; diff --git a/man/login.defs.d/ENV_TZ.xml b/man/login.defs.d/ENV_TZ.xml index 04d208ed05..bef702f58e 100644 --- a/man/login.defs.d/ENV_TZ.xml +++ b/man/login.defs.d/ENV_TZ.xml @@ -11,14 +11,14 @@ If set, it will be used to define the TZ environment variable when a user login. The value can be the name of a timezone preceded by TZ= (for example - TZ=CST6CDT), or the full path to the file + TZ=UTC), or the full path to the file containing the timezone specification (for example /etc/tzname). If a full path is specified but the file does not exist or cannot be - read, the default is to use TZ=CST6CDT. + read, the default is to use TZ=UTC. diff --git a/tests/newusers/62_create_user_no_aging/config/etc/login.defs b/tests/newusers/62_create_user_no_aging/config/etc/login.defs index 1e61b5cd41..8beb789ee3 100644 --- a/tests/newusers/62_create_user_no_aging/config/etc/login.defs +++ b/tests/newusers/62_create_user_no_aging/config/etc/login.defs @@ -143,7 +143,7 @@ HUSHLOGIN_FILE .hushlogin # If defined, either a TZ environment parameter spec or the # fully-rooted pathname of a file containing such a spec. # -#ENV_TZ TZ=CST6CDT +#ENV_TZ TZ=UTC #ENV_TZ /etc/tzname # diff --git a/tests/system/etc/login.defs b/tests/system/etc/login.defs index 7f1feffd64..2286c0b85b 100644 --- a/tests/system/etc/login.defs +++ b/tests/system/etc/login.defs @@ -143,7 +143,7 @@ HUSHLOGIN_FILE .hushlogin # If defined, either a TZ environment parameter spec or the # fully-rooted pathname of a file containing such a spec. # -#ENV_TZ TZ=CST6CDT +#ENV_TZ TZ=UTC #ENV_TZ /etc/tzname # From 67d85220165b5ea74bdd733757d014dac35d2efb Mon Sep 17 00:00:00 2001 From: Alejandro Colomar Date: Tue, 14 Jul 2026 23:50:11 +0200 Subject: [PATCH 02/14] lib/tz.c: tz(): Remove unused initialization We set 'fp' unconditionally with the result of fopen(3). Signed-off-by: Alejandro Colomar --- lib/tz.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/tz.c b/lib/tz.c index afcb952373..2bc5eca408 100644 --- a/lib/tz.c +++ b/lib/tz.c @@ -32,7 +32,7 @@ */ /*@observer@*/const char *tz (const char *fname) { - FILE *fp = NULL; + FILE *fp; const char *result; static char tzbuf[BUFSIZ]; From 2c626466b45c30a0fcbad65ab5069fe02fd55ce1 Mon Sep 17 00:00:00 2001 From: Alejandro Colomar Date: Tue, 14 Jul 2026 23:47:07 +0200 Subject: [PATCH 03/14] lib/tz.c: tz(): Return early on error, to simplify Signed-off-by: Alejandro Colomar --- lib/tz.c | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/lib/tz.c b/lib/tz.c index 2bc5eca408..13f352d1ee 100644 --- a/lib/tz.c +++ b/lib/tz.c @@ -37,18 +37,17 @@ static char tzbuf[BUFSIZ]; fp = fopen (fname, "r"); - if ( (NULL == fp) - || (fgets_a(tzbuf, fp) == NULL)) { + if (fp == NULL) + return "TZ=UTC"; + + if (fgets_a(tzbuf, fp) == NULL) { result = "TZ=UTC"; } else { stpsep(tzbuf, "\n"); result = tzbuf; } - if (NULL != fp) { - (void) fclose (fp); - } - + fclose(fp); return result; } #else /* !USE_PAM */ From 274dcd2858603565802df7ffcec4df47ef7c7f64 Mon Sep 17 00:00:00 2001 From: Alejandro Colomar Date: Tue, 14 Jul 2026 23:51:24 +0200 Subject: [PATCH 04/14] lib/tz.c: tz(): Use goto to 'hide' error handling Signed-off-by: Alejandro Colomar --- lib/tz.c | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/lib/tz.c b/lib/tz.c index 13f352d1ee..ac62955a18 100644 --- a/lib/tz.c +++ b/lib/tz.c @@ -33,22 +33,22 @@ /*@observer@*/const char *tz (const char *fname) { FILE *fp; - const char *result; static char tzbuf[BUFSIZ]; fp = fopen (fname, "r"); if (fp == NULL) return "TZ=UTC"; - if (fgets_a(tzbuf, fp) == NULL) { - result = "TZ=UTC"; - } else { - stpsep(tzbuf, "\n"); - result = tzbuf; - } + if (fgets_a(tzbuf, fp) == NULL) + goto def; + stpsep(tzbuf, "\n"); + + fclose(fp); + return tzbuf; +def: fclose(fp); - return result; + return "TZ=UTC"; } #else /* !USE_PAM */ extern int ISO_C_forbids_an_empty_translation_unit; From c749d8029debfa131129c3dfe41e35d02d39236d Mon Sep 17 00:00:00 2001 From: Alejandro Colomar Date: Tue, 14 Jul 2026 23:59:02 +0200 Subject: [PATCH 05/14] lib/tz.c: tz(): Fail for non-text files Signed-off-by: Alejandro Colomar --- lib/tz.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/tz.c b/lib/tz.c index ac62955a18..0e4bb9835d 100644 --- a/lib/tz.c +++ b/lib/tz.c @@ -41,8 +41,8 @@ if (fgets_a(tzbuf, fp) == NULL) goto def; - - stpsep(tzbuf, "\n"); + if (stpsep(tzbuf, "\n") == NULL) + goto def; fclose(fp); return tzbuf; From 8ad129a78dcc707bf51c9b26179069b2093c9ed5 Mon Sep 17 00:00:00 2001 From: Alejandro Colomar Date: Wed, 15 Jul 2026 00:03:26 +0200 Subject: [PATCH 06/14] lib/tz.c: tz(): Rename local variables 'buf' instead of 'tzbuf', since it's a more common name. 'fname' => 'path', since it represents a path name, and not just a file component. Signed-off-by: Alejandro Colomar --- lib/tz.c | 36 +++++++++++++++++------------------- 1 file changed, 17 insertions(+), 19 deletions(-) diff --git a/lib/tz.c b/lib/tz.c index 0e4bb9835d..c090605ac2 100644 --- a/lib/tz.c +++ b/lib/tz.c @@ -1,19 +1,16 @@ -/* - * SPDX-FileCopyrightText: 1991 - 1994, Julianne Frances Haugh - * SPDX-FileCopyrightText: 1991 - 1994, Chip Rosenthal - * SPDX-FileCopyrightText: 1996 - 1998, Marek Michałkiewicz - * SPDX-FileCopyrightText: 2003 - 2005, Tomasz Kłoczko - * SPDX-FileCopyrightText: 2007 - 2010, Nicolas François - * - * SPDX-License-Identifier: BSD-3-Clause - */ +// SPDX-FileCopyrightText: 1991-1994, Julianne Frances Haugh +// SPDX-FileCopyrightText: 1991-1994, Chip Rosenthal +// SPDX-FileCopyrightText: 1996-1998, Marek Michałkiewicz +// SPDX-FileCopyrightText: 2003-2005, Tomasz Kłoczko +// SPDX-FileCopyrightText: 2007-2010, Nicolas François +// SPDX-FileCopyrightText: 2026, Alejandro Colomar +// SPDX-License-Identifier: BSD-3-Clause + #include "config.h" #ifndef USE_PAM -#ident "$Id$" - #include #include @@ -28,24 +25,26 @@ * tz - return local timezone name * * tz() determines the name of the local timezone by reading the - * contents of the file named by ``fname''. + * contents of the file named by 'path'. */ -/*@observer@*/const char *tz (const char *fname) +/*@observer@*/ +const char * +tz(const char *path) { FILE *fp; - static char tzbuf[BUFSIZ]; + static char buf[BUFSIZ]; - fp = fopen (fname, "r"); + fp = fopen(path, "r"); if (fp == NULL) return "TZ=UTC"; - if (fgets_a(tzbuf, fp) == NULL) + if (fgets_a(buf, fp) == NULL) goto def; - if (stpsep(tzbuf, "\n") == NULL) + if (stpsep(buf, "\n") == NULL) goto def; fclose(fp); - return tzbuf; + return buf; def: fclose(fp); return "TZ=UTC"; @@ -53,4 +52,3 @@ #else /* !USE_PAM */ extern int ISO_C_forbids_an_empty_translation_unit; #endif /* !USE_PAM */ - From 9091ca85d7fad9fbcb425b32c82852d4d4afd394 Mon Sep 17 00:00:00 2001 From: Alejandro Colomar Date: Wed, 15 Jul 2026 00:17:32 +0200 Subject: [PATCH 07/14] lib/tz.c: tz(): Use a more appropriate buffer size While at it, remove unused includes. Signed-off-by: Alejandro Colomar --- lib/tz.c | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/lib/tz.c b/lib/tz.c index c090605ac2..61077dbbd3 100644 --- a/lib/tz.c +++ b/lib/tz.c @@ -11,11 +11,9 @@ #ifndef USE_PAM +#include #include -#include -#include "defines.h" -#include "getdef.h" #include "io/fgets/fgets.h" #include "prototypes.h" #include "string/strtok/stpsep.h" @@ -32,7 +30,7 @@ const char * tz(const char *path) { FILE *fp; - static char buf[BUFSIZ]; + static char buf[LINE_MAX + 1]; fp = fopen(path, "r"); if (fp == NULL) From 6b884cb2be399237c46dd506f0f29f8b6cea4d8f Mon Sep 17 00:00:00 2001 From: Alejandro Colomar Date: Wed, 15 Jul 2026 00:31:04 +0200 Subject: [PATCH 08/14] lib/tz.c: DEFAULT_TZ: Add macro to centralize "TZ=UTC" Signed-off-by: Alejandro Colomar --- lib/tz.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/lib/tz.c b/lib/tz.c index 61077dbbd3..bbcddb4253 100644 --- a/lib/tz.c +++ b/lib/tz.c @@ -19,6 +19,9 @@ #include "string/strtok/stpsep.h" +#define DEFAULT_TZ "TZ=UTC" + + /* * tz - return local timezone name * @@ -34,7 +37,7 @@ tz(const char *path) fp = fopen(path, "r"); if (fp == NULL) - return "TZ=UTC"; + return DEFAULT_TZ; if (fgets_a(buf, fp) == NULL) goto def; @@ -45,7 +48,7 @@ tz(const char *path) return buf; def: fclose(fp); - return "TZ=UTC"; + return DEFAULT_TZ; } #else /* !USE_PAM */ extern int ISO_C_forbids_an_empty_translation_unit; From bf0f2f7e8edabb41718bc32ee5ab0990fdfa5eb1 Mon Sep 17 00:00:00 2001 From: Alejandro Colomar Date: Tue, 14 Jul 2026 15:05:18 +0200 Subject: [PATCH 09/14] src/ch[g]passwd.c: Fail as early as possible This minimizes the risk of misbehaving. Fixes: 45c6603cc86c (2007-10-07; "[svn-upgrade] Integrating new upstream version, shadow (19990709)") Signed-off-by: Alejandro Colomar --- src/chgpasswd.c | 32 +++++++++++++------------------- src/chpasswd.c | 49 +++++++++++++++++++++---------------------------- 2 files changed, 34 insertions(+), 47 deletions(-) diff --git a/src/chgpasswd.c b/src/chgpasswd.c index e77ecc6ac7..6fbf42cd54 100644 --- a/src/chgpasswd.c +++ b/src/chgpasswd.c @@ -348,7 +348,6 @@ int main (int argc, char **argv) const struct group *gr; struct group newgr; - bool errors = false; intmax_t line = 0; struct option_flags flags = {.chroot = false}; bool process_selinux; @@ -389,8 +388,7 @@ int main (int argc, char **argv) if (stpsep(buf, "\n") == NULL) { fprintf (stderr, _("%s: line %jd: line too long\n"), Prog, line); - errors = true; - continue; + goto fail; } /* @@ -408,8 +406,7 @@ int main (int argc, char **argv) fprintf (stderr, _("%s: line %jd: missing new password\n"), Prog, line); - errors = true; - continue; + goto fail; } newpwd = cp; if ( (!eflg) @@ -452,8 +449,7 @@ int main (int argc, char **argv) fprintf (stderr, _("%s: line %jd: group '%s' does not exist\n"), Prog, line, name); - errors = true; - continue; + goto fail; } #ifdef SHADOWGRP if (is_shadow_grp) { @@ -512,8 +508,7 @@ int main (int argc, char **argv) fprintf (stderr, _("%s: line %jd: failed to prepare the new %s entry '%s'\n"), Prog, line, sgr_dbname (), newsg.sg_namp); - errors = true; - continue; + goto fail; } } if ( (NULL == sg) @@ -524,12 +519,18 @@ int main (int argc, char **argv) fprintf (stderr, _("%s: line %jd: failed to prepare the new %s entry '%s'\n"), Prog, line, gr_dbname (), newgr.gr_name); - errors = true; - continue; + goto fail; } } } + close_files (&flags); + + nscd_flush_cache ("group"); + sssd_flush_cache (SSSD_DB_GROUP); + + return (0); +fail: /* * Any detected errors will cause the entire set of changes to be * aborted. Unlocking the group file will cause all of the @@ -537,17 +538,10 @@ int main (int argc, char **argv) * changes to be written out all at once, and then unlocked * afterwards. */ - if (errors) { + { fprintf (stderr, _("%s: error detected, changes ignored\n"), Prog); fail_exit (1, process_selinux); } - - close_files (&flags); - - nscd_flush_cache ("group"); - sssd_flush_cache (SSSD_DB_GROUP); - - return (0); } diff --git a/src/chpasswd.c b/src/chpasswd.c index 9c2c5e7901..bf37156904 100644 --- a/src/chpasswd.c +++ b/src/chpasswd.c @@ -374,7 +374,6 @@ int main (int argc, char **argv) bool use_pam = true; #endif /* USE_PAM */ - bool errors = false; intmax_t line = 0; struct option_flags flags = {.chroot = false, .prefix = false}; bool process_selinux; @@ -439,8 +438,7 @@ int main (int argc, char **argv) fprintf (stderr, _("%s: line %jd: line too long\n"), Prog, line); - errors = true; - continue; + goto fail; } } @@ -459,8 +457,7 @@ int main (int argc, char **argv) fprintf (stderr, _("%s: line %jd: missing new password\n"), Prog, line); - errors = true; - continue; + goto fail; } newpwd = cp; @@ -470,7 +467,7 @@ int main (int argc, char **argv) fprintf (stderr, _("%s: (line %jd, user %s) password not changed\n"), Prog, line, name); - errors = true; + goto fail; } } else #endif /* USE_PAM */ @@ -486,8 +483,7 @@ int main (int argc, char **argv) fprintf (stderr, _("%s: (line %jd, user %s) invalid password hash\n"), Prog, line, name); - errors = true; - continue; + goto fail; } } const struct spwd *sp; @@ -514,8 +510,7 @@ int main (int argc, char **argv) fprintf (stderr, _("%s: line %jd: user '%s' does not exist\n"), Prog, line, name); - errors = true; - continue; + goto fail; } if (is_shadow_pwd) { /* The shadow entry should be updated if the @@ -580,8 +575,7 @@ int main (int argc, char **argv) fprintf (stderr, _("%s: line %jd: failed to prepare the new %s entry '%s'\n"), Prog, line, spw_dbname (), newsp.sp_namp); - errors = true; - continue; + goto fail; } } if ( (NULL == sp) @@ -590,13 +584,25 @@ int main (int argc, char **argv) fprintf (stderr, _("%s: line %jd: failed to prepare the new %s entry '%s'\n"), Prog, line, pw_dbname (), newpw.pw_name); - errors = true; - continue; + goto fail; } } } } +#ifdef USE_PAM + if (!use_pam) +#endif /* USE_PAM */ + { + /* Save the changes */ + close_files (&flags); + } + + nscd_flush_cache ("passwd"); + sssd_flush_cache (SSSD_DB_PASSWD); + + return (0); +fail: /* * Any detected errors will cause the entire set of changes to be * aborted. Unlocking the password file will cause all of the @@ -607,7 +613,7 @@ int main (int argc, char **argv) * With PAM, it is not possible to delay the update of the * password database. */ - if (errors) { + { #ifdef USE_PAM if (!use_pam) #endif /* USE_PAM */ @@ -618,18 +624,5 @@ int main (int argc, char **argv) } fail_exit (1, process_selinux); } - -#ifdef USE_PAM - if (!use_pam) -#endif /* USE_PAM */ - { - /* Save the changes */ - close_files (&flags); - } - - nscd_flush_cache ("passwd"); - sssd_flush_cache (SSSD_DB_PASSWD); - - return (0); } From 7c9c05248bb0c93e4f93e2aef44dd710ba8e0bf5 Mon Sep 17 00:00:00 2001 From: Alejandro Colomar Date: Tue, 14 Jul 2026 19:36:41 +0200 Subject: [PATCH 10/14] src/ch[g]passwd.c: Reduce indentation This is a clean-up after the last commit. Signed-off-by: Alejandro Colomar --- src/chgpasswd.c | 7 ++----- src/chpasswd.c | 14 +++++--------- 2 files changed, 7 insertions(+), 14 deletions(-) diff --git a/src/chgpasswd.c b/src/chgpasswd.c index 6fbf42cd54..4df7d2cc0c 100644 --- a/src/chgpasswd.c +++ b/src/chgpasswd.c @@ -538,10 +538,7 @@ int main (int argc, char **argv) * changes to be written out all at once, and then unlocked * afterwards. */ - { - fprintf (stderr, - _("%s: error detected, changes ignored\n"), Prog); - fail_exit (1, process_selinux); - } + fprintf(stderr, "%s: %s\n", Prog, _("error detected, changes ignored\n")); + fail_exit(1, process_selinux); } diff --git a/src/chpasswd.c b/src/chpasswd.c index bf37156904..aa938c1e96 100644 --- a/src/chpasswd.c +++ b/src/chpasswd.c @@ -613,16 +613,12 @@ int main (int argc, char **argv) * With PAM, it is not possible to delay the update of the * password database. */ - { #ifdef USE_PAM - if (!use_pam) -#endif /* USE_PAM */ - { - fprintf (stderr, - _("%s: error detected, changes ignored\n"), - Prog); - } - fail_exit (1, process_selinux); + if (!use_pam) +#endif + { + fprintf(stderr, _("%s: error detected, changes ignored\n"), Prog); } + fail_exit(1, process_selinux); } From 6a39bcb4ec1f4cbc1cc48a0bc5d17af777e4ad86 Mon Sep 17 00:00:00 2001 From: Alejandro Colomar Date: Tue, 14 Jul 2026 19:31:06 +0200 Subject: [PATCH 11/14] src/chpasswd.c: Use 'continue' to avoid an else and a block Signed-off-by: Alejandro Colomar --- src/chpasswd.c | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/chpasswd.c b/src/chpasswd.c index aa938c1e96..95c5646898 100644 --- a/src/chpasswd.c +++ b/src/chpasswd.c @@ -469,9 +469,9 @@ int main (int argc, char **argv) Prog, line, name); goto fail; } - } else + continue; + } #endif /* USE_PAM */ - { /* * Prevent adding a non valid hash to /etc/shadow and @@ -587,7 +587,6 @@ int main (int argc, char **argv) goto fail; } } - } } #ifdef USE_PAM From d55c1ea591eba066ad1c53def332f149b3ff5649 Mon Sep 17 00:00:00 2001 From: Alejandro Colomar Date: Tue, 14 Jul 2026 19:33:16 +0200 Subject: [PATCH 12/14] src/chpasswd.c: Don't read input if we'll fail immediately Signed-off-by: Alejandro Colomar --- src/chpasswd.c | 6 ------ 1 file changed, 6 deletions(-) diff --git a/src/chpasswd.c b/src/chpasswd.c index 95c5646898..5eb4283eec 100644 --- a/src/chpasswd.c +++ b/src/chpasswd.c @@ -429,12 +429,6 @@ int main (int argc, char **argv) line++; if (stpsep(buf, "\n") == NULL) { if (feof (stdin) == 0) { - // Drop all remaining characters on this line. - while (fgets_a(buf, stdin) != NULL) { - if (strchr(buf, '\n')) - break; - } - fprintf (stderr, _("%s: line %jd: line too long\n"), Prog, line); From 284fbd99ea0cbec8a463bb2e69ebddbf12691cb2 Mon Sep 17 00:00:00 2001 From: Alejandro Colomar Date: Tue, 14 Jul 2026 23:35:44 +0200 Subject: [PATCH 13/14] lib/limits.c: Use streq(,"\n") after fgets(3) The '\n' necessarily ends the string, so strprefix() makes little sense. Also, while at it, use strspn(3) instead of strprefix() for '#'. Signed-off-by: Alejandro Colomar --- lib/limits.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/limits.c b/lib/limits.c index 3fabe54d3a..63ea4e35e1 100644 --- a/lib/limits.c +++ b/lib/limits.c @@ -363,9 +363,9 @@ static int setup_user_limits (const char *uname) * FIXME: a better (smarter) checking should be done */ while (fgets_a(buf, fil) != NULL) { - if (strprefix(buf, "#") || strprefix(buf, "\n")) { + if (strspn(buf, "#") || streq(buf, "\n")) continue; - } + memzero_a(tempbuf); /* a valid line should have a username, then spaces, * then limits From a2b5d9ea02194ef28f5dd9c9a445b22fb0565f67 Mon Sep 17 00:00:00 2001 From: Alejandro Colomar Date: Tue, 14 Jul 2026 19:35:17 +0200 Subject: [PATCH 14/14] lib/, src/: Reject non-text files Files that don't end in a newline character are not POSIX text files, and thus invalid input. Let's reject them. Signed-off-by: Alejandro Colomar --- lib/getdef.c | 9 +++++---- lib/hushed.c | 5 ++++- lib/limits.c | 6 +++++- lib/port.c | 8 ++++++-- lib/user_busy.c | 4 ++++ src/chgpasswd.c | 3 +-- src/chpasswd.c | 8 ++------ src/newusers.c | 5 ++--- src/suauth.c | 8 ++++---- src/useradd.c | 23 ++++++++++------------- 10 files changed, 43 insertions(+), 36 deletions(-) diff --git a/lib/getdef.c b/lib/getdef.c index e85e0a4460..30a413e2d7 100644 --- a/lib/getdef.c +++ b/lib/getdef.c @@ -559,11 +559,12 @@ static void def_load (void) * Go through all of the lines in the file. */ while (fgets_a(buf, fp) != NULL) { + if (stpsep(buf, "\n") == NULL) { + SYSLOG(LOG_CRIT, "%s: %s", def_fname, _("Non-text file.")); + exit(EXIT_FAILURE); + } - /* - * Trim trailing whitespace. - */ - stpcpy(stprspn(buf, " \t\n"), ""); + stpcpy(stprspn(buf, " \t"), ""); /* * Break the line into two fields. diff --git a/lib/hushed.c b/lib/hushed.c index d88549a647..d021268196 100644 --- a/lib/hushed.c +++ b/lib/hushed.c @@ -76,7 +76,10 @@ bool hushed (const char *username) return false; } for (found = false; !found && (fgets_a(buf, fp) != NULL);) { - stpsep(buf, "\n"); + if (stpsep(buf, "\n") == NULL) { + fclose(fp); + return false; + } found = streq(buf, pw->pw_shell) || streq(buf, pw->pw_name); } diff --git a/lib/limits.c b/lib/limits.c index 63ea4e35e1..659829b958 100644 --- a/lib/limits.c +++ b/lib/limits.c @@ -363,7 +363,11 @@ static int setup_user_limits (const char *uname) * FIXME: a better (smarter) checking should be done */ while (fgets_a(buf, fil) != NULL) { - if (strspn(buf, "#") || streq(buf, "\n")) + if (stpsep(buf, "\n") == NULL) { + fclose(fil); + return 0; + } + if (strspn(buf, "#") || streq(buf, "")) continue; memzero_a(tempbuf); diff --git a/lib/port.c b/lib/port.c index cde50b27e2..bb0035f449 100644 --- a/lib/port.c +++ b/lib/port.c @@ -145,11 +145,15 @@ getportent(void) errno = saveerr; return NULL; } + + if (stpsep(buf, "\n") == NULL) { + errno = EINVAL; + return NULL; + } + if (strprefix(buf, "#")) goto next; - stpsep(buf, "\n"); - if (strsep2arr_a(buf, ":", fields) == -1) goto next; diff --git a/lib/user_busy.c b/lib/user_busy.c index 2f21ca0993..c4bea49e6d 100644 --- a/lib/user_busy.c +++ b/lib/user_busy.c @@ -130,6 +130,9 @@ static int check_status (const char *name, uid_t uid, pid_t pid, pid_t tid) return 0; } while (fgets_a(line, sfile) != NULL) { + if (stpsep(line, "\n") == NULL) + goto nontext; + if (strprefix(line, "Uid:\t")) { unsigned long ruid, euid, suid; @@ -158,6 +161,7 @@ static int check_status (const char *name, uid_t uid, pid_t pid, pid_t tid) return 0; } } +nontext: (void) fclose (sfile); return 0; } diff --git a/src/chgpasswd.c b/src/chgpasswd.c index 4df7d2cc0c..7626f637d6 100644 --- a/src/chgpasswd.c +++ b/src/chgpasswd.c @@ -386,8 +386,7 @@ int main (int argc, char **argv) while (fgets_a(buf, stdin) != NULL) { line++; if (stpsep(buf, "\n") == NULL) { - fprintf (stderr, _("%s: line %jd: line too long\n"), - Prog, line); + fprintf(stderr, "%s: %jd: %s\n", Prog, line, _("Non-text file.")); goto fail; } diff --git a/src/chpasswd.c b/src/chpasswd.c index 5eb4283eec..f0897d32b9 100644 --- a/src/chpasswd.c +++ b/src/chpasswd.c @@ -428,12 +428,8 @@ int main (int argc, char **argv) line++; if (stpsep(buf, "\n") == NULL) { - if (feof (stdin) == 0) { - fprintf (stderr, - _("%s: line %jd: line too long\n"), - Prog, line); - goto fail; - } + fprintf(stderr, "%s: %jd: %s\n", Prog, line, _("Non-text file.")); + goto fail; } /* diff --git a/src/newusers.c b/src/newusers.c index b316d8b5ca..7f7831fbe5 100644 --- a/src/newusers.c +++ b/src/newusers.c @@ -1025,9 +1025,8 @@ int main (int argc, char **argv) */ while (fgets_a(buf, stdin) != NULL) { line++; - if (stpsep(buf, "\n") == NULL && feof(stdin) == 0) { - fprintf (stderr, _("%s: line %jd: line too long\n"), - Prog, line); + if (stpsep(buf, "\n") == NULL) { + fprintf(stderr, "%s: %jd: %s\n", Prog, line, _("Non-text file.")); fail_exit (EXIT_FAILURE, process_selinux); } diff --git a/src/suauth.c b/src/suauth.c index 5d55484ce9..0894a1d8c6 100644 --- a/src/suauth.c +++ b/src/suauth.c @@ -79,10 +79,10 @@ check_su_auth(const char *actual_id, const char *wanted_id, bool su_to_root) lines++; if (stpsep(temp, "\n") == NULL) { - SYSLOG(LOG_ERR, - "%s, line %jd: line too long or missing newline", - SUAUTHFILE, lines); - continue; + fprintf(stderr, "%s:%jd: %s\n", SUAUTHFILE, lines, _("Non-text file.")); + SYSLOG(LOG_ERR, "%s:%jd: Non-text file.", SUAUTHFILE, lines); + fclose(authfile_fd); + return DENY; } stpcpy(stprspn(temp, " \t"), ""); diff --git a/src/useradd.c b/src/useradd.c index 81798b6af5..4e53b0c3b9 100644 --- a/src/useradd.c +++ b/src/useradd.c @@ -358,7 +358,10 @@ get_defaults(const struct option_flags *flags) * values are used, everything else can be ignored. */ while (fgets_a(buf, fp) != NULL) { - stpsep(buf, "\n"); + if (stpsep(buf, "\n") == NULL) { + fprintf(stderr, "%s: %s: %s\n", Prog, default_file, _("Non-text file.")); + goto nontext; + } cp = stpsep(buf, "="); if (NULL == cp) @@ -487,8 +490,9 @@ get_defaults(const struct option_flags *flags) def_log_init = xstrdup(ccp); } } +nontext: (void) fclose (fp); - getdef_err: +getdef_err: if (prefix[0]) { free(default_file); } @@ -605,17 +609,10 @@ set_defaults(void) char *val; if (stpsep(buf, "\n") == NULL) { - /* A line which does not end with \n is only valid - * at the end of the file. - */ - if (feof (ifp) == 0) { - fprintf (stderr, - _("%s: line too long in %s: %s..."), - Prog, default_file, buf); - fclose(ifp); - fclose(ofp); - goto err_free_def; - } + fprintf(stderr, "%s: %s: %s\n", Prog, default_file, _("Non-text file.")); + fclose(ifp); + fclose(ofp); + goto err_free_def; } val = stpsep(buf, "=");