blob: 680e69ab01fb59553e2398e3101e752e3e709da7 [file] [log] [blame]
plars865695b2001-08-27 22:15:12 +00001/*
2 *
3 * Copyright (c) International Business Machines Corp., 2001
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program 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
13 * the GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
Wanlong Gao4548c6c2012-10-19 18:03:36 +080017 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
plars865695b2001-08-27 22:15:12 +000018 */
19
20/*
21 * Test Name: stat03
22 *
23 * Test Description:
24 * Verify that,
25 * 1) stat(2) returns -1 and sets errno to EACCES if search permission is
26 * denied on a component of the path prefix.
27 * 2) stat(2) returns -1 and sets errno to ENOENT if the specified file
28 * does not exists or empty string.
29 * 3) stat(2) returns -1 and sets errno to EFAULT if pathname points
30 * outside user's accessible address space.
31 * 4) stat(2) returns -1 and sets errno to ENAMETOOLONG if the pathname
32 * component is too long.
33 * 5) stat(2) returns -1 and sets errno to ENOTDIR if the directory
34 * component in pathname is not a directory.
35 *
36 * Expected Result:
37 * stat() should fail with return value -1 and set expected errno.
38 *
39 * Algorithm:
40 * Setup:
41 * Setup signal handling.
42 * Create temporary directory.
43 * Pause for SIGUSR1 if option specified.
44 *
45 * Test:
46 * Loop if the proper options are given.
47 * Execute system call
48 * Check return code, if system call failed (return=-1)
49 * if errno set == expected errno
50 * Issue sys call fails with expected return value and errno.
51 * Otherwise,
52 * Issue sys call fails with unexpected errno.
53 * Otherwise,
54 * Issue sys call returns unexpected value.
55 *
56 * Cleanup:
57 * Print errno log and/or timing stats if options given
58 * Delete the temporary directory(s)/file(s) created.
59 *
60 * Usage: <for command-line>
61 * stat03 [-c n] [-e] [-i n] [-I x] [-p x] [-t]
62 * where, -c n : Run n copies concurrently.
63 * -e : Turn on errno logging.
64 * -i n : Execute test n times.
65 * -I x : Execute test for x seconds.
66 * -P x : Pause for x seconds between iterations.
67 * -t : Turn on syscall timing.
68 *
69 * History
70 * 07/2001 John George
71 * -Ported
72 *
73 * Restrictions:
plars865695b2001-08-27 22:15:12 +000074 *
75 */
76
77#include <stdio.h>
78#include <stdlib.h>
79#include <unistd.h>
80#include <fcntl.h>
81#include <errno.h>
82#include <string.h>
83#include <signal.h>
84#include <sys/types.h>
85#include <sys/stat.h>
plars1ad84512002-07-23 13:11:18 +000086#include <sys/mman.h>
robbiewb1c11a12001-08-28 15:47:02 +000087#include <pwd.h>
plars865695b2001-08-27 22:15:12 +000088
89#include "test.h"
plars865695b2001-08-27 22:15:12 +000090
91#define MODE_RWX S_IRWXU | S_IRWXG | S_IRWXO
92#define FILE_MODE S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH
93#define DIR_TEMP "testdir_1"
94#define TEST_FILE1 "testdir_1/tfile_1"
95#define TEST_FILE2 "t_file/tfile_2"
96
97int no_setup();
98int setup1(); /* setup function to test chmod for EACCES */
99int setup2(); /* setup function to test chmod for ENOTDIR */
subrata_modak56207ce2009-03-23 13:35:39 +0000100int longpath_setup(); /* setup function to test chmod for ENAMETOOLONG */
robbiewb1c11a12001-08-28 15:47:02 +0000101char nobody_uid[] = "nobody";
102struct passwd *ltpuser;
103
subrata_modak56207ce2009-03-23 13:35:39 +0000104char Longpathname[PATH_MAX + 2];
plars865695b2001-08-27 22:15:12 +0000105char High_address_node[64];
106
subrata_modak56207ce2009-03-23 13:35:39 +0000107struct test_case_t { /* test case struct. to hold ref. test cond's */
plars865695b2001-08-27 22:15:12 +0000108 char *pathname;
109 char *desc;
110 int exp_errno;
subrata_modak56207ce2009-03-23 13:35:39 +0000111 int (*setupfunc) ();
plars865695b2001-08-27 22:15:12 +0000112} Test_cases[] = {
subrata_modak56207ce2009-03-23 13:35:39 +0000113 {
114 TEST_FILE1, "No Search permissions to process", EACCES, setup1},
robbiewd34d5812005-07-11 22:28:09 +0000115#if !defined(UCLINUX)
subrata_modak56207ce2009-03-23 13:35:39 +0000116 {
117 High_address_node, "Address beyond address space", EFAULT, no_setup},
118 {
119 (char *)-1, "Negative address", EFAULT, no_setup},
vapierb5ed1f62006-08-24 04:16:32 +0000120#endif
subrata_modak56207ce2009-03-23 13:35:39 +0000121 {
122 Longpathname, "Pathname too long", ENAMETOOLONG, longpath_setup}, {
123 "", "Pathname is empty", ENOENT, no_setup}, {
124 TEST_FILE2, "Path contains regular file", ENOTDIR, setup2}, {
125 NULL, NULL, 0, no_setup}
plars865695b2001-08-27 22:15:12 +0000126};
127
Cyril Hrubisfdce7d52013-04-04 18:35:48 +0200128char *TCID = "stat03";
Cyril Hrubisb863a0b2014-09-24 13:15:29 +0200129int TST_TOTAL = ARRAY_SIZE(Test_cases);
plars865695b2001-08-27 22:15:12 +0000130
subrata_modak56207ce2009-03-23 13:35:39 +0000131char *bad_addr = 0;
plars1ad84512002-07-23 13:11:18 +0000132
plars865695b2001-08-27 22:15:12 +0000133void setup(); /* Main setup function for the tests */
134void cleanup(); /* cleanup function for the test */
135
subrata_modak56207ce2009-03-23 13:35:39 +0000136int main(int ac, char **av)
plars865695b2001-08-27 22:15:12 +0000137{
138 struct stat stat_buf; /* stat structure buffer */
Cyril Hrubis89af32a2012-10-24 16:39:11 +0200139 int lc;
Cyril Hrubis0b9589f2014-05-27 17:40:33 +0200140 const char *msg;
subrata_modak56207ce2009-03-23 13:35:39 +0000141 char *file_name; /* ptr. for file name whose mode is modified */
plars865695b2001-08-27 22:15:12 +0000142 char *test_desc; /* test specific error message */
143 int ind; /* counter to test different test conditions */
144
Garrett Cooper53740502010-12-16 00:04:01 -0800145 if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL)
plars865695b2001-08-27 22:15:12 +0000146 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
plars865695b2001-08-27 22:15:12 +0000147
148 /*
149 * Invoke setup function to call individual test setup functions
150 * to simulate test conditions.
151 */
152 setup();
153
plars865695b2001-08-27 22:15:12 +0000154 for (lc = 0; TEST_LOOPING(lc); lc++) {
Garrett Cooper2c282152010-12-16 00:55:50 -0800155
Caspar Zhangd59a6592013-03-07 14:59:12 +0800156 tst_count = 0;
plars865695b2001-08-27 22:15:12 +0000157
158 for (ind = 0; Test_cases[ind].desc != NULL; ind++) {
159 file_name = Test_cases[ind].pathname;
160 test_desc = Test_cases[ind].desc;
161
robbiewd34d5812005-07-11 22:28:09 +0000162#if !defined(UCLINUX)
plars865695b2001-08-27 22:15:12 +0000163 if (file_name == High_address_node) {
164 file_name = (char *)get_high_address();
165 }
robbiewd34d5812005-07-11 22:28:09 +0000166#endif
plars865695b2001-08-27 22:15:12 +0000167
168 /*
169 * Call stat(2) to test different test conditions.
170 * verify that it fails with -1 return value and
171 * sets appropriate errno.
172 */
173 TEST(stat(file_name, &stat_buf));
subrata_modakbdbaec52009-02-26 12:14:51 +0000174
plars865695b2001-08-27 22:15:12 +0000175 /* Check return code from stat(2) */
176 if (TEST_RETURN == -1) {
plars865695b2001-08-27 22:15:12 +0000177 if (TEST_ERRNO == Test_cases[ind].exp_errno) {
178 tst_resm(TPASS,
179 "stat() fails, %s, errno:%d",
180 test_desc, TEST_ERRNO);
181 } else {
plars74948ad2002-11-14 16:16:14 +0000182 tst_resm(TFAIL,
subrata_modak56207ce2009-03-23 13:35:39 +0000183 "stat() fails, %s, errno:%d, expected errno:%d",
184 test_desc, TEST_ERRNO,
185 Test_cases[ind].exp_errno);
plars865695b2001-08-27 22:15:12 +0000186 }
187 } else {
plars74948ad2002-11-14 16:16:14 +0000188 tst_resm(TFAIL,
subrata_modak923b23f2009-11-02 13:57:16 +0000189 "stat(2) returned %ld, expected -1, errno:%d",
subrata_modak56207ce2009-03-23 13:35:39 +0000190 TEST_RETURN,
191 Test_cases[ind].exp_errno);
plars865695b2001-08-27 22:15:12 +0000192 }
Garrett Cooper2c282152010-12-16 00:55:50 -0800193 }
Caspar Zhangd59a6592013-03-07 14:59:12 +0800194 tst_count++; /* incr TEST_LOOP counter */
Garrett Cooper2c282152010-12-16 00:55:50 -0800195 }
plars865695b2001-08-27 22:15:12 +0000196
197 /*
198 * Invoke cleanup() to delete the test directory/file(s) created
199 * in the setup().
200 */
201 cleanup();
Garrett Cooper1e6f5a62010-12-19 09:58:10 -0800202 tst_exit();
plars865695b2001-08-27 22:15:12 +0000203
Garrett Cooper2c282152010-12-16 00:55:50 -0800204}
plars865695b2001-08-27 22:15:12 +0000205
206/*
subrata_modak4bb656a2009-02-26 12:02:09 +0000207 * void
plars865695b2001-08-27 22:15:12 +0000208 * setup(void) - performs all ONE TIME setup for this test.
209 * Exit the test program on receipt of unexpected signals.
210 * Create a temporary directory and change directory to it.
211 * Invoke individual test setup functions according to the order
212 * set in struct. definition.
213 */
Mike Frysingerc57fba52014-04-09 18:56:30 -0400214void setup(void)
plars865695b2001-08-27 22:15:12 +0000215{
Garrett Cooper2c282152010-12-16 00:55:50 -0800216 int ind;
plars865695b2001-08-27 22:15:12 +0000217
Nicolas Jolyd4ceb372014-06-22 17:03:57 +0200218 tst_require_root(NULL);
219
plars865695b2001-08-27 22:15:12 +0000220 /* Capture unexpected signals */
221 tst_sig(FORK, DEF_HANDLER, cleanup);
222
robbiewb1c11a12001-08-28 15:47:02 +0000223 /* Switch to nobody user for correct error code collection */
subrata_modak56207ce2009-03-23 13:35:39 +0000224 ltpuser = getpwnam(nobody_uid);
225 if (setuid(ltpuser->pw_uid) == -1) {
226 tst_resm(TINFO, "setuid failed to "
227 "to set the effective uid to %d", ltpuser->pw_uid);
228 perror("setuid");
229 }
plars865695b2001-08-27 22:15:12 +0000230
231 /* Pause if that option was specified
232 * TEST_PAUSE contains the code to fork the test with the -i option.
233 * You want to make sure you do this before you create your temporary
234 * directory.
235 */
236 TEST_PAUSE;
237
238 /* Make a temp dir and cd to it */
239 tst_tmpdir();
240
vapier62b16cf2007-02-09 20:48:23 +0000241#if !defined(UCLINUX)
robbiewd34d5812005-07-11 22:28:09 +0000242 bad_addr = mmap(0, 1, PROT_NONE,
subrata_modak56207ce2009-03-23 13:35:39 +0000243 MAP_PRIVATE_EXCEPT_UCLINUX | MAP_ANONYMOUS, 0, 0);
vapier0722a2b2006-05-12 15:44:11 +0000244 if (bad_addr == MAP_FAILED) {
plars1ad84512002-07-23 13:11:18 +0000245 tst_brkm(TBROK, cleanup, "mmap failed");
246 }
247 Test_cases[2].pathname = bad_addr;
mreed1081534c32006-08-03 05:21:24 +0000248#endif
vapier62b16cf2007-02-09 20:48:23 +0000249
plars865695b2001-08-27 22:15:12 +0000250 /* call individual setup functions */
251 for (ind = 0; Test_cases[ind].desc != NULL; ind++) {
252 Test_cases[ind].setupfunc();
253 }
Garrett Cooper2c282152010-12-16 00:55:50 -0800254}
plars865695b2001-08-27 22:15:12 +0000255
256/*
257 * int
258 * no_setup() - Some test conditions for stat(2) do not any setup.
259 * Hence, this function just returns 0.
260 * This function simply returns 0.
261 */
Mike Frysingerc57fba52014-04-09 18:56:30 -0400262int no_setup(void)
plars865695b2001-08-27 22:15:12 +0000263{
subrata_modak56207ce2009-03-23 13:35:39 +0000264 return 0;
plars865695b2001-08-27 22:15:12 +0000265}
266
plars865695b2001-08-27 22:15:12 +0000267/*
268 * int
269 * setup1() - setup function for a test condition for which stat(2)
270 * returns -1 and sets errno to EACCES.
271 * Create a test directory under temporary directory and create a test file
272 * under this directory with mode "0666" permissions.
273 * Modify the mode permissions on test directory such that process will not
274 * have search permissions on test directory.
275 *
276 * The function returns 0.
277 */
Mike Frysingerc57fba52014-04-09 18:56:30 -0400278int setup1(void)
plars865695b2001-08-27 22:15:12 +0000279{
280 int fd; /* file handle for testfile */
281
282 /* Creat a test directory */
283 if (mkdir(DIR_TEMP, MODE_RWX) < 0) {
284 tst_brkm(TBROK, cleanup, "mkdir(2) of %s failed", DIR_TEMP);
Wanlong Gao354ebb42012-12-07 10:10:04 +0800285 }
plars865695b2001-08-27 22:15:12 +0000286
287 /* Creat a test file under above test directory */
subrata_modak56207ce2009-03-23 13:35:39 +0000288 if ((fd = open(TEST_FILE1, O_RDWR | O_CREAT, 0666)) == -1) {
plars865695b2001-08-27 22:15:12 +0000289 tst_brkm(TBROK, cleanup,
290 "open(%s, O_RDWR|O_CREAT, 0666) failed, errno=%d : %s",
291 TEST_FILE1, errno, strerror(errno));
Wanlong Gao354ebb42012-12-07 10:10:04 +0800292 }
plars865695b2001-08-27 22:15:12 +0000293 /* Close the test file */
294 if (close(fd) == -1) {
295 tst_brkm(TBROK, cleanup,
296 "close(%s) Failed, errno=%d : %s",
297 TEST_FILE1, errno, strerror(errno));
Wanlong Gao354ebb42012-12-07 10:10:04 +0800298 }
plars865695b2001-08-27 22:15:12 +0000299
300 /* Modify mode permissions on test directory */
301 if (chmod(DIR_TEMP, FILE_MODE) < 0) {
302 tst_brkm(TBROK, cleanup, "chmod(2) of %s failed", DIR_TEMP);
Wanlong Gao354ebb42012-12-07 10:10:04 +0800303 }
plars865695b2001-08-27 22:15:12 +0000304 return 0;
305}
306
307/*
308 * int
309 * setup2() - setup function for a test condition for which stat(2)
310 * returns -1 and sets errno to ENOTDIR.
subrata_modak4bb656a2009-02-26 12:02:09 +0000311 *
plars865695b2001-08-27 22:15:12 +0000312 * Create a test file under temporary directory so that test tries to
313 * change mode of a testfile "tfile_2" under "t_file" which happens to be
314 * another regular file.
315 */
Mike Frysingerc57fba52014-04-09 18:56:30 -0400316int setup2(void)
plars865695b2001-08-27 22:15:12 +0000317{
subrata_modak56207ce2009-03-23 13:35:39 +0000318 int fd; /* File handle for test file */
subrata_modakbdbaec52009-02-26 12:14:51 +0000319
plars865695b2001-08-27 22:15:12 +0000320 /* Creat a test file under temporary directory */
subrata_modak56207ce2009-03-23 13:35:39 +0000321 if ((fd = open("t_file", O_RDWR | O_CREAT, MODE_RWX)) == -1) {
plars865695b2001-08-27 22:15:12 +0000322 tst_brkm(TBROK, cleanup,
323 "open(2) on t_file failed, errno=%d : %s",
324 errno, strerror(errno));
Wanlong Gao354ebb42012-12-07 10:10:04 +0800325 }
plars865695b2001-08-27 22:15:12 +0000326 /* Close the test file created above */
327 if (close(fd) == -1) {
328 tst_brkm(TBROK, cleanup,
329 "close(t_file) Failed, errno=%d : %s",
330 errno, strerror(errno));
Wanlong Gao354ebb42012-12-07 10:10:04 +0800331 }
plars865695b2001-08-27 22:15:12 +0000332 return 0;
333}
334
335/*
336 * int
337 * longpath_setup() - setup to create a node with a name length exceeding
338 * the MAX. length of PATH_MAX.
339 * This function retruns 0.
340 */
Mike Frysingerc57fba52014-04-09 18:56:30 -0400341int longpath_setup(void)
plars865695b2001-08-27 22:15:12 +0000342{
subrata_modak56207ce2009-03-23 13:35:39 +0000343 int ind; /* counter variable */
plars865695b2001-08-27 22:15:12 +0000344
345 for (ind = 0; ind <= (PATH_MAX + 1); ind++) {
346 Longpathname[ind] = 'a';
347 }
348 return 0;
349}
350
351/*
352 * void
353 * cleanup() - Performs all ONE TIME cleanup for this test at
354 * completion or premature exit.
355 * Print test timing stats and errno log if test executed with options.
356 * Remove temporary directory and sub-directories/files under it
357 * created during setup().
358 * Exit the test program with normal exit code.
359 */
Mike Frysingerc57fba52014-04-09 18:56:30 -0400360void cleanup(void)
plars865695b2001-08-27 22:15:12 +0000361{
subrata_modakbdbaec52009-02-26 12:14:51 +0000362
plars865695b2001-08-27 22:15:12 +0000363 /* Restore mode permissions on test directory created in setup2() */
364 if (chmod(DIR_TEMP, MODE_RWX) < 0) {
365 tst_brkm(TFAIL, NULL, "chmod(2) of %s failed", DIR_TEMP);
366 }
367
plars865695b2001-08-27 22:15:12 +0000368 tst_rmdir();
Chris Dearmanec6edca2012-10-17 19:54:01 -0700369}