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) 2013 Wanlong Gao <[email protected]>
4*49cdfc7eSAndroid Build Coastguard Worker * Copyright (c) 2018 Linux Test Project
5*49cdfc7eSAndroid Build Coastguard Worker */
6*49cdfc7eSAndroid Build Coastguard Worker
7*49cdfc7eSAndroid Build Coastguard Worker /*\
8*49cdfc7eSAndroid Build Coastguard Worker * [Description]
9*49cdfc7eSAndroid Build Coastguard Worker *
10*49cdfc7eSAndroid Build Coastguard Worker * Verify that open() fails with:
11*49cdfc7eSAndroid Build Coastguard Worker *
12*49cdfc7eSAndroid Build Coastguard Worker * - EEXIST when pathname already exists and O_CREAT and O_EXCL were used
13*49cdfc7eSAndroid Build Coastguard Worker * - EISDIR when pathname refers to a directory and the access requested
14*49cdfc7eSAndroid Build Coastguard Worker * involved writing
15*49cdfc7eSAndroid Build Coastguard Worker * - ENOTDIR when O_DIRECTORY was specified and pathname was not a directory
16*49cdfc7eSAndroid Build Coastguard Worker * - ENAMETOOLONG when pathname was too long
17*49cdfc7eSAndroid Build Coastguard Worker * - EACCES when requested access to the file is not allowed
18*49cdfc7eSAndroid Build Coastguard Worker * - EFAULT when pathname points outside the accessible address space
19*49cdfc7eSAndroid Build Coastguard Worker */
20*49cdfc7eSAndroid Build Coastguard Worker
21*49cdfc7eSAndroid Build Coastguard Worker #define _GNU_SOURCE /* for O_DIRECTORY */
22*49cdfc7eSAndroid Build Coastguard Worker
23*49cdfc7eSAndroid Build Coastguard Worker #include <pwd.h>
24*49cdfc7eSAndroid Build Coastguard Worker #include "tst_test.h"
25*49cdfc7eSAndroid Build Coastguard Worker
26*49cdfc7eSAndroid Build Coastguard Worker #define FLAGS_DESC(x) .flags = x, .desc = #x
27*49cdfc7eSAndroid Build Coastguard Worker
28*49cdfc7eSAndroid Build Coastguard Worker static char *existing_fname = "open08_testfile";
29*49cdfc7eSAndroid Build Coastguard Worker static char *toolong_fname = "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstmnopqrstuvwxyzabcdefghijklmnopqrstmnopqrstuvwxyzabcdefghijklmnopqrstmnopqrstuvwxyzabcdefghijklmnopqrstmnopqrstuvwxyzabcdefghijklmnopqrstmnopqrstuvwxyzabcdefghijklmnopqrstmnopqrstuvwxyz";
30*49cdfc7eSAndroid Build Coastguard Worker static char *dir_fname = "/tmp";
31*49cdfc7eSAndroid Build Coastguard Worker static char *user2_fname = "user2_0600";
32*49cdfc7eSAndroid Build Coastguard Worker static char *unmapped_fname;
33*49cdfc7eSAndroid Build Coastguard Worker
34*49cdfc7eSAndroid Build Coastguard Worker struct test_case_t;
35*49cdfc7eSAndroid Build Coastguard Worker
36*49cdfc7eSAndroid Build Coastguard Worker static struct test_case_t {
37*49cdfc7eSAndroid Build Coastguard Worker char **fname;
38*49cdfc7eSAndroid Build Coastguard Worker int flags;
39*49cdfc7eSAndroid Build Coastguard Worker const char *desc;
40*49cdfc7eSAndroid Build Coastguard Worker int error;
41*49cdfc7eSAndroid Build Coastguard Worker } tcases[] = {
42*49cdfc7eSAndroid Build Coastguard Worker {&existing_fname, FLAGS_DESC(O_CREAT | O_EXCL), EEXIST},
43*49cdfc7eSAndroid Build Coastguard Worker {&dir_fname, FLAGS_DESC(O_RDWR), EISDIR},
44*49cdfc7eSAndroid Build Coastguard Worker {&existing_fname, FLAGS_DESC(O_DIRECTORY), ENOTDIR},
45*49cdfc7eSAndroid Build Coastguard Worker {&toolong_fname, FLAGS_DESC(O_RDWR), ENAMETOOLONG},
46*49cdfc7eSAndroid Build Coastguard Worker {&user2_fname, FLAGS_DESC(O_WRONLY), EACCES},
47*49cdfc7eSAndroid Build Coastguard Worker {&unmapped_fname, FLAGS_DESC(O_CREAT), EFAULT},
48*49cdfc7eSAndroid Build Coastguard Worker };
49*49cdfc7eSAndroid Build Coastguard Worker
verify_open(unsigned int i)50*49cdfc7eSAndroid Build Coastguard Worker static void verify_open(unsigned int i)
51*49cdfc7eSAndroid Build Coastguard Worker {
52*49cdfc7eSAndroid Build Coastguard Worker TST_EXP_FAIL2(open(*tcases[i].fname, tcases[i].flags, 0644),
53*49cdfc7eSAndroid Build Coastguard Worker tcases[i].error, "%s", tcases[i].desc);
54*49cdfc7eSAndroid Build Coastguard Worker }
55*49cdfc7eSAndroid Build Coastguard Worker
setup(void)56*49cdfc7eSAndroid Build Coastguard Worker static void setup(void)
57*49cdfc7eSAndroid Build Coastguard Worker {
58*49cdfc7eSAndroid Build Coastguard Worker int fildes;
59*49cdfc7eSAndroid Build Coastguard Worker char nobody_uid[] = "nobody";
60*49cdfc7eSAndroid Build Coastguard Worker struct passwd *ltpuser;
61*49cdfc7eSAndroid Build Coastguard Worker
62*49cdfc7eSAndroid Build Coastguard Worker umask(0);
63*49cdfc7eSAndroid Build Coastguard Worker
64*49cdfc7eSAndroid Build Coastguard Worker SAFE_CREAT(user2_fname, 0600);
65*49cdfc7eSAndroid Build Coastguard Worker
66*49cdfc7eSAndroid Build Coastguard Worker /* Switch to nobody user for correct error code collection */
67*49cdfc7eSAndroid Build Coastguard Worker ltpuser = getpwnam(nobody_uid);
68*49cdfc7eSAndroid Build Coastguard Worker SAFE_SETGID(ltpuser->pw_gid);
69*49cdfc7eSAndroid Build Coastguard Worker SAFE_SETUID(ltpuser->pw_uid);
70*49cdfc7eSAndroid Build Coastguard Worker
71*49cdfc7eSAndroid Build Coastguard Worker fildes = SAFE_CREAT(existing_fname, 0600);
72*49cdfc7eSAndroid Build Coastguard Worker SAFE_CLOSE(fildes);
73*49cdfc7eSAndroid Build Coastguard Worker
74*49cdfc7eSAndroid Build Coastguard Worker unmapped_fname = tst_get_bad_addr(NULL);
75*49cdfc7eSAndroid Build Coastguard Worker }
76*49cdfc7eSAndroid Build Coastguard Worker
77*49cdfc7eSAndroid Build Coastguard Worker static struct tst_test test = {
78*49cdfc7eSAndroid Build Coastguard Worker .tcnt = ARRAY_SIZE(tcases),
79*49cdfc7eSAndroid Build Coastguard Worker .needs_tmpdir = 1,
80*49cdfc7eSAndroid Build Coastguard Worker .needs_root = 1,
81*49cdfc7eSAndroid Build Coastguard Worker .setup = setup,
82*49cdfc7eSAndroid Build Coastguard Worker .test = verify_open,
83*49cdfc7eSAndroid Build Coastguard Worker };
84