1f08c3bdfSopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later 2f08c3bdfSopenharmony_ci/* 3f08c3bdfSopenharmony_ci * Copyright (c) 2020 Viresh Kumar <viresh.kumar@linaro.org> 4f08c3bdfSopenharmony_ci */ 5f08c3bdfSopenharmony_ci 6f08c3bdfSopenharmony_ci/*\ 7f08c3bdfSopenharmony_ci * [Description] 8f08c3bdfSopenharmony_ci * 9f08c3bdfSopenharmony_ci * Basic finit_module() failure tests. 10f08c3bdfSopenharmony_ci * 11f08c3bdfSopenharmony_ci * [Algorithm] 12f08c3bdfSopenharmony_ci * 13f08c3bdfSopenharmony_ci * Tests various failure scenarios for finit_module(). 14f08c3bdfSopenharmony_ci */ 15f08c3bdfSopenharmony_ci 16f08c3bdfSopenharmony_ci#include <linux/capability.h> 17f08c3bdfSopenharmony_ci#include <errno.h> 18f08c3bdfSopenharmony_ci#include "lapi/init_module.h" 19f08c3bdfSopenharmony_ci#include "tst_module.h" 20f08c3bdfSopenharmony_ci#include "tst_capability.h" 21f08c3bdfSopenharmony_ci 22f08c3bdfSopenharmony_ci#define MODULE_NAME "finit_module.ko" 23f08c3bdfSopenharmony_ci#define TEST_DIR "test_dir" 24f08c3bdfSopenharmony_ci 25f08c3bdfSopenharmony_cistatic char *mod_path; 26f08c3bdfSopenharmony_ci 27f08c3bdfSopenharmony_cistatic int fd, fd_zero, fd_invalid = -1, fd_dir; 28f08c3bdfSopenharmony_cistatic int kernel_lockdown, secure_boot; 29f08c3bdfSopenharmony_ci 30f08c3bdfSopenharmony_cistatic struct tst_cap cap_req = TST_CAP(TST_CAP_REQ, CAP_SYS_MODULE); 31f08c3bdfSopenharmony_cistatic struct tst_cap cap_drop = TST_CAP(TST_CAP_DROP, CAP_SYS_MODULE); 32f08c3bdfSopenharmony_ci 33f08c3bdfSopenharmony_cistruct tcase { 34f08c3bdfSopenharmony_ci const char *name; 35f08c3bdfSopenharmony_ci int *fd; 36f08c3bdfSopenharmony_ci const char *param; 37f08c3bdfSopenharmony_ci int open_flags; 38f08c3bdfSopenharmony_ci int flags; 39f08c3bdfSopenharmony_ci int cap; 40f08c3bdfSopenharmony_ci int exp_errno; 41f08c3bdfSopenharmony_ci int skip_in_lockdown; 42f08c3bdfSopenharmony_ci void (*fix_errno)(struct tcase *tc); 43f08c3bdfSopenharmony_ci}; 44f08c3bdfSopenharmony_ci 45f08c3bdfSopenharmony_cistatic void bad_fd_setup(struct tcase *tc) 46f08c3bdfSopenharmony_ci{ 47f08c3bdfSopenharmony_ci if (tst_kvercmp(4, 6, 0) < 0) 48f08c3bdfSopenharmony_ci tc->exp_errno = ENOEXEC; 49f08c3bdfSopenharmony_ci else 50f08c3bdfSopenharmony_ci tc->exp_errno = EBADF; 51f08c3bdfSopenharmony_ci} 52f08c3bdfSopenharmony_ci 53f08c3bdfSopenharmony_cistatic void dir_setup(struct tcase *tc) 54f08c3bdfSopenharmony_ci{ 55f08c3bdfSopenharmony_ci if (tst_kvercmp(4, 6, 0) < 0) 56f08c3bdfSopenharmony_ci tc->exp_errno = EISDIR; 57f08c3bdfSopenharmony_ci else 58f08c3bdfSopenharmony_ci tc->exp_errno = EINVAL; 59f08c3bdfSopenharmony_ci} 60f08c3bdfSopenharmony_ci 61f08c3bdfSopenharmony_cistatic struct tcase tcases[] = { 62f08c3bdfSopenharmony_ci {"invalid-fd", &fd_invalid, "", O_RDONLY | O_CLOEXEC, 0, 0, 0, 0, 63f08c3bdfSopenharmony_ci bad_fd_setup}, 64f08c3bdfSopenharmony_ci {"zero-fd", &fd_zero, "", O_RDONLY | O_CLOEXEC, 0, 0, EINVAL, 0, NULL}, 65f08c3bdfSopenharmony_ci {"null-param", &fd, NULL, O_RDONLY | O_CLOEXEC, 0, 0, EFAULT, 1, NULL}, 66f08c3bdfSopenharmony_ci {"invalid-param", &fd, "status=invalid", O_RDONLY | O_CLOEXEC, 0, 0, 67f08c3bdfSopenharmony_ci EINVAL, 1, NULL}, 68f08c3bdfSopenharmony_ci {"invalid-flags", &fd, "", O_RDONLY | O_CLOEXEC, -1, 0, EINVAL, 0, 69f08c3bdfSopenharmony_ci NULL}, 70f08c3bdfSopenharmony_ci {"no-perm", &fd, "", O_RDONLY | O_CLOEXEC, 0, 1, EPERM, 0, NULL}, 71f08c3bdfSopenharmony_ci {"module-exists", &fd, "", O_RDONLY | O_CLOEXEC, 0, 0, EEXIST, 1, 72f08c3bdfSopenharmony_ci NULL}, 73f08c3bdfSopenharmony_ci {"file-not-readable", &fd, "", O_WRONLY | O_CLOEXEC, 0, 0, EBADF, 0, 74f08c3bdfSopenharmony_ci NULL}, 75f08c3bdfSopenharmony_ci {"file-readwrite", &fd, "", O_RDWR | O_CLOEXEC, 0, 0, ETXTBSY, 0, 76f08c3bdfSopenharmony_ci NULL}, 77f08c3bdfSopenharmony_ci {"directory", &fd_dir, "", O_RDONLY | O_CLOEXEC, 0, 0, 0, 0, dir_setup}, 78f08c3bdfSopenharmony_ci}; 79f08c3bdfSopenharmony_ci 80f08c3bdfSopenharmony_cistatic void setup(void) 81f08c3bdfSopenharmony_ci{ 82f08c3bdfSopenharmony_ci unsigned long int i; 83f08c3bdfSopenharmony_ci 84f08c3bdfSopenharmony_ci tst_module_exists(MODULE_NAME, &mod_path); 85f08c3bdfSopenharmony_ci 86f08c3bdfSopenharmony_ci kernel_lockdown = tst_lockdown_enabled() > 0; 87f08c3bdfSopenharmony_ci secure_boot = tst_secureboot_enabled() > 0; 88f08c3bdfSopenharmony_ci 89f08c3bdfSopenharmony_ci SAFE_MKDIR(TEST_DIR, 0700); 90f08c3bdfSopenharmony_ci fd_dir = SAFE_OPEN(TEST_DIR, O_DIRECTORY); 91f08c3bdfSopenharmony_ci 92f08c3bdfSopenharmony_ci for (i = 0; i < ARRAY_SIZE(tcases); i++) { 93f08c3bdfSopenharmony_ci if (tcases[i].fix_errno) 94f08c3bdfSopenharmony_ci tcases[i].fix_errno(&tcases[i]); 95f08c3bdfSopenharmony_ci } 96f08c3bdfSopenharmony_ci} 97f08c3bdfSopenharmony_ci 98f08c3bdfSopenharmony_cistatic void cleanup(void) 99f08c3bdfSopenharmony_ci{ 100f08c3bdfSopenharmony_ci SAFE_CLOSE(fd_dir); 101f08c3bdfSopenharmony_ci} 102f08c3bdfSopenharmony_ci 103f08c3bdfSopenharmony_cistatic void run(unsigned int n) 104f08c3bdfSopenharmony_ci{ 105f08c3bdfSopenharmony_ci struct tcase *tc = &tcases[n]; 106f08c3bdfSopenharmony_ci 107f08c3bdfSopenharmony_ci if (tc->skip_in_lockdown && (kernel_lockdown || secure_boot)) { 108f08c3bdfSopenharmony_ci tst_res(TCONF, "Cannot load unsigned modules, skipping %s", tc->name); 109f08c3bdfSopenharmony_ci return; 110f08c3bdfSopenharmony_ci } 111f08c3bdfSopenharmony_ci 112f08c3bdfSopenharmony_ci fd = SAFE_OPEN(mod_path, tc->open_flags); 113f08c3bdfSopenharmony_ci 114f08c3bdfSopenharmony_ci if (tc->cap) 115f08c3bdfSopenharmony_ci tst_cap_action(&cap_drop); 116f08c3bdfSopenharmony_ci 117f08c3bdfSopenharmony_ci /* Insert module twice */ 118f08c3bdfSopenharmony_ci if (tc->exp_errno == EEXIST) 119f08c3bdfSopenharmony_ci tst_module_load(MODULE_NAME, NULL); 120f08c3bdfSopenharmony_ci 121f08c3bdfSopenharmony_ci TST_EXP_FAIL(finit_module(*tc->fd, tc->param, tc->flags), tc->exp_errno, 122f08c3bdfSopenharmony_ci "TestName: %s", tc->name); 123f08c3bdfSopenharmony_ci 124f08c3bdfSopenharmony_ci if (tc->exp_errno == EEXIST) 125f08c3bdfSopenharmony_ci tst_module_unload(MODULE_NAME); 126f08c3bdfSopenharmony_ci 127f08c3bdfSopenharmony_ci if (!TST_PASS && !TST_RET) 128f08c3bdfSopenharmony_ci tst_module_unload(MODULE_NAME); 129f08c3bdfSopenharmony_ci 130f08c3bdfSopenharmony_ci if (tc->cap) 131f08c3bdfSopenharmony_ci tst_cap_action(&cap_req); 132f08c3bdfSopenharmony_ci 133f08c3bdfSopenharmony_ci SAFE_CLOSE(fd); 134f08c3bdfSopenharmony_ci} 135f08c3bdfSopenharmony_ci 136f08c3bdfSopenharmony_cistatic struct tst_test test = { 137f08c3bdfSopenharmony_ci .tags = (const struct tst_tag[]) { 138f08c3bdfSopenharmony_ci {"linux-git", "032146cda855"}, 139f08c3bdfSopenharmony_ci {"linux-git", "39d637af5aa7"}, 140f08c3bdfSopenharmony_ci {} 141f08c3bdfSopenharmony_ci }, 142f08c3bdfSopenharmony_ci .test = run, 143f08c3bdfSopenharmony_ci .tcnt = ARRAY_SIZE(tcases), 144f08c3bdfSopenharmony_ci .setup = setup, 145f08c3bdfSopenharmony_ci .cleanup = cleanup, 146f08c3bdfSopenharmony_ci .needs_tmpdir = 1, 147f08c3bdfSopenharmony_ci .needs_root = 1, 148f08c3bdfSopenharmony_ci}; 149