Skip to contentSkip to conversation

libsepol: initialize tmp_key->ibdev_name if its allocation failed

PATCH Applied Nicolas Iooss 3 participants 11 messages Mar 5, 2018 – Mar 8, 2018 SELinux security module

lore
Share & tools
lore
#0 lore raw

PATCH 1/3 libsepol: initialize tmp_key->ibdev_name if its allocation failed

In sepol_ibendport_key_create(), if sepol_ibendport_alloc_ibdev_name() fails to allocate tmp_key->ibdev_name, sepol_ibendport_key_free() is called to free the memory associated with tmp_key, which results in free() being called on uninitialized tmp_key->ibdev_name.

This issue is reported by clang's static analyzer with the following message:

    ibendport_record.c:115:2: warning: 1st function call argument is an
    uninitialized value
            free(key->ibdev_name);
            ^~~~~~~~~~~~~~~~~~~~~
---
 libsepol/src/ibendport_record.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
libsepol/src/ibendport_record.c +3 -1
index 912aeb536fd9..8285a9d6ffcf 100644
@@ -62,8 +62,10 @@ int sepol_ibendport_key_create(sepol_handle_t *handle,
 		goto omem;
 	}
 
-	if (sepol_ibendport_alloc_ibdev_name(handle, &tmp_key->ibdev_name) < 0)
+	if (sepol_ibendport_alloc_ibdev_name(handle, &tmp_key->ibdev_name) < 0) {
+		tmp_key->ibdev_name = NULL;
 		goto err;
+	}
 
 	strncpy(tmp_key->ibdev_name, ibdev_name, IB_DEVICE_NAME_MAX);
 	tmp_key->port = port;
Signature · 1 line
2.16.0
#1 lore raw

PATCH 2/3 libsepol: cil: show an error when cil_expr_to_string() fails

cil_tree_print_expr() calls cil_expr_to_string() in order to compute a string expression into expr_str. If this function fails, expr_str is left unitialized but its value is dereferenced with:

    cil_log(CIL_INFO, "%s)", expr_str);

Prevent such an issue by checking cil_expr_to_string()'s return value before using expr_str.

This issue has been found with clang's static analyzer.

---
 libsepol/cil/src/cil_tree.c | 10 +++++++---
 1 file changed, 7 insertions(+), 3 deletions(-)
libsepol/cil/src/cil_tree.c +7 -3
index d36401b41dba..b394a9d849df 100644
@@ -503,15 +503,19 @@ exit:
 void cil_tree_print_expr(struct cil_list *datum_expr, struct cil_list *str_expr)
 {
 	char *expr_str;
+	int rc;
 
 	cil_log(CIL_INFO, "(");
 
 	if (datum_expr != NULL) {
-		cil_expr_to_string(datum_expr, &expr_str);
+		rc = cil_expr_to_string(datum_expr, &expr_str);
 	} else {
-		cil_expr_to_string(str_expr, &expr_str);
+		rc = cil_expr_to_string(str_expr, &expr_str);
+	}
+	if (rc < 0) {
+		cil_log(CIL_INFO, "ERROR)");
+		return;
 	}
-
 	cil_log(CIL_INFO, "%s)", expr_str);
 	free(expr_str);
 }
Signature · 1 line
2.16.0
#2 lore raw

Re: [PATCH 2/3] libsepol: cil: show an error when cil_expr_to_string() fails

Quoted 28 lines · Nicolas Iooss

cil_tree_print_expr() calls cil_expr_to_string() in order to compute a string expression into expr_str. If this function fails, expr_str is left unitialized but its value is dereferenced with:

    cil_log(CIL_INFO, "%s)", expr_str);

Prevent such an issue by checking cil_expr_to_string()'s return value before using expr_str.

This issue has been found with clang's static analyzer.

---
 libsepol/cil/src/cil_tree.c | 10 +++++++---
 1 file changed, 7 insertions(+), 3 deletions(-)
