Skip to content

Conversation

@Johan-Liebert1
Copy link
Collaborator

Add a function to get the raw section out of a UKI without converting it to a string.

Useful in bootc for checking kernel skew while performing soft-reboot

image: &'a [u8],
section_name: &'static str,
) -> Option<Result<&'a str, UkiError>> {
get_section(image, section_name).map(|res| {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can't you just use ?.and_then for that instead of the map? None on an Option works the same way as Err...

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need map to get an Option. Doing just ?.and_then converts the entire thing to just a Result. But yeah, if we simplify the return type to just Result<..> then we could get away without the map

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think in any case this function ought not to return Option<Result<..>>... the None return from the inner function is meant to mean "PortableExecutableError" so we should just convert that in the "text" variant of the function, I think... Having a separate None possibility makes no sense here...

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed. Updated the PR

image: &'a [u8],
section_name: &'static str,
) -> Option<Result<&'a str, UkiError>> {
) -> Option<Result<&'a [u8], UkiError>> {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there anything left in this function that returns Some(Err(..))? I think you could simplify the return value to just be Option<&'a [u8]> now, no?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We do have

    if pe_header.pe_magic != PE_MAGIC {
        return None;
    }

which maybe could be an error instead?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The final return value has to be a Result as all of the zerocopy functions return Results. We can probably do with a Result return type

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's a comment before the function explaining how we convert all of the Result to Option because the error return type of the zerocopy functions is evil. That's what all of that .ok()? all over the place is about.

Copy link
Collaborator Author

@Johan-Liebert1 Johan-Liebert1 Dec 10, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see. But still, returning just an Option feels weird as we lose info like UkiError::MissingSection / UTF-8 string conversion error, etc

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah indeed the part where we return MissingSection is just out of the context of the diff so I missed it :)

Copy link
Collaborator

@allisonkarlitskaya allisonkarlitskaya left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks more or less correct now, minus one question. Can you squash the commits since the second one is just deleting most of the code from the first one?

let (pe_header, rest) = PeHeader::ref_from_prefix(rest).ok()?;
if pe_header.pe_magic != PE_MAGIC {
return None;
return Some(Err(UkiError::PortableExecutableError));
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Was this change required?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not really, but thought it conveys the intent better? If there is a header mismatch then we can consider it as an error

Add a function to get the raw section out of a UKI without converting it
to a string.

Useful in bootc for checking kernel skew while performing soft-reboot

Signed-off-by: Pragyan Poudyal <pragyanpoudyal41999@gmail.com>

uki: Refactor `get_text_section` to return `Result`

In `get_section` return an error on PE Header mismatch

Signed-off-by: Pragyan Poudyal <pragyanpoudyal41999@gmail.com>
@Johan-Liebert1
Copy link
Collaborator Author

Squashed the commits into one

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.

2 participants