Blame SOURCES/libcap-fix-ambient-caps.patch

12fca1
diff --color -ru a/libcap/cap_proc.c b/libcap/cap_proc.c
12fca1
--- a/libcap/cap_proc.c	2021-12-22 12:33:20.739126763 +0100
12fca1
+++ b/libcap/cap_proc.c	2021-12-22 12:33:53.195733115 +0100
12fca1
@@ -406,6 +406,29 @@
12fca1
 }
12fca1
 
12fca1
 /*
12fca1
+ * cap_prctl performs a prctl() 6 argument call on the current
12fca1
+ * thread. Use cap_prctlw() if you want to perform a POSIX semantics
12fca1
+ * prctl() system call.
12fca1
+ */
12fca1
+int cap_prctl(long int pr_cmd, long int arg1, long int arg2,
12fca1
+             long int arg3, long int arg4, long int arg5)
12fca1
+{
12fca1
+    return prctl(pr_cmd, arg1, arg2, arg3, arg4, arg5);
12fca1
+}
12fca1
+
12fca1
+/*
12fca1
+ * cap_prctlw performs a POSIX semantics prctl() call. That is a 6 arg
12fca1
+ * prctl() call that executes on all available threads when libpsx is
12fca1
+ * linked. The suffix 'w' refers to the fact one only ever needs to
12fca1
+ * invoke this is if the call will write some kernel state.
12fca1
+ */
12fca1
+int cap_prctlw(long int pr_cmd, long int arg1, long int arg2,
12fca1
+              long int arg3, long int arg4, long int arg5)
12fca1
+{
12fca1
+    return _libcap_wprctl6(&multithread, pr_cmd, arg1, arg2, arg3, arg4, arg5);
12fca1
+}
12fca1
+
12fca1
+/*
12fca1
  * Some predefined constants
12fca1
  */
12fca1
 #define CAP_SECURED_BITS_BASIC                                 \
12fca1
diff --color -ru a/libcap/include/sys/capability.h b/libcap/include/sys/capability.h
12fca1
--- a/libcap/include/sys/capability.h	2021-02-05 06:52:17.000000000 +0100
12fca1
+++ b/libcap/include/sys/capability.h	2021-12-22 12:33:53.196733134 +0100
12fca1
@@ -175,6 +175,11 @@
12fca1
 extern unsigned cap_get_secbits(void);
12fca1
 extern int cap_set_secbits(unsigned bits);
12fca1
 
12fca1
+extern int cap_prctl(long int pr_cmd, long int arg1, long int arg2,
12fca1
+		    long int arg3, long int arg4, long int arg5);
12fca1
+extern int cap_prctlw(long int pr_cmd, long int arg1, long int arg2,
12fca1
+		     long int arg3, long int arg4, long int arg5);
12fca1
+
12fca1
 extern int cap_setuid(uid_t uid);
12fca1
 extern int cap_setgroups(gid_t gid, size_t ngroups, const gid_t groups[]);
12fca1
 
12fca1
diff --color -ru a/pam_cap/pam_cap.c b/pam_cap/pam_cap.c
12fca1
--- a/pam_cap/pam_cap.c	2021-12-22 12:33:20.740126781 +0100
12fca1
+++ b/pam_cap/pam_cap.c	2021-12-22 12:33:53.196733134 +0100
12fca1
@@ -21,6 +21,7 @@
12fca1
 #include <string.h>
12fca1
 #include <syslog.h>
12fca1
 #include <sys/capability.h>
12fca1
+#include <sys/prctl.h>
12fca1
 #include <sys/types.h>
12fca1
 #include <linux/limits.h>
12fca1
 
12fca1
@@ -33,8 +34,11 @@
12fca1
 
12fca1
 struct pam_cap_s {
12fca1
     int debug;
12fca1
+    int keepcaps;
12fca1
+    int defer;
12fca1
     const char *user;
12fca1
     const char *conf_filename;
12fca1
+    pam_handle_t *pamh;
12fca1
 };
12fca1
 
12fca1
 /*
12fca1
@@ -178,6 +182,33 @@
12fca1
 }
12fca1
 
12fca1
 /*
12fca1
+ * This is the "defer" cleanup function that actually applies the IAB
12fca1
+ * tuple. This happens really late in the PAM session, hopefully after
12fca1
+ * the application has performed its setuid() function.
12fca1
+ */
12fca1
+static void iab_apply(pam_handle_t *pamh, void *data, int error_status)
12fca1
+{
12fca1
+    cap_iab_t iab = data;
12fca1
+    int retval = error_status & ~(PAM_DATA_REPLACE|PAM_DATA_SILENT);
12fca1
+
12fca1
+    data = NULL;
12fca1
+    if (error_status & PAM_DATA_REPLACE) {
12fca1
+	goto done;
12fca1
+    }
12fca1
+
12fca1
+    if (retval != PAM_SUCCESS || !(error_status & PAM_DATA_SILENT)) {
12fca1
+	goto done;
12fca1
+    }
12fca1
+
12fca1
+    if (cap_iab_set_proc(iab) != 0) {
12fca1
+	D(("IAB setting failed"));
12fca1
+    }
12fca1
+
12fca1
+done:
12fca1
+    cap_free(iab);
12fca1
+}
12fca1
+
12fca1
+/*
12fca1
  * Set capabilities for current process to match the current
12fca1
  * permitted+executable sets combined with the configured inheritable
12fca1
  * set.
12fca1
@@ -230,12 +261,21 @@
12fca1
 	goto cleanup_conf;
12fca1
     }
12fca1
 
12fca1
-    if (!cap_iab_set_proc(iab)) {
12fca1
+    if (cs->defer) {
12fca1
+	D(("configured to delay applying IAB"));
12fca1
+	pam_set_data(cs->pamh, "pam_cap_iab", iab, iab_apply);
12fca1
+	iab = NULL;
12fca1
+    } else if (!cap_iab_set_proc(iab)) {
12fca1
 	D(("able to set the IAB [%s] value", conf_caps));
12fca1
 	ok = 1;
12fca1
     }
12fca1
     cap_free(iab);
12fca1
 
12fca1
+    if (cs->keepcaps) {
12fca1
+	D(("setting keepcaps"));
12fca1
+	(void) cap_prctlw(PR_SET_KEEPCAPS, 1, 0, 0, 0, 0);
12fca1
+    }
12fca1
+
12fca1
 cleanup_conf:
12fca1
     memset(conf_caps, 0, conf_caps_length);
12fca1
     _pam_drop(conf_caps);
12fca1
@@ -268,6 +308,10 @@
12fca1
 	    pcs->debug = 1;
12fca1
 	} else if (!strncmp(*argv, "config=", 7)) {
12fca1
 	    pcs->conf_filename = 7 + *argv;
12fca1
+	} else if (!strcmp(*argv, "keepcaps")) {
12fca1
+	    pcs->keepcaps = 1;
12fca1
+	} else if (!strcmp(*argv, "defer")) {
12fca1
+	    pcs->defer = 1;
12fca1
 	} else {
12fca1
 	    _pam_log(LOG_ERR, "unknown option; %s", *argv);
12fca1
 	}
12fca1
@@ -353,6 +397,7 @@
12fca1
 	return PAM_AUTH_ERR;
12fca1
     }
12fca1
 
12fca1
+    pcs.pamh = pamh;
12fca1
     retval = set_capabilities(&pcs;;
12fca1
     memset(&pcs, 0, sizeof(pcs));
12fca1