Skip to content

Commit cb6ea72

Browse files
committed
visudo: avoid potential TOCTOU issue by keeping the temporary file open.
If visudo is used to edit a file in a directory writable by an unprivileged user, the temporary file could be replaced by the user (potentially with a symbolic link) between when the file is edited and when it is installed. This is not a problem when visudo is used to edit the system sudoers file. Reported by Stack Security Intelligence
1 parent bcecbce commit cb6ea72

1 file changed

Lines changed: 91 additions & 29 deletions

File tree

‎plugins/sudoers/visudo.c‎

Lines changed: 91 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -69,11 +69,15 @@ struct sudoersfile {
6969
TAILQ_ENTRY(sudoersfile) entries;
7070
char *opath; /* original path we opened */
7171
char *dpath; /* destination path to write to */
72+
const char *dbase; /* destination file basename */
7273
char *tpath; /* editor temporary file path */
74+
const char *tbase; /* editor temporary file basename */
7375
bool created; /* true if visudo created a new sudoers file */
7476
bool modified; /* true if the user modified the file */
7577
bool doedit; /* true when editing (not just checking) sudoers */
7678
int fd; /* fd of the original file (if it exists) */
79+
int tfd; /* fd of the temporary file */
80+
int dfd; /* fd of the destination directory */
7781
int errorline; /* line number when there is a syntax error */
7882
};
7983
TAILQ_HEAD(sudoersfile_list, sudoersfile);
@@ -479,7 +483,6 @@ static bool
479483
edit_sudoers(struct sudoersfile *sp, char *editor, int editor_argc,
480484
char **editor_argv, int lineno)
481485
{
482-
int tfd = -1; /* sudoers temp file descriptor */
483486
bool modified; /* was the file modified? */
484487
int ac; /* argument count */
485488
char linestr[64]; /* string version of lineno */
@@ -503,9 +506,10 @@ edit_sudoers(struct sudoersfile *sp, char *editor, int editor_argc,
503506
sudo_warnx(U_("%s: %s"), __func__, U_("unable to allocate memory"));
504507
goto done;
505508
}
506-
tfd = open(sp->tpath, O_WRONLY|O_CREAT|O_TRUNC|O_NOFOLLOW,
509+
sp->tbase = sudo_basename(sp->tpath);
510+
sp->tfd = openat(sp->dfd, sp->tbase, O_RDWR|O_CREAT|O_TRUNC|O_NOFOLLOW,
507511
S_IRUSR|S_IWUSR);
508-
if (tfd < 0) {
512+
if (sp->tfd == -1) {
509513
sudo_warn("%s", sp->tpath);
510514
goto done;
511515
}
@@ -515,9 +519,12 @@ edit_sudoers(struct sudoersfile *sp, char *editor, int editor_argc,
515519
char buf[4096], lastch = '\0';
516520
ssize_t nread;
517521

518-
(void) lseek(sp->fd, (off_t)0, SEEK_SET);
522+
if (lseek(sp->fd, (off_t)0, SEEK_SET) == -1) {
523+
sudo_warn("lseek");
524+
goto done;
525+
}
519526
while ((nread = read(sp->fd, buf, sizeof(buf))) > 0) {
520-
if (write(tfd, buf, (size_t)nread) != nread) {
527+
if (write(sp->tfd, buf, (size_t)nread) != nread) {
521528
sudo_warn("%s", U_("write error"));
522529
goto done;
523530
}
@@ -531,18 +538,16 @@ edit_sudoers(struct sudoersfile *sp, char *editor, int editor_argc,
531538
/* Add missing newline at EOF if needed. */
532539
if (lastch != '\n') {
533540
lastch = '\n';
534-
if (write(tfd, &lastch, 1) != 1) {
541+
if (write(sp->tfd, &lastch, 1) != 1) {
535542
sudo_warn("%s", U_("write error"));
536543
goto done;
537544
}
538545
}
539546
}
540-
(void) close(tfd);
541-
tfd = -1;
542547
}
543548
times[0].tv_sec = times[1].tv_sec = orig_mtim.tv_sec;
544549
times[0].tv_nsec = times[1].tv_nsec = orig_mtim.tv_nsec;
545-
(void) utimensat(AT_FDCWD, sp->tpath, times, 0);
550+
(void) futimens(sp->tfd, times);
546551

547552
/* Disable +lineno if editor doesn't support it. */
548553
if (lineno > 0 && !editor_supports_plus(editor))
@@ -580,7 +585,7 @@ edit_sudoers(struct sudoersfile *sp, char *editor, int editor_argc,
580585
/*
581586
* Check for zero length sudoers file.
582587
*/
583-
if (stat(sp->tpath, &sb) == -1) {
588+
if (fstat(sp->tfd, &sb) == -1) {
584589
sudo_warnx(U_("unable to stat temporary file (%s), %s unchanged"),
585590
sp->tpath, sp->opath);
586591
goto done;
@@ -624,15 +629,13 @@ edit_sudoers(struct sudoersfile *sp, char *editor, int editor_argc,
624629
*/
625630
if (sp->created && orig_size == 0) {
626631
if (sp == TAILQ_FIRST(&sudoerslist))
627-
unlink(sp->dpath);
632+
unlinkat(sp->dfd, sp->dbase, 0);
628633
}
629634
sudo_warnx(U_("%s unchanged"), sp->tpath);
630635
}
631636

632637
ret = true;
633638
done:
634-
if (tfd != -1)
635-
close(tfd);
636639
debug_return_bool(ret);
637640
}
638641

@@ -665,6 +668,7 @@ reparse_sudoers(struct sudoers_context *ctx, char *editor, int editor_argc,
665668
{
666669
struct sudoersfile *sp, *last;
667670
FILE *fp;
671+
int fd;
668672
bool ret = false;
669673
int ch, oldlocale;
670674
debug_decl(reparse_sudoers, SUDOERS_DEBUG_UTIL);
@@ -675,10 +679,13 @@ reparse_sudoers(struct sudoers_context *ctx, char *editor, int editor_argc,
675679
errors = 0;
676680
while ((sp = TAILQ_FIRST(&sudoerslist)) != NULL) {
677681
last = TAILQ_LAST(&sudoerslist, sudoersfile_list);
678-
fp = fopen(sp->tpath, "r+");
679-
if (fp == NULL) {
682+
fd = dup(sp->tfd);
683+
if (fd == -1 || lseek(fd, (off_t)0, SEEK_SET) == -1 ||
684+
(fp = fdopen(fd, "r+")) == NULL) {
680685
sudo_warnx(U_("unable to re-open temporary file (%s), %s unchanged."),
681686
sp->tpath, sp->opath);
687+
if (fd != -1)
688+
close(fd);
682689
goto done;
683690
}
684691

@@ -767,8 +774,9 @@ reparse_sudoers(struct sudoers_context *ctx, char *editor, int editor_argc,
767774
static bool
768775
install_sudoers(struct sudoersfile *sp, bool set_owner, bool set_mode)
769776
{
770-
struct stat sb;
777+
struct stat sb, sb2;
771778
bool ret = false;
779+
int result;
772780
debug_decl(install_sudoers, SUDOERS_DEBUG_UTIL);
773781

774782
if (sp->tpath == NULL) {
@@ -780,11 +788,11 @@ install_sudoers(struct sudoersfile *sp, bool set_owner, bool set_mode)
780788
/*
781789
* No changes but fix owner/mode if needed.
782790
*/
783-
(void) unlink(sp->tpath);
791+
(void) unlinkat(sp->dfd, sp->tbase, 0);
784792
if (fstat(sp->fd, &sb) == 0) {
785793
if (set_owner) {
786794
if (sb.st_uid != sudoers_file_uid() || sb.st_gid != sudoers_file_gid()) {
787-
if (chown(sp->opath, sudoers_file_uid(), sudoers_file_gid()) != 0) {
795+
if (fchown(sp->fd, sudoers_file_uid(), sudoers_file_gid()) != 0) {
788796
sudo_warn(U_("unable to set (uid, gid) of %s to (%u, %u)"),
789797
sp->opath, (unsigned int)sudoers_file_uid(),
790798
(unsigned int)sudoers_file_gid());
@@ -793,7 +801,7 @@ install_sudoers(struct sudoersfile *sp, bool set_owner, bool set_mode)
793801
}
794802
if (set_mode) {
795803
if ((sb.st_mode & ACCESSPERMS) != sudoers_file_mode()) {
796-
if (chmod(sp->opath, sudoers_file_mode()) != 0) {
804+
if (fchmod(sp->fd, sudoers_file_mode()) != 0) {
797805
sudo_warn(U_("unable to change mode of %s to 0%o"),
798806
sp->opath, (unsigned int)sudoers_file_mode());
799807
}
@@ -804,6 +812,21 @@ install_sudoers(struct sudoersfile *sp, bool set_owner, bool set_mode)
804812
goto done;
805813
}
806814

815+
/*
816+
* Verify that the temp file on disk matches the fd we have open.
817+
* If these don't match either, the editor didn't re-use the existing
818+
* file or the temporary file was changed outside of the editor.
819+
*/
820+
if (fstat(sp->tfd, &sb) == -1 ||
821+
fstatat(sp->dfd, sp->tbase, &sb2, AT_SYMLINK_NOFOLLOW) == -1) {
822+
sudo_warn(U_("unable to stat %s"), sp->tpath);
823+
goto done;
824+
}
825+
if (sb.st_dev != sb2.st_dev || sb.st_ino != sb2.st_ino) {
826+
sudo_warnx(U_("temporary file changed on disk %s"), sp->tpath);
827+
goto done;
828+
}
829+
807830
/*
808831
* Change mode and ownership of temp file before moving it into place
809832
* to avoid a race condition.
@@ -816,26 +839,26 @@ install_sudoers(struct sudoersfile *sp, bool set_owner, bool set_mode)
816839
}
817840
}
818841
if (set_owner) {
819-
if (chown(sp->tpath, sudoers_file_uid(), sudoers_file_gid()) != 0) {
842+
if (fchown(sp->tfd, sudoers_file_uid(), sudoers_file_gid()) != 0) {
820843
sudo_warn(U_("unable to set (uid, gid) of %s to (%u, %u)"),
821844
sp->tpath, (unsigned int)sudoers_file_uid(),
822845
(unsigned int)sudoers_file_gid());
823846
goto done;
824847
}
825848
} else {
826-
if (chown(sp->tpath, sb.st_uid, sb.st_gid) != 0) {
849+
if (fchown(sp->tfd, sb.st_uid, sb.st_gid) != 0) {
827850
sudo_warn(U_("unable to set (uid, gid) of %s to (%u, %u)"),
828851
sp->tpath, (unsigned int)sb.st_uid, (unsigned int)sb.st_gid);
829852
}
830853
}
831854
if (set_mode) {
832-
if (chmod(sp->tpath, sudoers_file_mode()) != 0) {
855+
if (fchmod(sp->tfd, sudoers_file_mode()) != 0) {
833856
sudo_warn(U_("unable to change mode of %s to 0%o"), sp->tpath,
834857
(unsigned int)sudoers_file_mode());
835858
goto done;
836859
}
837860
} else {
838-
if (chmod(sp->tpath, sb.st_mode & ACCESSPERMS) != 0) {
861+
if (fchmod(sp->tfd, sb.st_mode & ACCESSPERMS) != 0) {
839862
sudo_warn(U_("unable to change mode of %s to 0%o"), sp->tpath,
840863
(unsigned int)(sb.st_mode & ACCESSPERMS));
841864
}
@@ -846,7 +869,12 @@ install_sudoers(struct sudoersfile *sp, bool set_owner, bool set_mode)
846869
* rename(2)'d to sp->dpath. If the rename(2) fails we try using
847870
* mv(1) in case sp->tpath and sp->dpath are on different file systems.
848871
*/
849-
if (rename(sp->tpath, sp->dpath) == 0) {
872+
#ifdef HAVE_RENAMEAT
873+
result = renameat(sp->dfd, sp->tbase, sp->dfd, sp->dbase);
874+
#else
875+
result = rename(sp->tpath, sp->dpath);
876+
#endif
877+
if (result == 0) {
850878
free(sp->tpath);
851879
sp->tpath = NULL;
852880
} else {
@@ -877,6 +905,13 @@ install_sudoers(struct sudoersfile *sp, bool set_owner, bool set_mode)
877905
}
878906
ret = true;
879907
done:
908+
close(sp->fd);
909+
sp->fd = -1;
910+
close(sp->tfd);
911+
sp->tfd = -1;
912+
if (sp->dfd != AT_FDCWD)
913+
close(sp->dfd);
914+
sp->dfd = -1;
880915
debug_return_bool(ret);
881916
}
882917

@@ -1096,6 +1131,8 @@ new_sudoers(const char *path, bool doedit)
10961131
struct stat sb;
10971132
size_t len;
10981133
int fd = -1;
1134+
int dfd = -1;
1135+
char *slash;
10991136
debug_decl(new_sudoersfile, SUDOERS_DEBUG_UTIL);
11001137

11011138
/* We always write to the first file in the colon-separated path. */
@@ -1104,6 +1141,26 @@ new_sudoers(const char *path, bool doedit)
11041141
if (entry == NULL || (entry->dpath = strndup(path, len)) == NULL)
11051142
sudo_fatalx(U_("%s: %s"), __func__, U_("unable to allocate memory"));
11061143

1144+
/*
1145+
* Open parent directory and set basename of dpath.
1146+
* The directory fd will be used for the destination and temporary files.
1147+
*/
1148+
slash = strrchr(entry->dpath, '/');
1149+
if (slash == NULL) {
1150+
/* No parent directory, e.g. "visudo -f sudoers" */
1151+
dfd = AT_FDCWD;
1152+
entry->dbase = entry->dpath;
1153+
} else {
1154+
*slash = '\0';
1155+
dfd = open(entry->dpath, O_RDONLY|O_DIRECTORY);
1156+
*slash = '/';
1157+
entry->dbase = slash + 1;
1158+
}
1159+
if (dfd == -1) {
1160+
sudo_warn("%s", entry->dpath);
1161+
goto bad;
1162+
}
1163+
11071164
/* Open the first file found in the colon-separated path. */
11081165
path_end = path + strlen(path);
11091166
for (cp = sudo_strsplit(path, path_end, ":", &ep);
@@ -1138,7 +1195,7 @@ new_sudoers(const char *path, bool doedit)
11381195
if (fd == -1) {
11391196
if (!checkonly) {
11401197
/* No sudoers file, create the destination file for editing. */
1141-
fd = open(entry->dpath, O_RDWR|O_CREAT|O_EXCL,
1198+
fd = openat(dfd, entry->dbase, O_RDWR|O_CREAT|O_EXCL,
11421199
sudoers_file_mode());
11431200
entry->created = true;
11441201
}
@@ -1157,7 +1214,10 @@ new_sudoers(const char *path, bool doedit)
11571214
goto bad;
11581215
}
11591216
entry->fd = fd;
1217+
entry->tfd = -1;
1218+
entry->dfd = dfd;
11601219
/* entry->tpath = NULL; */
1220+
/* entry->tbase = NULL; */
11611221
/* entry->modified = false; */
11621222
entry->doedit = doedit;
11631223
if (!checkonly && !lock_sudoers(entry))
@@ -1166,6 +1226,8 @@ new_sudoers(const char *path, bool doedit)
11661226
bad:
11671227
if (fd != -1)
11681228
close(fd);
1229+
if (dfd != -1 && dfd != AT_FDCWD)
1230+
close(dfd);
11691231
if (entry->opath != entry->dpath)
11701232
free(entry->opath);
11711233
free(entry->dpath);
@@ -1206,13 +1268,13 @@ open_sudoers(const char *path, char **outfile, bool doedit, bool *keepopen)
12061268
} else {
12071269
/* Already exists, open .tmp version if there is one. */
12081270
if (entry->tpath != NULL) {
1209-
if ((fp = fopen(entry->tpath, "r")) == NULL)
1271+
if ((fp = fdopen(entry->tfd, "r")) == NULL)
12101272
sudo_fatal("%s", entry->tpath);
12111273
} else {
12121274
if ((fp = fdopen(entry->fd, "r")) == NULL)
12131275
sudo_fatal("%s", entry->opath);
1214-
rewind(fp);
12151276
}
1277+
rewind(fp);
12161278
}
12171279
if (keepopen != NULL)
12181280
*keepopen = true;
@@ -1332,12 +1394,12 @@ visudo_cleanup(void)
13321394

13331395
TAILQ_FOREACH(sp, &sudoerslist, entries) {
13341396
if (sp->tpath != NULL)
1335-
(void) unlink(sp->tpath);
1397+
(void) unlinkat(sp->dfd, sp->tbase, 0);
13361398
if (sp->created) {
13371399
struct stat sb;
13381400
if (fstat(sp->fd, &sb) == 0 && sb.st_size == 0) {
13391401
/* Remove newly created zero-length file. */
1340-
(void) unlink(sp->dpath);
1402+
(void) unlinkat(sp->dfd, sp->dbase, 0);
13411403
}
13421404
}
13431405
}

0 commit comments

Comments
 (0)