Blame SOURCES/fapolicyd-markfs-1.patch

b0079f
From 2d15ea13e2a3dca1bb159f2cf031ca437c0b9aa1 Mon Sep 17 00:00:00 2001
b0079f
From: Steve Grubb <sgrubb@redhat.com>
b0079f
Date: Tue, 27 Sep 2022 10:33:44 -0400
b0079f
Subject: [PATCH] Add support for using FAN_MARK_FILESYSTEM to see bind mounted
b0079f
 accesses
b0079f
b0079f
---
b0079f
 ChangeLog                   |  1 +
b0079f
 configure.ac                |  1 +
b0079f
 doc/fapolicyd.conf.5        |  5 ++++-
b0079f
 init/fapolicyd.conf         |  1 +
b0079f
 src/daemon/notify.c         | 12 ++++++++++--
b0079f
 src/library/conf.h          |  3 ++-
b0079f
 src/library/daemon-config.c | 28 +++++++++++++++++++++++++++-
b0079f
 7 files changed, 46 insertions(+), 5 deletions(-)
b0079f
b0079f
diff --git a/configure.ac b/configure.ac
b0079f
index 4437685..a67c46b 100644
b0079f
--- a/configure.ac
b0079f
+++ b/configure.ac
b0079f
@@ -56,6 +56,7 @@ AC_CHECK_DECLS([FAN_OPEN_EXEC_PERM], [perm=yes], [perm=no], [[#include 
b0079f
 if test $perm = "no"; then
b0079f
 	AC_MSG_ERROR([FAN_OPEN_EXEC_PERM is not defined in linux/fanotify.h. It is required for the kernel to support it])
b0079f
 fi
b0079f
+AC_CHECK_DECLS([FAN_MARK_FILESYSTEM], [], [], [[#include <linux/fanotify.h>]])
b0079f
 
b0079f
 withval=""
b0079f
 AC_ARG_WITH(rpm,
b0079f
diff --git a/doc/fapolicyd.conf.5 b/doc/fapolicyd.conf.5
b0079f
index 812cfa4..d8cb296 100644
b0079f
--- a/doc/fapolicyd.conf.5
b0079f
+++ b/doc/fapolicyd.conf.5
b0079f
@@ -1,4 +1,4 @@
b0079f
-.TH FAPOLICYD.CONF: "6" "October 2021" "Red Hat" "System Administration Utilities"
b0079f
+.TH FAPOLICYD.CONF: "6" "September 2022" "Red Hat" "System Administration Utilities"
b0079f
 .SH NAME
b0079f
 fapolicyd.conf \- fapolicyd configuration file
b0079f
 .SH DESCRIPTION
b0079f
@@ -87,6 +87,9 @@ Example:
b0079f
 .B rpm_sha256_only
b0079f
 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.
b0079f
 
b0079f
+.TP
b0079f
+.B allow_filesystem_mark
b0079f
+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.
b0079f
 
b0079f
 .SH "SEE ALSO"
b0079f
 .BR fapolicyd (8),
b0079f
diff --git a/init/fapolicyd.conf b/init/fapolicyd.conf
b0079f
index 42e8798..8363b89 100644
b0079f
--- a/init/fapolicyd.conf
b0079f
+++ b/init/fapolicyd.conf
b0079f
@@ -18,3 +18,4 @@ trust = rpmdb,file
b0079f
 integrity = none
b0079f
 syslog_format = rule,dec,perm,auid,pid,exe,:,path,ftype,trust
b0079f
 rpm_sha256_only = 0
b0079f
+allow_filesystem_mark = 0
b0079f
diff --git a/src/daemon/notify.c b/src/daemon/notify.c
b0079f
index f550e99..c91abc4 100644
b0079f
--- a/src/daemon/notify.c
b0079f
+++ b/src/daemon/notify.c
b0079f
@@ -123,8 +123,16 @@ int init_fanotify(const conf_t *conf, mlist *m)
b0079f
 	path = mlist_first(m);
b0079f
 	while (path) {
b0079f
 retry_mark:
b0079f
-		if (fanotify_mark(fd, FAN_MARK_ADD | FAN_MARK_MOUNT,
b0079f
-				mask, -1, path) == -1) {
b0079f
+		unsigned int flags = FAN_MARK_ADD;
b0079f
+#ifdef HAVE_DECL_FAN_MARK_FILESYSTEM
b0079f
+		if (conf->allow_filesystem_mark)
b0079f
+		    flags |= FAN_MARK_FILESYSTEM;
b0079f
+#else
b0079f
+		if (conf->allow_filesystem_mark)
b0079f
+			msg(LOG_ERR,
b0079f
+	    "allow_filesystem_mark is unsupported for this kernel - ignoring");
b0079f
+#endif
b0079f
+		if (fanotify_mark(fd, flags, mask, -1, path) == -1) {
b0079f
 			/*
b0079f
 			 * The FAN_OPEN_EXEC_PERM mask is not supported by
b0079f
 			 * all kernel releases prior to 5.0. Retry setting
b0079f
diff --git a/src/library/conf.h b/src/library/conf.h
b0079f
index e774ff6..57c19a2 100644
b0079f
--- a/src/library/conf.h
b0079f
+++ b/src/library/conf.h
b0079f
@@ -1,5 +1,5 @@
b0079f
 /* conf.h configuration structure
b0079f
- * Copyright 2018-20 Red Hat Inc.
b0079f
+ * Copyright 2018-20,22 Red Hat Inc.
b0079f
  * All Rights Reserved.
b0079f
  *
b0079f
  * This program is free software; you can redistribute it and/or modify
b0079f
@@ -45,6 +45,7 @@ typedef struct conf
b0079f
 	integrity_t integrity;
b0079f
 	const char *syslog_format;
b0079f
 	unsigned int rpm_sha256_only;
b0079f
+	unsigned int allow_filesystem_mark;
b0079f
 } conf_t;
b0079f
 
b0079f
 #endif
b0079f
diff --git a/src/library/daemon-config.c b/src/library/daemon-config.c
b0079f
index e803e0b..89b7f68 100644
b0079f
--- a/src/library/daemon-config.c
b0079f
+++ b/src/library/daemon-config.c
b0079f
@@ -1,7 +1,7 @@
b0079f
 /*
b0079f
  * daemon-config.c - This is a config file parser
b0079f
  *
b0079f
- * Copyright 2018-21 Red Hat Inc.
b0079f
+ * Copyright 2018-22 Red Hat Inc.
b0079f
  * All Rights Reserved.
b0079f
  *
b0079f
  * This library is free software; you can redistribute it and/or
b0079f
@@ -92,6 +92,8 @@ static int syslog_format_parser(const struct nv_pair *nv, int line,
b0079f
 		conf_t *config);
b0079f
 static int rpm_sha256_only_parser(const struct nv_pair *nv, int line,
b0079f
 		conf_t *config);
b0079f
+static int fs_mark_parser(const struct nv_pair *nv, int line,
b0079f
+		conf_t *config);
b0079f
 
b0079f
 static const struct kw_pair keywords[] =
b0079f
 {
b0079f
@@ -110,6 +112,7 @@ static const struct kw_pair keywords[] =
b0079f
   {"integrity",		integrity_parser },
b0079f
   {"syslog_format",	syslog_format_parser },
b0079f
   {"rpm_sha256_only", rpm_sha256_only_parser},
b0079f
+  {"allow_filesystem_mark",	fs_mark_parser },
b0079f
   { NULL,		NULL }
b0079f
 };
b0079f
 
b0079f
@@ -138,6 +141,7 @@ static void clear_daemon_config(conf_t *config)
b0079f
 	config->syslog_format =
b0079f
 		strdup("rule,dec,perm,auid,pid,exe,:,path,ftype");
b0079f
 	config->rpm_sha256_only = 0;
b0079f
+	config->allow_filesystem_mark = 0;
b0079f
 }
b0079f
 
b0079f
 int load_daemon_config(conf_t *config)
b0079f
@@ -590,6 +594,7 @@ static int syslog_format_parser(const struct nv_pair *nv, int line,
b0079f
 	return 1;
b0079f
 }
b0079f
 
b0079f
+
b0079f
 static int rpm_sha256_only_parser(const struct nv_pair *nv, int line,
b0079f
                 conf_t *config)
b0079f
 {
b0079f
@@ -607,3 +612,24 @@ static int rpm_sha256_only_parser(const struct nv_pair *nv, int line,
b0079f
 
b0079f
 	return rc;
b0079f
 }
b0079f
+
b0079f
+
b0079f
+static int fs_mark_parser(const struct nv_pair *nv, int line,
b0079f
+		conf_t *config)
b0079f
+{
b0079f
+	int rc = 0;
b0079f
+#ifndef HAVE_DECL_FAN_MARK_FILESYSTEM
b0079f
+	msg(LOG_WARNING,
b0079f
+	    "allow_filesystem_mark is unsupported on this kernel - ignoring");
b0079f
+#else
b0079f
+	rc = unsigned_int_parser(&(config->allow_filesystem_mark), nv->value, line);
b0079f
+
b0079f
+	if (rc == 0 && config->allow_filesystem_mark > 1) {
b0079f
+		msg(LOG_WARNING,
b0079f
+			"allow_filesystem_mark value reset to 0 - line %d", line);
b0079f
+		config->allow_filesystem_mark = 0;
b0079f
+	}
b0079f
+#endif
b0079f
+
b0079f
+	return rc;
b0079f
+}