Skip to contentSkip to conversation

libsemanage: clear freed keys during component merges

PATCH In review Stephen Smalley 1 participant 2 messages Oct 23 – Oct 26 SELinux security module

lore
Share & tools
lore

Stephen Smalley

author maintainer
#0 lore raw

PATCH 1/1 libsemanage: clear freed keys during component merges

clear_obsolete() and load_records() free the current key after each successful iteration but leave the pointer unchanged. If extracting the next key fails, the error path frees the previous key again. Record key constructors such as sepol_bool_key_create() leave their output untouched on allocation failure, so cleanup accesses freed memory instead of simply returning the merge error.

Reset each key pointer to NULL immediately after freeing it. This keeps cleanup valid when a later extraction fails without changing which records are merged or how errors propagate to the caller.

---
 libsemanage/src/policy_components.c | 2 ++
 1 file changed, 2 insertions(+)
libsemanage/src/policy_components.c +2 -0
index 767c083b..cd4b500a 100644
@@ -44,6 +44,7 @@ static int clear_obsolete(semanage_handle_t *handle, record_t **records,
 		}
 
 		rtable->key_free(key);
+		key = NULL;
 	}
 
 	return STATUS_SUCCESS;
@@ -81,6 +82,7 @@ static int load_records(semanage_handle_t *handle, dbase_config_t *dst,
 			goto err;
 
 		rtable->key_free(rkey);
+		rkey = NULL;
 	}
 
 	return STATUS_SUCCESS;
Signature · 1 line
2.56.0
author maintainer
#1 lore raw
Quoted 8 lines · Stephen Smalley

clear_obsolete() and load_records() free the current key after each successful iteration but leave the pointer unchanged. If extracting the next key fails, the error path frees the previous key again. Record key constructors such as sepol_bool_key_create() leave their output untouched on allocation failure, so cleanup accesses freed memory instead of simply returning the merge error.

Quoted 5 lines

Reset each key pointer to NULL immediately after freeing it. This keeps cleanup valid when a later extraction fails without changing which records are merged or how errors propagate to the caller.

This has been merged.