teknoraver / rpms / systemd

Forked from rpms/systemd 2 months ago
Clone

Blame SOURCES/0222-ima-setup-write-policy-one-line-at-a-time.patch

803fb7
From 877774af9162dde25e314ff99a427dd28435c26a Mon Sep 17 00:00:00 2001
803fb7
From: =?UTF-8?q?Zbigniew=20J=C4=99drzejewski-Szmek?= <zbyszek@in.waw.pl>
803fb7
Date: Wed, 10 Jun 2015 15:19:03 -0400
803fb7
Subject: [PATCH] ima-setup: write policy one line at a time
803fb7
803fb7
ima_write_policy() expects data to be written as one or more
803fb7
rules, no more than PAGE_SIZE at a time. Easiest way to ensure
803fb7
that we are not splitting rules is to read and write one line at
803fb7
a time.
803fb7
803fb7
https://bugzilla.redhat.com/show_bug.cgi?id=1226948
803fb7
(cherry picked from commit 92994160afa888255a7ede525dd16e3f1e2ed10d)
803fb7
803fb7
Cherry-picked from: 9299416
803fb7
Resolves: #1222517
803fb7
---
803fb7
 src/core/ima-setup.c | 41 +++++++++++++++++------------------------
803fb7
 1 file changed, 17 insertions(+), 24 deletions(-)
803fb7
803fb7
diff --git a/src/core/ima-setup.c b/src/core/ima-setup.c
803fb7
index 1d4acfa3b..81ce2cda9 100644
803fb7
--- a/src/core/ima-setup.c
803fb7
+++ b/src/core/ima-setup.c
803fb7
@@ -24,11 +24,6 @@
803fb7
 #include <unistd.h>
803fb7
 #include <stdio.h>
803fb7
 #include <errno.h>
803fb7
-#include <sys/types.h>
803fb7
-#include <sys/stat.h>
803fb7
-#include <fcntl.h>
803fb7
-#include <sys/stat.h>
803fb7
-#include <sys/mman.h>
803fb7
 
803fb7
 #include "ima-setup.h"
803fb7
 #include "util.h"
803fb7
@@ -39,20 +34,19 @@
803fb7
 #define IMA_POLICY_PATH "/etc/ima/ima-policy"
803fb7
 
803fb7
 int ima_setup(void) {
803fb7
-        int r = 0;
803fb7
-
803fb7
 #ifdef HAVE_IMA
803fb7
-        _cleanup_close_ int policyfd = -1, imafd = -1;
803fb7
-        struct stat st;
803fb7
-        char *policy;
803fb7
+        _cleanup_fclose_ FILE *input = NULL;
803fb7
+        _cleanup_close_ int imafd = -1;
803fb7
+        unsigned lineno = 0;
803fb7
+        char line[page_size()];
803fb7
 
803fb7
         if (access(IMA_SECFS_DIR, F_OK) < 0) {
803fb7
                 log_debug("IMA support is disabled in the kernel, ignoring.");
803fb7
                 return 0;
803fb7
         }
803fb7
 
803fb7
-        policyfd = open(IMA_POLICY_PATH, O_RDONLY|O_CLOEXEC);
803fb7
-        if (policyfd < 0) {
803fb7
+        input = fopen(IMA_POLICY_PATH, "re");
803fb7
+        if (!input) {
803fb7
                 log_full_errno(errno == ENOENT ? LOG_DEBUG : LOG_WARNING, errno,
803fb7
                                "Failed to open the IMA custom policy file "IMA_POLICY_PATH", ignoring: %m");
803fb7
                 return 0;
803fb7
@@ -69,20 +63,19 @@ int ima_setup(void) {
803fb7
                 return 0;
803fb7
         }
803fb7
 
803fb7
-        if (fstat(policyfd, &st) < 0)
803fb7
-                return log_error_errno(errno, "Failed to fstat "IMA_POLICY_PATH": %m");
803fb7
+        FOREACH_LINE(line, input,
803fb7
+                     return log_error_errno(errno, "Failed to read the IMA custom policy file "IMA_POLICY_PATH": %m")) {
803fb7
+                size_t len;
803fb7
 
803fb7
-        policy = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, policyfd, 0);
803fb7
-        if (policy == MAP_FAILED)
803fb7
-                return log_error_errno(errno, "Failed to mmap "IMA_POLICY_PATH": %m");
803fb7
+                len = strlen(line);
803fb7
+                lineno++;
803fb7
 
803fb7
-        r = loop_write(imafd, policy, (size_t) st.st_size, false);
803fb7
-        if (r < 0)
803fb7
-                log_error_errno(r, "Failed to load the IMA custom policy file "IMA_POLICY_PATH": %m");
803fb7
-        else
803fb7
-                log_info("Successfully loaded the IMA custom policy "IMA_POLICY_PATH".");
803fb7
+                if (len > 0 && write(imafd, line, len) < 0)
803fb7
+                        return log_error_errno(errno, "Failed to load the IMA custom policy file "IMA_POLICY_PATH"%u: %m",
803fb7
+                                               lineno);
803fb7
+        }
803fb7
 
803fb7
-        munmap(policy, st.st_size);
803fb7
+        log_info("Successfully loaded the IMA custom policy "IMA_POLICY_PATH".");
803fb7
 #endif /* HAVE_IMA */
803fb7
-        return r;
803fb7
+        return 0;
803fb7
 }