1 /*
2 * Copyright (c) International Business Machines Corp., 2001
3 * 07/2001 Ported by Wayne Boyer
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
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 */
19 /*
20 * DESCRIPTION
21 * 1. open a new file without O_CREAT, ENOENT should be returned.
22 * 2. open a file with O_RDONLY | O_NOATIME and the caller was not
23 * privileged, EPERM should be returned.
24 */
25
26 #define _GNU_SOURCE
27
28 #include <stdio.h>
29 #include <sys/types.h>
30 #include <sys/stat.h>
31 #include <errno.h>
32 #include <sys/fcntl.h>
33 #include <pwd.h>
34 #include "test.h"
35 #include "safe_macros.h"
36 #include "lapi/fcntl.h"
37
38 #define TEST_FILE "test_file"
39 #define TEST_FILE2 "test_file2"
40
41 char *TCID = "open02";
42
43 static void cleanup(void);
44 static void setup(void);
45
46 static struct test_case_t {
47 char *filename;
48 int flag;
49 int exp_errno;
50 } test_cases[] = {
51 {TEST_FILE, O_RDWR, ENOENT},
52 {TEST_FILE2, O_RDONLY | O_NOATIME, EPERM},
53 };
54
55 int TST_TOTAL = ARRAY_SIZE(test_cases);
56 static void open_verify(const struct test_case_t *);
57
main(int ac,char ** av)58 int main(int ac, char **av)
59 {
60 int lc;
61 int i;
62
63 tst_parse_opts(ac, av, NULL, NULL);
64
65 setup();
66
67 for (lc = 0; TEST_LOOPING(lc); lc++) {
68 tst_count = 0;
69 for (i = 0; i < TST_TOTAL; i++)
70 open_verify(&test_cases[i]);
71 }
72
73 cleanup();
74 tst_exit();
75 }
76
setup(void)77 static void setup(void)
78 {
79 struct passwd *ltpuser;
80
81 tst_require_root();
82
83 tst_sig(NOFORK, DEF_HANDLER, cleanup);
84
85 TEST_PAUSE;
86
87 tst_tmpdir();
88
89 SAFE_TOUCH(cleanup, TEST_FILE2, 0644, NULL);
90
91 ltpuser = SAFE_GETPWNAM(cleanup, "nobody");
92
93 SAFE_SETEUID(cleanup, ltpuser->pw_uid);
94 }
95
open_verify(const struct test_case_t * test)96 static void open_verify(const struct test_case_t *test)
97 {
98 TEST(open(test->filename, test->flag, 0444));
99
100 if (TEST_RETURN != -1) {
101 tst_resm(TFAIL, "open(%s) succeeded unexpectedly",
102 test->filename);
103 return;
104 }
105
106 if (TEST_ERRNO != test->exp_errno) {
107 tst_resm(TFAIL | TTERRNO,
108 "open() failed unexpectedly; expected: %d - %s",
109 test->exp_errno, strerror(test->exp_errno));
110 } else {
111 tst_resm(TPASS | TTERRNO, "open() failed as expected");
112 }
113 }
114
cleanup(void)115 static void cleanup(void)
116 {
117 if (seteuid(0))
118 tst_resm(TWARN | TERRNO, "seteuid(0) failed");
119
120 tst_rmdir();
121 }
122