1// SPDX-License-Identifier: GPL-2.0-later
2/*
3 * Copyright (c) International Business Machines  Corp., 2001
4 * Ported by John George
5 * Copyright (c) 2023 SUSE LLC Avinesh Kumar <avinesh.kumar@suse.com>
6 */
7
8/*\
9 * [Description]
10 *
11 * Verify that setreuid(2) syscall fails with EPERM errno when the calling
12 * process is not privileged and a change other than
13 * (i) swapping the effective user ID with the real user ID, or
14 * (ii) setting one to the value of the other or
15 * (iii) setting the effective user ID to the value of the saved set-user-ID
16 * was specified.
17 */
18
19#include <pwd.h>
20#include "tst_test.h"
21#include "tst_uid.h"
22#include "compat_tst_16.h"
23
24static struct passwd *ltpuser;
25static uid_t other_uid;
26
27static void setup(void)
28{
29	tst_get_uids(&other_uid, 0, 1);
30
31	UID16_CHECK(other_uid, setreuid);
32
33	ltpuser = SAFE_GETPWNAM("nobody");
34	SAFE_SETUID(ltpuser->pw_uid);
35}
36
37static void run(void)
38{
39
40	TST_EXP_FAIL(SETREUID(-1, other_uid), EPERM,
41				"setreuid(%d, %d)", -1, other_uid);
42	TST_EXP_FAIL(SETREUID(other_uid, -1), EPERM,
43				"setreuid(%d, %d)", other_uid, -1);
44	TST_EXP_FAIL(SETREUID(other_uid, other_uid), EPERM,
45				"setreuid(%d, %d)", other_uid, other_uid);
46}
47
48static struct tst_test test = {
49	.setup = setup,
50	.test_all = run,
51	.needs_root = 1
52};
53