Skip to content

Commit

Permalink
Avoid changing flash to work better with tc-flash-hash
Browse files Browse the repository at this point in the history
  • Loading branch information
tadeubas committed Dec 2, 2024
1 parent 8c7779a commit d2a99d1
Showing 1 changed file with 41 additions and 25 deletions.
66 changes: 41 additions & 25 deletions src/krux/pages/tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,9 @@ def __init__(self, ctx):
),
)

def _check_signature(self, path_prefix, filename, data_hash):
def _check_signature(self, sig, data_hash):
from embit import ec
from ..metadata import SIGNER_PUBKEY

Check warning on line 66 in src/krux/pages/tools.py

View check run for this annotation

Codecov / codecov/patch

src/krux/pages/tools.py#L65-L66

Added lines #L65 - L66 were not covered by tests
from krux.sd_card import SIGNATURE_FILE_EXTENSION

pubkey = None
try:
Expand All @@ -73,13 +72,6 @@ def _check_signature(self, path_prefix, filename, data_hash):
self.flash_error(t("Invalid public key"))
return MENU_CONTINUE

Check warning on line 73 in src/krux/pages/tools.py

View check run for this annotation

Codecov / codecov/patch

src/krux/pages/tools.py#L68-L73

Added lines #L68 - L73 were not covered by tests

sig = None
try:
sig = open(path_prefix + filename + SIGNATURE_FILE_EXTENSION, "rb").read()
except:
self.flash_error(t("Missing signature file"))
return MENU_CONTINUE

try:

Check warning on line 75 in src/krux/pages/tools.py

View check run for this annotation

Codecov / codecov/patch

src/krux/pages/tools.py#L75

Added line #L75 was not covered by tests
# Parse, serialize, and reparse to ensure signature is compact prior to verification
sig = ec.Signature.parse(ec.Signature.parse(sig).serialize())

Check warning on line 77 in src/krux/pages/tools.py

View check run for this annotation

Codecov / codecov/patch

src/krux/pages/tools.py#L77

Added line #L77 was not covered by tests
Expand Down Expand Up @@ -135,30 +127,55 @@ def sd_load_app(self): # pylint: disable=R1710

# Check signature of .mpy file in SD
path_prefix = "/%s/" % SD_PATH
if self._check_signature(path_prefix, filename, data_hash) == MENU_CONTINUE:
sig_data = None
try:
sig_data = open(

Check warning on line 132 in src/krux/pages/tools.py

View check run for this annotation

Codecov / codecov/patch

src/krux/pages/tools.py#L129-L132

Added lines #L129 - L132 were not covered by tests
path_prefix + filename + SIGNATURE_FILE_EXTENSION, "rb"
).read()
except:
self.flash_error(t("Missing signature file"))
return MENU_CONTINUE

Check warning on line 137 in src/krux/pages/tools.py

View check run for this annotation

Codecov / codecov/patch

src/krux/pages/tools.py#L135-L137

Added lines #L135 - L137 were not covered by tests

if self._check_signature(sig_data, data_hash) == MENU_CONTINUE:
return MENU_CONTINUE

Check warning on line 140 in src/krux/pages/tools.py

View check run for this annotation

Codecov / codecov/patch

src/krux/pages/tools.py#L139-L140

Added lines #L139 - L140 were not covered by tests
sig_data = open(path_prefix + filename + SIGNATURE_FILE_EXTENSION, "rb").read()

# Delete any .mpy files from flash VFS to avoid malicious code import/execution
import os
from krux.settings import FLASH_PATH

Check warning on line 144 in src/krux/pages/tools.py

View check run for this annotation

Codecov / codecov/patch

src/krux/pages/tools.py#L143-L144

Added lines #L143 - L144 were not covered by tests

found_in_flash_vfs = False
path_prefix = "/%s/" % FLASH_PATH
for file in os.listdir(path_prefix):
if file.endswith(MPY_FILE_EXTENSION):

Check warning on line 149 in src/krux/pages/tools.py

View check run for this annotation

Codecov / codecov/patch

src/krux/pages/tools.py#L146-L149

Added lines #L146 - L149 were not covered by tests
os.remove(path_prefix + file)
# Check if file is the same from SD
if (

Check warning on line 151 in src/krux/pages/tools.py

View check run for this annotation

Codecov / codecov/patch

src/krux/pages/tools.py#L151

Added line #L151 was not covered by tests
hashlib.sha256(open(path_prefix + file, "rb").read()).digest()
!= data_hash
):
os.remove(path_prefix + file)

