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"
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 static const char* const smack_attr_table
[_SMACK_ATTR_MAX
] = {
38 [SMACK_ATTR_ACCESS
] = "security.SMACK64",
39 [SMACK_ATTR_EXEC
] = "security.SMACK64EXEC",
40 [SMACK_ATTR_MMAP
] = "security.SMACK64MMAP",
41 [SMACK_ATTR_TRANSMUTE
] = "security.SMACK64TRANSMUTE",
42 [SMACK_ATTR_IPIN
] = "security.SMACK64IPIN",
43 [SMACK_ATTR_IPOUT
] = "security.SMACK64IPOUT",
46 DEFINE_STRING_TABLE_LOOKUP(smack_attr
, SmackAttr
);
48 int mac_smack_read(const char *path
, SmackAttr attr
, char **label
) {
50 assert(attr
>= 0 && attr
< _SMACK_ATTR_MAX
);
56 return getxattr_malloc(path
, smack_attr_to_string(attr
), label
, true);
59 int mac_smack_read_fd(int fd
, SmackAttr attr
, char **label
) {
61 assert(attr
>= 0 && attr
< _SMACK_ATTR_MAX
);
67 return fgetxattr_malloc(fd
, smack_attr_to_string(attr
), label
);
70 int mac_smack_apply(const char *path
, SmackAttr attr
, const char *label
) {
74 assert(attr
>= 0 && attr
< _SMACK_ATTR_MAX
);
80 r
= lsetxattr(path
, smack_attr_to_string(attr
), label
, strlen(label
), 0);
82 r
= lremovexattr(path
, smack_attr_to_string(attr
));
89 int mac_smack_apply_fd(int fd
, SmackAttr attr
, const char *label
) {
93 assert(attr
>= 0 && attr
< _SMACK_ATTR_MAX
);
99 r
= fsetxattr(fd
, smack_attr_to_string(attr
), label
, strlen(label
), 0);
101 r
= fremovexattr(fd
, smack_attr_to_string(attr
));
108 int mac_smack_apply_pid(pid_t pid
, const char *label
) {
114 if (!mac_smack_use())
117 p
= procfs_file_alloca(pid
, "attr/current");
118 r
= write_string_file(p
, label
, WRITE_STRING_FILE_DISABLE_BUFFER
);
125 static int smack_fix_fd(int fd
, const char *abspath
, LabelFixFlags flags
) {
126 char procfs_path
[STRLEN("/proc/self/fd/") + DECIMAL_STR_MAX(int)];
131 /* The caller should have done the sanity checks. */
133 assert(path_is_absolute(abspath
));
135 /* Path must be in /dev. */
136 if (!path_startswith(abspath
, "/dev"))
139 if (fstat(fd
, &st
) < 0)
143 * Label directories and character devices "*".
144 * Label symlinks "_".
145 * Don't change anything else.
148 if (S_ISDIR(st
.st_mode
))
149 label
= SMACK_STAR_LABEL
;
150 else if (S_ISLNK(st
.st_mode
))
151 label
= SMACK_FLOOR_LABEL
;
152 else if (S_ISCHR(st
.st_mode
))
153 label
= SMACK_STAR_LABEL
;
157 xsprintf(procfs_path
, "/proc/self/fd/%i", fd
);
158 if (setxattr(procfs_path
, "security.SMACK64", label
, strlen(label
), 0) < 0) {
159 _cleanup_free_
char *old_label
= NULL
;
163 /* If the FS doesn't support labels, then exit without warning */
164 if (r
== -EOPNOTSUPP
)
167 /* It the FS is read-only and we were told to ignore failures caused by that, suppress error */
168 if (r
== -EROFS
&& (flags
& LABEL_IGNORE_EROFS
))
171 /* If the old label is identical to the new one, suppress any kind of error */
172 if (getxattr_malloc(procfs_path
, "security.SMACK64", &old_label
, false) >= 0 &&
173 streq(old_label
, label
))
176 return log_debug_errno(r
, "Unable to fix SMACK label of %s: %m", abspath
);
182 int mac_smack_fix_at(int dirfd
, const char *path
, LabelFixFlags flags
) {
183 _cleanup_free_
char *p
= NULL
;
184 _cleanup_close_
int fd
= -1;
189 if (!mac_smack_use())
192 fd
= openat(dirfd
, path
, O_NOFOLLOW
|O_CLOEXEC
|O_PATH
);
194 if ((flags
& LABEL_IGNORE_ENOENT
) && errno
== ENOENT
)
200 if (!path_is_absolute(path
)) {
201 r
= fd_get_path(fd
, &p
);
207 return smack_fix_fd(fd
, path
, flags
);
210 int mac_smack_fix(const char *path
, LabelFixFlags flags
) {
211 _cleanup_free_
char *abspath
= NULL
;
212 _cleanup_close_
int fd
= -1;
217 if (!mac_smack_use())
220 r
= path_make_absolute_cwd(path
, &abspath
);
224 fd
= open(abspath
, O_NOFOLLOW
|O_CLOEXEC
|O_PATH
);
226 if ((flags
& LABEL_IGNORE_ENOENT
) && errno
== ENOENT
)
232 return smack_fix_fd(fd
, abspath
, flags
);
235 int mac_smack_copy(const char *dest
, const char *src
) {
237 _cleanup_free_
char *label
= NULL
;
242 r
= mac_smack_read(src
, SMACK_ATTR_ACCESS
, &label
);
246 r
= mac_smack_apply(dest
, SMACK_ATTR_ACCESS
, label
);
254 bool mac_smack_use(void) {
258 int mac_smack_read(const char *path
, SmackAttr attr
, char **label
) {
262 int mac_smack_read_fd(int fd
, SmackAttr attr
, char **label
) {
266 int mac_smack_apply(const char *path
, SmackAttr attr
, const char *label
) {
270 int mac_smack_apply_fd(int fd
, SmackAttr attr
, const char *label
) {
274 int mac_smack_apply_pid(pid_t pid
, const char *label
) {
278 int mac_smack_fix(const char *path
, LabelFixFlags flags
) {
282 int mac_smack_fix_at(int dirfd
, const char *path
, LabelFixFlags flags
) {
286 int mac_smack_copy(const char *dest
, const char *src
) {