• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Copyright (c) International Business Machines  Corp., 2001
4  * 07/2001 Ported by Wayne Boyer
5  * Copyright (c) 2021 Xie Ziyao <xieziyao@huawei.com>
6  */
7 
8 /*\
9  * [Description]
10  *
11  * Verify that:
12  *
13  * 1. fchown() returns -1 and sets errno to EPERM if the effective user id
14  *    of process does not match the owner of the file and the process is
15  *    not super user.
16  * 2. fchown() returns -1 and sets errno to EBADF if the file descriptor
17  *    of the specified file is not valid.
18  * 3. fchown() returns -1 and sets errno to EROFS if the named file resides
19  *    on a read-only file system.
20  */
21 
22 #include <pwd.h>
23 
24 #include "tst_test.h"
25 #include "compat_tst_16.h"
26 #include "tst_safe_macros.h"
27 
28 #define MNT_POINT	"mntpoint"
29 #define TEST_FILE	"tfile_1"
30 #define MODE		0666
31 #define DIR_MODE	(S_IRUSR|S_IWUSR|S_IXUSR|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH)
32 
33 static int fd1;
34 static int fd2 = -1;
35 static int fd3;
36 
37 static struct test_case_t {
38 	int *fd;
39 	int exp_errno;
40 } tc[] = {
41 	{&fd1, EPERM},
42 	{&fd2, EBADF},
43 	{&fd3, EROFS},
44 };
45 
setup(void)46 static void setup(void)
47 {
48 	struct passwd *ltpuser;
49 
50 	fd1 = SAFE_OPEN(TEST_FILE, O_RDWR | O_CREAT, MODE);
51 	fd3 = SAFE_OPEN(MNT_POINT, O_RDONLY);
52 
53 	ltpuser = SAFE_GETPWNAM("nobody");
54 	SAFE_SETEUID(ltpuser->pw_uid);
55 }
56 
run(unsigned int i)57 static void run(unsigned int i)
58 {
59 	uid_t uid;
60 	gid_t gid;
61 
62 	UID16_CHECK((uid = geteuid()), "fchown");
63 	GID16_CHECK((gid = getegid()), "fchown");
64 
65 	TST_EXP_FAIL(FCHOWN(*tc[i].fd, uid, gid), tc[i].exp_errno,
66 	             "fchown(%i, %i, %i)", *tc[i].fd, uid, gid);
67 }
68 
cleanup(void)69 static void cleanup(void)
70 {
71 	if (fd1 > 0)
72 		SAFE_CLOSE(fd1);
73 
74 	if (fd3 > 0)
75 		SAFE_CLOSE(fd3);
76 }
77 
78 static struct tst_test test = {
79 	.needs_root = 1,
80 	.needs_rofs = 1,
81 	.mntpoint = MNT_POINT,
82 	.tcnt = ARRAY_SIZE(tc),
83 	.test = run,
84 	.setup = setup,
85 	.cleanup = cleanup,
86 };
87