1f08c3bdfSopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later 2f08c3bdfSopenharmony_ci/* 3f08c3bdfSopenharmony_ci * Copyright (c) 2023 FUJITSU LIMITED. All rights reserved. 4f08c3bdfSopenharmony_ci * Copyright (c) Linux Test Project, 2003-2023 5f08c3bdfSopenharmony_ci * Author: Yang Xu <xuyang2018.jy@fujitsu.com> 6f08c3bdfSopenharmony_ci */ 7f08c3bdfSopenharmony_ci 8f08c3bdfSopenharmony_ci/*\ 9f08c3bdfSopenharmony_ci * [Description] 10f08c3bdfSopenharmony_ci * 11f08c3bdfSopenharmony_ci * Test basic error handling of faccessat2 syscall: 12f08c3bdfSopenharmony_ci * 13f08c3bdfSopenharmony_ci * - faccessat2() fails with EFAULT if pathname is a bad pathname point. 14f08c3bdfSopenharmony_ci * - faccessat2() fails with EINVAL if flags is -1. 15f08c3bdfSopenharmony_ci * - faccessat2() fails with EINVAL if mode is -1. 16f08c3bdfSopenharmony_ci * - faccessat2() fails with EBADF if dirfd is -1. 17f08c3bdfSopenharmony_ci * - faccessat2() fails with ENOTDIR if pathname is relative path to a 18f08c3bdfSopenharmony_ci * file and dir_fd is file descriptor for this file. 19f08c3bdfSopenharmony_ci * - faccessat2() fails with EACCES if flags is AT_EACCESS and not using 20f08c3bdfSopenharmony_ci * the effective user and group IDs. 21f08c3bdfSopenharmony_ci * 22f08c3bdfSopenharmony_ci * Minimum Linux version required is v5.8. 23f08c3bdfSopenharmony_ci */ 24f08c3bdfSopenharmony_ci 25f08c3bdfSopenharmony_ci#include <pwd.h> 26f08c3bdfSopenharmony_ci 27f08c3bdfSopenharmony_ci#include "tst_test.h" 28f08c3bdfSopenharmony_ci#include "lapi/syscalls.h" 29f08c3bdfSopenharmony_ci#include "lapi/faccessat.h" 30f08c3bdfSopenharmony_ci 31f08c3bdfSopenharmony_ci#define TESTUSER "nobody" 32f08c3bdfSopenharmony_ci#define TESTDIR "faccessat2dir" 33f08c3bdfSopenharmony_ci#define RELPATH "faccessat2dir/faccessat2file" 34f08c3bdfSopenharmony_ci 35f08c3bdfSopenharmony_cistatic int fd; 36f08c3bdfSopenharmony_cistatic int bad_fd = -1; 37f08c3bdfSopenharmony_cistatic int atcwd_fd = AT_FDCWD; 38f08c3bdfSopenharmony_cistatic char *bad_path; 39f08c3bdfSopenharmony_cistatic char *rel_path; 40f08c3bdfSopenharmony_ci 41f08c3bdfSopenharmony_cistatic struct passwd *ltpuser; 42f08c3bdfSopenharmony_ci 43f08c3bdfSopenharmony_cistatic struct tcase { 44f08c3bdfSopenharmony_ci int *fd; 45f08c3bdfSopenharmony_ci char **filename; 46f08c3bdfSopenharmony_ci int mode; 47f08c3bdfSopenharmony_ci int flags; 48f08c3bdfSopenharmony_ci int exp_errno; 49f08c3bdfSopenharmony_ci const char *desc; 50f08c3bdfSopenharmony_ci} tcases[] = { 51f08c3bdfSopenharmony_ci {&atcwd_fd, &bad_path, R_OK, 0, EFAULT, "invalid address"}, 52f08c3bdfSopenharmony_ci {&atcwd_fd, &rel_path, R_OK, -1, EINVAL, "invalid flags"}, 53f08c3bdfSopenharmony_ci {&atcwd_fd, &rel_path, -1, 0, EINVAL, "invalid mode"}, 54f08c3bdfSopenharmony_ci {&bad_fd, &rel_path, R_OK, 0, EBADF, "invalid fd"}, 55f08c3bdfSopenharmony_ci {&fd, &rel_path, R_OK, 0, ENOTDIR, "fd pointing to file"}, 56f08c3bdfSopenharmony_ci {&atcwd_fd, &rel_path, R_OK, AT_EACCESS, EACCES, 57f08c3bdfSopenharmony_ci "AT_EACCESS and unprivileged EUID"}, 58f08c3bdfSopenharmony_ci}; 59f08c3bdfSopenharmony_ci 60f08c3bdfSopenharmony_cistatic void verify_faccessat2(unsigned int i) 61f08c3bdfSopenharmony_ci{ 62f08c3bdfSopenharmony_ci struct tcase *tc = &tcases[i]; 63f08c3bdfSopenharmony_ci 64f08c3bdfSopenharmony_ci if (tc->exp_errno == EACCES) 65f08c3bdfSopenharmony_ci SAFE_SETEUID(ltpuser->pw_uid); 66f08c3bdfSopenharmony_ci 67f08c3bdfSopenharmony_ci TST_EXP_FAIL(faccessat2(*tc->fd, *tc->filename, tc->mode, tc->flags), 68f08c3bdfSopenharmony_ci tc->exp_errno, "faccessat2() with %s", tc->desc); 69f08c3bdfSopenharmony_ci 70f08c3bdfSopenharmony_ci if (tc->exp_errno == EACCES) 71f08c3bdfSopenharmony_ci SAFE_SETEUID(0); 72f08c3bdfSopenharmony_ci} 73f08c3bdfSopenharmony_ci 74f08c3bdfSopenharmony_cistatic void setup(void) 75f08c3bdfSopenharmony_ci{ 76f08c3bdfSopenharmony_ci SAFE_MKDIR(TESTDIR, 0666); 77f08c3bdfSopenharmony_ci SAFE_TOUCH(RELPATH, 0444, NULL); 78f08c3bdfSopenharmony_ci 79f08c3bdfSopenharmony_ci fd = SAFE_OPEN(RELPATH, O_RDONLY); 80f08c3bdfSopenharmony_ci bad_path = tst_get_bad_addr(NULL); 81f08c3bdfSopenharmony_ci 82f08c3bdfSopenharmony_ci ltpuser = SAFE_GETPWNAM(TESTUSER); 83f08c3bdfSopenharmony_ci} 84f08c3bdfSopenharmony_ci 85f08c3bdfSopenharmony_cistatic void cleanup(void) 86f08c3bdfSopenharmony_ci{ 87f08c3bdfSopenharmony_ci if (fd > -1) 88f08c3bdfSopenharmony_ci SAFE_CLOSE(fd); 89f08c3bdfSopenharmony_ci} 90f08c3bdfSopenharmony_ci 91f08c3bdfSopenharmony_cistatic struct tst_test test = { 92f08c3bdfSopenharmony_ci .test = verify_faccessat2, 93f08c3bdfSopenharmony_ci .tcnt = ARRAY_SIZE(tcases), 94f08c3bdfSopenharmony_ci .setup = setup, 95f08c3bdfSopenharmony_ci .cleanup = cleanup, 96f08c3bdfSopenharmony_ci .bufs = (struct tst_buffers []) { 97f08c3bdfSopenharmony_ci {&rel_path, .str = RELPATH}, 98f08c3bdfSopenharmony_ci {}, 99f08c3bdfSopenharmony_ci }, 100f08c3bdfSopenharmony_ci .needs_tmpdir = 1, 101f08c3bdfSopenharmony_ci .needs_root = 1, 102f08c3bdfSopenharmony_ci}; 103