1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 Copyright © 2013 Intel Corporation
5 Author: Auke Kok <auke-jan.h.kok@intel.com>
12 #include <sys/xattr.h>
15 #include "alloc-util.h"
16 //#include "fd-util.h"
20 #include "path-util.h"
21 #include "process-util.h"
22 #include "smack-util.h"
23 //#include "stdio-util.h"
24 #include "string-table.h"
25 #include "xattr-util.h"
28 bool mac_smack_use(void) {
29 static int cached_use = -1;
32 cached_use = access("/sys/fs/smackfs/", F_OK) >= 0;
37 #if 0 /// UNNEEDED by elogind
38 static const char* const smack_attr_table[_SMACK_ATTR_MAX] = {
39 [SMACK_ATTR_ACCESS] = "security.SMACK64",
40 [SMACK_ATTR_EXEC] = "security.SMACK64EXEC",
41 [SMACK_ATTR_MMAP] = "security.SMACK64MMAP",
42 [SMACK_ATTR_TRANSMUTE] = "security.SMACK64TRANSMUTE",
43 [SMACK_ATTR_IPIN] = "security.SMACK64IPIN",
44 [SMACK_ATTR_IPOUT] = "security.SMACK64IPOUT",
47 DEFINE_STRING_TABLE_LOOKUP(smack_attr, SmackAttr);
49 int mac_smack_read(const char *path, SmackAttr attr, char **label) {
51 assert(attr >= 0 && attr < _SMACK_ATTR_MAX);
57 return getxattr_malloc(path, smack_attr_to_string(attr), label, true);
60 int mac_smack_read_fd(int fd, SmackAttr attr, char **label) {
62 assert(attr >= 0 && attr < _SMACK_ATTR_MAX);
68 return fgetxattr_malloc(fd, smack_attr_to_string(attr), label);
71 int mac_smack_apply(const char *path, SmackAttr attr, const char *label) {
75 assert(attr >= 0 && attr < _SMACK_ATTR_MAX);
81 r = lsetxattr(path, smack_attr_to_string(attr), label, strlen(label), 0);
83 r = lremovexattr(path, smack_attr_to_string(attr));
90 int mac_smack_apply_fd(int fd, SmackAttr attr, const char *label) {
94 assert(attr >= 0 && attr < _SMACK_ATTR_MAX);
100 r = fsetxattr(fd, smack_attr_to_string(attr), label, strlen(label), 0);
102 r = fremovexattr(fd, smack_attr_to_string(attr));
109 int mac_smack_apply_pid(pid_t pid, const char *label) {
115 if (!mac_smack_use())
118 p = procfs_file_alloca(pid, "attr/current");
119 r = write_string_file(p, label, 0);
127 int mac_smack_fix(const char *path, LabelFixFlags flags) {
128 char procfs_path[STRLEN("/proc/self/fd/") + DECIMAL_STR_MAX(int)];
129 _cleanup_close_ int fd = -1;
136 if (!mac_smack_use())
139 /* Path must be in /dev. Note that this check is pretty sloppy, as we might be called with non-normalized paths
140 * and hence not detect all cases of /dev. */
142 if (path_is_absolute(path)) {
143 if (!path_startswith(path, "/dev"))
146 _cleanup_free_ char *cwd = NULL;
148 r = safe_getcwd(&cwd);
152 if (!path_startswith(cwd, "/dev"))
156 fd = open(path, O_NOFOLLOW|O_CLOEXEC|O_PATH);
158 if ((flags & LABEL_IGNORE_ENOENT) && errno == ENOENT)
164 if (fstat(fd, &st) < 0)
168 * Label directories and character devices "*".
169 * Label symlinks "_".
170 * Don't change anything else.
173 if (S_ISDIR(st.st_mode))
174 label = SMACK_STAR_LABEL;
175 else if (S_ISLNK(st.st_mode))
176 label = SMACK_FLOOR_LABEL;
177 else if (S_ISCHR(st.st_mode))
178 label = SMACK_STAR_LABEL;
182 xsprintf(procfs_path, "/proc/self/fd/%i", fd);
183 if (setxattr(procfs_path, "security.SMACK64", label, strlen(label), 0) < 0) {
184 _cleanup_free_ char *old_label = NULL;
188 /* If the FS doesn't support labels, then exit without warning */
189 if (r == -EOPNOTSUPP)
192 /* It the FS is read-only and we were told to ignore failures caused by that, suppress error */
193 if (r == -EROFS && (flags & LABEL_IGNORE_EROFS))
196 /* If the old label is identical to the new one, suppress any kind of error */
197 if (getxattr_malloc(procfs_path, "security.SMACK64", &old_label, false) >= 0 &&
198 streq(old_label, label))
201 return log_debug_errno(r, "Unable to fix SMACK label of %s: %m", path);
207 #if 0 /// UNNEEDED by elogind
208 int mac_smack_copy(const char *dest, const char *src) {
210 _cleanup_free_ char *label = NULL;
215 r = mac_smack_read(src, SMACK_ATTR_ACCESS, &label);
219 r = mac_smack_apply(dest, SMACK_ATTR_ACCESS, label);
228 bool mac_smack_use(void) {
232 #if 0 /// UNNEEDED by elogind
233 int mac_smack_read(const char *path, SmackAttr attr, char **label) {
237 int mac_smack_read_fd(int fd, SmackAttr attr, char **label) {
241 int mac_smack_apply(const char *path, SmackAttr attr, const char *label) {
245 int mac_smack_apply_fd(int fd, SmackAttr attr, const char *label) {
249 int mac_smack_apply_pid(pid_t pid, const char *label) {
254 int mac_smack_fix(const char *path, LabelFixFlags flags) {
258 #if 0 /// UNNEEDED by elogind
259 int mac_smack_copy(const char *dest, const char *src) {