Skip to content

Commit 41665e5

Browse files
committed
Implemented secure upgrade
1 parent 8d6431e commit 41665e5

File tree

8 files changed

+85
-3
lines changed

8 files changed

+85
-3
lines changed

Makefile.work

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -400,6 +400,9 @@ SONIC_BUILD_INSTRUCTION := make \
400400
SONIC_ENABLE_IMAGE_SIGNATURE=$(ENABLE_IMAGE_SIGNATURE) \
401401
SONIC_ENABLE_SECUREBOOT_SIGNATURE=$(SONIC_ENABLE_SECUREBOOT_SIGNATURE) \
402402
SONIC_DEFAULT_CONTAINER_REGISTRY=$(DEFAULT_CONTAINER_REGISTRY) \
403+
SECURE_UPGRADE_MODE=$(SECURE_UPGRADE_MODE) \
404+
SECURE_UPGRADE_DEV_SIGNING_KEY=$(SECURE_UPGRADE_DEV_SIGNING_KEY) \
405+
SECURE_UPGRADE_DEV_SIGNING_CERT=$(SECURE_UPGRADE_DEV_SIGNING_CERT) \
403406
ENABLE_HOST_SERVICE_ON_START=$(ENABLE_HOST_SERVICE_ON_START) \
404407
SLAVE_DIR=$(SLAVE_DIR) \
405408
ENABLE_AUTO_TECH_SUPPORT=$(ENABLE_AUTO_TECH_SUPPORT) \

build_image.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ generate_onie_installer_image()
8686
## Note: Don't leave blank between lines. It is single line command.
8787
./onie-mk-demo.sh $CONFIGURED_ARCH $TARGET_MACHINE $TARGET_PLATFORM-$TARGET_MACHINE-$ONIEIMAGE_VERSION \
8888
installer platform/$TARGET_MACHINE/platform.conf $output_file OS $IMAGE_VERSION $ONIE_IMAGE_PART_SIZE \
89-
$ONIE_INSTALLER_PAYLOAD
89+
$ONIE_INSTALLER_PAYLOAD $SECURE_UPGRADE_DEV_SIGNING_CERT $SECURE_UPGRADE_DEV_SIGNING_KEY
9090
}
9191

9292
# Generate asic-specific device list

files/build_templates/sonic_debian_extension.j2

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,9 @@ fi
7878
# Update apt's snapshot of its repos
7979
sudo LANG=C DEBIAN_FRONTEND=noninteractive chroot $FILESYSTEM_ROOT apt-get update
8080

81+
# Install efitools to support secure upgrade
82+
sudo LANG=C DEBIAN_FRONTEND=noninteractive chroot $FILESYSTEM_ROOT apt-get -y install efitools
83+
8184
# Apply environtment configuration files
8285
sudo cp $IMAGE_CONFIGS/environment/environment $FILESYSTEM_ROOT/etc/
8386
sudo cp $IMAGE_CONFIGS/environment/motd $FILESYSTEM_ROOT/etc/

installer/sharch_body.sh

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@
1111
##
1212

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

1618
payload_sha1=%%IMAGE_SHA1%%
1719

@@ -45,7 +47,9 @@ if [ "$(id -u)" = "0" ] ; then
4547
fi
4648
cd $tmp_dir
4749
echo -n "Preparing image archive ..."
48-
sed -e '1,/^exit_marker$/d' $archive_path | tar xf - || exit 1
50+
51+
sed -e '1,/^exit_marker$/d' $archive_path | head -c $payload_image_size | tar xf - || clean_up 1
52+
4953
echo " OK."
5054
cd $cur_wd
5155
if [ -n "$extract" ] ; then

onie-mk-demo.sh

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ output_file=$6
1414
demo_type=$7
1515
image_version=$8
1616
onie_image_part_size=$9
17+
cert_file=${11}
18+
key_file=${12}
1719

1820
shift 9
1921

