1f08c3bdfSopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
2f08c3bdfSopenharmony_ci/* Copyright (c) International Business Machines Corp., 2001
3f08c3bdfSopenharmony_ci */
4f08c3bdfSopenharmony_ci
5f08c3bdfSopenharmony_ci/*
6f08c3bdfSopenharmony_ci * Verify that user cannot create a directory inside directory owned by another
7f08c3bdfSopenharmony_ci * user with restrictive permissions and that the errno is set to EACCESS.
8f08c3bdfSopenharmony_ci */
9f08c3bdfSopenharmony_ci
10f08c3bdfSopenharmony_ci#include <errno.h>
11f08c3bdfSopenharmony_ci#include <sys/types.h>
12f08c3bdfSopenharmony_ci#include <pwd.h>
13f08c3bdfSopenharmony_ci#include "tst_test.h"
14f08c3bdfSopenharmony_ci#include "tst_uid.h"
15f08c3bdfSopenharmony_ci
16f08c3bdfSopenharmony_ci#define TESTDIR	 "testdir"
17f08c3bdfSopenharmony_ci#define TESTSUBDIR "testdir/testdir"
18f08c3bdfSopenharmony_ci
19f08c3bdfSopenharmony_cistatic void verify_mkdir(void)
20f08c3bdfSopenharmony_ci{
21f08c3bdfSopenharmony_ci	if (mkdir(TESTSUBDIR, 0777) != -1) {
22f08c3bdfSopenharmony_ci		tst_res(TFAIL, "mkdir(%s, %#o) succeeded unexpectedly",
23f08c3bdfSopenharmony_ci			TESTSUBDIR, 0777);
24f08c3bdfSopenharmony_ci		return;
25f08c3bdfSopenharmony_ci	}
26f08c3bdfSopenharmony_ci
27f08c3bdfSopenharmony_ci	if (errno != EACCES) {
28f08c3bdfSopenharmony_ci		tst_res(TFAIL | TERRNO, "Expected EACCES got");
29f08c3bdfSopenharmony_ci		return;
30f08c3bdfSopenharmony_ci	}
31f08c3bdfSopenharmony_ci
32f08c3bdfSopenharmony_ci	tst_res(TPASS | TERRNO, "mkdir() failed expectedly");
33f08c3bdfSopenharmony_ci}
34f08c3bdfSopenharmony_ci
35f08c3bdfSopenharmony_cistatic void setup(void)
36f08c3bdfSopenharmony_ci{
37f08c3bdfSopenharmony_ci	uid_t test_users[2];
38f08c3bdfSopenharmony_ci
39f08c3bdfSopenharmony_ci	tst_get_uids(test_users, 0, 2);
40f08c3bdfSopenharmony_ci
41f08c3bdfSopenharmony_ci	SAFE_MKDIR(TESTDIR, 0700);
42f08c3bdfSopenharmony_ci	SAFE_CHOWN(TESTDIR, test_users[0], getgid());
43f08c3bdfSopenharmony_ci
44f08c3bdfSopenharmony_ci	SAFE_SETREUID(test_users[1], test_users[1]);
45f08c3bdfSopenharmony_ci}
46f08c3bdfSopenharmony_ci
47f08c3bdfSopenharmony_cistatic struct tst_test test = {
48f08c3bdfSopenharmony_ci	.test_all = verify_mkdir,
49f08c3bdfSopenharmony_ci	.needs_tmpdir = 1,
50f08c3bdfSopenharmony_ci	.needs_root = 1,
51f08c3bdfSopenharmony_ci	.setup = setup,
52f08c3bdfSopenharmony_ci};
53