xref: /aosp_15_r20/external/ltp/testcases/kernel/syscalls/chroot/chroot04.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) Bull S.A. 2001
4*49cdfc7eSAndroid Build Coastguard Worker   *   Copyright (c) International Business Machines  Corp., 2001
5*49cdfc7eSAndroid Build Coastguard Worker   *
6*49cdfc7eSAndroid Build Coastguard Worker   *   04/2002 Ported by Jacky Malcles
7*49cdfc7eSAndroid Build Coastguard Worker   */
8*49cdfc7eSAndroid Build Coastguard Worker 
9*49cdfc7eSAndroid Build Coastguard Worker /*\
10*49cdfc7eSAndroid Build Coastguard Worker  * [Description]
11*49cdfc7eSAndroid Build Coastguard Worker  *
12*49cdfc7eSAndroid Build Coastguard Worker  * Testcase to check that chroot sets errno to EACCES.
13*49cdfc7eSAndroid Build Coastguard Worker  *
14*49cdfc7eSAndroid Build Coastguard Worker  * As a non-root user attempt to perform chroot() to a directory that the user
15*49cdfc7eSAndroid Build Coastguard Worker  * does not have a search permission for. The chroot() call should fail with
16*49cdfc7eSAndroid Build Coastguard Worker  * EACESS.
17*49cdfc7eSAndroid Build Coastguard Worker  */
18*49cdfc7eSAndroid Build Coastguard Worker 
19*49cdfc7eSAndroid Build Coastguard Worker #include <stdio.h>
20*49cdfc7eSAndroid Build Coastguard Worker #include <pwd.h>
21*49cdfc7eSAndroid Build Coastguard Worker #include "tst_test.h"
22*49cdfc7eSAndroid Build Coastguard Worker 
23*49cdfc7eSAndroid Build Coastguard Worker #define TEST_TMPDIR	"chroot04_tmpdir"
24*49cdfc7eSAndroid Build Coastguard Worker 
verify_chroot(void)25*49cdfc7eSAndroid Build Coastguard Worker static void verify_chroot(void)
26*49cdfc7eSAndroid Build Coastguard Worker {
27*49cdfc7eSAndroid Build Coastguard Worker 	TST_EXP_FAIL(chroot(TEST_TMPDIR), EACCES, "no search permission chroot()");
28*49cdfc7eSAndroid Build Coastguard Worker }
29*49cdfc7eSAndroid Build Coastguard Worker 
setup(void)30*49cdfc7eSAndroid Build Coastguard Worker static void setup(void)
31*49cdfc7eSAndroid Build Coastguard Worker {
32*49cdfc7eSAndroid Build Coastguard Worker 	struct passwd *ltpuser;
33*49cdfc7eSAndroid Build Coastguard Worker 
34*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_MKDIR(TEST_TMPDIR, 0222);
35*49cdfc7eSAndroid Build Coastguard Worker 
36*49cdfc7eSAndroid Build Coastguard Worker 	ltpuser = SAFE_GETPWNAM("nobody");
37*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_SETEUID(ltpuser->pw_uid);
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 	.setup = setup,
42*49cdfc7eSAndroid Build Coastguard Worker 	.test_all = verify_chroot,
43*49cdfc7eSAndroid Build Coastguard Worker 	.needs_root = 1,
44*49cdfc7eSAndroid Build Coastguard Worker 	.needs_tmpdir = 1,
45*49cdfc7eSAndroid Build Coastguard Worker };
46