1f08c3bdfSopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
2f08c3bdfSopenharmony_ci/* Copyright (c) Kerlabs 2008.
3f08c3bdfSopenharmony_ci * Copyright (c) International Business Machines  Corp., 2008
4f08c3bdfSopenharmony_ci * Copyright (c) 2022 SUSE LLC Avinesh Kumar <avinesh.kumar@suse.com>
5f08c3bdfSopenharmony_ci */
6f08c3bdfSopenharmony_ci
7f08c3bdfSopenharmony_ci/*\
8f08c3bdfSopenharmony_ci * [Description]
9f08c3bdfSopenharmony_ci *
10f08c3bdfSopenharmony_ci * Verify that setresuid() behaves correctly with file permissions.
11f08c3bdfSopenharmony_ci * The test creates a file as ROOT with permissions 0644, does a setresuid
12f08c3bdfSopenharmony_ci * to change euid to a non-root user and tries to open the file with RDWR
13f08c3bdfSopenharmony_ci * permissions, which should fail with EACCES errno.
14f08c3bdfSopenharmony_ci * The same test is done in a fork also to check that child process also
15f08c3bdfSopenharmony_ci * inherits new euid and open fails with EACCES.
16f08c3bdfSopenharmony_ci * Test verifies the successful open action after reverting the euid back
17f08c3bdfSopenharmony_ci * ROOT user.
18f08c3bdfSopenharmony_ci */
19f08c3bdfSopenharmony_ci
20f08c3bdfSopenharmony_ci#include <sys/wait.h>
21f08c3bdfSopenharmony_ci#include <pwd.h>
22f08c3bdfSopenharmony_ci#include "tst_test.h"
23f08c3bdfSopenharmony_ci#include "compat_tst_16.h"
24f08c3bdfSopenharmony_ci
25f08c3bdfSopenharmony_ci#define TEMP_FILE	"testfile"
26f08c3bdfSopenharmony_cistatic char nobody_uid[] = "nobody";
27f08c3bdfSopenharmony_cistatic struct passwd *ltpuser;
28f08c3bdfSopenharmony_cistatic int fd = -1;
29f08c3bdfSopenharmony_ci
30f08c3bdfSopenharmony_cistatic void setup(void)
31f08c3bdfSopenharmony_ci{
32f08c3bdfSopenharmony_ci	ltpuser = SAFE_GETPWNAM(nobody_uid);
33f08c3bdfSopenharmony_ci	UID16_CHECK(ltpuser->pw_uid, "setresuid");
34f08c3bdfSopenharmony_ci
35f08c3bdfSopenharmony_ci	fd = SAFE_OPEN(TEMP_FILE, O_CREAT | O_RDWR, 0644);
36f08c3bdfSopenharmony_ci}
37f08c3bdfSopenharmony_ci
38f08c3bdfSopenharmony_cistatic void run(void)
39f08c3bdfSopenharmony_ci{
40f08c3bdfSopenharmony_ci	pid_t pid;
41f08c3bdfSopenharmony_ci	int status;
42f08c3bdfSopenharmony_ci
43f08c3bdfSopenharmony_ci	TST_EXP_PASS_SILENT(SETRESUID(-1, ltpuser->pw_uid, -1));
44f08c3bdfSopenharmony_ci	TST_EXP_FAIL2(open(TEMP_FILE, O_RDWR), EACCES);
45f08c3bdfSopenharmony_ci
46f08c3bdfSopenharmony_ci	pid = SAFE_FORK();
47f08c3bdfSopenharmony_ci	if (!pid) {
48f08c3bdfSopenharmony_ci		TST_EXP_FAIL2(open(TEMP_FILE, O_RDWR), EACCES);
49f08c3bdfSopenharmony_ci		return;
50f08c3bdfSopenharmony_ci	}
51f08c3bdfSopenharmony_ci	SAFE_WAITPID(pid, &status, 0);
52f08c3bdfSopenharmony_ci	if (WIFEXITED(status) && WEXITSTATUS(status) != 0)
53f08c3bdfSopenharmony_ci		tst_res(TFAIL, "child process exited with status: %d", status);
54f08c3bdfSopenharmony_ci
55f08c3bdfSopenharmony_ci	TST_EXP_PASS_SILENT(SETRESUID(-1, 0, -1));
56f08c3bdfSopenharmony_ci	TST_EXP_FD(open(TEMP_FILE, O_RDWR));
57f08c3bdfSopenharmony_ci	SAFE_CLOSE(TST_RET);
58f08c3bdfSopenharmony_ci}
59f08c3bdfSopenharmony_ci
60f08c3bdfSopenharmony_cistatic void cleanup(void)
61f08c3bdfSopenharmony_ci{
62f08c3bdfSopenharmony_ci	if (fd > 0)
63f08c3bdfSopenharmony_ci		SAFE_CLOSE(fd);
64f08c3bdfSopenharmony_ci}
65f08c3bdfSopenharmony_ci
66f08c3bdfSopenharmony_cistatic struct tst_test test = {
67f08c3bdfSopenharmony_ci	.setup = setup,
68f08c3bdfSopenharmony_ci	.test_all = run,
69f08c3bdfSopenharmony_ci	.cleanup = cleanup,
70f08c3bdfSopenharmony_ci	.needs_root = 1,
71f08c3bdfSopenharmony_ci	.needs_tmpdir = 1,
72f08c3bdfSopenharmony_ci	.forks_child = 1
73f08c3bdfSopenharmony_ci};
74