teknoraver / rpms / rpm

Forked from rpms/rpm 3 months ago
Clone

Blame SOURCES/rpm-4.14.3-add-fapolicyd-rpm-plugin.patch

976aaf
From c33faabc2d09b9ad8c80b941b6114c1e4c2be80f Mon Sep 17 00:00:00 2001
976aaf
Message-Id: <c33faabc2d09b9ad8c80b941b6114c1e4c2be80f.1612252390.git.pmatilai@redhat.com>
976aaf
From: Radovan Sroka <rsroka@redhat.com>
976aaf
Date: Tue, 27 Oct 2020 16:18:04 +0100
976aaf
Subject: [PATCH] Added fapolicyd rpm plugin
976aaf
976aaf
Fapolicyd (File Access Policy Daemon) implements application whitelisting
976aaf
to decide file access rights. Applications that are known via a reputation
976aaf
source are allowed access while unknown applications are not.
976aaf
976aaf
The rpm plugin allows us to use rpm database as a source of trust.
976aaf
We used dnf plugin since the beggining but it only provides notification
976aaf
when transaction ends. With "integrity checking" requirement we need
976aaf
a continual addition of files which are installed during the system
976aaf
update. With fapolicyd rpm plugin we can allow using of recently
976aaf
added/updated files in scriptlets during rpm transaction.
976aaf
976aaf
The fapolicyd plugin gathers metadata of currently installed files.
976aaf
It sends the information about files and about ongoing rpm transaction
976aaf
to the fapolicyd daemon. The information is written to Linux pipe which
976aaf
is placed in /var/run/fapolicyd/fapolicyd.fifo.
976aaf
976aaf
The data format is "%s %lu %64s\n". [path, size, sha256]
976aaf
976aaf
The fapolicyd rpm plugin can be enabled with "--with-fapolicyd"
976aaf
configure option.
976aaf
976aaf
Related PRs:
976aaf
https://github.com/linux-application-whitelisting/fapolicyd/pull/105
976aaf
https://github.com/linux-application-whitelisting/fapolicyd/pull/106
976aaf
976aaf
Signed-off-by: Radovan Sroka <rsroka@redhat.com>
976aaf
(cherry picked from commit 39595ccee321497dc3b08c7cab8a10304345429c)
976aaf
976aaf
Backported from commit 39595ccee321497dc3b08c7cab8a10304345429c
976aaf
---
976aaf
 Makefile.am                |   1 +
976aaf
 configure.ac               |   8 ++
976aaf
 doc/Makefile.am            |   2 +-
976aaf
 doc/rpm-plugin-fapolicyd.8 |  21 +++++
976aaf
 macros.in                  |   1 +
976aaf
 plugins/Makefile.am        |   6 ++
976aaf
 plugins/fapolicyd.c        | 189 +++++++++++++++++++++++++++++++++++++
976aaf
 7 files changed, 227 insertions(+), 1 deletion(-)
976aaf
 create mode 100644 doc/rpm-plugin-fapolicyd.8
976aaf
 create mode 100644 plugins/fapolicyd.c
976aaf
976aaf
diff --git a/Makefile.am b/Makefile.am
976aaf
index 1f20f05b7..8e92f0cde 100644
976aaf
--- a/Makefile.am
976aaf
+++ b/Makefile.am
976aaf
@@ -16,6 +16,7 @@ DISTCHECK_CONFIGURE_FLAGS = \
976aaf
 	--with-selinux \
976aaf
 	--with-imaevm \
976aaf
 	--with-crypto=openssl \
976aaf
+	--with-fapolicyd \
976aaf
 	--disable-dependency-tracking
976aaf
 
976aaf
 include $(top_srcdir)/rpm.am
976aaf
diff --git a/configure.ac b/configure.ac
976aaf
index 3fcb3ff20..3d0e9ef91 100644
976aaf
--- a/configure.ac
976aaf
+++ b/configure.ac
976aaf
@@ -983,6 +983,14 @@ AS_IF([test "$enable_inhibit_plugin" = yes],[
976aaf
 ])
