All of lore.kernel.org
 help / color / mirror / Atom feed
From: Laszlo Ersek <lersek@redhat.com>
To: Borislav Petkov <bp@alien8.de>
Cc: edk2-devel@lists.sourceforge.net,
	David Woodhouse <dwmw2@infradead.org>,
	linux-efi@vger.kernel.org, lkml <linux-kernel@vger.kernel.org>,
	Gleb Natapov <gleb@redhat.com>,
	Matthew Garrett <mjg59@srcf.ucam.org>
Subject: Re: [edk2] Corrupted EFI region
Date: Mon, 05 Aug 2013 23:26:46 +0200	[thread overview]
Message-ID: <52001896.1030509@redhat.com> (raw)
In-Reply-To: <20130805164731.GG31845@pd.tnic>

On 08/05/13 18:47, Borislav Petkov wrote:

> Here's the whole dmesg up until efi_enter_virtual_map. When we have entered
> efi_enter_virtual_mode, the region has changed from
> 
> [    0.000000] efi: mem11: type=4, attr=0xf, range=[0x000000007e0ad000-0x000000007e0cc000) (0MB)
> 
> to
> 
> [    0.023004] efi: mem11: type=4, attr=0xf, range=[0x000000007e0ad000-0x000000007e0ad000) (0MB)
> 
> 
> And yes, I still need to audit whether the kernel actually does that
> change. I'm still looking...

The following is a long shot, but I have no better idea for now.

Normally the following relevant sequence of calls are made to UEFI services:
(a) GetMemoryMap() --> returns memory map and map key,
(b) ExitBootServices() <-- takes map key
(c) SetVirtualAddressMap() <-- takes memory map (completed with virtual
addresses)

((a)+(b) can be repeated if (b) fails, and Linux seems to retry once.)

Now see Linux commit


<http://git.kernel.org/cgit/linux/kernel/git/torvalds/linux.git/commit/?id=916f676f>

by Matthew. If I understand correctly, it introduces the function
efi_reserve_boot_services(). Normally, immediately after a successful
(b) -- ExitBootServices() -- one should be allowed to free boot services
code and data. However (c) itself -- SetVirtualAddressMap() -- seems to
depend on boot services code and data in some firmware implementations
(probably violating the spec). Therefore this commit keeps boot services
code and data around long enough for SetVirtualAddressMap(), and
releases them after.

I *think* efi_reserve_boot_services() runs between (b) and (c), that is,
after the initial EFI memmap dump, and before efi_enter_virtual_mode()
does its thing (ie. before your debug memmap dump is executed there):

efi_main() [arch/x86/boot/compressed/eboot.c]
  exit_boot()
    --> covers (a) and (b)

start_kernel() [init/main.c]
  setup_arch() [arch/x86/kernel/setup.c]
    efi_memblock_x86_reserve_range() [arch/x86/platform/efi/efi.c]
    efi_reserve_boot_services() [arch/x86/platform/efi/efi.c]
  efi_enter_virtual_mode() [arch/x86/platform/efi/efi.c]
    --> covers (c)

That is, efi_reserve_boot_services() is called in a place where it can
potentially alter the EFI memmap between the two dumps.

