]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/core/smack-setup.c
smack: relabel directories and files created by systemd
[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)
59 log_warning("Failed to open %s: %m", dstpath);
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
ZJS
66 if (errno != ENOENT)
67 log_warning("Failed to opendir %s: %m", srcdir);
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;
82 log_warning("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;
90 close_nointr_nofail(fd);
91 log_error("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,
a4783bd1 97 log_error("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;
108 log_error("Failed to flush writes to %s: %m", dstpath);
109 break;
110 }
ffbd2c4d
NC
111 }
112 }
113
a4783bd1
ZJS
114 return r;
115}
116
2b3e18de 117#endif
ffbd2c4d 118
a4783bd1 119int smack_setup(void) {
2b3e18de
KL
120
121#ifdef HAVE_SMACK
122
a4783bd1
ZJS
123 int r;
124
125 r = write_rules("/sys/fs/smackfs/load2", SMACK_CONFIG);
126 switch(r) {
127 case -ENOENT:
128 log_debug("Smack is not enabled in the kernel.");
129 return 0;
130 case ENOENT:
131 log_debug("Smack access rules directory " SMACK_CONFIG " not found");
132 return 0;
133 case 0:
134 log_info("Successfully loaded Smack policies.");
abbacb1d
NC
135 break;
136 default:
137 log_warning("Failed to load Smack access rules: %s, ignoring.",
138 strerror(abs(r)));
139 return 0;
140 }
141
8b197c3a
AK
142#ifdef SMACK_RUN_LABEL
143 r = write_string_file("/proc/self/attr/current", SMACK_RUN_LABEL);
144 if (r)
145 log_warning("Failed to set SMACK label \"%s\" on self: %s",
146 SMACK_RUN_LABEL, strerror(-r));
147#endif
148
abbacb1d
NC
149 r = write_rules("/sys/fs/smackfs/cipso2", CIPSO_CONFIG);
150 switch(r) {
151 case -ENOENT:
152 log_debug("Smack/CIPSO is not enabled in the kernel.");
153 return 0;
154 case ENOENT:
155 log_debug("Smack/CIPSO access rules directory " CIPSO_CONFIG " not found");
156 return 0;
157 case 0:
158 log_info("Successfully loaded Smack/CIPSO policies.");
a4783bd1
ZJS
159 return 0;
160 default:
abbacb1d 161 log_warning("Failed to load Smack/CIPSO access rules: %s, ignoring.",
a4783bd1
ZJS
162 strerror(abs(r)));
163 return 0;
164 }
2b3e18de
KL
165
166#endif
167
168 return 0;
ffbd2c4d 169}