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