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

Add support for secure upgrade #11862

Merged
merged 24 commits into from
Jun 26, 2023
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
41665e5
Implemented secure upgrade
ycoheNvidia Aug 15, 2022
5848e45
Fixed weird binary related sha1 issue
ycoheNvidia Aug 18, 2022
b3fdcb4
Fixed sha1 installer issue
ycoheNvidia Aug 21, 2022
e11fb1d
Fixed minor sharch_body.sh issue
ycoheNvidia Aug 23, 2022
56c9fa6
minor typo fix
ycoheNvidia Aug 23, 2022
f44a67b
newline
ycoheNvidia Aug 23, 2022
02d126a
fixed secure upgrade image creation bug copying irrelevant files to i…
ycoheNvidia Jan 9, 2023
ea214ef
Merge branch 'master' into secure_upgrade
ycoheNvidia Jan 30, 2023
1de4175
Merge branch 'sonic-net:master' into secure_upgrade
ycoheNvidia Feb 20, 2023
19c4118
Merge branch 'master' into secure_upgrade
ycoheNvidia Mar 1, 2023
510c792
Improved prints and remove image on build failure
ycoheNvidia Apr 4, 2023
94baacf
Merge branch 'master' into secure_upgrade
ycoheNvidia Apr 4, 2023
e00f384
Merge branch 'sonic-net:master' into secure_upgrade
ycoheNvidia May 2, 2023
5860989
Reverted wrong onie-mk-demo.sh change
ycoheNvidia May 8, 2023
5b438b5
Update slave.mk
ycoheNvidia May 9, 2023
af9d766
Update onie-mk-demo.sh
ycoheNvidia May 9, 2023
25bfd26
Update build_image.sh
ycoheNvidia May 9, 2023
4afa067
Update Makefile.work
ycoheNvidia May 9, 2023
b0236db
Update build_image.sh
ycoheNvidia May 9, 2023
3b0d157
Update onie-mk-demo.sh
ycoheNvidia May 14, 2023
7447541
Merge branch 'sonic-net:master' into secure_upgrade
ycoheNvidia May 17, 2023
18d1e13
Fixed issues raised on review
ycoheNvidia May 17, 2023
f43f02d
Calling image sign on sub shell
ycoheNvidia May 30, 2023
7faa74c
Merge branch 'sonic-net:master' into secure_upgrade
ycoheNvidia May 30, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions Makefile.work
Original file line number Diff line number Diff line change
Expand Up @@ -400,6 +400,9 @@ SONIC_BUILD_INSTRUCTION := make \
SONIC_ENABLE_IMAGE_SIGNATURE=$(ENABLE_IMAGE_SIGNATURE) \
SONIC_ENABLE_SECUREBOOT_SIGNATURE=$(SONIC_ENABLE_SECUREBOOT_SIGNATURE) \
SONIC_DEFAULT_CONTAINER_REGISTRY=$(DEFAULT_CONTAINER_REGISTRY) \
SECURE_UPGRADE_MODE=$(SECURE_UPGRADE_MODE) \
SECURE_UPGRADE_DEV_SIGNING_KEY=$(SECURE_UPGRADE_DEV_SIGNING_KEY) \
SECURE_UPGRADE_DEV_SIGNING_CERT=$(SECURE_UPGRADE_DEV_SIGNING_CERT) \
ycoheNvidia marked this conversation as resolved.
Show resolved Hide resolved
ENABLE_HOST_SERVICE_ON_START=$(ENABLE_HOST_SERVICE_ON_START) \
SLAVE_DIR=$(SLAVE_DIR) \
ENABLE_AUTO_TECH_SUPPORT=$(ENABLE_AUTO_TECH_SUPPORT) \
Expand Down
2 changes: 1 addition & 1 deletion build_image.sh
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ generate_onie_installer_image()
## Note: Don't leave blank between lines. It is single line command.
./onie-mk-demo.sh $CONFIGURED_ARCH $TARGET_MACHINE $TARGET_PLATFORM-$TARGET_MACHINE-$ONIEIMAGE_VERSION \
installer platform/$TARGET_MACHINE/platform.conf $output_file OS $IMAGE_VERSION $ONIE_IMAGE_PART_SIZE \
$ONIE_INSTALLER_PAYLOAD
$ONIE_INSTALLER_PAYLOAD $SECURE_UPGRADE_DEV_SIGNING_CERT $SECURE_UPGRADE_DEV_SIGNING_KEY
ycoheNvidia marked this conversation as resolved.
Show resolved Hide resolved
}

# Generate asic-specific device list
Expand Down
3 changes: 3 additions & 0 deletions files/build_templates/sonic_debian_extension.j2
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,9 @@ fi
# Update apt's snapshot of its repos
sudo LANG=C DEBIAN_FRONTEND=noninteractive chroot $FILESYSTEM_ROOT apt-get update

# Install efitools to support secure upgrade
sudo LANG=C DEBIAN_FRONTEND=noninteractive chroot $FILESYSTEM_ROOT apt-get -y install efitools

