e96547
diff --color -ru a/libcap/cap_proc.c b/libcap/cap_proc.c
e96547
--- a/libcap/cap_proc.c	2022-01-28 12:42:39.726331628 +0100
e96547
+++ b/libcap/cap_proc.c	2022-01-28 12:44:05.007936110 +0100
e96547
@@ -712,6 +712,10 @@
e96547
     cap_value_t c;
e96547
     int raising = 0;
e96547
 
e96547
+    if (temp == NULL) {
e96547
+	return -1;
e96547
+    }
e96547
+
e96547
     for (i = 0; i < _LIBCAP_CAPABILITY_U32S; i++) {
e96547
 	__u32 newI = iab->i[i];
e96547
 	__u32 oldIP = temp->u[i].flat[CAP_INHERITABLE] |
e96547
diff --color -ru a/libcap/cap_text.c b/libcap/cap_text.c
e96547
--- a/libcap/cap_text.c	2022-01-28 12:42:39.725331609 +0100
e96547
+++ b/libcap/cap_text.c	2022-01-28 12:44:05.008936129 +0100
e96547
@@ -160,6 +160,7 @@
e96547
 	cap_blks = _LINUX_CAPABILITY_U32S_3;
e96547
 	break;
e96547
     default:
e96547
+	cap_free(res);
e96547
 	errno = EINVAL;
e96547
 	return NULL;
e96547
     }
