From 0bc23a938bcfe3ab4a3236a660934953f3ab3cef Mon Sep 17 00:00:00 2001 From: srs5694 Date: Sun, 7 Jul 2013 21:16:39 -0400 Subject: [PATCH] Fixed bug that caused Btrfs driver to hang sometimes. --- docs/refind/secureboot.html | 40 +++++++++++++++++++++++-------------- filesystems/fsw_efi.c | 40 ++++++++++++++++++++++--------------- refind/main.c | 4 ++-- 3 files changed, 51 insertions(+), 33 deletions(-) diff --git a/docs/refind/secureboot.html b/docs/refind/secureboot.html index aeb2a8d..2de8fa3 100644 --- a/docs/refind/secureboot.html +++ b/docs/refind/secureboot.html @@ -144,7 +144,7 @@ href="mailto:rodsmith@rodsbooks.com">rodsmith@rodsbooks.com

  • Managing Your MOKs
  • -
  • Using rEFInd with PreBootloader
  • +
  • Using rEFInd with PreLoader
  • Secure Boot Caveats
  • @@ -152,7 +152,7 @@ href="mailto:rodsmith@rodsbooks.com">rodsmith@rodsbooks.com

    -

    If you're using a computer that supports Secure Boot, you may run into extra complications. This feature is intended to make it difficult for malware to insert itself early into the computer's boot process. Unfortunately, it also complicates multi-boot configurations such as those that rEFInd is intended to manage. This page describes some secure boot basics and two specific ways of using rEFInd with Secure Boot: Using the shim program and using the PreBootloader program. It concludes with a look at known bugs and limitations in rEFInd's Secure Boot features.

    +

    If you're using a computer that supports Secure Boot, you may run into extra complications. This feature is intended to make it difficult for malware to insert itself early into the computer's boot process. Unfortunately, it also complicates multi-boot configurations such as those that rEFInd is intended to manage. This page describes some secure boot basics and two specific ways of using rEFInd with Secure Boot: Using the shim program and using the PreLoader program. It concludes with a look at known bugs and limitations in rEFInd's Secure Boot features.

    Basic Issues

    @@ -162,11 +162,11 @@ href="mailto:rodsmith@rodsbooks.com">rodsmith@rodsbooks.com

    If you don't want it, you can disable it, at least on x86-64 PCs. If an ARM-based computer ships with -Windows 8, this isn't an option for it. Unfortunately, the shim and PreBootloader programs described on this page currently support only x86-64, not x86 or ARM.

    +Windows 8, this isn't an option for it. Unfortunately, the shim and PreLoader programs described on this page currently support only x86-64, not x86 or ARM.

    Through 2012, it became obvious that Secure Boot would be a feature that was controlled, to a large extent, by Microsoft. This is because Microsoft requires that non-server computers that display Windows 8 logos ship with Secure Boot enabled. As a practical matter, this also means that such computers ship with Microsoft's keys in their firmware. In the absence of an industry-standard body to manage the signing of Secure Boot keys, this means that Microsoft's key is the only one that's more-or-less guaranteed to be installed on the computer, thus blocking the ability to boot any OS that lacks a boot path through Microsoft's signing key.

    -

    Fortunately, Microsoft will sign third-party binaries with their key—or more precisely, with a key that Microsoft uses to sign third-party binaries. (Microsoft uses another key to sign its own binaries, and some devices, such as the Microsoft Surface tablet, lack the third-party Microsoft key.) A payment of $99 to Verisign enables a software distributor to sign as many binaries as desired. Red Hat (Fedora), Novell (SUSE), and Canonical (Ubuntu) are all using this system to enable their boot loaders to run. Unfortunately, using a third-party signing service is an awkward solution for open source software. In fact, for this very reason two separate programs exist that shift the Secure Boot "train" from Microsoft's proprietary "track" to one that's more friendly to open source authors. Both of these programs (shim and PreBootloader) are available in binary form signed by Microsoft's key. Shim enables the computer to launch binaries that are signed by a key that's built into it or that the user adds to a list known as the Machine Owner Key (MOK) list. PreBootloader enables the computer to launch binaries that the user has explicitly identified as being OK. Distributions beginning with Ubuntu 12.10 (and 12.04.2), Fedora 18, and OpenSUSE 12.3 use shim, although Ubuntu ships with an early version of shim that's useless for launching rEFInd, at least through Ubuntu 13.04. To the best of my knowledge, no major distribution uses PreBootloader, but it may see use on live CDs and is easier to set up if you're not using a distribution that supports shim.

    +

    Fortunately, Microsoft will sign third-party binaries with their key—or more precisely, with a key that Microsoft uses to sign third-party binaries. (Microsoft uses another key to sign its own binaries, and some devices, such as the Microsoft Surface tablet, lack the third-party Microsoft key.) A payment of $99 to Verisign enables a software distributor to sign as many binaries as desired. Red Hat (Fedora), Novell (SUSE), and Canonical (Ubuntu) are all using this system to enable their boot loaders to run. Unfortunately, using a third-party signing service is an awkward solution for open source software. In fact, for this very reason two separate programs exist that shift the Secure Boot "train" from Microsoft's proprietary "track" to one that's more friendly to open source authors. Both of these programs (shim and PreLoader) are available in binary form signed by Microsoft's key. Shim enables the computer to launch binaries that are signed by a key that's built into it or that the user adds to a list known as the Machine Owner Key (MOK) list. PreLoader enables the computer to launch binaries that the user has explicitly identified as being OK. Distributions beginning with Ubuntu 12.10 (and 12.04.2), Fedora 18, and OpenSUSE 12.3 use shim, although Ubuntu ships with an early version of shim that's useless for launching rEFInd, at least through Ubuntu 13.04. To the best of my knowledge, no major distribution uses PreLoader, but it may see use on live CDs and is easier to set up if you're not using a distribution that supports shim.

    There are three ways to sign a binary that will get it launched on a computer that uses shim:

    @@ -203,11 +203,11 @@ Windows 8, this isn't an option for it. Unfortunately, the shim and PreBootloade

    All three key types are the same in form—shim's built-in keys and MOKs are both generated using the same tools used to generate Secure Boot keys. The keys can be generated with the common openssl program, but signing EFI binaries requires a rarer program called sbsign or pesign. If you use shim with a distribution that doesn't support this tool, you'll need to either sign the kernels yourself, which can be a hassle, or launch the kernels by way of a boot loader that doesn't check for signatures, such as ELILO.

    - + -

    PreBootloader is easier to set up on a distribution that doesn't support shim because PreBootloader doesn't rely on keys; instead, you tell it which binaries you trust and it will let you launch them. This works well on a system with boot managers, boot loaders, and kernels that seldom change. It's not a good solution for distribution maintainers, though, because it requires that users manually add binaries to PreBootloader's list of approved binaries when the OS is installed and every time those binaries change. Also, PreBootloader relies on a helper program, HashTool, to enroll hashes. (This is Geek for "tell the computer that a binary is OK.") Unfortunately, HashTool can enroll hashes only from the partition from which it was launched, so if you want to use rEFInd to launch Linux kernels directly, it's easiest if you mount your ESP at /boot in Linux or copy your kernels to the ESP. Another approach is to copy HashTool.efi to the partition that holds your kernel and rename it to almost anything else. rEFInd will then treat it like an OS boot loader and create a menu entry for it, enabling you to launch it as needed.

    +

    PreLoader is easier to set up on a distribution that doesn't support shim because PreLoader doesn't rely on keys; instead, you tell it which binaries you trust and it will let you launch them. This works well on a system with boot managers, boot loaders, and kernels that seldom change. It's not a good solution for distribution maintainers, though, because it requires that users manually add binaries to PreLoader's list of approved binaries when the OS is installed and every time those binaries change. Also, PreLoader relies on a helper program, HashTool, to enroll hashes. (This is Geek for "tell the computer that a binary is OK.") Unfortunately, HashTool can enroll hashes only from the partition from which it was launched, so if you want to use rEFInd to launch Linux kernels directly, it's easiest if you mount your ESP at /boot in Linux or copy your kernels to the ESP. Another approach is to copy HashTool.efi to the partition that holds your kernel and rename it to almost anything else. rEFInd will then treat it like an OS boot loader and create a menu entry for it, enabling you to launch it as needed.

    -

    Beginning with version 0.5.0, rEFInd can communicate with the shim system to authenticate boot loaders. If a boot loader has been signed by a valid UEFI Secure Boot key, a valid shim key, or a valid MOK key, rEFInd will launch it. rEFInd will also launch unsigned boot loaders or those with invalid signatures if Secure Boot is disabled in or unsupported by the firmware. (If that's your situation, you needn't bother reading this page.) PreBootloader is designed in such a way that it requires no explicit support in rEFInd to work.

    +

    Beginning with version 0.5.0, rEFInd can communicate with the shim system to authenticate boot loaders. If a boot loader has been signed by a valid UEFI Secure Boot key, a valid shim key, or a valid MOK key, rEFInd will launch it. rEFInd will also launch unsigned boot loaders or those with invalid signatures if Secure Boot is disabled in or unsupported by the firmware. (If that's your situation, you needn't bother reading this page.) PreLoader is designed in such a way that it requires no explicit support in rEFInd to work.

    Version 0.5.0 ships signed with my own keys, and I provide the public version of this key with the rEFInd package. This can help simplify setup, since you needn't generate your own keys to get rEFInd working; however, if you lack public keys for the boot loaders that rEFInd launches, you'll need to generate your own keys and sign your boot loaders, as described in the Managing Your MOKs section.

    @@ -215,7 +215,7 @@ Windows 8, this isn't an option for it. Unfortunately, the shim and PreBootloade

    Using rEFInd with Shim

    -

    Because several major distributions support shim, I describe it first. You may need to adjust the rEFInd installation process to get it working with shim, especially if you're not using a distribution that uses this software. In addition to installing shim, you should know how to manage your MOKs, so I describe this topic, too. If you don't want to use shim, you can skip ahead to the section on PreBootloader.

    +

    Because several major distributions support shim, I describe it first. You may need to adjust the rEFInd installation process to get it working with shim, especially if you're not using a distribution that uses this software. In addition to installing shim, you should know how to manage your MOKs, so I describe this topic, too. If you don't want to use shim, you can skip ahead to the section on PreLoader.

    Installing Shim and rEFInd

    @@ -381,11 +381,11 @@ $ openssl x509 -in refind_local.crt -out refind_local.cer

    At this point you should be able to launch the binaries you've signed. Unfortunately, there can still be problems at this point....

    -
    -

    Using rEFInd with PreBootloader

    +
    +

    Using rEFInd with PreLoader

    -

    If you want to use Secure Boot with a distribution that doesn't come with shim but the preceding description exhausts you, take heart: PreBootloader is easier to set up and use for your situation! Unfortunately, it's still not as easy to use as not using Secure Boot at all, and it's got some drawbacks, but it may represent an acceptable middle ground. To get started, proceed as follows:

    +

    If you want to use Secure Boot with a distribution that doesn't come with shim but the preceding description exhausts you, take heart: PreLoader is easier to set up and use for your situation! Unfortunately, it's still not as easy to use as not using Secure Boot at all, and it's got some drawbacks, but it may represent an acceptable middle ground. To get started, proceed as follows:

      @@ -397,7 +397,7 @@ $ openssl x509 -in refind_local.crt -out refind_local.cer zip or CD-R image file). If you download the binary zip file, unzip it; if you get the CD-R image file, burn it to a CD-R and mount it. -
    1. Download PreBootloader from Download PreLoader from its release page or by clicking the following links. Be sure to get both the openssl x509 -in refind_local.crt -out refind_local.cer alt="Be sure to select the right binary when you enroll its hash." border=2>
      - +
    2. Repeat the preceding two steps for any additional binaries you might want to enroll. These include any EFI filesystem drivers you're using, @@ -462,6 +462,16 @@ $ openssl x509 -in refind_local.crt -out refind_local.cer
        +
      • rEFInd uses the same EFI "hooks" as PreLoader. This method, however, is + part of an optional EFI subsystem, so in theory some EFIs might not + support it. For months, I knew of no such implementation, but this + SuperUser question indicates that at least one such implementation + exists. The board in question is old enough that it might not support + Secure Boot at all, though, so I'm not sure if this report indicates a + serious limitation or just a stray error message on a computer that + doesn't need this feature at all.
      • +
      • Under certain circumstances, the time required to launch a boot loader can increase. This is unlikely to be noticeable for the average small boot loader, but could be significant for larger boot loaders on slow @@ -470,7 +480,7 @@ $ openssl x509 -in refind_local.crt -out refind_local.cer
      • As of version 0.6.2, rEFInd's own Secure Boot support is theoretically able to work on non-x86-64 platforms; however, shim 0.2 and - PreBootloader both work only on x86-64, and rEFInd is dependent + PreLoader both work only on x86-64, and rEFInd is dependent upon these tools. Thus, you'll have to wait for future developments if you want to use Secure Boot on x86 or ARM computers.
      • @@ -481,7 +491,7 @@ $ openssl x509 -in refind_local.crt -out refind_local.cer
      -

      If you launch a boot loader or other program from rEFInd that relies on the EFI's standard program-launching code, that program should take advantage of shim and its MOKs. For instance, if you launch gummiboot from rEFInd (and rEFInd from shim), gummiboot should be able to launch shim/MOK-signed Linux kernels. This is not currently true if you launch gummiboot directly from shim. (You can launch gummiboot from PreBootloader and it should work, though, because of technical differences between how shim and PreBootloader work.)

      +

      If you launch a boot loader or other program from rEFInd that relies on the EFI's standard program-launching code, that program should take advantage of shim and its MOKs. For instance, if you launch gummiboot from rEFInd (and rEFInd from shim), gummiboot should be able to launch shim/MOK-signed Linux kernels. This is not currently true if you launch gummiboot directly from shim. (You can launch gummiboot from PreLoader and it should work, though, because of technical differences between how shim and PreLoader work.)

      My focus in testing rEFInd's Secure Boot capabilities has been on getting Linux kernels with EFI stub loaders to launch correctly. I've done some minimal testing with GRUB 2, though. I've also tested some self-signed binaries, such as an EFI shell and MokManager. (The EFI shell launches, but will not itself launch anything that's not been signed with a UEFI Secure Boot key, even with rEFInd 0.6.2. This of course limits its utility.)

      diff --git a/filesystems/fsw_efi.c b/filesystems/fsw_efi.c index bed6a80..636f8e0 100644 --- a/filesystems/fsw_efi.c +++ b/filesystems/fsw_efi.c @@ -98,7 +98,7 @@ EFI_GUID gEfiFileSystemVolumeLabelInfoIdGuid = EFI_FILE_SYSTEM_VOLUME_LABEL_INFO /** Helper macro for stringification. */ #define FSW_EFI_STRINGIFY(x) #x /** Expands to the EFI driver name given the file system type name. */ -#define FSW_EFI_DRIVER_NAME(t) L"rEFInd 0.7.0 " FSW_EFI_STRINGIFY(t) L" File System Driver" +#define FSW_EFI_DRIVER_NAME(t) L"rEFInd 0.7.0.3 " FSW_EFI_STRINGIFY(t) L" File System Driver" // function prototypes @@ -176,6 +176,7 @@ struct cache_data { #define NUM_CACHES 2 /* Don't increase without modifying fsw_efi_read_block() */ static struct cache_data Caches[NUM_CACHES]; +static int LastRead = -1; /** * Interface structure for the EFI Driver Binding protocol. @@ -215,6 +216,22 @@ extern struct fsw_fstype_table FSW_FSTYPE_TABLE_NAME(FSTYPE); //#include "OverrideFunctions-kabyl.edk2.c.include" +static VOID EFIAPI fsw_efi_clear_cache(VOID) { + int i; + + // clear the cache + for (i = 0; i < NUM_CACHES; i++) { + if (Caches[i].Cache != NULL) { + FreePool(Caches[i].Cache); + Caches[i].Cache = NULL; + } // if + Caches[i].CacheStart = 0; + Caches[i].CacheValid = FALSE; + Caches[i].Volume = NULL; + } + LastRead = -1; +} // VOID EFIAPI fsw_efi_clear_cache(); + /** * Image entry point. Installs the Driver Binding and Component Name protocols * on the image's handle. Actually mounting a file system is initiated through @@ -354,8 +371,7 @@ EFI_STATUS EFIAPI fsw_efi_DriverBinding_Start(IN EFI_DRIVER_BINDING_PROTOCOL *T ControllerHandle, EFI_OPEN_PROTOCOL_BY_DRIVER); if (EFI_ERROR(Status)) { - Print(L"Fsw ERROR: OpenProtocol(DiskIo) returned %r\n", Status); - return Status; + return Status; } // allocate volume structure @@ -460,12 +476,8 @@ EFI_STATUS EFIAPI fsw_efi_DriverBinding_Stop(IN EFI_DRIVER_BINDING_PROTOCOL *T ControllerHandle); // clear the cache - for (i = 0; i < NUM_CACHES; i++) { - if (Caches[i].Cache != NULL) { - FreePool(Caches[i].Cache); - Caches[i].Cache = NULL; - } // if - } + fsw_efi_clear_cache(); + return Status; } @@ -527,7 +539,6 @@ void fsw_efi_change_blocksize(struct fsw_volume *vol, */ fsw_status_t fsw_efi_read_block(struct fsw_volume *vol, fsw_u64 phys_bno, void *buffer) { - static int LastRead = -1; int i, ReadCache = -1; FSW_VOLUME_DATA *Volume = (FSW_VOLUME_DATA *)vol->host_data; EFI_STATUS Status = EFI_SUCCESS; @@ -539,12 +550,7 @@ fsw_status_t fsw_efi_read_block(struct fsw_volume *vol, fsw_u64 phys_bno, void * // Initialize static data structures, if necessary.... if (LastRead < 0) { - for (i = 0; i < NUM_CACHES; i++) { - Caches[i].Cache = NULL; - Caches[i].CacheStart = 0; - Caches[i].CacheValid = FALSE; - Caches[i].Volume = NULL; - } // for + fsw_efi_clear_cache(); } // if // Look for a cache hit on the current query.... @@ -593,6 +599,7 @@ fsw_status_t fsw_efi_read_block(struct fsw_volume *vol, fsw_u64 phys_bno, void * vol->phys_blocksize, buffer); } + Volume->LastIOStatus = Status; return Status; } // fsw_status_t *fsw_efi_read_block() @@ -640,6 +647,7 @@ EFI_STATUS EFIAPI fsw_efi_FileSystem_OpenVolume(IN EFI_FILE_IO_INTERFACE *This, Print(L"fsw_efi_FileSystem_OpenVolume\n"); #endif + fsw_efi_clear_cache(); Status = fsw_efi_dnode_to_FileHandle(Volume->vol->root, Root); return Status; diff --git a/refind/main.c b/refind/main.c index af51b59..54fc858 100644 --- a/refind/main.c +++ b/refind/main.c @@ -147,7 +147,7 @@ static VOID AboutrEFInd(VOID) if (AboutMenu.EntryCount == 0) { AboutMenu.TitleImage = BuiltinIcon(BUILTIN_ICON_FUNC_ABOUT); - AddMenuInfoLine(&AboutMenu, L"rEFInd Version 0.7.0.2"); + AddMenuInfoLine(&AboutMenu, L"rEFInd Version 0.7.0.3"); AddMenuInfoLine(&AboutMenu, L""); AddMenuInfoLine(&AboutMenu, L"Copyright (c) 2006-2010 Christoph Pfisterer"); AddMenuInfoLine(&AboutMenu, L"Copyright (c) 2012-2013 Roderick W. Smith"); @@ -2135,7 +2135,7 @@ static VOID WarnIfLegacyProblems() { // Locates boot loaders. NOTE: This assumes that GlobalConfig.LegacyType is set correctly. static VOID ScanForBootloaders(VOID) { - UINTN i; + UINTN i; ScanVolumes(); -- 2.39.2