# Apply environtment configuration files
sudo cp $IMAGE_CONFIGS/environment/environment $FILESYSTEM_ROOT/etc/
sudo cp $IMAGE_CONFIGS/environment/motd $FILESYSTEM_ROOT/etc/
Expand Down
8 changes: 6 additions & 2 deletions installer/sharch_body.sh
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@
##

echo -n "Verifying image checksum ..."
sha1=$(sed -e '1,/^exit_marker$/d' "$0" | sha1sum | awk '{ print $1 }')
payload_image_size=%%PAYLOAD_IMAGE_SIZE%%

sha1=$(sed -e '1,/^exit_marker$/d' "$0" | head -c $payload_image_size | sha1sum | awk '{ print $1 }')

payload_sha1=%%IMAGE_SHA1%%

Expand Down Expand Up @@ -45,7 +47,9 @@ if [ "$(id -u)" = "0" ] ; then
fi
cd $tmp_dir
echo -n "Preparing image archive ..."
sed -e '1,/^exit_marker$/d' $archive_path | tar xf - || exit 1

sed -e '1,/^exit_marker$/d' $archive_path | head -c $payload_image_size | tar xf - || exit 1

echo " OK."
cd $cur_wd
if [ -n "$extract" ] ; then
Expand Down
38 changes: 38 additions & 0 deletions onie-mk-demo.sh
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ output_file=$6
demo_type=$7
image_version=$8
onie_image_part_size=$9
cert_file=${11}
key_file=${12}

shift 9

