• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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  */
6 
7 /*
8  * DESCRIPTION
9  *	Testcase to check creat(2) sets the following errnos correctly:
10  *	1.	EISDIR
11  *	2.	ENAMETOOLONG
12  *	3.	ENOENT
13  *	4.	ENOTDIR
14  *	5.	EFAULT
15  *	6.	EACCES
16  *	7.	ELOOP
17  *	8.	EROFS
18  *
19  *
20  * ALGORITHM
21  *	1.	Attempt to creat(2) an existing directory, and test for
22  *		EISDIR
23  *	2.	Attempt to creat(2) a file whose name is more than
24  *		VFS_MAXNAMLEN and test for ENAMETOOLONG.
25  *	3.	Attempt to creat(2) a file inside a directory which doesn't
26  *		exist, and test for ENOENT
27  *	4.	Attempt to creat(2) a file, the pathname of which comprises
28  *		a component which is a file, test for ENOTDIR.
29  *	5.	Attempt to creat(2) a file with a bad address
30  *		and test for EFAULT
31  *	6.	Attempt to creat(2) a file in a directory with no
32  *		execute permission and test for EACCES
33  *	7.	Attempt to creat(2) a file which links the other file that
34  *		links the former and test for ELOOP
35  *	8.	Attempt to creat(2) a file in a Read-only file system
36  *		and test for EROFS
37  */
38 
39 #include <errno.h>
40 #include <string.h>
41 #include <limits.h>
42 #include <pwd.h>
43 #include <sys/mman.h>
44 #include <sys/types.h>
45 #include <sys/stat.h>
46 #include <sys/mount.h>
47 
48 #include "tst_test.h"
49 
50 #define	TEST_FILE	"test_dir"
51 #define	NO_DIR		"testfile/testdir"
52 #define	NOT_DIR		"file1/testdir"
53 #define	TEST6_FILE	"dir6/file6"
54 #define	TEST7_FILE	"file7"
55 #define	TEST8_FILE	"mntpoint/tmp"
56 
57 #define	MODE1		0444
58 #define	MODE2		0666
59 
60 static void setup(void);
61 static void test6_setup(void);
62 static void test6_cleanup(void);
63 #if !defined(UCLINUX)
64 static void bad_addr_setup(int);
65 #endif
66 
67 static struct passwd *ltpuser;
68 static char long_name[PATH_MAX+2];
69 
70 static struct test_case_t {
71 	char *fname;
72 	int mode;
73 	int error;
74 	void (*setup)();
75 	void (*cleanup)(void);
76 } tcases[] = {
77 	{TEST_FILE, MODE1, EISDIR, NULL, NULL},
78 	{long_name, MODE1, ENAMETOOLONG, NULL, NULL},
79 	{NO_DIR, MODE1, ENOENT, NULL, NULL},
80 	{NOT_DIR, MODE1, ENOTDIR, NULL, NULL},
81 #if !defined(UCLINUX)
82 	{NULL, MODE1, EFAULT, bad_addr_setup, NULL},
83 #endif
84 	{TEST6_FILE, MODE1, EACCES, test6_setup, test6_cleanup},
85 	{TEST7_FILE, MODE1, ELOOP, NULL, NULL},
86 	{TEST8_FILE, MODE1, EROFS, NULL, NULL},
87 };
88 
verify_creat(unsigned int i)89 static void verify_creat(unsigned int i)
90 {
91 	if (tcases[i].setup != NULL)
92 		tcases[i].setup(i);
93 
94 	TEST(creat(tcases[i].fname, tcases[i].mode));
95 
96 	if (tcases[i].cleanup != NULL)
97 		tcases[i].cleanup();
98 
99 	if (TST_RET != -1) {
100 		tst_res(TFAIL, "call succeeded unexpectedly");
101 		return;
102 	}
103 
104 	if (TST_ERR == tcases[i].error) {
105 		tst_res(TPASS | TTERRNO, "got expected failure");
106 		return;
107 	}
108 
109 	tst_res(TFAIL | TTERRNO, "expected %s",
110 	         tst_strerrno(tcases[i].error));
111 }
112 
113 
setup(void)114 static void setup(void)
115 {
116 	ltpuser = SAFE_GETPWNAM("nobody");
117 
118 	SAFE_MKDIR(TEST_FILE, MODE2);
119 
120 	memset(long_name, 'a', PATH_MAX+1);
121 
122 	SAFE_TOUCH("file1", MODE1, NULL);
123 
124 	SAFE_MKDIR("dir6", MODE2);
125 
126 	SAFE_SYMLINK(TEST7_FILE, "test_file_eloop2");
127 	SAFE_SYMLINK("test_file_eloop2", TEST7_FILE);
128 }
129 
130 #if !defined(UCLINUX)
bad_addr_setup(int i)131 static void bad_addr_setup(int i)
132 {
133 	if (tcases[i].fname)
134 		return;
135 
136 	tcases[i].fname = SAFE_MMAP(0, 1, PROT_NONE,
137 	                            MAP_PRIVATE | MAP_ANONYMOUS, 0, 0);
138 }
139 #endif
140 
test6_setup(void)141 static void test6_setup(void)
142 {
143 	SAFE_SETEUID(ltpuser->pw_uid);
144 }
145 
test6_cleanup(void)146 static void test6_cleanup(void)
147 {
148 	SAFE_SETEUID(0);
149 }
150 
151 static struct tst_test test = {
152 	.tcnt = ARRAY_SIZE(tcases),
153 	.test = verify_creat,
154 	.needs_root = 1,
155 	.needs_rofs = 1,
156 	.mntpoint = "mntpoint",
157 	.setup = setup,
158 };
159