libsepol/cil/src/cil_tree.c +1 -0
index d36401b41dba..b394a9d849df 100644
@@ -503,15 +503,19 @@ exit:
 void cil_tree_print_expr(struct cil_list *datum_expr, struct cil_list *str_expr)
 {
 	char *expr_str;
+	int rc;
 
 	cil_log(CIL_INFO, "(");
 

libsepol/cil/src/cil_tree.c exit: · line 517

Quoted 10 lines
libsepol/cil/src/cil_tree.c +6 -2
 	if (datum_expr != NULL) {
-		cil_expr_to_string(datum_expr, &expr_str);
+		rc = cil_expr_to_string(datum_expr, &expr_str);
 	} else {
-		cil_expr_to_string(str_expr, &expr_str);
+		rc = cil_expr_to_string(str_expr, &expr_str);
+	}
+	if (rc < 0) {
+		cil_log(CIL_INFO, "ERROR)");
+		return;

Wondering if we should abort or return an error to the caller instead of just logging an error and returning?

Quoted 6 lines
libsepol/cil/src/cil_tree.c +0 -1
 	}
-
 	cil_log(CIL_INFO, "%s)", expr_str);
 	free(expr_str);
 }
#3 lore raw

Re: [PATCH 2/3] libsepol: cil: show an error when cil_expr_to_string() fails

Quoted 29 lines · Stephen Smalley
Quoted 28 lines · Nicolas Iooss

cil_tree_print_expr() calls cil_expr_to_string() in order to compute a string expression into expr_str. If this function fails, expr_str is left unitialized but its value is dereferenced with:

    cil_log(CIL_INFO, "%s)", expr_str);

Prevent such an issue by checking cil_expr_to_string()'s return value before using expr_str.

This issue has been found with clang's static analyzer.

---
 libsepol/cil/src/cil_tree.c | 10 +++++++---
 1 file changed, 7 insertions(+), 3 deletions(-)
