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:
12f08c3bdfSopenharmony_ci *
13f08c3bdfSopenharmony_ci * 1. fchown() returns -1 and sets errno to EPERM if the effective user id
14f08c3bdfSopenharmony_ci *    of process does not match the owner of the file and the process is
15f08c3bdfSopenharmony_ci *    not super user.
16f08c3bdfSopenharmony_ci * 2. fchown() returns -1 and sets errno to EBADF if the file descriptor
17f08c3bdfSopenharmony_ci *    of the specified file is not valid.
18f08c3bdfSopenharmony_ci * 3. fchown() returns -1 and sets errno to EROFS if the named file resides
19f08c3bdfSopenharmony_ci *    on a read-only file system.
20f08c3bdfSopenharmony_ci */
21f08c3bdfSopenharmony_ci
22f08c3bdfSopenharmony_ci#include <pwd.h>
23f08c3bdfSopenharmony_ci
24f08c3bdfSopenharmony_ci#include "tst_test.h"
25f08c3bdfSopenharmony_ci#include "compat_tst_16.h"
26f08c3bdfSopenharmony_ci#include "tst_safe_macros.h"
27f08c3bdfSopenharmony_ci
28f08c3bdfSopenharmony_ci#define MNT_POINT	"mntpoint"
29f08c3bdfSopenharmony_ci#define TEST_FILE	"tfile_1"
30f08c3bdfSopenharmony_ci#define MODE		0666
31f08c3bdfSopenharmony_ci#define DIR_MODE	(S_IRUSR|S_IWUSR|S_IXUSR|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH)
32f08c3bdfSopenharmony_ci
33f08c3bdfSopenharmony_cistatic int fd1;
34f08c3bdfSopenharmony_cistatic int fd2 = -1;
35f08c3bdfSopenharmony_cistatic int fd3;
36f08c3bdfSopenharmony_ci
37f08c3bdfSopenharmony_cistatic struct test_case_t {
38f08c3bdfSopenharmony_ci	int *fd;
39f08c3bdfSopenharmony_ci	int exp_errno;
40f08c3bdfSopenharmony_ci} tc[] = {
41f08c3bdfSopenharmony_ci	{&fd1, EPERM},
42f08c3bdfSopenharmony_ci	{&fd2, EBADF},
43f08c3bdfSopenharmony_ci	{&fd3, EROFS},
44f08c3bdfSopenharmony_ci};
45f08c3bdfSopenharmony_ci
46f08c3bdfSopenharmony_cistatic void setup(void)
47f08c3bdfSopenharmony_ci{
48f08c3bdfSopenharmony_ci	struct passwd *ltpuser;
49f08c3bdfSopenharmony_ci
50f08c3bdfSopenharmony_ci	fd1 = SAFE_OPEN(TEST_FILE, O_RDWR | O_CREAT, MODE);
51f08c3bdfSopenharmony_ci	fd3 = SAFE_OPEN(MNT_POINT, O_RDONLY);
52f08c3bdfSopenharmony_ci
53f08c3bdfSopenharmony_ci	ltpuser = SAFE_GETPWNAM("nobody");
54f08c3bdfSopenharmony_ci	SAFE_SETEUID(ltpuser->pw_uid);
55f08c3bdfSopenharmony_ci}
56f08c3bdfSopenharmony_ci
57f08c3bdfSopenharmony_cistatic void run(unsigned int i)
58f08c3bdfSopenharmony_ci{
59f08c3bdfSopenharmony_ci	uid_t uid;
60f08c3bdfSopenharmony_ci	gid_t gid;
61f08c3bdfSopenharmony_ci
62f08c3bdfSopenharmony_ci	UID16_CHECK((uid = geteuid()), "fchown");
63f08c3bdfSopenharmony_ci	GID16_CHECK((gid = getegid()), "fchown");
64f08c3bdfSopenharmony_ci
65f08c3bdfSopenharmony_ci	TST_EXP_FAIL(FCHOWN(*tc[i].fd, uid, gid), tc[i].exp_errno,
66f08c3bdfSopenharmony_ci	             "fchown(%i, %i, %i)", *tc[i].fd, uid, gid);
67f08c3bdfSopenharmony_ci}
68f08c3bdfSopenharmony_ci
69f08c3bdfSopenharmony_cistatic void cleanup(void)
70f08c3bdfSopenharmony_ci{
71f08c3bdfSopenharmony_ci	if (fd1 > 0)
72f08c3bdfSopenharmony_ci		SAFE_CLOSE(fd1);
73f08c3bdfSopenharmony_ci
74f08c3bdfSopenharmony_ci	if (fd3 > 0)
75f08c3bdfSopenharmony_ci		SAFE_CLOSE(fd3);
76f08c3bdfSopenharmony_ci}
77f08c3bdfSopenharmony_ci
78f08c3bdfSopenharmony_cistatic struct tst_test test = {
79f08c3bdfSopenharmony_ci	.needs_root = 1,
80f08c3bdfSopenharmony_ci	.needs_rofs = 1,
81f08c3bdfSopenharmony_ci	.mntpoint = MNT_POINT,
82f08c3bdfSopenharmony_ci	.tcnt = ARRAY_SIZE(tc),
83f08c3bdfSopenharmony_ci	.test = run,
84f08c3bdfSopenharmony_ci	.setup = setup,
85f08c3bdfSopenharmony_ci	.cleanup = cleanup,
86f08c3bdfSopenharmony_ci};
87