public inbox for ltp@lists.linux.it
 help / color / mirror / Atom feed
* [LTP] [PATCH 0/6] Utility functions for string in guarded buffers
@ 2023-08-11 11:56 Cyril Hrubis
  2023-08-11 11:56 ` [LTP] [PATCH 1/6] lib: tst_buffers: Add bufs .str and tst_aprintf() Cyril Hrubis
                   ` (6 more replies)
  0 siblings, 7 replies; 12+ messages in thread
From: Cyril Hrubis @ 2023-08-11 11:56 UTC (permalink / raw)
  To: ltp

First patch adds utilitilty functions for strings in guarded buffers,
which is followed by a few test conversions to the newly introduced API.

Cyril Hrubis (6):
  lib: tst_buffers: Add bufs .str and tst_aprintf()
  syscalls/access04: Make use of guarded buffers
  syscalls/acct01: Make use of guarded buffers
  syscalls/chdir01: Make use use of guarded buffers
  syscalls/chmod01: Make use of guarded buffers
  syscalls/chroot03: Make use of guarded buffers

 include/tst_buffers.h                       | 11 +++
 lib/tst_buffers.c                           | 28 ++++++-
 testcases/kernel/syscalls/access/access04.c | 36 ++++++---
 testcases/kernel/syscalls/acct/acct01.c     | 84 +++++++++++----------
 testcases/kernel/syscalls/chdir/chdir01.c   | 55 +++++++++-----
 testcases/kernel/syscalls/chmod/chmod01.c   | 28 ++++---
 testcases/kernel/syscalls/chroot/chroot03.c | 46 ++++++-----
 7 files changed, 188 insertions(+), 100 deletions(-)

-- 
2.41.0


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp

^ permalink raw reply	[flat|nested] 12+ messages in thread

* [LTP] [PATCH 1/6] lib: tst_buffers: Add bufs .str and tst_aprintf()
  2023-08-11 11:56 [LTP] [PATCH 0/6] Utility functions for string in guarded buffers Cyril Hrubis
@ 2023-08-11 11:56 ` Cyril Hrubis
  2023-08-11 12:01   ` Cyril Hrubis
  2023-08-15  9:43   ` Li Wang
  2023-08-11 11:56 ` Cyril Hrubis
                   ` (5 subsequent siblings)
  6 siblings, 2 replies; 12+ messages in thread
From: Cyril Hrubis @ 2023-08-11 11:56 UTC (permalink / raw)
  To: ltp

Signed-off-by: Cyril Hrubis <chrubis@suse.cz>
---
 include/tst_buffers.h | 11 +++++++++++
 lib/tst_buffers.c     | 28 +++++++++++++++++++++++++---
 2 files changed, 36 insertions(+), 3 deletions(-)

diff --git a/include/tst_buffers.h b/include/tst_buffers.h
index d19ac8cf0..b5f355f0f 100644
--- a/include/tst_buffers.h
+++ b/include/tst_buffers.h
@@ -25,6 +25,11 @@ struct tst_buffers {
 	 * Array of iov buffer sizes terminated by -1.
 	 */
 	int *iov_sizes;
+	/*
+	 * If size and iov_sizes is NULL this is the string we want to strdup()
+	 * into the buffer.
+	 */
+	char *str;
 };
 
 /*
@@ -46,6 +51,12 @@ char *tst_strdup(const char *str);
  */
 void *tst_alloc(size_t size);
 
+/*
+ * Printf into a guarded buffer.
+ */
+char *tst_aprintf(const char *fmt, ...)
+      __attribute__((format (printf, 1, 2)));
+
 /*
  * Allocates iovec structure including the buffers.
  *
diff --git a/lib/tst_buffers.c b/lib/tst_buffers.c
index b8b597a12..b0bd359eb 100644
--- a/lib/tst_buffers.c
+++ b/lib/tst_buffers.c
@@ -5,6 +5,7 @@
 
 #include <sys/mman.h>
 #include <stdlib.h>
+#include <stdio.h>
 #define TST_NO_DEFAULT_MAIN
 #include "tst_test.h"
 
@@ -76,6 +77,25 @@ void *tst_alloc(size_t size)
 	return ret + map->buf_shift;
 }
 
+char *tst_aprintf(const char *fmt, ...)
+{
+	va_list va;
+	int len;
+	char *ret;
+
+        va_start(va, fmt);
+        len = vsnprintf(NULL, 0, fmt, va)+1;
+        va_end(va);
+
+	ret = tst_alloc(len);
+
+	va_start(va, fmt);
+        vsprintf(ret, fmt, va);
+        va_end(va);
+
+	return ret;
+}
+
 static int count_iovec(int *sizes)
 {
 	int ret = 0;
@@ -115,15 +135,17 @@ void tst_buffers_alloc(struct tst_buffers bufs[])
 	for (i = 0; bufs[i].ptr; i++) {
 		if (bufs[i].size)
 			*((void**)bufs[i].ptr) = tst_alloc(bufs[i].size);
-		else
+		else if (bufs[i].iov_sizes)
 			*((void**)bufs[i].ptr) = tst_iovec_alloc(bufs[i].iov_sizes);
+		else
+			*((void**)bufs[i].ptr) = tst_strdup(bufs[i].str);
 	}
 }
 
 char *tst_strdup(const char *str)
 {
-	size_t len = strlen(str);
-	char *ret = tst_alloc(len + 1);
+	char *ret = tst_alloc(strlen(str) + 1);
+
 	return strcpy(ret, str);
 }
 
-- 
2.41.0


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp

^ permalink raw reply related	[flat|nested] 12+ messages in thread

* [LTP] [PATCH 1/6] lib: tst_buffers: Add bufs .str and tst_aprintf()
  2023-08-11 11:56 [LTP] [PATCH 0/6] Utility functions for string in guarded buffers Cyril Hrubis
  2023-08-11 11:56 ` [LTP] [PATCH 1/6] lib: tst_buffers: Add bufs .str and tst_aprintf() Cyril Hrubis
