richardphibel / rpms / rpm

Forked from rpms/rpm 2 years ago
Clone
45e748
From f1a92e02faa2715777286acd07b8d0f465c5df37 Mon Sep 17 00:00:00 2001
45e748
From: Jes Sorensen <jsorensen@fb.com>
45e748
Date: Mon, 20 Apr 2020 11:11:25 -0400
45e748
Subject: [PATCH 27/33] plugins/fsverity: Install fsverity signatures
45e748
45e748
This plugin installs fsverity signatures for regular files, when a signature
45e748
is found in the RPM. It tries to enable them unconditionally, but fails
45e748
gracefully if fsverity isn't supported or enabled.
45e748
45e748
Signed-off-by: Jes Sorensen <jsorensen@fb.com>
45e748
---
45e748
 configure.ac        |  29 ++++++++
45e748
 macros.in           |   4 +
45e748
 plugins/Makefile.am |   7 ++
45e748
 plugins/fsverity.c  | 177 ++++++++++++++++++++++++++++++++++++++++++++
45e748
 4 files changed, 217 insertions(+)
45e748
 create mode 100644 plugins/fsverity.c
45e748
45e748
diff --git a/configure.ac b/configure.ac
45e748
index cc7144440..7d3c31831 100644
45e748
--- a/configure.ac
45e748
+++ b/configure.ac
45e748
@@ -1049,6 +1049,11 @@ AS_IF([test "$enable_plugins" != no],[
45e748
 ])
45e748
 AM_CONDITIONAL(IMA, [test "x$ac_cv_func_lsetxattr" = xyes])
45e748
 
45e748
+AS_IF([test "$enable_plugins" != no],[
45e748
+AC_CHECK_HEADERS([linux/fsverity.h],[FSVERITY_IOCTL="yes"])
45e748
+])
45e748
+AM_CONDITIONAL(FSVERITY_IOCTL,[test "x$FSVERITY_IOCTL" = xyes])
45e748
+
45e748
 #=================
45e748
 # Check for fapolicyd support
