Gentoo Websites Logo
Go to: Gentoo Home Documentation Forums Lists Bugs Planet Store Wiki Get Gentoo!
Bug 942201 - sys-boot/grub: cannot boot UKIs with secureboot enabled (via shim) and missing support for multi-profile UKIs
Summary: sys-boot/grub: cannot boot UKIs with secureboot enabled (via shim) and missin...
Status: CONFIRMED
Alias: None
Product: Gentoo Linux
Classification: Unclassified
Component: Current packages (show other bugs)
Hardware: All Linux
: Normal enhancement
Assignee: Mike Gilbert
URL:
Whiteboard:
Keywords: PullRequest
Depends on:
Blocks:
 
Reported: 2024-10-26 06:16 UTC by Peter Levine
Modified: 2025-02-21 18:50 UTC (History)
3 users (show)

See Also:
Package list:
Runtime testing required: ---


Attachments
/etc/grub.d/05_uki (05_uki,9.06 KB, text/plain)
2024-10-26 06:16 UTC, Peter Levine
Details

Note You need to log in before you can comment on or make changes to this bug.
Description Peter Levine 2024-10-26 06:16:24 UTC
Created attachment 906753 [details]
/etc/grub.d/05_uki

I'm not sure to what degree you're comfortable maintaining an extra grub.d script but I managed to create a script largely based on, and comprising the bulk of /etc/grub.d/10_linux that uses glob patterns to find UKIs, specifically "/boot/*EFI/Linux/*-*.efi /efi/EFI/Linux/*-*.efi", and adds appropriate entries based on the GRUB chainloader command instead of the GRUB linux command.


> menuentry 'Gentoo GNU/Linux' --class gentoo --class gnu-linux --class gnu --class os
> $menuentry_id_option 'gnulinux-simple-cc267496-0418-468c-934c-b4dd04dab0d8' {
>         load_video
>         insmod chain
>         insmod part_gpt
>         insmod fat
>         search --no-floppy --fs-uuid --set=root D313-9444
>         echo    'Loading Linux 6.11.5-gentoo ...'
>         chainloader     /EFI/Linux/8e6dfbd2da15a3abb3e1a5a862dd78f4-6.11.5-gentoo.efi
> root=UUID=cc267496-0418-468c-934c-b4dd04dab0e9 ro rootflags=subvol=@gentoo
> root=LABEL=linux rootfstype=btrfs mitigations=auto apparmor=1 security=apparmor
> systemd.unified_cgroup_hierarchy=1 fsck.repair=yes quiet splash
Comment 1 Mike Gilbert gentoo-dev 2024-10-26 14:41:14 UTC
I am not interesting in maintaining this. I would suggest submitting it upstream.
Comment 2 Mike Gilbert gentoo-dev 2024-10-26 17:05:49 UTC
Pardon the knee-jerk reaction. So long as somebody keeps it working, I guess it would be fine to add this.
Comment 3 Peter Levine 2024-10-28 03:23:42 UTC
(In reply to Mike Gilbert from comment #2)
> Pardon the knee-jerk reaction. So long as somebody keeps it working, I guess
> it would be fine to add this.

I don't mind helping to maintain it if CCed with relevant bugs.  I'd just want to adjust for aesthetics before submitting upstream.
Comment 4 Nowa Ammerlaan gentoo-dev 2024-10-29 13:17:08 UTC
(In reply to Peter Levine from comment #3)
> (In reply to Mike Gilbert from comment #2)
> > Pardon the knee-jerk reaction. So long as somebody keeps it working, I guess
> > it would be fine to add this.
> 
> I don't mind helping to maintain it if CCed with relevant bugs.  I'd just
> want to adjust for aesthetics before submitting upstream.

I think it would be a good idea to also work on the multi-profile UKI support before submitting it upstream. In essence what would need to be done is:
1) List the number of .profile sections in the found UKI: "objdump -h ${uki} | grep -o .profile | wc -l"
2) If zero found, no change in behaviour, it is not a multi-profile UKI.
3) If one or more are found, loop over the number of found .profile's and:
4) Extract the "TITLE=" of the ${i}'th .profile section (with e.g. objcopy) and use this title to make a chainload entry that loads "${uki} @${i} ${usual_cmdline}".

See also here: https://github.com/uapi-group/specifications/pull/108/files
Comment 5 Nowa Ammerlaan gentoo-dev 2024-11-05 15:05:56 UTC
There seems to be something additional required to support UKIs in GRUB with secureboot enabled: https://forums.gentoo.org/viewtopic.php?p=8845252

