1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2 
3 #include <errno.h>
4 #include <sys/stat.h>
5 #include <unistd.h>
6 
7 #include "btrfs-util.h"
8 #include "fs-util.h"
9 #include "label.h"
10 #include "macro.h"
11 #include "selinux-util.h"
12 #include "smack-util.h"
13 
label_fix_container(const char * path,const char * inside_path,LabelFixFlags flags)14 int label_fix_container(const char *path, const char *inside_path, LabelFixFlags flags) {
15         int r, q;
16 
17         r = mac_selinux_fix_container(path, inside_path, flags);
18         q = mac_smack_fix_container(path, inside_path, flags);
19 
20         if (r < 0)
21                 return r;
22         if (q < 0)
23                 return q;
24 
25         return 0;
26 }
27 
symlink_label(const char * old_path,const char * new_path)28 int symlink_label(const char *old_path, const char *new_path) {
29         int r;
30 
31         assert(old_path);
32         assert(new_path);
33 
34         r = mac_selinux_create_file_prepare(new_path, S_IFLNK);
35         if (r < 0)
36                 return r;
37 
38         r = RET_NERRNO(symlink(old_path, new_path));
39         mac_selinux_create_file_clear();
40 
41         if (r < 0)
42                 return r;
43 
44         return mac_smack_fix(new_path, 0);
45 }
46 
symlink_atomic_label(const char * from,const char * to)47 int symlink_atomic_label(const char *from, const char *to) {
48         int r;
49 
50         assert(from);
51         assert(to);
52 
53         r = mac_selinux_create_file_prepare(to, S_IFLNK);
54         if (r < 0)
55                 return r;
56 
57         r = symlink_atomic(from, to);
58         mac_selinux_create_file_clear();
59 
60         if (r < 0)
61                 return r;
62 
63         return mac_smack_fix(to, 0);
64 }
65 
mknod_label(const char * pathname,mode_t mode,dev_t dev)66 int mknod_label(const char *pathname, mode_t mode, dev_t dev) {
67         int r;
68 
69         assert(pathname);
70 
71         r = mac_selinux_create_file_prepare(pathname, mode);
72         if (r < 0)
73                 return r;
74 
75         r = RET_NERRNO(mknod(pathname, mode, dev));
76         mac_selinux_create_file_clear();
77 
78         if (r < 0)
79                 return r;
80 
81         return mac_smack_fix(pathname, 0);
82 }
83 
btrfs_subvol_make_label(const char * path)84 int btrfs_subvol_make_label(const char *path) {
85         int r;
86 
87         assert(path);
88 
89         r = mac_selinux_create_file_prepare(path, S_IFDIR);
90         if (r < 0)
91                 return r;
92 
93         r = btrfs_subvol_make(path);
94         mac_selinux_create_file_clear();
95 
96         if (r < 0)
97                 return r;
98 
99         return mac_smack_fix(path, 0);
100 }
101