• 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 the setresuid system call sets the proper errno values when
12  * a non-root user attempts to change the real, effective or saved uid
13  * to a value other than one of the current uid, the current effective uid
14  * or the current saved uid.
15  */
16 
17 #include "tst_test.h"
18 #include "tst_uid.h"
19 #include "compat_tst_16.h"
20 
21 static uid_t root_uid, main_uid, other_uid, neg_one = -1;
22 
23 static struct test_data_t {
24 	uid_t *real_uid;
25 	uid_t *eff_uid;
26 	uid_t *sav_uid;
27 	int exp_errno;
28 	uid_t *exp_real_uid;
29 	uid_t *exp_eff_uid;
30 	uid_t *exp_sav_uid;
31 	char *test_msg;
32 } test_data[] = {
33 	{&other_uid, &neg_one, &neg_one, EPERM, &root_uid, &main_uid,
34 		&main_uid, "setresuid(other, -1, -1)"},
35 	{&neg_one, &neg_one, &other_uid, EPERM, &root_uid, &main_uid,
36 		&main_uid, "setresuid(-1, -1, other)"},
37 	{&neg_one, &other_uid, &neg_one, EPERM, &root_uid, &main_uid,
38 		&main_uid, "setresuid(-1, other, -1)"}
39 };
40 
setup(void)41 static void setup(void)
42 {
43 	uid_t test_users[2];
44 
45 	root_uid = getuid();
46 	tst_get_uids(test_users, 0, 2);
47 	main_uid = test_users[0];
48 	other_uid = test_users[1];
49 
50 	UID16_CHECK(root_uid, setresuid);
51 	UID16_CHECK(main_uid, setresuid);
52 	UID16_CHECK(other_uid, setresuid);
53 
54 	SAFE_SETRESUID(root_uid, main_uid, main_uid);
55 }
56 
run(unsigned int n)57 static void run(unsigned int n)
58 {
59 	const struct test_data_t *tc = test_data + n;
60 
61 	TST_EXP_FAIL(SETRESUID(*tc->real_uid, *tc->eff_uid, *tc->sav_uid),
62 		tc->exp_errno, "%s", tc->test_msg);
63 
64 	if (!TST_PASS)
65 		return;
66 
67 	tst_check_resuid(tc->test_msg, *tc->exp_real_uid, *tc->exp_eff_uid,
68 		*tc->exp_sav_uid);
69 }
70 
71 static struct tst_test test = {
72 	.test = run,
73 	.tcnt = ARRAY_SIZE(test_data),
74 	.setup = setup,
75 	.needs_root = 1,
76 };
77