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