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