1 /*
2 * Copyright (c) International Business Machines Corp., 2001
3 * 07/2001 Ported by Wayne Boyer
4 * 06/2017 Modified by Guangwen Feng <fenggw-fnst@cn.fujitsu.com>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
14 * the GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20 /*
21 * DESCRIPTION
22 * 1. open a new file without O_CREAT, ENOENT should be returned.
23 * 2. open a file with O_RDONLY | O_NOATIME and the caller was not
24 * privileged, EPERM should be returned.
25 */
26
27 #define _GNU_SOURCE
28
29 #include <stdio.h>
30 #include <sys/types.h>
31 #include <sys/stat.h>
32 #include <errno.h>
33 #include <fcntl.h>
34 #include <pwd.h>
35 #include "tst_test.h"
36
37 #define TEST_FILE "test_file"
38 #define TEST_FILE2 "test_file2"
39
40 static struct tcase {
41 char *filename;
42 int flag;
43 int exp_errno;
44 } tcases[] = {
45 {TEST_FILE, O_RDWR, ENOENT},
46 {TEST_FILE2, O_RDONLY | O_NOATIME, EPERM},
47 };
48
setup(void)49 void setup(void)
50 {
51 struct passwd *ltpuser;
52
53 SAFE_TOUCH(TEST_FILE2, 0644, NULL);
54
55 ltpuser = SAFE_GETPWNAM("nobody");
56
57 SAFE_SETEUID(ltpuser->pw_uid);
58 }
59
verify_open(unsigned int n)60 static void verify_open(unsigned int n)
61 {
62 struct tcase *tc = &tcases[n];
63
64 TEST(open(tc->filename, tc->flag, 0444));
65
66 if (TST_RET != -1) {
67 tst_res(TFAIL, "open(%s) succeeded unexpectedly",
68 tc->filename);
69 return;
70 }
71
72 if (tc->exp_errno != TST_ERR) {
73 tst_res(TFAIL | TTERRNO,
74 "open() should fail with %s",
75 tst_strerrno(tc->exp_errno));
76 return;
77 }
78
79 tst_res(TPASS | TTERRNO, "open() failed as expected");
80 }
81
cleanup(void)82 void cleanup(void)
83 {
84 SAFE_SETEUID(0);
85 }
86
87 static struct tst_test test = {
88 .tcnt = ARRAY_SIZE(tcases),
89 .needs_root = 1,
90 .needs_tmpdir = 1,
91 .setup = setup,
92 .cleanup = cleanup,
93 .test = verify_open,
94 };
95