I don't understand why but it seems the chainload command works differently then a direct load by UEFI, and somehow works differently when secureboot is enabled.
Comment 6 Nowa Ammerlaan gentoo-dev 2024-11-05 16:02:36 UTC
(In reply to Nowa Ammerlaan from comment #5)
> I don't understand why but it seems the chainload command works differently
> then a direct load by UEFI, and somehow works differently when secureboot is
> enabled.

Now I understand why, upstream grub's chainload command requests the firmware directly to load the EFI file and does not use the shim protocol when it is booted via shim. Fedora/Ubuntu/SUSE carry (different) patches for this: [1]

[1] https://src.fedoraproject.org/rpms/grub2/blob/rawhide/f/0215-Add-secureboot-support-on-efi-chainloader.patch

By the way, there is also this Fedora patch [2] for BLS type 1 support, and this pending patch [2] that extends that support to BLS type 2 (i.e. UKI support). In principle this could be re-used instead of starting from scratch.

[2] https://src.fedoraproject.org/rpms/grub2/blob/rawhide/f/0053-Add-BLS-support-to-grub-mkconfig.patch
[3] https://github.com/rhboot/grub2/pull/119

It's a bit frustrating that none of this has made it upstream.
Comment 7 Peter Levine 2024-11-12 11:25:39 UTC
(In reply to Nowa Ammerlaan from comment #4)
> I think it would be a good idea to also work on the multi-profile UKI
> support before submitting it upstream. In essence what would need to be done
> is:
> 1) List the number of .profile sections in the found UKI: "objdump -h ${uki}
> | grep -o .profile | wc -l"
> 2) If zero found, no change in behaviour, it is not a multi-profile UKI.
> 3) If one or more are found, loop over the number of found .profile's and:
> 4) Extract the "TITLE=" of the ${i}'th .profile section (with e.g. objcopy)
> and use this title to make a chainload entry that loads "${uki} @${i}
> ${usual_cmdline}".
> 
> See also here: https://github.com/uapi-group/specifications/pull/108/files

I'm fine with working on this but I doubt objdump/objcopy would be adequate.  There's a lot about the systemd's multi-profile UKI implementation that poses some problems.  For instance, to avoid creating temp files by outputting a section to stdout, one can do something like `objcopy -O binary -j .profile /boot/vmlinuz.efi >(cat)`.  But because there are multiple .profile sections, I get "ID=mainobjcopy: /dev/fd/63[.profile]: Illegal seek".  I'd have to parse the output of `objdump -h vmlinuz.efi` for .profile section offset addresses and output one or more temp files just to examine them.  And if the UKI was created using ukify's "--join-profile" parameter without an initial .profile in the main file, it will insert an initial .profile section with "ID=main" and no "Title", implicitly inheriting the preceding sections.  For that corner-case, I'd need to look for ID=main and, if found, setup the title using the filename instead.

Ultimately, it would be much easier and less prone to error with the aid of `ukify inspect`.  There doesn't have to be an explicit dependency on ukify.  You could just add:

> pkg_postinst() {
>     optfeature "multi-profile UKI support" sys-apps/systemd[ukify] sys-apps/systemd-utils[ukify]
> }

And AFAICT, ukify is the only tool as of now that is designed to build and work with multi-profile UKIs.
Comment 8 Nowa Ammerlaan gentoo-dev 2024-11-12 11:37:31 UTC
(In reply to Peter Levine from comment #7)
> Ultimately, it would be much easier and less prone to error with the aid of
> `ukify inspect`.  

Yeah this approach does sound a lot simpler
Comment 9 Larry the Git Cow gentoo-dev 2025-02-21 18:45:22 UTC
The bug has been referenced in the following commit(s):

https://gitweb.gentoo.org/repo/gentoo.git/commit/?id=8ebfd0b7f956e8d37c620ccaf41476dd3697e46a

commit 8ebfd0b7f956e8d37c620ccaf41476dd3697e46a
Author:     Nowa Ammerlaan <nowa@gentoo.org>
AuthorDate: 2025-02-21 18:39:48 +0000
Commit:     Nowa Ammerlaan <nowa@gentoo.org>
CommitDate: 2025-02-21 18:44:35 +0000

    sys-kernel/installkernel: add 54
    
    Leaving the bug open because the secureboot/shim situation needs
    resolving upstream.
    
    Bug: https://bugs.gentoo.org/942201
    Closes: https://github.com/gentoo/gentoo/pull/39113
    Signed-off-by: Nowa Ammerlaan <nowa@gentoo.org>

 sys-kernel/installkernel/Manifest                |   1 +
 sys-kernel/installkernel/installkernel-54.ebuild | 236 +++++++++++++++++++++++
 2 files changed, 237 insertions(+)