Expand Down Expand Up @@ -130,7 +132,43 @@ cp $installer_dir/sharch_body.sh $output_file || {
# Replace variables in the sharch template
sed -i -e "s/%%IMAGE_SHA1%%/$sha1/" $output_file
echo -n "."
tar_size="$(wc -c < "${sharch}")"
sed -i -e "s|%%PAYLOAD_IMAGE_SIZE%%|${tar_size}|" ${output_file}
cat $sharch >> $output_file
echo "secure upgrade flags: SECURE_UPGRADE_MODE = $SECURE_UPGRADE_MODE, \
SECURE_UPGRADE_DEV_SIGNING_KEY = $SECURE_UPGRADE_DEV_SIGNING_KEY, SECURE_UPGRADE_DEV_SIGNING_CERT = $SECURE_UPGRADE_DEV_SIGNING_CERT"
ycoheNvidia marked this conversation as resolved.
Show resolved Hide resolved

if [ "$SECURE_UPGRADE_MODE" = "dev" -o "$SECURE_UPGRADE_MODE" = "prod" ]; then
CMS_SIG="${tmp_dir}/signature.sig"
echo "$0 Creating CMS signature for ${output_file} with ${key_file}. Output file ${CMS_SIG}"
DIR="$(dirname "$0")"
scripts_dir="${DIR}/scripts"
if [ "$SECURE_UPGRADE_MODE" = "dev" ]; then
. ${scripts_dir}/sign_image_dev.sh
sign_image_dev ${cert_file} ${key_file} ${output_file} ${CMS_SIG} || {
echo "CMS sign error $?"
sudo rm -rf ${CMS_SIG}
clean_up 1
}
else # "$SECURE_UPGRADE_MODE" has to be equal to "prod"
. ${scripts_dir}/sign_image_${platform}.sh
sign_image_prod ${output_file} ${CMS_SIG} || {
echo "CMS sign error $?"
sudo rm -rf ${CMS_SIG}
clean_up 1
}
fi
[ -f "$CMS_SIG" ] || {
echo "Error: CMS signature not created - exiting without signing"
clean_up 1
}
# append signature to binary
cat ${CMS_SIG} >> ${output_file}
sudo rm -rf ${CMS_SIG}
elif [ "$SECURE_UPGRADE_MODE" != "no_sign" ]; then
echo "SECURE_UPGRADE_MODE not defined or defined as $SECURE_UPGRADE_MODE - build without signing"
fi

rm -rf $tmp_dir
echo " Done."

Expand Down
8 changes: 8 additions & 0 deletions rules/config
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,14 @@ SONIC_ENABLE_IMAGE_SIGNATURE ?= n
# The absolute path should be provided.
SONIC_ENABLE_SECUREBOOT_SIGNATURE ?= n

# following flags are used for image secure upgrade verification:
# SECURE_UPGRADE_DEV_SIGNING_KEY - path to development signing key, used for image signing during build
# SECURE_UPGRADE_DEV_SIGNING_CERT - path to development signing certificate, used for image signing during build
# SECURE_UPGRADE_MODE - enum value for secure upgrade mode, valid options are "dev", "prod" and "no_sign"
#SECURE_UPGRADE_DEV_SIGNING_KEY =
#SECURE_UPGRADE_DEV_SIGNING_CERT =
SECURE_UPGRADE_MODE = "no_sign"
Copy link
Collaborator

@xumia xumia Nov 14, 2022

Choose a reason for hiding this comment

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

Can we only give two options: sign, no_sign? The dev or prod is depended on what kinds of certificates users used, and we can provide a script to generate all dev certificates.

We may be not necessary to provide some options only for DEV.

#SECURE_UPGRADE_DEV_SIGNING_KEY = 
#SECURE_UPGRADE_DEV_SIGNING_CERT =  

Suggest change as below:

#SECURE_UPGRADE_SIGNING_KEY = 
#SECURE_UPGRADE_SIGNING_CERT =  

Do we need to distinguish the UPGRADE/NONE-UPGRADE certificate? The signed image can be used for both fresh installation or upgrade, right?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This mechanism is part of the feature architecture, as described in its HLD. Doing as your suggestion will change our architecture and we prefer not to do so in this stage. There is a serious demand from our costumers, to allow production signing with their own signing mechanisms. If someone wants to use the current flags they can take the DEV_SIGNING flags and assign it to a different variable or flag.
For your last question - signed image can be used for both fresh installation (from ONIE) or upgrade (from SONIC). In case of ONIE installation - ONIE master is currently not verifying the image - it can be implemented in the future if required and approved


# PACKAGE_URL_PREFIX - the package url prefix
PACKAGE_URL_PREFIX ?= https://packages.trafficmanager.net/public/packages

Expand Down
14 changes: 14 additions & 0 deletions scripts/sign_image_dev.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
sign_image_dev()
ycoheNvidia marked this conversation as resolved.
Show resolved Hide resolved
{
cert_file=$1
key_file=$2
image_to_sign=$3
cms_sig_out=$4
openssl cms -sign -nosmimecap -signer ${cert_file} -inkey ${key_file} -binary -in $image_to_sign -outform pem -out ${cms_sig_out} || {
echo "$?: CMS sign error"
sudo rm -rf ${cms_sig_out}
exit 1
}
echo "CMS sign OK"
ycoheNvidia marked this conversation as resolved.
Show resolved Hide resolved
return 0
}
9 changes: 9 additions & 0 deletions slave.mk
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,9 @@ $(info "USE_NATIVE_DOCKERD_FOR_BUILD" : "$(SONIC_CONFIG_USE_NATIVE_DOCKERD_FO
$(info "SONIC_USE_DOCKER_BUILDKIT" : "$(SONIC_USE_DOCKER_BUILDKIT)")
$(info "USERNAME" : "$(USERNAME)")
$(info "PASSWORD" : "$(PASSWORD)")
$(info "SECURE_UPGRADE_MODE" : "$(SECURE_UPGRADE_MODE)")
$(info "SECURE_UPGRADE_DEV_SIGNING_KEY" : "$(SECURE_UPGRADE_DEV_SIGNING_KEY)")
$(info "SECURE_UPGRADE_DEV_SIGNING_CERT" : "$(SECURE_UPGRADE_DEV_SIGNING_CERT)")
ycoheNvidia marked this conversation as resolved.
Show resolved Hide resolved
$(info "ENABLE_DHCP_GRAPH_SERVICE" : "$(ENABLE_DHCP_GRAPH_SERVICE)")
$(info "SHUTDOWN_BGP_ON_START" : "$(SHUTDOWN_BGP_ON_START)")
$(info "ENABLE_PFCWD_ON_START" : "$(ENABLE_PFCWD_ON_START)")
Expand Down Expand Up @@ -1174,6 +1177,9 @@ $(addprefix $(TARGET_PATH)/, $(SONIC_INSTALLERS)) : $(TARGET_PATH)/% : \
export enable_organization_extensions="$(ENABLE_ORGANIZATION_EXTENSIONS)"
export enable_dhcp_graph_service="$(ENABLE_DHCP_GRAPH_SERVICE)"
export enable_ztp="$(ENABLE_ZTP)"
export sonic_su_dev_signing_key="$(SECURE_UPGRADE_DEV_SIGNING_KEY)"
export sonic_su_dev_signing_cert="$(SECURE_UPGRADE_DEV_SIGNING_CERT)"
export sonic_su_mode="$(SECURE_UPGRADE_MODE)"
ycoheNvidia marked this conversation as resolved.
Show resolved Hide resolved
export include_system_telemetry="$(INCLUDE_SYSTEM_TELEMETRY)"
export include_restapi="$(INCLUDE_RESTAPI)"
export include_nat="$(INCLUDE_NAT)"
Expand Down Expand Up @@ -1373,6 +1379,9 @@ $(addprefix $(TARGET_PATH)/, $(SONIC_INSTALLERS)) : $(TARGET_PATH)/% : \
TARGET_MACHINE=$(dep_machine) \
IMAGE_TYPE=$($*_IMAGE_TYPE) \
SONIC_ENABLE_IMAGE_SIGNATURE="$(SONIC_ENABLE_IMAGE_SIGNATURE)" \
SECURE_UPGRADE_MODE="$(SECURE_UPGRADE_MODE)" \
SECURE_UPGRADE_DEV_SIGNING_KEY="$(SECURE_UPGRADE_DEV_SIGNING_KEY)" \
SECURE_UPGRADE_DEV_SIGNING_CERT="$(SECURE_UPGRADE_DEV_SIGNING_CERT)" \
SIGNING_KEY="$(SIGNING_KEY)" \
SIGNING_CERT="$(SIGNING_CERT)" \
CA_CERT="$(CA_CERT)" \
Expand Down