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

fix erroneous shutdown when rejecting firmware upload #3122

Merged
merged 2 commits into from
Sep 27, 2023

Conversation

TychoVrahe
Copy link
Contributor

this was only triggered when bootloader does not detect valid firmware, therefore shows "get started with your trezor" screen, but at least firmware header is valid so bootloader asks for confirmation.

@TychoVrahe TychoVrahe requested a review from prusnak as a code owner June 30, 2023 21:05
@TychoVrahe TychoVrahe self-assigned this Jun 30, 2023
@matejcik
Copy link
Contributor

matejcik commented Jul 3, 2023

this doesn't look right

first off you're repeatedly erasing flash in the while-true loop, that's not great

second, we probably shouldn't infinite-loop in the first place:

  • CONTINUE boots to firmware
  • SHUTDOWN early-returns
  • RETURN might want to go into the normal boot menu instead of returning to "Welcome to Trezor"

@matejcik
Copy link
Contributor

matejcik commented Jul 3, 2023

(while we're here, maybe rename CONTINUE -> CONTINUE_TO_FIRMWARE and RETURN -> RETURN_TO_MENU ?)

@TychoVrahe
Copy link
Contributor Author

As suggested: dfe5ded

Note that RETURN_TO_MENU does not always return to menu, but sometimes to the Intro screen (the one you can select to install firmware or enter menu).

Also. When user cancels the upload now, he is taken to Intro screen, where he can go to menu and 'restart' from there: which will then show fatal error with "firmware corrupted" message. This is also case when there is firmware header but corrupted fw and user chooses in the welcome screen to issue Wipe command through trezorctl and cancels.

Even worse probably is that if there is no firmware header, user does the wipe command, cancels, there is crash with BL.rs message, as the Intro screen expects some data from the header, which are not there. This will definitely need some fixing, question is what is the correct behavior in this situation?

@matejcik
Copy link
Contributor

matejcik commented Jul 7, 2023

Even worse probably is that if there is no firmware header, user does the wipe command, cancels, there is crash with BL.rs message, as the Intro screen expects some data from the header, which are not there. This will definitely need some fixing, question is what is the correct behavior in this situation?

maybe we should fold the "go to trezor.io/start" screen into the main thing? like, if there is a fw header, we show the blue intro, and if there isn't, we connect to host directly and show the black welcome?

@TychoVrahe
Copy link
Contributor Author

trying that in 22ca85e

Seems to be working fine, one thing is that when this (corrupted fw with valid header) happens, we now go directly to bootloader menu and user has no clue why. Maybe we should in the "blue intro" screen show some warning that fw is corrupted?

Another thing, "header present" is evaluated with same logic as when deciding whether to ask user for confirmation, but at least theoretically more things can happen, like having valid header for different model will be evaluated as no-header in this context. This is probably fine, just something to be aware of.

@Hannsek Hannsek requested a review from matejcik August 10, 2023 13:48
@matejcik
Copy link
Contributor

it turns out that this PR incidentally implements a variant of #2794, right? header_present and firmware_present are considered separate states.

Tagging @andrewkozlik and @prusnak for a careful review of the logic here. It looks perfectly sensible to me: determine header validity separate from firmware validity, and only erase seed if the header is missing.
But we do need to be careful here never to jump into firmware unless all the checks have passed.

wrt merging, let's wait with this until #2919 is ready and rebase on top of that. When that is ready, we'll need to carefully review the resulting code, hopefully with notes about what to look out for from here.

@TychoVrahe TychoVrahe force-pushed the tychovrahe/bootloader/fix_shutdown branch from 22ca85e to ea96641 Compare August 16, 2023 10:47
@TychoVrahe
Copy link
Contributor Author

rebased on current master.

@Hannsek
Copy link
Contributor

Hannsek commented Aug 30, 2023

What has to be done here in order to merge it?

@TychoVrahe
Copy link
Contributor Author

as said in the comment above, this is waiting for #2919 and it needs a review after rebasing.

@Hannsek
Copy link
Contributor

Hannsek commented Aug 30, 2023

Ahh, sorry, didn't notice the comment.

@TychoVrahe TychoVrahe force-pushed the tychovrahe/bootloader/fix_shutdown branch 2 times, most recently from 0a7bed6 to 650ff96 Compare September 18, 2023 13:09
@Hannsek Hannsek requested a review from hiviah September 19, 2023 04:56
@matejcik
Copy link
Contributor

desired modifications:

  • use stronger constants for return values of bootloader_usb_loop -- and probably also for ui_results?
  • in case firmware is corrupted, (a) indicate this on INTRO screen, and (b) remove "continue to firmware" option from MENU screen
  • convert if (continue_to_firmware) break; to something like, "if continue_to_firmware, then double-check against another copy and invoke "do_jump_to_firmware()", which is a pointer whose default value is some sort of halt, but the step which sets continue_to_firmware also sets it to the correct value.
    • maybe the function pointer itself can replace the continue_to_firmware variable? if (jump_fn == &do_firmware_jump) { jump_fn() } ?

@TychoVrahe
Copy link
Contributor Author

Part of the hardening done here: c5e7395

@TychoVrahe
Copy link
Contributor Author

The corrupted warnings are like this atm. In T2B1 the reboot choice is removed from the menu entirely.
20230919_160149
20230919_153638
20230919_153623

@Hannsek
Copy link
Contributor

Hannsek commented Sep 20, 2023

Ahh, this. I saw it yesterday. It seems to be ok as this should be visible only is case of something wrong happens.

Copy link
Contributor Author

@TychoVrahe TychoVrahe left a comment

Choose a reason for hiding this comment

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

Looking at the glitching protection part @hiviah , just two minor things, otherwise it looks reasonable to me

core/embed/bootloader/main.c Outdated Show resolved Hide resolved
core/embed/bootloader/main.c Outdated Show resolved Hide resolved
core/embed/rust/src/ui/model_tr/bootloader/menu.rs Outdated Show resolved Hide resolved
core/embed/bootloader/main.c Outdated Show resolved Hide resolved
core/embed/bootloader/main.c Outdated Show resolved Hide resolved
@matejcik
Copy link
Contributor

added a thing in f286ca8, @TychoVrahe @hiviah please review

@TychoVrahe
Copy link
Contributor Author

ad "a thing" : this does not seem to work when checking bootloader image (in boardloader). When updating the bootloader the sector is padded with zeroes (not sure what combined image contains). Also it does not count with checking images with only one chunk (=bootloader again) - as the remaining partial chunk is checked beyond the first chuck. @matejcik

@matejcik
Copy link
Contributor

this does not seem to work when checking bootloader image

thanks, didn't realize that boardloader also uses this code.
that is a little problematic and it's not required here so i'm backing it out for now, will make a separate PR for it

@matejcik matejcik force-pushed the tychovrahe/bootloader/fix_shutdown branch from f286ca8 to 84aa673 Compare September 26, 2023 18:15
@TychoVrahe
Copy link
Contributor Author

tried to handle remaining issues.

@TychoVrahe
Copy link
Contributor Author

and one more fix 7f93d97

@TychoVrahe TychoVrahe force-pushed the tychovrahe/bootloader/fix_shutdown branch 2 times, most recently from 985393b to 97ea3be Compare September 27, 2023 09:48
@TychoVrahe TychoVrahe force-pushed the tychovrahe/bootloader/fix_shutdown branch from 97ea3be to 77061a8 Compare September 27, 2023 09:49
@TychoVrahe TychoVrahe merged commit 36e4a44 into master Sep 27, 2023
8 checks passed
@TychoVrahe TychoVrahe deleted the tychovrahe/bootloader/fix_shutdown branch September 27, 2023 10:14
@bosomt
Copy link

bosomt commented Oct 31, 2023

QA OK

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

Successfully merging this pull request may close these issues.

5 participants