1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Copyright (c) International Business Machines Corp., 2001
4 * Ported to LTP: Wayne Boyer
5 * 06/2017 Modified by Guangwen Feng <[email protected]>
6 */
7
8 /*\
9 * [Description]
10 *
11 * 1. open a new file without O_CREAT, ENOENT should be returned.
12 * 2. open a file with O_RDONLY | O_NOATIME and the caller was not
13 * privileged, EPERM should be returned.
14 */
15
16 #define _GNU_SOURCE
17
18 #include <pwd.h>
19 #include "tst_test.h"
20
21 #define TEST_FILE "test_file"
22 #define TEST_FILE2 "test_file2"
23
24 static struct tcase {
25 const char *filename;
26 int flag;
27 int exp_errno;
28 const char *desc;
29 } tcases[] = {
30 {TEST_FILE, O_RDWR, ENOENT, "new file without O_CREAT"},
31 {TEST_FILE2, O_RDONLY | O_NOATIME, EPERM, "unprivileged O_RDONLY | O_NOATIME"},
32 };
33
setup(void)34 void setup(void)
35 {
36 struct passwd *ltpuser;
37
38 SAFE_TOUCH(TEST_FILE2, 0644, NULL);
39
40 ltpuser = SAFE_GETPWNAM("nobody");
41
42 SAFE_SETEUID(ltpuser->pw_uid);
43 }
44
verify_open(unsigned int n)45 static void verify_open(unsigned int n)
46 {
47 struct tcase *tc = &tcases[n];
48
49 TST_EXP_FAIL2(open(tc->filename, tc->flag, 0444),
50 tc->exp_errno, "open() %s", tc->desc);
51 }
52
cleanup(void)53 void cleanup(void)
54 {
55 SAFE_SETEUID(0);
56 }
57
58 static struct tst_test test = {
59 .tcnt = ARRAY_SIZE(tcases),
60 .needs_root = 1,
61 .needs_tmpdir = 1,
62 .setup = setup,
63 .cleanup = cleanup,
64 .test = verify_open,
65 };
66