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