1f08c3bdfSopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
2f08c3bdfSopenharmony_ci/*
3f08c3bdfSopenharmony_ci *   Copyright (c) International Business Machines  Corp., 2001
4f08c3bdfSopenharmony_ci */
5f08c3bdfSopenharmony_ci
6f08c3bdfSopenharmony_ci/*\
7f08c3bdfSopenharmony_ci * [Description]
8f08c3bdfSopenharmony_ci *
9f08c3bdfSopenharmony_ci * Testcase to check the whether chroot sets errno to EPERM.
10f08c3bdfSopenharmony_ci *
11f08c3bdfSopenharmony_ci * As a non-root user attempt to perform chroot() to a directory. The
12f08c3bdfSopenharmony_ci * chroot() call should fail with EPERM
13f08c3bdfSopenharmony_ci */
14f08c3bdfSopenharmony_ci
15f08c3bdfSopenharmony_ci#include <stdlib.h>
16f08c3bdfSopenharmony_ci#include <pwd.h>
17f08c3bdfSopenharmony_ci#include "tst_test.h"
18f08c3bdfSopenharmony_ci
19f08c3bdfSopenharmony_cistatic char *path;
20f08c3bdfSopenharmony_ci
21f08c3bdfSopenharmony_cistatic void verify_chroot(void)
22f08c3bdfSopenharmony_ci{
23f08c3bdfSopenharmony_ci	TST_EXP_FAIL(chroot(path), EPERM, "unprivileged chroot()");
24f08c3bdfSopenharmony_ci}
25f08c3bdfSopenharmony_ci
26f08c3bdfSopenharmony_cistatic void setup(void)
27f08c3bdfSopenharmony_ci{
28f08c3bdfSopenharmony_ci	struct passwd *ltpuser;
29f08c3bdfSopenharmony_ci
30f08c3bdfSopenharmony_ci	path = tst_get_tmpdir();
31f08c3bdfSopenharmony_ci	ltpuser = SAFE_GETPWNAM("nobody");
32f08c3bdfSopenharmony_ci	SAFE_SETEUID(ltpuser->pw_uid);
33f08c3bdfSopenharmony_ci}
34f08c3bdfSopenharmony_ci
35f08c3bdfSopenharmony_cistatic void cleanup(void)
36f08c3bdfSopenharmony_ci{
37f08c3bdfSopenharmony_ci	free(path);
38f08c3bdfSopenharmony_ci}
39f08c3bdfSopenharmony_ci
40f08c3bdfSopenharmony_cistatic struct tst_test test = {
41f08c3bdfSopenharmony_ci	.cleanup = cleanup,
42f08c3bdfSopenharmony_ci	.setup = setup,
43f08c3bdfSopenharmony_ci	.test_all = verify_chroot,
44f08c3bdfSopenharmony_ci	.needs_root = 1,
45f08c3bdfSopenharmony_ci	.needs_tmpdir = 1,
46f08c3bdfSopenharmony_ci};
47