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) 2014 Cyril Hrubis <chrubis@suse.cz>
6 * Copyright (c) 2021 Xie Ziyao <xieziyao@huawei.com>
7 */
8
9 /*\
10 * [Description]
11 *
12 * Verify that:
13 *
14 * 1. Chown() returns -1 and sets errno to EPERM if the effective user id
15 * of process does not match the owner of the file and the process is not
16 * super user.
17 * 2. Chown() returns -1 and sets errno to EACCES if search permission is
18 * denied on a component of the path prefix.
19 * 3. Chown() returns -1 and sets errno to EFAULT if pathname points outside
20 * user's accessible address space.
21 * 4. Chown() returns -1 and sets errno to ENAMETOOLONG if the pathname
22 * component is too long.
23 * 5. Chown() returns -1 and sets errno to ENOENT if the specified file does
24 * not exists.
25 * 6. Chown() returns -1 and sets errno to ENOTDIR if the directory component
26 * in pathname is not a directory.
27 * 7. Chown() returns -1 and sets errno to ELOOP if too many symbolic links
28 * were encountered in resolving pathname.
29 * 8. Chown() returns -1 and sets errno to EROFS if the named file resides on
30 * a read-only filesystem.
31 */
32
33 #include <pwd.h>
34
35 #include "tst_test.h"
36 #include "compat_tst_16.h"
37 #include "tst_safe_macros.h"
38
39 #define MODE 0666
40 #define MODE_RWX (S_IRWXU|S_IRWXG|S_IRWXO)
41 #define FILE_MODE (S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH)
42 #define DIR_MODE (S_IRUSR|S_IWUSR|S_IXUSR|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH)
43
44 #define MNT_POINT "mntpoint"
45 #define DIR_TEMP "testdir_1"
46 #define TEST_FILE1 "tfile_1"
47 #define TEST_FILE2 "testdir_1/tfile_2"
48 #define TEST_FILE3 "t_file/tfile_3"
49 #define TEST_FILE4 "test_eloop1"
50 #define TEST_FILE5 "mntpoint"
51
52 static char long_path[PATH_MAX + 2] = {[0 ... PATH_MAX + 1] = 'a'};
53
54 static struct test_case_t {
55 char *pathname;
56 int exp_errno;
57 char *desc;
58 } tc[] = {
59 {TEST_FILE1, EPERM, "without permissions"},
60 {TEST_FILE2, EACCES, "without full permissions of the path prefix"},
61 {(char *)-1, EFAULT, "with unaccessible pathname points"},
62 {long_path, ENAMETOOLONG, "when pathname is too long"},
63 {"", ENOENT, "when file does not exist"},
64 {TEST_FILE3, ENOTDIR, "when the path prefix is not a directory"},
65 {TEST_FILE4, ELOOP, "with too many symbolic links"},
66 {TEST_FILE5, EROFS, "when the named file resides on a read-only filesystem"}
67 };
68
run(unsigned int i)69 static void run(unsigned int i)
70 {
71 uid_t uid;
72 gid_t gid;
73
74 UID16_CHECK((uid = geteuid()), "chown");
75 GID16_CHECK((gid = getegid()), "chown");
76
77 TST_EXP_FAIL(CHOWN(tc[i].pathname, uid, gid), tc[i].exp_errno,
78 "chown() %s", tc[i].desc);
79 }
80
setup(void)81 static void setup(void)
82 {
83 struct passwd *ltpuser;
84
85 tc[2].pathname = tst_get_bad_addr(NULL);
86
87 SAFE_SYMLINK("test_eloop1", "test_eloop2");
88 SAFE_SYMLINK("test_eloop2", "test_eloop1");
89
90 SAFE_SETEUID(0);
91 SAFE_TOUCH("t_file", MODE_RWX, NULL);
92 SAFE_TOUCH(TEST_FILE1, MODE, NULL);
93 SAFE_MKDIR(DIR_TEMP, S_IRWXU);
94 SAFE_TOUCH(TEST_FILE2, MODE, NULL);
95
96 ltpuser = SAFE_GETPWNAM("nobody");
97 SAFE_SETEUID(ltpuser->pw_uid);
98 }
99
100 static struct tst_test test = {
101 .needs_root = 1,
102 .needs_rofs = 1,
103 .mntpoint = MNT_POINT,
104 .tcnt = ARRAY_SIZE(tc),
105 .test = run,
106 .setup = setup,
107 };
108