Blame SOURCES/libcap-static-analysis.patch

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