1// SPDX-License-Identifier: GPL-2.0-or-later
2/*
3 * Copyright (c) International Business Machines  Corp., 2001
4 * Ported by John George
5 * Copyright (c) 2022 SUSE LLC Avinesh Kumar <avinesh.kumar@suse.com>
6 */
7
8/*\
9 * [Description]
10 *
11 * Verify that root user can change the real and effective uid to an
12 * unprivileged user.
13 */
14
15#include <pwd.h>
16#include "tst_test.h"
17#include "compat_tst_16.h"
18
19static uid_t root_uid, nobody_uid;
20
21static void setup(void)
22{
23	struct passwd *nobody;
24
25	root_uid = getuid();
26	nobody = SAFE_GETPWNAM("nobody");
27	nobody_uid = nobody->pw_uid;
28
29	UID16_CHECK(nobody_uid, setreuid);
30	UID16_CHECK(root_uid, setreuid);
31}
32
33static void run(void)
34{
35	if (!SAFE_FORK()) {
36		TST_EXP_PASS(SETREUID(nobody_uid, nobody_uid));
37
38		TST_EXP_EQ_LI(GETUID(), nobody_uid);
39		TST_EXP_EQ_LI(GETEUID(), nobody_uid);
40	}
41	tst_reap_children();
42}
43
44static struct tst_test test = {
45	.setup = setup,
46	.test_all = run,
47	.needs_root = 1,
48	.forks_child = 1
49};
50