]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/core/smack-setup.c
Mount /run, /dev/shm usable to tasks when using SMACK.
[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"
39#include "log.h"
40#include "label.h"
41
a4783bd1 42#define SMACK_CONFIG "/etc/smack/accesses.d/"
a1c9563c 43#define CIPSO_CONFIG "/etc/smack/cipso.d/"
ffbd2c4d 44
2b3e18de
KL
45#ifdef HAVE_SMACK
46
a4783bd1
ZJS
47static int write_rules(const char* dstpath, const char* srcdir) {
48 _cleanup_fclose_ FILE *dst = NULL;
ffbd2c4d
NC
49 _cleanup_closedir_ DIR *dir = NULL;
50 struct dirent *entry;
51 char buf[NAME_MAX];
52 int dfd = -1;
a4783bd1 53 int r = 0;
ffbd2c4d 54
a4783bd1
ZJS
55 dst = fopen(dstpath, "we");
56 if (!dst) {
57 if (errno != ENOENT)
58 log_warning("Failed to open %s: %m", dstpath);
59 return -errno; /* negative error */
ffbd2c4d
NC
60 }
61
a4783bd1
ZJS
62 /* write rules to dst from every file in the directory */
63 dir = opendir(srcdir);
ffbd2c4d 64 if (!dir) {
a4783bd1
ZJS
65 if (errno != ENOENT)
66 log_warning("Failed to opendir %s: %m", srcdir);
67 return errno; /* positive on purpose */
ffbd2c4d
NC
68 }
69
70 dfd = dirfd(dir);
fea7838e 71 assert(dfd >= 0);
ffbd2c4d
NC
72
73 FOREACH_DIRENT(entry, dir, return 0) {
a4783bd1 74 int fd;
ffbd2c4d 75 _cleanup_fclose_ FILE *policy = NULL;
ffbd2c4d 76
a4783bd1
ZJS
77 fd = openat(dfd, entry->d_name, O_RDONLY|O_CLOEXEC);
78 if (fd < 0) {
79 if (r == 0)
80 r = -errno;
81 log_warning("Failed to open %s: %m", entry->d_name);
ffbd2c4d
NC
82 continue;
83 }
84
a4783bd1 85 policy = fdopen(fd, "re");
ffbd2c4d 86 if (!policy) {
a4783bd1
ZJS
87 if (r == 0)
88 r = -errno;
89 close_nointr_nofail(fd);
90 log_error("Failed to open %s: %m", entry->d_name);
ffbd2c4d
NC
91 continue;
92 }
93
ffbd2c4d 94 /* load2 write rules in the kernel require a line buffered stream */
fea7838e 95 FOREACH_LINE(buf, policy,
a4783bd1 96 log_error("Failed to read line from %s: %m",
fea7838e 97 entry->d_name)) {
a4783bd1
ZJS
98 if (!fputs(buf, dst)) {
99 if (r == 0)
100 r = -EINVAL;
101 log_error("Failed to write line to %s", dstpath);
102 break;
103 }
104 if (fflush(dst)) {
105 if (r == 0)
106 r = -errno;
107 log_error("Failed to flush writes to %s: %m", dstpath);
108 break;
109 }
ffbd2c4d
NC
110 }
111 }
112
a4783bd1
ZJS
113 return r;
114}
115
2b3e18de 116#endif
ffbd2c4d 117
a4783bd1 118int smack_setup(void) {
2b3e18de
KL
119
120#ifdef HAVE_SMACK
121
a4783bd1
ZJS
122 int r;
123
124 r = write_rules("/sys/fs/smackfs/load2", SMACK_CONFIG);
125 switch(r) {
126 case -ENOENT:
127 log_debug("Smack is not enabled in the kernel.");
128 return 0;
129 case ENOENT:
130 log_debug("Smack access rules directory " SMACK_CONFIG " not found");
131 return 0;
132 case 0:
133 log_info("Successfully loaded Smack policies.");
abbacb1d
NC
134 break;
135 default:
136 log_warning("Failed to load Smack access rules: %s, ignoring.",
137 strerror(abs(r)));
138 return 0;
139 }
140
141 r = write_rules("/sys/fs/smackfs/cipso2", CIPSO_CONFIG);
142 switch(r) {
143 case -ENOENT:
144 log_debug("Smack/CIPSO is not enabled in the kernel.");
145 return 0;
146 case ENOENT:
147 log_debug("Smack/CIPSO access rules directory " CIPSO_CONFIG " not found");
148 return 0;
149 case 0:
150 log_info("Successfully loaded Smack/CIPSO policies.");
a4783bd1
ZJS
151 return 0;
152 default:
abbacb1d 153 log_warning("Failed to load Smack/CIPSO access rules: %s, ignoring.",
a4783bd1
ZJS
154 strerror(abs(r)));
155 return 0;
156 }
2b3e18de
KL
157
158#endif
159
160 return 0;
ffbd2c4d 161}