1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Copyright (c) 2013 Wanlong Gao <gaowanlong@cn.fujitsu.com>
4 * Copyright (c) 2018 Linux Test Project
5 */
6
7 /*
8 * DESCRIPTION
9 * Check for the following errors:
10 * 1. EEXIST
11 * 2. EISDIR
12 * 3. ENOTDIR
13 * 4. ENAMETOOLONG
14 * 5. EACCES
15 * 6. EFAULT
16 *
17 * ALGORITHM
18 * 1. Open a file with O_CREAT and O_EXCL, when the file already
19 * exists. Check the errno for EEXIST
20 *
21 * 2. Pass a directory as the pathname and request a write access,
22 * check for errno for EISDIR
23 *
24 * 3. Specify O_DIRECTORY as a parameter to open and pass a file as the
25 * pathname, check errno for ENOTDIR
26 *
27 * 4. Attempt to open() a filename which is more than VFS_MAXNAMLEN, and
28 * check for errno to be ENAMETOOLONG.
29 *
30 * 5. Attempt to open a (0600) file owned by different user in WRONLY mode,
31 * open(2) should fail with EACCES.
32 *
33 * 6. Attempt to pass an invalid pathname with an address pointing outside
34 * the accessible address space of the process, as the argument to open(),
35 * and expect to get EFAULT.
36 */
37
38 #define _GNU_SOURCE /* for O_DIRECTORY */
39 #include <sys/types.h>
40 #include <sys/stat.h>
41 #include <stdio.h>
42 #include <errno.h>
43 #include <sys/mman.h>
44 #include <fcntl.h>
45 #include <signal.h>
46 #include <pwd.h>
47 #include "tst_test.h"
48 #include "tst_get_bad_addr.h"
49
50 static char *existing_fname = "open08_testfile";
51 static char *toolong_fname = "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstmnopqrstuvwxyzabcdefghijklmnopqrstmnopqrstuvwxyzabcdefghijklmnopqrstmnopqrstuvwxyzabcdefghijklmnopqrstmnopqrstuvwxyzabcdefghijklmnopqrstmnopqrstuvwxyzabcdefghijklmnopqrstmnopqrstuvwxyz";
52 static char *dir_fname = "/tmp";
53 static char *user2_fname = "user2_0600";
54 static char *unmapped_fname;
55
56 struct test_case_t;
57
58 static struct test_case_t {
59 char **fname;
60 int flags;
61 int error;
62 } tcases[] = {
63 {&existing_fname, O_CREAT | O_EXCL, EEXIST},
64 {&dir_fname, O_RDWR, EISDIR},
65 {&existing_fname, O_DIRECTORY, ENOTDIR},
66 {&toolong_fname, O_RDWR, ENAMETOOLONG},
67 {&user2_fname, O_WRONLY, EACCES},
68 {&unmapped_fname, O_CREAT, EFAULT}
69 };
70
verify_open(unsigned int i)71 void verify_open(unsigned int i)
72 {
73 TEST(open(*tcases[i].fname, tcases[i].flags,
74 S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH));
75
76 if (TST_RET != -1) {
77 tst_res(TFAIL, "call succeeded unexpectedly");
78 return;
79 }
80
81 if (TST_ERR == tcases[i].error) {
82 tst_res(TPASS, "expected failure - "
83 "errno = %d : %s", TST_ERR,
84 strerror(TST_ERR));
85 } else {
86 tst_res(TFAIL, "unexpected error - %d : %s - "
87 "expected %d", TST_ERR,
88 strerror(TST_ERR), tcases[i].error);
89 }
90 }
91
setup(void)92 static void setup(void)
93 {
94 int fildes;
95 char nobody_uid[] = "nobody";
96 struct passwd *ltpuser;
97
98 umask(0);
99
100 SAFE_CREAT(user2_fname, 0600);
101
102 /* Switch to nobody user for correct error code collection */
103 ltpuser = getpwnam(nobody_uid);
104 SAFE_SETGID(ltpuser->pw_gid);
105 SAFE_SETUID(ltpuser->pw_uid);
106
107 fildes = SAFE_CREAT(existing_fname, 0600);
108 close(fildes);
109
110 unmapped_fname = tst_get_bad_addr(NULL);
111 }
112
113 static struct tst_test test = {
114 .tcnt = ARRAY_SIZE(tcases),
115 .needs_tmpdir = 1,
116 .needs_root = 1,
117 .setup = setup,
118 .test = verify_open,
119 };
120