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