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