All of lore.kernel.org
 help / color / mirror / Atom feed
From: Tudor Cretu <tudor.cretu@arm.com>
To: ltp@lists.linux.it
Subject: [LTP] [PATCH v2 4/4] syscalls/statfs: Accept segfault instead of EFAULT
Date: Mon, 22 Aug 2022 12:39:19 +0100	[thread overview]
Message-ID: <20220822113919.196953-5-tudor.cretu@arm.com> (raw)
In-Reply-To: <20220822113919.196953-1-tudor.cretu@arm.com>

The [f]statfs02 testsuites check that [f]statfs returns EFUALT when the
provided buf parameter is invalid. There are cases in which the supported
libcs don't exhibit this behaviour.

glibc versions newer than 2.34 and on systems that support [f]statfs64,
call the syscall with a local struct statfs and then copy the result
into buf. This throws a segfault for an invalid buf. musl dereferences buf
before the syscall is called and, similarly, throws a segfault.

Implement a sighandler to handle the cases where a segfault is thrown
instead of returning EFAULT.

Signed-off-by: Tudor Cretu <tudor.cretu@arm.com>
---
 testcases/kernel/syscalls/fstatfs/fstatfs02.c | 60 ++++++++++++++-----
 testcases/kernel/syscalls/statfs/statfs02.c   | 32 +++++++++-
 2 files changed, 73 insertions(+), 19 deletions(-)

diff --git a/testcases/kernel/syscalls/fstatfs/fstatfs02.c b/testcases/kernel/syscalls/fstatfs/fstatfs02.c
index db2230f82..e46b9df23 100644
--- a/testcases/kernel/syscalls/fstatfs/fstatfs02.c
+++ b/testcases/kernel/syscalls/fstatfs/fstatfs02.c
@@ -21,6 +21,8 @@
  *	Testcase to check fstatfs() sets errno correctly.
  */
 
+#include <setjmp.h>
+#include <signal.h>
 #include <sys/vfs.h>
 #include <sys/types.h>
 #include <sys/statfs.h>
@@ -28,9 +30,6 @@
 #include "test.h"
 #include "safe_macros.h"
 
-static void setup(void);
-static void cleanup(void);
-
 char *TCID = "fstatfs02";
 
 static struct statfs buf;
