SELinux Security Module development
 help / color / mirror / Atom feed
* [RFC PATCH 1/4] libselinux: simplify policy path logic to avoid uninitialized read
@ 2022-05-10 18:20 Christian Göttsche
  2022-05-10 18:20 ` [RFC PATCH 2/4] libselinux: add header guard for internal header Christian Göttsche
                   ` (3 more replies)
  0 siblings, 4 replies; 10+ messages in thread
From: Christian Göttsche @ 2022-05-10 18:20 UTC (permalink / raw)
  To: selinux

In case the function __policy_init() gets called with a NULL pointer,
the stack variable path remains uninitialized (except at its last
index).  If parsing the binary policy fails in sepol_policydb_read() the
error branch would access those uninitialized memory.

Signed-off-by: Christian Göttsche <cgzones@googlemail.com>
---
 libselinux/src/audit2why.c | 34 +++++++++++++---------------------
 1 file changed, 13 insertions(+), 21 deletions(-)

diff --git a/libselinux/src/audit2why.c b/libselinux/src/audit2why.c
index ca38e13c..44a9a341 100644
--- a/libselinux/src/audit2why.c
+++ b/libselinux/src/audit2why.c
@@ -192,25 +192,16 @@ static PyObject *finish(PyObject *self __attribute__((unused)), PyObject *args)
 static int __policy_init(const char *init_path)
 {
 	FILE *fp;
-	char path[PATH_MAX];
+	const char *curpolicy;
 	char errormsg[PATH_MAX+1024+20];
 	struct sepol_policy_file *pf = NULL;
 	int rc;
 	unsigned int cnt;
 
-	path[PATH_MAX-1] = '\0';
 	if (init_path) {
-		strncpy(path, init_path, PATH_MAX-1);
-		fp = fopen(path, "re");
-		if (!fp) {
-			snprintf(errormsg, sizeof(errormsg), 
-				 "unable to open %s:  %m\n",
-				 path);
-			PyErr_SetString( PyExc_ValueError, errormsg);
-			return 1;
-		}
+		curpolicy = init_path;
 	} else {
-		const char *curpolicy = selinux_current_policy_path();
+		curpolicy = selinux_current_policy_path();
 		if (!curpolicy) {
 			/* SELinux disabled, must use -p option. */
 			snprintf(errormsg, sizeof(errormsg),
@@ -218,14 +209,15 @@ static int __policy_init(const char *init_path)
 			PyErr_SetString( PyExc_ValueError, errormsg);
 			return 1;
 		}
-		fp = fopen(curpolicy, "re");
-		if (!fp) {
-			snprintf(errormsg, sizeof(errormsg), 
-				 "unable to open %s:  %m\n",
-				 curpolicy);
-			PyErr_SetString( PyExc_ValueError, errormsg);
-			return 1;
-		}
+	}
+
+	fp = fopen(curpolicy, "re");
+	if (!fp) {
+		snprintf(errormsg, sizeof(errormsg),
+			 "unable to open %s:  %m\n",
+			 curpolicy);
+		PyErr_SetString( PyExc_ValueError, errormsg);
+		return 1;
 	}
 
 	avc = calloc(sizeof(struct avc_t), 1);
@@ -249,7 +241,7 @@ static int __policy_init(const char *init_path)
 	sepol_policy_file_set_fp(pf, fp);	
 	if (sepol_policydb_read(avc->policydb, pf)) {
 		snprintf(errormsg, sizeof(errormsg), 
-			 "invalid binary policy %s\n", path);
+			 "invalid binary policy %s\n", curpolicy);
 		PyErr_SetString( PyExc_ValueError, errormsg);
 		fclose(fp);
 		return 1;
-- 
2.36.1


^ permalink raw reply related	[flat|nested] 10+ messages in thread

end of thread, other threads:[~2022-06-08 13:40 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2022-05-10 18:20 [RFC PATCH 1/4] libselinux: simplify policy path logic to avoid uninitialized read Christian Göttsche
2022-05-10 18:20 ` [RFC PATCH 2/4] libselinux: add header guard for internal header Christian Göttsche
2022-05-10 18:20 ` [RFC PATCH 3/4] libselinux: introduce strlcpy Christian Göttsche
2022-05-10 18:20 ` [RFC PATCH 4/4] libselinux: check for truncations Christian Göttsche
2022-05-12 15:34   ` James Carter
2022-05-17 15:07   ` [RFC PATCH v2 " Christian Göttsche
2022-05-20 12:43     ` [PATCH v3 " Christian Göttsche
2022-06-07 17:14   ` [PATCH v4 " Christian Göttsche
2022-05-18 15:22 ` [RFC PATCH 1/4] libselinux: simplify policy path logic to avoid uninitialized read James Carter
2022-06-08 13:40   ` James Carter

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox