From 16afe18142bf8e0ba8b315aac10526b8998fa98e Mon Sep 17 00:00:00 2001 From: Iker Pedrosa Date: Fri, 7 Oct 2022 12:36:59 +0200 Subject: [PATCH] Raise limit for passwd and shadow entry length MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Moreover, include checks to prevent writing entries longer than the length limit. Resolves: https://bugzilla.redhat.com/show_bug.cgi?id=1422497 Signed-off-by: Tomáš Mráz Signed-off-by: Iker Pedrosa --- lib/defines.h | 3 +++ lib/pwio.c | 5 ++++- lib/sgetpwent.c | 9 +++++++-- lib/sgetspent.c | 6 +++++- lib/shadowio.c | 4 +++- 5 files changed, 22 insertions(+), 5 deletions(-) diff --git a/lib/defines.h b/lib/defines.h index 6a6bf73e..d01f691e 100644 --- a/lib/defines.h +++ b/lib/defines.h @@ -327,6 +327,9 @@ extern char *strerror (); # endif #endif +/* Maximum length of passwd entry */ +#define PASSWD_ENTRY_MAX_LENGTH 32768 + #ifdef HAVE_SECURE_GETENV # define shadow_getenv(name) secure_getenv(name) # else diff --git a/lib/pwio.c b/lib/pwio.c index 2efecfbf..e59b473c 100644 --- a/lib/pwio.c +++ b/lib/pwio.c @@ -56,7 +56,10 @@ static int passwd_put (const void *ent, FILE * file) || (pw->pw_gid == (gid_t)-1) || (valid_field (pw->pw_gecos, ":\n") == -1) || (valid_field (pw->pw_dir, ":\n") == -1) - || (valid_field (pw->pw_shell, ":\n") == -1)) { + || (valid_field (pw->pw_shell, ":\n") == -1) + || (strlen (pw->pw_name) + strlen (pw->pw_passwd) + + strlen (pw->pw_gecos) + strlen (pw->pw_dir) + + strlen (pw->pw_shell) + 100 > PASSWD_ENTRY_MAX_LENGTH)) { return -1; } diff --git a/lib/sgetpwent.c b/lib/sgetpwent.c index c6e5944c..1c8c63e0 100644 --- a/lib/sgetpwent.c +++ b/lib/sgetpwent.c @@ -16,6 +16,7 @@ #include #include #include "prototypes.h" +#include "shadowlog_internal.h" #define NFIELDS 7 @@ -34,7 +35,7 @@ struct passwd *sgetpwent (const char *buf) { static struct passwd pwent; - static char pwdbuf[1024]; + static char pwdbuf[PASSWD_ENTRY_MAX_LENGTH]; int i; char *cp; char *fields[NFIELDS]; @@ -44,8 +45,12 @@ struct passwd *sgetpwent (const char *buf) * the password structure remain valid. */ - if (strlen (buf) >= sizeof pwdbuf) + if (strlen (buf) >= sizeof pwdbuf) { + fprintf (shadow_logfd, + "%s: Too long passwd entry encountered, file corruption?\n", + shadow_progname); return 0; /* fail if too long */ + } strcpy (pwdbuf, buf); /* diff --git a/lib/sgetspent.c b/lib/sgetspent.c index cbadb7e6..f1d4b201 100644 --- a/lib/sgetspent.c +++ b/lib/sgetspent.c @@ -16,6 +16,7 @@ #include #include "prototypes.h" +#include "shadowlog_internal.h" #include "defines.h" #include #define FIELDS 9 @@ -25,7 +26,7 @@ */ struct spwd *sgetspent (const char *string) { - static char spwbuf[1024]; + static char spwbuf[PASSWD_ENTRY_MAX_LENGTH]; static struct spwd spwd; char *fields[FIELDS]; char *cp; @@ -37,6 +38,9 @@ struct spwd *sgetspent (const char *string) */ if (strlen (string) >= sizeof spwbuf) { + fprintf (shadow_logfd, + "%s: Too long passwd entry encountered, file corruption?\n", + shadow_progname); return 0; /* fail if too long */ } strcpy (spwbuf, string); diff --git a/lib/shadowio.c b/lib/shadowio.c index 34076040..683b6c81 100644 --- a/lib/shadowio.c +++ b/lib/shadowio.c @@ -56,7 +56,9 @@ static int shadow_put (const void *ent, FILE * file) if ( (NULL == sp) || (valid_field (sp->sp_namp, ":\n") == -1) - || (valid_field (sp->sp_pwdp, ":\n") == -1)) { + || (valid_field (sp->sp_pwdp, ":\n") == -1) + || (strlen (sp->sp_namp) + strlen (sp->sp_pwdp) + + 1000 > PASSWD_ENTRY_MAX_LENGTH)) { return -1; }