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