]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/core/ima-setup.c
man: add not to not use -x in bug reports
[thirdparty/systemd.git] / src / core / ima-setup.c
1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2
3 /***
4 This file is part of systemd.
5
6 Copyright 2010 Lennart Poettering
7 Copyright (C) 2012 Roberto Sassu - Politecnico di Torino, Italy
8 TORSEC group -- http://security.polito.it
9
10 systemd is free software; you can redistribute it and/or modify it
11 under the terms of the GNU Lesser General Public License as published by
12 the Free Software Foundation; either version 2.1 of the License, or
13 (at your option) any later version.
14
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 Lesser General Public License for more details.
19
20 You should have received a copy of the GNU Lesser General Public License
21 along with systemd; If not, see <http://www.gnu.org/licenses/>.
22 ***/
23
24 #include <unistd.h>
25 #include <stdio.h>
26 #include <errno.h>
27 #include <string.h>
28 #include <stdlib.h>
29 #include <fcntl.h>
30 #include <sys/stat.h>
31 #include <sys/mman.h>
32
33 #include "ima-setup.h"
34 #include "mount-setup.h"
35 #include "macro.h"
36 #include "util.h"
37 #include "log.h"
38 #include "label.h"
39
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"
43
44 int ima_setup(void) {
45
46 #ifdef HAVE_IMA
47 struct stat st;
48 ssize_t policy_size = 0, written = 0;
49 char *policy;
50 int policyfd = -1, imafd = -1;
51 int result = 0;
52
53 if (stat(IMA_POLICY_PATH, &st) < 0)
54 return 0;
55
56 policy_size = st.st_size;
57 if (stat(IMA_SECFS_DIR, &st) < 0) {
58 log_debug("IMA support is disabled in the kernel, ignoring.");
59 return 0;
60 }
61
62 if (stat(IMA_SECFS_POLICY, &st) < 0) {
63 log_error("Another IMA custom policy has already been loaded, "
64 "ignoring.");
65 return 0;
66 }
67
68 policyfd = open(IMA_POLICY_PATH, O_RDONLY|O_CLOEXEC);
69 if (policyfd < 0) {
70 log_error("Failed to open the IMA custom policy file %s (%m), "
71 "ignoring.", IMA_POLICY_PATH);
72 return 0;
73 }
74
75 imafd = open(IMA_SECFS_POLICY, O_WRONLY|O_CLOEXEC);
76 if (imafd < 0) {
77 log_error("Failed to open the IMA kernel interface %s (%m), "
78 "ignoring.", IMA_SECFS_POLICY);
79 goto out;
80 }
81
82 policy = mmap(NULL, policy_size, PROT_READ, MAP_PRIVATE, policyfd, 0);
83 if (policy == MAP_FAILED) {
84 log_error("mmap() failed (%m), freezing");
85 result = -errno;
86 goto out;
87 }
88
89 written = loop_write(imafd, policy, (size_t)policy_size, false);
90 if (written != policy_size) {
91 log_error("Failed to load the IMA custom policy file %s (%m), "
92 "ignoring.", IMA_POLICY_PATH);
93 goto out_mmap;
94 }
95
96 log_info("Successfully loaded the IMA custom policy %s.",
97 IMA_POLICY_PATH);
98 out_mmap:
99 munmap(policy, policy_size);
100 out:
101 if (policyfd >= 0)
102 close_nointr_nofail(policyfd);
103 if (imafd >= 0)
104 close_nointr_nofail(imafd);
105 if (result)
106 return result;
107 #endif /* HAVE_IMA */
108
109 return 0;
110 }