Skip to content

Commit 64dab4a

Browse files
committed
Added check for image verification support
1 parent 4d32280 commit 64dab4a

8 files changed

+37
-24
lines changed

scripts/verify_image_sign.sh

-15
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,10 @@
22
image_file="${1}"
33
cms_sig_file="sig.cms"
44
lines_for_lookup=50
5-
SECURE_UPGRADE_ENABLED=0
65
DIR="$(dirname "$0")"
7-
if [ -d "/sys/firmware/efi/efivars" ]; then
8-
if ! [ -n "$(ls -A /sys/firmware/efi/efivars 2>/dev/null)" ]; then
9-
mount -t efivarfs none /sys/firmware/efi/efivars 2>/dev/null
10-
fi
11-
SECURE_UPGRADE_ENABLED=$(bootctl status 2>/dev/null | grep -c "Secure Boot: enabled")
12-
else
13-
echo "efi not supported - exiting without verification"
14-
exit 0
15-
fi
166

177
. /usr/local/bin/verify_image_sign_common.sh
188

19-
if [ ${SECURE_UPGRADE_ENABLED} -eq 0 ]; then
20-
echo "secure boot not enabled - exiting without image verification"
21-
exit 0
22-
fi
23-
249
clean_up ()
2510
{
2611
if [ -d ${EFI_CERTS_DIR} ]; then rm -rf ${EFI_CERTS_DIR}; fi

sonic_installer/bootloader/bootloader.py

+3
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,9 @@ def verify_image_sign(self, image_path):
7979
"""verify image signature is valid"""
8080
raise NotImplementedError
8181

82+
def is_secure_upgrade_image_verification_supported(self):
83+
return False
84+
8285
@classmethod
8386
def detect(cls):
8487
"""returns True if the bootloader is in use"""

sonic_installer/bootloader/grub.py

+24
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,30 @@ def verify_image_platform(self, image_path):
153153
# Check if platform is inside image's target platforms
154154
return self.platform_in_platforms_asic(platform, image_path)
155155

156+
def is_secure_upgrade_image_verification_supported(self):
157+
158+
check_if_verification_is_enabled_and_supported_code = '''
159+
SECURE_UPGRADE_ENABLED=0
160+
if [ -d "/sys/firmware/efi/efivars" ]; then
161+
if ! [ -n "$(ls -A /sys/firmware/efi/efivars 2>/dev/null)" ]; then
162+
mount -t efivarfs none /sys/firmware/efi/efivars 2>/dev/null
163+
fi
164+
SECURE_UPGRADE_ENABLED=$(bootctl status 2>/dev/null | grep -c "Secure Boot: enabled")
165+
else
166+
echo "efi not supported - exiting without verification"
167+
exit 1
168+
fi
169+
170+
if [ ${SECURE_UPGRADE_ENABLED} -eq 0 ]; then
171+
echo "secure boot not enabled - exiting without image verification"
172+
exit 1
173+
fi
174+
exit 0
175+
'''
176+
verification_result = subprocess.run(['bash', '-c', check_if_verification_is_enabled_and_supported_code], check=True, capture_output=True)
177+
click.echo(str(verification_result.stdout) + " " + str(verification_result.stderr))
178+
return verification_result.returncode == 0
179+
156180
def verify_image_sign(self, image_path):
157181
click.echo('Verifying image signature')
158182
verification_script_name = 'verify_image_sign.sh'

sonic_installer/main.py

+3-5
Original file line numberDiff line numberDiff line change
@@ -577,16 +577,14 @@ def install(url, force, skip_platform_check=False, skip_migration=False, skip_pa
577577
"Aborting...", LOG_ERR)
578578
raise click.Abort()
579579

580-
# Calling verification script by default - signature will be checked if enabled in bios
581-
echo_and_log("Verifing image {} signature...".format(binary_image_version))
582-
try:
580+
if bootloader.is_secure_upgrade_image_verification_supported():
581+
echo_and_log("Verifing image {} signature...".format(binary_image_version))
583582
if not bootloader.verify_image_sign(image_path):
584583
echo_and_log('Error: Failed verify image signature', LOG_ERR)
585584
raise click.Abort()
586585
else:
587586
echo_and_log('Verification successful')
588-
except NotImplementedError:
589-
echo_and_log('Image verification not impelmented, continue image install without it')
587+
590588
echo_and_log("Installing image {} and setting it as default...".format(binary_image_version))
591589
with SWAPAllocator(not skip_setup_swap, swap_mem_size, total_mem_threshold, available_mem_threshold):
592590
bootloader.install_image(image_path)

tests/installer_bootloader_aboot_test.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -96,9 +96,10 @@ def test_set_fips_aboot():
9696
def test_verify_image_sign():
9797
bootloader = aboot.AbootBootloader()
9898
return_value = None
99+
is_supported = bootloader.is_secure_upgrade_image_verification_supported()
99100
try:
100101
return_value = bootloader.verify_image_sign(exp_image)
101102
except NotImplementedError:
102-
pass
103+
assert not is_supported
103104
else:
104105
assert False, "Wrong return value from verify_image_sign, returned" + str(return_value)

tests/installer_bootloader_grub_test.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,6 @@ def test_verify_image():
9393

9494
bootloader = grub.GrubBootloader()
9595
image = f'{grub.IMAGE_PREFIX}expeliarmus-{grub.IMAGE_PREFIX}abcde'
96-
96+
assert bootloader.is_secure_upgrade_image_verification_supported()
9797
# command should fail
9898
assert not bootloader.verify_image_sign(image)

tests/installer_bootloader_onie_test.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,10 @@ def test_get_current_image(re_search):
1919
def test_verify_image_sign():
2020
bootloader = onie.OnieInstallerBootloader()
2121
return_value = None
22+
is_supported = bootloader.is_secure_upgrade_image_verification_supported()
2223
try:
2324
return_value = bootloader.verify_image_sign('some_path.path')
2425
except NotImplementedError:
25-
pass
26+
assert not is_supported
2627
else:
2728
assert False, "Wrong return value from verify_image_sign, returned" + str(return_value)

tests/installer_bootloader_uboot_test.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -146,9 +146,10 @@ def test_verify_image_sign():
146146
bootloader = uboot.UbootBootloader()
147147
image = 'test-image'
148148
return_value = None
149+
is_supported = bootloader.is_secure_upgrade_image_verification_supported()
149150
try:
150151
return_value = bootloader.verify_image_sign(image)
151152
except NotImplementedError:
152-
pass
153+
assert not is_supported
153154
else:
154155
assert False, "Wrong return value from verify_image_sign, returned" + str(return_value)

0 commit comments

Comments
 (0)