Check warning on line 155 in src/krux/pages/tools.py

View check run for this annotation

Codecov / codecov/patch

src/krux/pages/tools.py#L155

Added line #L155 was not covered by tests

# Copy kapp + sig from SD to flash VFS
# sig file allows the check and execution of the kapp at startup for opsec
kapp_filename = "kapp"
with open(path_prefix + kapp_filename + MPY_FILE_EXTENSION, "wb") as kapp_file:
kapp_file.write(data)

with open(
path_prefix + kapp_filename + MPY_FILE_EXTENSION + SIGNATURE_FILE_EXTENSION,
"wb",
) as kapp_sig_file:
kapp_sig_file.write(sig_data)
if not found_in_flash_vfs:
with open(

Check warning on line 161 in src/krux/pages/tools.py

View check run for this annotation

Codecov / codecov/patch

src/krux/pages/tools.py#L159-L161

Added lines #L159 - L161 were not covered by tests
path_prefix + kapp_filename + MPY_FILE_EXTENSION, "wb"
) as kapp_file:
kapp_file.write(data)

Check warning on line 164 in src/krux/pages/tools.py

View check run for this annotation

Codecov / codecov/patch

src/krux/pages/tools.py#L164

Added line #L164 was not covered by tests

with open(

Check warning on line 166 in src/krux/pages/tools.py

View check run for this annotation

Codecov / codecov/patch

src/krux/pages/tools.py#L166

Added line #L166 was not covered by tests
path_prefix
+ kapp_filename
+ MPY_FILE_EXTENSION
+ SIGNATURE_FILE_EXTENSION,
"wb",
) as kapp_sig_file:
kapp_sig_file.write(sig_data)

Check warning on line 173 in src/krux/pages/tools.py

View check run for this annotation

Codecov / codecov/patch

src/krux/pages/tools.py#L173

Added line #L173 was not covered by tests

del data, sig_data
import gc

Check warning on line 176 in src/krux/pages/tools.py

View check run for this annotation

Codecov / codecov/patch

src/krux/pages/tools.py#L175-L176

Added lines #L175 - L176 were not covered by tests

gc.collect()

Check warning on line 178 in src/krux/pages/tools.py

View check run for this annotation

Codecov / codecov/patch

src/krux/pages/tools.py#L178

Added line #L178 was not covered by tests

# Allows import of files in flash VFS
# TODO: Dinamically enable vsf->execution
Expand All @@ -169,17 +186,16 @@ def sd_load_app(self): # pylint: disable=R1710
try:
i_kapp = __import__(kapp_filename)
i_kapp.run(self.ctx)

# avoids importing from flash VSF
os.chdir("/")
except Exception as e:
except:

Check warning on line 189 in src/krux/pages/tools.py

View check run for this annotation

Codecov / codecov/patch

src/krux/pages/tools.py#L185-L189

Added lines #L185 - L189 were not covered by tests
# avoids importing from flash VSF
os.chdir("/")

Check warning on line 191 in src/krux/pages/tools.py

View check run for this annotation

Codecov / codecov/patch

src/krux/pages/tools.py#L191

Added line #L191 was not covered by tests

print(e)
self.flash_error(t("Could not execute %s") % filename)
return MENU_CONTINUE

Check warning on line 194 in src/krux/pages/tools.py

View check run for this annotation

Codecov / codecov/patch

src/krux/pages/tools.py#L193-L194

Added lines #L193 - L194 were not covered by tests

# avoids importing from flash VSF
os.chdir("/")

Check warning on line 197 in src/krux/pages/tools.py

View check run for this annotation

Codecov / codecov/patch

src/krux/pages/tools.py#L197

Added line #L197 was not covered by tests

print("Exit kapp!")

Check warning on line 199 in src/krux/pages/tools.py

View check run for this annotation

Codecov / codecov/patch

src/krux/pages/tools.py#L199

Added line #L199 was not covered by tests
# After execution restart Krux (better safe than sorry)
from ..power import power_manager

Check warning on line 201 in src/krux/pages/tools.py

View check run for this annotation

Codecov / codecov/patch

src/krux/pages/tools.py#L201

Added line #L201 was not covered by tests
Expand Down

0 comments on commit d2a99d1

Please sign in to comment.