• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Copyright (c) International Business Machines  Corp., 2001
2  *
3  * This program is free software;  you can redistribute it and/or modify
4  * it under the terms of the GNU General Public License as published by
5  * the Free Software Foundation; either version 2 of the License, or
6  * (at your option) any later version.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY;  without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
11  * the GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program. If not, see <http://www.gnu.org/licenses/>.
15  */
16 
17 /*
18  * Verify that user cannot create a directory inside directory owned by another
19  * user with restrictive permissions and that the errno is set to EACCESS.
20  */
21 
22 #include <errno.h>
23 #include <sys/stat.h>
24 #include <sys/types.h>
25 #include <pwd.h>
26 #include <sys/wait.h>
27 #include <unistd.h>
28 #include <stdlib.h>
29 #include "tst_test.h"
30 
31 #define TESTDIR	 "testdir"
32 #define TESTSUBDIR "testdir/testdir"
33 
34 static uid_t nobody_uid, bin_uid;
35 
verify_mkdir(void)36 static void verify_mkdir(void)
37 {
38 	if (mkdir(TESTSUBDIR, 0777) != -1) {
39 		tst_res(TFAIL, "mkdir(%s, %#o) succeeded unexpectedly",
40 			TESTSUBDIR, 0777);
41 		return;
42 	}
43 
44 	if (errno != EACCES) {
45 		tst_res(TFAIL | TERRNO, "Expected EACCES got");
46 		return;
47 	}
48 
49 	tst_res(TPASS | TERRNO, "mkdir() failed expectedly");
50 }
51 
setup(void)52 static void setup(void)
53 {
54 	struct passwd *pw;
55 	pid_t pid;
56 
57 	pw = SAFE_GETPWNAM("nobody");
58 	nobody_uid = pw->pw_uid;
59 	pw = SAFE_GETPWNAM("bin");
60 	bin_uid = pw->pw_uid;
61 
62 	pid = SAFE_FORK();
63 	if (pid == 0) {
64 		SAFE_SETREUID(nobody_uid, nobody_uid);
65 		SAFE_MKDIR(TESTDIR, 0700);
66 		exit(0);
67 	}
68 
69 	tst_reap_children();
70 
71 	SAFE_SETREUID(bin_uid, bin_uid);
72 }
73 
74 static struct tst_test test = {
75 	.test_all = verify_mkdir,
76 	.needs_tmpdir = 1,
77 	.needs_root = 1,
78 	.setup = setup,
79 	.forks_child = 1,
80 };
81