]> granicus.if.org Git - sudo/commitdiff
The condition for adding a missing newline at the end of sudoers
authorTodd C. Miller <Todd.Miller@courtesan.com>
Tue, 18 Aug 2015 14:57:53 +0000 (08:57 -0600)
committerTodd C. Miller <Todd.Miller@courtesan.com>
Tue, 18 Aug 2015 14:57:53 +0000 (08:57 -0600)
was never reached.  Keep track of the last character and write a
newline character if when copying to the temp file.  Found by Radovan
Sroka.

plugins/sudoers/visudo.c

index d36ab5727760439ba9e50d98f89bee3d47ff257b..af0f46779a60b9a1d3181079ad75187d61a33b30 100644 (file)
@@ -415,12 +415,10 @@ edit_sudoers(struct sudoersfile *sp, char *editor, int editor_argc,
     int tfd;                           /* sudoers temp file descriptor */
     bool modified;                     /* was the file modified? */
     int ac;                            /* argument count */
-    char buf[4096];                    /* buffer used for copying files */
     char linestr[64];                  /* string version of lineno */
     struct timespec ts, times[2];      /* time before and after edit */
     struct timespec orig_mtim;         /* starting mtime of sudoers file */
     off_t orig_size;                   /* starting size of sudoers file */
-    ssize_t nread;                     /* number of bytes read */
     struct stat sb;                    /* stat buffer */
     bool rval = false;                 /* return value */
     debug_decl(edit_sudoers, SUDOERS_DEBUG_UTIL)
@@ -440,15 +438,20 @@ edit_sudoers(struct sudoersfile *sp, char *editor, int editor_argc,
 
        /* Copy sp->path -> sp->tpath and reset the mtime. */
        if (orig_size != 0) {
+           char buf[4096], lastch = '\0';
+           ssize_t nread;
+
            (void) lseek(sp->fd, (off_t)0, SEEK_SET);
-           while ((nread = read(sp->fd, buf, sizeof(buf))) > 0)
+           while ((nread = read(sp->fd, buf, sizeof(buf))) > 0) {
                if (write(tfd, buf, nread) != nread)
                    sudo_fatal(U_("write error"));
+               lastch = buf[nread - 1];
+           }
 
            /* Add missing newline at EOF if needed. */
-           if (nread > 0 && buf[nread - 1] != '\n') {
-               buf[0] = '\n';
-               if (write(tfd, buf, 1) != 1)
+           if (lastch != '\n') {
+               lastch = '\n';
+               if (write(tfd, &lastch, 1) != 1)
                    sudo_fatal(U_("write error"));
            }
        }
@@ -460,7 +463,7 @@ edit_sudoers(struct sudoersfile *sp, char *editor, int editor_argc,
 
     /* Disable +lineno if editor doesn't support it. */
     if (lineno > 0 && !editor_supports_plus(editor))
-           lineno = -1;
+       lineno = -1;
 
     /*
      * We pre-allocated 2 extra spaces for "+n filename" in argv.