230a1d
diff -up ./src/copy_file.c.symbolic-link-attack-4 ./src/copy_file.c
230a1d
--- ./src/copy_file.c.symbolic-link-attack-4	2021-02-02 16:35:18.453036846 +0100
230a1d
+++ ./src/copy_file.c	2021-02-02 16:38:09.430731749 +0100
230a1d
@@ -23,6 +23,7 @@
230a1d
 
230a1d
 #include <config.h>
230a1d
 
230a1d
+#include <sys/stat.h>
230a1d
 #include <sys/types.h>
230a1d
 
230a1d
 #include <stdio.h>
230a1d
@@ -126,3 +127,35 @@ write_error:
230a1d
 	debug_return_int(-1);
230a1d
     }
230a1d
 }
230a1d
+
230a1d
+#ifdef HAVE_SELINUX
230a1d
+bool
230a1d
+sudo_check_temp_file(int tfd, const char *tfile, uid_t uid, struct stat *sb)
230a1d
+{
230a1d
+    struct stat sbuf;
230a1d
+    debug_decl(sudo_check_temp_file, SUDO_DEBUG_UTIL);
230a1d
+
230a1d
+    if (sb == NULL)
230a1d
+	sb = &sbu;;
230a1d
+
230a1d
+    if (fstat(tfd, sb) == -1) {
230a1d
+	sudo_warn(U_("unable to stat %s"), tfile);
230a1d
+	debug_return_bool(false);
230a1d
+    }
230a1d
+    if (!S_ISREG(sb->st_mode)) {
230a1d
+	sudo_warnx(U_("%s: not a regular file"), tfile);
230a1d
+	debug_return_bool(false);
230a1d
+    }
230a1d
+    if ((sb->st_mode & ALLPERMS) != (S_IRUSR|S_IWUSR)) {
230a1d
+	sudo_warnx(U_("%s: bad file mode: 0%o"), tfile,
230a1d
+	    (unsigned int)(sb->st_mode & ALLPERMS));
230a1d
+	debug_return_bool(false);
230a1d
+    }
230a1d
+    if (sb->st_uid != uid) {
230a1d
+	sudo_warnx(U_("%s is owned by uid %u, should be %u"),
230a1d
+	    tfile, (unsigned int)sb->st_uid, (unsigned int)uid);
230a1d
+	debug_return_bool(false);
230a1d
+    }
230a1d
+    debug_return_bool(true);
230a1d
+}
230a1d
+#endif /* SELINUX */
230a1d
diff -up ./src/sesh.c.symbolic-link-attack-4 ./src/sesh.c
230a1d
--- ./src/sesh.c.symbolic-link-attack-4	2021-02-02 16:35:18.450036887 +0100
230a1d
+++ ./src/sesh.c	2021-02-02 16:38:52.907146897 +0100
230a1d
@@ -134,7 +134,7 @@ main(int argc, char *argv[], char *envp[
230a1d
 static int
230a1d
 sesh_sudoedit(int argc, char *argv[])
230a1d
 {
230a1d
-    int i, oflags_dst, post, ret = SESH_ERR_FAILURE;
230a1d
+    int i, oflags_src, oflags_dst, post, ret = SESH_ERR_FAILURE;
230a1d
     int fd_src = -1, fd_dst = -1, follow = 0;
230a1d
     ssize_t nread, nwritten;
230a1d
     struct stat sb;
230a1d
@@ -178,10 +178,12 @@ sesh_sudoedit(int argc, char *argv[])
230a1d
 	debug_return_int(SESH_ERR_BAD_PATHS);
230a1d
 
230a1d
     /*
230a1d
-     * Use O_EXCL if we are not in the post editing stage
230a1d
-     * so that it's ensured that the temporary files are
230a1d
-     * created by us and that we are not opening any symlinks.
230a1d
+     * In the pre-editing stage, use O_EXCL to ensure that the temporary
230a1d
+     * files are created by us and that we are not opening any symlinks.
230a1d
+     * In the post-editing stage, use O_NOFOLLOW so we don't follow symlinks
230a1d
+     * when opening the temporary files.
230a1d
      */
230a1d
+    oflags_src = O_RDONLY|(post ? O_NONBLOCK|O_NOFOLLOW : follow);
230a1d
     oflags_dst = O_WRONLY|O_CREAT|(post ? follow : O_EXCL);
230a1d
     for (i = 0; i < argc - 1; i += 2) {
230a1d
 	const char *path_src = argv[i];
230a1d
@@ -191,7 +193,7 @@ sesh_sudoedit(int argc, char *argv[])
230a1d
 	 * doesn't exist, that's OK, we'll create an empty
230a1d
 	 * destination file.
230a1d
 	 */
230a1d
-	if ((fd_src = open(path_src, O_RDONLY|follow, S_IRUSR|S_IWUSR)) < 0) {
230a1d
+	if ((fd_src = open(path_src, oflags_src, S_IRUSR|S_IWUSR)) < 0) {
230a1d
 	    if (errno != ENOENT) {
230a1d
 		sudo_warn("%s", path_src);
230a1d
 		if (post) {
230a1d
@@ -201,6 +203,14 @@ sesh_sudoedit(int argc, char *argv[])
230a1d
 		    goto cleanup_0;
230a1d
 	    }
230a1d
 	}
230a1d
+	if (post) {
230a1d
+	    /* Make sure the temporary file is safe and has the proper owner. */
230a1d
+	    if (!sudo_check_temp_file(fd_src, path_src, geteuid(), &sb)) {
230a1d
+		ret = SESH_ERR_SOME_FILES;
230a1d
+		goto nocleanup;
230a1d
+	    }
230a1d
+	    fcntl(fd_src, F_SETFL, fcntl(fd_src, F_GETFL, 0) & ~O_NONBLOCK);
230a1d
+	}
230a1d
 
230a1d
 	if ((fd_dst = open(path_dst, oflags_dst, post ?
230a1d
 	    (S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH) : (S_IRUSR|S_IWUSR))) < 0) {
230a1d
@@ -218,10 +228,7 @@ sesh_sudoedit(int argc, char *argv[])
230a1d
 	    off_t len_dst = -1;
230a1d
 
230a1d
 	    if (post) {
230a1d
-		if (fstat(fd_src, &sb) != 0) {
230a1d
-		    ret = SESH_ERR_SOME_FILES;
230a1d
-		    goto nocleanup;
230a1d
-		}
230a1d
+		/* sudo_check_temp_file() filled in sb for us. */
230a1d
 		len_src = sb.st_size;
230a1d
 		if (fstat(fd_dst, &sb) != 0) {
230a1d
 		    ret = SESH_ERR_SOME_FILES;
230a1d
diff -up ./src/sudo_edit.c.symbolic-link-attack-4 ./src/sudo_edit.c
230a1d
--- ./src/sudo_edit.c.symbolic-link-attack-4	2021-02-02 16:35:18.452036860 +0100
230a1d
+++ ./src/sudo_edit.c	2021-02-02 16:54:25.943429580 +0100
230a1d
@@ -253,8 +253,10 @@ sudo_edit_mktemp(const char *ofile, char
230a1d
     } else {
230a1d
 	len = asprintf(tfile, "%s/%s.XXXXXXXX", edit_tmpdir, cp);
230a1d
     }
230a1d
-    if (len == -1)
230a1d
-	sudo_fatalx(U_("%s: %s"), __func__, U_("unable to allocate memory"));
230a1d
+    if (len == -1) {
230a1d
+	sudo_warnx(U_("%s: %s"), __func__, U_("unable to allocate memory"));
230a1d
+	debug_return_int(-1);
230a1d
+    }
230a1d
     tfd = mkstemps(*tfile, suff ? strlen(suff) : 0);
230a1d
     sudo_debug_printf(SUDO_DEBUG_INFO|SUDO_DEBUG_LINENO,
230a1d
 	"%s -> %s, fd %d", ofile, *tfile, tfd);
230a1d
@@ -757,7 +759,8 @@ bad:
230a1d
 
230a1d
 #ifdef HAVE_SELINUX
230a1d
 static int
230a1d
-selinux_run_helper(char *argv[], char *envp[])
230a1d
+selinux_run_helper(uid_t uid, gid_t gid, int ngroups, GETGROUPS_T *groups,
230a1d
+    char *const argv[], char *const envp[])
230a1d
 {
230a1d
     int status, ret = SESH_ERR_FAILURE;
230a1d
     const char *sesh;
230a1d
@@ -777,8 +780,10 @@ selinux_run_helper(char *argv[], char *e
230a1d
 	break;
230a1d
     case 0:
230a1d
 	/* child runs sesh in new context */
230a1d
-	if (selinux_setcon() == 0)
230a1d
+	if (selinux_setcon() == 0) {
230a1d
+	    switch_user(uid, gid, ngroups, groups);
230a1d
 	    execve(sesh, argv, envp);
230a1d
+	}
230a1d
 	_exit(SESH_ERR_FAILURE);
230a1d
     default:
230a1d
 	/* parent waits */
230a1d
@@ -797,7 +802,7 @@ selinux_edit_create_tfiles(struct comman
230a1d
     struct tempfile *tf, char *files[], int nfiles)
230a1d
 {
230a1d
     char **sesh_args, **sesh_ap;
230a1d
-    int i, rc, sesh_nargs;
230a1d
+    int i, rc, error, sesh_nargs, ret = -1;
230a1d
     struct stat sb;
230a1d
     debug_decl(selinux_edit_create_tfiles, SUDO_DEBUG_EDIT)
230a1d
 
230a1d
@@ -809,7 +814,7 @@ selinux_edit_create_tfiles(struct comman
230a1d
     sesh_args = sesh_ap = reallocarray(NULL, sesh_nargs, sizeof(char *));
230a1d
     if (sesh_args == NULL) {
230a1d
 	sudo_warnx(U_("%s: %s"), __func__, U_("unable to allocate memory"));
230a1d
-	debug_return_int(-1);
230a1d
+	goto done;
230a1d
     }
230a1d
     *sesh_ap++ = "sesh";
230a1d
     *sesh_ap++ = "-e";
230a1d
@@ -817,7 +822,6 @@ selinux_edit_create_tfiles(struct comman
230a1d
 	*sesh_ap++ = "-h";
230a1d
     *sesh_ap++ = "0";
230a1d
 
230a1d
-    /* XXX - temp files should be created with user's context */
230a1d
     for (i = 0; i < nfiles; i++) {
230a1d
 	char *tfile, *ofile = files[i];
230a1d
 	int tfd;
230a1d
@@ -835,8 +839,7 @@ selinux_edit_create_tfiles(struct comman
230a1d
 	if (tfd == -1) {
230a1d
 	    sudo_warn("mkstemps");
230a1d
 	    free(tfile);
230a1d
-	    free(sesh_args);
230a1d
-	    debug_return_int(-1);
230a1d
+	    goto done;
230a1d
 	}
230a1d
 	/* Helper will re-create temp file with proper security context. */
230a1d
 	close(tfd);
230a1d
@@ -847,8 +850,10 @@ selinux_edit_create_tfiles(struct comman
230a1d
     *sesh_ap = NULL;
230a1d
 
230a1d
     /* Run sesh -e [-h] 0 <o1> <t1> ... <on> <tn> */
230a1d
-    rc = selinux_run_helper(sesh_args, command_details->envp);
230a1d
-    switch (rc) {
230a1d
+    error = selinux_run_helper(command_details->uid, command_details->gid,
230a1d
+	command_details->ngroups, command_details->groups, sesh_args,
230a1d
+	command_details->envp);
230a1d
+    switch (error) {
230a1d
     case SESH_SUCCESS:
230a1d
 	break;
230a1d
     case SESH_ERR_BAD_PATHS:
230a1d
@@ -858,21 +863,34 @@ selinux_edit_create_tfiles(struct comman
230a1d
     case SESH_ERR_KILLED:
230a1d
 	sudo_fatalx(U_("sesh: killed by a signal"));
230a1d
     default:
230a1d
-	sudo_fatalx(U_("sesh: unknown error %d"), rc);
230a1d
+	sudo_fatalx(U_("sesh: unknown error %d"), error);
230a1d
+    goto done;
230a1d
     }
230a1d
 
230a1d
-    /* Chown to user's UID so they can edit the temporary files. */
230a1d
     for (i = 0; i < nfiles; i++) {
230a1d
-	if (chown(tf[i].tfile, user_details.uid, user_details.gid) != 0) {
230a1d
-	    sudo_warn("unable to chown(%s) to %d:%d for editing",
230a1d
-		tf[i].tfile, user_details.uid, user_details.gid);
230a1d
-	}
230a1d
+        int tfd = open(tf[i].tfile, O_RDONLY|O_NONBLOCK|O_NOFOLLOW);
230a1d
+        if (tfd == -1) {
230a1d
+            sudo_warn(U_("unable to open %s"), tf[i].tfile);
230a1d
+            goto done;
230a1d
+        }
230a1d
+        if (!sudo_check_temp_file(tfd, tf[i].tfile, command_details->uid, NULL)) {
230a1d
+            close(tfd);
230a1d
+            goto done;
230a1d
+        }
230a1d
+        if (fchown(tfd, user_details.uid, user_details.gid) != 0) {
230a1d
+            sudo_warn("unable to chown(%s) to %d:%d for editing",
230a1d
+                      tf[i].tfile, user_details.uid, user_details.gid);
230a1d
+            close(tfd);
230a1d
+            goto done;
230a1d
+        }
230a1d
+        close(tfd);
230a1d
     }
230a1d
 
230a1d
+done:
230a1d
     /* Contents of tf will be freed by caller. */
230a1d
     free(sesh_args);
230a1d
 
230a1d
-    return (nfiles);
230a1d
+    debug_return_int(ret);
230a1d
 }
230a1d
 
230a1d
 static int
230a1d
@@ -880,7 +898,8 @@ selinux_edit_copy_tfiles(struct command_
230a1d
     struct tempfile *tf, int nfiles, struct timespec *times)
230a1d
 {
230a1d
     char **sesh_args, **sesh_ap;
230a1d
-    int i, rc, sesh_nargs, ret = 1;
230a1d
+    int i, rc, error, sesh_nargs, ret = 1;
230a1d
+    int tfd = -1;
230a1d
     struct timespec ts;
230a1d
     struct stat sb;
230a1d
     debug_decl(selinux_edit_copy_tfiles, SUDO_DEBUG_EDIT)
230a1d
@@ -901,33 +920,43 @@ selinux_edit_copy_tfiles(struct command_
230a1d
 
230a1d
     /* Construct args for sesh -e 1 */
230a1d
     for (i = 0; i < nfiles; i++) {
230a1d
-	if (stat(tf[i].tfile, &sb) == 0) {
230a1d
-	    mtim_get(&sb, ts);
230a1d
-	    if (tf[i].osize == sb.st_size && sudo_timespeccmp(&tf[i].omtim, &ts, ==)) {
230a1d
-		/*
230a1d
-		 * If mtime and size match but the user spent no measurable
230a1d
-		 * time in the editor we can't tell if the file was changed.
230a1d
-		 */
230a1d
-		if (sudo_timespeccmp(&times[0], &times[1], !=)) {
230a1d
-		    sudo_warnx(U_("%s unchanged"), tf[i].ofile);
230a1d
-		    unlink(tf[i].tfile);
230a1d
-		    continue;
230a1d
-		}
230a1d
+	if (tfd != -1)
230a1d
+	    close(tfd);
230a1d
+	if ((tfd = open(tf[i].tfile, O_RDONLY|O_NONBLOCK|O_NOFOLLOW)) == -1) {
230a1d
+	    sudo_warn(U_("unable to open %s"), tf[i].tfile);
230a1d
+	    continue;
230a1d
+	}
230a1d
+	if (!sudo_check_temp_file(tfd, tf[i].tfile, user_details.uid, &sb))
230a1d
+	    continue;
230a1d
+	mtim_get(&sb, ts);
230a1d
+	if (tf[i].osize == sb.st_size && sudo_timespeccmp(&tf[i].omtim, &ts, ==)) {
230a1d
+	    /*
230a1d
+	     * If mtime and size match but the user spent no measurable
230a1d
+	     * time in the editor we can't tell if the file was changed.
230a1d
+	     */
230a1d
+	    if (sudo_timespeccmp(&times[0], &times[1], !=)) {
230a1d
+		sudo_warnx(U_("%s unchanged"), tf[i].ofile);
230a1d
+		unlink(tf[i].tfile);
230a1d
+		continue;
230a1d
 	    }
230a1d
 	}
230a1d
 	*sesh_ap++ = tf[i].tfile;
230a1d
 	*sesh_ap++ = tf[i].ofile;
230a1d
-	if (chown(tf[i].tfile, command_details->uid, command_details->gid) != 0) {
230a1d
+	if (fchown(tfd, command_details->uid, command_details->gid) != 0) {
230a1d
 	    sudo_warn("unable to chown(%s) back to %d:%d", tf[i].tfile,
230a1d
 		command_details->uid, command_details->gid);
230a1d
 	}
230a1d
     }
230a1d
     *sesh_ap = NULL;
230a1d
+    if (tfd != -1)
230a1d
+	close(tfd);
230a1d
 
230a1d
     if (sesh_ap - sesh_args > 3) {
230a1d
 	/* Run sesh -e 1 <t1> <o1> ... <tn> <on> */
230a1d
-	rc = selinux_run_helper(sesh_args, command_details->envp);
230a1d
-	switch (rc) {
230a1d
+	error = selinux_run_helper(command_details->uid, command_details->gid,
230a1d
+	    command_details->ngroups, command_details->groups, sesh_args,
230a1d
+	    command_details->envp);
230a1d
+	switch (error) {
230a1d
 	case SESH_SUCCESS:
230a1d
 	    ret = 0;
230a1d
 	    break;
230a1d
@@ -941,7 +970,7 @@ selinux_edit_copy_tfiles(struct command_
230a1d
 	    sudo_warnx(U_("sesh: killed by a signal"));
230a1d
 	    break;
230a1d
 	default:
230a1d
-	    sudo_warnx(U_("sesh: unknown error %d"), rc);
230a1d
+	    sudo_warnx(U_("sesh: unknown error %d"), error);
230a1d
 	    break;
230a1d
 	}
230a1d
 	if (ret != 0)
230a1d
@@ -963,7 +992,7 @@ sudo_edit(struct command_details *comman
230a1d
 {
230a1d
     struct command_details saved_command_details;
230a1d
     char **nargv = NULL, **ap, **files = NULL;
230a1d
-    int errors, i, ac, nargc, rc;
230a1d
+    int errors, i, ac, nargc, ret;
230a1d
     int editor_argc = 0, nfiles = 0;
230a1d
     struct timespec times[2];
230a1d
     struct tempfile *tf = NULL;
230a1d
@@ -1058,7 +1087,7 @@ sudo_edit(struct command_details *comman
230a1d
     command_details->ngroups = user_details.ngroups;
230a1d
     command_details->groups = user_details.groups;
230a1d
     command_details->argv = nargv;
230a1d
-    rc = run_command(command_details);
230a1d
+    ret = run_command(command_details);
230a1d
     if (sudo_gettime_real(&times[1]) == -1) {
230a1d
 	sudo_warn(U_("unable to read the clock"));
230a1d
 	goto cleanup;
230a1d
@@ -1080,14 +1109,16 @@ sudo_edit(struct command_details *comman
230a1d
     else
230a1d
 #endif
230a1d
 	errors = sudo_edit_copy_tfiles(command_details, tf, nfiles, times);
230a1d
-    if (errors)
230a1d
-	goto cleanup;
230a1d
+    if (errors) {
230a1d
+        /* Preserve the edited temporary files. */
230a1d
+        ret = W_EXITCODE(1, 0);
230a1d
+    }
230a1d
 
230a1d
     for (i = 0; i < nfiles; i++)
230a1d
 	free(tf[i].tfile);
230a1d
     free(tf);
230a1d
     free(nargv);
230a1d
-    debug_return_int(rc);
230a1d
+    debug_return_int(ret);
230a1d
 
230a1d
 cleanup:
230a1d
     /* Clean up temp files and return. */
230a1d
diff -up ./src/sudo_exec.h.symbolic-link-attack-4 ./src/sudo_exec.h
230a1d
--- ./src/sudo_exec.h.symbolic-link-attack-4	2021-02-02 16:35:18.452036860 +0100
230a1d
+++ ./src/sudo_exec.h	2021-02-02 16:35:18.454036833 +0100
230a1d
@@ -1,7 +1,7 @@
230a1d
 /*
230a1d
  * SPDX-License-Identifier: ISC
230a1d
  *
230a1d
- * Copyright (c) 2010-2016 Todd C. Miller <Todd.Miller@sudo.ws>
230a1d
+ * Copyright (c) 2010-2017, 2020-2021 Todd C. Miller <Todd.Miller@sudo.ws>
230a1d
  *
230a1d
  * Permission to use, copy, modify, and distribute this software for any
230a1d
  * purpose with or without fee is hereby granted, provided that the above
230a1d
@@ -84,9 +84,11 @@
230a1d
  */
230a1d
 struct command_details;
230a1d
 struct command_status;
230a1d
+struct stat;
230a1d
 
230a1d
 /* copy_file.c */
230a1d
 int sudo_copy_file(const char *src, int src_fd, off_t src_len, const char *dst, int dst_fd, off_t dst_len);
230a1d
+bool sudo_check_temp_file(int tfd, const char *tname, uid_t uid, struct stat *sb);
230a1d
 
230a1d
 /* exec.c */
230a1d
 void exec_cmnd(struct command_details *details, int errfd);