698cd1
From 613a8053dbc3ab43cf0cdaf09f207ffdb0b40e08 Mon Sep 17 00:00:00 2001
698cd1
From: Radovan Sroka <rsroka@redhat.com>
698cd1
Date: Wed, 7 Apr 2021 14:43:40 +0200
698cd1
Subject: [PATCH] Fixed bad condition for sesh args
698cd1
698cd1
In selinux_edit_copy_tfiles() when there is only one file and the open()
698cd1
fails then number of arguments is lower than expected.
698cd1
Sudo should return error with or without "Defaults !sudoedit_checkdir" set.
698cd1
698cd1
This was found with regression testing of CVE-2021-23240.
698cd1
698cd1
Signed-off-by: Radovan Sroka <rsroka@redhat.com>
698cd1
---
698cd1
 src/sudo_edit.c | 10 ++++++++--
698cd1
 1 file changed, 8 insertions(+), 2 deletions(-)
698cd1
698cd1
diff --git a/src/sudo_edit.c b/src/sudo_edit.c
698cd1
index 41fc61c3a..15c75d8c4 100644
698cd1
--- a/src/sudo_edit.c
698cd1
+++ b/src/sudo_edit.c
698cd1
@@ -529,6 +529,8 @@ selinux_edit_copy_tfiles(struct command_details *command_details,
698cd1
     if (nfiles < 1)
698cd1
 	debug_return_int(0);
698cd1
 
698cd1
+    const int check_dir = ISSET(command_details->flags, CD_SUDOEDIT_CHECKDIR);
698cd1
+
698cd1
     /* Construct common args for sesh */
698cd1
     sesh_nargs = 5 + (nfiles * 2) + 1;
698cd1
     sesh_args = sesh_ap = reallocarray(NULL, sesh_nargs, sizeof(char *));
698cd1
@@ -538,7 +540,7 @@ selinux_edit_copy_tfiles(struct command_details *command_details,
698cd1
     }
698cd1
     *sesh_ap++ = "sesh";
698cd1
     *sesh_ap++ = "-e";
698cd1
-    if (ISSET(command_details->flags, CD_SUDOEDIT_CHECKDIR)) {
698cd1
+    if (check_dir) {
698cd1
 	if ((user_str = selinux_fmt_sudo_user()) == NULL) {
698cd1
 	    sudo_warnx(U_("%s: %s"), __func__, U_("unable to allocate memory"));
698cd1
 	    goto done;
698cd1
@@ -581,7 +583,11 @@ selinux_edit_copy_tfiles(struct command_details *command_details,
698cd1
     if (tfd != -1)
698cd1
 	close(tfd);
698cd1
 
698cd1
-    if (sesh_ap - sesh_args > 3) {
698cd1
+    /*
698cd1
+     * check dir adds two more args to the array
698cd1
+     */
698cd1
+    if ((!check_dir && sesh_ap - sesh_args > 3)
698cd1
+        || (check_dir && sesh_ap - sesh_args > 5)) {
698cd1
 	/* Run sesh -e 1 <t1> <o1> ... <tn> <on> */
698cd1
 	error = selinux_run_helper(command_details->cred.uid, command_details->cred.gid,
698cd1
 	    command_details->cred.ngroups, command_details->cred.groups, sesh_args,