1 | #include <dirent.h> |
2 | #include <fcntl.h> |
3 | #include <stdio.h> |
4 | #include <stdlib.h> |
5 | #include <string.h> |
6 | #include <unistd.h> |
7 | #include <sys/stat.h> |
8 | |
9 | |
10 | static void prepare (void); |
11 | #define PREPARE(argc, argv) prepare () |
12 | |
13 | static int do_test (void); |
14 | #define TEST_FUNCTION do_test () |
15 | |
16 | #include "../test-skeleton.c" |
17 | |
18 | static int dir_fd; |
19 | |
20 | static void |
21 | prepare (void) |
22 | { |
23 | #if _POSIX_CHOWN_RESTRICTED == 0 |
24 | if (pathconf (path: test_dir, _PC_CHOWN_RESTRICTED) != 0) |
25 | #endif |
26 | { |
27 | uid_t uid = getuid (); |
28 | if (uid != 0) |
29 | { |
30 | puts (s: "need root privileges" ); |
31 | exit (0); |
32 | } |
33 | } |
34 | |
35 | size_t test_dir_len = strlen (test_dir); |
36 | static const char dir_name[] = "/tst-fchownat.XXXXXX" ; |
37 | |
38 | size_t dirbuflen = test_dir_len + sizeof (dir_name); |
39 | char *dirbuf = malloc (size: dirbuflen); |
40 | if (dirbuf == NULL) |
41 | { |
42 | puts (s: "out of memory" ); |
43 | exit (1); |
44 | } |
45 | |
46 | snprintf (s: dirbuf, maxlen: dirbuflen, format: "%s%s" , test_dir, dir_name); |
47 | if (mkdtemp (template: dirbuf) == NULL) |
48 | { |
49 | puts (s: "cannot create temporary directory" ); |
50 | exit (1); |
51 | } |
52 | |
53 | add_temp_file (name: dirbuf); |
54 | |
55 | dir_fd = open (file: dirbuf, O_RDONLY | O_DIRECTORY); |
56 | if (dir_fd == -1) |
57 | { |
58 | puts (s: "cannot open directory" ); |
59 | exit (1); |
60 | } |
61 | } |
62 | |
63 | |
64 | static int |
65 | do_test (void) |
66 | { |
67 | /* fdopendir takes over the descriptor, make a copy. */ |
68 | int dupfd = dup (fd: dir_fd); |
69 | if (dupfd == -1) |
70 | { |
71 | puts (s: "dup failed" ); |
72 | return 1; |
73 | } |
74 | if (lseek (fd: dupfd, offset: 0, SEEK_SET) != 0) |
75 | { |
76 | puts (s: "1st lseek failed" ); |
77 | return 1; |
78 | } |
79 | |
80 | /* The directory should be empty safe the . and .. files. */ |
81 | DIR *dir = fdopendir (fd: dupfd); |
82 | if (dir == NULL) |
83 | { |
84 | puts (s: "fdopendir failed" ); |
85 | return 1; |
86 | } |
87 | struct dirent64 *d; |
88 | while ((d = readdir64 (dirp: dir)) != NULL) |
89 | if (strcmp (d->d_name, "." ) != 0 && strcmp (d->d_name, ".." ) != 0) |
90 | { |
91 | printf (format: "temp directory contains file \"%s\"\n" , d->d_name); |
92 | return 1; |
93 | } |
94 | closedir (dirp: dir); |
95 | |
96 | /* Try to create a file. */ |
97 | int fd = openat (fd: dir_fd, file: "some-file" , O_CREAT|O_RDWR|O_EXCL, 0666); |
98 | if (fd == -1) |
99 | { |
100 | if (errno == ENOSYS) |
101 | { |
102 | puts (s: "*at functions not supported" ); |
103 | return 0; |
104 | } |
105 | |
106 | puts (s: "file creation failed" ); |
107 | return 1; |
108 | } |
109 | write (fd, "hello" , 5); |
110 | puts (s: "file created" ); |
111 | |
112 | struct stat64 st1; |
113 | if (fstat64 (fd: fd, buf: &st1) != 0) |
114 | { |
115 | puts (s: "fstat64 failed" ); |
116 | return 1; |
117 | } |
118 | |
119 | /* Before closing the file, try using this file descriptor to open |
120 | another file. This must fail. */ |
121 | if (fchownat (fd: fd, file: "some-file" , owner: 1, group: 1, flag: 0) != -1) |
122 | { |
123 | puts (s: "fchownat using descriptor for normal file worked" ); |
124 | return 1; |
125 | } |
126 | if (errno != ENOTDIR) |
127 | { |
128 | puts (s: "\ |
129 | error for fchownat using descriptor for normal file not ENOTDIR " ); |
130 | return 1; |
131 | } |
132 | |
133 | close (fd: fd); |
134 | |
135 | if (fchownat (fd: dir_fd, file: "some-file" , owner: st1.st_uid + 1, group: st1.st_gid + 1, flag: 0) != 0) |
136 | { |
137 | puts (s: "fchownat failed" ); |
138 | return 1; |
139 | } |
140 | |
141 | struct stat64 st2; |
142 | if (fstatat64 (fd: dir_fd, file: "some-file" , buf: &st2, flag: 0) != 0) |
143 | { |
144 | puts (s: "fstatat64 failed" ); |
145 | return 1; |
146 | } |
147 | |
148 | if (st1.st_uid + 1 != st2.st_uid || st1.st_gid + 1 != st2.st_gid) |
149 | { |
150 | puts (s: "owner change failed" ); |
151 | return 1; |
152 | } |
153 | |
154 | if (unlinkat (fd: dir_fd, name: "some-file" , flag: 0) != 0) |
155 | { |
156 | puts (s: "unlinkat failed" ); |
157 | return 1; |
158 | } |
159 | |
160 | /* Create a file descriptor which is closed again right away. */ |
161 | int dir_fd2 = dup (fd: dir_fd); |
162 | if (dir_fd2 == -1) |
163 | { |
164 | puts (s: "dup failed" ); |
165 | return 1; |
166 | } |
167 | close (fd: dir_fd2); |
168 | |
169 | if (fchownat (fd: dir_fd2, file: "some-file" , owner: 1, group: 1, flag: 0) != -1) |
170 | { |
171 | puts (s: "fchownat using closed descriptor worked" ); |
172 | return 1; |
173 | } |
174 | if (errno != EBADF) |
175 | { |
176 | puts (s: "error for fchownat using closed descriptor not EBADF " ); |
177 | return 1; |
178 | } |
179 | |
180 | close (fd: dir_fd); |
181 | |
182 | if (fchownat (fd: -1, file: "some-file" , owner: 1, group: 1, flag: 0) != -1) |
183 | { |
184 | puts (s: "fchownat using invalid descriptor worked" ); |
185 | return 1; |
186 | } |
187 | if (errno != EBADF) |
188 | { |
189 | puts (s: "error for fchownat using invalid descriptor not EBADF " ); |
190 | return 1; |
191 | } |
192 | |
193 | return 0; |
194 | } |
195 | |