Blame SOURCES/0102-system-change-our-notification-fd-handling.patch

2f610f
From 5201e324d2025b1febbade9bb90d6e405b92a14c Mon Sep 17 00:00:00 2001
2f610f
From: Paul Moore <paul@paul-moore.com>
2f610f
Date: Tue, 4 Aug 2020 10:52:08 -0400
2f610f
Subject: [PATCH 102/102] system: change our notification fd handling
2f610f
2f610f
This commit changes how we handle the notification fd by only
2f610f
requesting it via _NEW_LISTENER if the filter has a _NOTIFY action
2f610f
in it.  We also augment the seccomp_reset(NULL, ...) behavior so
2f610f
that it closes the notification fd before resetting the global
2f610f
state; applications that need to keep their notification fd open
2f610f
across a call to seccomp_reset(NULL, ...) can simply dup() it.
2f610f
Although one would have to wonder why the application would be
2f610f
calling seccomp_reset(NULL, ...) in that case.
2f610f
2f610f
Signed-off-by: Paul Moore <paul@paul-moore.com>
2f610f
---
2f610f
 doc/man/man3/seccomp_init.3 |  6 ++++--
2f610f
 src/system.c                | 18 +++++++++++++++---
2f610f
 2 files changed, 19 insertions(+), 5 deletions(-)
2f610f
2f610f
diff --git a/doc/man/man3/seccomp_init.3 b/doc/man/man3/seccomp_init.3
2f610f
index 87520cd..7881c35 100644
2f610f
--- a/doc/man/man3/seccomp_init.3
2f610f
+++ b/doc/man/man3/seccomp_init.3
2f610f
@@ -38,8 +38,10 @@ and can only be called after a call to
2f610f
 .BR seccomp_init ()
2f610f
 has succeeded.  If
2f610f
 .BR seccomp_reset ()
2f610f
-is called with a NULL filter, it resets the library's global task state;
2f610f
-normally this is not needed, but it may be required to continue using the
2f610f
+is called with a NULL filter, it resets the library's global task state,
2f610f
+including any notification file descriptors retrieved by
2f610f
+.BR seccomp_notify_fd(3) .
2f610f
+Normally this is not needed, but it may be required to continue using the
2f610f
 library after a
2f610f
 .BR fork ()
2f610f
 or
2f610f
diff --git a/src/system.c b/src/system.c
2f610f
index 3b43b2a..c646c65 100644
2f610f
--- a/src/system.c
2f610f
+++ b/src/system.c
2f610f
@@ -84,7 +84,11 @@ static struct task_state state = {
2f610f
 void sys_reset_state(void)
2f610f
 {
2f610f
 	state.nr_seccomp = -1;
2f610f
+
2f610f
+	if (state.notify_fd > 0)
2f610f
+		close(state.notify_fd);
2f610f
 	state.notify_fd = -1;
2f610f
+
2f610f
 	state.sup_syscall = -1;
2f610f
 	state.sup_flag_tsync = -1;
2f610f
 	state.sup_flag_log = -1;
2f610f
@@ -353,6 +357,7 @@ int sys_filter_load(struct db_filter_col *col, bool rawrc)
2f610f
 {
2f610f
 	int rc;
2f610f
 	bool tsync_notify;
2f610f
+	bool listener_req;
2f610f
 	struct bpf_program *prgm = NULL;
2f610f
 
2f610f
 	rc = gen_bpf_generate(col, &prgm);
2f610f
@@ -367,6 +372,8 @@ int sys_filter_load(struct db_filter_col *col, bool rawrc)
2f610f
 	}
2f610f
 
2f610f
 	tsync_notify = state.sup_flag_tsync_esrch > 0 && state.notify_fd == -1;
2f610f
+	listener_req = state.sup_user_notif > 0 && \
2f610f
+		       col->notify_used && state.notify_fd == -1;
2f610f
 
2f610f
 	/* load the filter into the kernel */
2f610f
 	if (sys_chk_seccomp_syscall() == 1) {
2f610f
@@ -375,11 +382,16 @@ int sys_filter_load(struct db_filter_col *col, bool rawrc)
2f610f
 			if (col->attr.tsync_enable)
2f610f
 				flgs |= SECCOMP_FILTER_FLAG_TSYNC | \
2f610f
 					SECCOMP_FILTER_FLAG_TSYNC_ESRCH;
2f610f
-			if (state.sup_user_notif > 0)
2f610f
+			if (listener_req)
2f610f
 				flgs |= SECCOMP_FILTER_FLAG_NEW_LISTENER;
2f610f
-		} else if (col->attr.tsync_enable)
2f610f
+		} else if (col->attr.tsync_enable) {
2f610f
+			if (listener_req) {
2f610f
+				/* NOTE: we _should_ catch this in db.c */
2f610f
+				rc = -EFAULT;
2f610f
+				goto filter_load_out;
2f610f
+			}
2f610f
 			flgs |= SECCOMP_FILTER_FLAG_TSYNC;
2f610f
-		else if (state.sup_user_notif > 0 && state.notify_fd == -1)
2f610f
+		} else if (listener_req)
2f610f
 			flgs |= SECCOMP_FILTER_FLAG_NEW_LISTENER;
2f610f
 		if (col->attr.log_enable)
2f610f
 			flgs |= SECCOMP_FILTER_FLAG_LOG;
2f610f
-- 
2f610f
2.26.2
2f610f