976aaf
 AM_CONDITIONAL(ENABLE_INHIBIT_PLUGIN,[test "$enable_inhibit_plugin" = yes])
976aaf
 
976aaf
+#=================
976aaf
+# Check for fapolicyd support
976aaf
+AC_ARG_WITH(fapolicyd,
976aaf
+AS_HELP_STRING([--with-fapolicyd],[build with File Access Policy Daemon support]),
976aaf
+with_fapolicyd=$withval,
976aaf
+with_fapolicyd=auto)
976aaf
+AM_CONDITIONAL(FAPOLICYD,[test "$with_fapolicyd" = yes])
976aaf
+
976aaf
 with_dbus=no
976aaf
 AS_IF([test "$enable_plugins" != no],[
976aaf
  AS_IF([test "$enable_inhibit_plugin" != no],[
976aaf
diff --git a/doc/Makefile.am b/doc/Makefile.am
976aaf
index d2f520d64..535ad3ec3 100644
976aaf
--- a/doc/Makefile.am
976aaf
+++ b/doc/Makefile.am
976aaf
@@ -9,7 +9,7 @@ EXTRA_DIST += $(man_man1_DATA)
976aaf
 man_man8dir = $(mandir)/man8
976aaf
 man_man8_DATA = rpm.8 rpm-misc.8 rpmbuild.8 rpmdeps.8 rpmgraph.8 rpm2cpio.8
976aaf
 man_man8_DATA += rpmdb.8 rpmkeys.8 rpmsign.8 rpmspec.8
976aaf
-man_man8_DATA += rpm-plugin-systemd-inhibit.8
976aaf
+man_man8_DATA += rpm-plugin-systemd-inhibit.8 rpm-plugin-fapolicyd.8
976aaf
 EXTRA_DIST += $(man_man8_DATA)
976aaf
 
976aaf
 man_fr_man8dir = $(mandir)/fr/man8
976aaf
diff --git a/doc/rpm-plugin-fapolicyd.8 b/doc/rpm-plugin-fapolicyd.8
976aaf
new file mode 100644
976aaf
index 000000000..fe7a8c78e
976aaf
--- /dev/null
976aaf
+++ b/doc/rpm-plugin-fapolicyd.8
976aaf
@@ -0,0 +1,21 @@
976aaf
+'\" t
976aaf
+.TH "RPM-FAPOLICYD" "8" "28 Jan 2021" "Red Hat, Inc."
976aaf
+.SH NAME
976aaf
+rpm-plugin-fapolicyd \- Fapolicyd plugin for the RPM Package Manager
976aaf
+
976aaf
+.SH Description
976aaf
+
976aaf
+The plugin gathers metadata of currently installed files. It sends the
976aaf
+information about files and about ongoing rpm transaction to the fapolicyd daemon.
976aaf
+The information is written to Linux pipe which is placed in
976aaf
+/var/run/fapolicyd/fapolicyd.fifo.
976aaf
+
976aaf
+.SH Configuration
976aaf
+
976aaf
+There are currently no options for this plugin in particular. See
976aaf
+.BR rpm-plugins (8)
976aaf
+on how to control plugins in general.
976aaf
+
976aaf
+.SH SEE ALSO
976aaf
+.IR fapolicyd (8)
976aaf
+.IR rpm-plugins (8)
976aaf
diff --git a/macros.in b/macros.in
976aaf
index a6069ee4d..2fbda64cc 100644
976aaf
--- a/macros.in
976aaf
+++ b/macros.in
976aaf
@@ -1173,6 +1173,7 @@ package or when debugging this package.\
976aaf
 %__transaction_selinux		%{__plugindir}/selinux.so
976aaf
 %__transaction_syslog		%{__plugindir}/syslog.so
976aaf
 %__transaction_ima		%{__plugindir}/ima.so
976aaf
+%__transaction_fapolicyd	%{__plugindir}/fapolicyd.so
976aaf
 %__transaction_prioreset	%{__plugindir}/prioreset.so
976aaf
 
976aaf
 #------------------------------------------------------------------------------
976aaf
diff --git a/plugins/Makefile.am b/plugins/Makefile.am
976aaf
index ab4eee34f..cbfb81e19 100644
976aaf
--- a/plugins/Makefile.am
976aaf
+++ b/plugins/Makefile.am
976aaf
@@ -42,3 +42,9 @@ ima_la_sources = ima.c
976aaf
 ima_la_LIBADD = $(top_builddir)/lib/librpm.la $(top_builddir)/rpmio/librpmio.la
976aaf
 plugins_LTLIBRARIES += ima.la
976aaf
 endif
976aaf
+
976aaf
+if FAPOLICYD
976aaf
+fapolicyd_la_sources = fapolicyd.c
976aaf
+fapolicyd_la_LIBADD = $(top_builddir)/lib/librpm.la $(top_builddir)/rpmio/librpmio.la
976aaf
+plugins_LTLIBRARIES += fapolicyd.la
976aaf
+endif
976aaf
diff --git a/plugins/fapolicyd.c b/plugins/fapolicyd.c
976aaf
new file mode 100644
976aaf
index 000000000..50f50155c
976aaf
--- /dev/null
976aaf
+++ b/plugins/fapolicyd.c
976aaf
@@ -0,0 +1,189 @@
976aaf
+#include "system.h"
976aaf
+
976aaf
+#include <rpm/rpmts.h>
976aaf
+#include <rpm/rpmlog.h>
976aaf
+#include "lib/rpmplugin.h"
976aaf
+
976aaf
+#include <fcntl.h>
976aaf
+#include <errno.h>
976aaf
+#include <unistd.h>
976aaf
+#include <sys/stat.h>
976aaf
+
976aaf
+struct fapolicyd_data {
976aaf
+    int fd;
976aaf
+    long changed_files;
976aaf
+    const char * fifo_path;
976aaf
+};
976aaf
+
976aaf
+static struct fapolicyd_data fapolicyd_state = {
976aaf
+    .fd = -1,
976aaf
+    .changed_files = 0,
976aaf
+    .fifo_path = "/run/fapolicyd/fapolicyd.fifo",
976aaf
+};
976aaf
+
976aaf
+static rpmRC open_fifo(struct fapolicyd_data* state)
976aaf
+{
976aaf
+    int fd = -1;
976aaf
+    struct stat s;
976aaf
+
976aaf
+    fd = open(state->fifo_path, O_RDWR);
976aaf
+    if (fd == -1) {
976aaf
+        rpmlog(RPMLOG_DEBUG, "Open: %s -> %s\n", state->fifo_path, strerror(errno));
976aaf
+        goto bad;
976aaf
+    }
976aaf
+
976aaf
+    if (stat(state->fifo_path, &s) == -1) {
976aaf
+        rpmlog(RPMLOG_DEBUG, "Stat: %s -> %s\n", state->fifo_path, strerror(errno));
976aaf
+        goto bad;
976aaf
+    }
976aaf
+
976aaf
+    if (!S_ISFIFO(s.st_mode)) {
976aaf
+        rpmlog(RPMLOG_DEBUG, "File: %s exists but it is not a pipe!\n", state->fifo_path);
976aaf
+        goto bad;
976aaf
+    }
976aaf
+
976aaf
+    /* keep only file's permition bits */
976aaf
+    mode_t mode = s.st_mode & ~S_IFMT;
976aaf
+
976aaf
+    /* we require pipe to have 0660 permission */
976aaf
+    if (mode != 0660) {
976aaf
+        rpmlog(RPMLOG_ERR, "File: %s has %o instead of 0660 \n",
976aaf
+               state->fifo_path,
976aaf
+               mode );
976aaf
+        goto bad;
976aaf
+    }
976aaf
+
976aaf
+    state->fd = fd;
976aaf
+    /* considering success */
976aaf
+    return RPMRC_OK;
976aaf
+
976aaf
+ bad:
976aaf
+    if (fd > 0)
976aaf
+        close(fd);
976aaf
+    return RPMRC_FAIL;
976aaf
+}
976aaf
+
976aaf
+static rpmRC write_fifo(struct fapolicyd_data* state, const char * str)
976aaf
+{
976aaf
+    ssize_t len = strlen(str);
976aaf
+    ssize_t written = 0;
976aaf
+    ssize_t n = 0;
976aaf
+
976aaf
+    while (written < len) {
976aaf
+        if ((n = write(state->fd, str + written, len - written)) < 0) {
976aaf
+            if (errno == EINTR || errno == EAGAIN)
976aaf
+                continue;
976aaf
+            rpmlog(RPMLOG_DEBUG, "Write: %s -> %s\n", state->fifo_path, strerror(errno));
976aaf
+            goto bad;
976aaf
+        }
976aaf
+        written += n;
976aaf
+    }
976aaf
+
976aaf
+    return RPMRC_OK;
976aaf
+
976aaf
+ bad:
976aaf
+    return RPMRC_FAIL;
976aaf
+}
976aaf
+
976aaf
+static rpmRC fapolicyd_init(rpmPlugin plugin, rpmts ts)
976aaf
+{
976aaf
+    if (rpmtsFlags(ts) & (RPMTRANS_FLAG_TEST|RPMTRANS_FLAG_BUILD_PROBS))
976aaf
+        goto end;
976aaf
+
976aaf
+    if (!rstreq(rpmtsRootDir(ts), "/"))
976aaf
+        goto end;
976aaf
+
976aaf
+    (void) open_fifo(&fapolicyd_state);
976aaf
+
976aaf
+ end:
976aaf
+    return RPMRC_OK;
976aaf
+}
976aaf
+
976aaf
+static void fapolicyd_cleanup(rpmPlugin plugin)
976aaf
+{
976aaf
+    if (fapolicyd_state.fd > 0)
976aaf
+        (void) close(fapolicyd_state.fd);
976aaf
+
976aaf
+    fapolicyd_state.fd = -1;
976aaf
+}
976aaf
+
976aaf
+static rpmRC fapolicyd_tsm_post(rpmPlugin plugin, rpmts ts, int res)
976aaf
+{
976aaf
+    if (rpmtsFlags(ts) & (RPMTRANS_FLAG_TEST|RPMTRANS_FLAG_BUILD_PROBS))
976aaf
+        goto end;
976aaf
+
976aaf
+    /* we are ready */
976aaf
+    if (fapolicyd_state.fd > 0) {
976aaf
+        /* send a signal that transaction is over */
976aaf
+        (void) write_fifo(&fapolicyd_state, "1\n");
976aaf
+        /* flush cache */
976aaf
+        (void) write_fifo(&fapolicyd_state, "2\n");
976aaf
+    }
976aaf
+
976aaf
+ end:
976aaf
+    return RPMRC_OK;
976aaf
+}
976aaf
+
976aaf
+static rpmRC fapolicyd_scriptlet_pre(rpmPlugin plugin, const char *s_name,
976aaf
+                                     int type)
976aaf
+{
976aaf
+    if (fapolicyd_state.fd == -1)
976aaf
+        goto end;
976aaf
+
976aaf
+    if (fapolicyd_state.changed_files > 0) {
976aaf
+        /* send signal to flush cache */
976aaf
+        (void) write_fifo(&fapolicyd_state, "2\n");
976aaf
+
976aaf
+        /* optimize flushing */
976aaf
+        /* flush only when there was an actual change */
976aaf
+        fapolicyd_state.changed_files = 0;
976aaf
+    }
976aaf
+
976aaf
+ end:
976aaf
+    return RPMRC_OK;
976aaf
+}
976aaf
+
976aaf
+static rpmRC fapolicyd_fsm_file_prepare(rpmPlugin plugin, rpmfi fi,
976aaf
+                                        const char *path, const char *dest,
976aaf
+                                        mode_t file_mode, rpmFsmOp op)
976aaf
+{
976aaf
+    /* not ready  */
976aaf
+    if (fapolicyd_state.fd == -1)
976aaf
+        goto end;
976aaf
+
976aaf
+    rpmFileAction action = XFO_ACTION(op);
976aaf
+
976aaf
+    /* Ignore skipped files and unowned directories */
976aaf
+    if (XFA_SKIPPING(action) || (op & FAF_UNOWNED)) {
976aaf
+        rpmlog(RPMLOG_DEBUG, "fapolicyd skipping early: path %s dest %s\n",
976aaf
+               path, dest);
976aaf
+        goto end;
976aaf
+    }
976aaf
+
976aaf
+    if (!S_ISREG(rpmfiFMode(fi))) {
976aaf
+        rpmlog(RPMLOG_DEBUG, "fapolicyd skipping non regular: path %s dest %s\n",
976aaf
+               path, dest);
976aaf
+        goto end;
976aaf
+    }
976aaf
+
976aaf
+    fapolicyd_state.changed_files++;
976aaf
+
976aaf
+    char buffer[4096];
976aaf
+
976aaf
+    rpm_loff_t size = rpmfiFSize(fi);
976aaf
+    char * sha = rpmfiFDigestHex(fi, NULL);
976aaf
+
976aaf
+    snprintf(buffer, 4096, "%s %lu %64s\n", dest, size, sha);
976aaf
+    (void) write_fifo(&fapolicyd_state, buffer);
976aaf
+
976aaf
+ end:
976aaf
+    return RPMRC_OK;
976aaf
+}
976aaf
+
976aaf
+struct rpmPluginHooks_s fapolicyd_hooks = {
976aaf
+    .init = fapolicyd_init,
976aaf
+    .cleanup = fapolicyd_cleanup,
976aaf
+    .scriptlet_pre = fapolicyd_scriptlet_pre,
976aaf
+    .tsm_post = fapolicyd_tsm_post,
976aaf
+    .fsm_file_prepare = fapolicyd_fsm_file_prepare,
976aaf
+};
976aaf
-- 
976aaf
2.29.2
976aaf
976aaf
commit c66cee32e74ce1e507c031605e3d7b2c1391a52c
976aaf
Author: Radovan Sroka <rsroka@redhat.com>
976aaf
Date:   Wed Feb 10 17:04:55 2021 +0100
976aaf
976aaf
    Fixed issues find by coverity
976aaf
    
976aaf
    - enhance the check for the file descriptor fd because 0 is also a valid
976aaf
    descriptor
976aaf
    
976aaf
    - added free() for sha so it doesn't leak memory for every file that is
976aaf
    processed
976aaf
    
976aaf
    Signed-off-by: Radovan Sroka <rsroka@redhat.com>
976aaf
976aaf
diff --git a/plugins/fapolicyd.c b/plugins/fapolicyd.c
976aaf
index 50f50155c..48f65ae11 100644
976aaf
--- a/plugins/fapolicyd.c
976aaf
+++ b/plugins/fapolicyd.c
976aaf
@@ -58,7 +58,7 @@ static rpmRC open_fifo(struct fapolicyd_data* state)
976aaf
     return RPMRC_OK;
976aaf
 
976aaf
  bad:
976aaf
-    if (fd > 0)
976aaf
+    if (fd >= 0)
976aaf
         close(fd);
976aaf
     return RPMRC_FAIL;
976aaf
 }
976aaf
@@ -176,6 +176,8 @@ static rpmRC fapolicyd_fsm_file_prepare(rpmPlugin plugin, rpmfi fi,
976aaf
     snprintf(buffer, 4096, "%s %lu %64s\n", dest, size, sha);
976aaf
     (void) write_fifo(&fapolicyd_state, buffer);
976aaf
 
976aaf
+    free(sha);
976aaf
+
976aaf
  end:
976aaf
     return RPMRC_OK;
976aaf
 }