15c49f
diff -up ./src/exec_monitor.c.symbolic-link-attack-3 ./src/exec_monitor.c
15c49f
--- ./src/exec_monitor.c.symbolic-link-attack-3	2019-10-28 13:27:39.000000000 +0100
15c49f
+++ ./src/exec_monitor.c	2021-02-02 17:11:32.382020407 +0100
15c49f
@@ -613,7 +613,7 @@ exec_monitor(struct command_details *det
15c49f
 #ifdef HAVE_SELINUX
15c49f
     if (ISSET(details->flags, CD_RBAC_ENABLED)) {
15c49f
         if (selinux_setup(details->selinux_role, details->selinux_type,
15c49f
-            details->tty, io_fds[SFD_SLAVE]) == -1)
15c49f
+            details->tty, io_fds[SFD_SLAVE], true) == -1)
15c49f
             goto bad;
15c49f
     }
15c49f
 #endif
15c49f
diff -up ./src/exec_nopty.c.symbolic-link-attack-3 ./src/exec_nopty.c
15c49f
--- ./src/exec_nopty.c.symbolic-link-attack-3	2019-10-28 13:27:39.000000000 +0100
15c49f
+++ ./src/exec_nopty.c	2021-02-02 17:11:32.382020407 +0100
15c49f
@@ -381,7 +381,7 @@ exec_nopty(struct command_details *detai
15c49f
 #ifdef HAVE_SELINUX
15c49f
     if (ISSET(details->flags, CD_RBAC_ENABLED)) {
15c49f
         if (selinux_setup(details->selinux_role, details->selinux_type,
15c49f
-		details->tty, -1) == -1) {
15c49f
+		details->tty, -1, true) == -1) {
15c49f
 	    cstat->type = CMD_ERRNO;
15c49f
 	    cstat->val = errno;
15c49f
 	    debug_return;
15c49f
diff -up ./src/selinux.c.symbolic-link-attack-3 ./src/selinux.c
15c49f
--- ./src/selinux.c.symbolic-link-attack-3	2019-10-28 13:27:39.000000000 +0100
15c49f
+++ ./src/selinux.c	2021-02-02 17:11:32.382020407 +0100
15c49f
@@ -363,7 +363,7 @@ bad:
15c49f
  */
15c49f
 int
15c49f
 selinux_setup(const char *role, const char *type, const char *ttyn,
15c49f
-    int ptyfd)
15c49f
+    int ptyfd, bool label_tty)
15c49f
 {
15c49f
     int ret = -1;
15c49f
     debug_decl(selinux_setup, SUDO_DEBUG_SELINUX)
15c49f
@@ -392,7 +392,7 @@ selinux_setup(const char *role, const ch
15c49f
     sudo_debug_printf(SUDO_DEBUG_INFO, "%s: new context %s", __func__,
15c49f
 	se_state.new_context);
15c49f
     
15c49f
-    if (relabel_tty(ttyn, ptyfd) == -1) {
15c49f
+    if (label_tty && relabel_tty(ttyn, ptyfd) == -1) {
15c49f
 	sudo_warn(U_("unable to set tty context to %s"), se_state.new_context);
15c49f
 	goto done;
15c49f
     }
15c49f
@@ -408,6 +408,28 @@ done:
15c49f
     debug_return_int(ret);
15c49f
 }
15c49f
 
15c49f
+int
15c49f
+selinux_setcon(void)
15c49f
+{
15c49f
+    debug_decl(selinux_setcon, SUDO_DEBUG_SELINUX);
15c49f
+
15c49f
+    if (setexeccon(se_state.new_context)) {
15c49f
+	sudo_warn(U_("unable to set exec context to %s"), se_state.new_context);
15c49f
+	if (se_state.enforcing)
15c49f
+	    debug_return_int(-1);
15c49f
+    }
15c49f
+
15c49f
+#ifdef HAVE_SETKEYCREATECON
15c49f
+    if (setkeycreatecon(se_state.new_context)) {
15c49f
+	sudo_warn(U_("unable to set key creation context to %s"), se_state.new_context);
15c49f
+	if (se_state.enforcing)
15c49f
+	    debug_return_int(-1);
15c49f
+    }
15c49f
+#endif /* HAVE_SETKEYCREATECON */
15c49f
+
15c49f
+    debug_return_int(0);
15c49f
+}
15c49f
+
15c49f
 void
15c49f
 selinux_execve(int fd, const char *path, char *const argv[], char *envp[],
15c49f
     bool noexec)
15c49f
@@ -424,19 +446,9 @@ selinux_execve(int fd, const char *path,
15c49f
 	debug_return;
15c49f
     }
15c49f
 
15c49f
-    if (setexeccon(se_state.new_context)) {
15c49f
-	sudo_warn(U_("unable to set exec context to %s"), se_state.new_context);
15c49f
-	if (se_state.enforcing)
15c49f
-	    debug_return;
15c49f
-    }
15c49f
-
15c49f
-#ifdef HAVE_SETKEYCREATECON
15c49f
-    if (setkeycreatecon(se_state.new_context)) {
15c49f
-	sudo_warn(U_("unable to set key creation context to %s"), se_state.new_context);
15c49f
-	if (se_state.enforcing)
15c49f
-	    debug_return;
15c49f
-    }
15c49f
-#endif /* HAVE_SETKEYCREATECON */
15c49f
+    /* Set SELinux exec and keycreate contexts. */
15c49f
+    if (selinux_setcon() == -1)
15c49f
+	debug_return;
15c49f
 
15c49f
     /*
15c49f
      * Build new argv with sesh as argv[0].
15c49f
diff -up ./src/sudo.c.symbolic-link-attack-3 ./src/sudo.c
15c49f
--- ./src/sudo.c.symbolic-link-attack-3	2021-02-02 17:12:32.773182386 +0100
15c49f
+++ ./src/sudo.c	2021-02-02 17:12:48.510964009 +0100
15c49f
@@ -971,10 +971,6 @@ run_command(struct command_details *deta
15c49f
     case CMD_WSTATUS:
15c49f
 	/* Command ran, exited or was killed. */
15c49f
 	status = cstat.val;
15c49f
-#ifdef HAVE_SELINUX
15c49f
-	if (ISSET(details->flags, CD_SUDOEDIT_COPY))
15c49f
-	    break;
15c49f
-#endif
15c49f
 	sudo_debug_printf(SUDO_DEBUG_DEBUG,
15c49f
 	    "calling policy close with wait status %d", status);
15c49f
 	policy_close(&policy_plugin, status, 0);
15c49f
diff -up ./src/sudo_edit.c.symbolic-link-attack-3 ./src/sudo_edit.c
15c49f
--- ./src/sudo_edit.c.symbolic-link-attack-3	2021-02-02 17:11:32.380020435 +0100
15c49f
+++ ./src/sudo_edit.c	2021-02-02 17:11:32.382020407 +0100
15c49f
@@ -757,28 +757,54 @@ bad:
15c49f
 
15c49f
 #ifdef HAVE_SELINUX
15c49f
 static int
15c49f
+selinux_run_helper(char *argv[], char *envp[])
15c49f
+{
15c49f
+    int status, ret = SESH_ERR_FAILURE;
15c49f
+    const char *sesh;
15c49f
+    pid_t child, pid;
15c49f
+    debug_decl(selinux_run_helper, SUDO_DEBUG_EDIT);
15c49f
+
15c49f
+    sesh = sudo_conf_sesh_path();
15c49f
+    if (sesh == NULL) {
15c49f
+	sudo_warnx("internal error: sesh path not set");
15c49f
+	debug_return_int(-1);
15c49f
+    }
15c49f
+
15c49f
+    child = sudo_debug_fork();
15c49f
+    switch (child) {
15c49f
+    case -1:
15c49f
+	sudo_warn(U_("unable to fork"));
15c49f
+	break;
15c49f
+    case 0:
15c49f
+	/* child runs sesh in new context */
15c49f
+	if (selinux_setcon() == 0)
15c49f
+	    execve(sesh, argv, envp);
15c49f
+	_exit(SESH_ERR_FAILURE);
15c49f
+    default:
15c49f
+	/* parent waits */
15c49f
+	do {
15c49f
+	    pid = waitpid(child, &status, 0);
15c49f
+	} while (pid == -1 && errno == EINTR);
15c49f
+
15c49f
+	ret = WIFSIGNALED(status) ? SESH_ERR_KILLED : WEXITSTATUS(status);
15c49f
+    }
15c49f
+
15c49f
+    debug_return_int(ret);
15c49f
+}
15c49f
+
15c49f
+static int
15c49f
 selinux_edit_create_tfiles(struct command_details *command_details,
15c49f
     struct tempfile *tf, char *files[], int nfiles)
15c49f
 {
15c49f
     char **sesh_args, **sesh_ap;
15c49f
     int i, rc, sesh_nargs;
15c49f
     struct stat sb;
15c49f
-    struct command_details saved_command_details;
15c49f
     debug_decl(selinux_edit_create_tfiles, SUDO_DEBUG_EDIT)
15c49f
-    
15c49f
-    /* Prepare selinux stuff (setexeccon) */
15c49f
-    if (selinux_setup(command_details->selinux_role,
15c49f
-	command_details->selinux_type, NULL, -1) != 0)
15c49f
-	debug_return_int(-1);
15c49f
 
15c49f
     if (nfiles < 1)
15c49f
 	debug_return_int(0);
15c49f
 
15c49f
     /* Construct common args for sesh */
15c49f
-    memcpy(&saved_command_details, command_details, sizeof(struct command_details));
15c49f
-    command_details->command = _PATH_SUDO_SESH;
15c49f
-    command_details->flags |= CD_SUDOEDIT_COPY;
15c49f
-    
15c49f
     sesh_nargs = 4 + (nfiles * 2) + 1;
15c49f
     sesh_args = sesh_ap = reallocarray(NULL, sesh_nargs, sizeof(char *));
15c49f
     if (sesh_args == NULL) {
15c49f
@@ -791,6 +817,7 @@ 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
@@ -820,8 +847,7 @@ selinux_edit_create_tfiles(struct comman
15c49f
     *sesh_ap = NULL;
15c49f
 
15c49f
     /* Run sesh -e [-h] 0 <o1> <t1> ... <on> <tn> */
15c49f
-    command_details->argv = sesh_args;
15c49f
-    rc = run_command(command_details);
15c49f
+    rc = selinux_run_helper(sesh_args, command_details->envp);
15c49f
     switch (rc) {
15c49f
     case SESH_SUCCESS:
15c49f
 	break;
15c49f
@@ -829,15 +855,12 @@ selinux_edit_create_tfiles(struct comman
15c49f
 	sudo_fatalx(U_("sesh: internal error: odd number of paths"));
15c49f
     case SESH_ERR_NO_FILES:
15c49f
 	sudo_fatalx(U_("sesh: unable to create temporary files"));
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
     }
15c49f
 
15c49f
-    /* Restore saved command_details. */
15c49f
-    command_details->command = saved_command_details.command;
15c49f
-    command_details->flags = saved_command_details.flags;
15c49f
-    command_details->argv = saved_command_details.argv;
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
@@ -858,24 +881,14 @@ selinux_edit_copy_tfiles(struct command_
15c49f
 {
15c49f
     char **sesh_args, **sesh_ap;
15c49f
     int i, rc, sesh_nargs, ret = 1;
15c49f
-    struct command_details saved_command_details;
15c49f
     struct timespec ts;
15c49f
     struct stat sb;
15c49f
     debug_decl(selinux_edit_copy_tfiles, SUDO_DEBUG_EDIT)
15c49f
-    
15c49f
-    /* Prepare selinux stuff (setexeccon) */
15c49f
-    if (selinux_setup(command_details->selinux_role,
15c49f
-	command_details->selinux_type, NULL, -1) != 0)
15c49f
-	debug_return_int(1);
15c49f
 
15c49f
     if (nfiles < 1)
15c49f
 	debug_return_int(0);
15c49f
 
15c49f
     /* Construct common args for sesh */
15c49f
-    memcpy(&saved_command_details, command_details, sizeof(struct command_details));
15c49f
-    command_details->command = _PATH_SUDO_SESH;
15c49f
-    command_details->flags |= CD_SUDOEDIT_COPY;
15c49f
-    
15c49f
     sesh_nargs = 3 + (nfiles * 2) + 1;
15c49f
     sesh_args = sesh_ap = reallocarray(NULL, sesh_nargs, sizeof(char *));
15c49f
     if (sesh_args == NULL) {
15c49f
@@ -913,32 +926,29 @@ selinux_edit_copy_tfiles(struct command_
15c49f
 
15c49f
     if (sesh_ap - sesh_args > 3) {
15c49f
 	/* Run sesh -e 1 <t1> <o1> ... <tn> <on> */
15c49f
-	command_details->argv = sesh_args;
15c49f
-	rc = run_command(command_details);
15c49f
+	rc = selinux_run_helper(sesh_args, command_details->envp);
15c49f
 	switch (rc) {
15c49f
 	case SESH_SUCCESS:
15c49f
 	    ret = 0;
15c49f
 	    break;
15c49f
 	case SESH_ERR_NO_FILES:
15c49f
 	    sudo_warnx(U_("unable to copy temporary files back to their original location"));
15c49f
-	    sudo_warnx(U_("contents of edit session left in %s"), edit_tmpdir);
15c49f
 	    break;
15c49f
 	case SESH_ERR_SOME_FILES:
15c49f
 	    sudo_warnx(U_("unable to copy some of the temporary files back to their original location"));
15c49f
-	    sudo_warnx(U_("contents of edit session left in %s"), edit_tmpdir);
15c49f
+	    break;
15c49f
+	case SESH_ERR_KILLED:
15c49f
+	    sudo_warnx(U_("sesh: killed by a signal"));
15c49f
 	    break;
15c49f
 	default:
15c49f
 	    sudo_warnx(U_("sesh: unknown error %d"), rc);
15c49f
 	    break;
15c49f
 	}
15c49f
+	if (ret != 0)
15c49f
+	    sudo_warnx(U_("contents of edit session left in %s"), edit_tmpdir);
15c49f
     }
15c49f
     free(sesh_args);
15c49f
 
15c49f
-    /* Restore saved command_details. */
15c49f
-    command_details->command = saved_command_details.command;
15c49f
-    command_details->flags = saved_command_details.flags;
15c49f
-    command_details->argv = saved_command_details.argv;
15c49f
-
15c49f
     debug_return_int(ret);
15c49f
 }
15c49f
 #endif /* HAVE_SELINUX */
15c49f
@@ -990,6 +1000,15 @@ sudo_edit(struct command_details *comman
15c49f
 	goto cleanup;
15c49f
     }
15c49f
 
15c49f
+#ifdef HAVE_SELINUX
15c49f
+    /* Compute new SELinux security context. */
15c49f
+    if (ISSET(command_details->flags, CD_RBAC_ENABLED)) {
15c49f
+	if (selinux_setup(command_details->selinux_role,
15c49f
+		command_details->selinux_type, NULL, -1, false) != 0)
15c49f
+	    goto cleanup;
15c49f
+    }
15c49f
+#endif
15c49f
+
15c49f
     /* Copy editor files to temporaries. */
15c49f
     tf = calloc(nfiles, sizeof(*tf));
15c49f
     if (tf == NULL) {
15c49f
@@ -1025,6 +1044,7 @@ sudo_edit(struct command_details *comman
15c49f
     /*
15c49f
      * Run the editor with the invoking user's creds,
15c49f
      * keeping track of the time spent in the editor.
15c49f
+     * XXX - should run editor with user's context
15c49f
      */
15c49f
     if (sudo_gettime_real(&times[0]) == -1) {
15c49f
 	sudo_warn(U_("unable to read the clock"));
15c49f
diff -up ./src/sudo_exec.h.symbolic-link-attack-3 ./src/sudo_exec.h
15c49f
--- ./src/sudo_exec.h.symbolic-link-attack-3	2021-02-02 17:11:32.380020435 +0100
15c49f
+++ ./src/sudo_exec.h	2021-02-02 17:11:32.382020407 +0100
15c49f
@@ -73,6 +73,7 @@
15c49f
  */
15c49f
 #define SESH_SUCCESS	    0		/* successful operation */
15c49f
 #define SESH_ERR_FAILURE    1		/* unspecified error */
15c49f
+#define SESH_ERR_KILLED     2		/* killed by a signal */
15c49f
 #define SESH_ERR_INVALID    30		/* invalid -e arg value */
15c49f
 #define SESH_ERR_BAD_PATHS  31		/* odd number of paths */
15c49f
 #define SESH_ERR_NO_FILES   32		/* copy error, no files copied */
15c49f
diff -up ./src/sudo.h.symbolic-link-attack-3 ./src/sudo.h
15c49f
--- ./src/sudo.h.symbolic-link-attack-3	2019-10-28 13:28:52.000000000 +0100
15c49f
+++ ./src/sudo.h	2021-02-02 17:11:32.382020407 +0100
15c49f
@@ -135,12 +135,11 @@ struct user_details {
15c49f
 #define CD_USE_PTY		0x001000
15c49f
 #define CD_SET_UTMP		0x002000
15c49f
 #define CD_EXEC_BG		0x004000
15c49f
-#define CD_SUDOEDIT_COPY	0x008000
15c49f
-#define CD_SUDOEDIT_FOLLOW	0x010000
15c49f
-#define CD_SUDOEDIT_CHECKDIR	0x020000
15c49f
-#define CD_SET_GROUPS		0x040000
15c49f
-#define CD_LOGIN_SHELL		0x080000
15c49f
-#define CD_OVERRIDE_UMASK	0x100000
15c49f
+#define CD_SUDOEDIT_FOLLOW	0x008000
15c49f
+#define CD_SUDOEDIT_CHECKDIR	0x010000
15c49f
+#define CD_SET_GROUPS		0x020000
15c49f
+#define CD_LOGIN_SHELL		0x040000
15c49f
+#define CD_OVERRIDE_UMASK	0x080000
15c49f
 
15c49f
 struct preserved_fd {
15c49f
     TAILQ_ENTRY(preserved_fd) entries;
15c49f
@@ -240,7 +239,8 @@ int os_init_openbsd(int argc, char *argv
15c49f
 /* selinux.c */
15c49f
 int selinux_restore_tty(void);
15c49f
 int selinux_setup(const char *role, const char *type, const char *ttyn,
15c49f
-    int ttyfd);
15c49f
+    int ttyfd, bool label_tty);
15c49f
+int selinux_setcon(void);
15c49f
 void selinux_execve(int fd, const char *path, char *const argv[],
15c49f
     char *envp[], bool noexec);
15c49f