Skip to contentSkip to conversation

clock_nanosleep01: Avoid dereferencing bad pointers in libc on 32bit

PATCH Superseded Richard Palethorpe via ltp 4 participants 6 messages Sep 29, 2021 – Oct 5, 2021

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/2 clock_nanosleep01: Avoid dereferencing bad pointers in libc on 32bit

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.

---

Possibly we shouldn't test things like this at all through libc wrappers.

 .../kernel/syscalls/clock_nanosleep/clock_nanosleep01.c   | 8 ++++++++
 1 file changed, 8 insertions(+)
testcases/kernel/syscalls/clock_nanosleep/clock_nanosleep01.c +8 -0
index 382497918..7dc5a043d 100644
@@ -154,6 +154,14 @@ static void do_test(unsigned int i)
 	else
 		remain = tst_ts_get(rm);
 
+	if ((request == bad_addr || remain == bad_addr) &&
+	    tv->clock_nanosleep == libc_clock_nanosleep &&
+	    sizeof(long) < 8) {
+		tst_res(TCONF,
+			"The libc 32-bit wrapper may dereference req or rem");
+		return;
+	}
+
 	TEST(tv->clock_nanosleep(tc->clk_id, tc->flags, request, remain));
 
 	if (tv->clock_nanosleep == libc_clock_nanosleep) {
Signature · 5 lines
2.33.0


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

PATCH 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 7dc5a043d..d7b14fd94 100644
@@ -200,7 +200,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
Reviewed-by on v1 2/2
#2 lore raw

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

Obviously correct.

Kind regards,
Petr

Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
Reviewed-by on v1 1/2
#3 lore raw
In reply to Richard Palethorpe via ltp

Hi Richie,

Quoted 7 lines

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.

LGTM.

Quoted 2 lines

Possibly we shouldn't test things like this at all through libc wrappers.

Only for 32bit or also for 64 bit? Anyway, there has always been some cases where bad addr testing was problematic (e.g. non-intel arch).

Kind regards,
Petr

Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
Acked-by
#4 lore raw
Quoted 10 lines · Petr Vorel

Hi Richie,

Quoted 7 lines

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.

Quoted 9 lines

LGTM.

Quoted 2 lines

Possibly we shouldn't test things like this at all through libc wrappers.

Only for 32bit or also for 64 bit? Anyway, there has always been some cases where bad addr testing was problematic (e.g. non-intel arch).

I'd skip it for both, I recall that some implementations were crashing.

Quoted 8 lines

Kind regards,
Petr

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

Signature · 2 lines
-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
#5 lore raw

Hello Jan,

Quoted 16 lines · Jan Stancek

On Mon, Oct 4, 2021 at 8:07 AM Petr Vorel <[email protected]> wrote:

Hi Richie,

Quoted 7 lines

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.

LGTM.

Quoted 7 lines
Quoted 2 lines

Possibly we shouldn't test things like this at all through libc wrappers.

Only for 32bit or also for 64 bit? Anyway, there has always been some cases where bad addr testing was problematic (e.g. non-intel arch).

I'd skip it for both, I recall that some implementations were crashing.

Yes, there is not much we can do if it fails.

Quoted 9 lines

Kind regards,
Petr

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

Signature · 5 lines
Thank you,
Richard.

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