1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
4 This file is part of systemd.
6 Copyright 2010 Lennart Poettering
7 Copyright (C) 2012 Roberto Sassu - Politecnico di Torino, Italy
8 TORSEC group -- http://security.polito.it
10 systemd is free software; you can redistribute it and/or modify it
11 under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 2 of the License, or
13 (at your option) any later version.
15 systemd is distributed in the hope that it will be useful, but
16 WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with systemd; If not, see <http://www.gnu.org/licenses/>.
33 #include "ima-setup.h"
34 #include "mount-setup.h"
40 #define IMA_SECFS_DIR "/sys/kernel/security/ima"
41 #define IMA_SECFS_POLICY IMA_SECFS_DIR "/policy"
42 #define IMA_POLICY_PATH "/etc/ima/ima-policy"
48 ssize_t policy_size = 0, written = 0;
50 int policyfd = -1, imafd = -1;
54 /* Mount the securityfs filesystem */
58 if (stat(IMA_POLICY_PATH, &st) < 0)
61 policy_size = st.st_size;
62 if (stat(IMA_SECFS_DIR, &st) < 0) {
63 log_debug("IMA support is disabled in the kernel, ignoring.");
67 if (stat(IMA_SECFS_POLICY, &st) < 0) {
68 log_error("Another IMA custom policy has already been loaded, "
73 policyfd = open(IMA_POLICY_PATH, O_RDONLY|O_CLOEXEC);
75 log_error("Failed to open the IMA custom policy file %s (%m), "
76 "ignoring.", IMA_POLICY_PATH);
80 imafd = open(IMA_SECFS_POLICY, O_WRONLY|O_CLOEXEC);
82 log_error("Failed to open the IMA kernel interface %s (%m), "
83 "ignoring.", IMA_SECFS_POLICY);
87 policy = mmap(NULL, policy_size, PROT_READ, MAP_PRIVATE, policyfd, 0);
88 if (policy == MAP_FAILED) {
89 log_error("mmap() failed (%m), freezing");
94 written = loop_write(imafd, policy, (size_t)policy_size, false);
95 if (written != policy_size) {
96 log_error("Failed to load the IMA custom policy file %s (%m), "
97 "ignoring.", IMA_POLICY_PATH);
101 log_info("Successfully loaded the IMA custom policy %s.",
104 munmap(policy, policy_size);
107 close_nointr_nofail(policyfd);
109 close_nointr_nofail(imafd);
112 #endif /* HAVE_IMA */