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