@@ -130,7 +132,46 @@ cp $installer_dir/sharch_body.sh $output_file || {
130132
# Replace variables in the sharch template
131133
sed -i -e "s/%%IMAGE_SHA1%%/$sha1/" $output_file
132134
echo -n "."
135+
tar_size="$(wc -c < "${sharch}")"
133136
cat $sharch >> $output_file
137+
sed -i -e "s|%%PAYLOAD_IMAGE_SIZE%%|${tar_size}|" ${output_file}
138+
echo "secure upgrade flags: SECURE_UPGRADE_MODE = $SECURE_UPGRADE_MODE, \
139+
SECURE_UPGRADE_DEV_SIGNING_KEY = $SECURE_UPGRADE_DEV_SIGNING_KEY, SECURE_UPGRADE_DEV_SIGNING_CERT = $SECURE_UPGRADE_DEV_SIGNING_CERT"
140+
141+
if [ "$SECURE_UPGRADE_MODE" = "dev" -o "$SECURE_UPGRADE_MODE" = "prod" ]; then
142+
CMS_SIG="${tmp_dir}/signature.sig"
143+
144+
echo "$0 Creating CMS signature for ${output_file} with ${key_file}. Output file ${CMS_SIG}"
145+
DIR="$(dirname "$0")"
146+
147+
scripts_dir="${DIR}/scripts"
148+
if [ "$SECURE_UPGRADE_MODE" = "dev" ]; then
149+
. ${scripts_dir}/sign_image_dev.sh
150+
sign_image_dev ${cert_file} ${key_file} ${output_file} ${CMS_SIG} || {
151+
echo "CMS sign error $?"
152+
sudo rm -rf ${CMS_SIG}
153+
clean_up 1
154+
}
155+
else # "$SECURE_UPGRADE_MODE" has to be equal to "prod"
156+
. ${scripts_dir}/sign_image_${platform}.sh
157+
sign_image_prod ${output_file} ${CMS_SIG} || {
158+
echo "CMS sign error $?"
159+
sudo rm -rf ${CMS_SIG}
160+
clean_up 1
161+
}
162+
fi
163+
164+
[ -f "$CMS_SIG" ] || {
165+
echo "Error: CMS signature not created - exiting without signing"
166+
clean_up 1
167+
}
168+
# append signature to binary
169+
cat ${CMS_SIG} >> ${output_file}
170+
sudo rm -rf ${CMS_SIG}
171+
elif [ "$SECURE_UPGRADE_MODE" -ne "no_sign" ]; then
172+
echo "SECURE_UPGRADE_MODE not defined or defined as $SECURE_UPGRADE_MODE - build without signing"
173+
fi
174+
134175
rm -rf $tmp_dir
135176
echo " Done."
136177

rules/config

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,14 @@ SONIC_ENABLE_IMAGE_SIGNATURE ?= n
208208
# The absolute path should be provided.
209209
SONIC_ENABLE_SECUREBOOT_SIGNATURE ?= n
210210

211+
# folloing flags are used for image secure upgrade verification:
212+
# SECURE_UPGRADE_DEV_SIGNING_KEY - path to development signing key, used for image signing during build
213+
# SECURE_UPGRADE_DEV_SIGNING_CERT - path to development signing certificate, used for image signing during build
214+
# SECURE_UPGRADE_MODE - enum value for secure upgrade mode, valid options are "dev", "prod" and "no_sign"
215+
#SECURE_UPGRADE_DEV_SIGNING_KEY =
216+
#SECURE_UPGRADE_DEV_SIGNING_CERT =
217+
SECURE_UPGRADE_MODE = "no_sign"
218+
211219
# PACKAGE_URL_PREFIX - the package url prefix
212220
PACKAGE_URL_PREFIX ?= https://packages.trafficmanager.net/public/packages
213221

scripts/sign_image_dev.sh

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
sign_image_dev()
2+
{
3+
cert_file=$1
4+
key_file=$2
5+
image_to_sign=$3
6+
cms_sig_out=$4
7+
openssl cms -sign -nosmimecap -signer ${cert_file} -inkey ${key_file} -binary -in $image_to_sign -outform pem -out ${cms_sig_out} || {
8+
echo "$?: CMS sign error"
9+
sudo rm -rf ${cms_sig_out}
10+
exit 1
11+
}
12+
echo "CMS sign OK"
13+
return 0
14+
}

slave.mk

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -348,6 +348,9 @@ $(info "USE_NATIVE_DOCKERD_FOR_BUILD" : "$(SONIC_CONFIG_USE_NATIVE_DOCKERD_FO
348348
$(info "SONIC_USE_DOCKER_BUILDKIT" : "$(SONIC_USE_DOCKER_BUILDKIT)")
349349
$(info "USERNAME" : "$(USERNAME)")
350350
$(info "PASSWORD" : "$(PASSWORD)")
351+
$(info "SECURE_UPGRADE_MODE" : "$(SECURE_UPGRADE_MODE)")
352+
$(info "SECURE_UPGRADE_DEV_SIGNING_KEY" : "$(SECURE_UPGRADE_DEV_SIGNING_KEY)")
353+
$(info "SECURE_UPGRADE_DEV_SIGNING_CERT" : "$(SECURE_UPGRADE_DEV_SIGNING_CERT)")
351354
$(info "ENABLE_DHCP_GRAPH_SERVICE" : "$(ENABLE_DHCP_GRAPH_SERVICE)")
352355
$(info "SHUTDOWN_BGP_ON_START" : "$(SHUTDOWN_BGP_ON_START)")
353356
$(info "ENABLE_PFCWD_ON_START" : "$(ENABLE_PFCWD_ON_START)")
@@ -1174,6 +1177,9 @@ $(addprefix $(TARGET_PATH)/, $(SONIC_INSTALLERS)) : $(TARGET_PATH)/% : \
11741177
export enable_organization_extensions="$(ENABLE_ORGANIZATION_EXTENSIONS)"
11751178
export enable_dhcp_graph_service="$(ENABLE_DHCP_GRAPH_SERVICE)"
11761179
export enable_ztp="$(ENABLE_ZTP)"
1180+
export sonic_su_dev_signing_key="$(SECURE_UPGRADE_DEV_SIGNING_KEY)"
1181+
export sonic_su_dev_signing_cert="$(SECURE_UPGRADE_DEV_SIGNING_CERT)"
1182+
export sonic_su_mode="$(SECURE_UPGRADE_MODE)"
11771183
export include_system_telemetry="$(INCLUDE_SYSTEM_TELEMETRY)"
11781184
export include_restapi="$(INCLUDE_RESTAPI)"
11791185
export include_nat="$(INCLUDE_NAT)"
@@ -1373,6 +1379,9 @@ $(addprefix $(TARGET_PATH)/, $(SONIC_INSTALLERS)) : $(TARGET_PATH)/% : \
13731379
TARGET_MACHINE=$(dep_machine) \
13741380
IMAGE_TYPE=$($*_IMAGE_TYPE) \
13751381
SONIC_ENABLE_IMAGE_SIGNATURE="$(SONIC_ENABLE_IMAGE_SIGNATURE)" \
1382+
SECURE_UPGRADE_MODE="$(SECURE_UPGRADE_MODE)" \
1383+
SECURE_UPGRADE_DEV_SIGNING_KEY="$(SECURE_UPGRADE_DEV_SIGNING_KEY)" \
1384+
SECURE_UPGRADE_DEV_SIGNING_CERT="$(SECURE_UPGRADE_DEV_SIGNING_CERT)" \
13761385
SIGNING_KEY="$(SIGNING_KEY)" \
13771386
SIGNING_CERT="$(SIGNING_CERT)" \
13781387
CA_CERT="$(CA_CERT)" \

0 commit comments

Comments
 (0)