Blame SOURCES/0069-setpriv-implement-option-to-set-parent-death-signal.patch

e86b25
From 0b421290e05862e1abbb5a82654bd2de9829dd58 Mon Sep 17 00:00:00 2001
e86b25
From: Patrick Steinhardt <ps@pks.im>
e86b25
Date: Tue, 10 Apr 2018 12:08:21 +0100
e86b25
Subject: [PATCH 69/74] setpriv: implement option to set parent death signal
e86b25
e86b25
When a process uses the syscall `prctl(PR_SET_PDEATHSIG, ...)`, it will
e86b25
get notified with a process-defined signal as soon as its parent process
e86b25
dies. This is for example being used by unshare(1)'s recently added
e86b25
"--kill-child" option, causing the forked child to be killed as soon as
e86b25
unshare itself dies.
e86b25
e86b25
Unfortunately, some LSMs will cause the parent death signal to be reset
e86b25
when a process changes credentials, with the most important ones being
e86b25
SELinux and AppArmor. The following command will thus not work as
e86b25
expected:
e86b25
e86b25
    unshare --fork --kill-child setpriv --reuid user <executable>
e86b25
e86b25
As soon as setpriv changes UID, the parent death signal is cleared and
e86b25
the child will never get signalled when unshare gets killed.
e86b25
e86b25
Add a new option "--pdeathsig keep|clear|<signal>". Setting this flag
e86b25
will cause us to either
e86b25
e86b25
- restore the previously active parent death signal as soon as the
e86b25
  setpriv has applied all credential changes
e86b25
- clear the parent death signal
e86b25
- set the parent death signal to "<signal>"
e86b25
e86b25
Furthermore, print out the currently set signal when dumping process
e86b25
state.
e86b25
e86b25
[kzak@redhat.com: - small changes in codding style]
e86b25
e86b25
Addresses: https://bugzilla.redhat.com/show_bug.cgi?id=1894192
e86b25
Signed-off-by: Patrick Steinhardt <ps@pks.im>
e86b25
Signed-off-by: Karel Zak <kzak@redhat.com>
e86b25
---
e86b25
 sys-utils/setpriv.1 |  6 ++++++
e86b25
 sys-utils/setpriv.c | 49 +++++++++++++++++++++++++++++++++++++++++++++
e86b25
 2 files changed, 55 insertions(+)
e86b25
e86b25
diff --git a/sys-utils/setpriv.1 b/sys-utils/setpriv.1
e86b25
index b900f6e08..f989bf33c 100644
e86b25
--- a/sys-utils/setpriv.1
e86b25
+++ b/sys-utils/setpriv.1
e86b25
@@ -139,6 +139,12 @@ is cleared by
e86b25
 .BR execve (2)
e86b25
 and is therefore not allowed.
e86b25
 .TP
e86b25
+.BR "\-\-pdeathsig keep" | clear | <signal>
e86b25
+Keep, clear or set the parent death signal.  Some LSMs, most notably SELinux and
e86b25
+AppArmor, clear the signal when the process' credentials change.  Using
e86b25
+\fB--pdeathsig keep\fR will restore the parent death signal after changing
e86b25
+credentials to remedy that situation.
e86b25
+.TP
e86b25
 .BI \-\-selinux\-label " label"
e86b25
 Request a particular SELinux transition (using a transition on exec, not
e86b25
 dyntrans).  This will fail and cause
e86b25
diff --git a/sys-utils/setpriv.c b/sys-utils/setpriv.c
e86b25
index 4147978cc..0d3a3b3c9 100644
e86b25
--- a/sys-utils/setpriv.c
e86b25
+++ b/sys-utils/setpriv.c
e86b25
@@ -38,6 +38,7 @@
e86b25
 #include "strutils.h"
e86b25
 #include "xalloc.h"
e86b25
 #include "pathnames.h"
e86b25
+#include "signames.h"
e86b25
 
e86b25
 #ifndef PR_SET_NO_NEW_PRIVS
e86b25
 # define PR_SET_NO_NEW_PRIVS 38
e86b25
@@ -102,6 +103,8 @@ struct privctx {
e86b25
 
e86b25
 	/* securebits */
e86b25
 	int securebits;
e86b25
+	/* parent death signal (<0 clear, 0 nothing, >0 signal) */
e86b25
+	int pdeathsig;
e86b25
 
e86b25
 	/* LSMs */
e86b25
 	const char *selinux_label;
e86b25
@@ -135,6 +138,8 @@ static void __attribute__((__noreturn__)) usage(void)
e86b25
 	fputs(_(" --init-groups               initialize supplementary groups\n"), out);
e86b25
 	fputs(_(" --groups <group,...>        set supplementary groups\n"), out);
e86b25
 	fputs(_(" --securebits <bits>         set securebits\n"), out);
e86b25
+	fputs(_(" --pdeathsig keep|clear|<signame>\n"
e86b25
+	        "                             set or clear parent death signal\n"), out);
e86b25
 	fputs(_(" --selinux-label <label>     set SELinux label\n"), out);
e86b25
 	fputs(_(" --apparmor-profile <pr>     set AppArmor profile\n"), out);
e86b25
 
e86b25
@@ -329,6 +334,24 @@ static void dump_groups(void)
e86b25
 	free(groups);
e86b25
 }