@ 2023-08-11 11:56 ` Cyril Hrubis
  2023-08-11 11:56 ` [LTP] [PATCH 2/6] syscalls/access04: Make use of guarded buffers Cyril Hrubis
                   ` (4 subsequent siblings)
  6 siblings, 0 replies; 12+ messages in thread
From: Cyril Hrubis @ 2023-08-11 11:56 UTC (permalink / raw)
  To: ltp

Signed-off-by: Cyril Hrubis <chrubis@suse.cz>
---
 include/tst_buffers.h | 11 +++++++++++
 lib/tst_buffers.c     | 28 +++++++++++++++++++++++++---
 2 files changed, 36 insertions(+), 3 deletions(-)

diff --git a/include/tst_buffers.h b/include/tst_buffers.h
index d19ac8cf0..b5f355f0f 100644
--- a/include/tst_buffers.h
+++ b/include/tst_buffers.h
@@ -25,6 +25,11 @@ struct tst_buffers {
 	 * Array of iov buffer sizes terminated by -1.
 	 */
 	int *iov_sizes;
+	/*
+	 * If size and iov_sizes is NULL this is the string we want to strdup()
+	 * into the buffer.
+	 */
+	char *str;
 };
 
 /*
@@ -46,6 +51,12 @@ char *tst_strdup(const char *str);
  */
 void *tst_alloc(size_t size);
 
+/*
+ * Printf into a guarded buffer.
+ */
+char *tst_aprintf(const char *fmt, ...)
+      __attribute__((format (printf, 1, 2)));
+
 /*
  * Allocates iovec structure including the buffers.
  *
diff --git a/lib/tst_buffers.c b/lib/tst_buffers.c
index b8b597a12..b0bd359eb 100644
--- a/lib/tst_buffers.c
+++ b/lib/tst_buffers.c
@@ -5,6 +5,7 @@
 
 #include <sys/mman.h>
 #include <stdlib.h>
+#include <stdio.h>
 #define TST_NO_DEFAULT_MAIN
 #include "tst_test.h"
 
@@ -76,6 +77,25 @@ void *tst_alloc(size_t size)
 	return ret + map->buf_shift;
 }
 
+char *tst_aprintf(const char *fmt, ...)
+{
+	va_list va;
+	int len;
+	char *ret;
+
+        va_start(va, fmt);
+        len = vsnprintf(NULL, 0, fmt, va)+1;
+        va_end(va);
+
+	ret = tst_alloc(len);
+
+	va_start(va, fmt);
+        vsprintf(ret, fmt, va);
+        va_end(va);
+
+	return ret;
+}
+
 static int count_iovec(int *sizes)
 {
 	int ret = 0;
@@ -115,15 +135,17 @@ void tst_buffers_alloc(struct tst_buffers bufs[])
 	for (i = 0; bufs[i].ptr; i++) {
 		if (bufs[i].size)
 			*((void**)bufs[i].ptr) = tst_alloc(bufs[i].size);
-		else
+		else if (bufs[i].iov_sizes)
 			*((void**)bufs[i].ptr) = tst_iovec_alloc(bufs[i].iov_sizes);
+		else
+			*((void**)bufs[i].ptr) = tst_strdup(bufs[i].str);
 	}
 }
 
 char *tst_strdup(const char *str)
 {
-	size_t len = strlen(str);
-	char *ret = tst_alloc(len + 1);
+	char *ret = tst_alloc(strlen(str) + 1);
+
 	return strcpy(ret, str);
 }
 
-- 
2.41.0


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp

^ permalink raw reply related	[flat|nested] 12+ messages in thread

* [LTP] [PATCH 2/6] syscalls/access04: Make use of guarded buffers
  2023-08-11 11:56 [LTP] [PATCH 0/6] Utility functions for string in guarded buffers Cyril Hrubis
  2023-08-11 11:56 ` [LTP] [PATCH 1/6] lib: tst_buffers: Add bufs .str and tst_aprintf() Cyril Hrubis
  2023-08-11 11:56 ` Cyril Hrubis
@ 2023-08-11 11:56 ` Cyril Hrubis
  2023-08-11 11:56 ` [LTP] [PATCH 3/6] syscalls/acct01: " Cyril Hrubis
                   ` (3 subsequent siblings)
  6 siblings, 0 replies; 12+ messages in thread
