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

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