libsepol/cil/src/cil_tree.c +1 -0
index d36401b41dba..b394a9d849df 100644
@@ -503,15 +503,19 @@ exit:
 void cil_tree_print_expr(struct cil_list *datum_expr, struct cil_list *str_expr)
 {
      char *expr_str;
+     int rc;
 
      cil_log(CIL_INFO, "(");
 
Quoted 12 lines

libsepol/cil/src/cil_tree.c exit: · line 517

Quoted 10 lines
libsepol/cil/src/cil_tree.c +6 -2
      if (datum_expr != NULL) {
-             cil_expr_to_string(datum_expr, &expr_str);
+             rc = cil_expr_to_string(datum_expr, &expr_str);
      } else {
-             cil_expr_to_string(str_expr, &expr_str);
+             rc = cil_expr_to_string(str_expr, &expr_str);
+     }
+     if (rc < 0) {
+             cil_log(CIL_INFO, "ERROR)");
+             return;

Wondering if we should abort or return an error to the caller instead of just logging an error and returning?

I believe this depends on the usage of cil_tree_print_expr(). I have not found any caller of cil_tree_print_* outside of libsepol/cil/src/cil_tree.c so I guess this interface is only used by developers for debugging purpose when playing with libsepol's CIL objects (being able to quickly print an object in a human-readable form is a very valuable feature). Am I right? The current interface of ignoring all errors and logging as much as possible without aborting seems consistent with such a use-case.

If there is an interest in propagating errors found by cil_tree_print_expr() back to its callers, I can spend some time to clean up the code. Otherwise I will send other patches (once they are in a suitable state) to fix some issues that clang's static analyzer find.

Thanks,
Nicolas

#4 lore raw

Re: Re: [PATCH 2/3] libsepol: cil: show an error when cil_expr_to_string() fails

Quoted 52 lines · Nicolas Iooss
Quoted 29 lines · Stephen Smalley
Quoted 28 lines · Nicolas Iooss

cil_tree_print_expr() calls cil_expr_to_string() in order to compute a string expression into expr_str. If this function fails, expr_str is left unitialized but its value is dereferenced with:

    cil_log(CIL_INFO, "%s)", expr_str);

Prevent such an issue by checking cil_expr_to_string()'s return value before using expr_str.

This issue has been found with clang's static analyzer.

---
 libsepol/cil/src/cil_tree.c | 10 +++++++---
 1 file changed, 7 insertions(+), 3 deletions(-)
libsepol/cil/src/cil_tree.c +1 -0
index d36401b41dba..b394a9d849df 100644
@@ -503,15 +503,19 @@ exit:
 void cil_tree_print_expr(struct cil_list *datum_expr, struct cil_list *str_expr)
 {
      char *expr_str;
+     int rc;
 
      cil_log(CIL_INFO, "(");
 
Quoted 12 lines

libsepol/cil/src/cil_tree.c exit: · line 517

Quoted 10 lines
libsepol/cil/src/cil_tree.c +6 -2
      if (datum_expr != NULL) {
-             cil_expr_to_string(datum_expr, &expr_str);
+             rc = cil_expr_to_string(datum_expr, &expr_str);
      } else {
-             cil_expr_to_string(str_expr, &expr_str);
+             rc = cil_expr_to_string(str_expr, &expr_str);
+     }
+     if (rc < 0) {
+             cil_log(CIL_INFO, "ERROR)");
+             return;

Wondering if we should abort or return an error to the caller instead of just logging an error and returning?

I believe this depends on the usage of cil_tree_print_expr(). I have not found any caller of cil_tree_print_* outside of libsepol/cil/src/cil_tree.c so I guess this interface is only used by developers for debugging purpose when playing with libsepol's CIL objects (being able to quickly print an object in a human-readable form is a very valuable feature). Am I right? The current interface of ignoring all errors and logging as much as possible without aborting seems consistent with such a use-case.

Quoted 5 lines

If there is an interest in propagating errors found by cil_tree_print_expr() back to its callers, I can spend some time to clean up the code. Otherwise I will send other patches (once they are in a suitable state) to fix some issues that clang's static analyzer find.

You are correct, and James agreed with your approach, which is why I merged the patch as is.

Acked-by on v1 2/3
#5 lore raw

Re: [PATCH 2/3] libsepol: cil: show an error when cil_expr_to_string() fails

In reply to Nicolas Iooss
Quoted 4 lines · Nicolas Iooss

cil_tree_print_expr() calls cil_expr_to_string() in order to compute a string expression into expr_str. If this function fails, expr_str is left unitialized but its value is dereferenced with:

Quoted 8 lines
     cil_log(CIL_INFO, "%s)", expr_str);

Prevent such an issue by checking cil_expr_to_string()'s return value before using expr_str.

This issue has been found with clang's static analyzer.

Quoted 32 lines
---
  libsepol/cil/src/cil_tree.c | 10 +++++++---
  1 file changed, 7 insertions(+), 3 deletions(-)
libsepol/cil/src/cil_tree.c +7 -3
index d36401b41dba..b394a9d849df 100644
@@ -503,15 +503,19 @@ exit:
  void cil_tree_print_expr(struct cil_list *datum_expr, struct cil_list *str_expr)
  {
  	char *expr_str;
+	int rc;
 
  	cil_log(CIL_INFO, "(");
 
  	if (datum_expr != NULL) {
-		cil_expr_to_string(datum_expr, &expr_str);
+		rc = cil_expr_to_string(datum_expr, &expr_str);
  	} else {
-		cil_expr_to_string(str_expr, &expr_str);
+		rc = cil_expr_to_string(str_expr, &expr_str);
+	}
+	if (rc < 0) {
+		cil_log(CIL_INFO, "ERROR)");
+		return;
  	}
-
  	cil_log(CIL_INFO, "%s)", expr_str);
  	free(expr_str);
  }
Signature · 2 lines
James Carter <[email protected]>
National Security Agency
#6 lore raw

PATCH 3/3 libsemanage: silence clang static analyzer report

In reply to Nicolas Iooss

clang's static analyzer reports an out-of-bound array access in semanage_user_roles() when num_roles is zero, with the following statement:

    strcpy(roles,roles_arr[0]);

When num_roles is zero, roles_arr[0] is not uninitialized and roles is the result of malloc(0) so this strcpy is dangerous. Make semanage_user_roles() return an empty string instead.

---
 libsemanage/src/seusers_local.c | 16 ++++++++++------
 1 file changed, 10 insertions(+), 6 deletions(-)
libsemanage/src/seusers_local.c +10 -6
index 42c3a8b662c2..413ebdddeb34 100644
@@ -35,12 +35,16 @@ static char *semanage_user_roles(semanage_handle_t * handle, const char *sename)
 				for (i = 0; i<num_roles; i++) {
 					size += (strlen(roles_arr[i]) + 1);
 				}
-				roles = malloc(size);
-				if (roles) {
-					strcpy(roles,roles_arr[0]);
-					for (i = 1; i<num_roles; i++) {
-						strcat(roles,",");
-						strcat(roles,roles_arr[i]);
+				if (num_roles == 0) {
+					roles = strdup("");
+				} else {
+					roles = malloc(size);
+					if (roles) {
+						strcpy(roles,roles_arr[0]);
+						for (i = 1; i<num_roles; i++) {
+							strcat(roles,",");
+							strcat(roles,roles_arr[i]);
+						}
 					}
 				}
 			}
Signature · 1 line
2.16.0
Acked-by on v1 3/3
#7 lore raw

Re: [PATCH 3/3] libsemanage: silence clang static analyzer report

Quoted 11 lines · Nicolas Iooss

clang's static analyzer reports an out-of-bound array access in semanage_user_roles() when num_roles is zero, with the following statement:

    strcpy(roles,roles_arr[0]);

When num_roles is zero, roles_arr[0] is not uninitialized and roles is the result of malloc(0) so this strcpy is dangerous. Make semanage_user_roles() return an empty string instead.

Quoted 32 lines
---
 libsemanage/src/seusers_local.c | 16 ++++++++++------
 1 file changed, 10 insertions(+), 6 deletions(-)
libsemanage/src/seusers_local.c +10 -6
index 42c3a8b662c2..413ebdddeb34 100644
@@ -35,12 +35,16 @@ static char *semanage_user_roles(semanage_handle_t * handle, const char *sename)
 				for (i = 0; i<num_roles; i++) {
 					size += (strlen(roles_arr[i]) + 1);
 				}
-				roles = malloc(size);
-				if (roles) {
-					strcpy(roles,roles_arr[0]);
-					for (i = 1; i<num_roles; i++) {
-						strcat(roles,",");
-						strcat(roles,roles_arr[i]);
+				if (num_roles == 0) {
+					roles = strdup("");
+				} else {
+					roles = malloc(size);
+					if (roles) {
+						strcpy(roles,roles_arr[0]);
+						for (i = 1; i<num_roles; i++) {
+							strcat(roles,",");
+							strcat(roles,roles_arr[i]);
+						}
 					}
 				}
 			}
#8 lore raw

Re: [PATCH 3/3] libsemanage: silence clang static analyzer report

In reply to Nicolas Iooss
Quoted 9 lines · Nicolas Iooss

clang's static analyzer reports an out-of-bound array access in semanage_user_roles() when num_roles is zero, with the following statement:

    strcpy(roles,roles_arr[0]);

When num_roles is zero, roles_arr[0] is not uninitialized and roles is the result of malloc(0) so this strcpy is dangerous. Make semanage_user_roles() return an empty string instead.

Applied patches 2 and 3; patch 1 will be obsoleted by James' patch when it is merged, unless there are objections to it.

Quoted 34 lines
---
 libsemanage/src/seusers_local.c | 16 ++++++++++------
 1 file changed, 10 insertions(+), 6 deletions(-)
libsemanage/src/seusers_local.c +10 -6
index 42c3a8b662c2..413ebdddeb34 100644
@@ -35,12 +35,16 @@ static char *semanage_user_roles(semanage_handle_t * handle, const char *sename)
 				for (i = 0; i<num_roles; i++) {
 					size += (strlen(roles_arr[i]) + 1);
 				}
-				roles = malloc(size);
-				if (roles) {
-					strcpy(roles,roles_arr[0]);
-					for (i = 1; i<num_roles; i++) {
-						strcat(roles,",");
-						strcat(roles,roles_arr[i]);
+				if (num_roles == 0) {
+					roles = strdup("");
+				} else {
+					roles = malloc(size);
+					if (roles) {
+						strcpy(roles,roles_arr[0]);
+						for (i = 1; i<num_roles; i++) {
+							strcat(roles,",");
+							strcat(roles,roles_arr[i]);
+						}
 					}
 				}
 			}
Acked-by on v1 1/3
#9 lore raw
In reply to Nicolas Iooss
Quoted 8 lines · Nicolas Iooss

In sepol_ibendport_key_create(), if sepol_ibendport_alloc_ibdev_name() fails to allocate tmp_key->ibdev_name, sepol_ibendport_key_free() is called to free the memory associated with tmp_key, which results in free() being called on uninitialized tmp_key->ibdev_name.

This issue is reported by clang's static analyzer with the following message:

Quoted 6 lines
    ibendport_record.c:115:2: warning: 1st function call argument is an
    uninitialized value
            free(key->ibdev_name);
            ^~~~~~~~~~~~~~~~~~~~~
Quoted 21 lines
---
 libsepol/src/ibendport_record.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
libsepol/src/ibendport_record.c +3 -1
index 912aeb536fd9..8285a9d6ffcf 100644
@@ -62,8 +62,10 @@ int sepol_ibendport_key_create(sepol_handle_t *handle,
 		goto omem;
 	}
 
-	if (sepol_ibendport_alloc_ibdev_name(handle, &tmp_key->ibdev_name) < 0)
+	if (sepol_ibendport_alloc_ibdev_name(handle, &tmp_key->ibdev_name) < 0) {
+		tmp_key->ibdev_name = NULL;
 		goto err;
+	}
 
 	strncpy(tmp_key->ibdev_name, ibdev_name, IB_DEVICE_NAME_MAX);
 	tmp_key->port = port;
#10 lore raw
In reply to Nicolas Iooss
Quoted 25 lines · Nicolas Iooss

In sepol_ibendport_key_create(), if sepol_ibendport_alloc_ibdev_name() fails to allocate tmp_key->ibdev_name, sepol_ibendport_key_free() is called to free the memory associated with tmp_key, which results in free() being called on uninitialized tmp_key->ibdev_name.

This issue is reported by clang's static analyzer with the following message:

     ibendport_record.c:115:2: warning: 1st function call argument is an
     uninitialized value
             free(key->ibdev_name);
             ^~~~~~~~~~~~~~~~~~~~~
---
  libsepol/src/ibendport_record.c | 4 +++-
  1 file changed, 3 insertions(+), 1 deletion(-)
libsepol/src/ibendport_record.c +0 -0
index 912aeb536fd9..8285a9d6ffcf 100644
@@ -62,8 +62,10 @@ int sepol_ibendport_key_create(sepol_handle_t *handle,
  		goto omem;

libsepol/src/ibendport_record.c int sepol_ibendport_key_create(sepol_handle_t *handle, · line 71

Quoted 10 lines
libsepol/src/ibendport_record.c +3 -1
  	}
 
-	if (sepol_ibendport_alloc_ibdev_name(handle, &tmp_key->ibdev_name) < 0)
+	if (sepol_ibendport_alloc_ibdev_name(handle, &tmp_key->ibdev_name) < 0) {
+		tmp_key->ibdev_name = NULL;
  		goto err;
+	}
 
  	strncpy(tmp_key->ibdev_name, ibdev_name, IB_DEVICE_NAME_MAX);
  	tmp_key->port = port;

It seems to be that a better way to fix this is to modify sepol_ibendport_alloc_ibdev_name(), so that the result of the calloc is assigned to *ibdev_name. That way if the calloc fails, tmp_key->ibdev_name will be set to NULL automatically.

Signature · 2 lines
James Carter <[email protected]>
National Security Agency