Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Null-terminate 'arguments' in fallback #611

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

vittyvk
Copy link

@vittyvk vittyvk commented Sep 6, 2023

In case CSV entry contains boot argument (e.g. an image to load for shim) it must be null-terminated. While populate_stanza() makes sure 'arguments' end with '\0', add_boot_option() doesn't account for it in 'size' calculations. E.g. for the following CSV entry:

 shimx64.efi,6.6.0-0.rc0.20230904git708283abf896.6.fc40.x86_64,\EFI\Linux\5f93b3c9cf1c488a99786fb8e99fb840-6.6.0-0.rc0.20230904git708283abf896.6.fc40.x86_64.efi,Comment

the resulting variable after 'fallback' looks like:

 # hexdump /sys/firmware/efi/efivars/Boot0004-8be4df61-93ca-11d2-aa0d-00e098032b8c | tail -3
 0000180 0038 0036 005f 0036 0034 002e 0065 0066
 0000190 0069
 0000192

Add trailing '\0' to 'size' calculations in add_boot_option() when 'arguments' is not empty. The resulting variable looks like:

 # hexdump /sys/firmware/efi/efivars/Boot0004-8be4df61-93ca-11d2-aa0d-00e098032b8c | tail -3
 0000180 0038 0036 005f 0036 0034 002e 0065 0066
 0000190 0069 0000
 0000194

and the specified image is loaded by shim without issues.

snbuback pushed a commit to snbuback/virt-firmware that referenced this pull request Sep 12, 2023
In case CSV entry contains boot argument (e.g. an image to load for shim)
it must be null-terminated. While populate_stanza() makes sure 'arguments'
end with '\0', add_boot_option() doesn't account for it in 'size'
calculations. E.g. for the following CSV entry:

 shimx64.efi,6.6.0-0.rc0.20230904git708283abf896.6.fc40.x86_64,\EFI\Linux\5f93b3c9cf1c488a99786fb8e99fb840-6.6.0-0.rc0.20230904git708283abf896.6.fc40.x86_64.efi,Comment

the resulting variable after 'fallback' looks like:

 # hexdump /sys/firmware/efi/efivars/Boot0004-8be4df61-93ca-11d2-aa0d-00e098032b8c | tail -3
 0000180 0038 0036 005f 0036 0034 002e 0065 0066
 0000190 0069
 0000192

Add trailing '\0' to 'size' calculations in add_boot_option() when
'arguments' is not empty. The resulting variable looks like:

 # hexdump /sys/firmware/efi/efivars/Boot0004-8be4df61-93ca-11d2-aa0d-00e098032b8c | tail -3
 0000180 0038 0036 005f 0036 0034 002e 0065 0066
 0000190 0069 0000
 0000194

and the specified image is loaded by shim without issues.

Signed-off-by: Vitaly Kuznetsov <vkuznets@redhat.com>
@vittyvk
Copy link
Author

vittyvk commented Apr 29, 2024

ping?

@julian-klode
Copy link
Collaborator

julian-klode commented Apr 29, 2024

FWIW, the message is misleading you into thinking it's a single \0 byte, but it's a \0 UCS-2 character (i.e. 2 bytes).

But yes I believe this is useful. It's not strictly necessary: As I'm sure you know, you can always append a space to the entry in the .csv and it works. But it's an ugly workaround for sure.

I can't speak to whether the implementation is otherwise correct as I haven't looked at it in detail, mostly just tracing the reasoning with my work. Generally I'd favor not copying trailing \0s, but append them explicitly again, but I don't have enough context in the code here.

@vittyvk
Copy link
Author

vittyvk commented Apr 30, 2024

The workaround using extra space is indeed known and is already used in the wild, e.g.
https://gitlab.com/kraxel/virt-firmware/-/commit/77d3801ccc963da747ba576da0fe586d352615d3
but it probably takes some time to discover it. It'd be nice to have a permanent solution.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants