Skip to content

Commit 0059a33

Browse files
authored
Merge pull request #2491 from flatcar/chewi/split-sbsign-job
Do Secure Boot signing for official builds in a separate additional job
2 parents c90a73a + e6e3daf commit 0059a33

File tree

12 files changed

+303
-313
lines changed

12 files changed

+303
-313
lines changed

build_image

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,10 @@ DEFINE_string disk_layout "" \
4141
"The disk layout type to use for this image."
4242
DEFINE_string group "${DEFAULT_GROUP}" \
4343
"The update group."
44-
DEFINE_boolean generate_update "${FLAGS_FALSE}" \
45-
"Generate update payload. (prod only)"
4644
DEFINE_boolean extract_update "${FLAGS_TRUE}" \
47-
"Extract the /usr partition for generating updates."
45+
"Extract the /usr partition for generating updates. Only valid for the prod image."
46+
DEFINE_boolean generate_update "${FLAGS_TRUE}" \
47+
"Generate update payload for testing. The update is signed with a dev key. The kernel is signed with a dev key (unofficial builds) or not at all (official builds). Only valid for the prod image. Implies --extract_update."
4848
DEFINE_string developer_data "" \
4949
"Insert a custom cloudinit file into the image."
5050
DEFINE_string devcontainer_binhost "${DEFAULT_DEVCONTAINER_BINHOST}" \
@@ -139,6 +139,11 @@ fi
139139
# Create the output directory and temporary mount points.
140140
mkdir -p "${BUILD_DIR}"
141141

142+
# --generate_update implies --extract_update.
143+
if [[ ${FLAGS_generate_update} -eq ${FLAGS_TRUE} ]]; then
144+
FLAGS_extract_update=${FLAGS_TRUE}
145+
fi
146+
142147
DISK_LAYOUT="${FLAGS_disk_layout:-base}"
143148
CONTAINER_LAYOUT="${FLAGS_disk_layout:-container}"
144149

@@ -169,11 +174,12 @@ fi
169174
if [[ "${PROD_IMAGE}" -eq 1 ]]; then
170175
IMAGE_BUILD_TYPE="prod"
171176
create_prod_image ${FLATCAR_PRODUCTION_IMAGE_NAME} ${DISK_LAYOUT} ${FLAGS_group} ${FLAGS_base_pkg} ${FLAGS_base_sysexts}
172-
if [[ ${FLAGS_generate_update} -eq ${FLAGS_TRUE} ]]; then
173-
generate_update "${FLATCAR_PRODUCTION_IMAGE_NAME}" ${DISK_LAYOUT}
174-
elif [[ ${FLAGS_extract_update} -eq ${FLAGS_TRUE} ]]; then
177+
if [[ ${FLAGS_extract_update} -eq ${FLAGS_TRUE} ]]; then
175178
extract_update "${FLATCAR_PRODUCTION_IMAGE_NAME}" "${DISK_LAYOUT}"
176179
fi
180+
if [[ ${FLAGS_generate_update} -eq ${FLAGS_TRUE} ]]; then
181+
generate_update "${FLATCAR_PRODUCTION_IMAGE_NAME}" "${DISK_LAYOUT}"
182+
fi
177183
if [[ "${PROD_TAR}" -eq 1 ]]; then
178184
create_prod_tar ${FLATCAR_PRODUCTION_IMAGE_NAME}
179185
fi
@@ -182,9 +188,7 @@ if [[ "${SYSEXT}" -eq 1 ]]; then
182188
create_prod_sysexts "${FLATCAR_PRODUCTION_IMAGE_NAME}"
183189
fi
184190

185-
if [[ ${FLAGS_generate_update} -eq ${FLAGS_TRUE} ]] || \
186-
[[ ${FLAGS_extract_update} -eq ${FLAGS_TRUE} ]]
187-
then
191+
if [[ ${FLAGS_extract_update} -eq ${FLAGS_TRUE} ]]; then
188192
zip_update_tools
189193
fi
190194

build_library/build_image_util.sh