45e748
 AC_ARG_WITH(fapolicyd,
45e748
diff --git a/macros.in b/macros.in
45e748
index fe8862903..3c722146b 100644
45e748
--- a/macros.in
45e748
+++ b/macros.in
45e748
@@ -767,6 +767,9 @@ package or when debugging this package.\
45e748
 # a wrong or missing signature.
45e748
 #%_ima_sign_config_files	0
45e748
 
45e748
+# Set to 1 to have fsverity signatures written for %config files.
45e748
+#%_fsverity_sign_config_files	0
45e748
+
45e748
 #
45e748
 # Default output format string for rpm -qa
45e748
 #
45e748
@@ -1185,6 +1188,7 @@ package or when debugging this package.\
45e748
 %__transaction_syslog		%{__plugindir}/syslog.so
45e748
 %__transaction_ima		%{__plugindir}/ima.so
45e748
 %__transaction_fapolicyd	%{__plugindir}/fapolicyd.so
45e748
+%__transaction_fsverity		%{__plugindir}/fsverity.so
45e748
 %__transaction_prioreset	%{__plugindir}/prioreset.so
45e748
 %__transaction_audit		%{__plugindir}/audit.so
45e748
 
45e748
diff --git a/plugins/Makefile.am b/plugins/Makefile.am
45e748
index cbfb81e19..e51b71f62 100644
45e748
--- a/plugins/Makefile.am
45e748
+++ b/plugins/Makefile.am
45e748
@@ -48,3 +48,10 @@ audit_la_sources = audit.c
45e748
 audit_la_LIBADD = $(top_builddir)/lib/librpm.la $(top_builddir)/rpmio/librpmio.la @WITH_AUDIT_LIB@
45e748
 plugins_LTLIBRARIES += audit.la
45e748
 endif
45e748
+
45e748
+if FSVERITY_IOCTL
45e748
+fsverity_la_sources = fsverity.c
45e748
+fsverity_la_LIBADD = $(top_builddir)/lib/librpm.la $(top_builddir)/rpmio/librpmio.la
45e748
+plugins_LTLIBRARIES += fsverity.la
45e748
+endif
45e748
+
45e748
diff --git a/plugins/fsverity.c b/plugins/fsverity.c
45e748
new file mode 100644
45e748
index 000000000..15ddcf33e
45e748
--- /dev/null
45e748
+++ b/plugins/fsverity.c
45e748
@@ -0,0 +1,177 @@
45e748
+/**
45e748
+ * Copyright (C) 2020 Facebook
45e748
+ *
45e748
+ * Author: Jes Sorensen <jsorensen@fb.com>
45e748
+ */
45e748
+
45e748
+#include "system.h"
45e748
+
45e748
+#include <errno.h>
45e748
+#include <fcntl.h>
45e748
+#include <sys/ioctl.h>
45e748
+#include <linux/fsverity.h>
45e748
+
45e748
+#include <rpm/rpmfi.h>
45e748
+#include <rpm/rpmte.h>
45e748
+#include <rpm/rpmfiles.h>
45e748
+#include <rpm/rpmtypes.h>
45e748
+#include <rpm/rpmlog.h>
45e748
+#include <rpmio/rpmstring.h>
45e748
+#include <rpmio/rpmmacro.h>
45e748
+
45e748
+#include "lib/rpmfs.h"
45e748
+#include "lib/rpmplugin.h"
45e748
+#include "lib/rpmte_internal.h"
45e748
+
45e748
+#include "sign/rpmsignverity.h"
45e748
+
45e748
+static int sign_config_files = 0;
45e748
+
45e748
+/*
45e748
+ * This unconditionally tries to apply the fsverity signature to a file,
45e748
+ * but fails gracefully if the file system doesn't support it or the
45e748
+ * verity feature flag isn't enabled in the file system (ext4).
45e748
+ */
45e748
+static rpmRC fsverity_fsm_file_prepare(rpmPlugin plugin, rpmfi fi,
45e748
+				       const char *path, const char *dest,
45e748
+				       mode_t file_mode, rpmFsmOp op)
45e748
+{
45e748
+    struct fsverity_enable_arg arg;
45e748
+    const unsigned char * signature = NULL;
45e748
+    size_t len;
45e748
+    int rc = RPMRC_OK;
45e748
+    int fd;
45e748
+    rpmFileAction action = XFO_ACTION(op);
45e748
+    char *buffer;
45e748
+
45e748
+    /* Ignore skipped files and unowned directories */
45e748
+    if (XFA_SKIPPING(action) || (op & FAF_UNOWNED)) {
45e748
+	rpmlog(RPMLOG_DEBUG, "fsverity skipping early: path %s dest %s\n",
45e748
+	       path, dest);
45e748
+	goto exit;
45e748
+    }
45e748
+
45e748
+    /*
45e748
+     * Do not install signatures for config files unless the
45e748
+     * user explicitly asks for it.
45e748
+     */
45e748
+    if (rpmfiFFlags(fi) & RPMFILE_CONFIG) {
45e748
+	if (!(rpmfiFMode(fi) & (S_IXUSR|S_IXGRP|S_IXOTH)) &&
45e748
+	    !sign_config_files) {
45e748
+	    rpmlog(RPMLOG_DEBUG, "fsverity skipping: path %s dest %s\n",
45e748
+		   path, dest);
45e748
+
45e748
+	    goto exit;
45e748
+	}
45e748
+    }
45e748
+
45e748
+    /*
45e748
+     * Right now fsverity doesn't deal with symlinks or directories, so do
45e748
+     * not try to install signatures for non regular files.
45e748
+     */
45e748
+    if (!S_ISREG(rpmfiFMode(fi))) {
45e748
+	rpmlog(RPMLOG_DEBUG, "fsverity skipping non regular: path %s dest %s\n",
45e748
+	       path, dest);
45e748
+	goto exit;
45e748
+    }
45e748
+
45e748
+    signature = rpmfiVSignature(fi, &len;;
45e748
+    if (!signature || !len) {
45e748
+	rpmlog(RPMLOG_DEBUG, "fsverity no signature for: path %s dest %s\n",
45e748
+	       path, dest);
45e748
+	goto exit;
45e748
+    }
45e748
+
45e748
+    memset(&arg, 0, sizeof(arg));
45e748
+    arg.version = 1;
45e748
+    arg.hash_algorithm = FS_VERITY_HASH_ALG_SHA256;
45e748
+    arg.block_size = RPM_FSVERITY_BLKSZ;
45e748
+    arg.sig_ptr = (uintptr_t)signature;
45e748
+    arg.sig_size = len;
45e748
+
45e748
+    buffer = pgpHexStr(signature, arg.sig_size);
45e748
+    rpmlog(RPMLOG_DEBUG, "applying signature: %s\n", buffer);
45e748
+    free(buffer);
45e748
+
45e748
+    fd = open(path, O_RDONLY);
45e748
+    if (fd < 0) {
45e748
+	rpmlog(RPMLOG_ERR, "failed to open path %s\n", path);
45e748
+	goto exit;
45e748
+    }
45e748
+
45e748
+    /*
45e748
+     * Enable fsverity on the file.
45e748
+     * fsverity not supported by file system (ENOTTY) and fsverity not
45e748
+     * enabled on file system are expected and not considered
45e748
+     * errors. Every other non-zero error code will result in the
45e748
+     * installation failing.
45e748
+     */
45e748
+    if (ioctl(fd, FS_IOC_ENABLE_VERITY, &arg) != 0) {
45e748
+	switch(errno) {
45e748
+	case EBADMSG:
45e748
+	    rpmlog(RPMLOG_DEBUG, "invalid or malformed fsverity signature for %s\n", path);
45e748
+	    rc = RPMRC_FAIL;
45e748
+	    break;
45e748
+	case EEXIST:
45e748
+	    rpmlog(RPMLOG_DEBUG, "fsverity signature already enabled %s\n",
45e748
+		   path);
45e748
+	    rc = RPMRC_FAIL;
45e748
+	    break;
45e748
+	case EINVAL:
45e748
+	    rpmlog(RPMLOG_DEBUG, "invalid arguments for ioctl %s\n", path);
45e748
+	    rc = RPMRC_FAIL;
45e748
+	    break;
45e748
+	case EKEYREJECTED:
45e748
+	    rpmlog(RPMLOG_DEBUG, "signature doesn't match file %s\n", path);
45e748
+	    rc = RPMRC_FAIL;
45e748
+	    break;
45e748
+	case EMSGSIZE:
45e748
+	    rpmlog(RPMLOG_DEBUG, "invalid signature size for %s\n", path);
45e748
+	    rc = RPMRC_FAIL;
45e748
+	    break;
45e748
+	case ENOPKG:
45e748
+	    rpmlog(RPMLOG_DEBUG, "unsupported signature algoritm (%i) for %s\n",
45e748
+		   arg.hash_algorithm, path);
45e748
+	    rc = RPMRC_FAIL;
45e748
+	    break;
45e748
+	case ETXTBSY:
45e748
+	    rpmlog(RPMLOG_DEBUG, "file is open by other process %s\n",
45e748
+		   path);
45e748
+	    rc = RPMRC_FAIL;
45e748
+	    break;
45e748
+	case ENOTTY:
45e748
+	    rpmlog(RPMLOG_DEBUG, "fsverity not supported by file system for %s\n",
45e748
+		   path);
45e748
+	    break;
45e748
+	case EOPNOTSUPP:
45e748
+	    rpmlog(RPMLOG_DEBUG, "fsverity not enabled on file system for %s\n",
45e748
+		   path);
45e748
+	    break;
45e748
+	default:
45e748
+	    rpmlog(RPMLOG_DEBUG, "failed to enable verity (errno %i) for %s\n",
45e748
+		   errno, path);
45e748
+	    rc = RPMRC_FAIL;
45e748
+	    break;
45e748
+	}
45e748
+    }
45e748
+
45e748
+    rpmlog(RPMLOG_DEBUG, "fsverity enabled signature for: path %s dest %s\n",
45e748
+	   path, dest);
45e748
+    close(fd);
45e748
+exit:
45e748
+    return rc;
45e748
+}
45e748
+
45e748
+static rpmRC fsverity_init(rpmPlugin plugin, rpmts ts)
45e748
+{
45e748
+    sign_config_files = rpmExpandNumeric("%{?_fsverity_sign_config_files}");
45e748
+
45e748
+    rpmlog(RPMLOG_DEBUG, "fsverity_init\n");
45e748
+
45e748
+    return RPMRC_OK;
45e748
+}
45e748
+
45e748
+struct rpmPluginHooks_s fsverity_hooks = {
45e748
+    .init = fsverity_init,
45e748
+    .fsm_file_prepare = fsverity_fsm_file_prepare,
45e748
+};
45e748
-- 
45e748
2.27.0
45e748