anitazha / rpms / systemd

Forked from rpms/systemd 3 years ago
Clone

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

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