[LTP] [PATCH 6/6] syscalls/chroot03: Make use of guarded buffers

Cyril Hrubis chrubis@suse.cz
Fri Aug 11 13:56:47 CEST 2023


Signed-off-by: Cyril Hrubis <chrubis@suse.cz>
---
 testcases/kernel/syscalls/chroot/chroot03.c | 46 ++++++++++++---------
 1 file changed, 27 insertions(+), 19 deletions(-)

diff --git a/testcases/kernel/syscalls/chroot/chroot03.c b/testcases/kernel/syscalls/chroot/chroot03.c
index ba8c1e9ac..87faec316 100644
--- a/testcases/kernel/syscalls/chroot/chroot03.c
+++ b/testcases/kernel/syscalls/chroot/chroot03.c
@@ -25,41 +25,42 @@
 #include <stdio.h>
 #include "tst_test.h"
 
-static char fname[255];
-static char nonexistent_dir[100] = "testdir";
-static char bad_dir[] = "abcdefghijklmnopqrstmnopqrstuvwxyzabcdefghijklmnopqrstmnopqrstuvwxyzabcdefghijklmnopqrstmnopqrstuvwxyzabcdefghijklmnopqrstmnopqrstuvwxyzabcdefghijklmnopqrstmnopqrstuvwxyzabcdefghijklmnopqrstmnopqrstuvwxyzabcdefghijklmnopqrstmnopqrstuvwxyzabcdefghijklmnopqrstmnopqrstuvwxyz";
-static char symbolic_dir[] = "sym_dir1";
+#define FILE_NAME "test_file"
+#define LOOP_DIR "sym_dir1"
+#define NONEXISTENT_DIR "does_not_exist"
+
+static char *longname_dir;
+static char *file_name;
+static char *nonexistent_dir;
+static char *bad_ptr;
+static char *loop_dir;
 
 static struct tcase {
-	char *dir;
+	char **dir;
 	int error;
 	char *desc;
 } tcases[] = {
-	{bad_dir, ENAMETOOLONG, "chroot(longer than VFS_MAXNAMELEN)"},
-	{fname, ENOTDIR, "chroot(not a directory)"},
-	{nonexistent_dir, ENOENT, "chroot(does not exists)"},
-	{(char *)-1, EFAULT, "chroot(an invalid address)"},
-	{symbolic_dir, ELOOP, "chroot(symlink loop)"}
+	{&longname_dir, ENAMETOOLONG, "chroot(longer than VFS_MAXNAMELEN)"},
+	{&file_name, ENOTDIR, "chroot(not a directory)"},
+	{&nonexistent_dir, ENOENT, "chroot(does not exists)"},
+	{&bad_ptr, EFAULT, "chroot(an invalid address)"},
+	{&loop_dir, ELOOP, "chroot(symlink loop)"}
 };
 
 static void verify_chroot(unsigned int n)
 {
 	struct tcase *tc = &tcases[n];
 
-	TST_EXP_FAIL(chroot(tc->dir), tc->error, "%s", tc->desc);
+	TST_EXP_FAIL(chroot(*tc->dir), tc->error, "%s", tc->desc);
 }
 
 static void setup(void)
 {
-	unsigned int i;
-
-	(void)sprintf(fname, "tfile_%d", getpid());
-	SAFE_TOUCH(fname, 0666, NULL);
+	SAFE_TOUCH(FILE_NAME, 0666, NULL);
+	bad_ptr = tst_get_bad_addr(NULL);
 
-	for (i = 0; i < ARRAY_SIZE(tcases); i++) {
-		if (tcases[i].error == EFAULT)
-			tcases[3].dir = tst_get_bad_addr(NULL);
-	}
+	memset(longname_dir, 'a', PATH_MAX + 1);
+	longname_dir[PATH_MAX+1] = 0;
 
 	SAFE_SYMLINK("sym_dir1/", "sym_dir2");
 	SAFE_SYMLINK("sym_dir2/", "sym_dir1");
@@ -70,4 +71,11 @@ static struct tst_test test = {
 	.tcnt = ARRAY_SIZE(tcases),
 	.test = verify_chroot,
 	.needs_tmpdir = 1,
+	.bufs = (struct tst_buffers []) {
+		{&file_name, .str = FILE_NAME},
+		{&nonexistent_dir, .str = NONEXISTENT_DIR},
+		{&loop_dir, .str = LOOP_DIR},
+		{&longname_dir, .size = PATH_MAX+2},
+		{}
+	}
 };
-- 
2.41.0



More information about the ltp mailing list