xref: /aosp_15_r20/external/ltp/testcases/kernel/syscalls/setresuid/setresuid02.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) International Business Machines  Corp., 2001
4*49cdfc7eSAndroid Build Coastguard Worker  *	07/2001 ported by John George
5*49cdfc7eSAndroid Build Coastguard Worker  * Copyright (C) 2021 SUSE LLC <[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  * Test that a non-root user can change the real, effective and saved uid
12*49cdfc7eSAndroid Build Coastguard Worker  * values through the setresuid system call.
13*49cdfc7eSAndroid Build Coastguard Worker  */
14*49cdfc7eSAndroid Build Coastguard Worker 
15*49cdfc7eSAndroid Build Coastguard Worker #define _GNU_SOURCE 1
16*49cdfc7eSAndroid Build Coastguard Worker #include <sys/types.h>
17*49cdfc7eSAndroid Build Coastguard Worker #include <pwd.h>
18*49cdfc7eSAndroid Build Coastguard Worker 
19*49cdfc7eSAndroid Build Coastguard Worker #include "tst_test.h"
20*49cdfc7eSAndroid Build Coastguard Worker #include "tst_uid.h"
21*49cdfc7eSAndroid Build Coastguard Worker #include "compat_tst_16.h"
22*49cdfc7eSAndroid Build Coastguard Worker 
23*49cdfc7eSAndroid Build Coastguard Worker static uid_t nobody_uid, other_uid, neg_one = -1;
24*49cdfc7eSAndroid Build Coastguard Worker 
25*49cdfc7eSAndroid Build Coastguard Worker static struct test_data_t {
26*49cdfc7eSAndroid Build Coastguard Worker 	uid_t *real_uid;
27*49cdfc7eSAndroid Build Coastguard Worker 	uid_t *eff_uid;
28*49cdfc7eSAndroid Build Coastguard Worker 	uid_t *sav_uid;
29*49cdfc7eSAndroid Build Coastguard Worker 	uid_t *exp_real_uid;
30*49cdfc7eSAndroid Build Coastguard Worker 	uid_t *exp_eff_uid;
31*49cdfc7eSAndroid Build Coastguard Worker 	uid_t *exp_sav_uid;
32*49cdfc7eSAndroid Build Coastguard Worker 	char *test_msg;
33*49cdfc7eSAndroid Build Coastguard Worker } test_data[] = {
34*49cdfc7eSAndroid Build Coastguard Worker 	{&neg_one, &neg_one, &other_uid, &nobody_uid, &other_uid, &other_uid,
35*49cdfc7eSAndroid Build Coastguard Worker 		"setresuid(-1, -1, other)"},
36*49cdfc7eSAndroid Build Coastguard Worker 	{&neg_one, &nobody_uid, &neg_one, &nobody_uid, &nobody_uid, &other_uid,
37*49cdfc7eSAndroid Build Coastguard Worker 		"setresuid(-1, nobody -1)"},
38*49cdfc7eSAndroid Build Coastguard Worker 	{&other_uid, &neg_one, &neg_one, &other_uid, &nobody_uid, &other_uid,
39*49cdfc7eSAndroid Build Coastguard Worker 		"setresuid(other, -1 -1)"},
40*49cdfc7eSAndroid Build Coastguard Worker 	/* Return to initial state */
41*49cdfc7eSAndroid Build Coastguard Worker 	{&nobody_uid, &other_uid, &nobody_uid, &nobody_uid, &other_uid,
42*49cdfc7eSAndroid Build Coastguard Worker 		&nobody_uid, "setresuid(nobody, other, nobody)"},
43*49cdfc7eSAndroid Build Coastguard Worker };
44*49cdfc7eSAndroid Build Coastguard Worker 
setup(void)45*49cdfc7eSAndroid Build Coastguard Worker static void setup(void)
46*49cdfc7eSAndroid Build Coastguard Worker {
47*49cdfc7eSAndroid Build Coastguard Worker 	uid_t test_users[2];
48*49cdfc7eSAndroid Build Coastguard Worker 	struct passwd *pw = SAFE_GETPWNAM("nobody");
49*49cdfc7eSAndroid Build Coastguard Worker 
50*49cdfc7eSAndroid Build Coastguard Worker 	nobody_uid = test_users[0] = pw->pw_uid;
51*49cdfc7eSAndroid Build Coastguard Worker 	tst_get_uids(test_users, 1, 2);
52*49cdfc7eSAndroid Build Coastguard Worker 	other_uid = test_users[1];
53*49cdfc7eSAndroid Build Coastguard Worker 
54*49cdfc7eSAndroid Build Coastguard Worker 	UID16_CHECK(nobody_uid, setresuid);
55*49cdfc7eSAndroid Build Coastguard Worker 	UID16_CHECK(other_uid, setresuid);
56*49cdfc7eSAndroid Build Coastguard Worker 
57*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_SETRESUID(nobody_uid, other_uid, nobody_uid);
58*49cdfc7eSAndroid Build Coastguard Worker }
59*49cdfc7eSAndroid Build Coastguard Worker 
run(unsigned int n)60*49cdfc7eSAndroid Build Coastguard Worker static void run(unsigned int n)
61*49cdfc7eSAndroid Build Coastguard Worker {
62*49cdfc7eSAndroid Build Coastguard Worker 	const struct test_data_t *tc = test_data + n;
63*49cdfc7eSAndroid Build Coastguard Worker 
64*49cdfc7eSAndroid Build Coastguard Worker 	TST_EXP_PASS_SILENT(SETRESUID(*tc->real_uid, *tc->eff_uid,
65*49cdfc7eSAndroid Build Coastguard Worker 		*tc->sav_uid), "%s", tc->test_msg);
66*49cdfc7eSAndroid Build Coastguard Worker 
67*49cdfc7eSAndroid Build Coastguard Worker 	if (!TST_PASS)
68*49cdfc7eSAndroid Build Coastguard Worker 		return;
69*49cdfc7eSAndroid Build Coastguard Worker 
70*49cdfc7eSAndroid Build Coastguard Worker 	if (tst_check_resuid(tc->test_msg, *tc->exp_real_uid,
71*49cdfc7eSAndroid Build Coastguard Worker 		*tc->exp_eff_uid, *tc->exp_sav_uid))
72*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TPASS, "%s works as expected", tc->test_msg);
73*49cdfc7eSAndroid Build Coastguard Worker }
74*49cdfc7eSAndroid Build Coastguard Worker 
75*49cdfc7eSAndroid Build Coastguard Worker static struct tst_test test = {
76*49cdfc7eSAndroid Build Coastguard Worker 	.test = run,
77*49cdfc7eSAndroid Build Coastguard Worker 	.tcnt = ARRAY_SIZE(test_data),
78*49cdfc7eSAndroid Build Coastguard Worker 	.setup = setup,
79*49cdfc7eSAndroid Build Coastguard Worker 	.needs_root = 1,
80*49cdfc7eSAndroid Build Coastguard Worker };
81