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