xref: /aosp_15_r20/external/ltp/testcases/kernel/syscalls/creat/creat04.c (revision 49cdfc7efb34551c7342be41a7384b9c40d7cab7)
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  */
6*49cdfc7eSAndroid Build Coastguard Worker 
7*49cdfc7eSAndroid Build Coastguard Worker /*
8*49cdfc7eSAndroid Build Coastguard Worker  * Testcase to check creat(2) fails with EACCES
9*49cdfc7eSAndroid Build Coastguard Worker  */
10*49cdfc7eSAndroid Build Coastguard Worker 
11*49cdfc7eSAndroid Build Coastguard Worker #include <stdio.h>
12*49cdfc7eSAndroid Build Coastguard Worker #include <errno.h>
13*49cdfc7eSAndroid Build Coastguard Worker #include <fcntl.h>
14*49cdfc7eSAndroid Build Coastguard Worker #include <pwd.h>
15*49cdfc7eSAndroid Build Coastguard Worker #include <sys/types.h>
16*49cdfc7eSAndroid Build Coastguard Worker #include <sys/stat.h>
17*49cdfc7eSAndroid Build Coastguard Worker 
18*49cdfc7eSAndroid Build Coastguard Worker #include "tst_test.h"
19*49cdfc7eSAndroid Build Coastguard Worker 
20*49cdfc7eSAndroid Build Coastguard Worker #define DIRNAME "testdir"
21*49cdfc7eSAndroid Build Coastguard Worker #define FILENAME DIRNAME"/file1"
22*49cdfc7eSAndroid Build Coastguard Worker 
23*49cdfc7eSAndroid Build Coastguard Worker static uid_t nobody_uid;
24*49cdfc7eSAndroid Build Coastguard Worker 
25*49cdfc7eSAndroid Build Coastguard Worker static struct tcase {
26*49cdfc7eSAndroid Build Coastguard Worker 	const char *fname;
27*49cdfc7eSAndroid Build Coastguard Worker } tcases[] = {
28*49cdfc7eSAndroid Build Coastguard Worker 	{DIRNAME "/file"},
29*49cdfc7eSAndroid Build Coastguard Worker 	{FILENAME}
30*49cdfc7eSAndroid Build Coastguard Worker };
31*49cdfc7eSAndroid Build Coastguard Worker 
child_fn(unsigned int i)32*49cdfc7eSAndroid Build Coastguard Worker static void child_fn(unsigned int i)
33*49cdfc7eSAndroid Build Coastguard Worker {
34*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_SETEUID(nobody_uid);
35*49cdfc7eSAndroid Build Coastguard Worker 
36*49cdfc7eSAndroid Build Coastguard Worker 	TEST(creat(tcases[i].fname, 0444));
37*49cdfc7eSAndroid Build Coastguard Worker 
38*49cdfc7eSAndroid Build Coastguard Worker 	if (TST_RET != -1) {
39*49cdfc7eSAndroid Build Coastguard Worker 		SAFE_UNLINK(tcases[i].fname);
40*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TFAIL, "call succeeded unexpectedly");
41*49cdfc7eSAndroid Build Coastguard Worker 		return;
42*49cdfc7eSAndroid Build Coastguard Worker 	}
43*49cdfc7eSAndroid Build Coastguard Worker 
44*49cdfc7eSAndroid Build Coastguard Worker 	if (TST_ERR != EACCES) {
45*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TFAIL | TTERRNO, "Expected EACCES");
46*49cdfc7eSAndroid Build Coastguard Worker 		return;
47*49cdfc7eSAndroid Build Coastguard Worker 	}
48*49cdfc7eSAndroid Build Coastguard Worker 
49*49cdfc7eSAndroid Build Coastguard Worker 	tst_res(TPASS, "call failed with EACCES as expected");
50*49cdfc7eSAndroid Build Coastguard Worker }
51*49cdfc7eSAndroid Build Coastguard Worker 
verify_creat(unsigned int i)52*49cdfc7eSAndroid Build Coastguard Worker static void verify_creat(unsigned int i)
53*49cdfc7eSAndroid Build Coastguard Worker {
54*49cdfc7eSAndroid Build Coastguard Worker 	if (SAFE_FORK() == 0)
55*49cdfc7eSAndroid Build Coastguard Worker 		child_fn(i);
56*49cdfc7eSAndroid Build Coastguard Worker }
57*49cdfc7eSAndroid Build Coastguard Worker 
setup(void)58*49cdfc7eSAndroid Build Coastguard Worker static void setup(void)
59*49cdfc7eSAndroid Build Coastguard Worker {
60*49cdfc7eSAndroid Build Coastguard Worker 	struct passwd *pw;
61*49cdfc7eSAndroid Build Coastguard Worker 	int fd;
62*49cdfc7eSAndroid Build Coastguard Worker 
63*49cdfc7eSAndroid Build Coastguard Worker 	pw = SAFE_GETPWNAM("nobody");
64*49cdfc7eSAndroid Build Coastguard Worker 	nobody_uid = pw->pw_uid;
65*49cdfc7eSAndroid Build Coastguard Worker 
66*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_MKDIR(DIRNAME, 0700);
67*49cdfc7eSAndroid Build Coastguard Worker 	fd = SAFE_OPEN(FILENAME, O_RDWR | O_CREAT, 0444);
68*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_CLOSE(fd);
69*49cdfc7eSAndroid Build Coastguard Worker }
70*49cdfc7eSAndroid Build Coastguard Worker 
71*49cdfc7eSAndroid Build Coastguard Worker static struct tst_test test = {
72*49cdfc7eSAndroid Build Coastguard Worker 	.tcnt = ARRAY_SIZE(tcases),
73*49cdfc7eSAndroid Build Coastguard Worker 	.test = verify_creat,
74*49cdfc7eSAndroid Build Coastguard Worker 	.needs_tmpdir = 1,
75*49cdfc7eSAndroid Build Coastguard Worker 	.needs_root = 1,
76*49cdfc7eSAndroid Build Coastguard Worker 	.forks_child = 1,
77*49cdfc7eSAndroid Build Coastguard Worker 	.setup = setup,
78*49cdfc7eSAndroid Build Coastguard Worker };
79