1f08c3bdfSopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later 2f08c3bdfSopenharmony_ci/* 3f08c3bdfSopenharmony_ci * Copyright (c) International Business Machines Corp., 2001 4f08c3bdfSopenharmony_ci * 5f08c3bdfSopenharmony_ci * 07/2001 Ported by Wayne Boyer 6f08c3bdfSopenharmony_ci */ 7f08c3bdfSopenharmony_ci 8f08c3bdfSopenharmony_ci/*\ 9f08c3bdfSopenharmony_ci * [Description] 10f08c3bdfSopenharmony_ci * 11f08c3bdfSopenharmony_ci * Testcase to test whether chroot(2) sets errno correctly. 12f08c3bdfSopenharmony_ci * 13f08c3bdfSopenharmony_ci * - to test whether chroot() is setting ENAMETOOLONG if the 14f08c3bdfSopenharmony_ci * pathname is more than VFS_MAXNAMELEN. 15f08c3bdfSopenharmony_ci * - to test whether chroot() is setting ENOTDIR if the argument 16f08c3bdfSopenharmony_ci * is not a directory. 17f08c3bdfSopenharmony_ci * - to test whether chroot() is setting ENOENT if the directory 18f08c3bdfSopenharmony_ci * does not exist. 19f08c3bdfSopenharmony_ci * - attempt to chroot to a path pointing to an invalid address 20f08c3bdfSopenharmony_ci * and expect EFAULT as errno. 21f08c3bdfSopenharmony_ci * - to test whether chroot() is setting ELOOP if the two 22f08c3bdfSopenharmony_ci * symbolic directory who point to each other. 23f08c3bdfSopenharmony_ci */ 24f08c3bdfSopenharmony_ci 25f08c3bdfSopenharmony_ci#include <stdio.h> 26f08c3bdfSopenharmony_ci#include "tst_test.h" 27f08c3bdfSopenharmony_ci 28f08c3bdfSopenharmony_ci#define FILE_NAME "test_file" 29f08c3bdfSopenharmony_ci#define LOOP_DIR "sym_dir1" 30f08c3bdfSopenharmony_ci#define NONEXISTENT_DIR "does_not_exist" 31f08c3bdfSopenharmony_ci 32f08c3bdfSopenharmony_cistatic char *longname_dir; 33f08c3bdfSopenharmony_cistatic char *file_name; 34f08c3bdfSopenharmony_cistatic char *nonexistent_dir; 35f08c3bdfSopenharmony_cistatic char *bad_ptr; 36f08c3bdfSopenharmony_cistatic char *loop_dir; 37f08c3bdfSopenharmony_ci 38f08c3bdfSopenharmony_cistatic struct tcase { 39f08c3bdfSopenharmony_ci char **dir; 40f08c3bdfSopenharmony_ci int error; 41f08c3bdfSopenharmony_ci char *desc; 42f08c3bdfSopenharmony_ci} tcases[] = { 43f08c3bdfSopenharmony_ci {&longname_dir, ENAMETOOLONG, "chroot(longer than VFS_MAXNAMELEN)"}, 44f08c3bdfSopenharmony_ci {&file_name, ENOTDIR, "chroot(not a directory)"}, 45f08c3bdfSopenharmony_ci {&nonexistent_dir, ENOENT, "chroot(does not exists)"}, 46f08c3bdfSopenharmony_ci {&bad_ptr, EFAULT, "chroot(an invalid address)"}, 47f08c3bdfSopenharmony_ci {&loop_dir, ELOOP, "chroot(symlink loop)"} 48f08c3bdfSopenharmony_ci}; 49f08c3bdfSopenharmony_ci 50f08c3bdfSopenharmony_cistatic void verify_chroot(unsigned int n) 51f08c3bdfSopenharmony_ci{ 52f08c3bdfSopenharmony_ci struct tcase *tc = &tcases[n]; 53f08c3bdfSopenharmony_ci 54f08c3bdfSopenharmony_ci TST_EXP_FAIL(chroot(*tc->dir), tc->error, "%s", tc->desc); 55f08c3bdfSopenharmony_ci} 56f08c3bdfSopenharmony_ci 57f08c3bdfSopenharmony_cistatic void setup(void) 58f08c3bdfSopenharmony_ci{ 59f08c3bdfSopenharmony_ci SAFE_TOUCH(FILE_NAME, 0666, NULL); 60f08c3bdfSopenharmony_ci bad_ptr = tst_get_bad_addr(NULL); 61f08c3bdfSopenharmony_ci 62f08c3bdfSopenharmony_ci memset(longname_dir, 'a', PATH_MAX + 1); 63f08c3bdfSopenharmony_ci longname_dir[PATH_MAX+1] = 0; 64f08c3bdfSopenharmony_ci 65f08c3bdfSopenharmony_ci SAFE_SYMLINK("sym_dir1/", "sym_dir2"); 66f08c3bdfSopenharmony_ci SAFE_SYMLINK("sym_dir2/", "sym_dir1"); 67f08c3bdfSopenharmony_ci} 68f08c3bdfSopenharmony_ci 69f08c3bdfSopenharmony_cistatic struct tst_test test = { 70f08c3bdfSopenharmony_ci .setup = setup, 71f08c3bdfSopenharmony_ci .tcnt = ARRAY_SIZE(tcases), 72f08c3bdfSopenharmony_ci .test = verify_chroot, 73f08c3bdfSopenharmony_ci .needs_tmpdir = 1, 74f08c3bdfSopenharmony_ci .bufs = (struct tst_buffers []) { 75f08c3bdfSopenharmony_ci {&file_name, .str = FILE_NAME}, 76f08c3bdfSopenharmony_ci {&nonexistent_dir, .str = NONEXISTENT_DIR}, 77f08c3bdfSopenharmony_ci {&loop_dir, .str = LOOP_DIR}, 78f08c3bdfSopenharmony_ci {&longname_dir, .size = PATH_MAX+2}, 79f08c3bdfSopenharmony_ci {} 80f08c3bdfSopenharmony_ci } 81f08c3bdfSopenharmony_ci}; 82