]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/core/ima-setup.c
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
3 Copyright © 2012 Roberto Sassu - Politecnico di Torino, Italy
4 TORSEC group — http://security.polito.it
10 #include <sys/types.h>
13 #include "alloc-util.h"
16 #include "ima-setup.h"
19 #define IMA_SECFS_DIR "/sys/kernel/security/ima"
20 #define IMA_SECFS_POLICY IMA_SECFS_DIR "/policy"
21 #define IMA_POLICY_PATH "/etc/ima/ima-policy"
25 _cleanup_fclose_
FILE *input
= NULL
;
26 _cleanup_close_
int imafd
= -EBADF
;
30 if (access(IMA_SECFS_DIR
, F_OK
) < 0) {
31 log_debug_errno(errno
, "IMA support is disabled in the kernel, ignoring: %m");
35 if (access(IMA_SECFS_POLICY
, W_OK
) < 0) {
36 log_warning_errno(errno
, "Another IMA custom policy has already been loaded, ignoring: %m");
40 if (access(IMA_POLICY_PATH
, F_OK
) < 0) {
41 log_debug_errno(errno
, "No IMA custom policy file "IMA_POLICY_PATH
", ignoring: %m");
45 imafd
= open(IMA_SECFS_POLICY
, O_WRONLY
|O_CLOEXEC
);
47 log_error_errno(errno
, "Failed to open the IMA kernel interface "IMA_SECFS_POLICY
", ignoring: %m");
51 /* attempt to write the name of the policy file into sysfs file */
52 if (write(imafd
, IMA_POLICY_PATH
, STRLEN(IMA_POLICY_PATH
)) > 0)
55 /* fall back to copying the policy line-by-line */
56 input
= fopen(IMA_POLICY_PATH
, "re");
58 log_warning_errno(errno
, "Failed to open the IMA custom policy file "IMA_POLICY_PATH
", ignoring: %m");
64 imafd
= open(IMA_SECFS_POLICY
, O_WRONLY
|O_CLOEXEC
);
66 log_error_errno(errno
, "Failed to open the IMA kernel interface "IMA_SECFS_POLICY
", ignoring: %m");
71 _cleanup_free_
char *line
= NULL
;
74 r
= read_line(input
, LONG_LINE_MAX
, &line
);
76 return log_error_errno(r
, "Failed to read the IMA custom policy file "IMA_POLICY_PATH
": %m");
83 if (len
> 0 && write(imafd
, line
, len
) < 0)
84 return log_error_errno(errno
, "Failed to load the IMA custom policy file "IMA_POLICY_PATH
"%u: %m",
89 log_info("Successfully loaded the IMA custom policy "IMA_POLICY_PATH
".");
90 #endif /* ENABLE_IMA */