1f08c3bdfSopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
2f08c3bdfSopenharmony_ci/*
3f08c3bdfSopenharmony_ci * Copyright (c) International Business Machines  Corp., 2001
4f08c3bdfSopenharmony_ci * 07/2001 Ported by Wayne Boyer
5f08c3bdfSopenharmony_ci * Copyright (c) 2021 Xie Ziyao <xieziyao@huawei.com>
6f08c3bdfSopenharmony_ci */
7f08c3bdfSopenharmony_ci
8f08c3bdfSopenharmony_ci/*\
9f08c3bdfSopenharmony_ci * [Description]
10f08c3bdfSopenharmony_ci *
11f08c3bdfSopenharmony_ci * Verify that chown(2) invoked by super-user:
12f08c3bdfSopenharmony_ci *
13f08c3bdfSopenharmony_ci *  - clears setuid and setgid bits set on an executable file
14f08c3bdfSopenharmony_ci *  - preserves setgid bit set on a non-group-executable file
15f08c3bdfSopenharmony_ci */
16f08c3bdfSopenharmony_ci
17f08c3bdfSopenharmony_ci#include "tst_test.h"
18f08c3bdfSopenharmony_ci#include "compat_tst_16.h"
19f08c3bdfSopenharmony_ci#include "tst_safe_macros.h"
20f08c3bdfSopenharmony_ci
21f08c3bdfSopenharmony_ci#define NEW_PERMS1	(S_IFREG|S_IRWXU|S_IRWXG|S_ISUID|S_ISGID)
22f08c3bdfSopenharmony_ci#define NEW_PERMS2	(S_IFREG|S_IRWXU|S_ISGID)
23f08c3bdfSopenharmony_ci#define EXP_PERMS	(S_IFREG|S_IRWXU|S_IRWXG)
24f08c3bdfSopenharmony_ci#define TESTFILE1	"testfile1"
25f08c3bdfSopenharmony_ci#define TESTFILE2	"testfile2"
26f08c3bdfSopenharmony_ci
27f08c3bdfSopenharmony_cistruct test_case_t {
28f08c3bdfSopenharmony_ci	const char *filename;
29f08c3bdfSopenharmony_ci	mode_t set_mode;
30f08c3bdfSopenharmony_ci	mode_t exp_mode;
31f08c3bdfSopenharmony_ci} tc[] = {
32f08c3bdfSopenharmony_ci	{TESTFILE1, NEW_PERMS1, EXP_PERMS},
33f08c3bdfSopenharmony_ci	{TESTFILE2, NEW_PERMS2, NEW_PERMS2}
34f08c3bdfSopenharmony_ci};
35f08c3bdfSopenharmony_ci
36f08c3bdfSopenharmony_cistatic void run(unsigned int i)
37f08c3bdfSopenharmony_ci{
38f08c3bdfSopenharmony_ci	uid_t uid;
39f08c3bdfSopenharmony_ci	gid_t gid;
40f08c3bdfSopenharmony_ci
41f08c3bdfSopenharmony_ci	UID16_CHECK((uid = geteuid()), "chown");
42f08c3bdfSopenharmony_ci	GID16_CHECK((gid = getegid()), "chown");
43f08c3bdfSopenharmony_ci
44f08c3bdfSopenharmony_ci	SAFE_CHMOD(tc[i].filename, tc[i].set_mode);
45f08c3bdfSopenharmony_ci
46f08c3bdfSopenharmony_ci	TST_EXP_PASS(CHOWN(tc[i].filename, uid, gid), "chown(%s, %d, %d)",
47f08c3bdfSopenharmony_ci		     tc[i].filename, uid, gid);
48f08c3bdfSopenharmony_ci
49f08c3bdfSopenharmony_ci	struct stat stat_buf;
50f08c3bdfSopenharmony_ci	SAFE_STAT(tc[i].filename, &stat_buf);
51f08c3bdfSopenharmony_ci
52f08c3bdfSopenharmony_ci	if (stat_buf.st_uid != uid || stat_buf.st_gid != gid) {
53f08c3bdfSopenharmony_ci		tst_res(TFAIL, "%s: owner set to (uid=%d, gid=%d), expected (uid=%d, gid=%d)",
54f08c3bdfSopenharmony_ci			tc[i].filename, stat_buf.st_uid, stat_buf.st_gid, uid, gid);
55f08c3bdfSopenharmony_ci	}
56f08c3bdfSopenharmony_ci
57f08c3bdfSopenharmony_ci	if (stat_buf.st_mode != tc[i].exp_mode) {
58f08c3bdfSopenharmony_ci		tst_res(TFAIL, "%s: wrong mode permissions %#o, expected %#o",
59f08c3bdfSopenharmony_ci			tc[i].filename, stat_buf.st_mode, tc[i].exp_mode);
60f08c3bdfSopenharmony_ci	}
61f08c3bdfSopenharmony_ci}
62f08c3bdfSopenharmony_ci
63f08c3bdfSopenharmony_cistatic void setup(void)
64f08c3bdfSopenharmony_ci{
65f08c3bdfSopenharmony_ci	unsigned int i;
66f08c3bdfSopenharmony_ci
67f08c3bdfSopenharmony_ci	for (i = 0; i < ARRAY_SIZE(tc); i++)
68f08c3bdfSopenharmony_ci		SAFE_TOUCH(tc[i].filename, tc[i].set_mode, NULL);
69f08c3bdfSopenharmony_ci}
70f08c3bdfSopenharmony_ci
71f08c3bdfSopenharmony_cistatic struct tst_test test = {
72f08c3bdfSopenharmony_ci	.tcnt = ARRAY_SIZE(tc),
73f08c3bdfSopenharmony_ci	.needs_root = 1,
74f08c3bdfSopenharmony_ci	.needs_tmpdir = 1,
75f08c3bdfSopenharmony_ci	.setup = setup,
76f08c3bdfSopenharmony_ci	.test = run,
77f08c3bdfSopenharmony_ci};
78