1f08c3bdfSopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
2f08c3bdfSopenharmony_ci/*
3f08c3bdfSopenharmony_ci * Copyright (c) International Business Machines  Corp., 2001
4f08c3bdfSopenharmony_ci *  Ported by Wayne Boyer
5f08c3bdfSopenharmony_ci */
6f08c3bdfSopenharmony_ci
7f08c3bdfSopenharmony_ci/*\
8f08c3bdfSopenharmony_ci * [Description]
9f08c3bdfSopenharmony_ci *
10f08c3bdfSopenharmony_ci * Test if setgid() system call sets errno to EPERM correctly.
11f08c3bdfSopenharmony_ci *
12f08c3bdfSopenharmony_ci * [Algorithm]
13f08c3bdfSopenharmony_ci *
14f08c3bdfSopenharmony_ci * Call setgid() to set the gid to that of root. Run this test as
15f08c3bdfSopenharmony_ci * nobody, and expect to get EPERM.
16f08c3bdfSopenharmony_ci */
17f08c3bdfSopenharmony_ci
18f08c3bdfSopenharmony_ci#include <pwd.h>
19f08c3bdfSopenharmony_ci#include "tst_test.h"
20f08c3bdfSopenharmony_ci#include "compat_tst_16.h"
21f08c3bdfSopenharmony_ci
22f08c3bdfSopenharmony_cistatic void run(void)
23f08c3bdfSopenharmony_ci{
24f08c3bdfSopenharmony_ci	struct passwd *rootpwent;
25f08c3bdfSopenharmony_ci
26f08c3bdfSopenharmony_ci	rootpwent = SAFE_GETPWNAM("root");
27f08c3bdfSopenharmony_ci
28f08c3bdfSopenharmony_ci	GID16_CHECK(rootpwent->pw_gid, setgid);
29f08c3bdfSopenharmony_ci
30f08c3bdfSopenharmony_ci	TST_EXP_FAIL(SETGID(rootpwent->pw_gid), EPERM);
31f08c3bdfSopenharmony_ci}
32f08c3bdfSopenharmony_ci
33f08c3bdfSopenharmony_cistatic void setup(void)
34f08c3bdfSopenharmony_ci{
35f08c3bdfSopenharmony_ci	struct passwd *nobody = SAFE_GETPWNAM("nobody");
36f08c3bdfSopenharmony_ci
37f08c3bdfSopenharmony_ci	SAFE_SETGID(nobody->pw_gid);
38f08c3bdfSopenharmony_ci	SAFE_SETUID(nobody->pw_uid);
39f08c3bdfSopenharmony_ci}
40f08c3bdfSopenharmony_ci
41f08c3bdfSopenharmony_cistatic struct tst_test test = {
42f08c3bdfSopenharmony_ci	.needs_root = 1,
43f08c3bdfSopenharmony_ci	.setup = setup,
44f08c3bdfSopenharmony_ci	.test_all = run,
45f08c3bdfSopenharmony_ci};
46