(I only display efi_memblock_x86_reserve_range() in the callstack above
for completeness; I'll refer back to it lower down.)

Now look at Linux commit


<http://git.kernel.org/cgit/linux/kernel/git/torvalds/linux.git/commit/?id=7d68dc3f>

This commit changes efi_reserve_boot_services() -- it restricts the
function to reserve the boot services code & data only under some
circumstances. If those don't hold, then:

  md->num_pages = 0;

Which I think is exactly the source of the region being truncated to
zero size.

("memmap.phys_map" is set to the EFI memory map in
efi_memblock_x86_reserve_range(), see the above partial callstack, and
"memmap.map" is pointed at "memmap.phys_map" in efi_memmap_init().
efi_reserve_boot_services() iterates over "memmap.map", so we can say it
modifies the EFI memory map.)

Granted, memblock_dbg() is called too if num_pages is reset, and the
message it prints is not included in your dmesg. However I think that
could be explained by memblock_debug==0 [include/linux/memblock.h].

What happens if you pass "memblock=debug" on the kernel command line
(see early_memblock() in "mm/memblock.c")?

(I just tried it in my Fedora 19 guest, and it in fact produced the message

[    0.000000] efi: Could not reserve boot range [0x0000800000-0x0000ffffff]

)


BTW, regarding Michael's answer, I think this is just one of several
ways in which Linux manipulates the EFI memmap between (b) and (c). For
example it seems to merge ranges in the map.

Thanks,
Laszlo

WARNING: multiple messages have this Message-ID (diff)
From: Laszlo Ersek <lersek@redhat.com>
To: Borislav Petkov <bp@alien8.de>
Cc: linux-efi@vger.kernel.org, Gleb Natapov <gleb@redhat.com>,
	edk2-devel@lists.sourceforge.net,
	lkml <linux-kernel@vger.kernel.org>,
	David Woodhouse <dwmw2@infradead.org>
Subject: Re: [edk2] Corrupted EFI region
Date: Mon, 05 Aug 2013 23:26:46 +0200	[thread overview]
Message-ID: <52001896.1030509@redhat.com> (raw)
In-Reply-To: <20130805164731.GG31845@pd.tnic>

On 08/05/13 18:47, Borislav Petkov wrote:

> Here's the whole dmesg up until efi_enter_virtual_map. When we have entered
> efi_enter_virtual_mode, the region has changed from
> 
> [    0.000000] efi: mem11: type=4, attr=0xf, range=[0x000000007e0ad000-0x000000007e0cc000) (0MB)
> 
> to
> 
> [    0.023004] efi: mem11: type=4, attr=0xf, range=[0x000000007e0ad000-0x000000007e0ad000) (0MB)
> 
> 
> And yes, I still need to audit whether the kernel actually does that
> change. I'm still looking...

The following is a long shot, but I have no better idea for now.

Normally the following relevant sequence of calls are made to UEFI services:
(a) GetMemoryMap() --> returns memory map and map key,
(b) ExitBootServices() <-- takes map key
(c) SetVirtualAddressMap() <-- takes memory map (completed with virtual
addresses)

((a)+(b) can be repeated if (b) fails, and Linux seems to retry once.)

Now see Linux commit


<http://git.kernel.org/cgit/linux/kernel/git/torvalds/linux.git/commit/?id=916f676f>

by Matthew. If I understand correctly, it introduces the function
efi_reserve_boot_services(). Normally, immediately after a successful
(b) -- ExitBootServices() -- one should be allowed to free boot services
code and data. However (c) itself -- SetVirtualAddressMap() -- seems to
depend on boot services code and data in some firmware implementations
(probably violating the spec). Therefore this commit keeps boot services
code and data around long enough for SetVirtualAddressMap(), and
releases them after.

I *think* efi_reserve_boot_services() runs between (b) and (c), that is,
after the initial EFI memmap dump, and before efi_enter_virtual_mode()
does its thing (ie. before your debug memmap dump is executed there):

efi_main() [arch/x86/boot/compressed/eboot.c]
  exit_boot()
    --> covers (a) and (b)

start_kernel() [init/main.c]
  setup_arch() [arch/x86/kernel/setup.c]
    efi_memblock_x86_reserve_range() [arch/x86/platform/efi/efi.c]
    efi_reserve_boot_services() [arch/x86/platform/efi/efi.c]
  efi_enter_virtual_mode() [arch/x86/platform/efi/efi.c]
    --> covers (c)

That is, efi_reserve_boot_services() is called in a place where it can
potentially alter the EFI memmap between the two dumps.

(I only display efi_memblock_x86_reserve_range() in the callstack above
for completeness; I'll refer back to it lower down.)

Now look at Linux commit


<http://git.kernel.org/cgit/linux/kernel/git/torvalds/linux.git/commit/?id=7d68dc3f>

This commit changes efi_reserve_boot_services() -- it restricts the
function to reserve the boot services code & data only under some
circumstances. If those don't hold, then:

  md->num_pages = 0;

Which I think is exactly the source of the region being truncated to
zero size.

("memmap.phys_map" is set to the EFI memory map in
efi_memblock_x86_reserve_range(), see the above partial callstack, and
"memmap.map" is pointed at "memmap.phys_map" in efi_memmap_init().
efi_reserve_boot_services() iterates over "memmap.map", so we can say it
modifies the EFI memory map.)

Granted, memblock_dbg() is called too if num_pages is reset, and the
message it prints is not included in your dmesg. However I think that
could be explained by memblock_debug==0 [include/linux/memblock.h].

What happens if you pass "memblock=debug" on the kernel command line
(see early_memblock() in "mm/memblock.c")?

(I just tried it in my Fedora 19 guest, and it in fact produced the message

[    0.000000] efi: Could not reserve boot range [0x0000800000-0x0000ffffff]

)


BTW, regarding Michael's answer, I think this is just one of several
ways in which Linux manipulates the EFI memmap between (b) and (c). For
example it seems to merge ranges in the map.

Thanks,
Laszlo

------------------------------------------------------------------------------
Get your SQL database under version control now!
Version control is standard for application code, but databases havent 
caught up. So what steps can you take to put your SQL databases under 
version control? Why should you start doing it? Read more to find out.
http://pubads.g.doubleclick.net/gampad/clk?id=49501711&iu=/4140/ostg.clktrk

  parent reply	other threads:[~2013-08-05 21:25 UTC|newest]

Thread overview: 108+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-07-31 20:54 Corrupted EFI region Borislav Petkov
2013-07-31 20:54 ` Borislav Petkov
2013-07-31 20:58 ` Matthew Garrett
2013-07-31 20:58   ` Matthew Garrett
2013-07-31 21:51   ` Borislav Petkov
2013-07-31 21:51     ` Borislav Petkov
2013-07-31 21:54     ` Matthew Garrett
2013-07-31 21:54       ` Matthew Garrett
2013-08-01 16:51       ` Borislav Petkov
2013-08-01 16:51         ` Borislav Petkov
2013-07-31 21:55 ` David Woodhouse
2013-07-31 21:55   ` David Woodhouse
2013-08-01 16:49   ` Borislav Petkov
2013-08-01 16:49     ` Borislav Petkov
2013-08-05 11:27     ` [edk2] " Laszlo Ersek
2013-08-05 11:27       ` Laszlo Ersek
2013-08-05 13:02       ` Borislav Petkov
2013-08-05 13:02         ` Borislav Petkov
2013-08-05 13:39         ` Laszlo Ersek
2013-08-05 13:39           ` Laszlo Ersek
2013-08-05 14:03           ` Borislav Petkov
2013-08-05 14:03             ` Borislav Petkov
2013-08-05 14:27             ` Laszlo Ersek
2013-08-05 14:27               ` Laszlo Ersek
2013-08-05 14:40               ` Borislav Petkov
2013-08-05 14:40                 ` Borislav Petkov
2013-08-05 15:15                 ` Laszlo Ersek
2013-08-05 15:15                   ` Laszlo Ersek
2013-08-05 15:34                   ` James Bottomley
2013-08-05 15:34                     ` James Bottomley
2013-08-05 16:27                     ` Laszlo Ersek
2013-08-05 16:27                       ` Laszlo Ersek
2013-08-05 16:12                   ` Borislav Petkov
2013-08-05 16:12                     ` Borislav Petkov
2013-08-05 16:41                     ` Laszlo Ersek
2013-08-05 16:41                       ` Laszlo Ersek
2013-08-05 16:47                       ` Borislav Petkov
2013-08-05 16:47                         ` Borislav Petkov
2013-08-05 17:00                         ` Kinney, Michael D
2013-08-05 17:00                           ` Kinney, Michael D
2013-08-05 17:09                         ` Laszlo Ersek
2013-08-05 17:09                           ` Laszlo Ersek
2013-08-05 21:26                         ` Laszlo Ersek [this message]
2013-08-05 21:26                           ` Laszlo Ersek
2013-08-05 22:08                           ` Borislav Petkov
2013-08-05 22:08                             ` Borislav Petkov
2013-08-06 14:10                             ` Borislav Petkov
2013-08-06 14:10                               ` Borislav Petkov
2013-08-06 15:31                               ` Laszlo Ersek
2013-08-06 15:31                                 ` Laszlo Ersek
2013-08-07 15:19                                 ` Borislav Petkov
2013-08-07 17:23                                   ` Andrew Fish
2013-08-07 17:23                                     ` Andrew Fish
2013-08-07 20:19                                     ` Matt Fleming
2013-08-07 20:19                                       ` Matt Fleming
2013-08-07 20:24                                       ` Matt Fleming
2013-08-07 20:24                                         ` Matt Fleming
2013-08-07 21:10                                       ` Andrew Fish
2013-08-07 21:10                                         ` Andrew Fish
2013-08-07 21:23                                         ` Matthew Garrett
2013-08-08 10:17                                         ` Matt Fleming
2013-08-08 10:17                                           ` Matt Fleming
2013-08-08 13:46                                           ` Andrew Fish
2013-08-08 13:46                                             ` Andrew Fish
2013-09-02  8:19                                             ` Matt Fleming
2013-09-02  8:19                                               ` Matt Fleming
2013-09-13 20:38                                           ` jerry.hoemann
2013-09-13 20:38                                             ` jerry.hoemann-VXdhtT5mjnY
2013-09-16 10:59                                             ` Matt Fleming
2013-09-16 10:59                                               ` Matt Fleming
2013-09-16 11:50                                               ` Laszlo Ersek
2013-09-16 11:50                                                 ` Laszlo Ersek
2013-09-16 15:57                                                 ` Josh Triplett
2013-09-16 15:57                                                   ` Josh Triplett
2013-09-16 16:25                                                   ` Laszlo Ersek
2013-09-16 16:25                                                     ` Laszlo Ersek
2013-09-16 16:27                                                     ` Matthew Garrett
2013-09-16 16:27                                                       ` Matthew Garrett
2013-09-16 16:29                                                     ` Josh Triplett
2013-09-16 16:29                                                       ` Josh Triplett
2013-09-18 19:24                                               ` jerry.hoemann
2013-09-18 19:24                                                 ` jerry.hoemann-VXdhtT5mjnY
2013-09-20  9:06                                                 ` Matt Fleming
2013-09-20  9:06                                                   ` Matt Fleming
2013-08-07 17:49                                   ` Laszlo Ersek
2013-08-07 17:49                                     ` Laszlo Ersek
2013-08-08 15:02                                     ` Borislav Petkov
2013-08-08 15:02                                       ` Borislav Petkov
2013-08-08 21:45                                       ` Brian J. Johnson
2013-08-08 21:45                                         ` Brian J. Johnson
2013-08-18  7:33                                     ` Jordan Justen
2013-08-18  7:33                                       ` Jordan Justen
2013-08-05 15:50                 ` Andrew Fish
2013-08-05 15:50                   ` Andrew Fish
2013-08-05 18:12                   ` Borislav Petkov
2013-08-05 18:12                     ` Borislav Petkov
2013-08-05 21:37                     ` H. Peter Anvin
2013-08-05 21:37                       ` H. Peter Anvin
2013-08-05 21:41                       ` Borislav Petkov
2013-08-05 21:41                         ` Borislav Petkov
2013-08-05 21:49                         ` H. Peter Anvin
2013-08-05 21:49                           ` H. Peter Anvin
2013-08-05 21:55                         ` Laszlo Ersek
2013-08-05 21:55                           ` Laszlo Ersek
2013-08-05 22:52                           ` James Bottomley
2013-08-05 22:52                             ` James Bottomley
2013-08-06  7:26                             ` Laszlo Ersek
2013-08-06  7:26                               ` Laszlo Ersek

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=52001896.1030509@redhat.com \
    --to=lersek@redhat.com \
    --cc=bp@alien8.de \
    --cc=dwmw2@infradead.org \
    --cc=edk2-devel@lists.sourceforge.net \
    --cc=gleb@redhat.com \
    --cc=linux-efi@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mjg59@srcf.ucam.org \
    /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.