lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 1 | /* Test for chmod functions. |
| 2 | Copyright (C) 2000-2015 Free Software Foundation, Inc. |
| 3 | This file is part of the GNU C Library. |
| 4 | Contributed by Ulrich Drepper <drepper@cygnus.com>, 2000. |
| 5 | |
| 6 | The GNU C Library is free software; you can redistribute it and/or |
| 7 | modify it under the terms of the GNU Lesser General Public |
| 8 | License as published by the Free Software Foundation; either |
| 9 | version 2.1 of the License, or (at your option) any later version. |
| 10 | |
| 11 | The GNU C Library is distributed in the hope that it will be useful, |
| 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 | Lesser General Public License for more details. |
| 15 | |
| 16 | You should have received a copy of the GNU Lesser General Public |
| 17 | License along with the GNU C Library; if not, see |
| 18 | <http://www.gnu.org/licenses/>. */ |
| 19 | |
| 20 | #include <dirent.h> |
| 21 | #include <errno.h> |
| 22 | #include <error.h> |
| 23 | #include <fcntl.h> |
| 24 | #include <mcheck.h> |
| 25 | #include <stdio.h> |
| 26 | #include <stdlib.h> |
| 27 | #include <string.h> |
| 28 | #include <unistd.h> |
| 29 | #include <sys/stat.h> |
| 30 | |
| 31 | |
| 32 | #define OUT_OF_MEMORY \ |
| 33 | do { \ |
| 34 | puts ("cannot allocate memory"); \ |
| 35 | result = 1; \ |
| 36 | goto fail; \ |
| 37 | } while (0) |
| 38 | |
| 39 | static int |
| 40 | do_test (int argc, char *argv[]) |
| 41 | { |
| 42 | const char *builddir; |
| 43 | struct stat64 st1; |
| 44 | struct stat64 st2; |
| 45 | char *buf; |
| 46 | char *testdir; |
| 47 | char *testfile = NULL; |
| 48 | char *startdir; |
| 49 | size_t buflen; |
| 50 | int fd; |
| 51 | int result = 0; |
| 52 | DIR *dir; |
| 53 | |
| 54 | mtrace (); |
| 55 | |
| 56 | if (argc <= 1) |
| 57 | error (EXIT_FAILURE, 0, "no parameters"); |
| 58 | |
| 59 | /* This is where we will create the test files. */ |
| 60 | builddir = argv[1]; |
| 61 | buflen = strlen (builddir) + 50; |
| 62 | |
| 63 | startdir = getcwd (NULL, 0); |
| 64 | if (startdir == NULL) |
| 65 | { |
| 66 | printf ("cannot get current directory: %m\n"); |
| 67 | exit (EXIT_FAILURE); |
| 68 | } |
| 69 | |
| 70 | /* A buffer large enough for everything we need. */ |
| 71 | buf = (char *) alloca (buflen); |
| 72 | |
| 73 | /* Create the directory name. */ |
| 74 | snprintf (buf, buflen, "%s/chmoddirXXXXXX", builddir); |
| 75 | |
| 76 | if (mkdtemp (buf) == NULL) |
| 77 | { |
| 78 | printf ("cannot create test directory: %m\n"); |
| 79 | exit (EXIT_FAILURE); |
| 80 | } |
| 81 | |
| 82 | if (chmod ("", 0600) == 0) |
| 83 | { |
| 84 | puts ("chmod(\"\", 0600 didn't fail"); |
| 85 | result = 1; |
| 86 | } |
| 87 | else if (errno != ENOENT) |
| 88 | { |
| 89 | puts ("chmod(\"\",0600) does not set errno to ENOENT"); |
| 90 | result = 1; |
| 91 | } |
| 92 | |
| 93 | /* Create a duplicate. */ |
| 94 | testdir = strdup (buf); |
| 95 | if (testdir == NULL) |
| 96 | OUT_OF_MEMORY; |
| 97 | |
| 98 | if (stat64 (testdir, &st1) != 0) |
| 99 | { |
| 100 | printf ("cannot stat test directory: %m\n"); |
| 101 | exit (1); |
| 102 | } |
| 103 | if (!S_ISDIR (st1.st_mode)) |
| 104 | { |
| 105 | printf ("file not created as directory: %m\n"); |
| 106 | exit (1); |
| 107 | } |
| 108 | |
| 109 | /* We have to wait for a second to make sure the ctime changes. */ |
| 110 | sleep (1); |
| 111 | |
| 112 | /* Remove all access rights from the directory. */ |
| 113 | if (chmod (testdir, 0) != 0) |
| 114 | { |
| 115 | printf ("cannot change mode of test directory: %m\n"); |
| 116 | result = 1; |
| 117 | goto fail; |
| 118 | } |
| 119 | |
| 120 | if (stat64 (testdir, &st2) != 0) |
| 121 | { |
| 122 | printf ("cannot stat test directory: %m\n"); |
| 123 | result = 1; |
| 124 | goto fail; |
| 125 | } |
| 126 | |
| 127 | /* Compare result. */ |
| 128 | if ((st2.st_mode & ALLPERMS) != 0) |
| 129 | { |
| 130 | printf ("chmod(...,0) on directory left bits nonzero: %o\n", |
| 131 | st2.st_mode & ALLPERMS); |
| 132 | result = 1; |
| 133 | } |
| 134 | if (st1.st_ctime >= st2.st_ctime) |
| 135 | { |
| 136 | puts ("chmod(...,0) did not set ctime correctly"); |
| 137 | result = 1; |
| 138 | } |
| 139 | |
| 140 | /* Name of a file in the directory. */ |
| 141 | snprintf (buf, buflen, "%s/file", testdir); |
| 142 | testfile = strdup (buf); |
| 143 | if (testfile == NULL) |
| 144 | OUT_OF_MEMORY; |
| 145 | |
| 146 | fd = creat (testfile, 0); |
| 147 | if (fd != -1) |
| 148 | { |
| 149 | if (getuid () != 0) |
| 150 | { |
| 151 | puts ("managed to create test file in protected directory"); |
| 152 | result = 1; |
| 153 | } |
| 154 | close (fd); |
| 155 | } |
| 156 | else if (errno != EACCES) |
| 157 | { |
| 158 | puts ("creat didn't generate correct errno value"); |
| 159 | result = 1; |
| 160 | } |
| 161 | |
| 162 | /* With this mode it still shouldn't be possible to create a file. */ |
| 163 | if (chmod (testdir, 0600) != 0) |
| 164 | { |
| 165 | printf ("cannot change mode of test directory to 0600: %m\n"); |
| 166 | result = 1; |
| 167 | goto fail; |
| 168 | } |
| 169 | |
| 170 | fd = creat (testfile, 0); |
| 171 | if (fd != -1) |
| 172 | { |
| 173 | if (getuid () != 0) |
| 174 | { |
| 175 | puts ("managed to create test file in no-x protected directory"); |
| 176 | result = 1; |
| 177 | } |
| 178 | close (fd); |
| 179 | } |
| 180 | else if (errno != EACCES) |
| 181 | { |
| 182 | puts ("creat didn't generate correct errno value"); |
| 183 | result = 1; |
| 184 | } |
| 185 | |
| 186 | /* Change the directory mode back to allow creating a file. This |
| 187 | time with fchmod. */ |
| 188 | dir = opendir (testdir); |
| 189 | if (dir != NULL) |
| 190 | { |
| 191 | if (fchmod (dirfd (dir), 0700) != 0) |
| 192 | { |
| 193 | printf ("cannot change mode of test directory to 0700: %m\n"); |
| 194 | result = 1; |
| 195 | closedir (dir); |
| 196 | goto fail; |
| 197 | } |
| 198 | |
| 199 | closedir (dir); |
| 200 | } |
| 201 | else |
| 202 | { |
| 203 | printf ("cannot open directory: %m\n"); |
| 204 | result = 1; |
| 205 | |
| 206 | if (chmod (testdir, 0700) != 0) |
| 207 | { |
| 208 | printf ("cannot change mode of test directory to 0700: %m\n"); |
| 209 | goto fail; |
| 210 | } |
| 211 | } |
| 212 | |
| 213 | fd = creat (testfile, 0); |
| 214 | if (fd == -1) |
| 215 | { |
| 216 | puts ("still didn't manage to create test file in protected directory"); |
| 217 | result = 1; |
| 218 | goto fail; |
| 219 | } |
| 220 | if (fstat64 (fd, &st1) != 0) |
| 221 | { |
| 222 | printf ("cannot stat new file: %m\n"); |
| 223 | result = 1; |
| 224 | } |
| 225 | else if ((st1.st_mode & ALLPERMS) != 0) |
| 226 | { |
| 227 | puts ("file not created with access mode 0"); |
| 228 | result = 1; |
| 229 | } |
| 230 | close (fd); |
| 231 | |
| 232 | snprintf (buf, buflen, "%s/..", testdir); |
| 233 | chdir (buf); |
| 234 | /* We are now in the directory above the one we create the test |
| 235 | directory in. */ |
| 236 | |
| 237 | sleep (1); |
| 238 | snprintf (buf, buflen, "./%s/../%s/file", |
| 239 | basename (testdir), basename (testdir)); |
| 240 | if (chmod (buf, 0600) != 0) |
| 241 | { |
| 242 | printf ("cannot change mode of file to 0600: %m\n"); |
| 243 | result = 1; |
| 244 | goto fail; |
| 245 | } |
| 246 | snprintf (buf, buflen, "./%s//file", basename (testdir)); |
| 247 | if (stat64 (buf, &st2) != 0) |
| 248 | { |
| 249 | printf ("cannot stat new file: %m\n"); |
| 250 | result = 1; |
| 251 | } |
| 252 | else if ((st2.st_mode & ALLPERMS) != 0600) |
| 253 | { |
| 254 | puts ("file mode not changed to 0600"); |
| 255 | result = 1; |
| 256 | } |
| 257 | else if (st1.st_ctime >= st2.st_ctime) |
| 258 | { |
| 259 | puts ("chmod(\".../file\",0600) did not set ctime correctly"); |
| 260 | result = 1; |
| 261 | } |
| 262 | |
| 263 | if (chmod (buf, 0777 | S_ISUID | S_ISGID) != 0) |
| 264 | { |
| 265 | printf ("cannot change mode of file to %o: %m\n", |
| 266 | 0777 | S_ISUID | S_ISGID); |
| 267 | result = 1; |
| 268 | } |
| 269 | if (stat64 (buf, &st2) != 0) |
| 270 | { |
| 271 | printf ("cannot stat test file: %m\n"); |
| 272 | result = 1; |
| 273 | } |
| 274 | else if ((st2.st_mode & ALLPERMS) != (0777 | S_ISUID | S_ISGID)) |
| 275 | { |
| 276 | puts ("file mode not changed to 0777 | S_ISUID | S_ISGID"); |
| 277 | result = 1; |
| 278 | } |
| 279 | |
| 280 | if (chmod (basename (testdir), 0777 | S_ISUID | S_ISGID | S_ISVTX) != 0) |
| 281 | { |
| 282 | printf ("cannot change mode of test directory to %o: %m\n", |
| 283 | 0777 | S_ISUID | S_ISGID | S_ISVTX); |
| 284 | result = 1; |
| 285 | } |
| 286 | if (stat64 (basename (testdir), &st2) != 0) |
| 287 | { |
| 288 | printf ("cannot stat test directory: %m\n"); |
| 289 | result = 1; |
| 290 | } |
| 291 | else if ((st2.st_mode & ALLPERMS) != (0777 | S_ISUID | S_ISGID | S_ISVTX)) |
| 292 | { |
| 293 | puts ("directory mode not changed to 0777 | S_ISUID | S_ISGID | S_ISGID"); |
| 294 | result = 1; |
| 295 | } |
| 296 | |
| 297 | snprintf (buf, buflen, "./%s/no-such-file", basename (testdir)); |
| 298 | if (chmod (buf, 0600) != -1) |
| 299 | { |
| 300 | puts ("chmod(\".../no-such-file\",0600) did not fail"); |
| 301 | result = 1; |
| 302 | } |
| 303 | else if (errno != ENOENT) |
| 304 | { |
| 305 | puts ("chmod(\".../no-such-file\",0600) does not set errno to ENOENT"); |
| 306 | result = 1; |
| 307 | } |
| 308 | |
| 309 | snprintf (buf, buflen, "%s/", basename (testdir)); |
| 310 | if (chmod (basename (testdir), 0677) != 0) |
| 311 | { |
| 312 | printf ("cannot change mode of test directory to 0677: %m\n"); |
| 313 | result = 1; |
| 314 | } |
| 315 | else |
| 316 | { |
| 317 | snprintf (buf, buflen, "./%s/file", basename (testdir)); |
| 318 | if (chmod (buf, 0600) == 0) |
| 319 | { |
| 320 | if (getuid () != 0) |
| 321 | { |
| 322 | puts ("chmod(\".../file\") with no-exec directory succeeded"); |
| 323 | result = 1; |
| 324 | } |
| 325 | } |
| 326 | else if (errno != EACCES) |
| 327 | { |
| 328 | puts ("chmod(\".../file\") with no-exec directory didn't set EACCES"); |
| 329 | result = 1; |
| 330 | } |
| 331 | } |
| 332 | |
| 333 | if (chmod (basename (testdir), 0777) != 0) |
| 334 | { |
| 335 | printf ("cannot change mode of test directory to 0777: %m\n"); |
| 336 | result = 1; |
| 337 | goto fail; |
| 338 | } |
| 339 | |
| 340 | snprintf (buf, buflen, "%s/file/cannot-be", basename (testdir)); |
| 341 | if (chmod (buf, 0600) == 0) |
| 342 | { |
| 343 | puts ("chmod(\".../file/cannot-be\",0600) did not fail"); |
| 344 | result = 1; |
| 345 | } |
| 346 | else if (errno != ENOTDIR) |
| 347 | { |
| 348 | puts ("chmod(\".../file/cannot-be\",0600) does not set errno to ENOTDIR"); |
| 349 | result = 1; |
| 350 | } |
| 351 | |
| 352 | fail: |
| 353 | chdir (startdir); |
| 354 | |
| 355 | /* Remove all the files. */ |
| 356 | chmod (testdir, 0700); |
| 357 | if (testfile != NULL) |
| 358 | { |
| 359 | chmod (testfile, 0700); |
| 360 | unlink (testfile); |
| 361 | } |
| 362 | rmdir (testdir); |
| 363 | |
| 364 | /* Free the resources. */ |
| 365 | free (testfile); |
| 366 | free (testdir); |
| 367 | free (startdir); |
| 368 | |
| 369 | return result; |
| 370 | } |
| 371 | |
| 372 | |
| 373 | /* We need a few seconds since we have a few sleeps in the code. */ |
| 374 | #define TIMEOUT 20 |
| 375 | |
| 376 | |
| 377 | #include "../test-skeleton.c" |