dcavalca / rpms / rpm

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