1// SPDX-License-Identifier: GPL-2.0-or-later
2/*
3 * Copyright (c) International Business Machines Corp., 2001
4 * Copyright (c) Linux Test Project, 2009-2022
5 */
6
7/*\
8 * [Description]
9 *
10 * This test will switch to nobody user for correct error code collection.
11 * Verify setuid returns errno EPERM when it switches to root_user.
12 */
13
14#include <errno.h>
15#include <pwd.h>
16#include <unistd.h>
17#include <sys/types.h>
18#include "tst_test.h"
19#include "compat_tst_16.h"
20
21#define ROOT_USER	0
22
23static void verify_setuid(void)
24{
25	TST_EXP_FAIL(SETUID(ROOT_USER), EPERM);
26}
27
28static void setup(void)
29{
30	struct passwd *pw;
31	uid_t uid;
32
33	pw = SAFE_GETPWNAM("nobody");
34	uid = pw->pw_uid;
35
36	SAFE_SETUID(uid);
37
38	umask(0);
39}
40
41static struct tst_test test = {
42	.setup = setup,
43	.needs_root = 1,
44	.test_all =  verify_setuid,
45};
46