Lines changed: 89 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -61,57 +61,43 @@ delete_prompt() {
6161
extract_update() {
6262
local image_name="$1"
6363
local disk_layout="$2"
64-
local update_path="${BUILD_DIR}/${image_name%_image.bin}_update.bin"
64+
local update="${BUILD_DIR}/${image_name%_image.bin}_update.bin"
6565

6666
"${BUILD_LIBRARY_DIR}/disk_util" --disk_layout="${disk_layout}" \
67-
extract "${BUILD_DIR}/${image_name}" "USR-A" "${update_path}"
67+
extract "${BUILD_DIR}/${image_name}" "USR-A" "${update}"
6868

6969
# Compress image
70-
files_to_evaluate+=( "${update_path}" )
70+
files_to_evaluate+=( "${update}" )
7171
compress_disk_images files_to_evaluate
72-
73-
# For production as well as dev builds we generate a dev-key-signed update
74-
# payload for running tests (the signature won't be accepted by production systems).
75-
local update_test="${BUILD_DIR}/flatcar_test_update.gz"
76-
delta_generator \
77-
-private_key "/usr/share/update_engine/update-payload-key.key.pem" \
78-
-new_image "${update_path}" \
79-
-new_kernel "${BUILD_DIR}/${image_name%.bin}.vmlinuz" \
80-
-out_file "${update_test}"
81-
}
82-
83-
zip_update_tools() {
84-
# There isn't a 'dev' variant of this zip, so always call it production.
85-
local update_zip="flatcar_production_update.zip"
86-
87-
info "Generating update tools zip"
88-
# Make sure some vars this script needs are exported
89-
export REPO_MANIFESTS_DIR SCRIPTS_DIR
90-
"${BUILD_LIBRARY_DIR}/generate_au_zip.py" \
91-
--arch "$(get_sdk_arch)" --output-dir "${BUILD_DIR}" --zip-name "${update_zip}"
9272
}
9373

9474
generate_update() {
9575
local image_name="$1"
9676
local disk_layout="$2"
9777
local image_kernel="${BUILD_DIR}/${image_name%.bin}.vmlinuz"
98-
local update_prefix="${image_name%_image.bin}_update"
99-
local update="${BUILD_DIR}/${update_prefix}"
78+
local update="${BUILD_DIR}/${image_name%_image.bin}_update.bin"
10079
local devkey="/usr/share/update_engine/update-payload-key.key.pem"
10180

81+
# Extract the partition if it isn't extracted already.
82+
[[ -s ${update} ]] || extract_update "${image_name}" "${disk_layout}"
83+
10284
echo "Generating update payload, signed with a dev key"
103-
"${BUILD_LIBRARY_DIR}/disk_util" --disk_layout="${disk_layout}" \
104-
extract "${BUILD_DIR}/${image_name}" "USR-A" "${update}.bin"
10585
delta_generator \
10686
-private_key "${devkey}" \
107-
-new_image "${update}.bin" \
87+
-new_image "${update}" \
10888
-new_kernel "${image_kernel}" \
109-
-out_file "${update}.gz"
89+
-out_file "${BUILD_DIR}/flatcar_test_update.gz"
90+
}
11091

111-
# Compress image
112-
declare -a files_to_evaluate
113-
files_to_evaluate+=( "${update}.bin" )
114-
compress_disk_images files_to_evaluate
92+
zip_update_tools() {
93+
# There isn't a 'dev' variant of this zip, so always call it production.
94+
local update_zip="flatcar_production_update.zip"
95+
96+
info "Generating update tools zip"
97+
# Make sure some vars this script needs are exported
98+
local -x REPO_MANIFESTS_DIR SCRIPTS_DIR
99+
"${BUILD_LIBRARY_DIR}/generate_au_zip.py" \
100+
--arch "$(get_sdk_arch)" --output-dir "${BUILD_DIR}" --zip-name "${update_zip}"
115101
}
116102

117103
# ldconfig cannot generate caches for non-native arches.
@@ -805,10 +791,12 @@ EOF
805791
seek=${verity_offset} count=64 bs=1 status=none
806792
fi
807793

808-
# Sign the kernel after /usr is in a consistent state and verity is calculated
809-
[[ ${COREOS_OFFICIAL:-0} -ne 1 ]] && \
810-
do_sbsign --output "${root_fs_dir}/boot/flatcar/vmlinuz-a"{,}
811-
cleanup_sbsign_certs
794+
# Sign the kernel after /usr is in a consistent state and verity is
795+
# calculated. Only for unofficial builds as official builds get signed later.
796+
if [[ ${COREOS_OFFICIAL:-0} -ne 1 ]]; then
797+
do_sbsign --output "${root_fs_dir}/boot/flatcar/vmlinuz-a"{,}
798+
cleanup_sbsign_certs
799+
fi
812800

813801
if [[ -n "${image_kernel}" ]]; then
814802
# copying kernel from vfat so ignore the permissions
@@ -894,3 +882,67 @@ EOF
894882
cleanup_mounts "${root_fs_dir}"
895883
trap - EXIT
896884
}
885+
886+
sbsign_image() {
887+
local image_name="$1"
888+
local disk_layout="$2"
889+
local root_fs_dir="$3"
890+
local image_kernel="$4"
891+
local pcr_policy="$5"
892+
local image_grub="$6"
893+
894+
local disk_img="${BUILD_DIR}/${image_name}"
895+
local EFI_ARCH
896+
897+
case "${BOARD}" in
898+
amd64-usr) EFI_ARCH="x64" ;;
899+
arm64-usr) EFI_ARCH="aa64" ;;
900+
*) die "Unknown board ${BOARD@Q}" ;;
901+
esac
902+
903+
"${BUILD_LIBRARY_DIR}/disk_util" --disk_layout="${disk_layout}" \
904+
mount "${disk_img}" "${root_fs_dir}"
905+
trap "cleanup_mounts '${root_fs_dir}'; cleanup_sbsign_certs" EXIT
906+
907+
# Sign the kernel with the shim-embedded key.
908+
do_sbsign --output "${root_fs_dir}/boot/flatcar/vmlinuz-a"{,}
909+
910+
if [[ -n "${image_kernel}" ]]; then
911+
# copying kernel from vfat so ignore the permissions
912+
cp --no-preserve=mode \
913+
"${root_fs_dir}/boot/flatcar/vmlinuz-a" \
914+
"${BUILD_DIR}/${image_kernel}"
915+
fi
916+
917+
# Sign GRUB and mokmanager(mm) with the shim-embedded key.
918+
do_sbsign --output "${root_fs_dir}/boot/EFI/boot/grub${EFI_ARCH}.efi"{,}
919+
do_sbsign --output "${root_fs_dir}/boot/EFI/boot/mm${EFI_ARCH}.efi"{,}
920+
921+
# copying from vfat so ignore permissions
922+
if [[ -n "${image_grub}" ]]; then
923+
cp --no-preserve=mode "${root_fs_dir}/boot/EFI/boot/grub${EFI_ARCH}.efi" \
924+
"${BUILD_DIR}/${image_grub}"
925+
fi
926+
927+
if [[ -n "${pcr_policy}" ]]; then
928+
mkdir -p "${BUILD_DIR}/pcrs"
929+
"${BUILD_LIBRARY_DIR}"/generate_kernel_hash.py \
930+
"${root_fs_dir}/boot/flatcar/vmlinuz-a" "${FLATCAR_VERSION}" \
931+
>"${BUILD_DIR}/pcrs/kernel.config"
932+
fi
933+
934+
cleanup_mounts "${root_fs_dir}"
935+
cleanup_sbsign_certs
936+
trap - EXIT
937+
938+
if [[ -n "${pcr_policy}" ]]; then
939+
"${BUILD_LIBRARY_DIR}"/generate_grub_hashes.py \
940+
"${disk_img}" /usr/lib/grub/ "${BUILD_DIR}/pcrs" "${FLATCAR_VERSION}"
941+
942+
info "Generating $pcr_policy"
943+
pushd "${BUILD_DIR}" >/dev/null
944+
zip --quiet -r -9 "${BUILD_DIR}/${pcr_policy}" pcrs
945+
popd >/dev/null
946+
rm -rf "${BUILD_DIR}/pcrs"
947+
fi
948+
}

