-
Notifications
You must be signed in to change notification settings - Fork 39
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 script to wrap filesystem image in DDI #24
Open
jepio
wants to merge
2
commits into
main
Choose a base branch
from
dm-verity
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+123
−5
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,6 +6,13 @@ FORMAT="${FORMAT:-squashfs}" | |
ARCH="${ARCH-}" | ||
SOURCE_DATE_EPOCH="${SOURCE_DATE_EPOCH-0}" | ||
export SOURCE_DATE_EPOCH | ||
KEY="${KEY-}" | ||
CERT="${CERT-}" | ||
|
||
die() { | ||
echo >&2 "$@" | ||
exit 1 | ||
} | ||
|
||
# This script is to be called as helper by other scripts but can also be used standalone | ||
if [ $# -lt 1 ]; then | ||
|
@@ -22,12 +29,20 @@ elif [ "$1" = "-h" ] || [ "$1" = "--help" ]; then | |
exit 1 | ||
fi | ||
|
||
if [ "${FORMAT}" = "verity" ]; then | ||
[ -z "${KEY}" ] && die "\$KEY required for verity" | ||
[ -z "${CERT}" ] && die "\$CERT required for verity" | ||
fi | ||
|
||
SYSEXTNAME="$1" | ||
|
||
if [ "${FORMAT}" != "squashfs" ] && [ "${FORMAT}" != "btrfs" ] && [ "${FORMAT}" != "ext4" ] && [ "${FORMAT}" != "ext2" ]; then | ||
echo "Expected FORMAT=squashfs, FORMAT=btrfs, FORMAT=ext4, or FORMAT=ext2, got '${FORMAT}'" >&2 | ||
exit 1 | ||
fi | ||
case ${FORMAT} in | ||
squashfs) ;; | ||
btrfs) ;; | ||
ext4|ext2) ;; | ||
verity) ;; | ||
*) die "Unsupported format: '${FORMAT}'" ;; | ||
esac | ||
|
||
# Map to valid values for https://www.freedesktop.org/software/systemd/man/os-release.html#ARCHITECTURE= | ||
if [ "${ARCH}" = "amd64" ] || [ "${ARCH}" = "x86_64" ]; then | ||
|
@@ -57,7 +72,11 @@ elif [ "${FORMAT}" = "ext4" ] || [ "${FORMAT}" = "ext2" ]; then | |
# Note: We didn't chown to root:root, meaning that the file ownership is left as is | ||
mkfs."${FORMAT}" -E root_owner=0:0 -d "${SYSEXTNAME}" "${SYSEXTNAME}".raw | ||
resize2fs -M "${SYSEXTNAME}".raw | ||
else | ||
elif [ "${FORMAT}" = "squashfs" ]; then | ||
mksquashfs "${SYSEXTNAME}" "${SYSEXTNAME}".raw -all-root | ||
elif [ "${FORMAT}" = "verity" ]; then | ||
systemd-repart --private-key="${KEY}" --certificate="${CERT}" --root="${SYSEXTNAME}" --no-pager --empty=create --size=auto --definitions=repart.d "${SYSEXTNAME}.raw" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This assumes to be running in the script's folder, we need to add a |
||
else | ||
die "Unsupported format: ${FORMAT}" | ||
fi | ||
echo "Created ${SYSEXTNAME}.raw" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
[Partition] | ||
Type=root | ||
CopyFiles=/:/ | ||
Format=squashfs | ||
Minimize=best | ||
Verity=data | ||
VerityMatchKey=sysext |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
[Partition] | ||
Type=root-verity | ||
Verity=hash | ||
VerityMatchKey=sysext | ||
# Only works from v255 | ||
Minimize=best | ||
SizeMinBytes=4K |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
[Partition] | ||
Type=root-verity-sig | ||
Verity=signature | ||
VerityMatchKey=sysext |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
#!/bin/bash | ||
|
||
set -e | ||
|
||
data=$1 | ||
|
||
verity_hdr=$(veritysetup format $data ${data}.verity) | ||
root_hash=$(echo $(awk -F: '/Root hash:/ { print $2 }' <<<$verity_hdr)) | ||
|
||
data_size=$(stat -c %s $data) | ||
hash_size=$(stat -c %s ${data}.verity) | ||
root_hash_front=${root_hash:0:32} | ||
root_hash_back=${root_hash:32:32} | ||
|
||
# 512-byte sectors | ||
data_sectors=$(( $data_size >> 9 )) | ||
hash_sectors=$(( $hash_size >> 9 )) | ||
data_type=4f68bce3-e8cd-4db1-96e7-fbcaf984b709 | ||
hash_type=2c7357ed-ebd2-46d9-aec1-23d437ec2bf5 | ||
|
||
openssl req -batch -new -x509 -sha256 -newkey rsa:2048 -nodes -out root_key.crt -keyout root_key.pem -days 3650 | ||
echo -n "$root_hash" >${data}.roothash | ||
openssl smime -sign -nocerts -noattr -binary -in "${data}.roothash" -inkey "root_key.pem" -signer "root_key.crt" -outform der -out "${data}.roothash.p7s" | ||
|
||
cat <<EOF | tr -d '\n' >${data}.verity.sig | ||
{"rootHash":"$root_hash","signature":"$(base64 -w 0 <${data}.roothash.p7s)"} | ||
EOF | ||
sig_size=$(stat -c %s ${data}.verity.sig) | ||
# rounded up to 4096 bytes | ||
sig_size=$(( ( $sig_size + 4095 ) / 4096 * 4096 )) | ||
sig_sectors=$(( $sig_size >> 9 )) | ||
sig_type=41092b05-9fc8-4523-994f-2def0408b176 | ||
|
||
# signature + GPT header + PMBR (?) | ||
disk_size=$(( $data_size + $hash_size + 4096 + 2048 * 512 + 33 * 512)) | ||
rm -f disk.img | ||
fallocate -l $disk_size disk.img | ||
|
||
as_hex() { | ||
str=$1 | ||
printf "%.8s-%.4s-%.4s-%.4s-%.12s" \ | ||
"${str:0:8}" "${str:8:4}" "${str:12:4}" \ | ||
"${str:16:4}" "${str:20:12}" | ||
} | ||
data_uuid=$(as_hex $root_hash_front) | ||
hash_uuid=$(as_hex $root_hash_back) | ||
|
||
cat <<EOF >sda.sfdisk | ||
label: gpt | ||
unit: sectors | ||
sector-size: 512 | ||
|
||
/dev/sda1 : start=2048, size= ${data_sectors}, type=${data_type}, uuid=${data_uuid} | ||
/dev/sda2 : start=$(( $data_sectors + 2048 )), size= ${hash_sectors}, type=${hash_type}, uuid=${hash_uuid} | ||
/dev/sda3 : start=$(( ${hash_sectors} + $data_sectors + 2048 )), size= ${sig_sectors}, type=${sig_type} | ||
EOF | ||
|
||
sfdisk disk.img <sda.sfdisk | ||
|
||
loop=$(sudo losetup --find --show disk.img) | ||
sudo partx -u $loop | ||
sudo dd bs=512 if=$data of=${loop}p1 | ||
sudo dd bs=512 if=${data}.verity of=${loop}p2 | ||
sudo dd bs=512 if=${data}.verity.sig of=${loop}p3 | ||
sudo losetup -d $loop | ||
|
||
echo | ||
echo "Finished!" | ||
echo "Copy disk.img to /etc/extensions/$(basename $data)" | ||
echo "Copy root_key.crt to /etc/verity.d/root_key.crt" |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A sentence on how these can be loaded would be good.