xref: /aosp_15_r20/external/ltp/testcases/kernel/syscalls/umount/umount03.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  * Copyright (c) Linux Test Project, 2002-2023
5*49cdfc7eSAndroid Build Coastguard Worker  * Author: Nirmala Devi Dhanasekar <[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 umount(2) returns -1 and sets errno to EPERM if the user
12*49cdfc7eSAndroid Build Coastguard Worker  * is not the super-user.
13*49cdfc7eSAndroid Build Coastguard Worker  */
14*49cdfc7eSAndroid Build Coastguard Worker 
15*49cdfc7eSAndroid Build Coastguard Worker #include <pwd.h>
16*49cdfc7eSAndroid Build Coastguard Worker #include <sys/mount.h>
17*49cdfc7eSAndroid Build Coastguard Worker #include "tst_test.h"
18*49cdfc7eSAndroid Build Coastguard Worker 
19*49cdfc7eSAndroid Build Coastguard Worker #define MNTPOINT	"mntpoint"
20*49cdfc7eSAndroid Build Coastguard Worker 
21*49cdfc7eSAndroid Build Coastguard Worker static int mount_flag;
22*49cdfc7eSAndroid Build Coastguard Worker 
verify_umount(void)23*49cdfc7eSAndroid Build Coastguard Worker static void verify_umount(void)
24*49cdfc7eSAndroid Build Coastguard Worker {
25*49cdfc7eSAndroid Build Coastguard Worker 	TST_EXP_FAIL(umount(MNTPOINT), EPERM);
26*49cdfc7eSAndroid Build Coastguard Worker }
27*49cdfc7eSAndroid Build Coastguard Worker 
setup(void)28*49cdfc7eSAndroid Build Coastguard Worker static void setup(void)
29*49cdfc7eSAndroid Build Coastguard Worker {
30*49cdfc7eSAndroid Build Coastguard Worker 	struct passwd *pw;
31*49cdfc7eSAndroid Build Coastguard Worker 
32*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_MKDIR(MNTPOINT, 0775);
33*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_MOUNT(tst_device->dev, MNTPOINT, tst_device->fs_type, 0, NULL);
34*49cdfc7eSAndroid Build Coastguard Worker 	mount_flag = 1;
35*49cdfc7eSAndroid Build Coastguard Worker 
36*49cdfc7eSAndroid Build Coastguard Worker 	pw = SAFE_GETPWNAM("nobody");
37*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_SETEUID(pw->pw_uid);
38*49cdfc7eSAndroid Build Coastguard Worker }
39*49cdfc7eSAndroid Build Coastguard Worker 
cleanup(void)40*49cdfc7eSAndroid Build Coastguard Worker static void cleanup(void)
41*49cdfc7eSAndroid Build Coastguard Worker {
42*49cdfc7eSAndroid Build Coastguard Worker 	if (seteuid(0))
43*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TWARN | TERRNO, "seteuid(0) Failed");
44*49cdfc7eSAndroid Build Coastguard Worker 
45*49cdfc7eSAndroid Build Coastguard Worker 	if (mount_flag)
46*49cdfc7eSAndroid Build Coastguard Worker 		tst_umount(MNTPOINT);
47*49cdfc7eSAndroid Build Coastguard Worker }
48*49cdfc7eSAndroid Build Coastguard Worker 
49*49cdfc7eSAndroid Build Coastguard Worker static struct tst_test test = {
50*49cdfc7eSAndroid Build Coastguard Worker 	.needs_root = 1,
51*49cdfc7eSAndroid Build Coastguard Worker 	.format_device = 1,
52*49cdfc7eSAndroid Build Coastguard Worker 	.setup = setup,
53*49cdfc7eSAndroid Build Coastguard Worker 	.cleanup = cleanup,
54*49cdfc7eSAndroid Build Coastguard Worker 	.test_all = verify_umount,
55*49cdfc7eSAndroid Build Coastguard Worker };
56