]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
setpwnam: check if /etc/passwd was fully read
authorTobias Stoeckmann <tobias@stoeckmann.org>
Sun, 4 Jan 2026 15:26:13 +0000 (16:26 +0100)
committerTobias Stoeckmann <tobias@stoeckmann.org>
Sun, 4 Jan 2026 15:27:29 +0000 (16:27 +0100)
An error while reading the existing /etc/passwd should be properly
handled to avoid truncation due to a bad read.

Signed-off-by: Tobias Stoeckmann <tobias@stoeckmann.org>
login-utils/setpwnam.c

index e55fbb2346d3737de0388863f3a3fd179803956c..d2ecb00ea33331e85ae9d05b27a05101216658a5 100644 (file)
@@ -141,6 +141,10 @@ int setpwnam(struct passwd *pwd, const char *prefix)
                /* Nothing in particular happened, copy input to output */
                fputs(linebuf, fp);
        }
+       if (!feof(pwf))
+               goto fail;
+       fclose(pwf);    /* I don't think I want to know if this failed */
+       pwf = NULL;
 
        /* xfmkstemp is too restrictive by default for passwd file */
        if (fchmod(fileno(fp), 0644) < 0)
@@ -150,9 +154,6 @@ int setpwnam(struct passwd *pwd, const char *prefix)
        if (rc != 0)
                goto fail;
 
-       fclose(pwf);    /* I don't think I want to know if this failed */
-       pwf = NULL;
-
        if (!found) {
                errno = ENOENT; /* give me something better */
                goto fail;