From: Cyril Hrubis @ 2023-08-11 11:56 UTC (permalink / raw)
  To: ltp

Signed-off-by: Cyril Hrubis <chrubis@suse.cz>
---
 testcases/kernel/syscalls/access/access04.c | 36 +++++++++++++++------
 1 file changed, 26 insertions(+), 10 deletions(-)

diff --git a/testcases/kernel/syscalls/access/access04.c b/testcases/kernel/syscalls/access/access04.c
index 424fe6f10..b5764a5dd 100644
--- a/testcases/kernel/syscalls/access/access04.c
+++ b/testcases/kernel/syscalls/access/access04.c
@@ -40,26 +40,32 @@
 #define SNAME1	"symlink1"
 #define SNAME2	"symlink2"
 #define MNT_POINT	"mntpoint"
+#define LONGPATHSIZE (PATH_MAX + 2)
 
 static uid_t uid;
-static char longpathname[PATH_MAX + 2];
+static char *longpathname;
+static char *fname1;
+static char *fname2;
+static char *sname1;
+static char *empty_fname;
+static char *mnt_point;
 
 static struct tcase {
-	const char *pathname;
+	char **pathname;
 	int mode;
 	int exp_errno;
 } tcases[] = {
-	{FNAME1, -1, EINVAL},
-	{"", W_OK, ENOENT},
-	{longpathname, R_OK, ENAMETOOLONG},
-	{FNAME2, R_OK, ENOTDIR},
-	{SNAME1, R_OK, ELOOP},
-	{MNT_POINT, W_OK, EROFS}
+	{&fname1, -1, EINVAL},
+	{&empty_fname, W_OK, ENOENT},
+	{&longpathname, R_OK, ENAMETOOLONG},
+	{&fname2, R_OK, ENOTDIR},
+	{&sname1, R_OK, ELOOP},
+	{&mnt_point, W_OK, EROFS}
 };
 
 static void access_test(struct tcase *tc, const char *user)
 {
-	TST_EXP_FAIL(access(tc->pathname, tc->mode), tc->exp_errno,
+	TST_EXP_FAIL(access(*tc->pathname, tc->mode), tc->exp_errno,
 	             "access as %s", user);
 }
 
@@ -87,7 +93,8 @@ static void setup(void)
 
 	uid = pw->pw_uid;
 
-	memset(longpathname, 'a', sizeof(longpathname) - 1);
+	memset(longpathname, 'a', LONGPATHSIZE - 1);
+	longpathname[LONGPATHSIZE-1] = 0;
 
 	SAFE_TOUCH(FNAME1, 0333, NULL);
 	SAFE_TOUCH(DNAME, 0644, NULL);
@@ -104,4 +111,13 @@ static struct tst_test test = {
 	.mntpoint = MNT_POINT,
 	.setup = setup,
 	.test = verify_access,
+	.bufs = (struct tst_buffers []) {
+		{&fname1, .str = FNAME1},
+		{&fname2, .str = FNAME2},
+		{&sname1, .str = SNAME1},
+		{&empty_fname, .str = ""},
+		{&longpathname, .size = LONGPATHSIZE},
+		{&mnt_point, .str = MNT_POINT},
+		{}
+	}
 };
-- 
2.41.0


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp

^ permalink raw reply related	[flat|nested] 12+ messages in thread

* [LTP] [PATCH 3/6] syscalls/acct01: Make use of guarded buffers
  2023-08-11 11:56 [LTP] [PATCH 0/6] Utility functions for string in guarded buffers Cyril Hrubis
                   ` (2 preceding siblings ...)
  2023-08-11 11:56 ` [LTP] [PATCH 2/6] syscalls/access04: Make use of guarded buffers Cyril Hrubis
@ 2023-08-11 11:56 ` Cyril Hrubis
  2023-08-11 11:56 ` [LTP] [PATCH 4/6] syscalls/chdir01: Make use " Cyril Hrubis
                   ` (2 subsequent siblings)
  6 siblings, 0 replies; 12+ messages in thread
From: Cyril Hrubis @ 2023-08-11 11:56 UTC (permalink / raw)
  To: ltp

Signed-off-by: Cyril Hrubis <chrubis@suse.cz>
---
 testcases/kernel/syscalls/acct/acct01.c | 84 +++++++++++++------------
 1 file changed, 45 insertions(+), 39 deletions(-)

diff --git a/testcases/kernel/syscalls/acct/acct01.c b/testcases/kernel/syscalls/acct/acct01.c
index 254d7b503..52c4d41da 100644
--- a/testcases/kernel/syscalls/acct/acct01.c
+++ b/testcases/kernel/syscalls/acct/acct01.c
@@ -27,17 +27,25 @@
 #define DIR_MODE	(S_IRUSR|S_IWUSR|S_IXUSR|S_IRGRP| \
 			 S_IXGRP|S_IROTH|S_IXOTH)
 #define FILE_EISDIR		"."
-#define FILE_EACCES		"/dev/null"
+#define FILE_EACCESS		"/dev/null"
 #define FILE_ENOENT		"/tmp/does/not/exist"
 #define FILE_ENOTDIR		"./tmpfile/"
-#define TEST_TMPFILE		"./tmpfile"
-#define TEST_ELOOP		"test_file_eloop1"
-#define TEST_ENAMETOOLONG	nametoolong
-#define TEST_EROFS		"mntpoint/file"
+#define FILE_TMPFILE		"./tmpfile"
+#define FILE_ELOOP		"test_file_eloop1"
+#define FILE_EROFS		"ro_mntpoint/file"
 
-static char nametoolong[PATH_MAX+2];
 static struct passwd *ltpuser;
 
+static char *file_eisdir;
+static char *file_eaccess;
+static char *file_enoent;
+static char *file_enotdir;
+static char *file_tmpfile;
+static char *file_eloop;
+static char *file_enametoolong;
+static char *file_erofs;
+static char *file_null;
+
 static void setup_euid(void)
 {
 	SAFE_SETEUID(ltpuser->pw_uid);
@@ -49,21 +57,21 @@ static void cleanup_euid(void)
 }
 
 static struct test_case {
-	char *filename;
-	char *exp_errval;
+	char **filename;
+	char *desc;
 	int exp_errno;
 	void (*setupfunc) ();
 	void (*cleanfunc) ();
 } tcases[] = {
-	{FILE_EISDIR, "EISDIR",  EISDIR,  NULL,   NULL},
-	{FILE_EACCES, "EACCES",  EACCES,  NULL,   NULL},
-	{FILE_ENOENT, "ENOENT",  ENOENT,  NULL,   NULL},
-	{FILE_ENOTDIR, "ENOTDIR", ENOTDIR, NULL,   NULL},
-	{TEST_TMPFILE, "EPERM",   EPERM,   setup_euid, cleanup_euid},
-	{NULL,       "EPERM",   EPERM,   setup_euid, cleanup_euid},
-	{TEST_ELOOP, "ELOOP",        ELOOP,        NULL, NULL},
-	{TEST_ENAMETOOLONG, "ENAMETOOLONG", ENAMETOOLONG, NULL, NULL},
-	{TEST_EROFS, "EROFS",        EROFS,        NULL, NULL},
+	{&file_eisdir,  FILE_EISDIR,  EISDIR,  NULL,   NULL},
+	{&file_eaccess, FILE_EACCESS, EACCES,  NULL,   NULL},
+	{&file_enoent,  FILE_ENOENT,  ENOENT,  NULL,   NULL},
+	{&file_enotdir, FILE_ENOTDIR, ENOTDIR, NULL,   NULL},
+	{&file_tmpfile, FILE_TMPFILE, EPERM,   setup_euid, cleanup_euid},
+	{&file_null,    "NULL",       EPERM,   setup_euid, cleanup_euid},
+	{&file_eloop,   FILE_ELOOP,   ELOOP,        NULL, NULL},
+	{&file_enametoolong, "aaaa...", ENAMETOOLONG, NULL, NULL},
+	{&file_erofs,   FILE_EROFS,   EROFS,        NULL, NULL},
 };
 
 static void setup(void)
@@ -76,10 +84,10 @@ static void setup(void)
 
 	ltpuser = SAFE_GETPWNAM("nobody");
 
-	fd = SAFE_CREAT(TEST_TMPFILE, 0777);
+	fd = SAFE_CREAT(FILE_TMPFILE, 0777);
 	SAFE_CLOSE(fd);
 
-	TEST(acct(TEST_TMPFILE));
+	TEST(acct(FILE_TMPFILE));
 	if (TST_RET == -1)
 		tst_brk(TBROK | TTERRNO, "acct failed unexpectedly");
 
@@ -89,11 +97,11 @@ static void setup(void)
 		tst_brk(TBROK | TTERRNO, "acct(NULL) failed");
 
 	/* ELOOP SETTING */
-	SAFE_SYMLINK(TEST_ELOOP, "test_file_eloop2");
-	SAFE_SYMLINK("test_file_eloop2", TEST_ELOOP);
+	SAFE_SYMLINK(FILE_ELOOP, "test_file_eloop2");
+	SAFE_SYMLINK("test_file_eloop2", FILE_ELOOP);
 
-	/* ENAMETOOLONG SETTING */
-	memset(nametoolong, 'a', PATH_MAX+1);
+	memset(file_enametoolong, 'a', PATH_MAX+1);
+	file_enametoolong[PATH_MAX+1] = 0;
 }
 
 static void verify_acct(unsigned int nr)
@@ -103,31 +111,29 @@ static void verify_acct(unsigned int nr)
 	if (tcase->setupfunc)
 		tcase->setupfunc();
 
-	TEST(acct(tcase->filename));
+	TST_EXP_FAIL(acct(*tcase->filename), tcase->exp_errno,
+	             "acct(%s)", tcase->desc);
 
 	if (tcase->cleanfunc)
 		tcase->cleanfunc();
-
-	if (TST_RET != -1) {
-		tst_res(TFAIL, "acct(%s) succeeded unexpectedly",
-				tcase->filename);
-		return;
-	}
-
-	if (TST_ERR == tcase->exp_errno) {
-		tst_res(TPASS | TTERRNO, "acct() failed as expected");
-	} else {
-		tst_res(TFAIL | TTERRNO,
-				"acct() failed, expected: %s",
-				tst_strerrno(tcase->exp_errno));
-	}
 }
 
 static struct tst_test test = {
 	.needs_root = 1,
-	.mntpoint = "mntpoint",
+	.mntpoint = "ro_mntpoint",
 	.needs_rofs = 1,
 	.tcnt = ARRAY_SIZE(tcases),
 	.setup = setup,
 	.test = verify_acct,
+	.bufs = (struct tst_buffers []) {
+		{&file_eisdir, .str = FILE_EISDIR},
+		{&file_eaccess, .str = FILE_EACCESS},
+		{&file_enoent, .str = FILE_ENOENT},
+		{&file_enotdir, .str = FILE_ENOTDIR},
+		{&file_tmpfile, .str = FILE_TMPFILE},
+		{&file_eloop, .str = FILE_ELOOP},
+		{&file_enametoolong, .size = PATH_MAX+2},
+		{&file_erofs, .str = FILE_EROFS},
+		{}
+	}
 };
-- 
2.41.0


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp

^ permalink raw reply related	[flat|nested] 12+ messages in thread

* [LTP] [PATCH 4/6] syscalls/chdir01: Make use use of guarded buffers
  2023-08-11 11:56 [LTP] [PATCH 0/6] Utility functions for string in guarded buffers Cyril Hrubis
                   ` (3 preceding siblings ...)
  2023-08-11 11:56 ` [LTP] [PATCH 3/6] syscalls/acct01: " Cyril Hrubis
@ 2023-08-11 11:56 ` Cyril Hrubis
  2023-08-11 11:56 ` [LTP] [PATCH 5/6] syscalls/chmod01: Make " Cyril Hrubis
  2023-08-11 11:56 ` [LTP] [PATCH 6/6] syscalls/chroot03: " Cyril Hrubis
  6 siblings, 0 replies; 12+ messages in thread
From: Cyril Hrubis @ 2023-08-11 11:56 UTC (permalink / raw)
  To: ltp

Signed-off-by: Cyril Hrubis <chrubis@suse.cz>
---
 testcases/kernel/syscalls/chdir/chdir01.c | 55 +++++++++++++++--------
 1 file changed, 36 insertions(+), 19 deletions(-)

diff --git a/testcases/kernel/syscalls/chdir/chdir01.c b/testcases/kernel/syscalls/chdir/chdir01.c
index e4080e3f4..d50a8f50c 100644
--- a/testcases/kernel/syscalls/chdir/chdir01.c
+++ b/testcases/kernel/syscalls/chdir/chdir01.c
@@ -31,18 +31,27 @@ static char *workdir;
 static int skip_symlinks, skip_blocked;
 static struct passwd *ltpuser;
 
+static char *file_name;
+static char *blocked_name;
+static char *dir_name;
+static char *cwd_name;
+static char *parent_name;
+static char *root_name;
+static char *missing_name;
+static char *link_name;
+
 static struct test_case {
-	const char *name;
+	char **name;
 	int root_ret, root_err, nobody_ret, nobody_err;
 } testcase_list[] = {
-	{FILE_NAME, -1, ENOTDIR, -1, ENOTDIR},
-	{BLOCKED_NAME, 0, 0, -1, EACCES},
-	{DIR_NAME, 0, 0, 0, 0},
-	{".", 0, 0, 0, 0},
-	{"..", 0, 0, 0, 0},
-	{"/", 0, 0, 0, 0},
-	{"missing", -1, ENOENT, -1, ENOENT},
-	{LINK_NAME1, -1, ELOOP, -1, ELOOP},
+	{&file_name, -1, ENOTDIR, -1, ENOTDIR},
+	{&blocked_name, 0, 0, -1, EACCES},
+	{&dir_name, 0, 0, 0, 0},
+	{&cwd_name, 0, 0, 0, 0},
+	{&parent_name, 0, 0, 0, 0},
+	{&root_name, 0, 0, 0, 0},
+	{&missing_name, -1, ENOENT, -1, ENOENT},
+	{&link_name, -1, ELOOP, -1, ELOOP},
 };
 
 static void setup(void)
@@ -53,8 +62,6 @@ static void setup(void)
 
 	umask(0);
 
-	SAFE_MOUNT(tst_device->dev, MNTPOINT, tst_device->fs_type, 0, NULL);
-
 	cwd = SAFE_GETCWD(NULL, 0);
 	workdir = SAFE_MALLOC(strlen(cwd) + strlen(MNTPOINT) + 2);
 	sprintf(workdir, "%s/%s", cwd, MNTPOINT);
@@ -109,7 +116,7 @@ static void run(unsigned int n)
 {
 	struct test_case *tc = testcase_list + n;
 
-	tst_res(TINFO, "Testing '%s'", tc->name);
+	tst_res(TINFO, "Testing '%s'", *tc->name);
 
 	if (tc->root_err == ELOOP && skip_symlinks) {
 		tst_res(TCONF, "Skipping symlink loop test, not supported");
@@ -119,8 +126,8 @@ static void run(unsigned int n)
 	/* Reset current directory to mountpoint */
 	SAFE_CHDIR(workdir);
 
-	TEST(chdir(tc->name));
-	check_result("root", tc->name, tc->root_ret, tc->root_err);
+	TEST(chdir(*tc->name));
+	check_result("root", *tc->name, tc->root_ret, tc->root_err);
 
 	if (tc->nobody_err == EACCES && skip_blocked) {
 		tst_res(TCONF, "Skipping unprivileged permission test, "
@@ -130,25 +137,35 @@ static void run(unsigned int n)
 
 	SAFE_CHDIR(workdir);
 	SAFE_SETEUID(ltpuser->pw_uid);
-	TEST(chdir(tc->name));
+	TEST(chdir(*tc->name));
 	SAFE_SETEUID(0);
-	check_result(TESTUSER, tc->name, tc->nobody_ret, tc->nobody_err);
+	check_result(TESTUSER, *tc->name, tc->nobody_ret, tc->nobody_err);
 }
 
 static void cleanup(void)
 {
 	SAFE_CHDIR("..");
-	tst_umount(workdir);
 	free(workdir);
 }
 
 static struct tst_test test = {
 	.needs_root = 1,
-	.format_device = 1,
+	.mount_device = 1,
 	.mntpoint = MNTPOINT,
 	.all_filesystems = 1,
 	.test = run,
 	.tcnt = ARRAY_SIZE(testcase_list),
 	.setup = setup,
-	.cleanup = cleanup
+	.cleanup = cleanup,
+	.bufs = (struct tst_buffers []) {
+		{&file_name, .str = FILE_NAME},
+		{&blocked_name, .str = BLOCKED_NAME},
+		{&dir_name, .str = DIR_NAME},
+		{&cwd_name, .str = "."},
+		{&parent_name, .str = ".."},
+		{&root_name, .str = "/"},
+		{&missing_name, .str = "does_not_exist"},
+		{&link_name, .str = LINK_NAME1},
+		{}
+	}
 };
-- 
2.41.0


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp

^ permalink raw reply related	[flat|nested] 12+ messages in thread

* [LTP] [PATCH 5/6] syscalls/chmod01: Make use of guarded buffers
  2023-08-11 11:56 [LTP] [PATCH 0/6] Utility functions for string in guarded buffers Cyril Hrubis
                   ` (4 preceding siblings ...)
  2023-08-11 11:56 ` [LTP] [PATCH 4/6] syscalls/chdir01: Make use " Cyril Hrubis
@ 2023-08-11 11:56 ` Cyril Hrubis
  2023-08-11 11:56 ` [LTP] [PATCH 6/6] syscalls/chroot03: " Cyril Hrubis
  6 siblings, 0 replies; 12+ messages in thread
From: Cyril Hrubis @ 2023-08-11 11:56 UTC (permalink / raw)
  To: ltp

Signed-off-by: Cyril Hrubis <chrubis@suse.cz>
---
 testcases/kernel/syscalls/chmod/chmod01.c | 28 +++++++++++++++--------
 1 file changed, 18 insertions(+), 10 deletions(-)

diff --git a/testcases/kernel/syscalls/chmod/chmod01.c b/testcases/kernel/syscalls/chmod/chmod01.c
index 9f5ec4c67..b3b828ac3 100644
--- a/testcases/kernel/syscalls/chmod/chmod01.c
+++ b/testcases/kernel/syscalls/chmod/chmod01.c
@@ -19,13 +19,16 @@
 
 static int modes[] = {0, 07, 070, 0700, 0777, 02777, 04777, 06777};
 
+static char *test_dir;
+static char *test_file;
+
 static struct variant {
-	char *name;
+	char **name;
 	unsigned int mode_mask;
 	char *desc;
 } variants[] = {
-	{TESTFILE, S_IFREG, "verify permissions of file"},
-	{TESTDIR, S_IFDIR, "verify permissions of directory"},
+	{&test_file, S_IFREG, "verify permissions of file"},
+	{&test_dir, S_IFDIR, "verify permissions of directory"},
 };
 
 static void verify_chmod(unsigned int n)
@@ -34,21 +37,21 @@ static void verify_chmod(unsigned int n)
 	int mode = modes[n];
 	struct variant *tc = &variants[tst_variant];
 
-	TST_EXP_PASS(chmod(tc->name, mode), "chmod(%s, %04o)",
-	             tc->name, mode);
+	TST_EXP_PASS(chmod(*tc->name, mode), "chmod(%s, %04o)",
+	             *tc->name, mode);
 
 	if (!TST_PASS)
 		return;
 
-	SAFE_STAT(tc->name, &stat_buf);
+	SAFE_STAT(*tc->name, &stat_buf);
 	stat_buf.st_mode &= ~tc->mode_mask;
 
 	if (stat_buf.st_mode == (unsigned int)mode) {
 		tst_res(TPASS, "stat(%s) mode=%04o",
-				tc->name, stat_buf.st_mode);
+				*tc->name, stat_buf.st_mode);
 	} else {
 		tst_res(TFAIL, "stat(%s) mode=%04o",
-				tc->name, stat_buf.st_mode);
+				*tc->name, stat_buf.st_mode);
 	}
 }
 
@@ -57,9 +60,9 @@ static void setup(void)
 	tst_res(TINFO, "Testing variant: %s", variants[tst_variant].desc);
 
 	if (tst_variant)
-		SAFE_MKDIR(variants[tst_variant].name, MODE);
+		SAFE_MKDIR(*variants[tst_variant].name, MODE);
 	else
-		SAFE_TOUCH(variants[tst_variant].name, MODE, NULL);
+		SAFE_TOUCH(*variants[tst_variant].name, MODE, NULL);
 }
 
 static struct tst_test test = {
@@ -68,4 +71,9 @@ static struct tst_test test = {
 	.tcnt = ARRAY_SIZE(modes),
 	.test = verify_chmod,
 	.needs_tmpdir = 1,
+	.bufs = (struct tst_buffers []) {
+		{&test_file, .str = TESTFILE},
+		{&test_dir, .str = TESTDIR},
+		{}
+	}
 };
-- 
2.41.0


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp

^ permalink raw reply related	[flat|nested] 12+ messages in thread

* [LTP] [PATCH 6/6] syscalls/chroot03: Make use of guarded buffers
  2023-08-11 11:56 [LTP] [PATCH 0/6] Utility functions for string in guarded buffers Cyril Hrubis
                   ` (5 preceding siblings ...)
  2023-08-11 11:56 ` [LTP] [PATCH 5/6] syscalls/chmod01: Make " Cyril Hrubis
@ 2023-08-11 11:56 ` Cyril Hrubis
  6 siblings, 0 replies; 12+ messages in thread
From: Cyril Hrubis @ 2023-08-11 11:56 UTC (permalink / raw)
  To: ltp

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


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp

^ permalink raw reply related	[flat|nested] 12+ messages in thread

* Re: [LTP] [PATCH 1/6] lib: tst_buffers: Add bufs .str and tst_aprintf()
  2023-08-11 11:56 ` [LTP] [PATCH 1/6] lib: tst_buffers: Add bufs .str and tst_aprintf() Cyril Hrubis
@ 2023-08-11 12:01   ` Cyril Hrubis
  2023-08-15  9:43   ` Li Wang
  1 sibling, 0 replies; 12+ messages in thread
From: Cyril Hrubis @ 2023-08-11 12:01 UTC (permalink / raw)
  To: ltp

Hi!
Looks like I've send this patch twice by an accident, sorry.

-- 
Cyril Hrubis
chrubis@suse.cz

-- 
Mailing list info: https://lists.linux.it/listinfo/ltp

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: [LTP] [PATCH 1/6] lib: tst_buffers: Add bufs .str and tst_aprintf()
  2023-08-11 11:56 ` [LTP] [PATCH 1/6] lib: tst_buffers: Add bufs .str and tst_aprintf() Cyril Hrubis
  2023-08-11 12:01   ` Cyril Hrubis
@ 2023-08-15  9:43   ` Li Wang
  2023-08-18  9:12     ` Cyril Hrubis
  2023-08-18 14:17     ` Cyril Hrubis
  1 sibling, 2 replies; 12+ messages in thread
From: Li Wang @ 2023-08-15  9:43 UTC (permalink / raw)
  To: Cyril Hrubis; +Cc: ltp

On Fri, Aug 11, 2023 at 7:56 PM Cyril Hrubis <chrubis@suse.cz> wrote:

> Signed-off-by: Cyril Hrubis <chrubis@suse.cz>
> ---
>  include/tst_buffers.h | 11 +++++++++++
>  lib/tst_buffers.c     | 28 +++++++++++++++++++++++++---
>  2 files changed, 36 insertions(+), 3 deletions(-)
>
> diff --git a/include/tst_buffers.h b/include/tst_buffers.h
> index d19ac8cf0..b5f355f0f 100644
> --- a/include/tst_buffers.h
> +++ b/include/tst_buffers.h
> @@ -25,6 +25,11 @@ struct tst_buffers {
>          * Array of iov buffer sizes terminated by -1.
>          */
>         int *iov_sizes;
> +       /*
> +        * If size and iov_sizes is NULL this is the string we want to
> strdup()
> +        * into the buffer.
> +        */
> +       char *str;
>  };
>
>  /*
> @@ -46,6 +51,12 @@ char *tst_strdup(const char *str);
>   */
>  void *tst_alloc(size_t size);
>
> +/*
> + * Printf into a guarded buffer.
> + */
> +char *tst_aprintf(const char *fmt, ...)
> +      __attribute__((format (printf, 1, 2)));
> +
>  /*
>   * Allocates iovec structure including the buffers.
>   *
> diff --git a/lib/tst_buffers.c b/lib/tst_buffers.c
> index b8b597a12..b0bd359eb 100644
> --- a/lib/tst_buffers.c
> +++ b/lib/tst_buffers.c
> @@ -5,6 +5,7 @@
>
>  #include <sys/mman.h>
>  #include <stdlib.h>
> +#include <stdio.h>
>  #define TST_NO_DEFAULT_MAIN
>  #include "tst_test.h"
>
> @@ -76,6 +77,25 @@ void *tst_alloc(size_t size)
>         return ret + map->buf_shift;
>  }
>
> +char *tst_aprintf(const char *fmt, ...)
>

I didn't see any place to invoke this function in the patchset,
does it prepare for the coming case or others?

Anyway, the whole patch set looks pretty good to me.

Reviewed-by: Li Wang <liwang@redhat.com>



> +{
> +       va_list va;
> +       int len;
> +       char *ret;
> +
> +        va_start(va, fmt);
> +        len = vsnprintf(NULL, 0, fmt, va)+1;
> +        va_end(va);
> +
> +       ret = tst_alloc(len);
> +
> +       va_start(va, fmt);
> +        vsprintf(ret, fmt, va);
> +        va_end(va);
> +
> +       return ret;
> +}
> +
>  static int count_iovec(int *sizes)
>  {
>         int ret = 0;
> @@ -115,15 +135,17 @@ void tst_buffers_alloc(struct tst_buffers bufs[])
>         for (i = 0; bufs[i].ptr; i++) {
>                 if (bufs[i].size)
>                         *((void**)bufs[i].ptr) = tst_alloc(bufs[i].size);
> -               else
> +               else if (bufs[i].iov_sizes)
>                         *((void**)bufs[i].ptr) =
> tst_iovec_alloc(bufs[i].iov_sizes);
> +               else
> +                       *((void**)bufs[i].ptr) = tst_strdup(bufs[i].str);
>         }
>  }
>
>  char *tst_strdup(const char *str)
>  {
> -       size_t len = strlen(str);
> -       char *ret = tst_alloc(len + 1);
> +       char *ret = tst_alloc(strlen(str) + 1);
> +
>         return strcpy(ret, str);
>  }
>
> --
> 2.41.0
>
>
> --
> Mailing list info: https://lists.linux.it/listinfo/ltp
>
>

-- 
Regards,
Li Wang

-- 
Mailing list info: https://lists.linux.it/listinfo/ltp

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: [LTP] [PATCH 1/6] lib: tst_buffers: Add bufs .str and tst_aprintf()
  2023-08-15  9:43   ` Li Wang
@ 2023-08-18  9:12     ` Cyril Hrubis
  2023-08-18 14:17     ` Cyril Hrubis
  1 sibling, 0 replies; 12+ messages in thread
From: Cyril Hrubis @ 2023-08-18  9:12 UTC (permalink / raw)
  To: Li Wang; +Cc: ltp

Hi!
> I didn't see any place to invoke this function in the patchset,
> does it prepare for the coming case or others?

Indeed, this is added so that we can use it in setup functions for the
the *at() test rewrites done by Yang Xu.

-- 
Cyril Hrubis
chrubis@suse.cz

-- 
Mailing list info: https://lists.linux.it/listinfo/ltp

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: [LTP] [PATCH 1/6] lib: tst_buffers: Add bufs .str and tst_aprintf()
  2023-08-15  9:43   ` Li Wang
  2023-08-18  9:12     ` Cyril Hrubis
@ 2023-08-18 14:17     ` Cyril Hrubis
  1 sibling, 0 replies; 12+ messages in thread
From: Cyril Hrubis @ 2023-08-18 14:17 UTC (permalink / raw)
  To: Li Wang; +Cc: ltp

Hi!
> Anyway, the whole patch set looks pretty good to me.
> 
> Reviewed-by: Li Wang <liwang@redhat.com>

Pushed, thanks for the review.

-- 
Cyril Hrubis
chrubis@suse.cz

-- 
Mailing list info: https://lists.linux.it/listinfo/ltp

^ permalink raw reply	[flat|nested] 12+ messages in thread

end of thread, other threads:[~2023-08-18 14:16 UTC | newest]

Thread overview: 12+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2023-08-11 11:56 [LTP] [PATCH 0/6] Utility functions for string in guarded buffers Cyril Hrubis
2023-08-11 11:56 ` [LTP] [PATCH 1/6] lib: tst_buffers: Add bufs .str and tst_aprintf() Cyril Hrubis
2023-08-11 12:01   ` Cyril Hrubis
2023-08-15  9:43   ` Li Wang
2023-08-18  9:12     ` Cyril Hrubis
2023-08-18 14:17     ` Cyril Hrubis
2023-08-11 11:56 ` Cyril Hrubis
2023-08-11 11:56 ` [LTP] [PATCH 2/6] syscalls/access04: Make use of guarded buffers Cyril Hrubis
2023-08-11 11:56 ` [LTP] [PATCH 3/6] syscalls/acct01: " Cyril Hrubis
2023-08-11 11:56 ` [LTP] [PATCH 4/6] syscalls/chdir01: Make use " Cyril Hrubis
2023-08-11 11:56 ` [LTP] [PATCH 5/6] syscalls/chmod01: Make " Cyril Hrubis
2023-08-11 11:56 ` [LTP] [PATCH 6/6] syscalls/chroot03: " Cyril Hrubis

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox