• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *   Copyright (c) 2013 Wanlong Gao <gaowanlong@cn.fujitsu.com>
3  *
4  *   This program is free software;  you can redistribute it and/or modify
5  *   it under the terms of the GNU General Public License as published by
6  *   the Free Software Foundation; either version 2 of the License, or
7  *   (at your option) any later version.
8  *
9  *   This program is distributed in the hope that it will be useful,
10  *   but WITHOUT ANY WARRANTY;  without even the implied warranty of
11  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
12  *   the GNU General Public License for more details.
13  *
14  *   You should have received a copy of the GNU General Public License
15  *   along with this program;  if not, write to the Free Software
16  *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17  */
18 
19 /*
20  * DESCRIPTION
21  *	Check for the following errors:
22  *	1.	EEXIST
23  *	2.	EISDIR
24  *	3.	ENOTDIR
25  *	4.	ENAMETOOLONG
26  *	5.	EACCES
27  *	6.	EFAULT
28  *
29  * ALGORITHM
30  *	1. Open a file with O_CREAT and O_EXCL, when the file already
31  *	   exists. Check the errno for EEXIST
32  *
33  *	2. Pass a directory as the pathname and request a write access,
34  *	   check for errno for EISDIR
35  *
36  *	3. Specify O_DIRECTORY as a parameter to open and pass a file as the
37  *	   pathname, check errno for ENOTDIR
38  *
39  *	4. Attempt to open() a filename which is more than VFS_MAXNAMLEN, and
40  *	   check for errno to be ENAMETOOLONG.
41  *
42  *	5. Attempt to open a test executable in WRONLY mode,
43  *	   open(2) should fail with EACCES.
44  *
45  *	6. Attempt to pass an invalid pathname with an address pointing outside
46  *	   the address space of the process, as the argument to open(), and
47  *	   expect to get EFAULT.
48  */
49 
50 #define _GNU_SOURCE		/* for O_DIRECTORY */
51 #include <sys/types.h>
52 #include <sys/stat.h>
53 #include <stdio.h>
54 #include <errno.h>
55 #include <sys/mman.h>
56 #include <fcntl.h>
57 #include <signal.h>
58 #include <pwd.h>
59 #include "test.h"
60 
61 static void setup(void);
62 static void cleanup(void);
63 
64 char *TCID = "open08";
65 
66 static char nobody_uid[] = "nobody";
67 static struct passwd *ltpuser;
68 
69 static char *bad_addr;
70 
71 static char filename[40] = "";
72 static char fname[] = "/bin/cat";
73 static char bad_file[] = "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstmnopqrstuvwxyzabcdefghijklmnopqrstmnopqrstuvwxyzabcdefghijklmnopqrstmnopqrstuvwxyzabcdefghijklmnopqrstmnopqrstuvwxyzabcdefghijklmnopqrstmnopqrstuvwxyzabcdefghijklmnopqrstmnopqrstuvwxyz";
74 
75 static struct test_case_t {
76 	char *fname;
77 	int flags;
78 	int error;
79 } TC[] = {
80 	{filename, O_CREAT | O_EXCL, EEXIST},
81 	{"/tmp", O_RDWR, EISDIR},
82 	{filename, O_DIRECTORY, ENOTDIR},
83 	{bad_file, O_RDWR, ENAMETOOLONG},
84 	{fname, O_WRONLY, EACCES},
85 #if !defined(UCLINUX)
86 	{(char *)-1, O_CREAT, EFAULT}
87 #endif
88 };
89 
90 int TST_TOTAL = sizeof(TC) / sizeof(TC[0]);
91 
main(int ac,char ** av)92 int main(int ac, char **av)
93 {
94 	int lc;
95 	int i;
96 
97 	tst_parse_opts(ac, av, NULL, NULL);
98 
99 	setup();
100 
101 	for (lc = 0; TEST_LOOPING(lc); lc++) {
102 		tst_count = 0;
103 
104 		for (i = 0; i < TST_TOTAL; i++) {
105 			TEST(open(TC[i].fname, TC[i].flags,
106 				  S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH));
107 
108 			if (TEST_RETURN != -1) {
109 				tst_resm(TFAIL, "call succeeded unexpectedly");
110 				continue;
111 			}
112 
113 			if (TEST_ERRNO == TC[i].error) {
114 				tst_resm(TPASS, "expected failure - "
115 					 "errno = %d : %s", TEST_ERRNO,
116 					 strerror(TEST_ERRNO));
117 			} else {
118 				tst_resm(TFAIL, "unexpected error - %d : %s - "
119 					 "expected %d", TEST_ERRNO,
120 					 strerror(TEST_ERRNO), TC[i].error);
121 			}
122 		}
123 	}
124 
125 	cleanup();
126 	tst_exit();
127 }
128 
setup(void)129 static void setup(void)
130 {
131 	int fildes;
132 
133 	tst_require_root();
134 
135 	tst_sig(NOFORK, DEF_HANDLER, cleanup);
136 
137 	umask(0);
138 
139 	TEST_PAUSE;
140 
141 	/* Switch to nobody user for correct error code collection */
142 	ltpuser = getpwnam(nobody_uid);
143 	if (setgid(ltpuser->pw_gid) == -1) {
144 		tst_brkm(TBROK | TERRNO, NULL, "setgid(%d) failed",
145 			 ltpuser->pw_gid);
146 	} else if (setuid(ltpuser->pw_uid) == -1) {
147 		tst_brkm(TBROK | TERRNO, NULL, "setuid(%d) failed",
148 			 ltpuser->pw_uid);
149 	}
150 
151 	tst_tmpdir();
152 
153 	sprintf(filename, "open3.%d", getpid());
154 
155 	fildes = creat(filename, 0600);
156 	if (fildes == -1)
157 		tst_brkm(TBROK, cleanup, "Can't creat %s", filename);
158 
159 	close(fildes);
160 
161 #if !defined(UCLINUX)
162 	bad_addr = mmap(0, 1, PROT_NONE,
163 			MAP_PRIVATE_EXCEPT_UCLINUX | MAP_ANONYMOUS, 0, 0);
164 	if (bad_addr == MAP_FAILED)
165 		tst_brkm(TBROK, cleanup, "mmap failed");
166 
167 	TC[5].fname = bad_addr;
168 #endif
169 }
170 
cleanup(void)171 static void cleanup(void)
172 {
173 	tst_rmdir();
174 }
175