Skip to contentSkip to conversation

Possible solution to possible regression

RFC PATCH New James Carter 1 participant 3 messages Jan 22, 2025 SELinux security module

lore
Share & tools
lore
#0 lore raw

cover Possible solution to possible regression

As reported by Petr Lautrbach ([email protected]), commit d96f27bf7cb9 ("libsemanage: Preserve file context and ownership in policy store") can cause restorecon to use old data.

Here is a possible fix. It seems to work.

James Carter (2):
libselinux: Close old selabel handle when setting a new one
libsemanage: Set new restorecon handle before doing restorecon

 libselinux/src/selinux_restorecon.c | 4 ++++
 libsemanage/src/semanage_store.c    | 5 +++++
 2 files changed, 9 insertions(+)
Signature · 1 line
2.48.1
#1 lore raw

RFC PATCH 1/2 libselinux: Close old selabel handle when setting a new one

In selinux_restorecon_set_sehandle(), close the old selabel handle (if it exists) before setting the new one.

---
 libselinux/src/selinux_restorecon.c | 4 ++++
 1 file changed, 4 insertions(+)
libselinux/src/selinux_restorecon.c +4 -0
index bc6ed935..ab1c5216 100644
@@ -1367,6 +1367,10 @@ void selinux_restorecon_set_sehandle(struct selabel_handle *hndl)
 	unsigned char *fc_digest;
 	size_t num_specfiles, fc_digest_len;
 
+	if (fc_sehandle) {
+		selabel_close(fc_sehandle);
+	}
+
 	fc_sehandle = hndl;
 	if (!fc_sehandle)
 		return;
Signature · 1 line
2.48.1
#2 lore raw

RFC PATCH 2/2 libsemanage: Set new restorecon handle before doing restorecon

In reply to James Carter

In semanage_setfiles(), need to reset the restorecon handle to make sure restorecon is not operating on old selabel data.

---
 libsemanage/src/semanage_store.c | 5 +++++
 1 file changed, 5 insertions(+)
libsemanage/src/semanage_store.c +5 -0
index 2ca2e900..cf9aa809 100644
@@ -3012,9 +3012,14 @@ log_callback_mute(__attribute__((unused)) int type, __attribute__((unused)) cons
 void semanage_setfiles(semanage_handle_t * sh, const char *path){
 	struct stat sb;
 	int fd;
+	struct selabel_handle *sehandle;
+
 	union selinux_callback cb_orig = selinux_get_callback(SELINUX_CB_LOG);
 	union selinux_callback cb = { .func_log = log_callback_mute };
 
+	sehandle = selinux_restorecon_default_handle();
+	selinux_restorecon_set_sehandle(sehandle);
+
 	/* Mute all logs */
 	selinux_set_callback(SELINUX_CB_LOG, cb);
 
Signature · 1 line
2.48.1