@@ -53,6 +52,13 @@ static struct test_case_t {
 
 int TST_TOTAL = ARRAY_SIZE(TC);
 
+static int sig_caught;
+static sigjmp_buf env;
+
+static void setup(void);
+static void cleanup(void);
+static void fstatfs_verify(const struct test_case_t *);
+
 int main(int ac, char **av)
 {
 	int lc;
@@ -67,23 +73,20 @@ int main(int ac, char **av)
 		tst_count = 0;
 
 		for (i = 0; i < TST_TOTAL; i++) {
+			sig_caught = 0;
+			if (sigsetjmp(env, 1) == 0)
+				fstatfs_verify(&TC[i]);
 
-			TEST(fstatfs(TC[i].fd, TC[i].sbuf));
+			if (!sig_caught)
+				continue;
 
-			if (TEST_RETURN != -1) {
-				tst_resm(TFAIL, "call succeeded unexpectedly");
+			if (TC[i].error == EFAULT && sig_caught == SIGSEGV) {
+				tst_resm(TINFO, "received SIGSEGV instead of returning EFAULT");
+				tst_resm(TPASS | TTERRNO, "expected failure");
 				continue;
 			}
 
-			if (TEST_ERRNO == TC[i].error) {
-				tst_resm(TPASS, "expected failure - "
-					 "errno = %d : %s", TEST_ERRNO,
-					 strerror(TEST_ERRNO));
-			} else {
-				tst_resm(TFAIL, "unexpected error - %d : %s - "
-					 "expected %d", TEST_ERRNO,
-					 strerror(TEST_ERRNO), TC[i].error);
-			}
+			tst_resm(TFAIL, "Received an unexpected signal: %d", sig_caught);
 		}
 	}
 	cleanup();
@@ -91,9 +94,16 @@ int main(int ac, char **av)
 	tst_exit();
 }
 
+static void sighandler(int sig)
+{
+	sig_caught = sig;
+	siglongjmp(env, 1);
+
+}
+
 static void setup(void)
 {
-	tst_sig(NOFORK, DEF_HANDLER, cleanup);
+	tst_sig(NOFORK, sighandler, cleanup);
 
 	TEST_PAUSE;
 
@@ -103,6 +113,24 @@ static void setup(void)
 #endif
 }
 
+static void fstatfs_verify(const struct test_case_t *test)
+{
+	TEST(fstatfs(test->fd, test->sbuf));
+
+	if (TEST_RETURN != -1) {
+		tst_resm(TFAIL, "call succeeded unexpectedly");
+		return;
+	}
+
+	if (TEST_ERRNO == test->error) {
+		tst_resm(TPASS, "expected failure - errno = %d : %s",
+			 TEST_ERRNO, strerror(TEST_ERRNO));
+	} else {
+		tst_resm(TFAIL, "unexpected error - %d : %s - expected %d",
+			 TEST_ERRNO, strerror(TEST_ERRNO), test->error);
+	}
+}
+
 static void cleanup(void)
 {
 #ifndef UCLINUX
diff --git a/testcases/kernel/syscalls/statfs/statfs02.c b/testcases/kernel/syscalls/statfs/statfs02.c
index 279665f86..7f150d1d9 100644
--- a/testcases/kernel/syscalls/statfs/statfs02.c
+++ b/testcases/kernel/syscalls/statfs/statfs02.c
@@ -32,6 +32,8 @@
  *		ELOOP.
  */
 
+#include <setjmp.h>
+#include <signal.h>
 #include <sys/types.h>
 #include <sys/statfs.h>
 #include <sys/stat.h>
@@ -70,6 +72,10 @@ static struct test_case_t {
 };
 
 int TST_TOTAL = ARRAY_SIZE(TC);
+
+static int sig_caught;
+static sigjmp_buf env;
+
 static void setup(void);
 static void cleanup(void);
 static void statfs_verify(const struct test_case_t *);
@@ -85,17 +91,37 @@ int main(int ac, char **av)
 
 	for (lc = 0; TEST_LOOPING(lc); lc++) {
 		tst_count = 0;
-		for (i = 0; i < TST_TOTAL; i++)
-			statfs_verify(&TC[i]);
+		for (i = 0; i < TST_TOTAL; i++) {
+			sig_caught = 0;
+			if (sigsetjmp(env, 1) == 0)
+				statfs_verify(&TC[i]);
+
+			if (!sig_caught)
+				continue;
+
+			if (TC[i].exp_error == EFAULT && sig_caught == SIGSEGV) {
+				tst_resm(TINFO, "received SIGSEGV instead of returning EFAULT");
+				tst_resm(TPASS | TTERRNO, "expected failure");
+				continue;
+			}
+
+			tst_resm(TFAIL, "Received an unexpected signal: %d", sig_caught);
+		}
 	}
 
 	cleanup();
 	tst_exit();
 }
 
+static void sighandler(int sig)
+{
+	sig_caught = sig;
+	siglongjmp(env, 1);
+}
+
 static void setup(void)
 {
-	tst_sig(NOFORK, DEF_HANDLER, cleanup);
+	tst_sig(NOFORK, sighandler, cleanup);
 
 	TEST_PAUSE;
 
-- 
2.25.1


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

  parent reply	other threads:[~2022-08-22 11:39 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-08-22 11:39 [LTP] [PATCH v2 0/4] syscalls: Fix various syscall tests when compiled with Musl Tudor Cretu
2022-08-22 11:39 ` [LTP] [PATCH v2 1/4] lib: Fix initialization of recursive mutex Tudor Cretu
2022-08-23  9:22   ` Petr Vorel
2022-08-22 11:39 ` [LTP] [PATCH v2 2/4] syscalls/mprotect01: Invoke the syscall directly instead of the libc wrapper Tudor Cretu
2022-08-23  9:23   ` Petr Vorel
2022-08-22 11:39 ` [LTP] [PATCH v2 3/4] syscalls/prctl04: Allow rt_sigprocmask in the syscall filter Tudor Cretu
2022-08-23  9:41   ` Petr Vorel
2022-08-23 13:24     ` Cyril Hrubis
2022-08-23 14:59     ` Tudor Cretu
2022-08-23 15:06       ` Petr Vorel
2022-08-22 11:39 ` Tudor Cretu [this message]
2022-08-22 14:11   ` [LTP] [PATCH v2 4/4] syscalls/statfs: Accept segfault instead of EFAULT Cyril Hrubis

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20220822113919.196953-5-tudor.cretu@arm.com \
    --to=tudor.cretu@arm.com \
    --cc=ltp@lists.linux.it \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.