Skip to contentSkip to conversation

libselinux: make use of calloc(3)

PATCH In review Christian Göttsche 2 participants 5 messages Nov 20, 2024 – Dec 4, 2024 SELinux security module

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/3 libselinux: make use of calloc(3)

From: Christian Göttsche <[email protected]>

Use calloc(3) instead of calling malloc(3) plus a call to memset(3) or manual zero'ing.

---
 libselinux/src/fgetfilecon.c         | 3 +--
 libselinux/src/get_context_list.c    | 3 +--
 libselinux/src/get_initial_context.c | 3 +--
 libselinux/src/getfilecon.c          | 3 +--
 libselinux/src/getpeercon.c          | 3 +--
 libselinux/src/label_media.c         | 6 ++----
 libselinux/src/label_x.c             | 6 ++----
 libselinux/src/lgetfilecon.c         | 3 +--
 libselinux/src/matchpathcon.c        | 3 +--
 libselinux/src/procattr.c            | 3 +--
 libselinux/src/setrans_client.c      | 5 ++---
 11 files changed, 14 insertions(+), 27 deletions(-)
libselinux/src/fgetfilecon.c +1 -2
index d7051171..782861ed 100644
@@ -39,10 +39,9 @@ int fgetfilecon_raw(int fd, char ** context)
 	ssize_t ret;
 
 	size = INITCONTEXTLEN + 1;
-	buf = malloc(size);
+	buf = calloc(1, size);
 	if (!buf)
 		return -1;
