xref: /aosp_15_r20/external/ltp/testcases/kernel/syscalls/umount/umount03.c (revision 49cdfc7efb34551c7342be41a7384b9c40d7cab7)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Copyright (c) Wipro Technologies Ltd, 2002.  All Rights Reserved.
4  * Copyright (c) Linux Test Project, 2002-2023
5  * Author: Nirmala Devi Dhanasekar <[email protected]>
6  */
7 
8 /*\
9  * [Description]
10  *
11  * Verify that umount(2) returns -1 and sets errno to EPERM if the user
12  * is not the super-user.
13  */
14 
15 #include <pwd.h>
16 #include <sys/mount.h>
17 #include "tst_test.h"
18 
19 #define MNTPOINT	"mntpoint"
20 
21 static int mount_flag;
22 
verify_umount(void)23 static void verify_umount(void)
24 {
25 	TST_EXP_FAIL(umount(MNTPOINT), EPERM);
26 }
27 
setup(void)28 static void setup(void)
29 {
30 	struct passwd *pw;
31 
32 	SAFE_MKDIR(MNTPOINT, 0775);
33 	SAFE_MOUNT(tst_device->dev, MNTPOINT, tst_device->fs_type, 0, NULL);
34 	mount_flag = 1;
35 
36 	pw = SAFE_GETPWNAM("nobody");
37 	SAFE_SETEUID(pw->pw_uid);
38 }
39 
cleanup(void)40 static void cleanup(void)
41 {
42 	if (seteuid(0))
43 		tst_res(TWARN | TERRNO, "seteuid(0) Failed");
44 
45 	if (mount_flag)
46 		tst_umount(MNTPOINT);
47 }
48 
49 static struct tst_test test = {
50 	.needs_root = 1,
51 	.format_device = 1,
52 	.setup = setup,
53 	.cleanup = cleanup,
54 	.test_all = verify_umount,
55 };
56