Skip to contentSkip to conversation

libsepol: calloc all the *_to_val_structs

PATCH Superseded [email protected] 3 participants 8 messages Aug 18, 2016 – Aug 19, 2016 SELinux security module

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/2 libsepol: calloc all the *_to_val_structs

From: William Roberts <[email protected]>

The usage patterns between these structures seem similair to role_val_to_struct usages. Calloc these up to prevent any unitialized usages.

---
 libsepol/src/mls.c      | 2 +-
 libsepol/src/policydb.c | 6 +++---
 libsepol/src/users.c    | 9 ++++++++-
 3 files changed, 12 insertions(+), 5 deletions(-)
libsepol/src/mls.c +1 -1
index 2dc5f2b..8047d91 100644
@@ -312,7 +312,7 @@ int mls_context_isvalid(const policydb_t * p, const context_struct_t * c)
 	if (!c->user || c->user > p->p_users.nprim)
 		return 0;
 	usrdatum = p->user_val_to_struct[c->user - 1];
-	if (!mls_range_contains(usrdatum->exp_range, c->range))
+	if (!usrdatum || !mls_range_contains(usrdatum->exp_range, c->range))
 		return 0;	/* user may not be associated with range */
 
 	return 1;
libsepol/src/policydb.c +3 -3
index c225ac6..5f888d3 100644
@@ -1074,7 +1074,7 @@ int policydb_index_others(sepol_handle_t * handle,
 
 	free(p->user_val_to_struct);
 	p->user_val_to_struct = (user_datum_t **)
-	    malloc(p->p_users.nprim * sizeof(user_datum_t *));
+	    calloc(p->p_users.nprim, sizeof(user_datum_t *));
 	if (!p->user_val_to_struct)
 		return -1;
 
@@ -4006,12 +4006,12 @@ int policydb_reindex_users(policydb_t * p)
 		free(p->sym_val_to_name[i]);
 
 	p->user_val_to_struct = (user_datum_t **)
-	    malloc(p->p_users.nprim * sizeof(user_datum_t *));
+	    calloc(p->p_users.nprim, sizeof(user_datum_t *));
 	if (!p->user_val_to_struct)
 		return -1;
 
 	p->sym_val_to_name[i] = (char **)
-	    malloc(p->symtab[i].nprim * sizeof(char *));
+	    calloc(p->symtab[i].nprim, sizeof(char *));
 	if (!p->sym_val_to_name[i])
 		return -1;
 
libsepol/src/users.c +8 -1
index ce54c2b..3ffb166 100644
@@ -19,12 +19,17 @@ static int user_to_record(sepol_handle_t * handle,
 
 	const char *name = policydb->p_user_val_to_name[user_idx];
 	user_datum_t *usrdatum = policydb->user_val_to_struct[user_idx];
-	ebitmap_t *roles = &(usrdatum->roles.roles);
+	ebitmap_t *roles;
 	ebitmap_node_t *rnode;
 	unsigned bit;
 
 	sepol_user_t *tmp_record = NULL;
 
+	if (!usrdatum)
+		goto err;
+
+	roles = &(usrdatum->roles.roles);
+
 	if (sepol_user_create(handle, &tmp_record) < 0)
 		goto err;
 
@@ -234,6 +239,7 @@ int sepol_user_modify(sepol_handle_t * handle,
 		if (!tmp_ptr)
 			goto omem;
 		policydb->user_val_to_struct = tmp_ptr;
+		policydb->user_val_to_struct[policydb->p_users.nprim] = NULL;
 
 		tmp_ptr = realloc(policydb->sym_val_to_name[SYM_USERS],
 				  (policydb->p_users.nprim +
@@ -241,6 +247,7 @@ int sepol_user_modify(sepol_handle_t * handle,
 		if (!tmp_ptr)
 			goto omem;
 		policydb->sym_val_to_name[SYM_USERS] = tmp_ptr;
+		policydb->p_user_val_to_name[policydb->p_users.nprim] = NULL;
 
 		/* Need to copy the user name */
 		name = strdup(cname);
Signature · 1 line
1.9.1
#1 lore raw

PATCH 2/2 libsepol: port str_read from kernel

From: William Roberts <[email protected]>

Rather than duplicating the following sequence:
1. Read len from file
2. alloc up space based on 1
3. read the contents into the buffer from 2
4. null terminate the buffer from 2

Use the str_read() function that is in the kernel, which collapses steps 2 and 4. This not only reduces redundant code, but also has the side-affect of providing a central check on zero_or_saturated lengths from step 1 when generating string values.

---
 libsepol/src/conditional.c |  9 +------
 libsepol/src/module.c      | 66 ++++++++++++++++++++++------------------------
 libsepol/src/policydb.c    | 10 +------
 libsepol/src/private.h     |  1 +
 libsepol/src/services.c    | 33 +++++++++++++++++++++++
 5 files changed, 68 insertions(+), 51 deletions(-)
libsepol/src/conditional.c +1 -8
index 8680eb2..e1bc961 100644
@@ -589,15 +589,8 @@ int cond_read_bool(policydb_t * p,
 		goto err;
 
 	len = le32_to_cpu(buf[2]);
-	if (zero_or_saturated(len))
+	if (str_read(&key, fp, len))
 		goto err;
-	key = malloc(len + 1);
-	if (!key)
-		goto err;
-	rc = next_entry(key, fp, len);
-	if (rc < 0)
-		goto err;
-	key[len] = 0;
 
 	if (p->policy_type != POLICY_KERN &&
 	    p->policyvers >= MOD_POLICYDB_VERSION_TUNABLE_SEP) {
libsepol/src/module.c +32 -34

83 lines not on this page · raw message

libsepol/src/policydb.c +1 -9
index 5f888d3..cdb3cde 100644
@@ -1911,19 +1911,11 @@ static int perm_read(policydb_t * p
 		goto bad;
 
 	len = le32_to_cpu(buf[0]);
-	if (zero_or_saturated(len))
+	if(str_read(&key, fp, len))
 		goto bad;
 
 	perdatum->s.value = le32_to_cpu(buf[1]);
 
-	key = malloc(len + 1);
-	if (!key)
-		goto bad;
-	rc = next_entry(key, fp, len);
-	if (rc < 0)
-		goto bad;
-	key[len] = 0;
-
 	if (hashtab_insert(h, key, perdatum))
 		goto bad;
 
libsepol/src/private.h +1 -0
index 0beb4d4..b884c23 100644
@@ -65,3 +65,4 @@ extern struct policydb_compat_info *policydb_lookup_compat(unsigned int version,
 extern int next_entry(void *buf, struct policy_file *fp, size_t bytes) hidden;
 extern size_t put_entry(const void *ptr, size_t size, size_t n,
 		        struct policy_file *fp) hidden;
+extern int str_read(char **strp, struct policy_file *fp, size_t len) hidden;
libsepol/src/services.c +33 -0
index d2b80b4..f61f692 100644
@@ -1679,6 +1679,39 @@ size_t hidden put_entry(const void *ptr, size_t size, size_t n,
 }
 
 /*
+ * Reads a string and null terminates it from the policy file.
+ * This is a port of str_read from the SE Linux kernel code.
+ *
+ * It returns:
+ *   0 - Success
+ *   EINVAL - len is no good
+ *   ENOMEM - allocation failed
+ *   or any error possible from next_entry().
+ */
+int hidden str_read(char **strp, struct policy_file *fp, size_t len)
+{
+	int rc;
+	char *str;
+
+	if (zero_or_saturated(len))
+		return EINVAL;
+
+	str = malloc(len + 1);
+	if (!str)
+		return ENOMEM;
+
+	/* it's expected the caller should free the str */
+	*strp = str;
+
+	rc = next_entry(str, fp, len);
+	if (rc)
+		return rc;
+
+	str[len] = '\0';
+	return 0;
+}
+
+/*
  * Read a new set of configuration data from 
  * a policy database binary representation file.
  *
Signature · 1 line
1.9.1
#2 lore raw

Re: [PATCH 2/2] libsepol: port str_read from kernel

Quoted 75 lines · On 08/18/2016 04:54 PM, [email protected] wrote:

From: William Roberts <[email protected]>

Rather than duplicating the following sequence:
1. Read len from file
2. alloc up space based on 1
3. read the contents into the buffer from 2
4. null terminate the buffer from 2

Use the str_read() function that is in the kernel, which collapses steps 2 and 4. This not only reduces redundant code, but also has the side-affect of providing a central check on zero_or_saturated lengths from step 1 when generating string values.

---
 libsepol/src/conditional.c |  9 +------
 libsepol/src/module.c      | 66 ++++++++++++++++++++++------------------------
 libsepol/src/policydb.c    | 10 +------
 libsepol/src/private.h     |  1 +
 libsepol/src/services.c    | 33 +++++++++++++++++++++++
 5 files changed, 68 insertions(+), 51 deletions(-)
libsepol/src/conditional.c +1 -8
index 8680eb2..e1bc961 100644
@@ -589,15 +589,8 @@ int cond_read_bool(policydb_t * p,
 		goto err;
 
 	len = le32_to_cpu(buf[2]);
-	if (zero_or_saturated(len))
+	if (str_read(&key, fp, len))
 		goto err;
-	key = malloc(len + 1);
-	if (!key)
-		goto err;
-	rc = next_entry(key, fp, len);
-	if (rc < 0)
-		goto err;
-	key[len] = 0;
 
 	if (p->policy_type != POLICY_KERN &&
 	    p->policyvers >= MOD_POLICYDB_VERSION_TUNABLE_SEP) {
libsepol/src/module.c +6 -16
index f25df95..a9d7c54 100644
@@ -793,26 +793,26 @@ int sepol_module_package_info(struct sepol_policy_file *spf, int *type,
 				    i);
 				goto cleanup;
 			}
+
 			len = le32_to_cpu(buf[0]);
-			if (zero_or_saturated(len)) {
-				ERR(file->handle,
-				    "invalid module name length: 0x%"PRIx32,
-				    len);
-				goto cleanup;
-			}
-			*name = malloc(len + 1);
-			if (!*name) {
-				ERR(file->handle, "out of memory");
-				goto cleanup;
-			}
-			rc = next_entry(*name, file, len);
-			if (rc < 0) {
-				ERR(file->handle,
-				    "cannot get module name string (at section %u)",
-				    i);
+			if (str_read(name, file, len)) {
+				switch(rc) {
+				case EINVAL:
+					ERR(file->handle,
+						"invalid module name length: 0x%"PRIx32,

libsepol/src/module.c int sepol_module_package_info(struct sepol_policy_file *spf, int *type, · line 812

Quoted 10 lines
libsepol/src/module.c +10 -0
+						len);
+					break;
+				case ENOMEM:
+					ERR(file->handle, "out of memory");
+					break;
+				default:
+					ERR(file->handle,
+						"cannot get module name string (at section %u)",
+						i);
+				}

1) You didn't set rc = str_read(), so you can't switch on it above.
2) Using positive values for errors is likely to confuse matters when interacting with the existing code, which always uses negative values (either -errno as a legacy of common code with the kernel or -1).
3) I think this overcomplicates the error handling / reporting. If str_read() were to set errno and return -1 instead, then you could just include strerror(errno) in a single error message. Or you can just always report the most general error message. But it isn't worth a switch statement.

Same applies throughout.

Quoted 127 lines
libsepol/src/module.c +16 -18
 				goto cleanup;
 			}
-			(*name)[len] = '\0';
+
 			rc = next_entry(buf, file, sizeof(uint32_t));
 			if (rc < 0) {
 				ERR(file->handle,
@@ -821,25 +821,23 @@ int sepol_module_package_info(struct sepol_policy_file *spf, int *type,
 				goto cleanup;
 			}
 			len = le32_to_cpu(buf[0]);
-			if (zero_or_saturated(len)) {
-				ERR(file->handle,
-				    "invalid module version length: 0x%"PRIx32,
-				    len);
-				goto cleanup;
-			}
-			*version = malloc(len + 1);
-			if (!*version) {
-				ERR(file->handle, "out of memory");
-				goto cleanup;
-			}
-			rc = next_entry(*version, file, len);
-			if (rc < 0) {
-				ERR(file->handle,
-				    "cannot get module version string (at section %u)",
-				    i);
+			if (str_read(version, file, len)) {
+				switch(rc) {
+				case EINVAL:
+					ERR(file->handle,
+						"invalid module name length: 0x%"PRIx32,
+						len);
+					break;
+				case ENOMEM:
+					ERR(file->handle, "out of memory");
+					break;
+				default:
+					ERR(file->handle,
+						"cannot get module version string (at section %u)",
+						i);
+				}
 				goto cleanup;
 			}
-			(*version)[len] = '\0';
 			seen |= SEEN_MOD;
 			break;
 		default:
libsepol/src/policydb.c +1 -9
index 5f888d3..cdb3cde 100644
@@ -1911,19 +1911,11 @@ static int perm_read(policydb_t * p
 		goto bad;
 
 	len = le32_to_cpu(buf[0]);
-	if (zero_or_saturated(len))
+	if(str_read(&key, fp, len))
 		goto bad;
 
 	perdatum->s.value = le32_to_cpu(buf[1]);
 
-	key = malloc(len + 1);
-	if (!key)
-		goto bad;
-	rc = next_entry(key, fp, len);
-	if (rc < 0)
-		goto bad;
-	key[len] = 0;
-
 	if (hashtab_insert(h, key, perdatum))
 		goto bad;
 
libsepol/src/private.h +1 -0
index 0beb4d4..b884c23 100644
@@ -65,3 +65,4 @@ extern struct policydb_compat_info *policydb_lookup_compat(unsigned int version,
 extern int next_entry(void *buf, struct policy_file *fp, size_t bytes) hidden;
 extern size_t put_entry(const void *ptr, size_t size, size_t n,
 		        struct policy_file *fp) hidden;
+extern int str_read(char **strp, struct policy_file *fp, size_t len) hidden;
libsepol/src/services.c +33 -0
index d2b80b4..f61f692 100644
@@ -1679,6 +1679,39 @@ size_t hidden put_entry(const void *ptr, size_t size, size_t n,
 }
 
 /*
+ * Reads a string and null terminates it from the policy file.
+ * This is a port of str_read from the SE Linux kernel code.
+ *
+ * It returns:
+ *   0 - Success
+ *   EINVAL - len is no good
+ *   ENOMEM - allocation failed
+ *   or any error possible from next_entry().
+ */
+int hidden str_read(char **strp, struct policy_file *fp, size_t len)
+{
+	int rc;
+	char *str;
+
+	if (zero_or_saturated(len))
+		return EINVAL;
+
+	str = malloc(len + 1);
+	if (!str)
+		return ENOMEM;
+
+	/* it's expected the caller should free the str */
+	*strp = str;
+
+	rc = next_entry(str, fp, len);
+	if (rc)
+		return rc;
+
+	str[len] = '\0';
+	return 0;
+}
+
+/*
  * Read a new set of configuration data from 
  * a policy database binary representation file.
  *
#3 lore raw

Re: [PATCH 2/2] libsepol: port str_read from kernel

Quoted 16 lines · Stephen Smalley
Quoted 14 lines · On 08/18/2016 04:54 PM, [email protected] wrote:

From: William Roberts <[email protected]>

Rather than duplicating the following sequence:
1. Read len from file
2. alloc up space based on 1
3. read the contents into the buffer from 2
4. null terminate the buffer from 2

Use the str_read() function that is in the kernel, which collapses steps 2 and 4. This not only reduces redundant code, but also has the side-affect of providing a central check on zero_or_saturated lengths from step 1 when generating string values.

Quoted 4 lines
Quoted 4 lines
---
 libsepol/src/conditional.c |  9 +------
 libsepol/src/module.c      | 66

++++++++++++++++++++++------------------------

Quoted 24 lines
Quoted 24 lines
 libsepol/src/policydb.c    | 10 +------
 libsepol/src/private.h     |  1 +
 libsepol/src/services.c    | 33 +++++++++++++++++++++++
 5 files changed, 68 insertions(+), 51 deletions(-)
libsepol/src/conditional.c +1 -8
index 8680eb2..e1bc961 100644
@@ -589,15 +589,8 @@ int cond_read_bool(policydb_t * p,
              goto err;
 
      len = le32_to_cpu(buf[2]);
-     if (zero_or_saturated(len))
+     if (str_read(&key, fp, len))
              goto err;
-     key = malloc(len + 1);
-     if (!key)
-             goto err;
-     rc = next_entry(key, fp, len);
-     if (rc < 0)
-             goto err;
-     key[len] = 0;
 
Quoted 7 lines

libsepol/src/module.c int sepol_module_package_info(struct

Quoted 7 lines
libsepol/src/conditional.c +0 -0
      if (p->policy_type != POLICY_KERN &&
          p->policyvers >= MOD_POLICYDB_VERSION_TUNABLE_SEP) {
libsepol/src/module.c +0 -0
index f25df95..a9d7c54 100644
@@ -793,26 +793,26 @@ int sepol_module_package_info(struct

sepol_policy_file *spf, int *type,

Quoted 8 lines

libsepol/src/module.c int sepol_module_package_info(struct sepol_policy_file *spf, int *type,

Quoted 8 lines
libsepol/src/module.c +1 -3
                                  i);
                              goto cleanup;
                      }
+
                      len = le32_to_cpu(buf[0]);
-                     if (zero_or_saturated(len)) {
-                             ERR(file->handle,
-                                 "invalid module name length:

0x%"PRIx32,

Quoted 4 lines
Quoted 4 lines
libsepol/src/module.c +0 -4
-                                 len);
-                             goto cleanup;
-                     }
-                     *name = malloc(len + 1);
Quoted 8 lines

libsepol/src/module.c int sepol_module_package_info(struct sepol_policy_file *spf, int *type,

Quoted 8 lines
libsepol/src/module.c +0 -8
-                     if (!*name) {
-                             ERR(file->handle, "out of memory");
-                             goto cleanup;
-                     }
-                     rc = next_entry(*name, file, len);
-                     if (rc < 0) {
-                             ERR(file->handle,
-                                 "cannot get module name string (at

section %u)",

Quoted 6 lines

libsepol/src/module.c int sepol_module_package_info(struct sepol_policy_file *spf, int *type,

Quoted 6 lines
libsepol/src/module.c +5 -1
-                                 i);
+                     if (str_read(name, file, len)) {
+                             switch(rc) {
+                             case EINVAL:
+                                     ERR(file->handle,
+                                             "invalid module name

length: 0x%"PRIx32,

Quoted 4 lines

libsepol/src/module.c int sepol_module_package_info(struct sepol_policy_file *spf, int *type,

Quoted 4 lines
libsepol/src/module.c +4 -0
+                                             len);
+                                     break;
+                             case ENOMEM:
+                                     ERR(file->handle, "out of

memory");

Quoted 4 lines

libsepol/src/module.c int sepol_module_package_info(struct sepol_policy_file *spf, int *type,

Quoted 4 lines
libsepol/src/module.c +4 -0
+                                     break;
+                             default:
+                                     ERR(file->handle,
+                                             "cannot get module name

string (at section %u)",

Quoted 4 lines

libsepol/src/module.c int sepol_module_package_info(struct · line 812

Quoted 2 lines
libsepol/src/module.c +2 -0
+                                             i);
+                             }

1) You didn't set rc = str_read(), so you can't switch on it above.

It's a new gnuc extension ;-p

Quoted 8 lines

2) Using positive values for errors is likely to confuse matters when interacting with the existing code, which always uses negative values (either -errno as a legacy of common code with the kernel or -1).
3) I think this overcomplicates the error handling / reporting. If str_read() were to set errno and return -1 instead, then you could just include strerror(errno) in a single error message. Or you can just always report the most general error message. But it isn't worth a switch statement.

Yeah I had all those thoughts, wasn't sure the best way considering the conflict on -1 vs -errno. If you're OK with setting errno let's use that.

Quoted 11 lines

Same applies throughout.

libsepol/src/module.c int sepol_module_package_info(struct

Quoted 8 lines
libsepol/src/module.c +1 -1
                              goto cleanup;
                      }
-                     (*name)[len] = '\0';
+
                      rc = next_entry(buf, file, sizeof(uint32_t));
                      if (rc < 0) {
                              ERR(file->handle,
@@ -821,25 +821,23 @@ int sepol_module_package_info(struct

sepol_policy_file *spf, int *type,

Quoted 6 lines

libsepol/src/module.c int sepol_module_package_info(struct sepol_policy_file *spf, int *type,

Quoted 6 lines
libsepol/src/module.c +0 -3
                              goto cleanup;
                      }
                      len = le32_to_cpu(buf[0]);
-                     if (zero_or_saturated(len)) {
-                             ERR(file->handle,
-                                 "invalid module version length:

0x%"PRIx32,

Quoted 4 lines
Quoted 4 lines
libsepol/src/module.c +0 -4
-                                 len);
-                             goto cleanup;
-                     }
-                     *version = malloc(len + 1);
Quoted 8 lines

libsepol/src/module.c int sepol_module_package_info(struct sepol_policy_file *spf, int *type,

Quoted 8 lines
libsepol/src/module.c +0 -8
-                     if (!*version) {
-                             ERR(file->handle, "out of memory");
-                             goto cleanup;
-                     }
-                     rc = next_entry(*version, file, len);
-                     if (rc < 0) {
-                             ERR(file->handle,
-                                 "cannot get module version string (at

section %u)",

Quoted 6 lines

libsepol/src/module.c int sepol_module_package_info(struct sepol_policy_file *spf, int *type,

Quoted 6 lines
libsepol/src/module.c +5 -1
-                                 i);
+                     if (str_read(version, file, len)) {
+                             switch(rc) {
+                             case EINVAL:
+                                     ERR(file->handle,
+                                             "invalid module name

length: 0x%"PRIx32,

Quoted 4 lines

libsepol/src/module.c int sepol_module_package_info(struct sepol_policy_file *spf, int *type,

Quoted 4 lines
libsepol/src/module.c +4 -0
+                                             len);
+                                     break;
+                             case ENOMEM:
+                                     ERR(file->handle, "out of

memory");

Quoted 4 lines

libsepol/src/module.c int sepol_module_package_info(struct sepol_policy_file *spf, int *type,

Quoted 4 lines
libsepol/src/module.c +4 -0
+                                     break;
+                             default:
+                                     ERR(file->handle,
+                                             "cannot get module

version string (at section %u)",

Quoted 33 lines
Quoted 33 lines
libsepol/src/module.c +2 -1
+                                             i);
+                             }
                              goto cleanup;
                      }
-                     (*version)[len] = '\0';
                      seen |= SEEN_MOD;
                      break;
              default:
libsepol/src/policydb.c +1 -9
index 5f888d3..cdb3cde 100644
@@ -1911,19 +1911,11 @@ static int perm_read(policydb_t * p
              goto bad;
 
      len = le32_to_cpu(buf[0]);
-     if (zero_or_saturated(len))
+     if(str_read(&key, fp, len))
              goto bad;
 
      perdatum->s.value = le32_to_cpu(buf[1]);
 
-     key = malloc(len + 1);
-     if (!key)
-             goto bad;
-     rc = next_entry(key, fp, len);
-     if (rc < 0)
-             goto bad;
-     key[len] = 0;
-
      if (hashtab_insert(h, key, perdatum))
              goto bad;
 
Quoted 5 lines

libsepol/src/private.h extern struct policydb_compat_info

Quoted 5 lines
libsepol/src/private.h +0 -0
index 0beb4d4..b884c23 100644
@@ -65,3 +65,4 @@ extern struct policydb_compat_info

*policydb_lookup_compat(unsigned int version,

Quoted 1 line

libsepol/src/private.h extern struct policydb_compat_info · line 65

Quoted 1 line
libsepol/src/private.h +0 -0
 extern int next_entry(void *buf, struct policy_file *fp, size_t bytes)

hidden;

Quoted 3 lines

libsepol/src/private.h extern struct policydb_compat_info *policydb_lookup_compat(unsigned int version,

Quoted 3 lines
libsepol/src/private.h +1 -0
 extern size_t put_entry(const void *ptr, size_t size, size_t n,
                      struct policy_file *fp) hidden;
+extern int str_read(char **strp, struct policy_file *fp, size_t len)

hidden;

Quoted 5 lines

libsepol/src/services.c size_t hidden put_entry(const void *ptr, size_t

Quoted 5 lines
libsepol/src/services.c +0 -0
index d2b80b4..f61f692 100644
@@ -1679,6 +1679,39 @@ size_t hidden put_entry(const void *ptr, size_t

size, size_t n,

Quoted 38 lines
Quoted 38 lines
libsepol/src/services.c +33 -0
 }
 
 /*
+ * Reads a string and null terminates it from the policy file.
+ * This is a port of str_read from the SE Linux kernel code.
+ *
+ * It returns:
+ *   0 - Success
+ *   EINVAL - len is no good
+ *   ENOMEM - allocation failed
+ *   or any error possible from next_entry().
+ */
+int hidden str_read(char **strp, struct policy_file *fp, size_t len)
+{
+     int rc;
+     char *str;
+
+     if (zero_or_saturated(len))
+             return EINVAL;
+
+     str = malloc(len + 1);
+     if (!str)
+             return ENOMEM;
+
+     /* it's expected the caller should free the str */
+     *strp = str;
+
+     rc = next_entry(str, fp, len);
+     if (rc)
+             return rc;
+
+     str[len] = '\0';
+     return 0;
+}
+
+/*
  * Read a new set of configuration data from
  * a policy database binary representation file.
Quoted 8 lines

libsepol/src/services.c size_t hidden put_entry(const void *ptr, size_t · line 1717

Quoted 2 lines
libsepol/src/services.c +0 -0
  *

_______________________________________________
Seandroid-list mailing list [email protected]
To unsubscribe, send email to [email protected]. To get help, send an email containing "help" to

[email protected].

#4 lore raw

Re: [PATCH 2/2] libsepol: port str_read from kernel

Quoted 113 lines · William Roberts
Quoted 2 lines · On Aug 19, 2016 06:12, "Stephen Smalley" <[email protected]…

On 08/18/2016 04:54 PM, [email protected]

Quoted 1 line · <mailto:[email protected]> wrote:
Quoted 1 line

From: William Roberts <[email protected]

<mailto:[email protected]>>

Quoted 7 lines
Quoted 7 lines

Rather than duplicating the following sequence:
1. Read len from file
2. alloc up space based on 1
3. read the contents into the buffer from 2
4. null terminate the buffer from 2

Quoted 7 lines
Quoted 7 lines

Use the str_read() function that is in the kernel, which collapses steps 2 and 4. This not only reduces redundant code, but also has the side-affect of providing a central check on zero_or_saturated lengths from step 1 when generating string values.

<mailto:[email protected]>>

Quoted 3 lines
Quoted 3 lines
---
 libsepol/src/conditional.c |  9 +------
 libsepol/src/module.c      | 66

++++++++++++++++++++++------------------------

Quoted 24 lines
Quoted 24 lines
 libsepol/src/policydb.c    | 10 +------
 libsepol/src/private.h     |  1 +
 libsepol/src/services.c    | 33 +++++++++++++++++++++++
 5 files changed, 68 insertions(+), 51 deletions(-)
libsepol/src/conditional.c +1 -8
index 8680eb2..e1bc961 100644
@@ -589,15 +589,8 @@ int cond_read_bool(policydb_t * p,
              goto err;
 
      len = le32_to_cpu(buf[2]);
-     if (zero_or_saturated(len))
+     if (str_read(&key, fp, len))
              goto err;
-     key = malloc(len + 1);
-     if (!key)
-             goto err;
-     rc = next_entry(key, fp, len);
-     if (rc < 0)
-             goto err;
-     key[len] = 0;
 
Quoted 7 lines

libsepol/src/module.c int sepol_module_package_info(struct

Quoted 7 lines
libsepol/src/conditional.c +0 -0
      if (p->policy_type != POLICY_KERN &&
          p->policyvers >= MOD_POLICYDB_VERSION_TUNABLE_SEP) {
libsepol/src/module.c +0 -0
index f25df95..a9d7c54 100644
@@ -793,26 +793,26 @@ int sepol_module_package_info(struct

sepol_policy_file *spf, int *type,

Quoted 8 lines

libsepol/src/module.c int sepol_module_package_info(struct sepol_policy_file *spf, int *type,

Quoted 8 lines
libsepol/src/module.c +1 -3
                                  i);
                              goto cleanup;
                      }
+
                      len = le32_to_cpu(buf[0]);
-                     if (zero_or_saturated(len)) {
-                             ERR(file->handle,
-                                 "invalid module name length:

0x%"PRIx32,

Quoted 4 lines
Quoted 4 lines
libsepol/src/module.c +0 -4
-                                 len);
-                             goto cleanup;
-                     }
-                     *name = malloc(len + 1);
Quoted 8 lines

libsepol/src/module.c int sepol_module_package_info(struct sepol_policy_file *spf, int *type,

Quoted 8 lines
libsepol/src/module.c +0 -8
-                     if (!*name) {
-                             ERR(file->handle, "out of memory");
-                             goto cleanup;
-                     }
-                     rc = next_entry(*name, file, len);
-                     if (rc < 0) {
-                             ERR(file->handle,
-                                 "cannot get module name string (at

section %u)",

Quoted 6 lines

libsepol/src/module.c int sepol_module_package_info(struct sepol_policy_file *spf, int *type,

Quoted 6 lines
libsepol/src/module.c +5 -1
-                                 i);
+                     if (str_read(name, file, len)) {
+                             switch(rc) {
+                             case EINVAL:
+                                     ERR(file->handle,
+                                             "invalid module name

length: 0x%"PRIx32,

Quoted 4 lines

libsepol/src/module.c int sepol_module_package_info(struct sepol_policy_file *spf, int *type,

Quoted 4 lines
libsepol/src/module.c +4 -0
+                                             len);
+                                     break;
+                             case ENOMEM:
+                                     ERR(file->handle, "out of

memory");

Quoted 4 lines

libsepol/src/module.c int sepol_module_package_info(struct sepol_policy_file *spf, int *type,

Quoted 4 lines
libsepol/src/module.c +4 -0
+                                     break;
+                             default:
+                                     ERR(file->handle,
+                                             "cannot get module

name string (at section %u)",

Quoted 4 lines

libsepol/src/module.c int sepol_module_package_info(struct · line 812

Quoted 2 lines
libsepol/src/module.c +2 -0
+                                             i);
+                             }

1) You didn't set rc = str_read(), so you can't switch on it above.

It's a new gnuc extension ;-p

Quoted 8 lines

2) Using positive values for errors is likely to confuse matters when interacting with the existing code, which always uses negative values (either -errno as a legacy of common code with the kernel or -1).
3) I think this overcomplicates the error handling / reporting. If str_read() were to set errno and return -1 instead, then you could just include strerror(errno) in a single error message. Or you can just always report the most general error message. But it isn't worth a switch statement.

Quoted 2 lines

Yeah I had all those thoughts, wasn't sure the best way considering the conflict on -1 vs -errno. If you're OK with setting errno let's use that.

Yes, setting errno is fine with me. Not even necessary if the malloc fails.

#5 lore raw
In reply to [email protected]
Quoted 86 lines · On 08/18/2016 04:54 PM, [email protected] wrote:

From: William Roberts <[email protected]>

The usage patterns between these structures seem similair to role_val_to_struct usages. Calloc these up to prevent any unitialized usages.

---
 libsepol/src/mls.c      | 2 +-
 libsepol/src/policydb.c | 6 +++---
 libsepol/src/users.c    | 9 ++++++++-
 3 files changed, 12 insertions(+), 5 deletions(-)
libsepol/src/mls.c +1 -1
index 2dc5f2b..8047d91 100644
@@ -312,7 +312,7 @@ int mls_context_isvalid(const policydb_t * p, const context_struct_t * c)
 	if (!c->user || c->user > p->p_users.nprim)
 		return 0;
 	usrdatum = p->user_val_to_struct[c->user - 1];
-	if (!mls_range_contains(usrdatum->exp_range, c->range))
+	if (!usrdatum || !mls_range_contains(usrdatum->exp_range, c->range))
 		return 0;	/* user may not be associated with range */
 
 	return 1;
libsepol/src/policydb.c +3 -3
index c225ac6..5f888d3 100644
@@ -1074,7 +1074,7 @@ int policydb_index_others(sepol_handle_t * handle,
 
 	free(p->user_val_to_struct);
 	p->user_val_to_struct = (user_datum_t **)
-	    malloc(p->p_users.nprim * sizeof(user_datum_t *));
+	    calloc(p->p_users.nprim, sizeof(user_datum_t *));
 	if (!p->user_val_to_struct)
 		return -1;
 
@@ -4006,12 +4006,12 @@ int policydb_reindex_users(policydb_t * p)
 		free(p->sym_val_to_name[i]);
 
 	p->user_val_to_struct = (user_datum_t **)
-	    malloc(p->p_users.nprim * sizeof(user_datum_t *));
+	    calloc(p->p_users.nprim, sizeof(user_datum_t *));
 	if (!p->user_val_to_struct)
 		return -1;
 
 	p->sym_val_to_name[i] = (char **)
-	    malloc(p->symtab[i].nprim * sizeof(char *));
+	    calloc(p->symtab[i].nprim, sizeof(char *));
 	if (!p->sym_val_to_name[i])
 		return -1;
 
libsepol/src/users.c +7 -1
index ce54c2b..3ffb166 100644
@@ -19,12 +19,17 @@ static int user_to_record(sepol_handle_t * handle,
 
 	const char *name = policydb->p_user_val_to_name[user_idx];
 	user_datum_t *usrdatum = policydb->user_val_to_struct[user_idx];
-	ebitmap_t *roles = &(usrdatum->roles.roles);
+	ebitmap_t *roles;
 	ebitmap_node_t *rnode;
 	unsigned bit;
 
 	sepol_user_t *tmp_record = NULL;
 
+	if (!usrdatum)
+		goto err;
+
+	roles = &(usrdatum->roles.roles);
+
 	if (sepol_user_create(handle, &tmp_record) < 0)
 		goto err;
 
@@ -234,6 +239,7 @@ int sepol_user_modify(sepol_handle_t * handle,
 		if (!tmp_ptr)
 			goto omem;
 		policydb->user_val_to_struct = tmp_ptr;
+		policydb->user_val_to_struct[policydb->p_users.nprim] = NULL;
 
 		tmp_ptr = realloc(policydb->sym_val_to_name[SYM_USERS],
 				  (policydb->p_users.nprim +

libsepol/src/users.c int sepol_user_modify(sepol_handle_t * handle, · line 250

Quoted 4 lines
libsepol/src/users.c +1 -0
@@ -241,6 +247,7 @@ int sepol_user_modify(sepol_handle_t * handle,
 		if (!tmp_ptr)
 			goto omem;
 		policydb->sym_val_to_name[SYM_USERS] = tmp_ptr;
+		policydb->p_user_val_to_name[policydb->p_users.nprim] = NULL;

This one is wrong.

Quoted 4 lines
libsepol/src/users.c +0 -0
 		/* Need to copy the user name */
 		name = strdup(cname);
 
#6 lore raw
Quoted 104 lines

-----Original Message-----
From: Stephen Smalley [mailto:[email protected]]
Sent: Friday, August 19, 2016 6:06 AM
To: Roberts, William C <[email protected]>; [email protected]; [email protected]; [email protected]
Subject: Re: [PATCH 1/2] libsepol: calloc all the *_to_val_structs

libsepol/src/mls.c int mls_context_isvalid(const policydb_t * p, const

Quoted 18 lines · On 08/18/2016 04:54 PM, [email protected] wrote:

From: William Roberts <[email protected]>

The usage patterns between these structures seem similair to role_val_to_struct usages. Calloc these up to prevent any unitialized usages.

---
 libsepol/src/mls.c      | 2 +-
 libsepol/src/policydb.c | 6 +++---
 libsepol/src/users.c    | 9 ++++++++-
 3 files changed, 12 insertions(+), 5 deletions(-)
libsepol/src/mls.c index renamed from libsepol/src/mls.c +0 -0

2dc5f2b..8047d91 100644

libsepol/src/mls.c +0 -0
@@ -312,7 +312,7 @@ int mls_context_isvalid(const policydb_t * p, const

context_struct_t * c)

Quoted 69 lines
libsepol/src/mls.c +1 -1
 	if (!c->user || c->user > p->p_users.nprim)
 		return 0;
 	usrdatum = p->user_val_to_struct[c->user - 1];
-	if (!mls_range_contains(usrdatum->exp_range, c->range))
+	if (!usrdatum || !mls_range_contains(usrdatum->exp_range, c->range))
 		return 0;	/* user may not be associated with range */
 
 	return 1;
libsepol/src/policydb.c index renamed from libsepol/src/policydb.c +0 -0

c225ac6..5f888d3 100644

libsepol/src/policydb.c +3 -3
@@ -1074,7 +1074,7 @@ int policydb_index_others(sepol_handle_t *
 handle,
 
 	free(p->user_val_to_struct);
 	p->user_val_to_struct = (user_datum_t **)
-	    malloc(p->p_users.nprim * sizeof(user_datum_t *));
+	    calloc(p->p_users.nprim, sizeof(user_datum_t *));
 	if (!p->user_val_to_struct)
 		return -1;
@@ -4006,12 +4006,12 @@ int policydb_reindex_users(policydb_t * p)
 		free(p->sym_val_to_name[i]);
 
 	p->user_val_to_struct = (user_datum_t **)
-	    malloc(p->p_users.nprim * sizeof(user_datum_t *));
+	    calloc(p->p_users.nprim, sizeof(user_datum_t *));
 	if (!p->user_val_to_struct)
 		return -1;
 
 	p->sym_val_to_name[i] = (char **)
-	    malloc(p->symtab[i].nprim * sizeof(char *));
+	    calloc(p->symtab[i].nprim, sizeof(char *));
 	if (!p->sym_val_to_name[i])
 		return -1;
 
libsepol/src/users.c index renamed from libsepol/src/users.c +0 -0

ce54c2b..3ffb166 100644

libsepol/src/users.c +7 -1
@@ -19,12 +19,17 @@ static int user_to_record(sepol_handle_t * handle,
 
 	const char *name = policydb->p_user_val_to_name[user_idx];
 	user_datum_t *usrdatum = policydb->user_val_to_struct[user_idx];
-	ebitmap_t *roles = &(usrdatum->roles.roles);
+	ebitmap_t *roles;
 	ebitmap_node_t *rnode;
 	unsigned bit;
 
 	sepol_user_t *tmp_record = NULL;
 
+	if (!usrdatum)
+		goto err;
+
+	roles = &(usrdatum->roles.roles);
+
 	if (sepol_user_create(handle, &tmp_record) < 0)
 		goto err;
 
@@ -234,6 +239,7 @@ int sepol_user_modify(sepol_handle_t * handle,
 		if (!tmp_ptr)
 			goto omem;
 		policydb->user_val_to_struct = tmp_ptr;
+		policydb->user_val_to_struct[policydb->p_users.nprim] = NULL;
 
 		tmp_ptr = realloc(policydb->sym_val_to_name[SYM_USERS],
 				  (policydb->p_users.nprim +

libsepol/src/users.c int sepol_user_modify(sepol_handle_t * handle, · line 250

Quoted 4 lines
libsepol/src/users.c +1 -0
@@ -241,6 +247,7 @@ int sepol_user_modify(sepol_handle_t * handle,
 		if (!tmp_ptr)
 			goto omem;
 		policydb->sym_val_to_name[SYM_USERS] = tmp_ptr;
+		policydb->p_user_val_to_name[policydb->p_users.nprim] =

NULL;

Quoted 6 lines

This one is wrong.

Quoted 4 lines
libsepol/src/users.c +0 -0
 		/* Need to copy the user name */
 		name = strdup(cname);
 

After looking at the email and the patch again, that’s just a context hunk, I see no + or - next it, And I verified it still exists in my source tree. I never touched that hunk or am I missing Some subtle interaction?

#7 lore raw
Quoted 107 lines · William C
Quoted 107 lines

-----Original Message-----
From: Stephen Smalley [mailto:[email protected]]
Sent: Friday, August 19, 2016 6:06 AM
To: Roberts, William C <[email protected]>; [email protected]; [email protected]; [email protected]
Subject: Re: [PATCH 1/2] libsepol: calloc all the *_to_val_structs

libsepol/src/mls.c int mls_context_isvalid(const policydb_t * p, const

Quoted 18 lines · On 08/18/2016 04:54 PM, [email protected] wrote:

From: William Roberts <[email protected]>

The usage patterns between these structures seem similair to role_val_to_struct usages. Calloc these up to prevent any unitialized usages.

---
 libsepol/src/mls.c      | 2 +-
 libsepol/src/policydb.c | 6 +++---
 libsepol/src/users.c    | 9 ++++++++-
 3 files changed, 12 insertions(+), 5 deletions(-)
libsepol/src/mls.c index renamed from libsepol/src/mls.c +0 -0

2dc5f2b..8047d91 100644

libsepol/src/mls.c +0 -0
@@ -312,7 +312,7 @@ int mls_context_isvalid(const policydb_t * p, const

context_struct_t * c)

Quoted 69 lines
libsepol/src/mls.c +1 -1
 	if (!c->user || c->user > p->p_users.nprim)
 		return 0;
 	usrdatum = p->user_val_to_struct[c->user - 1];
-	if (!mls_range_contains(usrdatum->exp_range, c->range))
+	if (!usrdatum || !mls_range_contains(usrdatum->exp_range, c->range))
 		return 0;	/* user may not be associated with range */
 
 	return 1;
libsepol/src/policydb.c index renamed from libsepol/src/policydb.c +0 -0

c225ac6..5f888d3 100644

libsepol/src/policydb.c +3 -3
@@ -1074,7 +1074,7 @@ int policydb_index_others(sepol_handle_t *
 handle,
 
 	free(p->user_val_to_struct);
 	p->user_val_to_struct = (user_datum_t **)
-	    malloc(p->p_users.nprim * sizeof(user_datum_t *));
+	    calloc(p->p_users.nprim, sizeof(user_datum_t *));
 	if (!p->user_val_to_struct)
 		return -1;
@@ -4006,12 +4006,12 @@ int policydb_reindex_users(policydb_t * p)
 		free(p->sym_val_to_name[i]);
 
 	p->user_val_to_struct = (user_datum_t **)
-	    malloc(p->p_users.nprim * sizeof(user_datum_t *));
+	    calloc(p->p_users.nprim, sizeof(user_datum_t *));
 	if (!p->user_val_to_struct)
 		return -1;
 
 	p->sym_val_to_name[i] = (char **)
-	    malloc(p->symtab[i].nprim * sizeof(char *));
+	    calloc(p->symtab[i].nprim, sizeof(char *));
 	if (!p->sym_val_to_name[i])
 		return -1;
 
libsepol/src/users.c index renamed from libsepol/src/users.c +0 -0

ce54c2b..3ffb166 100644

libsepol/src/users.c +7 -1
@@ -19,12 +19,17 @@ static int user_to_record(sepol_handle_t * handle,
 
 	const char *name = policydb->p_user_val_to_name[user_idx];
 	user_datum_t *usrdatum = policydb->user_val_to_struct[user_idx];
-	ebitmap_t *roles = &(usrdatum->roles.roles);
+	ebitmap_t *roles;
 	ebitmap_node_t *rnode;
 	unsigned bit;
 
 	sepol_user_t *tmp_record = NULL;
 
+	if (!usrdatum)
+		goto err;
+
+	roles = &(usrdatum->roles.roles);
+
 	if (sepol_user_create(handle, &tmp_record) < 0)
 		goto err;
 
@@ -234,6 +239,7 @@ int sepol_user_modify(sepol_handle_t * handle,
 		if (!tmp_ptr)
 			goto omem;
 		policydb->user_val_to_struct = tmp_ptr;
+		policydb->user_val_to_struct[policydb->p_users.nprim] = NULL;
 
 		tmp_ptr = realloc(policydb->sym_val_to_name[SYM_USERS],
 				  (policydb->p_users.nprim +

libsepol/src/users.c int sepol_user_modify(sepol_handle_t * handle, · line 250

Quoted 4 lines
libsepol/src/users.c +1 -0
@@ -241,6 +247,7 @@ int sepol_user_modify(sepol_handle_t * handle,
 		if (!tmp_ptr)
 			goto omem;
 		policydb->sym_val_to_name[SYM_USERS] = tmp_ptr;
+		policydb->p_user_val_to_name[policydb->p_users.nprim] =

NULL;

This one is wrong.

Quoted 1 line
Quoted 7 lines
Quoted 3 lines

libsepol/src/users.c int sepol_user_modify(sepol_handle_t * handle, · line 253

Quoted 3 lines
libsepol/src/users.c +0 -0
 		/* Need to copy the user name */
 		name = strdup(cname);

After looking at the email and the patch again, that’s just a context hunk, I see no + or - next it, And I verified it still exists in my source tree. I never touched that hunk or am I missing Some subtle interaction?

I was referring to the lines above that you added. But on second thought, never mind - I think this one is ok.