1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
4 This file is part of systemd.
6 Copyright 2013 Intel Corporation
8 Author: Auke Kok <auke-jan.h.kok@intel.com>
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 by
12 the Free Software Foundation; either version 2.1 of the License, or
13 (at your option) any later version.
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.
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/>.
24 #include <sys/xattr.h>
27 #include "process-util.h"
28 #include "path-util.h"
30 #include "smack-util.h"
32 #define SMACK_FLOOR_LABEL "_"
33 #define SMACK_STAR_LABEL "*"
35 bool mac_smack_use(void) {
37 static int cached_use = -1;
40 cached_use = access("/sys/fs/smackfs/", F_OK) >= 0;
48 int mac_smack_apply(const char *path, const char *label) {
58 r = lsetxattr(path, "security.SMACK64", label, strlen(label), 0);
60 r = lremovexattr(path, "security.SMACK64");
68 int mac_smack_apply_fd(int fd, const char *label) {
78 r = fsetxattr(fd, "security.SMACK64", label, strlen(label), 0);
80 r = fremovexattr(fd, "security.SMACK64");
88 int mac_smack_apply_ip_out_fd(int fd, const char *label) {
98 r = fsetxattr(fd, "security.SMACK64IPOUT", label, strlen(label), 0);
100 r = fremovexattr(fd, "security.SMACK64IPOUT");
108 int mac_smack_apply_ip_in_fd(int fd, const char *label) {
114 if (!mac_smack_use())
118 r = fsetxattr(fd, "security.SMACK64IPIN", label, strlen(label), 0);
120 r = fremovexattr(fd, "security.SMACK64IPIN");
128 int mac_smack_apply_pid(pid_t pid, const char *label) {
138 if (!mac_smack_use())
141 p = procfs_file_alloca(pid, "attr/current");
142 r = write_string_file(p, label);
150 int mac_smack_fix(const char *path, bool ignore_enoent, bool ignore_erofs) {
160 if (!mac_smack_use())
164 * Path must be in /dev and must exist
166 if (!path_startswith(path, "/dev"))
169 r = lstat(path, &st);
174 * Label directories and character devices "*".
175 * Label symlinks "_".
176 * Don't change anything else.
179 if (S_ISDIR(st.st_mode))
180 label = SMACK_STAR_LABEL;
181 else if (S_ISLNK(st.st_mode))
182 label = SMACK_FLOOR_LABEL;
183 else if (S_ISCHR(st.st_mode))
184 label = SMACK_STAR_LABEL;
188 r = lsetxattr(path, "security.SMACK64", label, strlen(label), 0);
190 /* If the FS doesn't support labels, then exit without warning */
191 if (r < 0 && errno == EOPNOTSUPP)
196 /* Ignore ENOENT in some cases */
197 if (ignore_enoent && errno == ENOENT)
200 if (ignore_erofs && errno == EROFS)
203 r = log_debug_errno(errno, "Unable to fix SMACK label of %s: %m", path);