Skip to contentSkip to conversation

libsemanage: Fix USE_AFTER_FREE (CWE-672) in semanage_direct_get_module_info()

PATCH In review Petr Lautrbach 2 participants 5 messages Apr 1, 2022 – Apr 6, 2022 SELinux security module

lore
Share & tools
lore
#0 lore raw

PATCH 1/3 libsemanage: Fix USE_AFTER_FREE (CWE-672) in semanage_direct_get_module_info()

Quoted 1 line

From fclose(3):

Upon successful completion, 0 is returned. Otherwise, EOF is returned and errno is set to indicate the error. In either case, any further access (including another call to fclose()) to the stream results in undefined behavior.

---
 libsemanage/src/direct_api.c | 1 +
 1 file changed, 1 insertion(+)
libsemanage/src/direct_api.c +1 -0
index d83941b05ca1..d5716ce579e9 100644
@@ -2293,6 +2293,7 @@ static int semanage_direct_get_module_info(semanage_handle_t *sh,
 	tmp = NULL;
 
 	if (fclose(fp) != 0) {
+		fp = NULL;
 		ERR(sh,
 		    "Unable to close %s module lang ext file.",
 		    (*modinfo)->name);
Signature · 1 line
2.35.1
#1 lore raw

PATCH 2/3 semodule_package: Close leaking fd

---
 semodule-utils/semodule_package/semodule_package.c | 1 +
 1 file changed, 1 insertion(+)
semodule-utils/semodule_package/semodule_package.c +1 -0
index 3515234e36de..bc8584b51079 100644
@@ -73,6 +73,7 @@ static int file_to_data(const char *path, char **data, size_t * len)
 		goto err;
 	}
 	if (!sb.st_size) {
+		close(fd);
 		*len = 0;
 		return 0;
 	}
Signature · 1 line
2.35.1
#2 lore raw

PATCH 3/3 mcstrans: Fir RESOURCE_LEAK and USE_AFTER_FREE coverity scan defects

In reply to Petr Lautrbach
---
 mcstrans/src/mcstrans.c  | 25 ++++++++++++++++++++++++-
 mcstrans/src/mcstransd.c |  4 +++-
 2 files changed, 27 insertions(+), 2 deletions(-)
mcstrans/src/mcstrans.c +24 -1
index d42760fdbfc2..af3f507ef718 100644
@@ -632,16 +632,23 @@ add_cache(domain_t *domain, char *raw, char *trans) {
 
 	map->raw = strdup(raw);
 	if (!map->raw) {
+		free(map);
 		goto err;
 	}
 	map->trans = strdup(trans);
 	if (!map->trans) {
+		free(map->raw);
+		free(map);
 		goto err;
 	}
 
 	log_debug(" add_cache (%s,%s)\n", raw, trans);
-	if (add_to_hashtable(domain->raw_to_trans, map->raw, map) < 0)
+	if (add_to_hashtable(domain->raw_to_trans, map->raw, map) < 0) {
+		free(map->trans);
+		free(map->raw);
+		free(map);
 		goto err;
+	}
 
 	if (add_to_hashtable(domain->trans_to_raw, map->trans, map) < 0)
 		goto err;
@@ -1568,6 +1575,7 @@ trans_context(const char *incon, char **rcon) {
 			trans = compute_trans_from_raw(range, domain);
 			if (trans)
 				if (add_cache(domain, range, trans) < 0) {
+					free(trans);
 					free(range);
 					return -1;
 				}
@@ -1579,6 +1587,7 @@ trans_context(const char *incon, char **rcon) {
 				ltrans = compute_trans_from_raw(lrange, domain);
 				if (ltrans) {
 					if (add_cache(domain, lrange, ltrans) < 0) {
+						free(ltrans);
 						free(range);
 						return -1;
 					}
@@ -1597,6 +1606,7 @@ trans_context(const char *incon, char **rcon) {
 				utrans = compute_trans_from_raw(urange, domain);
 				if (utrans) {
 					if (add_cache(domain, urange, utrans) < 0) {
+						free(utrans);
 						free(ltrans);
 						free(range);
 						return -1;
@@ -1636,6 +1646,10 @@ trans_context(const char *incon, char **rcon) {
 		}
 		if (dashp)
 			*dashp = '-';
+		if (trans) {
+			free(trans);
+			trans = NULL;
+		}
 	}
 
 	if (trans) {
@@ -1696,7 +1710,9 @@ untrans_context(const char *incon, char **rcon) {
 					canonical = compute_trans_from_raw(raw, domain);
 					if (canonical && strcmp(canonical, range))
 						if (add_cache(domain, raw, canonical) < 0) {
+							free(canonical);
 							free(range);
+							free(raw);
 							return -1;
 						}
 				}
@@ -1704,6 +1720,7 @@ untrans_context(const char *incon, char **rcon) {
 					free(canonical);
 				if (add_cache(domain, raw, range) < 0) {
 					free(range);
+					free(raw);
 					return -1;
 				}
 			} else {
@@ -1721,6 +1738,7 @@ untrans_context(const char *incon, char **rcon) {
 						canonical = compute_trans_from_raw(lraw, domain);
 						if (canonical)
 							if (add_cache(domain, lraw, canonical) < 0) {
+								free(canonical);
 								free(lraw);
 								free(range);
 								return -1;
@@ -1752,6 +1770,7 @@ untrans_context(const char *incon, char **rcon) {
 						canonical = compute_trans_from_raw(uraw, domain);
 						if (canonical)
 							if (add_cache(domain, uraw, canonical) < 0) {
+								free(canonical);
 								free(uraw);
 								free(lraw);
 								free(range);
@@ -1802,6 +1821,10 @@ untrans_context(const char *incon, char **rcon) {
 		}
 		if (dashp)
 			*dashp = '-';
+		if (raw) {
+			free(raw);
+			raw = NULL;
+		}
 	}
 
 	if (raw) {
mcstrans/src/mcstransd.c +3 -1
index 536c0f32f23a..42262e580386 100644
@@ -328,6 +328,7 @@ process_events(struct pollfd **ufds, int *nfds)
 					/* Setup pollfd for deletion later. */
 					(*ufds)[ii].fd = -1;
 					close(connfd);
+					connfd = -1;
 					/* So we don't get bothered later */
 					revents = revents & ~(POLLHUP);
 				}
@@ -341,10 +342,11 @@ process_events(struct pollfd **ufds, int *nfds)
 			/* Set the pollfd up for deletion later. */
 			(*ufds)[ii].fd = -1;
 			close(connfd);
+			connfd = -1;
 
 			revents = revents & ~(POLLHUP);
 		}
-		if (revents) {
+		if (revents && connfd != -1) {
 			syslog(LOG_ERR, "Unknown/error events (%x) encountered"
 					" for fd (%d)\n", revents, connfd);
 
Signature · 1 line
2.35.1
Acked-by on v1 1/3
#3 lore raw
In reply to Petr Lautrbach
Quoted 8 lines · Petr Lautrbach
Quoted 2 lines

From fclose(3):

Upon successful completion, 0 is returned. Otherwise, EOF is returned and errno is set to indicate the error. In either case, any further access (including another call to fclose()) to the stream results in undefined behavior.

For all three patches:

Quoted 19 lines
---
 libsemanage/src/direct_api.c | 1 +
 1 file changed, 1 insertion(+)
libsemanage/src/direct_api.c +1 -1
index d83941b05ca1..d5716ce579e9 100644
@@ -2293,6 +2293,7 @@ static int semanage_direct_get_module_info(semanage_handle_t *sh,
        tmp = NULL;
 
        if (fclose(fp) != 0) {
+               fp = NULL;
                ERR(sh,
                    "Unable to close %s module lang ext file.",
                    (*modinfo)->name);
--

2.35.1

author Applied
#4 lore raw
Quoted 8 lines · James Carter
Quoted 7 lines · Petr Lautrbach
Quoted 2 lines

From fclose(3):

Upon successful completion, 0 is returned. Otherwise, EOF is returned and errno is set to indicate the error. In either case, any further access (including another call to fclose()) to the stream results in undefined behavior.

Quoted 4 lines
Quoted 1 line

For all three patches:

Merged, thanks!

Quoted 20 lines
Quoted 20 lines
---
 libsemanage/src/direct_api.c | 1 +
 1 file changed, 1 insertion(+)
libsemanage/src/direct_api.c +1 -1
index d83941b05ca1..d5716ce579e9 100644
@@ -2293,6 +2293,7 @@ static int semanage_direct_get_module_info(semanage_handle_t *sh,
        tmp = NULL;
 
        if (fclose(fp) != 0) {
+               fp = NULL;
                ERR(sh,
                    "Unable to close %s module lang ext file.",
                    (*modinfo)->name);
--

2.35.1