Skip to content
Navigation Menu
Toggle navigation
Sign in
In this repository
All GitHub Enterprise
↵
Jump to
↵
No suggested jump to results
In this repository
All GitHub Enterprise
↵
Jump to
↵
In this organization
All GitHub Enterprise
↵
Jump to
↵
In this repository
All GitHub Enterprise
↵
Jump to
↵
Sign in
Reseting focus
You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.
You switched accounts on another tab or window.
Reload
to refresh your session.
Dismiss alert
{{ message }}
git-mirror
/
glibc
Public
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
0
Security
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security
Insights
Files
231d5da
aout
argp
assert
bare
bits
catgets
conf
conform
crypt
csu
ctype
debug
dirent
dlfcn
elf
gmon
gnulib
grp
hesiod
hurd
iconv
iconvdata
include
inet
intl
io
libio
linuxthreads
linuxthreads_db
locale
localedata
login
mach
malloc
manual
math
misc
nis
nscd
nss
po
posix
bits
glob
sys
.cvsignore
Depend
Makefile
PTESTS
PTESTS2C.sed
TESTS
TESTS2C.sed
Versions
annexc.c
bsd-getpgrp.c
confstr.c
cpio.h
execl.c
execle.c
execlp.c
execv.c
execvp.c
fnmatch.c
fnmatch.h
fnmatch_loop.c
getconf.c
getopt.c
getopt.h
getopt1.c
getopt_init.c
glob.h
globtest.c
globtest.sh
ptestcases.h
re_comp.h
regex.c
regex.h
regexbug1.c
runptests.c
runtests.c
sched.h
spawn.c
spawn.h
spawn_faction_addclose.c
spawn_faction_adddup2.c
spawn_faction_addopen.c
spawn_faction_destroy.c
spawn_faction_init.c
spawn_int.h
spawnattr_destroy.c
spawnattr_getdefault.c
spawnattr_getflags.c
spawnattr_getpgroup.c
spawnattr_getschedparam.c
spawnattr_getschedpolicy.c
spawnattr_getsigmask.c
spawnattr_init.c
spawnattr_setdefault.c
spawnattr_setflags.c
spawnattr_setpgroup.c
spawnattr_setschedparam.c
spawnattr_setschedpolicy.c
spawnattr_setsigmask.c
spawni.c
spawnp.c
tar.h
test-vfork.c
testcases.h
testfnm.c
tst-chmod.c
tst-dir.c
tst-exec.c
tst-fnmatch.c
tst-fnmatch.input
tst-fork.c
tst-getaddrinfo.c
tst-getlogin.c
tst-mmap.c
tst-preadwrite.c
tst-preadwrite64.c
tst-regexloc.c
tst-spawn.c
tst-truncate.c
tst-truncate64.c
tstgetopt.c
unistd.h
wait.h
wordexp-test.c
wordexp-tst.sh
wordexp.c
wordexp.h
pwd
resolv
resource
rpm
rt
scripts
setjmp
shadow
signal
socket
soft-fp
stdio-common
stdio
stdlib
streams
string
sunrpc
sysdeps
sysvipc
termios
time
timezone
wcsmbs
wctype
.cvsignore
BUGS
CONFORMANCE
COPYING
COPYING.LIB
ChangeLog
ChangeLog.1
ChangeLog.10
ChangeLog.2
ChangeLog.3
ChangeLog.4
ChangeLog.5
ChangeLog.6
ChangeLog.7
ChangeLog.8
ChangeLog.9
FAQ
FAQ.in
INSTALL
INTERFACE
Make-dist
MakeTAGS
Makeconfig
Makefile
Makefile.in
Makerules
NAMESPACE
NEWS
NOTES
PROJECTS
README
README-alpha
README.libm
README.template
Rules
Versions.def
abi-tags
aclocal.m4
config-name.in
config.h.in
config.make.in
configure
configure.in
extra-lib.mk
glibcbug.in
o-iterator.mk
shlib-versions
test-skeleton.c
version.h
Breadcrumbs
glibc
/
posix
/
tst-chmod.c
Blame
Blame
Latest commit
History
History
377 lines (332 loc) · 8.8 KB
Breadcrumbs
glibc
/
posix
/
tst-chmod.c
Top
File metadata and controls
Code
Blame
377 lines (332 loc) · 8.8 KB
Raw
/* Test for chmod functions. Copyright (C) 2000 Free Software Foundation, Inc. Contributed by Ulrich Drepper <drepper@cygnus.com>, 2000. The GNU C Library is free software; you can redistribute it and/or modify it under the terms of the GNU Library General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. The GNU C Library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License for more details. You should have received a copy of the GNU Library General Public License along with the GNU C Library; see the file COPYING.LIB. If not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ #include <dirent.h> #include <errno.h> #include <error.h> #include <fcntl.h> #include <mcheck.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> #include <sys/stat.h> #define OUT_OF_MEMORY \ do { \ puts ("cannot allocate memory"); \ result = 1; \ goto fail; \ } while (0) int do_test (int argc, char *argv[]) { const char *builddir; struct stat64 st1; struct stat64 st2; char *buf; char *testdir; char *testfile = NULL; char *startdir; size_t buflen; int fd; int result = 0; DIR *dir; mtrace (); if (argc <= 1) error (EXIT_FAILURE, 0, "no parameters"); /* This is where we will create the test files. */ builddir = argv[1]; buflen = strlen (builddir) + 50; startdir = getcwd (NULL, 0); if (startdir == NULL) { printf ("cannot get current directory: %m\n"); exit (EXIT_FAILURE); } /* A buffer large enough for everything we need. */ buf = (char *) alloca (buflen); /* Create the directory name. */ snprintf (buf, buflen, "%schmoddirXXXXXX", builddir); if (mkdtemp (buf) == NULL) { printf ("cannot create test directory: %m\n"); exit (EXIT_FAILURE); } if (chmod ("", 0600) == 0) { puts ("chmod(\"\", 0600 didn't fail"); result = 1; } else if (errno != ENOENT) { puts ("chmod(\"\",0600) does not set errno to ENOENT"); result = 1; } /* Create a duplicate. */ testdir = strdup (buf); if (testdir == NULL) OUT_OF_MEMORY; if (stat64 (testdir, &st1) != 0) { printf ("cannot stat test directory: %m\n"); exit (1); } if (!S_ISDIR (st1.st_mode)) { printf ("file not created as directory: %m\n"); exit (1); } /* We have to wait for a second to make sure the ctime changes. */ sleep (1); /* Remove all access rights from the directory. */ if (chmod (testdir, 0) != 0) { printf ("cannot change mode of test directory: %m\n"); result = 1; goto fail; } if (stat64 (testdir, &st2) != 0) { printf ("cannot stat test directory: %m\n"); result = 1; goto fail; } /* Compare result. */ if ((st2.st_mode & ALLPERMS) != 0) { printf ("chmod(...,0) on directory left bits nonzero: %o\n", st2.st_mode & ALLPERMS); result = 1; } if (st1.st_ctime >= st2.st_ctime) { puts ("chmod(...,0) did not set ctime correctly"); result = 1; } /* Name of a file in the directory. */ snprintf (buf, buflen, "%s/file", testdir); testfile = strdup (buf); if (testfile == NULL) OUT_OF_MEMORY; fd = creat (testfile, 0); if (fd != -1) { if (getuid () != 0) { puts ("managed to create test file in protected directory"); result = 1; } close (fd); } else if (errno != EACCES) { puts ("creat didn't generate correct errno value"); result = 1; } /* With this mode it still shouldn't be possible to create a file. */ if (chmod (testdir, 0600) != 0) { printf ("cannot change mode of test directory to 0600: %m\n"); result = 1; goto fail; } fd = creat (testfile, 0); if (fd != -1) { if (getuid () != 0) { puts ("managed to create test file in no-x protected directory"); result = 1; } close (fd); } else if (errno != EACCES) { puts ("creat didn't generate correct errno value"); result = 1; } /* Change the directory mode back to allow creating a file. This time with fchmod. */ dir = opendir (testdir); if (dir != NULL) { if (fchmod (dirfd (dir), 0700) != 0) { printf ("cannot change mode of test directory to 0700: %m\n"); result = 1; closedir (dir); goto fail; } closedir (dir); } else { printf ("cannot open directory: %m\n"); result = 1; if (chmod (testdir, 0700) != 0) { printf ("cannot change mode of test directory to 0700: %m\n"); goto fail; } } fd = creat (testfile, 0); if (fd == -1) { puts ("still didn't manage to create test file in protected directory"); result = 1; goto fail; } if (fstat64 (fd, &st1) != 0) { printf ("cannot stat new file: %m\n"); result = 1; } else if ((st1.st_mode & ALLPERMS) != 0) { puts ("file not created with access mode 0"); result = 1; } close (fd); snprintf (buf, buflen, "%s/..", testdir); chdir (buf); /* We are now in the directory above the one we create the test directory in. */ sleep (1); snprintf (buf, buflen, "./%s/../%s/file", basename (testdir), basename (testdir)); if (chmod (buf, 0600) != 0) { printf ("cannot change mode of file to 0600: %m\n"); result = 1; goto fail; } snprintf (buf, buflen, "./%s//file", basename (testdir)); if (stat64 (buf, &st2) != 0) { printf ("cannot stat new file: %m\n"); result = 1; } else if ((st2.st_mode & ALLPERMS) != 0600) { puts ("file mode not changed to 0600"); result = 1; } else if (st1.st_ctime >= st2.st_ctime) { puts ("chmod(\".../file\",0600) did not set ctime correctly"); result = 1; } if (chmod (buf, 0777 | S_ISUID | S_ISGID) != 0) { printf ("cannot change mode of file to %o: %m\n", 0777 | S_ISUID | S_ISGID); result = 1; } if (stat64 (buf, &st2) != 0) { printf ("cannot stat test file: %m\n"); result = 1; } else if ((st2.st_mode & ALLPERMS) != (0777 | S_ISUID | S_ISGID)) { puts ("file mode not changed to 0777 | S_ISUID | S_ISGID"); result = 1; } if (chmod (basename (testdir), 0777 | S_ISUID | S_ISGID | S_ISVTX) != 0) { printf ("cannot change mode of test directory to %o: %m\n", 0777 | S_ISUID | S_ISGID | S_ISVTX); result = 1; } if (stat64 (basename (testdir), &st2) != 0) { printf ("cannot stat test directory: %m\n"); result = 1; } else if ((st2.st_mode & ALLPERMS) != (0777 | S_ISUID | S_ISGID | S_ISVTX)) { puts ("directory mode not changed to 0777 | S_ISUID | S_ISGID | S_ISGID"); result = 1; } snprintf (buf, buflen, "./%s/no-such-file", basename (testdir)); if (chmod (buf, 0600) != -1) { puts ("chmod(\".../no-such-file\",0600) did not fail"); result = 1; } else if (errno != ENOENT) { puts ("chmod(\".../no-such-file\",0600) does not set errno to ENOENT"); result = 1; } snprintf (buf, buflen, "%s/", basename (testdir)); if (chmod (basename (testdir), 0677) != 0) { printf ("cannot change mode of test directory to 0677: %m\n"); result = 1; } else { snprintf (buf, buflen, "./%s/file", basename (testdir)); if (chmod (buf, 0600) == 0) { if (getuid () != 0) { puts ("chmod(\".../file\") with no-exec directory succeeded"); result = 1; } } else if (errno != EACCES) { puts ("chmod(\".../file\") with no-exec directory didn't set EACCES"); result = 1; } } if (chmod (basename (testdir), 0777) != 0) { printf ("cannot change mode of test directory to 0777: %m\n"); result = 1; goto fail; } snprintf (buf, buflen, "%s/file/cannot-be", basename (testdir)); if (chmod (buf, 0600) == 0) { puts ("chmod(\".../file/cannot-be\",0600) did not fail"); result = 1; } else if (errno != ENOTDIR) { puts ("chmod(\".../file/cannot-be\",0600) does not set errno to ENOTDIR"); result = 1; } fail: chdir (startdir); /* Remove all the files. */ chmod (testdir, 0700); if (testfile != NULL) { chmod (testfile, 0700); unlink (testfile); } rmdir (testdir); /* Free the resources. */ free (testfile); free (testdir); free (startdir); return result; } /* We need a few seconds since we have a few sleeps in the code. */ #define TIMEOUT 20 #include "../test-skeleton.c"
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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
You can’t perform that action at this time.