]> git.proxmox.com Git - systemd.git/blame - src/core/smack-setup.c
Imported Upstream version 217
[systemd.git] / src / core / smack-setup.c
CommitLineData
663996b3
MS
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"
60f067b4 39#include "fileio.h"
663996b3
MS
40#include "log.h"
41#include "label.h"
42
43#define SMACK_CONFIG "/etc/smack/accesses.d/"
14228c0d
MB
44#define CIPSO_CONFIG "/etc/smack/cipso.d/"
45
46#ifdef HAVE_SMACK
663996b3
MS
47
48static int write_rules(const char* dstpath, const char* srcdir) {
49 _cleanup_fclose_ FILE *dst = NULL;
50 _cleanup_closedir_ DIR *dir = NULL;
51 struct dirent *entry;
52 char buf[NAME_MAX];
53 int dfd = -1;
54 int r = 0;
55
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 */
61 }
62
63 /* write rules to dst from every file in the directory */
64 dir = opendir(srcdir);
65 if (!dir) {
66 if (errno != ENOENT)
67 log_warning("Failed to opendir %s: %m", srcdir);
68 return errno; /* positive on purpose */
69 }
70
71 dfd = dirfd(dir);
72 assert(dfd >= 0);
73
74 FOREACH_DIRENT(entry, dir, return 0) {
75 int fd;
76 _cleanup_fclose_ FILE *policy = NULL;
77
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);
83 continue;
84 }
85
86 policy = fdopen(fd, "re");
87 if (!policy) {
88 if (r == 0)
89 r = -errno;
60f067b4 90 safe_close(fd);
663996b3
MS
91 log_error("Failed to open %s: %m", entry->d_name);
92 continue;
93 }
94
95 /* load2 write rules in the kernel require a line buffered stream */
96 FOREACH_LINE(buf, policy,
97 log_error("Failed to read line from %s: %m",
98 entry->d_name)) {
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 }
111 }
112 }
113
114 return r;
115}
116
14228c0d 117#endif
663996b3 118
5eef597e 119int mac_smack_setup(bool *loaded_policy) {
14228c0d
MB
120
121#ifdef HAVE_SMACK
122
663996b3
MS
123 int r;
124
60f067b4
JS
125 assert(loaded_policy);
126
663996b3
MS
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.");
137 break;
138 default:
139 log_warning("Failed to load Smack access rules: %s, ignoring.",
140 strerror(abs(r)));
141 return 0;
142 }
143
60f067b4
JS
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
663996b3
MS
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.");
5eef597e 161 break;
663996b3
MS
162 default:
163 log_warning("Failed to load Smack/CIPSO access rules: %s, ignoring.",
164 strerror(abs(r)));
165 return 0;
166 }
14228c0d 167
60f067b4
JS
168 *loaded_policy = true;
169
14228c0d
MB
170#endif
171
172 return 0;
663996b3 173}