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