Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

selftests/landlock: Add tests for execveat + AT_EXECVE_CHECK

Extend layout1.execute with the new AT_EXECVE_CHECK flag. The semantic
with AT_EXECVE_CHECK is the same as with a simple execve(2),
LANDLOCK_ACCESS_FS_EXECUTE is enforced the same way.

Cc: Günther Noack <gnoack@google.com>
Cc: Kees Cook <keescook@chromium.org>
Cc: Paul Moore <paul@paul-moore.com>
Signed-off-by: Mickaël Salaün <mic@digikod.net>
Link: https://lore.kernel.org/r/20241212174223.389435-5-mic@digikod.net
Signed-off-by: Kees Cook <kees@kernel.org>

authored by

Mickaël Salaün and committed by
Kees Cook
0e7f90f3 b083cc81

+27
+27
tools/testing/selftests/landlock/fs_test.c
··· 37 37 #include <linux/fs.h> 38 38 #include <linux/mount.h> 39 39 40 + /* Defines AT_EXECVE_CHECK without type conflicts. */ 41 + #define _ASM_GENERIC_FCNTL_H 42 + #include <linux/fcntl.h> 43 + 40 44 #include "common.h" 41 45 42 46 #ifndef renameat2 ··· 2012 2008 }; 2013 2009 } 2014 2010 2011 + static void test_check_exec(struct __test_metadata *const _metadata, 2012 + const int err, const char *const path) 2013 + { 2014 + int ret; 2015 + char *const argv[] = { (char *)path, NULL }; 2016 + 2017 + ret = execveat(AT_FDCWD, path, argv, NULL, 2018 + AT_EMPTY_PATH | AT_EXECVE_CHECK); 2019 + if (err) { 2020 + EXPECT_EQ(-1, ret); 2021 + EXPECT_EQ(errno, err); 2022 + } else { 2023 + EXPECT_EQ(0, ret); 2024 + } 2025 + } 2026 + 2015 2027 TEST_F_FORK(layout1, execute) 2016 2028 { 2017 2029 const struct rule rules[] = { ··· 2045 2025 copy_binary(_metadata, file1_s1d2); 2046 2026 copy_binary(_metadata, file1_s1d3); 2047 2027 2028 + /* Checks before file1_s1d1 being denied. */ 2029 + test_execute(_metadata, 0, file1_s1d1); 2030 + test_check_exec(_metadata, 0, file1_s1d1); 2031 + 2048 2032 enforce_ruleset(_metadata, ruleset_fd); 2049 2033 ASSERT_EQ(0, close(ruleset_fd)); 2050 2034 2051 2035 ASSERT_EQ(0, test_open(dir_s1d1, O_RDONLY)); 2052 2036 ASSERT_EQ(0, test_open(file1_s1d1, O_RDONLY)); 2053 2037 test_execute(_metadata, EACCES, file1_s1d1); 2038 + test_check_exec(_metadata, EACCES, file1_s1d1); 2054 2039 2055 2040 ASSERT_EQ(0, test_open(dir_s1d2, O_RDONLY)); 2056 2041 ASSERT_EQ(0, test_open(file1_s1d2, O_RDONLY)); 2057 2042 test_execute(_metadata, 0, file1_s1d2); 2043 + test_check_exec(_metadata, 0, file1_s1d2); 2058 2044 2059 2045 ASSERT_EQ(0, test_open(dir_s1d3, O_RDONLY)); 2060 2046 ASSERT_EQ(0, test_open(file1_s1d3, O_RDONLY)); 2061 2047 test_execute(_metadata, 0, file1_s1d3); 2048 + test_check_exec(_metadata, 0, file1_s1d3); 2062 2049 } 2063 2050 2064 2051 TEST_F_FORK(layout1, link)