• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* Copyright (c) International Business Machines Corp., 2001
3  * Ported to LTP: Wayne Boyer
4  */
5 
6 /* DESCRIPTION
7  * This test will verify the mkdir(2) creates a new directory successfully and
8  * it is owned by the effective UID and GID of the process.
9  */
10 
11 #include <errno.h>
12 #include <sys/stat.h>
13 #include <sys/types.h>
14 #include <unistd.h>
15 #include <pwd.h>
16 #include "tst_test.h"
17 
18 #define PERMS		0777
19 #define TESTDIR		"testdir"
20 
verify_mkdir(void)21 static void verify_mkdir(void)
22 {
23 	struct stat buf;
24 
25 	TEST(mkdir(TESTDIR, PERMS));
26 	if (TST_RET == -1) {
27 		tst_res(TFAIL | TTERRNO, "mkdir() Failed");
28 		return;
29 	}
30 
31 	SAFE_STAT(TESTDIR, &buf);
32 
33 	if (buf.st_uid != geteuid()) {
34 		tst_res(TFAIL, "mkdir() FAILED to set owner ID "
35 			"as process's effective ID");
36 		return;
37 	}
38 
39 	if (buf.st_gid != getegid()) {
40 		tst_res(TFAIL, "mkdir() failed to set group ID "
41 			"as the process's group ID");
42 		return;
43 	}
44 
45 	tst_res(TPASS, "mkdir() functionality is correct");
46 
47 	SAFE_RMDIR(TESTDIR);
48 }
49 
setup(void)50 void setup(void)
51 {
52 	struct passwd *pw;
53 
54 	pw = SAFE_GETPWNAM("nobody");
55 
56 	SAFE_SETUID(pw->pw_uid);
57 }
58 
59 static struct tst_test test = {
60 	.test_all = verify_mkdir,
61 	.needs_tmpdir = 1,
62 	.needs_root = 1,
63 	.setup = setup,
64 };
65