Debian Codegraph @

src/shared/label-util.c

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <sys/stat.h>
4 #include <unistd.h>
5
6 #include "btrfs-util.h"
7 #include "errno-util.h"
8 #include "label-util.h"
9 #include "selinux-util.h"
10 #include "smack-util.h"
11
12 int label_fix_full(
13 int atfd,
14 const char *inode_path, /* path of inode to apply label to */
15 const char *label_path, /* path to use as database lookup key in label database (typically same as inode_path, but not always) */
16 LabelFixFlags flags) {
17
18 int r, q;
19
20 if (atfd < 0 && atfd != AT_FDCWD)
21 return -EBADF;
22
23 if (!inode_path && atfd < 0) /* We need at least one of atfd and an inode path */
24 return -EINVAL;
25
26 /* If both atfd and inode_path are specified, we take the specified path relative to atfd which must be an fd to a dir.
27 *
28 * If only atfd is specified (and inode_path is NULL), we'll operated on the inode the atfd refers to.
29 *
30 * If atfd is AT_FDCWD then we'll operate on the inode the path refers to.
31 */
32
33 r = mac_selinux_fix_full(atfd, inode_path, label_path, flags);
34 q = mac_smack_fix_full(atfd, inode_path, label_path, flags);
35 if (r < 0)
36 return r;
37 if (q < 0)
38 return q;
39
40 return 0;
41 }
42
43 int symlink_label(const char *old_path, const char *new_path) {
44 int r;
45
46 assert(old_path);
47 assert(new_path);
48
49 r = mac_selinux_create_file_prepare(new_path, S_IFLNK);
50 if (r < 0)
51 return r;
52
53 r = RET_NERRNO(symlink(old_path, new_path));
54 mac_selinux_create_file_clear();
55
56 if (r < 0)
57 return r;
58
59 return mac_smack_fix(new_path, 0);
60 }
61
62 int mknodat_label(int dirfd, const char *pathname, mode_t mode, dev_t dev) {
63 int r;
64
65 assert(dirfd >= 0 || dirfd == AT_FDCWD);
66 assert(pathname);
67
68 r = mac_selinux_create_file_prepare_at(dirfd, pathname, mode);
69 if (r < 0)
70 return r;
71
72 r = RET_NERRNO(mknodat(dirfd, pathname, mode, dev));
73 mac_selinux_create_file_clear();
74
75 if (r < 0)
76 return r;
77
78 return mac_smack_fix_full(dirfd, pathname, NULL, 0);
79 }
80
81 int btrfs_subvol_make_label(const char *path) {
82 int r;
83
84 assert(path);
85
86 r = mac_selinux_create_file_prepare(path, S_IFDIR);
87 if (r < 0)
88 return r;
89
90 r = btrfs_subvol_make(AT_FDCWD, path);
91 mac_selinux_create_file_clear();
92
93 if (r < 0)
94 return r;
95
96 return mac_smack_fix(path, 0);
97 }
98
99 static int init_internal(bool lazy) {
100 int r;
101
102 assert(!(mac_selinux_use() && mac_smack_use()));
103
104 if (lazy)
105 r = mac_selinux_init_lazy();
106 else
107 r = mac_selinux_init();
108 if (r < 0)
109 return r;
110
111 return mac_smack_init();
112 }
113
114 int mac_init_lazy(void) {
115 return init_internal(/* lazy= */ true);
116 }
117
118 int mac_init(void) {
119 return init_internal(/* lazy= */ false);
120 }