1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
|
/* SPDX-License-Identifier: LGPL-2.1+ */
#include <sys/xattr.h>
#include "alloc-util.h"
#include "chown-recursive.h"
#include "fileio.h"
#include "log.h"
#include "rm-rf.h"
#include "string-util.h"
#include "tests.h"
static const uint8_t acl[] = {
0x02, 0x00, 0x00, 0x00, 0x01, 0x00, 0x07, 0x00,
0xff, 0xff, 0xff, 0xff, 0x02, 0x00, 0x07, 0x00,
0x02, 0x00, 0x00, 0x00, 0x04, 0x00, 0x07, 0x00,
0xff, 0xff, 0xff, 0xff, 0x10, 0x00, 0x07, 0x00,
0xff, 0xff, 0xff, 0xff, 0x20, 0x00, 0x05, 0x00,
0xff, 0xff, 0xff, 0xff,
};
static const uint8_t default_acl[] = {
0x02, 0x00, 0x00, 0x00, 0x01, 0x00, 0x07, 0x00,
0xff, 0xff, 0xff, 0xff, 0x04, 0x00, 0x07, 0x00,
0xff, 0xff, 0xff, 0xff, 0x08, 0x00, 0x07, 0x00,
0x04, 0x00, 0x00, 0x00, 0x10, 0x00, 0x07, 0x00,
0xff, 0xff, 0xff, 0xff, 0x20, 0x00, 0x05, 0x00,
0xff, 0xff, 0xff, 0xff,
};
static bool has_xattr(const char *p) {
char buffer[sizeof(acl) * 4];
if (lgetxattr(p, "system.posix_acl_access", buffer, sizeof(buffer)) < 0) {
if (IN_SET(errno, EOPNOTSUPP, ENOTTY, ENODATA, ENOSYS))
return false;
}
return true;
}
static void test_chown_recursive(void) {
_cleanup_(rm_rf_physical_and_freep) char *t = NULL;
struct stat st;
const char *p;
umask(022);
assert_se(mkdtemp_malloc(NULL, &t) >= 0);
p = strjoina(t, "/dir");
assert_se(mkdir(p, 0777) >= 0);
assert_se(lstat(p, &st) >= 0);
assert_se(S_ISDIR(st.st_mode));
assert_se((st.st_mode & 07777) == 0755);
assert_se(st.st_uid == 0);
assert_se(st.st_gid == 0);
assert_se(!has_xattr(p));
p = strjoina(t, "/dir/symlink");
assert_se(symlink("../../", p) >= 0);
assert_se(lstat(p, &st) >= 0);
assert_se(S_ISLNK(st.st_mode));
assert_se((st.st_mode & 07777) == 0777);
assert_se(st.st_uid == 0);
assert_se(st.st_gid == 0);
assert_se(!has_xattr(p));
p = strjoina(t, "/dir/reg");
assert_se(mknod(p, S_IFREG|0777, 0) >= 0);
assert_se(lstat(p, &st) >= 0);
assert_se(S_ISREG(st.st_mode));
assert_se((st.st_mode & 07777) == 0755);
assert_se(st.st_uid == 0);
assert_se(st.st_gid == 0);
assert_se(!has_xattr(p));
p = strjoina(t, "/dir/sock");
assert_se(mknod(p, S_IFSOCK|0777, 0) >= 0);
assert_se(lstat(p, &st) >= 0);
assert_se(S_ISSOCK(st.st_mode));
assert_se((st.st_mode & 07777) == 0755);
assert_se(st.st_uid == 0);
assert_se(st.st_gid == 0);
assert_se(!has_xattr(p));
p = strjoina(t, "/dir/fifo");
assert_se(mknod(p, S_IFIFO|0777, 0) >= 0);
assert_se(lstat(p, &st) >= 0);
assert_se(S_ISFIFO(st.st_mode));
assert_se((st.st_mode & 07777) == 0755);
assert_se(st.st_uid == 0);
assert_se(st.st_gid == 0);
assert_se(!has_xattr(p));
/* We now apply an xattr to the dir, and check it again */
p = strjoina(t, "/dir");
assert_se(setxattr(p, "system.posix_acl_access", acl, sizeof(acl), 0) >= 0);
assert_se(setxattr(p, "system.posix_acl_default", default_acl, sizeof(default_acl), 0) >= 0);
assert_se(lstat(p, &st) >= 0);
assert_se(S_ISDIR(st.st_mode));
assert_se((st.st_mode & 07777) == 0775); /* acl change changed the mode too */
assert_se(st.st_uid == 0);
assert_se(st.st_gid == 0);
assert_se(has_xattr(p));
assert_se(path_chown_recursive(t, 1, 2) >= 0);
p = strjoina(t, "/dir");
assert_se(lstat(p, &st) >= 0);
assert_se(S_ISDIR(st.st_mode));
assert_se((st.st_mode & 07777) == 0775);
assert_se(st.st_uid == 1);
assert_se(st.st_gid == 2);
assert_se(!has_xattr(p));
p = strjoina(t, "/dir/symlink");
assert_se(lstat(p, &st) >= 0);
assert_se(S_ISLNK(st.st_mode));
assert_se((st.st_mode & 07777) == 0777);
assert_se(st.st_uid == 1);
assert_se(st.st_gid == 2);
assert_se(!has_xattr(p));
p = strjoina(t, "/dir/reg");
assert_se(lstat(p, &st) >= 0);
assert_se(S_ISREG(st.st_mode));
assert_se((st.st_mode & 07777) == 0755);
assert_se(st.st_uid == 1);
assert_se(st.st_gid == 2);
assert_se(!has_xattr(p));
p = strjoina(t, "/dir/sock");
assert_se(lstat(p, &st) >= 0);
assert_se(S_ISSOCK(st.st_mode));
assert_se((st.st_mode & 07777) == 0755);
assert_se(st.st_uid == 1);
assert_se(st.st_gid == 2);
assert_se(!has_xattr(p));
p = strjoina(t, "/dir/fifo");
assert_se(lstat(p, &st) >= 0);
assert_se(S_ISFIFO(st.st_mode));
assert_se((st.st_mode & 07777) == 0755);
assert_se(st.st_uid == 1);
assert_se(st.st_gid == 2);
assert_se(!has_xattr(p));
}
int main(int argc, char *argv[]) {
test_setup_logging(LOG_DEBUG);
if (geteuid() != 0)
return log_tests_skipped("not running as root");
test_chown_recursive();
return EXIT_SUCCESS;
}
|