-	memset(buf, 0, size);
 
 	ret = fgetxattr_wrapper(fd, XATTR_NAME_SELINUX, buf, size - 1);
 	if (ret < 0 && errno == ERANGE) {
libselinux/src/get_context_list.c +1 -2
index 222b54c1..8d5ee6fb 100644
@@ -481,12 +481,11 @@ int get_ordered_context_list(const char *user,
 	   the "failsafe" context to at least permit root login
 	   for emergency recovery if possible. */
 	freeconary(reachable);
-	reachable = malloc(2 * sizeof(char *));
+	reachable = calloc(2, sizeof(char *));
 	if (!reachable) {
 		rc = -1;
 		goto out;
 	}
-	reachable[0] = reachable[1] = 0;
 	rc = get_failsafe_context(user, &reachable[0]);
 	if (rc < 0) {
 		freeconary(reachable);
libselinux/src/get_initial_context.c +1 -2
index 0f25ba3f..fb774c82 100644
@@ -39,12 +39,11 @@ int security_get_initial_context_raw(const char * name, char ** con)
 		return -1;
 
 	size = selinux_page_size;
-	buf = malloc(size);
+	buf = calloc(1, size);
 	if (!buf) {
 		ret = -1;
 		goto out;
 	}
-	memset(buf, 0, size);
 	ret = read(fd, buf, size - 1);
 	if (ret < 0)
 		goto out2;
libselinux/src/getfilecon.c +1 -2
index 4bee3137..31c9f0de 100644
@@ -14,10 +14,9 @@ int getfilecon_raw(const char *path, char ** context)
 	ssize_t ret;
 
 	size = INITCONTEXTLEN + 1;
-	buf = malloc(size);
+	buf = calloc(1, size);
 	if (!buf)
 		return -1;
-	memset(buf, 0, size);
 
 	ret = getxattr(path, XATTR_NAME_SELINUX, buf, size - 1);
 	if (ret < 0 && errno == ERANGE) {
libselinux/src/getpeercon.c +1 -2
index a9dca73e..c7abd886 100644
@@ -18,10 +18,9 @@ int getpeercon_raw(int fd, char ** context)
 	ssize_t ret;
 
 	size = INITCONTEXTLEN + 1;
-	buf = malloc(size);
+	buf = calloc(1, size);
 	if (!buf)
 		return -1;
-	memset(buf, 0, size);
 
 	ret = getsockopt(fd, SOL_SOCKET, SO_PEERSEC, buf, &size);
 	if (ret < 0 && errno == ERANGE) {
libselinux/src/label_media.c +2 -4
index 0510b5b1..be3df388 100644
@@ -134,10 +134,9 @@ static int init(struct selabel_handle *rec, const struct selinux_opt *opts,
 				status = 0;
 				goto finish;
 			}
-			data->spec_arr = malloc(sizeof(spec_t)*data->nspec);
+			data->spec_arr = calloc(data->nspec, sizeof(spec_t));
 			if (data->spec_arr == NULL)
 				goto finish;
-			memset(data->spec_arr, 0, sizeof(spec_t)*data->nspec);
 			maxnspec = data->nspec;
 
 			status = fseek(fp, 0L, SEEK_SET);
@@ -230,10 +229,9 @@ int selabel_media_init(struct selabel_handle *rec,
 {
 	struct saved_data *data;
 
-	data = (struct saved_data *)malloc(sizeof(*data));
+	data = (struct saved_data *)calloc(1, sizeof(*data));
 	if (!data)
 		return -1;
-	memset(data, 0, sizeof(*data));
 
 	rec->data = data;
 	rec->func_close = &close;
libselinux/src/label_x.c +2 -4
index 1a5b9268..5b0e4063 100644
@@ -161,10 +161,9 @@ static int init(struct selabel_handle *rec, const struct selinux_opt *opts,
 				status = 0;
 				goto finish;
 			}
-			data->spec_arr = malloc(sizeof(spec_t)*data->nspec);
+			data->spec_arr = calloc(data->nspec, sizeof(spec_t));
 			if (data->spec_arr == NULL)
 				goto finish;
-			memset(data->spec_arr, 0, sizeof(spec_t)*data->nspec);
 			maxnspec = data->nspec;
 
 			status = fseek(fp, 0L, SEEK_SET);
@@ -255,10 +254,9 @@ int selabel_x_init(struct selabel_handle *rec, const struct selinux_opt *opts,
 {
 	struct saved_data *data;
 
-	data = (struct saved_data *)malloc(sizeof(*data));
+	data = (struct saved_data *)calloc(1, sizeof(*data));
 	if (!data)
 		return -1;
-	memset(data, 0, sizeof(*data));
 
 	rec->data = data;
 	rec->func_close = &close;
libselinux/src/lgetfilecon.c +1 -2
index d1fb821b..f0a3aa1a 100644
@@ -14,10 +14,9 @@ int lgetfilecon_raw(const char *path, char ** context)
 	ssize_t ret;
 
 	size = INITCONTEXTLEN + 1;
-	buf = malloc(size);
+	buf = calloc(1, size);
 	if (!buf)
 		return -1;
-	memset(buf, 0, size);
 
 	ret = lgetxattr(path, XATTR_NAME_SELINUX, buf, size - 1);
 	if (ret < 0 && errno == ERANGE) {
libselinux/src/matchpathcon.c +1 -2
index 967520e4..dbc11ee1 100644
@@ -202,10 +202,9 @@ int matchpathcon_filespec_add(ino_t ino, int specind, const char *file)
 	struct stat sb;
 
 	if (!fl_head) {
-		fl_head = malloc(sizeof(file_spec_t) * HASH_BUCKETS);
+		fl_head = calloc(HASH_BUCKETS, sizeof(file_spec_t));
 		if (!fl_head)
 			goto oom;
-		memset(fl_head, 0, sizeof(file_spec_t) * HASH_BUCKETS);
 	}
 
 	h = (ino + (ino >> HASH_BITS)) & HASH_MASK;
libselinux/src/procattr.c +1 -2
index b7a93a2b..ddcc7f8d 100644
@@ -139,12 +139,11 @@ static int getprocattrcon_raw(char **context, pid_t pid, const char *attr,
 		return -1;
 
 	size = selinux_page_size;
-	buf = malloc(size);
+	buf = calloc(1, size);
 	if (!buf) {
 		ret = -1;
 		goto out;
 	}
-	memset(buf, 0, size);
 
 	do {
 		ret = read(fd, buf, size - 1);
libselinux/src/setrans_client.c +2 -3
index d7dbc0ca..45cbe5c1 100644
@@ -173,11 +173,10 @@ receive_response(int fd, uint32_t function, char **outdata, int32_t * ret_val)
 		return -1;
 	}
 
-	data = malloc(data_size);
+	/* coveriety doesn't realize that data will be initialized in readv */
+	data = calloc(1, data_size);
 	if (!data)
 		return -1;
-	/* coveriety doesn't realize that data will be initialized in readv */
-	memset(data, 0, data_size);
 
 	resp_data.iov_base = data;
 	resp_data.iov_len = data_size;
Signature · 1 line
2.45.2
#1 lore raw

PATCH v2 2/3 libselinux: avoid dynamic allocation in openattr()

From: Christian Göttsche <[email protected]>

openattr() supplies the simplementation for the getcon(3) interface family. Use a short local buffer instead of descend into memory allocation.

---
v2:
- minimize buffer from 56 to 44 characters and assert pid_t is not wider than 32bit

---
 libselinux/src/procattr.c | 27 +++++++++++++++------------
 1 file changed, 15 insertions(+), 12 deletions(-)
libselinux/src/procattr.c +15 -12
index ddcc7f8d..21c810d2 100644
@@ -1,3 +1,4 @@
+#include <assert.h>
 #include <sys/syscall.h>
 #include <unistd.h>
 #include <fcntl.h>
@@ -86,32 +87,34 @@ static void init_procattr(void)
 static int openattr(pid_t pid, const char *attr, int flags)
 {
 	int fd, rc;
-	char *path;
+	char path[44];  /* must hold "/proc/self/task/%d/attr/sockcreate" */
 	pid_t tid;
 
+	static_assert(sizeof(pid_t) <= 4, "content written to path might get truncated");
+
 	if (pid > 0) {
-		rc = asprintf(&path, "/proc/%d/attr/%s", pid, attr);
+		rc = snprintf(path, sizeof(path), "/proc/%d/attr/%s", pid, attr);
 	} else if (pid == 0) {
-		rc = asprintf(&path, "/proc/thread-self/attr/%s", attr);
-		if (rc < 0)
+		rc = snprintf(path, sizeof(path), "/proc/thread-self/attr/%s", attr);
+		if (rc < 0 || (size_t)rc >= sizeof(path)) {
+			errno = EOVERFLOW;
 			return -1;
+		}
 		fd = open(path, flags | O_CLOEXEC);
 		if (fd >= 0 || errno != ENOENT)
-			goto out;
-		free(path);
+			return fd;
 		tid = selinux_gettid();
-		rc = asprintf(&path, "/proc/self/task/%d/attr/%s", tid, attr);
+		rc = snprintf(path, sizeof(path), "/proc/self/task/%d/attr/%s", tid, attr);
 	} else {
 		errno = EINVAL;
 		return -1;
 	}
-	if (rc < 0)
+	if (rc < 0 || (size_t)rc >= sizeof(path)) {
+		errno = EOVERFLOW;
 		return -1;
+	}
 
-	fd = open(path, flags | O_CLOEXEC);
-out:
-	free(path);
-	return fd;
+	return open(path, flags | O_CLOEXEC);
 }
 
 static int getprocattrcon_raw(char **context, pid_t pid, const char *attr,
Signature · 1 line
2.45.2
#2 lore raw

PATCH v2 3/3 libselinux: move functions out of header file

In reply to Christian Göttsche

From: Christian Göttsche <[email protected]>

When building libselinux from its own directory GCC complains about the two functions free_spec_node() and sort_spec_node(), which are not tiny and also recursive.

    In file included from label_file.c:27:
    In function ‘load_mmap’,
       inlined from ‘process_file’ at label_file.c:1106:9:
    label_file.h:816:20: error: inlining failed in call to ‘free_spec_node’: --param max-inline-insns-single limit reached [-Werror=inline]
      816 | static inline void free_spec_node(struct spec_node *node)
          |                    ^~~~~~~~~~~~~~
    label_file.c:899:17: note: called from here
      899 |                 free_spec_node(data->root);
          |                 ^~~~~~~~~~~~~~~~~~~~~~~~~~
    label_file.h:816:20: error: inlining failed in call to ‘free_spec_node’: --param max-inline-insns-single limit reached [-Werror=inline]
      816 | static inline void free_spec_node(struct spec_node *node)
          |                    ^~~~~~~~~~~~~~
    label_file.c:908:17: note: called from here
      908 |                 free_spec_node(root);
          |                 ^~~~~~~~~~~~~~~~~~~~

In function ‘sort_specs’,
inlined from ‘init’ at label_file.c:1350:3:
label_file.h:404:20: error: inlining failed in call to ‘sort_spec_node’: --param max-inline-insns-single limit reached [-Werror=inline]
404 | static inline void sort_spec_node(struct spec_node *node, struct spec_node *parent)
| ^~~~~~~~~~~~~~
label_file.h:433:9: note: called from here
433 | sort_spec_node(data->root, NULL);
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
In function ‘sort_specs’,
inlined from ‘init’ at label_file.c:1370:3:
label_file.h:404:20: error: inlining failed in call to ‘sort_spec_node’: --param max-inline-insns-single limit reached [-Werror=inline]
404 | static inline void sort_spec_node(struct spec_node *node, struct spec_node *parent)
| ^~~~~~~~~~~~~~
label_file.h:433:9: note: called from here
433 | sort_spec_node(data->root, NULL);
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Fixes 92306daf ("libselinux: rework selabel_file(5) database")

---
v2:
- add patch

---
 libselinux/src/label_file.c | 68 +++++++++++++++++++++++++++++++++++
 libselinux/src/label_file.h | 71 ++-----------------------------------
 2 files changed, 71 insertions(+), 68 deletions(-)
libselinux/src/label_file.c +68 -0
index 189a5ed2..40bcb9ee 100644
@@ -34,6 +34,74 @@
 #endif  /* FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION */
 
 
+void free_spec_node(struct spec_node *node)
+{
+	for (uint32_t i = 0; i < node->literal_specs_num; i++) {
+		struct literal_spec *lspec = &node->literal_specs[i];
+
+		free(lspec->lr.ctx_raw);
+		free(lspec->lr.ctx_trans);
+		__pthread_mutex_destroy(&lspec->lr.lock);
+
+		if (lspec->from_mmap)
+			continue;
+
+		free(lspec->literal_match);
+		free(lspec->regex_str);
+	}
+	free(node->literal_specs);
+
+	for (uint32_t i = 0; i < node->regex_specs_num; i++) {
+		struct regex_spec *rspec = &node->regex_specs[i];
+
+		free(rspec->lr.ctx_raw);
+		free(rspec->lr.ctx_trans);
+		__pthread_mutex_destroy(&rspec->lr.lock);
+		regex_data_free(rspec->regex);
+		__pthread_mutex_destroy(&rspec->regex_lock);
+
+		if (rspec->from_mmap)
+			continue;
+
+		free(rspec->regex_str);
+	}
+	free(node->regex_specs);
+
+	for (uint32_t i = 0; i < node->children_num; i++)
+		free_spec_node(&node->children[i]);
+	free(node->children);
+
+	if (!node->from_mmap)
+		free(node->stem);
+}
+
+void sort_spec_node(struct spec_node *node, struct spec_node *parent)
+{
+	/* A node should not be its own parent */
+	assert(node != parent);
+	/* Only root node has NULL stem */
+	assert((!parent && !node->stem) || (parent && node->stem && node->stem[0] != '\0'));
+	/* A non-root node should not be empty */
+	assert(!parent || (node->literal_specs_num || node->regex_specs_num || node->children_num));
+
+
+	node->parent = parent;
+
+	/* Sort for comparison support and binary search lookup */
+
+	if (node->literal_specs_num > 1)
+		qsort(node->literal_specs, node->literal_specs_num, sizeof(struct literal_spec), compare_literal_spec);
+
+	if (node->regex_specs_num > 1)
+		qsort(node->regex_specs, node->regex_specs_num, sizeof(struct regex_spec), compare_regex_spec);
+
+	if (node->children_num > 1)
+		qsort(node->children, node->children_num, sizeof(struct spec_node), compare_spec_node);
+
+	for (uint32_t i = 0; i < node->children_num; i++)
+		sort_spec_node(&node->children[i], node);
+}
+
 /*
  * Warn about duplicate specifications.
  */
libselinux/src/label_file.h +3 -68

89 lines not on this page · raw message

Signature · 1 line
2.45.2
Acked-by on v2 1/3
#3 lore raw
In reply to Christian Göttsche
Quoted 7 lines · Christian Göttsche

From: Christian Göttsche <[email protected]>

Use calloc(3) instead of calling malloc(3) plus a call to memset(3) or manual zero'ing.

For these three patches:

Quoted 226 lines
---
 libselinux/src/fgetfilecon.c         | 3 +--
 libselinux/src/get_context_list.c    | 3 +--
 libselinux/src/get_initial_context.c | 3 +--
 libselinux/src/getfilecon.c          | 3 +--
 libselinux/src/getpeercon.c          | 3 +--
 libselinux/src/label_media.c         | 6 ++----
 libselinux/src/label_x.c             | 6 ++----
 libselinux/src/lgetfilecon.c         | 3 +--
 libselinux/src/matchpathcon.c        | 3 +--
 libselinux/src/procattr.c            | 3 +--
 libselinux/src/setrans_client.c      | 5 ++---
 11 files changed, 14 insertions(+), 27 deletions(-)
libselinux/src/fgetfilecon.c +1 -2
index d7051171..782861ed 100644
@@ -39,10 +39,9 @@ int fgetfilecon_raw(int fd, char ** context)
        ssize_t ret;
 
        size = INITCONTEXTLEN + 1;
-       buf = malloc(size);
+       buf = calloc(1, size);
        if (!buf)
                return -1;
-       memset(buf, 0, size);
 
        ret = fgetxattr_wrapper(fd, XATTR_NAME_SELINUX, buf, size - 1);
        if (ret < 0 && errno == ERANGE) {
libselinux/src/get_context_list.c +1 -2
index 222b54c1..8d5ee6fb 100644
@@ -481,12 +481,11 @@ int get_ordered_context_list(const char *user,
           the "failsafe" context to at least permit root login
           for emergency recovery if possible. */
        freeconary(reachable);
-       reachable = malloc(2 * sizeof(char *));
+       reachable = calloc(2, sizeof(char *));
        if (!reachable) {
                rc = -1;
                goto out;
        }
-       reachable[0] = reachable[1] = 0;
        rc = get_failsafe_context(user, &reachable[0]);
        if (rc < 0) {
                freeconary(reachable);
libselinux/src/get_initial_context.c +1 -2
index 0f25ba3f..fb774c82 100644
@@ -39,12 +39,11 @@ int security_get_initial_context_raw(const char * name, char ** con)
                return -1;
 
        size = selinux_page_size;
-       buf = malloc(size);
+       buf = calloc(1, size);
        if (!buf) {
                ret = -1;
                goto out;
        }
-       memset(buf, 0, size);
        ret = read(fd, buf, size - 1);
        if (ret < 0)
                goto out2;
libselinux/src/getfilecon.c +1 -2
index 4bee3137..31c9f0de 100644
@@ -14,10 +14,9 @@ int getfilecon_raw(const char *path, char ** context)
        ssize_t ret;
 
        size = INITCONTEXTLEN + 1;
-       buf = malloc(size);
+       buf = calloc(1, size);
        if (!buf)
                return -1;
-       memset(buf, 0, size);
 
        ret = getxattr(path, XATTR_NAME_SELINUX, buf, size - 1);
        if (ret < 0 && errno == ERANGE) {
libselinux/src/getpeercon.c +1 -2
index a9dca73e..c7abd886 100644
@@ -18,10 +18,9 @@ int getpeercon_raw(int fd, char ** context)
        ssize_t ret;
 
        size = INITCONTEXTLEN + 1;
-       buf = malloc(size);
+       buf = calloc(1, size);
        if (!buf)
                return -1;
-       memset(buf, 0, size);
 
        ret = getsockopt(fd, SOL_SOCKET, SO_PEERSEC, buf, &size);
        if (ret < 0 && errno == ERANGE) {
libselinux/src/label_media.c +2 -4
index 0510b5b1..be3df388 100644
@@ -134,10 +134,9 @@ static int init(struct selabel_handle *rec, const struct selinux_opt *opts,
                                status = 0;
                                goto finish;
                        }
-                       data->spec_arr = malloc(sizeof(spec_t)*data->nspec);
+                       data->spec_arr = calloc(data->nspec, sizeof(spec_t));
                        if (data->spec_arr == NULL)
                                goto finish;
-                       memset(data->spec_arr, 0, sizeof(spec_t)*data->nspec);
                        maxnspec = data->nspec;
 
                        status = fseek(fp, 0L, SEEK_SET);
@@ -230,10 +229,9 @@ int selabel_media_init(struct selabel_handle *rec,
 {
        struct saved_data *data;
 
-       data = (struct saved_data *)malloc(sizeof(*data));
+       data = (struct saved_data *)calloc(1, sizeof(*data));
        if (!data)
                return -1;
-       memset(data, 0, sizeof(*data));
 
        rec->data = data;
        rec->func_close = &close;
libselinux/src/label_x.c +2 -4
index 1a5b9268..5b0e4063 100644
@@ -161,10 +161,9 @@ static int init(struct selabel_handle *rec, const struct selinux_opt *opts,
                                status = 0;
                                goto finish;
                        }
-                       data->spec_arr = malloc(sizeof(spec_t)*data->nspec);
+                       data->spec_arr = calloc(data->nspec, sizeof(spec_t));
                        if (data->spec_arr == NULL)
                                goto finish;
-                       memset(data->spec_arr, 0, sizeof(spec_t)*data->nspec);
                        maxnspec = data->nspec;
 
                        status = fseek(fp, 0L, SEEK_SET);
@@ -255,10 +254,9 @@ int selabel_x_init(struct selabel_handle *rec, const struct selinux_opt *opts,
 {
        struct saved_data *data;
 
-       data = (struct saved_data *)malloc(sizeof(*data));
+       data = (struct saved_data *)calloc(1, sizeof(*data));
        if (!data)
                return -1;
-       memset(data, 0, sizeof(*data));
 
        rec->data = data;
        rec->func_close = &close;
libselinux/src/lgetfilecon.c +1 -2
index d1fb821b..f0a3aa1a 100644
@@ -14,10 +14,9 @@ int lgetfilecon_raw(const char *path, char ** context)
        ssize_t ret;
 
        size = INITCONTEXTLEN + 1;
-       buf = malloc(size);
+       buf = calloc(1, size);
        if (!buf)
                return -1;
-       memset(buf, 0, size);
 
        ret = lgetxattr(path, XATTR_NAME_SELINUX, buf, size - 1);
        if (ret < 0 && errno == ERANGE) {
libselinux/src/matchpathcon.c +1 -2
index 967520e4..dbc11ee1 100644
@@ -202,10 +202,9 @@ int matchpathcon_filespec_add(ino_t ino, int specind, const char *file)
        struct stat sb;
 
        if (!fl_head) {
-               fl_head = malloc(sizeof(file_spec_t) * HASH_BUCKETS);
+               fl_head = calloc(HASH_BUCKETS, sizeof(file_spec_t));
                if (!fl_head)
                        goto oom;
-               memset(fl_head, 0, sizeof(file_spec_t) * HASH_BUCKETS);
        }
 
        h = (ino + (ino >> HASH_BITS)) & HASH_MASK;
libselinux/src/procattr.c +1 -2
index b7a93a2b..ddcc7f8d 100644
@@ -139,12 +139,11 @@ static int getprocattrcon_raw(char **context, pid_t pid, const char *attr,
                return -1;
 
        size = selinux_page_size;
-       buf = malloc(size);
+       buf = calloc(1, size);
        if (!buf) {
                ret = -1;
                goto out;
        }
-       memset(buf, 0, size);
 
        do {
                ret = read(fd, buf, size - 1);
libselinux/src/setrans_client.c +2 -4
index d7dbc0ca..45cbe5c1 100644
@@ -173,11 +173,10 @@ receive_response(int fd, uint32_t function, char **outdata, int32_t * ret_val)
                return -1;
        }
 
-       data = malloc(data_size);
+       /* coveriety doesn't realize that data will be initialized in readv */
+       data = calloc(1, data_size);
        if (!data)
                return -1;
-       /* coveriety doesn't realize that data will be initialized in readv */
-       memset(data, 0, data_size);
 
        resp_data.iov_base = data;
        resp_data.iov_len = data_size;
--

2.45.2

#4 lore raw
Quoted 6 lines · James Carter
Quoted 3 lines · Christian Göttsche

From: Christian Göttsche <[email protected]>

Quoted 8 lines
Quoted 4 lines

Use calloc(3) instead of calling malloc(3) plus a call to memset(3) or manual zero'ing.

For these three patches:

These three patches have been merged.
Thanks,
Jim

Quoted 226 lines
Quoted 226 lines
---
 libselinux/src/fgetfilecon.c         | 3 +--
 libselinux/src/get_context_list.c    | 3 +--
 libselinux/src/get_initial_context.c | 3 +--
 libselinux/src/getfilecon.c          | 3 +--
 libselinux/src/getpeercon.c          | 3 +--
 libselinux/src/label_media.c         | 6 ++----
 libselinux/src/label_x.c             | 6 ++----
 libselinux/src/lgetfilecon.c         | 3 +--
 libselinux/src/matchpathcon.c        | 3 +--
 libselinux/src/procattr.c            | 3 +--
 libselinux/src/setrans_client.c      | 5 ++---
 11 files changed, 14 insertions(+), 27 deletions(-)
libselinux/src/fgetfilecon.c +1 -2
index d7051171..782861ed 100644
@@ -39,10 +39,9 @@ int fgetfilecon_raw(int fd, char ** context)
        ssize_t ret;
 
        size = INITCONTEXTLEN + 1;
-       buf = malloc(size);
+       buf = calloc(1, size);
        if (!buf)
                return -1;
-       memset(buf, 0, size);
 
        ret = fgetxattr_wrapper(fd, XATTR_NAME_SELINUX, buf, size - 1);
        if (ret < 0 && errno == ERANGE) {
libselinux/src/get_context_list.c +1 -2
index 222b54c1..8d5ee6fb 100644
@@ -481,12 +481,11 @@ int get_ordered_context_list(const char *user,
           the "failsafe" context to at least permit root login
           for emergency recovery if possible. */
        freeconary(reachable);
-       reachable = malloc(2 * sizeof(char *));
+       reachable = calloc(2, sizeof(char *));
        if (!reachable) {
                rc = -1;
                goto out;
        }
-       reachable[0] = reachable[1] = 0;
        rc = get_failsafe_context(user, &reachable[0]);
        if (rc < 0) {
                freeconary(reachable);
libselinux/src/get_initial_context.c +1 -2
index 0f25ba3f..fb774c82 100644
@@ -39,12 +39,11 @@ int security_get_initial_context_raw(const char * name, char ** con)
                return -1;
 
        size = selinux_page_size;
-       buf = malloc(size);
+       buf = calloc(1, size);
        if (!buf) {
                ret = -1;
                goto out;
        }
-       memset(buf, 0, size);
        ret = read(fd, buf, size - 1);
        if (ret < 0)
                goto out2;
libselinux/src/getfilecon.c +1 -2
index 4bee3137..31c9f0de 100644
@@ -14,10 +14,9 @@ int getfilecon_raw(const char *path, char ** context)
        ssize_t ret;
 
        size = INITCONTEXTLEN + 1;
-       buf = malloc(size);
+       buf = calloc(1, size);
        if (!buf)
                return -1;
-       memset(buf, 0, size);
 
        ret = getxattr(path, XATTR_NAME_SELINUX, buf, size - 1);
        if (ret < 0 && errno == ERANGE) {
libselinux/src/getpeercon.c +1 -2
index a9dca73e..c7abd886 100644
@@ -18,10 +18,9 @@ int getpeercon_raw(int fd, char ** context)
        ssize_t ret;
 
        size = INITCONTEXTLEN + 1;
-       buf = malloc(size);
+       buf = calloc(1, size);
        if (!buf)
                return -1;
-       memset(buf, 0, size);
 
        ret = getsockopt(fd, SOL_SOCKET, SO_PEERSEC, buf, &size);
        if (ret < 0 && errno == ERANGE) {
libselinux/src/label_media.c +2 -4
index 0510b5b1..be3df388 100644
@@ -134,10 +134,9 @@ static int init(struct selabel_handle *rec, const struct selinux_opt *opts,
                                status = 0;
                                goto finish;
                        }
-                       data->spec_arr = malloc(sizeof(spec_t)*data->nspec);
+                       data->spec_arr = calloc(data->nspec, sizeof(spec_t));
                        if (data->spec_arr == NULL)
                                goto finish;
-                       memset(data->spec_arr, 0, sizeof(spec_t)*data->nspec);
                        maxnspec = data->nspec;
 
                        status = fseek(fp, 0L, SEEK_SET);
@@ -230,10 +229,9 @@ int selabel_media_init(struct selabel_handle *rec,
 {
        struct saved_data *data;
 
-       data = (struct saved_data *)malloc(sizeof(*data));
+       data = (struct saved_data *)calloc(1, sizeof(*data));
        if (!data)
                return -1;
-       memset(data, 0, sizeof(*data));
 
        rec->data = data;
        rec->func_close = &close;
libselinux/src/label_x.c +2 -4
index 1a5b9268..5b0e4063 100644
@@ -161,10 +161,9 @@ static int init(struct selabel_handle *rec, const struct selinux_opt *opts,
                                status = 0;
                                goto finish;
                        }
-                       data->spec_arr = malloc(sizeof(spec_t)*data->nspec);
+                       data->spec_arr = calloc(data->nspec, sizeof(spec_t));
                        if (data->spec_arr == NULL)
                                goto finish;
-                       memset(data->spec_arr, 0, sizeof(spec_t)*data->nspec);
                        maxnspec = data->nspec;
 
                        status = fseek(fp, 0L, SEEK_SET);
@@ -255,10 +254,9 @@ int selabel_x_init(struct selabel_handle *rec, const struct selinux_opt *opts,
 {
        struct saved_data *data;
 
-       data = (struct saved_data *)malloc(sizeof(*data));
+       data = (struct saved_data *)calloc(1, sizeof(*data));
        if (!data)
                return -1;
-       memset(data, 0, sizeof(*data));
 
        rec->data = data;
        rec->func_close = &close;
libselinux/src/lgetfilecon.c +1 -2
index d1fb821b..f0a3aa1a 100644
@@ -14,10 +14,9 @@ int lgetfilecon_raw(const char *path, char ** context)
        ssize_t ret;
 
        size = INITCONTEXTLEN + 1;
-       buf = malloc(size);
+       buf = calloc(1, size);
        if (!buf)
                return -1;
-       memset(buf, 0, size);
 
        ret = lgetxattr(path, XATTR_NAME_SELINUX, buf, size - 1);
        if (ret < 0 && errno == ERANGE) {
libselinux/src/matchpathcon.c +1 -2
index 967520e4..dbc11ee1 100644
@@ -202,10 +202,9 @@ int matchpathcon_filespec_add(ino_t ino, int specind, const char *file)
        struct stat sb;
 
        if (!fl_head) {
-               fl_head = malloc(sizeof(file_spec_t) * HASH_BUCKETS);
+               fl_head = calloc(HASH_BUCKETS, sizeof(file_spec_t));
                if (!fl_head)
                        goto oom;
-               memset(fl_head, 0, sizeof(file_spec_t) * HASH_BUCKETS);
        }
 
        h = (ino + (ino >> HASH_BITS)) & HASH_MASK;
libselinux/src/procattr.c +1 -2
index b7a93a2b..ddcc7f8d 100644
@@ -139,12 +139,11 @@ static int getprocattrcon_raw(char **context, pid_t pid, const char *attr,
                return -1;
 
        size = selinux_page_size;
-       buf = malloc(size);
+       buf = calloc(1, size);
        if (!buf) {
                ret = -1;
                goto out;
        }
-       memset(buf, 0, size);
 
        do {
                ret = read(fd, buf, size - 1);
libselinux/src/setrans_client.c +2 -4
index d7dbc0ca..45cbe5c1 100644
@@ -173,11 +173,10 @@ receive_response(int fd, uint32_t function, char **outdata, int32_t * ret_val)
                return -1;
        }
 
-       data = malloc(data_size);
+       /* coveriety doesn't realize that data will be initialized in readv */
+       data = calloc(1, data_size);
        if (!data)
                return -1;
-       /* coveriety doesn't realize that data will be initialized in readv */
-       memset(data, 0, data_size);
 
        resp_data.iov_base = data;
        resp_data.iov_len = data_size;
--

2.45.2