Skip to contentSkip to conversation

Modify audit2why analyze function to use loaded policy

PATCH Applied Joshua Brindle 2 participants 7 messages Jun 3, 2016 – Jun 21, 2016 SELinux security module

lore
Share & tools
lore
#0 lore raw

PATCH 1/2 Modify audit2why analyze function to use loaded policy

Class and perms should come from the policy being used for analysis, not the system policy so use sepol_ interfaces

Change-Id Ia0590ed2514249fd98810a8d4fe87f8bf5280561
---
 libselinux/src/audit2why.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
libselinux/src/audit2why.c +4 -4
index 12745b3..abe1701 100644
@@ -343,8 +343,8 @@ static PyObject *analyze(PyObject *self __attribute__((unused)) , PyObject *args
 	if (rc < 0)
 		RETURN(BADTCON)
 
-	tclass = string_to_security_class(tclassstr);
-	if (!tclass)
+	rc = sepol_string_to_security_class(tclassstr, &tclass);
+	if (rc < 0)
 		RETURN(BADTCLASS)
 
 	/* Convert the permission list to an AV. */
@@ -365,8 +365,8 @@ static PyObject *analyze(PyObject *self __attribute__((unused)) , PyObject *args
 		permstr = PyString_AsString( strObj );
 #endif
 		
-		perm = string_to_av_perm(tclass, permstr);
-		if (!perm)
+		rc = sepol_string_to_av_perm(tclass, permstr, &perm);
+		if (rc < 0)
 			RETURN(BADPERM)
 
 		av |= perm;
Signature · 1 line
2.1.0
#1 lore raw

PATCH 2/2 Correctly detect unknown classes in sepol_string_to_security_class

Bail before running off the end of the class index

Change-Id I47c4eaac3c7d789f8d85047e34e37e3f0bb38b3a
---
 libsepol/src/services.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
libsepol/src/services.c +3 -1
index d64a8e8..8679545 100644
@@ -1155,7 +1155,7 @@ int hidden sepol_string_to_security_class(const char *class_name,
 	char *class = NULL;
 	sepol_security_class_t id;
 
-	for (id = 1;; id++) {
+	for (id = 1; id < policydb->p_classes.nprim; id++) {
 		class = policydb->p_class_val_to_name[id - 1];
 		if (class == NULL) {
 			ERR(NULL, "could not convert %s to class id", class_name);
@@ -1166,6 +1166,8 @@ int hidden sepol_string_to_security_class(const char *class_name,
 			return STATUS_SUCCESS;
 		}
 	}
+	ERR(NULL, "unrecognized class %s", class_name);
+	return -EINVAL;
 }
 
 /*
Signature · 1 line
2.1.0
#2 lore raw

PATCH 1/1 Correctly detect unknown classes in sepol_string_to_security_class

Bail before running off the end of the class index

Change-Id I47c4eaac3c7d789f8d85047e34e37e3f0bb38b3a
---
 libsepol/src/services.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
libsepol/src/services.c +3 -1
index d64a8e8..665fcaa 100644
@@ -1155,7 +1155,7 @@ int hidden sepol_string_to_security_class(const char *class_name,
 	char *class = NULL;
 	sepol_security_class_t id;
 
-	for (id = 1;; id++) {
+	for (id = 1; id <= policydb->p_classes.nprim; id++) {
 		class = policydb->p_class_val_to_name[id - 1];
 		if (class == NULL) {
 			ERR(NULL, "could not convert %s to class id", class_name);
@@ -1166,6 +1166,8 @@ int hidden sepol_string_to_security_class(const char *class_name,
 			return STATUS_SUCCESS;
 		}
 	}
+	ERR(NULL, "unrecognized class %s", class_name);
+	return -EINVAL;
 }
 
 /*
Signature · 1 line
2.1.0
#3 lore raw

Re: [PATCH] Correctly detect unknown classes in sepol_string_to_security_class

Quoted 2 lines · Joshua Brindle

Bail before running off the end of the class index

This one correctly goes all the way to the end of the classes index, the last version did not.

Quoted 28 lines
Change-Id I47c4eaac3c7d789f8d85047e34e37e3f0bb38b3a
---
  libsepol/src/services.c | 4 +++-
  1 file changed, 3 insertions(+), 1 deletion(-)
libsepol/src/services.c +3 -1
index d64a8e8..665fcaa 100644
@@ -1155,7 +1155,7 @@ int hidden sepol_string_to_security_class(const char *class_name,
  	char *class = NULL;
  	sepol_security_class_t id;
 
-	for (id = 1;; id++) {
+	for (id = 1; id<= policydb->p_classes.nprim; id++) {
  		class = policydb->p_class_val_to_name[id - 1];
  		if (class == NULL) {
  			ERR(NULL, "could not convert %s to class id", class_name);
@@ -1166,6 +1166,8 @@ int hidden sepol_string_to_security_class(const char *class_name,
  			return STATUS_SUCCESS;
  		}
  	}
+	ERR(NULL, "unrecognized class %s", class_name);
+	return -EINVAL;
  }
 
  /*
#4 lore raw

Re: [PATCH] Correctly detect unknown classes in sepol_string_to_security_class

In reply to Joshua Brindle
Quoted 4 lines · Joshua Brindle

Bail before running off the end of the class index

Change-Id I47c4eaac3c7d789f8d85047e34e37e3f0bb38b3a

Applied this one and then rewrote it to use hashtab_search(). Not sure why it wasn't that way in the first place.

Quoted 27 lines
---
 libsepol/src/services.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
libsepol/src/services.c +3 -1
index d64a8e8..665fcaa 100644
@@ -1155,7 +1155,7 @@ int hidden sepol_string_to_security_class(const char *class_name,
 	char *class = NULL;
 	sepol_security_class_t id;
 
-	for (id = 1;; id++) {
+	for (id = 1; id <= policydb->p_classes.nprim; id++) {
 		class = policydb->p_class_val_to_name[id - 1];
 		if (class == NULL) {
 			ERR(NULL, "could not convert %s to class id", class_name);
@@ -1166,6 +1166,8 @@ int hidden sepol_string_to_security_class(const char *class_name,
 			return STATUS_SUCCESS;
 		}
 	}
+	ERR(NULL, "unrecognized class %s", class_name);
+	return -EINVAL;
 }
 
 /*
#5 lore raw

Re: [PATCH] Correctly detect unknown classes in sepol_string_to_security_class

Quoted 8 lines · Stephen Smalley
Quoted 4 lines · Joshua Brindle

Bail before running off the end of the class index

Change-Id I47c4eaac3c7d789f8d85047e34e37e3f0bb38b3a

Applied this one and then rewrote it to use hashtab_search(). Not sure why it wasn't that way in the first place.

Thank you, that was a much better fix that I should have noticed...

Quoted 29 lines
Quoted 28 lines
---
  libsepol/src/services.c | 4 +++-
  1 file changed, 3 insertions(+), 1 deletion(-)
libsepol/src/services.c +3 -1
index d64a8e8..665fcaa 100644
@@ -1155,7 +1155,7 @@ int hidden sepol_string_to_security_class(const char *class_name,
  	char *class = NULL;
  	sepol_security_class_t id;
 
-	for (id = 1;; id++) {
+	for (id = 1; id<= policydb->p_classes.nprim; id++) {
  		class = policydb->p_class_val_to_name[id - 1];
  		if (class == NULL) {
  			ERR(NULL, "could not convert %s to class id", class_name);
@@ -1166,6 +1166,8 @@ int hidden sepol_string_to_security_class(const char *class_name,
  			return STATUS_SUCCESS;
  		}
  	}
+	ERR(NULL, "unrecognized class %s", class_name);
+	return -EINVAL;
  }
 
  /*
#6 lore raw
In reply to Joshua Brindle
Quoted 8 lines · Joshua Brindle

Class and perms should come from the policy being used for analysis, not the system policy so use sepol_ interfaces

Change-Id Ia0590ed2514249fd98810a8d4fe87f8bf5280561
---
 libselinux/src/audit2why.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)

Thanks, applied.

Quoted 28 lines
libselinux/src/audit2why.c +4 -4
index 12745b3..abe1701 100644
@@ -343,8 +343,8 @@ static PyObject *analyze(PyObject *self __attribute__((unused)) , PyObject *args
 	if (rc < 0)
 		RETURN(BADTCON)
 
-	tclass = string_to_security_class(tclassstr);
-	if (!tclass)
+	rc = sepol_string_to_security_class(tclassstr, &tclass);
+	if (rc < 0)
 		RETURN(BADTCLASS)
 
 	/* Convert the permission list to an AV. */
@@ -365,8 +365,8 @@ static PyObject *analyze(PyObject *self __attribute__((unused)) , PyObject *args
 		permstr = PyString_AsString( strObj );
 #endif
 
-		perm = string_to_av_perm(tclass, permstr);
-		if (!perm)
+		rc = sepol_string_to_av_perm(tclass, permstr, &perm);
+		if (rc < 0)
 			RETURN(BADPERM)
 
 		av |= perm;