xref: /aosp_15_r20/external/ltp/testcases/kernel/syscalls/creat/creat06.c (revision 49cdfc7efb34551c7342be41a7384b9c40d7cab7)
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 static void bad_addr_setup(int);
64 
65 static struct passwd *ltpuser;
66 static char long_name[PATH_MAX+2];
67 
68 static struct test_case_t {
69 	char *fname;
70 	int mode;
71 	int error;
72 	void (*setup)();
73 	void (*cleanup)(void);
74 } tcases[] = {
75 	{TEST_FILE, MODE1, EISDIR, NULL, NULL},
76 	{long_name, MODE1, ENAMETOOLONG, NULL, NULL},
77 	{NO_DIR, MODE1, ENOENT, NULL, NULL},
78 	{NOT_DIR, MODE1, ENOTDIR, NULL, NULL},
79 	{NULL, MODE1, EFAULT, bad_addr_setup, NULL},
80 	{TEST6_FILE, MODE1, EACCES, test6_setup, test6_cleanup},
81 	{TEST7_FILE, MODE1, ELOOP, NULL, NULL},
82 	{TEST8_FILE, MODE1, EROFS, NULL, NULL},
83 };
84 
verify_creat(unsigned int i)85 static void verify_creat(unsigned int i)
86 {
87 	if (tcases[i].setup != NULL)
88 		tcases[i].setup(i);
89 
90 	TEST(creat(tcases[i].fname, tcases[i].mode));
91 
92 	if (tcases[i].cleanup != NULL)
93 		tcases[i].cleanup();
94 
95 	if (TST_RET != -1) {
96 		tst_res(TFAIL, "call succeeded unexpectedly");
97 		return;
98 	}
99 
100 	if (TST_ERR == tcases[i].error) {
101 		tst_res(TPASS | TTERRNO, "got expected failure");
102 		return;
103 	}
104 
105 	tst_res(TFAIL | TTERRNO, "expected %s",
106 	         tst_strerrno(tcases[i].error));
107 }
108 
109 
setup(void)110 static void setup(void)
111 {
112 	ltpuser = SAFE_GETPWNAM("nobody");
113 
114 	SAFE_MKDIR(TEST_FILE, MODE2);
115 
116 	memset(long_name, 'a', PATH_MAX+1);
117 
118 	SAFE_TOUCH("file1", MODE1, NULL);
119 
120 	SAFE_MKDIR("dir6", MODE2);
121 
122 	SAFE_SYMLINK(TEST7_FILE, "test_file_eloop2");
123 	SAFE_SYMLINK("test_file_eloop2", TEST7_FILE);
124 }
125 
bad_addr_setup(int i)126 static void bad_addr_setup(int i)
127 {
128 	if (tcases[i].fname)
129 		return;
130 
131 	tcases[i].fname = SAFE_MMAP(0, 1, PROT_NONE,
132 	                            MAP_PRIVATE | MAP_ANONYMOUS, 0, 0);
133 }
134 
test6_setup(void)135 static void test6_setup(void)
136 {
137 	SAFE_SETEUID(ltpuser->pw_uid);
138 }
139 
test6_cleanup(void)140 static void test6_cleanup(void)
141 {
142 	SAFE_SETEUID(0);
143 }
144 
145 static struct tst_test test = {
146 	.tcnt = ARRAY_SIZE(tcases),
147 	.test = verify_creat,
148 	.needs_root = 1,
149 	.needs_rofs = 1,
150 	.mntpoint = "mntpoint",
151 	.setup = setup,
152 };
153