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