e96547
@@ -398,6 +399,9 @@
e96547
 	for (n = 0; n < cmb; n++) {
e96547
 	    if (getstateflags(caps, n) == t) {
e96547
 	        char *this_cap_name = cap_to_name(n);
e96547
+		if (this_cap_name == NULL) {
e96547
+		    return NULL;
e96547
+		}
e96547
 	        if ((strlen(this_cap_name) + (p - buf)) > CAP_TEXT_SIZE) {
e96547
 		    cap_free(this_cap_name);
e96547
 		    errno = ERANGE;
e96547
@@ -450,6 +454,9 @@
e96547
 	for (n = cmb; n < __CAP_MAXBITS; n++) {
e96547
 	    if (getstateflags(caps, n) == t) {
e96547
 		char *this_cap_name = cap_to_name(n);
e96547
+		if (this_cap_name == NULL) {
e96547
+		    return NULL;
e96547
+		}
e96547
 	        if ((strlen(this_cap_name) + (p - buf)) > CAP_TEXT_SIZE) {
e96547
 		    cap_free(this_cap_name);
e96547
 		    errno = ERANGE;
e96547
@@ -549,6 +556,9 @@
e96547
 cap_iab_t cap_iab_from_text(const char *text)
e96547
 {
e96547
     cap_iab_t iab = cap_iab_init();
e96547
+    if (iab == NULL) {
e96547
+	return iab;
e96547
+    }
e96547
     if (text != NULL) {
e96547
 	unsigned flags;
e96547
 	for (flags = 0; *text; text++) {
e96547
diff --color -ru a/libcap/_makenames.c b/libcap/_makenames.c
e96547
--- a/libcap/_makenames.c	2022-01-28 12:42:39.725331609 +0100
e96547
+++ b/libcap/_makenames.c	2022-01-28 13:07:28.700817691 +0100
e96547
@@ -45,10 +45,14 @@
e96547
 	if (maxcaps <= list[i].index) {
e96547
 	    maxcaps = list[i].index + 1;
e96547
 	}
e96547
-        if (list[i].index >= pointers_avail) {
e96547
+        if (pointers == NULL || list[i].index >= pointers_avail) {
e96547
 	    int was = pointers_avail * sizeof(char *);
e96547
 	    pointers_avail = 2 * list[i].index + 1;
e96547
 	    pointers = recalloc(pointers, was, pointers_avail * sizeof(char *));
e96547
+	    if (pointers == NULL) {
e96547
+		perror("unable to continue");
e96547
+		exit(1);
e96547
+	    }
e96547
         }
e96547
 	pointers[list[i].index] = list[i].name;
e96547
 	int n = strlen(list[i].name);
e96547
diff --color -ru a/pam_cap/pam_cap.c b/pam_cap/pam_cap.c
e96547
--- a/pam_cap/pam_cap.c	2022-01-28 12:42:39.726331628 +0100
e96547
+++ b/pam_cap/pam_cap.c	2022-01-28 12:44:05.009936148 +0100
e96547
@@ -64,6 +64,9 @@
e96547
     }
e96547
 
e96547
     *groups = calloc(ngrps, sizeof(char *));
e96547
+    if (*groups == NULL) {
e96547
+	return -1;
e96547
+    }
e96547
     int g_n = 0, i;
e96547
     for (i = 0; i < ngrps; i++) {
e96547
 	const struct group *g = getgrgid(grps[i]);
e96547
@@ -249,7 +252,7 @@
e96547
 	if (!cap_set_proc(cap_s)) {
e96547
 	    ok = 1;
e96547
 	}
e96547
-	goto cleanup_cap_s;
e96547
+	goto cleanup_conf;
e96547
     }
e96547
 
e96547
     iab = cap_iab_from_text(conf_caps);
e96547
@@ -278,10 +281,9 @@
e96547
     _pam_drop(conf_caps);
e96547
 
e96547
 cleanup_cap_s:
e96547
-    if (cap_s) {
e96547
-	cap_free(cap_s);
e96547
-	cap_s = NULL;
e96547
-    }
e96547
+    cap_free(cap_s);
e96547
+    cap_s = NULL;
e96547
+
e96547
     return ok;
e96547
 }
e96547
 
e96547
diff --color -ru a/progs/capsh.c b/progs/capsh.c
e96547
--- a/progs/capsh.c	2021-02-05 06:52:17.000000000 +0100
e96547
+++ b/progs/capsh.c	2022-01-28 13:06:15.803465885 +0100
e96547
@@ -34,6 +34,35 @@
e96547
 
e96547
 #define MAX_GROUPS       100   /* max number of supplementary groups for user */
e96547
 
e96547
+/* parse a non-negative integer with some error handling */
e96547
+static unsigned long nonneg_uint(const char *text, const char *prefix, int *ok)
e96547
+{
e96547
+    char *remains;
e96547
+    unsigned long value;
e96547
+    ssize_t len = strlen(text);
e96547
+
e96547
+    if (len == 0 || *text == '-') {
e96547
+	goto fail;
e96547
+    }
e96547
+    value = strtoul(text, &remains, 0);
e96547
+    if (*remains) {
e96547
+	goto fail;
e96547
+    }
e96547
+    if (ok != NULL) {
e96547
+	*ok = 1;
e96547
+    }
e96547
+    return value;
e96547
+
e96547
+fail:
e96547
+    if (ok == NULL) {
e96547
+	fprintf(stderr, "%s: want non-negative integer, got \"%s\"\n",
e96547
+		prefix, text);
e96547
+	exit(1);
e96547
+    }
e96547
+    *ok = 0;
e96547
+    return 0;
e96547
+}
e96547
+
e96547
 static char *binary(unsigned long value)
e96547
 {
e96547
     static char string[8*sizeof(unsigned long) + 1];
e96547
@@ -100,7 +129,16 @@
e96547
     display_prctl_set("Bounding", cap_get_bound);
e96547
     display_prctl_set("Ambient", cap_get_ambient);
e96547
     iab = cap_iab_get_proc();
e96547
+    if (iab == NULL) {
e96547
+	perror("failed to get IAB for process");
e96547
+	exit(1);
e96547
+    }
e96547
     text = cap_iab_to_text(iab);
e96547
+    if (text == NULL) {
e96547
+	perror("failed to obtain text for IAB");
e96547
+	cap_free(iab);
e96547
+	exit(1);
e96547
+    }
e96547
     printf("Current IAB: %s\n", text);
e96547
     cap_free(text);
e96547
     cap_free(iab);
e96547
@@ -336,8 +374,8 @@
e96547
  */
e96547
 static char *find_self(const char *arg0)
e96547
 {
e96547
-    int i;
e96547
-    char *parts, *dir, *scratch;
e96547
+    int i, status=1;
e96547
+    char *p = NULL, *parts, *dir, *scratch;
e96547
     const char *path;
e96547
 
e96547
     for (i = strlen(arg0)-1; i >= 0 && arg0[i] != '/'; i--);
e96547
@@ -352,21 +390,45 @@
e96547
     }
e96547
 
e96547
     parts = strdup(path);
e96547
+    if (parts == NULL) {
e96547
+	fprintf(stderr, "insufficient memory for parts of path\n");
e96547
+	exit(1);
e96547
+    }
e96547
+
e96547
     scratch = malloc(2+strlen(path)+strlen(arg0));
e96547
-    if (parts == NULL || scratch == NULL) {
e96547
+    if (scratch == NULL) {
e96547
         fprintf(stderr, "insufficient memory for path building\n");
e96547
-	exit(1);
e96547
+	goto free_parts;
e96547
     }
e96547
 
e96547
-    for (i=0; (dir = strtok(parts, ":")); parts = NULL) {
e96547
+    for (p = parts; (dir = strtok(p, ":")); p = NULL) {
e96547
         sprintf(scratch, "%s/%s", dir, arg0);
e96547
 	if (access(scratch, X_OK) == 0) {
e96547
-            return scratch;
e96547
+	    status = 0;
e96547
+	    break;
e96547
 	}
e96547
     }
e96547
+    if (status) {
e96547
+	fprintf(stderr, "unable to find executable '%s' in PATH\n", arg0);
e96547
+	free(scratch);
e96547
+    }
e96547
 
e96547
-    fprintf(stderr, "unable to find executable '%s' in PATH\n", arg0);
e96547
-    exit(1);
e96547
+free_parts:
e96547
+    free(parts);
e96547
+    if (status) {
e96547
+	exit(status);
e96547
+    }
e96547
+    return scratch;
e96547
+}
e96547
+
e96547
+static long safe_sysconf(int name)
e96547
+{
e96547
+    long ans = sysconf(name);
e96547
+    if (ans <= 0) {
e96547
+	fprintf(stderr, "sysconf(%d) returned a non-positive number: %ld\n", name, ans);
e96547
+	exit(1);
e96547
+    }
e96547
+    return ans;
e96547
 }
e96547
 
e96547
 int main(int argc, char *argv[], char *envp[])
e96547
@@ -378,6 +440,10 @@
e96547
     child = 0;
e96547
 
e96547
     char *temp_name = cap_to_name(cap_max_bits() - 1);
e96547
+    if (temp_name == NULL) {
e96547
+	perror("obtaining highest capability name");
e96547
+	exit(1);
e96547
+    }
e96547
     if (temp_name[0] != 'c') {
e96547
 	printf("WARNING: libcap needs an update (cap=%d should have a name).\n",
e96547
 	       cap_max_bits() - 1);
e96547
@@ -573,7 +639,7 @@
e96547
 	    unsigned value;
e96547
 	    int set;
e96547
 
e96547
-	    value = strtoul(argv[i]+7, NULL, 0);
e96547
+	    value = nonneg_uint(argv[i]+7, "invalid --keep value", NULL);
e96547
 	    set = prctl(PR_SET_KEEPCAPS, value);
e96547
 	    if (set < 0) {
e96547
 		fprintf(stderr, "prctl(PR_SET_KEEPCAPS, %u) failed: %s\n",
e96547
@@ -617,7 +683,9 @@
e96547
 	     * Given we are now in a new directory tree, its good practice
e96547
 	     * to start off in a sane location
e96547
 	     */
e96547
-	    status = chdir("/");
e96547
+	    if (status == 0) {
e96547
+		status = chdir("/");
e96547
+	    }
e96547
 
e96547
 	    cap_free(orig);
e96547
 
e96547
@@ -628,7 +696,7 @@
e96547
 	} else if (!strncmp("--secbits=", argv[i], 10)) {
e96547
 	    unsigned value;
e96547
 	    int status;
e96547
-	    value = strtoul(argv[i]+10, NULL, 0);
e96547
+	    value = nonneg_uint(argv[i]+10, "invalid --secbits value", NULL);
e96547
 	    status = cap_set_secbits(value);
e96547
 	    if (status < 0) {
e96547
 		fprintf(stderr, "failed to set securebits to 0%o/0x%x\n",
e96547
@@ -641,7 +709,7 @@
e96547
 		fprintf(stderr, "already forked\n");
e96547
 		exit(1);
e96547
 	    }
e96547
-	    value = strtoul(argv[i]+10, NULL, 0);
e96547
+	    value = nonneg_uint(argv[i]+10, "invalid --forkfor value", NULL);
e96547
 	    if (value == 0) {
e96547
 		goto usage;
e96547
 	    }
e96547
@@ -657,7 +725,8 @@
e96547
 	    pid_t result;
e96547
 	    unsigned value;
e96547
 
e96547
-	    value = strtoul(argv[i]+9, NULL, 0);
e96547
+	    value = nonneg_uint(argv[i]+9, "invalid --killit signo value",
e96547
+				NULL);
e96547
 	    if (!child) {
e96547
 		fprintf(stderr, "no forked process to kill\n");
e96547
 		exit(1);
e96547
@@ -683,7 +752,7 @@
e96547
 	    unsigned value;
e96547
 	    int status;
e96547
 
e96547
-	    value = strtoul(argv[i]+6, NULL, 0);
e96547
+	    value = nonneg_uint(argv[i]+6, "invalid --uid value", NULL);
e96547
 	    status = setuid(value);
e96547
 	    if (status < 0) {
e96547
 		fprintf(stderr, "Failed to set uid=%u: %s\n",
e96547
@@ -694,7 +763,7 @@
e96547
 	    unsigned value;
e96547
 	    int status;
e96547
 
e96547
-	    value = strtoul(argv[i]+10, NULL, 0);
e96547
+	    value = nonneg_uint(argv[i]+10, "invalid --cap-uid value", NULL);
e96547
 	    status = cap_setuid(value);
e96547
 	    if (status < 0) {
e96547
 		fprintf(stderr, "Failed to cap_setuid(%u): %s\n",
e96547
@@ -705,7 +774,7 @@
e96547
 	    unsigned value;
e96547
 	    int status;
e96547
 
e96547
-	    value = strtoul(argv[i]+6, NULL, 0);
e96547
+	    value = nonneg_uint(argv[i]+6, "invalid --gid value", NULL);
e96547
 	    status = setgid(value);
e96547
 	    if (status < 0) {
e96547
 		fprintf(stderr, "Failed to set gid=%u: %s\n",
e96547
@@ -718,14 +787,14 @@
e96547
 	  gid_t *group_list;
e96547
 	  int g_count;
e96547
 
e96547
-	  length = sysconf(_SC_GETGR_R_SIZE_MAX);
e96547
+	  length = safe_sysconf(_SC_GETGR_R_SIZE_MAX);
e96547
 	  buf = calloc(1, length);
e96547
 	  if (NULL == buf) {
e96547
 	    fprintf(stderr, "No memory for [%s] operation\n", argv[i]);
e96547
 	    exit(1);
e96547
 	  }
e96547
 
e96547
-	  max_groups = sysconf(_SC_NGROUPS_MAX);
e96547
+	  max_groups = safe_sysconf(_SC_NGROUPS_MAX);
e96547
 	  group_list = calloc(max_groups, sizeof(gid_t));
e96547
 	  if (NULL == group_list) {
e96547
 	    fprintf(stderr, "No memory for gid list\n");
e96547
@@ -741,8 +810,7 @@
e96547
 	    }
e96547
 	    if (!isdigit(*ptr)) {
e96547
 	      struct group *g, grp;
e96547
-	      getgrnam_r(ptr, &grp, buf, length, &g);
e96547
-	      if (NULL == g) {
e96547
+	      if (getgrnam_r(ptr, &grp, buf, length, &g) || NULL == g) {
e96547
 		fprintf(stderr, "Failed to identify gid for group [%s]\n", ptr);
e96547
 		exit(1);
e96547
 	      }
e96547
@@ -835,6 +903,7 @@
e96547
 	    argv[argc] = NULL;
e96547
 	    execve(argv[i], argv+i, envp);
e96547
 	    fprintf(stderr, "execve '%s' failed!\n", argv[i]);
e96547
+	    free(argv[i]);
e96547
 	    exit(1);
e96547
 	} else if (!strncmp("--shell=", argv[i], 8)) {
e96547
 	    shell = argv[i]+8;
e96547
@@ -885,7 +954,7 @@
e96547
 	} else if (!strncmp("--is-uid=", argv[i], 9)) {
e96547
 	    unsigned value;
e96547
 	    uid_t uid;
e96547
-	    value = strtoul(argv[i]+9, NULL, 0);
e96547
+	    value = nonneg_uint(argv[i]+9, "invalid --is-uid value", NULL);
e96547
 	    uid = getuid();
e96547
 	    if (uid != value) {
e96547
 		fprintf(stderr, "uid: got=%d, want=%d\n", uid, value);
e96547
@@ -894,7 +963,7 @@
e96547
 	} else if (!strncmp("--is-gid=", argv[i], 9)) {
e96547
 	    unsigned value;
e96547
 	    gid_t gid;
e96547
-	    value = strtoul(argv[i]+9, NULL, 0);
e96547
+	    value = nonneg_uint(argv[i]+9, "invalid --is-gid value", NULL);
e96547
 	    gid = getgid();
e96547
 	    if (gid != value) {
e96547
 		fprintf(stderr, "gid: got=%d, want=%d\n", gid, value);
e96547
diff --color -ru a/progs/getcap.c b/progs/getcap.c
e96547
--- a/progs/getcap.c	2021-02-05 06:52:17.000000000 +0100
e96547
+++ b/progs/getcap.c	2022-01-28 12:44:05.009936148 +0100
e96547
@@ -110,11 +110,11 @@
e96547
 
e96547
     for (i=optind; argv[i] != NULL; i++) {
e96547
 	struct stat stbuf;
e96547
-
e96547
-	if (lstat(argv[i], &stbuf) != 0) {
e96547
-	    fprintf(stderr, "%s (%s)\n", argv[i], strerror(errno));
e96547
+	char *arg = argv[i];
e96547
+	if (lstat(arg, &stbuf) != 0) {
e96547
+	    fprintf(stderr, "%s (%s)\n", arg, strerror(errno));
e96547
 	} else if (recursive) {
e96547
-	    nftw(argv[i], do_getcap, 20, FTW_PHYS);
e96547
+	    nftw(arg, do_getcap, 20, FTW_PHYS);
e96547
 	} else {
e96547
 	    int tflag = S_ISREG(stbuf.st_mode) ? FTW_F :
e96547
 		(S_ISLNK(stbuf.st_mode) ? FTW_SL : FTW_NS);
e96547
diff --color -ru a/progs/setcap.c b/progs/setcap.c
e96547
--- a/progs/setcap.c	2021-02-05 06:52:17.000000000 +0100
e96547
+++ b/progs/setcap.c	2022-01-28 12:44:05.009936148 +0100
e96547
@@ -166,9 +166,12 @@
e96547
 	    }
e96547
 
e96547
 	    cap_on_file = cap_get_file(*++argv);
e96547
-
e96547
 	    if (cap_on_file == NULL) {
e96547
 		cap_on_file = cap_from_text("=");
e96547
+		if (cap_on_file == NULL) {
e96547
+		    perror("unable to use missing capability");
e96547
+		    exit(1);
e96547
+		}
e96547
 	    }
e96547
 
e96547
 	    cmp = cap_compare(cap_on_file, cap_d);
e96547
diff --color -ru a/psx/psx.c b/psx/psx.c
e96547
--- a/psx/psx.c	2021-02-05 06:52:17.000000000 +0100
e96547
+++ b/psx/psx.c	2022-01-28 12:44:05.009936148 +0100
e96547
@@ -107,6 +107,10 @@
e96547
  */
e96547
 static void *psx_do_registration(void) {
e96547
     registered_thread_t *node = calloc(1, sizeof(registered_thread_t));
e96547
+    if (node == NULL) {
e96547
+	perror("unable to register psx handler");
e96547
+	exit(1);
e96547
+    }
e96547
     pthread_mutex_init(&node->mu, NULL);
e96547
     node->thread = pthread_self();
e96547
     pthread_setspecific(psx_action_key, node);
e96547
@@ -454,6 +458,10 @@
e96547
 int __wrap_pthread_create(pthread_t *thread, const pthread_attr_t *attr,
e96547
 			  void *(*start_routine) (void *), void *arg) {
e96547
     psx_starter_t *starter = calloc(1, sizeof(psx_starter_t));
e96547
+    if (starter == NULL) {
e96547
+	perror("failed at thread creation");
e96547
+	exit(1);
e96547
+    }
e96547
     starter->fn = start_routine;
e96547
     starter->arg = arg;
e96547
     /*
e96547
diff --color -ru a/tests/libcap_launch_test.c b/tests/libcap_launch_test.c
e96547
--- a/tests/libcap_launch_test.c	2021-02-05 06:52:17.000000000 +0100
e96547
+++ b/tests/libcap_launch_test.c	2022-01-28 12:44:05.010936167 +0100
e96547
@@ -93,6 +93,10 @@
e96547
 	printf("[%d] test should %s\n", i,
e96547
 	       v->result ? "generate error" : "work");
e96547
 	cap_launch_t attr = cap_new_launcher(v->args[0], v->args, v->envp);
e96547
+	if (attr == NULL) {
e96547
+	    perror("failed to obtain launcher");
e96547
+	    exit(1);
e96547
+	}
e96547
 	if (v->chroot) {
e96547
 	    cap_launcher_set_chroot(attr, v->chroot);
e96547
 	}
e96547
diff --color -ru a/tests/libcap_psx_test.c b/tests/libcap_psx_test.c
e96547
--- a/tests/libcap_psx_test.c	2021-02-05 06:52:17.000000000 +0100
e96547
+++ b/tests/libcap_psx_test.c	2022-01-28 12:55:55.887807887 +0100
e96547
@@ -16,8 +16,15 @@
e96547
     usleep(1234);
e96547
     pid_t pid = fork();
e96547
     cap_t start = cap_get_proc();
e96547
+    if (start == NULL) {
e96547
+	perror("FAILED: unable to start");
e96547
+	exit(1);
e96547
+    }
e96547
     if (pid == 0) {
e96547
-	cap_set_proc(start);
e96547
+	if (cap_set_proc(start)) {
e96547
+	    perror("setting empty caps failed");
e96547
+	    exit(1);
e96547
+	}
e96547
 	exit(0);
e96547
     }
e96547
     int res;
e96547
@@ -27,6 +34,7 @@
e96547
 	exit(1);
e96547
     }
e96547
     cap_set_proc(start);
e96547
+    cap_free(start);
e96547
     return NULL;
e96547
 }
e96547
 
e96547
@@ -35,6 +43,10 @@
e96547
     printf("hello libcap and libpsx ");
e96547
     fflush(stdout);
e96547
     cap_t start = cap_get_proc();
e96547
+    if (start == NULL) {
e96547
+	perror("FAILED: to actually start");
e96547
+	exit(1);
e96547
+    }
e96547
     pthread_t ignored[10];
e96547
     for (i = 0; i < 10; i++) {
e96547
 	pthread_create(&ignored[i], NULL, thread_fork_exit, NULL);
e96547
@@ -42,7 +54,10 @@
e96547
     for (i = 0; i < 10; i++) {
e96547
 	printf(".");     /* because of fork, this may print double */
e96547
 	fflush(stdout);  /* try to limit the above effect */
e96547
-	cap_set_proc(start);
e96547
+	if (cap_set_proc(start)) {
e96547
+	    perror("failed to set proc");
e96547
+	    exit(1);
e96547
+	}
e96547
 	usleep(1000);
e96547
     }
e96547
     printf(" PASSED\n");