1 /* Tests for lchmod and fchmodat with AT_SYMLINK_NOFOLLOW.
2 Copyright (C) 2020-2023 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <https://www.gnu.org/licenses/>. */
19 #include <array_length.h>
26 #include <support/check.h>
27 #include <support/descriptors.h>
28 #include <support/namespace.h>
29 #include <support/support.h>
30 #include <support/temp_file.h>
31 #include <support/xunistd.h>
34 #if __has_include (<sys/mount.h>)
35 # include <sys/mount.h>
38 /* Array of file descriptors. */
39 #define DYNARRAY_STRUCT fd_list
40 #define DYNARRAY_ELEMENT int
41 #define DYNARRAY_INITIAL_SIZE 0
42 #define DYNARRAY_PREFIX fd_list_
43 #include <malloc/dynarray-skeleton.c>
46 fchmodat_with_lchmod (int fd
, const char *path
, mode_t mode
, int flags
)
48 TEST_COMPARE (fd
, AT_FDCWD
);
50 return chmod (path
, mode
);
53 TEST_COMPARE (flags
, AT_SYMLINK_NOFOLLOW
);
54 return lchmod (path
, mode
);
58 /* Chose the appropriate path to pass as the path argument to the *at
61 select_path (bool do_relative_path
, const char *full_path
, const char *relative_path
)
70 update_file_time_to_y2038 (const char *fname
, int flags
)
73 /* Y2038 threshold plus 1 second. */
74 const struct timespec ts
[] = { { 0x80000001LL
, 0}, { 0x80000001LL
} };
75 TEST_VERIFY_EXIT (utimensat (AT_FDCWD
, fname
, ts
, flags
) == 0);
80 test_1 (bool do_relative_path
, int (*chmod_func
) (int fd
, const char *, mode_t
, int))
82 char *tempdir
= support_create_temp_directory ("tst-lchmod-");
84 if (!support_path_support_time64 (tempdir
))
86 puts ("info: test skipped, filesystem does not support 64 bit time_t");
91 char *path_dangling
= xasprintf ("%s/dangling", tempdir
);
92 char *path_file
= xasprintf ("%s/file", tempdir
);
93 char *path_loop
= xasprintf ("%s/loop", tempdir
);
94 char *path_missing
= xasprintf ("%s/missing", tempdir
);
95 char *path_to_file
= xasprintf ("%s/to-file", tempdir
);
99 fd
= xopen (tempdir
, O_DIRECTORY
| O_RDONLY
, 0);
103 add_temp_file (path_dangling
);
104 add_temp_file (path_loop
);
105 add_temp_file (path_file
);
106 add_temp_file (path_to_file
);
108 support_write_file_string (path_file
, "");
109 xsymlink ("file", path_to_file
);
110 xsymlink ("loop", path_loop
);
111 xsymlink ("target-does-not-exist", path_dangling
);
113 update_file_time_to_y2038 (path_file
, 0);
114 update_file_time_to_y2038 (path_to_file
, AT_SYMLINK_NOFOLLOW
);
116 /* Check that the modes do not collide with what we will use in the
119 xstat (path_file
, &st
);
120 TEST_VERIFY ((st
.st_mode
& 0777) != 1);
121 xlstat (path_to_file
, &st
);
122 TEST_VERIFY ((st
.st_mode
& 0777) != 2);
123 mode_t original_symlink_mode
= st
.st_mode
;
125 /* We should be able to change the mode of a file, including through
126 the symbolic link to-file. */
127 const char *arg
= select_path (do_relative_path
, path_file
, "file");
128 TEST_COMPARE (chmod_func (fd
, arg
, 1, 0), 0);
129 xstat (path_file
, &st
);
130 TEST_COMPARE (st
.st_mode
& 0777, 1);
131 arg
= select_path (do_relative_path
, path_to_file
, "to-file");
132 TEST_COMPARE (chmod_func (fd
, arg
, 2, 0), 0);
133 xstat (path_file
, &st
);
134 TEST_COMPARE (st
.st_mode
& 0777, 2);
135 xlstat (path_to_file
, &st
);
136 TEST_COMPARE (original_symlink_mode
, st
.st_mode
);
137 arg
= select_path (do_relative_path
, path_file
, "file");
138 TEST_COMPARE (chmod_func (fd
, arg
, 1, 0), 0);
139 xstat (path_file
, &st
);
140 TEST_COMPARE (st
.st_mode
& 0777, 1);
141 xlstat (path_to_file
, &st
);
142 TEST_COMPARE (original_symlink_mode
, st
.st_mode
);
144 /* Changing the mode of a symbolic link should fail. */
145 arg
= select_path (do_relative_path
, path_to_file
, "to-file");
146 int ret
= chmod_func (fd
, arg
, 2, AT_SYMLINK_NOFOLLOW
);
147 TEST_COMPARE (ret
, -1);
148 TEST_COMPARE (errno
, EOPNOTSUPP
);
150 /* The modes should remain unchanged. */
151 xstat (path_file
, &st
);
152 TEST_COMPARE (st
.st_mode
& 0777, 1);
153 xlstat (path_to_file
, &st
);
154 TEST_COMPARE (original_symlink_mode
, st
.st_mode
);
156 /* Likewise, changing dangling and looping symbolic links must
158 const char *paths
[] = { path_dangling
, path_loop
};
159 for (size_t i
= 0; i
< array_length (paths
); ++i
)
161 const char *path
= paths
[i
];
162 const char *filename
= strrchr (path
, '/');
163 TEST_VERIFY_EXIT (filename
!= NULL
);
165 mode_t new_mode
= 010 + i
;
168 TEST_VERIFY ((st
.st_mode
& 0777) != new_mode
);
169 original_symlink_mode
= st
.st_mode
;
170 arg
= select_path (do_relative_path
, path
, filename
);
171 ret
= chmod_func (fd
, arg
, new_mode
, AT_SYMLINK_NOFOLLOW
);
172 TEST_COMPARE (ret
, -1);
173 TEST_COMPARE (errno
, EOPNOTSUPP
);
175 TEST_COMPARE (st
.st_mode
, original_symlink_mode
);
178 /* A missing file should always result in ENOENT. The presence of
179 /proc does not matter. */
180 arg
= select_path (do_relative_path
, path_missing
, "missing");
181 TEST_COMPARE (chmod_func (fd
, arg
, 020, 0), -1);
182 TEST_COMPARE (errno
, ENOENT
);
183 TEST_COMPARE (chmod_func (fd
, arg
, 020, AT_SYMLINK_NOFOLLOW
), -1);
184 TEST_COMPARE (errno
, ENOENT
);
186 /* Test without available file descriptors. */
188 struct fd_list fd_list
;
189 fd_list_init (&fd_list
);
192 int ret
= dup (STDOUT_FILENO
);
195 if (errno
== ENFILE
|| errno
== EMFILE
)
197 FAIL_EXIT1 ("dup: %m");
199 fd_list_add (&fd_list
, ret
);
200 TEST_VERIFY_EXIT (!fd_list_has_failed (&fd_list
));
202 /* Without AT_SYMLINK_NOFOLLOW, changing the permissions should
204 arg
= select_path (do_relative_path
, path_file
, "file");
205 TEST_COMPARE (chmod_func (fd
, arg
, 3, 0), 0);
206 xstat (path_file
, &st
);
207 TEST_COMPARE (st
.st_mode
& 0777, 3);
208 /* But with AT_SYMLINK_NOFOLLOW, even if we originally had
209 support, we may have lost it. */
210 ret
= chmod_func (fd
, arg
, 2, AT_SYMLINK_NOFOLLOW
);
213 xstat (path_file
, &st
);
214 TEST_COMPARE (st
.st_mode
& 0777, 2);
218 TEST_COMPARE (ret
, -1);
219 /* The error code from the openat fallback leaks out. */
220 if (errno
!= ENFILE
&& errno
!= EMFILE
)
221 TEST_COMPARE (errno
, EOPNOTSUPP
);
223 xstat (path_file
, &st
);
224 TEST_COMPARE (st
.st_mode
& 0777, 3);
226 /* Close the descriptors. */
227 for (int *pfd
= fd_list_begin (&fd_list
); pfd
< fd_list_end (&fd_list
);
230 fd_list_free (&fd_list
);
233 if (do_relative_path
)
236 free (path_dangling
);
248 puts ("info: testing lchmod");
249 test_1 (false, fchmodat_with_lchmod
);
250 puts ("info: testing fchmodat with AT_FDCWD");
251 test_1 (false, fchmodat
);
252 puts ("info: testing fchmodat with relative path");
253 test_1 (true, fchmodat
);
259 struct support_descriptors
*descriptors
= support_descriptors_list ();
261 /* Run the three tests in the default environment. */
264 /* Try to set up a /proc-less environment and re-test. */
265 #if __has_include (<sys/mount.h>)
266 if (!support_become_root ())
267 puts ("warning: could not obtain root-like privileges");
268 if (!support_enter_mount_namespace ())
269 puts ("warning: could enter a mount namespace");
272 /* Attempt to mount an empty directory over /proc. */
273 char *tempdir
= support_create_temp_directory ("tst-lchmod-");
275 = mount (tempdir
, "/proc", "none", MS_BIND
, NULL
) == 0;
277 printf ("warning: bind-mounting /proc failed: %m");
280 puts ("info: re-running tests (after trying to empty /proc)");
284 /* Reveal the original /proc, which is needed by the
285 descriptors check below. */
286 TEST_COMPARE (umount ("/proc"), 0);
288 #endif /* <sys/mount.h>. */
290 support_descriptors_check (descriptors
);
291 support_descriptors_free (descriptors
);
296 #include <support/test-driver.c>