mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH -tip] kmemcheck: fix crash in PnP BIOS calls
@ 2008-10-05 17:25 Vegard Nossum
  2008-10-05 18:15 ` Alan Cox
  0 siblings, 1 reply; 4+ messages in thread
From: Vegard Nossum @ 2008-10-05 17:25 UTC (permalink / raw)
  To: Ingo Molnar; +Cc: Pekka Enberg, linux-kernel

>From 251667c0cd0bc9d3d3e32ae137f49e51c0bb062c Mon Sep 17 00:00:00 2001
From: Vegard Nossum <vegard.nossum@gmail.com>
Date: Sun, 5 Oct 2008 15:46:30 +0200
Subject: [PATCH] kmemcheck: fix crash in PnP BIOS calls

Ingo Molnar reported this crash:
> PnPBIOS: Scanning system for PnP BIOS support...
> PnPBIOS: Found PnP BIOS installation structure at 0xc00fc550
> PnPBIOS: PnP BIOS version 1.0, entry 0xf0000:0xc580, dseg 0xf0000
> BUG: unable to handle kernel paging request at 0000c6ef

It turns out that BIOS calls are made with a different code segment. So
when kmemcheck tries to dereference the EIP/RIP (using the kernel data
segment register), we get the unhandled page fault.

I think we can solve this by verifying (in the page fault handler) that
the faulting code is using the kernel CS.

Signed-off-by: Vegard Nossum <vegard.nossum@gmail.com>
---
 arch/x86/mm/kmemcheck/kmemcheck.c |   11 +++++++++++
 1 files changed, 11 insertions(+), 0 deletions(-)

diff --git a/arch/x86/mm/kmemcheck/kmemcheck.c b/arch/x86/mm/kmemcheck/kmemcheck.c
index d649aa7..bd739a4 100644
--- a/arch/x86/mm/kmemcheck/kmemcheck.c
+++ b/arch/x86/mm/kmemcheck/kmemcheck.c
@@ -666,6 +666,17 @@ bool kmemcheck_fault(struct pt_regs *regs, unsigned long address,
 	pte_t *pte;
 	unsigned int level;
 
+	/*
+	 * XXX: Is it safe to assume that memory accesses from virtual 86
+	 * mode or non-kernel code segments will _never_ access kernel
+	 * memory (e.g. tracked pages)? For now, we need this to avoid
+	 * invoking kmemcheck for PnP BIOS calls.
+	 */
+	if (regs->flags & X86_VM_MASK)
+		return false;
+	if (regs->cs != __KERNEL_CS)
+		return false;
+
 	pte = lookup_address(address, &level);
 	if (!pte)
 		return false;
-- 
1.5.5.1


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

* Re: [PATCH -tip] kmemcheck: fix crash in PnP BIOS calls
  2008-10-05 17:25 [PATCH -tip] kmemcheck: fix crash in PnP BIOS calls Vegard Nossum
@ 2008-10-05 18:15 ` Alan Cox
  2008-10-05 19:04   ` Vegard Nossum
  0 siblings, 1 reply; 4+ messages in thread
From: Alan Cox @ 2008-10-05 18:15 UTC (permalink / raw)
  To: Vegard Nossum; +Cc: Ingo Molnar, Pekka Enberg, linux-kernel

> It turns out that BIOS calls are made with a different code segment. So
> when kmemcheck tries to dereference the EIP/RIP (using the kernel data
> segment register), we get the unhandled page fault.
> 
> I think we can solve this by verifying (in the page fault handler) that
> the faulting code is using the kernel CS.

That isn't an entirely safe assumption and some services such as BIOS32
are 32bit. Would it be better wrap BIOS calls with a kmemcheck wrapper
which changes the way kmemcheck works/dumps stuff and also gives you a
hook after BIOS calls to do stuff like corruption scans or change
detection on kernel pages ?

Alan

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

* Re: [PATCH -tip] kmemcheck: fix crash in PnP BIOS calls
  2008-10-05 18:15 ` Alan Cox
@ 2008-10-05 19:04   ` Vegard Nossum
  2008-10-05 19:10     ` Alan Cox
  0 siblings, 1 reply; 4+ messages in thread
From: Vegard Nossum @ 2008-10-05 19:04 UTC (permalink / raw)
  To: Alan Cox; +Cc: Ingo Molnar, Pekka Enberg, linux-kernel

Hi,

On Sun, Oct 5, 2008 at 8:15 PM, Alan Cox <alan@lxorguk.ukuu.org.uk> wrote:
>> It turns out that BIOS calls are made with a different code segment. So
>> when kmemcheck tries to dereference the EIP/RIP (using the kernel data
>> segment register), we get the unhandled page fault.
>>
>> I think we can solve this by verifying (in the page fault handler) that
>> the faulting code is using the kernel CS.
>
> That isn't an entirely safe assumption and some services such as BIOS32
> are 32bit. Would it be better wrap BIOS calls with a kmemcheck wrapper
> which changes the way kmemcheck works/dumps stuff and also gives you a
> hook after BIOS calls to do stuff like corruption scans or change
> detection on kernel pages ?

Thanks for the heads-up.

I think BIOS32 in particular is safe. I was looking at for instance
check_pcibios() in arch/x86/pci/pcbios.c. It doesn't change any
segment registers, so it means that whatever the actual code address
is, we should be able to dereference it (i.e. inspect the BIOS code)
without taking additional page faults.

And this is really all we need; BIOS code running with the kernel
segments can even access kernel memory and kmemcheck will be able to
catch those reads/writes just fine. I'm not sure if the BIOS code can
change segments (it would need to switch LDT/GDTs, I think), and even
if it did, I find it unlikely that it would directly access kernel
memory using these new segment registers. Do you happen to know of any
such cases?


Vegard

-- 
"The animistic metaphor of the bug that maliciously sneaked in while
the programmer was not looking is intellectually dishonest as it
disguises that the error is the programmer's own creation."
	-- E. W. Dijkstra, EWD1036

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

* Re: [PATCH -tip] kmemcheck: fix crash in PnP BIOS calls
  2008-10-05 19:04   ` Vegard Nossum
@ 2008-10-05 19:10     ` Alan Cox
  0 siblings, 0 replies; 4+ messages in thread
From: Alan Cox @ 2008-10-05 19:10 UTC (permalink / raw)
  To: Vegard Nossum; +Cc: Ingo Molnar, Pekka Enberg, linux-kernel

> catch those reads/writes just fine. I'm not sure if the BIOS code can
> change segments (it would need to switch LDT/GDTs, I think), and even

It can do so if it wants as far as I can tell

> if it did, I find it unlikely that it would directly access kernel
> memory using these new segment registers. Do you happen to know of any
> such cases?

No. The code either seems to be very simple or just be an SMM trap in the
BIOSes I've seen

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

end of thread, other threads:[~2008-10-05 19:12 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2008-10-05 17:25 [PATCH -tip] kmemcheck: fix crash in PnP BIOS calls Vegard Nossum
2008-10-05 18:15 ` Alan Cox
2008-10-05 19:04   ` Vegard Nossum
2008-10-05 19:10     ` Alan Cox

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

all inboxes | Powered by JetHome®