]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/core/smack-setup.c
treewide: use log_*_errno whenever %m is in the format string
[thirdparty/systemd.git] / src / core / smack-setup.c
CommitLineData
ffbd2c4d
NC
1/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2
3/***
4 This file is part of systemd.
5
6 Copyright (C) 2013 Intel Corporation
7 Authors:
8 Nathaniel Chen <nathaniel.chen@intel.com>
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
12 by the Free Software Foundation; either version 2.1 of the License,
13 or (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 <stdio.h>
25#include <errno.h>
26#include <string.h>
27#include <unistd.h>
28#include <stdlib.h>
29#include <sys/vfs.h>
30#include <fcntl.h>
31#include <sys/types.h>
32#include <dirent.h>
33#include <sys/mount.h>
34#include <stdint.h>
35
36#include "macro.h"
37#include "smack-setup.h"
38#include "util.h"
8b197c3a 39#include "fileio.h"
ffbd2c4d
NC
40#include "log.h"
41#include "label.h"
42
a4783bd1 43#define SMACK_CONFIG "/etc/smack/accesses.d/"
a1c9563c 44#define CIPSO_CONFIG "/etc/smack/cipso.d/"
ffbd2c4d 45
2b3e18de
KL
46#ifdef HAVE_SMACK
47
a4783bd1
ZJS
48static int write_rules(const char* dstpath, const char* srcdir) {
49 _cleanup_fclose_ FILE *dst = NULL;
ffbd2c4d
NC
50 _cleanup_closedir_ DIR *dir = NULL;
51 struct dirent *entry;
52 char buf[NAME_MAX];
53 int dfd = -1;
a4783bd1 54 int r = 0;
ffbd2c4d 55
a4783bd1
ZJS
56 dst = fopen(dstpath, "we");
57 if (!dst) {
58 if (errno != ENOENT)
56f64d95 59 log_warning_errno(errno, "Failed to open %s: %m", dstpath);
a4783bd1 60 return -errno; /* negative error */
ffbd2c4d
NC
61 }
62
a4783bd1
ZJS
63 /* write rules to dst from every file in the directory */
64 dir = opendir(srcdir);
ffbd2c4d 65 if (!dir) {
a4783bd1 66 if (errno != ENOENT)
56f64d95 67 log_warning_errno(errno, "Failed to opendir %s: %m", srcdir);
a4783bd1 68 return errno; /* positive on purpose */
ffbd2c4d
NC
69 }
70
71 dfd = dirfd(dir);
fea7838e 72 assert(dfd >= 0);
ffbd2c4d
NC
73
74 FOREACH_DIRENT(entry, dir, return 0) {
a4783bd1 75 int fd;
ffbd2c4d 76 _cleanup_fclose_ FILE *policy = NULL;
ffbd2c4d 77
a4783bd1
ZJS
78 fd = openat(dfd, entry->d_name, O_RDONLY|O_CLOEXEC);
79 if (fd < 0) {
80 if (r == 0)
81 r = -errno;
56f64d95 82 log_warning_errno(errno, "Failed to open %s: %m", entry->d_name);
ffbd2c4d
NC
83 continue;
84 }
85
a4783bd1 86 policy = fdopen(fd, "re");
ffbd2c4d 87 if (!policy) {
a4783bd1
ZJS
88 if (r == 0)
89 r = -errno;
03e334a1 90 safe_close(fd);
56f64d95 91 log_error_errno(errno, "Failed to open %s: %m", entry->d_name);
ffbd2c4d
NC
92 continue;
93 }
94
ffbd2c4d 95 /* load2 write rules in the kernel require a line buffered stream */
fea7838e 96 FOREACH_LINE(buf, policy,
56f64d95 97 log_error_errno(errno, "Failed to read line from %s: %m",
fea7838e 98 entry->d_name)) {
a4783bd1
ZJS
99 if (!fputs(buf, dst)) {
100 if (r == 0)
101 r = -EINVAL;
102 log_error("Failed to write line to %s", dstpath);
103 break;
104 }
105 if (fflush(dst)) {
106 if (r == 0)
107 r = -errno;
56f64d95 108 log_error_errno(errno, "Failed to flush writes to %s: %m", dstpath);
a4783bd1
ZJS
109 break;
110 }
ffbd2c4d
NC
111 }
112 }
113
a4783bd1
ZJS
114 return r;
115}
116
2b3e18de 117#endif
ffbd2c4d 118
8a188de9 119int mac_smack_setup(bool *loaded_policy) {
2b3e18de
KL
120
121#ifdef HAVE_SMACK
122
a4783bd1
ZJS
123 int r;
124
e49d3c01
ŁS
125 assert(loaded_policy);
126
a4783bd1
ZJS
127 r = write_rules("/sys/fs/smackfs/load2", SMACK_CONFIG);
128 switch(r) {
129 case -ENOENT:
130 log_debug("Smack is not enabled in the kernel.");
131 return 0;
132 case ENOENT:
133 log_debug("Smack access rules directory " SMACK_CONFIG " not found");
134 return 0;
135 case 0:
136 log_info("Successfully loaded Smack policies.");
abbacb1d
NC
137 break;
138 default:
139 log_warning("Failed to load Smack access rules: %s, ignoring.",
140 strerror(abs(r)));
141 return 0;
142 }
143
8b197c3a
AK
144#ifdef SMACK_RUN_LABEL
145 r = write_string_file("/proc/self/attr/current", SMACK_RUN_LABEL);
146 if (r)
147 log_warning("Failed to set SMACK label \"%s\" on self: %s",
148 SMACK_RUN_LABEL, strerror(-r));
149#endif
150
abbacb1d
NC
151 r = write_rules("/sys/fs/smackfs/cipso2", CIPSO_CONFIG);
152 switch(r) {
153 case -ENOENT:
154 log_debug("Smack/CIPSO is not enabled in the kernel.");
155 return 0;
156 case ENOENT:
157 log_debug("Smack/CIPSO access rules directory " CIPSO_CONFIG " not found");
158 return 0;
159 case 0:
160 log_info("Successfully loaded Smack/CIPSO policies.");
b9289d4c 161 break;
a4783bd1 162 default:
abbacb1d 163 log_warning("Failed to load Smack/CIPSO access rules: %s, ignoring.",
a4783bd1
ZJS
164 strerror(abs(r)));
165 return 0;
166 }
2b3e18de 167
e49d3c01
ŁS
168 *loaded_policy = true;
169
2b3e18de
KL
170#endif
171
172 return 0;
ffbd2c4d 173}