Skip to content

Commit 3656bc2

Browse files
committed
selftests/landlock: Do not allocate memory in fixture data
Do not allocate self->dir_path in the test process because this would not be visible in the FIXTURE_TEARDOWN() process when relying on fork()/clone3() instead of vfork(). This change is required for a following commit removing vfork() call to not break the layout3_fs.* test cases. Cc: Günther Noack <[email protected]> Cc: Shuah Khan <[email protected]> Reviewed-by: Kees Cook <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Mickaël Salaün <[email protected]>
1 parent a86f189 commit 3656bc2

File tree

1 file changed

+35
-22
lines changed

1 file changed

+35
-22
lines changed

tools/testing/selftests/landlock/fs_test.c

Lines changed: 35 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
#define _GNU_SOURCE
1111
#include <fcntl.h>
12+
#include <libgen.h>
1213
#include <linux/landlock.h>
1314
#include <linux/magic.h>
1415
#include <sched.h>
@@ -4624,7 +4625,6 @@ FIXTURE(layout3_fs)
46244625
{
46254626
bool has_created_dir;
46264627
bool has_created_file;
4627-
char *dir_path;
46284628
bool skip_test;
46294629
};
46304630

@@ -4683,11 +4683,24 @@ FIXTURE_VARIANT_ADD(layout3_fs, hostfs) {
46834683
.cwd_fs_magic = HOSTFS_SUPER_MAGIC,
46844684
};
46854685

4686+
static char *dirname_alloc(const char *path)
4687+
{
4688+
char *dup;
4689+
4690+
if (!path)
4691+
return NULL;
4692+
4693+
dup = strdup(path);
4694+
if (!dup)
4695+
return NULL;
4696+
4697+
return dirname(dup);
4698+
}
4699+
46864700
FIXTURE_SETUP(layout3_fs)
46874701
{
46884702
struct stat statbuf;
4689-
const char *slash;
4690-
size_t dir_len;
4703+
char *dir_path = dirname_alloc(variant->file_path);
46914704

46924705
if (!supports_filesystem(variant->mnt.type) ||
46934706
!cwd_matches_fs(variant->cwd_fs_magic)) {
@@ -4697,25 +4710,15 @@ FIXTURE_SETUP(layout3_fs)
46974710

46984711
_metadata->teardown_parent = true;
46994712

4700-
slash = strrchr(variant->file_path, '/');
4701-
ASSERT_NE(slash, NULL);
4702-
dir_len = (size_t)slash - (size_t)variant->file_path;
4703-
ASSERT_LT(0, dir_len);
4704-
self->dir_path = malloc(dir_len + 1);
4705-
self->dir_path[dir_len] = '\0';
4706-
strncpy(self->dir_path, variant->file_path, dir_len);
4707-
47084713
prepare_layout_opt(_metadata, &variant->mnt);
47094714

47104715
/* Creates directory when required. */
4711-
if (stat(self->dir_path, &statbuf)) {
4716+
if (stat(dir_path, &statbuf)) {
47124717
set_cap(_metadata, CAP_DAC_OVERRIDE);
4713-
EXPECT_EQ(0, mkdir(self->dir_path, 0700))
4718+
EXPECT_EQ(0, mkdir(dir_path, 0700))
47144719
{
47154720
TH_LOG("Failed to create directory \"%s\": %s",
4716-
self->dir_path, strerror(errno));
4717-
free(self->dir_path);
4718-
self->dir_path = NULL;
4721+
dir_path, strerror(errno));
47194722
}
47204723
self->has_created_dir = true;
47214724
clear_cap(_metadata, CAP_DAC_OVERRIDE);
@@ -4736,6 +4739,8 @@ FIXTURE_SETUP(layout3_fs)
47364739
self->has_created_file = true;
47374740
clear_cap(_metadata, CAP_DAC_OVERRIDE);
47384741
}
4742+
4743+
free(dir_path);
47394744
}
47404745

47414746
FIXTURE_TEARDOWN(layout3_fs)
@@ -4754,16 +4759,17 @@ FIXTURE_TEARDOWN(layout3_fs)
47544759
}
47554760

47564761
if (self->has_created_dir) {
4762+
char *dir_path = dirname_alloc(variant->file_path);
4763+
47574764
set_cap(_metadata, CAP_DAC_OVERRIDE);
47584765
/*
47594766
* Don't check for error because the directory might already
47604767
* have been removed (cf. release_inode test).
47614768
*/
4762-
rmdir(self->dir_path);
4769+
rmdir(dir_path);
47634770
clear_cap(_metadata, CAP_DAC_OVERRIDE);
4771+
free(dir_path);
47644772
}
4765-
free(self->dir_path);
4766-
self->dir_path = NULL;
47674773

47684774
cleanup_layout(_metadata);
47694775
}
@@ -4830,7 +4836,10 @@ TEST_F_FORK(layout3_fs, tag_inode_dir_mnt)
48304836

48314837
TEST_F_FORK(layout3_fs, tag_inode_dir_child)
48324838
{
4833-
layer3_fs_tag_inode(_metadata, self, variant, self->dir_path);
4839+
char *dir_path = dirname_alloc(variant->file_path);
4840+
4841+
layer3_fs_tag_inode(_metadata, self, variant, dir_path);
4842+
free(dir_path);
48344843
}
48354844

48364845
TEST_F_FORK(layout3_fs, tag_inode_file)
@@ -4857,9 +4866,13 @@ TEST_F_FORK(layout3_fs, release_inodes)
48574866
if (self->has_created_file)
48584867
EXPECT_EQ(0, remove_path(variant->file_path));
48594868

4860-
if (self->has_created_dir)
4869+
if (self->has_created_dir) {
4870+
char *dir_path = dirname_alloc(variant->file_path);
4871+
48614872
/* Don't check for error because of cgroup specificities. */
4862-
remove_path(self->dir_path);
4873+
remove_path(dir_path);
4874+
free(dir_path);
4875+
}
48634876

48644877
ruleset_fd =
48654878
create_ruleset(_metadata, LANDLOCK_ACCESS_FS_READ_DIR, layer1);

0 commit comments

Comments
 (0)