]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/basic/smack-util.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 This file is part of systemd.
5 Copyright 2013 Intel Corporation
7 Author: Auke Kok <auke-jan.h.kok@intel.com>
9 systemd is free software; you can redistribute it and/or modify it
10 under the terms of the GNU Lesser General Public License as published by
11 the Free Software Foundation; either version 2.1 of the License, or
12 (at your option) any later version.
14 systemd is distributed in the hope that it will be useful, but
15 WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 Lesser General Public License for more details.
19 You should have received a copy of the GNU Lesser General Public License
20 along with systemd; If not, see <http://www.gnu.org/licenses/>.
26 #include <sys/xattr.h>
29 #include "alloc-util.h"
33 #include "path-util.h"
34 #include "process-util.h"
35 #include "smack-util.h"
36 #include "string-table.h"
37 #include "xattr-util.h"
40 bool mac_smack_use(void) {
41 static int cached_use
= -1;
44 cached_use
= access("/sys/fs/smackfs/", F_OK
) >= 0;
49 static const char* const smack_attr_table
[_SMACK_ATTR_MAX
] = {
50 [SMACK_ATTR_ACCESS
] = "security.SMACK64",
51 [SMACK_ATTR_EXEC
] = "security.SMACK64EXEC",
52 [SMACK_ATTR_MMAP
] = "security.SMACK64MMAP",
53 [SMACK_ATTR_TRANSMUTE
] = "security.SMACK64TRANSMUTE",
54 [SMACK_ATTR_IPIN
] = "security.SMACK64IPIN",
55 [SMACK_ATTR_IPOUT
] = "security.SMACK64IPOUT",
58 DEFINE_STRING_TABLE_LOOKUP(smack_attr
, SmackAttr
);
60 int mac_smack_read(const char *path
, SmackAttr attr
, char **label
) {
62 assert(attr
>= 0 && attr
< _SMACK_ATTR_MAX
);
68 return getxattr_malloc(path
, smack_attr_to_string(attr
), label
, true);
71 int mac_smack_read_fd(int fd
, SmackAttr attr
, char **label
) {
73 assert(attr
>= 0 && attr
< _SMACK_ATTR_MAX
);
79 return fgetxattr_malloc(fd
, smack_attr_to_string(attr
), label
);
82 int mac_smack_apply(const char *path
, SmackAttr attr
, const char *label
) {
86 assert(attr
>= 0 && attr
< _SMACK_ATTR_MAX
);
92 r
= lsetxattr(path
, smack_attr_to_string(attr
), label
, strlen(label
), 0);
94 r
= lremovexattr(path
, smack_attr_to_string(attr
));
101 int mac_smack_apply_fd(int fd
, SmackAttr attr
, const char *label
) {
105 assert(attr
>= 0 && attr
< _SMACK_ATTR_MAX
);
107 if (!mac_smack_use())
111 r
= fsetxattr(fd
, smack_attr_to_string(attr
), label
, strlen(label
), 0);
113 r
= fremovexattr(fd
, smack_attr_to_string(attr
));
120 int mac_smack_apply_pid(pid_t pid
, const char *label
) {
126 if (!mac_smack_use())
129 p
= procfs_file_alloca(pid
, "attr/current");
130 r
= write_string_file(p
, label
, 0);
137 int mac_smack_fix(const char *path
, bool ignore_enoent
, bool ignore_erofs
) {
143 if (!mac_smack_use())
147 * Path must be in /dev and must exist
149 if (!path_startswith(path
, "/dev"))
152 r
= lstat(path
, &st
);
157 * Label directories and character devices "*".
158 * Label symlinks "_".
159 * Don't change anything else.
162 if (S_ISDIR(st
.st_mode
))
163 label
= SMACK_STAR_LABEL
;
164 else if (S_ISLNK(st
.st_mode
))
165 label
= SMACK_FLOOR_LABEL
;
166 else if (S_ISCHR(st
.st_mode
))
167 label
= SMACK_STAR_LABEL
;
171 r
= lsetxattr(path
, "security.SMACK64", label
, strlen(label
), 0);
173 /* If the FS doesn't support labels, then exit without warning */
174 if (r
< 0 && errno
== EOPNOTSUPP
)
179 /* Ignore ENOENT in some cases */
180 if (ignore_enoent
&& errno
== ENOENT
)
183 if (ignore_erofs
&& errno
== EROFS
)
186 r
= log_debug_errno(errno
, "Unable to fix SMACK label of %s: %m", path
);
192 int mac_smack_copy(const char *dest
, const char *src
) {
194 _cleanup_free_
char *label
= NULL
;
199 r
= mac_smack_read(src
, SMACK_ATTR_ACCESS
, &label
);
203 r
= mac_smack_apply(dest
, SMACK_ATTR_ACCESS
, label
);
211 bool mac_smack_use(void) {
215 int mac_smack_read(const char *path
, SmackAttr attr
, char **label
) {
219 int mac_smack_read_fd(int fd
, SmackAttr attr
, char **label
) {
223 int mac_smack_apply(const char *path
, SmackAttr attr
, const char *label
) {
227 int mac_smack_apply_fd(int fd
, SmackAttr attr
, const char *label
) {
231 int mac_smack_apply_pid(pid_t pid
, const char *label
) {
235 int mac_smack_fix(const char *path
, bool ignore_enoent
, bool ignore_erofs
) {
239 int mac_smack_copy(const char *dest
, const char *src
) {