Blame SOURCES/fapolicyd-markfs-1.patch

6db311
From 2d15ea13e2a3dca1bb159f2cf031ca437c0b9aa1 Mon Sep 17 00:00:00 2001
6db311
From: Steve Grubb <sgrubb@redhat.com>
6db311
Date: Tue, 27 Sep 2022 10:33:44 -0400
6db311
Subject: [PATCH] Add support for using FAN_MARK_FILESYSTEM to see bind mounted
6db311
 accesses
6db311
6db311
---
6db311
 ChangeLog                   |  1 +
6db311
 configure.ac                |  1 +
6db311
 doc/fapolicyd.conf.5        |  5 ++++-
6db311
 init/fapolicyd.conf         |  1 +
6db311
 src/daemon/notify.c         | 12 ++++++++++--
6db311
 src/library/conf.h          |  3 ++-
6db311
 src/library/daemon-config.c | 28 +++++++++++++++++++++++++++-
6db311
 7 files changed, 46 insertions(+), 5 deletions(-)
6db311
6db311
diff --git a/configure.ac b/configure.ac
6db311
index 4437685..a67c46b 100644
6db311
--- a/configure.ac
6db311
+++ b/configure.ac
6db311
@@ -56,6 +56,7 @@ AC_CHECK_DECLS([FAN_OPEN_EXEC_PERM], [perm=yes], [perm=no], [[#include 
6db311
 if test $perm = "no"; then
6db311
 	AC_MSG_ERROR([FAN_OPEN_EXEC_PERM is not defined in linux/fanotify.h. It is required for the kernel to support it])
6db311
 fi
6db311
+AC_CHECK_DECLS([FAN_MARK_FILESYSTEM], [], [], [[#include <linux/fanotify.h>]])
6db311
 
6db311
 withval=""
6db311
 AC_ARG_WITH(rpm,
6db311
diff --git a/doc/fapolicyd.conf.5 b/doc/fapolicyd.conf.5
6db311
index 812cfa4..d8cb296 100644
6db311
--- a/doc/fapolicyd.conf.5
6db311
+++ b/doc/fapolicyd.conf.5
6db311
@@ -1,4 +1,4 @@
6db311
-.TH FAPOLICYD.CONF: "6" "October 2021" "Red Hat" "System Administration Utilities"
6db311
+.TH FAPOLICYD.CONF: "6" "September 2022" "Red Hat" "System Administration Utilities"
6db311
 .SH NAME
6db311
 fapolicyd.conf \- fapolicyd configuration file
6db311
 .SH DESCRIPTION
6db311
@@ -87,6 +87,9 @@ Example:
6db311
 .B rpm_sha256_only
6db311
 The option set to 1 forces the daemon to work only with SHA256 hashes. This is useful on the systems where the integrity is set to SHA256 or IMA and some rpms were originally built with e.g. SHA1. The daemon will ingore these SHA1 entries therefore they can be added manually via CLI with correct SHA256 to a trust file later. If set to 0 the daemon stores SHA1 in trustdb as well. This is compatible with older behavior which works with the integrity set to NONE and SIZE. The NONE or SIZE integrity setting considers the files installed via rpm as trusted and it does not care about their hashes at all. On the other hand the integrity set to SHA256 or IMA will never consider a file with SHA1 in trustdb as trusted. The default value is 0.
6db311
 
6db311
+.TP
6db311
+.B allow_filesystem_mark
6db311
+When this option is set to 1, it allows fapolicyd to monitor file access events on the underlying file system when they are bind mounted or are overlayed (e.g. the overlayfs). Normally they block fapolicyd from seeing events on the underlying file systems. This may or may not be desirable. For example, you might start seeing containers accessing things outside of the container but there is no source of trust for the container. In that case you probably do not want to see access from the container. Or maybe you do not use containers but want to control anything run by systemd-run when dynamic users are allowed. In that case you probably want to turn it on. Not all kernel's supoport this option. Therefore the default value is 0.
6db311
 
6db311
 .SH "SEE ALSO"
6db311
 .BR fapolicyd (8),
6db311
diff --git a/init/fapolicyd.conf b/init/fapolicyd.conf
6db311
index 42e8798..8363b89 100644
6db311
--- a/init/fapolicyd.conf
6db311
+++ b/init/fapolicyd.conf
6db311
@@ -18,3 +18,4 @@ trust = rpmdb,file
6db311
 integrity = none
6db311
 syslog_format = rule,dec,perm,auid,pid,exe,:,path,ftype,trust
6db311
 rpm_sha256_only = 0
6db311
+allow_filesystem_mark = 0
6db311
diff --git a/src/daemon/notify.c b/src/daemon/notify.c
6db311
index f550e99..c91abc4 100644
6db311
--- a/src/daemon/notify.c
6db311
+++ b/src/daemon/notify.c
6db311
@@ -123,8 +123,16 @@ int init_fanotify(const conf_t *conf, mlist *m)
6db311
 	path = mlist_first(m);
6db311
 	while (path) {
6db311
 retry_mark:
6db311
-		if (fanotify_mark(fd, FAN_MARK_ADD | FAN_MARK_MOUNT,
6db311
-				mask, -1, path) == -1) {
6db311
+		unsigned int flags = FAN_MARK_ADD;
6db311
+#ifdef HAVE_DECL_FAN_MARK_FILESYSTEM
6db311
+		if (conf->allow_filesystem_mark)
6db311
+		    flags |= FAN_MARK_FILESYSTEM;
6db311
+#else
6db311
+		if (conf->allow_filesystem_mark)
6db311
+			msg(LOG_ERR,
6db311
+	    "allow_filesystem_mark is unsupported for this kernel - ignoring");
6db311
+#endif
6db311
+		if (fanotify_mark(fd, flags, mask, -1, path) == -1) {
6db311
 			/*
6db311
 			 * The FAN_OPEN_EXEC_PERM mask is not supported by
6db311
 			 * all kernel releases prior to 5.0. Retry setting
6db311
diff --git a/src/library/conf.h b/src/library/conf.h
6db311
index e774ff6..57c19a2 100644
6db311
--- a/src/library/conf.h
6db311
+++ b/src/library/conf.h
6db311
@@ -1,5 +1,5 @@
6db311
 /* conf.h configuration structure
6db311
- * Copyright 2018-20 Red Hat Inc.
6db311
+ * Copyright 2018-20,22 Red Hat Inc.
6db311
  * All Rights Reserved.
6db311
  *
6db311
  * This program is free software; you can redistribute it and/or modify
6db311
@@ -45,6 +45,7 @@ typedef struct conf
6db311
 	integrity_t integrity;
6db311
 	const char *syslog_format;
6db311
 	unsigned int rpm_sha256_only;
6db311
+	unsigned int allow_filesystem_mark;
6db311
 } conf_t;
6db311
 
6db311
 #endif
6db311
diff --git a/src/library/daemon-config.c b/src/library/daemon-config.c
6db311
index e803e0b..89b7f68 100644
6db311
--- a/src/library/daemon-config.c
6db311
+++ b/src/library/daemon-config.c
6db311
@@ -1,7 +1,7 @@
6db311
 /*
6db311
  * daemon-config.c - This is a config file parser
6db311
  *
6db311
- * Copyright 2018-21 Red Hat Inc.
6db311
+ * Copyright 2018-22 Red Hat Inc.
6db311
  * All Rights Reserved.
6db311
  *
6db311
  * This library is free software; you can redistribute it and/or
6db311
@@ -92,6 +92,8 @@ static int syslog_format_parser(const struct nv_pair *nv, int line,
6db311
 		conf_t *config);
6db311
 static int rpm_sha256_only_parser(const struct nv_pair *nv, int line,
6db311
 		conf_t *config);
6db311
+static int fs_mark_parser(const struct nv_pair *nv, int line,
6db311
+		conf_t *config);
6db311
 
6db311
 static const struct kw_pair keywords[] =
6db311
 {
6db311
@@ -110,6 +112,7 @@ static const struct kw_pair keywords[] =
6db311
   {"integrity",		integrity_parser },
6db311
   {"syslog_format",	syslog_format_parser },
6db311
   {"rpm_sha256_only", rpm_sha256_only_parser},
6db311
+  {"allow_filesystem_mark",	fs_mark_parser },
6db311
   { NULL,		NULL }
6db311
 };
6db311
 
6db311
@@ -138,6 +141,7 @@ static void clear_daemon_config(conf_t *config)
6db311
 	config->syslog_format =
6db311
 		strdup("rule,dec,perm,auid,pid,exe,:,path,ftype");
6db311
 	config->rpm_sha256_only = 0;
6db311
+	config->allow_filesystem_mark = 0;
6db311
 }
6db311
 
6db311
 int load_daemon_config(conf_t *config)
6db311
@@ -590,6 +594,7 @@ static int syslog_format_parser(const struct nv_pair *nv, int line,
6db311
 	return 1;
6db311
 }
6db311
 
6db311
+
6db311
 static int rpm_sha256_only_parser(const struct nv_pair *nv, int line,
6db311
                 conf_t *config)
6db311
 {
6db311
@@ -607,3 +612,24 @@ static int rpm_sha256_only_parser(const struct nv_pair *nv, int line,
6db311
 
6db311
 	return rc;
6db311
 }
6db311
+
6db311
+
6db311
+static int fs_mark_parser(const struct nv_pair *nv, int line,
6db311
+		conf_t *config)
6db311
+{
6db311
+	int rc = 0;
6db311
+#ifndef HAVE_DECL_FAN_MARK_FILESYSTEM
6db311
+	msg(LOG_WARNING,
6db311
+	    "allow_filesystem_mark is unsupported on this kernel - ignoring");
6db311
+#else
6db311
+	rc = unsigned_int_parser(&(config->allow_filesystem_mark), nv->value, line);
6db311
+
6db311
+	if (rc == 0 && config->allow_filesystem_mark > 1) {
6db311
+		msg(LOG_WARNING,
6db311
+			"allow_filesystem_mark value reset to 0 - line %d", line);
6db311
+		config->allow_filesystem_mark = 0;
6db311
+	}
6db311
+#endif
6db311
+
6db311
+	return rc;
6db311
+}