Debian Codegraph @

src/test/test-chown-rec.c

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