]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/core/ima-setup.c
Merge pull request #73 from zonque/mountinfo
[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 <errno.h>
26 #include <fcntl.h>
27 #include <sys/stat.h>
28 #include <sys/mman.h>
29
30 #include "ima-setup.h"
31 #include "util.h"
32 #include "log.h"
33
34 #define IMA_SECFS_DIR "/sys/kernel/security/ima"
35 #define IMA_SECFS_POLICY IMA_SECFS_DIR "/policy"
36 #define IMA_POLICY_PATH "/etc/ima/ima-policy"
37
38 int ima_setup(void) {
39 int r = 0;
40
41 #ifdef HAVE_IMA
42 _cleanup_close_ int policyfd = -1, imafd = -1;
43 struct stat st;
44 char *policy;
45
46 if (access(IMA_SECFS_DIR, F_OK) < 0) {
47 log_debug("IMA support is disabled in the kernel, ignoring.");
48 return 0;
49 }
50
51 policyfd = open(IMA_POLICY_PATH, O_RDONLY|O_CLOEXEC);
52 if (policyfd < 0) {
53 log_full_errno(errno == ENOENT ? LOG_DEBUG : LOG_WARNING, errno,
54 "Failed to open the IMA custom policy file "IMA_POLICY_PATH", ignoring: %m");
55 return 0;
56 }
57
58 if (access(IMA_SECFS_POLICY, F_OK) < 0) {
59 log_warning("Another IMA custom policy has already been loaded, ignoring.");
60 return 0;
61 }
62
63 imafd = open(IMA_SECFS_POLICY, O_WRONLY|O_CLOEXEC);
64 if (imafd < 0) {
65 log_error_errno(errno, "Failed to open the IMA kernel interface "IMA_SECFS_POLICY", ignoring: %m");
66 return 0;
67 }
68
69 if (fstat(policyfd, &st) < 0)
70 return log_error_errno(errno, "Failed to fstat "IMA_POLICY_PATH": %m");
71
72 policy = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, policyfd, 0);
73 if (policy == MAP_FAILED)
74 return log_error_errno(errno, "Failed to mmap "IMA_POLICY_PATH": %m");
75
76 r = loop_write(imafd, policy, (size_t) st.st_size, false);
77 if (r < 0)
78 log_error_errno(r, "Failed to load the IMA custom policy file "IMA_POLICY_PATH": %m");
79 else
80 log_info("Successfully loaded the IMA custom policy "IMA_POLICY_PATH".");
81
82 munmap(policy, st.st_size);
83 #endif /* HAVE_IMA */
84 return r;
85 }