xref: /aosp_15_r20/external/ltp/testcases/kernel/syscalls/mount/mount04.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) Wipro Technologies Ltd, 2002.  All Rights Reserved.
4*49cdfc7eSAndroid Build Coastguard Worker  *               Nirmala Devi Dhanasekar <[email protected]>
5*49cdfc7eSAndroid Build Coastguard Worker  * Copyright (C) 2023 SUSE LLC Andrea Cervesato <[email protected]>
6*49cdfc7eSAndroid Build Coastguard Worker  */
7*49cdfc7eSAndroid Build Coastguard Worker 
8*49cdfc7eSAndroid Build Coastguard Worker /*\
9*49cdfc7eSAndroid Build Coastguard Worker  * [Description]
10*49cdfc7eSAndroid Build Coastguard Worker  *
11*49cdfc7eSAndroid Build Coastguard Worker  * Verify that mount(2) returns -1 and sets errno to EPERM if the user
12*49cdfc7eSAndroid Build Coastguard Worker  * is not root.
13*49cdfc7eSAndroid Build Coastguard Worker  */
14*49cdfc7eSAndroid Build Coastguard Worker 
15*49cdfc7eSAndroid Build Coastguard Worker #include "tst_test.h"
16*49cdfc7eSAndroid Build Coastguard Worker #include <pwd.h>
17*49cdfc7eSAndroid Build Coastguard Worker #include <sys/mount.h>
18*49cdfc7eSAndroid Build Coastguard Worker 
19*49cdfc7eSAndroid Build Coastguard Worker #define MNTPOINT "mntpoint"
20*49cdfc7eSAndroid Build Coastguard Worker 
cleanup(void)21*49cdfc7eSAndroid Build Coastguard Worker static void cleanup(void)
22*49cdfc7eSAndroid Build Coastguard Worker {
23*49cdfc7eSAndroid Build Coastguard Worker 	if (tst_is_mounted(MNTPOINT))
24*49cdfc7eSAndroid Build Coastguard Worker 		SAFE_UMOUNT(MNTPOINT);
25*49cdfc7eSAndroid Build Coastguard Worker }
26*49cdfc7eSAndroid Build Coastguard Worker 
run(void)27*49cdfc7eSAndroid Build Coastguard Worker static void run(void)
28*49cdfc7eSAndroid Build Coastguard Worker {
29*49cdfc7eSAndroid Build Coastguard Worker 	struct passwd *ltpuser;
30*49cdfc7eSAndroid Build Coastguard Worker 
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 	TST_EXP_FAIL(
35*49cdfc7eSAndroid Build Coastguard Worker 		mount(tst_device->dev, MNTPOINT, tst_device->fs_type, 0, NULL),
36*49cdfc7eSAndroid Build Coastguard Worker 		EPERM,
37*49cdfc7eSAndroid Build Coastguard Worker 		"mount() failed expectedly for normal user"
38*49cdfc7eSAndroid Build Coastguard Worker 	);
39*49cdfc7eSAndroid Build Coastguard Worker 
40*49cdfc7eSAndroid Build Coastguard Worker 	if (tst_is_mounted(MNTPOINT))
41*49cdfc7eSAndroid Build Coastguard Worker 		SAFE_UMOUNT(MNTPOINT);
42*49cdfc7eSAndroid Build Coastguard Worker }
43*49cdfc7eSAndroid Build Coastguard Worker 
44*49cdfc7eSAndroid Build Coastguard Worker static struct tst_test test = {
45*49cdfc7eSAndroid Build Coastguard Worker 	.cleanup = cleanup,
46*49cdfc7eSAndroid Build Coastguard Worker 	.test_all = run,
47*49cdfc7eSAndroid Build Coastguard Worker 	.needs_root = 1,
48*49cdfc7eSAndroid Build Coastguard Worker 	.format_device = 1,
49*49cdfc7eSAndroid Build Coastguard Worker 	.mntpoint = MNTPOINT,
50*49cdfc7eSAndroid Build Coastguard Worker };
51