anitazha / rpms / systemd

Forked from rpms/systemd 3 years ago
Clone

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

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