Skip to contentSkip to conversation

clock_nanosleep01: Avoid dereferencing bad pointers in libc

PATCH In review Richard Palethorpe via ltp 3 participants 6 messages Oct 5, 2021 – Oct 11, 2021

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/2 clock_nanosleep01: Avoid dereferencing bad pointers in libc

In 32-bit (regardless of kernel bits) glibc and musl will usually dereference the timespec pointers and try to read them. In some cases this might be avoidable, but they must do it in others.

Passing invalid pointers is undefined in POSIX. In any case, AFAICT libc would have to catch the signal in order to guarantee EFAULT is returned.

It's also reported that some other libc implementations dereference the pointers even in 64bit. If this happens then it blocks more useful testing. So this avoids passing bad pointers to libc.

Also turn test_type into a bitmask to reduce if statement length.

---

V2:
* Also remove check on 64-bit
* Avoid pause waiting for child proc
* Use bitmask

 .../clock_nanosleep/clock_nanosleep01.c         | 17 ++++++++++++-----
 1 file changed, 12 insertions(+), 5 deletions(-)
testcases/kernel/syscalls/clock_nanosleep/clock_nanosleep01.c +12 -5
index 382497918..6d81151b6 100644
@@ -27,10 +27,10 @@ static void sighandler(int sig LTP_ATTRIBUTE_UNUSED)
 }
 
 enum test_type {
-	NORMAL,
-	SEND_SIGINT,
-	BAD_TS_ADDR_REQ,
-	BAD_TS_ADDR_REM,
+	NORMAL = 1,
+	SEND_SIGINT = 2,
+	BAD_TS_ADDR_REQ = 4,
+	BAD_TS_ADDR_REM = 8,
 };
 
 #define TYPE_NAME(x) .ttype = x, .desc = #x
@@ -138,7 +138,14 @@ static void do_test(unsigned int i)
 
 	tst_res(TINFO, "case %s", tc->desc);
 
-	if (tc->ttype == SEND_SIGINT || tc->ttype == BAD_TS_ADDR_REM)
+	if (tc->ttype & (BAD_TS_ADDR_REQ | BAD_TS_ADDR_REM) &&
+	    tv->clock_nanosleep == libc_clock_nanosleep) {
+		tst_res(TCONF,
+			"The libc wrapper may dereference req or rem");
+		return;
+	}
+
+	if (tc->ttype & (SEND_SIGINT | BAD_TS_ADDR_REM))
 		pid = create_sig_proc(SIGINT, 40, 500000);
 
 	tst_ts_set_sec(rq, tc->tv_sec);
Signature · 5 lines
2.33.0


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
#1 lore raw

PATCH v2 2/2 clock_nanosleep01: Add space to make make-check happy

---
 testcases/kernel/syscalls/clock_nanosleep/clock_nanosleep01.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
testcases/kernel/syscalls/clock_nanosleep/clock_nanosleep01.c +1 -1
index 6d81151b6..eef8a5992 100644
@@ -199,7 +199,7 @@ static void do_test(unsigned int i)
 		}
 
 		if (remain_ms > expect_ms) {
-			tst_res(TFAIL| TTERRNO,
+			tst_res(TFAIL | TTERRNO,
 				"remaining time > requested time (%lld > %lld)",
 				remain_ms, expect_ms);
 			return;
Signature · 5 lines
2.33.0


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
#3 lore raw

Re: [LTP] [PATCH v2 2/2] clock_nanosleep01: Add space to make make-check happy

Hi,

Quoted 4 lines · Cyril Hrubis

Hi!
Obviously fine.

Pushed thanks!

Signature · 5 lines
Thank you,
Richard.

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