build_library/grub_install.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ case "${FLAGS_target}" in
205205
--output "${ESP_DIR}/EFI/boot/boot${EFI_ARCH}.efi" \
206206
"${BOARD_ROOT}/usr/lib/shim/shim${EFI_ARCH}.efi"
207207
else
208-
# Official build: Copy the unsigned files.
208+
# Official build: Copy signed shim and mm for signing later.
209209
sudo cp "${BOARD_ROOT}/usr/lib/shim/mm${EFI_ARCH}.efi" \
210210
"${ESP_DIR}/EFI/boot/mm${EFI_ARCH}.efi"
211211
sudo cp "${BOARD_ROOT}/usr/lib/shim/shim${EFI_ARCH}.efi" \

build_library/modify_image_util.sh

Lines changed: 0 additions & 100 deletions
This file was deleted.

build_library/prod_image_util.sh

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,15 @@ EOF
180180
"${image_initrd_contents_wtd}" \
181181
"${image_disk_usage}"
182182

183+
# Official builds will sign and upload these files later, so remove them to
184+
# prevent them from being uploaded now.
185+
if [[ ${COREOS_OFFICIAL:-0} -eq 1 ]]; then
186+
rm -v \
187+
"${BUILD_DIR}/${image_kernel}" \
188+
"${BUILD_DIR}/${image_pcr_policy}" \
189+
"${BUILD_DIR}/${image_grub}"
190+
fi
191+
183192
local files_to_evaluate=( "${BUILD_DIR}/${image_name}" )
184193
compress_disk_images files_to_evaluate
185194
}
@@ -225,3 +234,26 @@ create_prod_sysexts() {
225234
-out_file "${BUILD_DIR}/flatcar_test_update-${name}.gz"
226235
done
227236
}
237+
238+
sbsign_prod_image() {
239+
local image_name="$1"
240+
local disk_layout="$2"
241+
242+
info "Signing production image ${image_name} for Secure Boot"
243+
local root_fs_dir="${BUILD_DIR}/rootfs"
244+
local image_prefix="${image_name%.bin}"
245+
local image_kernel="${image_prefix}.vmlinuz"
246+
local image_pcr_policy="${image_prefix}_pcr_policy.zip"
247+
local image_grub="${image_prefix}.grub"
248+
249+
sbsign_image \
250+
"${image_name}" \
251+
"${disk_layout}" \
252+
"${root_fs_dir}" \
253+
"${image_kernel}" \
254+
"${image_pcr_policy}" \
255+
"${image_grub}"
256+
257+
local files_to_evaluate=( "${BUILD_DIR}/${image_name}" )
258+
compress_disk_images files_to_evaluate
259+
}

0 commit comments

Comments
 (0)