e86b25
 
e86b25
+static void dump_pdeathsig(void)
e86b25
+{
e86b25
+	int pdeathsig;
e86b25
+
e86b25
+	if (prctl(PR_GET_PDEATHSIG, &pdeathsig) != 0) {
e86b25
+		warn(_("get pdeathsig failed"));
e86b25
+		return;
e86b25
+	}
e86b25
+
e86b25
+	printf("Parent death signal: ");
e86b25
+	if (pdeathsig && signum_to_signame(pdeathsig) != NULL)
e86b25
+		printf("%s\n", signum_to_signame(pdeathsig));
e86b25
+	else if (pdeathsig)
e86b25
+		printf("%d\n", pdeathsig);
e86b25
+	else
e86b25
+		printf("[none]\n");
e86b25
+}
e86b25
+
e86b25
 static void dump(int dumplevel)
e86b25
 {
e86b25
 	int x;
e86b25
@@ -392,6 +415,7 @@ static void dump(int dumplevel)
e86b25
 	printf("\n");
e86b25
 
e86b25
 	dump_securebits();
e86b25
+	dump_pdeathsig();
e86b25
 
e86b25
 	if (access(_PATH_SYS_SELINUX, F_OK) == 0)
e86b25
 		dump_label(_("SELinux label"));
e86b25
@@ -438,6 +462,19 @@ static void parse_groups(struct privctx *opts, const char *str)
e86b25
 	free(groups);
e86b25
 }
e86b25
 
e86b25
+static void parse_pdeathsig(struct privctx *opts, const char *str)
e86b25
+{
e86b25
+	if (!strcmp(str, "keep")) {
e86b25
+		if (prctl(PR_GET_PDEATHSIG, &opts->pdeathsig) != 0)
e86b25
+			errx(SETPRIV_EXIT_PRIVERR,
e86b25
+				 _("failed to get parent death signal"));
e86b25
+	} else if (!strcmp(str, "clear")) {
e86b25
+		opts->pdeathsig = -1;
e86b25
+	} else if ((opts->pdeathsig = signame_to_signum(str)) < 0) {
e86b25
+		errx(EXIT_FAILURE, _("unknown signal: %s"), str);
e86b25
+	}
e86b25
+}
e86b25
+
e86b25
 static void do_setresuid(const struct privctx *opts)
e86b25
 {
e86b25
 	uid_t ruid, euid, suid;
e86b25
@@ -711,6 +748,7 @@ int main(int argc, char **argv)
e86b25
 		LISTCAPS,
e86b25
 		CAPBSET,
e86b25
 		SECUREBITS,
e86b25
+		PDEATHSIG,
e86b25
 		SELINUX_LABEL,
e86b25
 		APPARMOR_PROFILE
e86b25
 	};
e86b25
@@ -734,6 +772,7 @@ int main(int argc, char **argv)
e86b25
 		{ "groups",           required_argument, NULL, GROUPS           },
e86b25
 		{ "bounding-set",     required_argument, NULL, CAPBSET          },
e86b25
 		{ "securebits",       required_argument, NULL, SECUREBITS       },
e86b25
+		{ "pdeathsig",        required_argument, NULL, PDEATHSIG,       },
e86b25
 		{ "selinux-label",    required_argument, NULL, SELINUX_LABEL    },
e86b25
 		{ "apparmor-profile", required_argument, NULL, APPARMOR_PROFILE },
e86b25
 		{ "help",             no_argument,       NULL, 'h'              },
e86b25
@@ -844,6 +883,12 @@ int main(int argc, char **argv)
e86b25
 				     _("duplicate --groups option"));
e86b25
 			parse_groups(&opts, optarg);
e86b25
 			break;
e86b25
+		case PDEATHSIG:
e86b25
+			if (opts.pdeathsig)
e86b25
+				errx(EXIT_FAILURE,
e86b25
+				     _("duplicate --keep-pdeathsig option"));
e86b25
+			parse_pdeathsig(&opts, optarg);
e86b25
+			break;
e86b25
 		case LISTCAPS:
e86b25
 			list_caps = 1;
e86b25
 			break;
e86b25
@@ -989,6 +1034,10 @@ int main(int argc, char **argv)
e86b25
 		do_caps(CAP_TYPE_AMBIENT, opts.ambient_caps);
e86b25
 	}
e86b25
 
e86b25
+	/* Clear or set parent death signal */
e86b25
+	if (opts.pdeathsig && prctl(PR_SET_PDEATHSIG, opts.pdeathsig < 0 ? 0 : opts.pdeathsig) != 0)
e86b25
+		err(SETPRIV_EXIT_PRIVERR, _("set parent death signal failed"));
e86b25
+
e86b25
 	execvp(argv[optind], argv + optind);
e86b25
 	errexec(argv[optind]);
e86b25
 }
e86b25
-- 
e86b25
2.31.1
e86b25