All of lore.kernel.org
 help / color / mirror / Atom feed
From: Gylstorff Quirin <quirin.gylstorff@siemens.com>
To: Jan Kiszka <jan.kiszka@siemens.com>,
	cip-dev@lists.cip-project.org, christian.storm@siemens.com
Subject: Re: [cip-dev][isar-cip-core][RFC v2 4/7] Add initramfs hook to encrypt a partition
Date: Fri, 17 Feb 2023 16:06:31 +0100	[thread overview]
Message-ID: <00766d63-d1cc-c792-c5bd-8c7d4a3778d4@siemens.com> (raw)
In-Reply-To: <2012e77d-1892-1797-3161-b834ccebda86@siemens.com>

[-- Attachment #1: Type: text/plain, Size: 11626 bytes --]



On 2/17/23 15:53, Jan Kiszka wrote:
> On 17.02.23 14:05, Quirin Gylstorff wrote:
>> From: Quirin Gylstorff <quirin.gylstorff@siemens.com>
>>
>> This creates a new luks encrypted ext4 partition with a the
>> key stored in the tpm2.
>>
>> The initial key is randomly generated and removed from the
>> LUKS partition. Therefore a new key cannot be added by the user
>> and if the LUKS header is corrupted the data is no longer readable.
>>
>> Signed-off-by: Quirin Gylstorff <quirin.gylstorff@siemens.com>
>> ---
>>   .../files/encrypt_partition.env.tmpl          |   2 +
>>   .../files/encrypt_partition.hook              |  49 +++++++
>>   .../files/encrypt_partition.script            | 135 ++++++++++++++++++
>>   .../initramfs-crypt-hook_0.1.bb               |  37 +++++
>>   wic/x86-efibootguard.wks.in                   |   4 +-
>>   5 files changed, 225 insertions(+), 2 deletions(-)
>>   create mode 100644 recipes-initramfs/initramfs-crypt-hook/files/encrypt_partition.env.tmpl
>>   create mode 100644 recipes-initramfs/initramfs-crypt-hook/files/encrypt_partition.hook
>>   create mode 100644 recipes-initramfs/initramfs-crypt-hook/files/encrypt_partition.script
>>   create mode 100644 recipes-initramfs/initramfs-crypt-hook/initramfs-crypt-hook_0.1.bb
>>
>> diff --git a/recipes-initramfs/initramfs-crypt-hook/files/encrypt_partition.env.tmpl b/recipes-initramfs/initramfs-crypt-hook/files/encrypt_partition.env.tmpl
>> new file mode 100644
>> index 0000000..943fe4b
>> --- /dev/null
>> +++ b/recipes-initramfs/initramfs-crypt-hook/files/encrypt_partition.env.tmpl
>> @@ -0,0 +1,2 @@
>> +PARTITIONS="${CRYPT_PARTITIONS}"
>> +REDUDUCE_DEVICE_SIZE="${CRYPT_REDUDUCE_DEVICE_SIZE}"
> 
> Did you mean "REDUCE"?
That was my intenion will fix it in v3.
> 
> And what is this variable for?

If you use online reencryption on a populated partition you need to 
create the space for the LUKS header. The necessary minimum size is 32MB.
As an alternative you can detach the LUKS header.


> 
>> diff --git a/recipes-initramfs/initramfs-crypt-hook/files/encrypt_partition.hook b/recipes-initramfs/initramfs-crypt-hook/files/encrypt_partition.hook
>> new file mode 100644
>> index 0000000..96e084a
>> --- /dev/null
>> +++ b/recipes-initramfs/initramfs-crypt-hook/files/encrypt_partition.hook
>> @@ -0,0 +1,49 @@
>> +#!/bin/sh
>> +# Copyright (C) Siemens AG, 2020-2022
>> +#
>> +# SPDX-License-Identifier: MIT
>> +
>> +PREREQ=""
>> +
>> +prereqs()
>> +{
>> +     echo "$PREREQ"
>> +}
>> +
>> +case $1 in
>> +prereqs)
>> +     prereqs
>> +     exit 0
>> +     ;;
>> +esac
>> +
>> +. /usr/share/initramfs-tools/scripts/functions
>> +. /usr/share/initramfs-tools/hook-functions
>> +
>> +manual_add_modules tpm
>> +manual_add_modules tpm_tis_core
>> +manual_add_modules tpm_tis
>> +manual_add_modules tpm_crb
>> +manual_add_modules dm_mod
>> +manual_add_modules dm_crypt
>> +
>> +copy_exec /usr/bin/openssl
>> +copy_exec /usr/sbin/mke2fs
>> +copy_exec /usr/bin/grep
>> +copy_exec /usr/bin/awk
>> +copy_exec /usr/bin/expr
>> +copy_exec /usr/sbin/e2fsck
>> +copy_exec /usr/sbin/resize2fs
>> +copy_exec /usr/sbin/cryptsetup
>> +copy_exec /usr/bin/systemd-cryptenroll
>> +copy_exec /usr/lib/systemd/systemd-cryptsetup
>> +
>> +if [ -x cryptsetup-reencrypt ]; then
>> +    copy_exec /usr/sbin/cryptsetup-reencrypt
>> +fi
>> +
>> +for _LIBRARY in /usr/lib/*/libtss2*; do
>> +    copy_exec "$_LIBRARY"
>> +done
>> +
>> +copy_file library /usr/share/encrypt_partition/encrypt_partition.env /usr/share/encrypt_partition/encrypt_partition.env
>> diff --git a/recipes-initramfs/initramfs-crypt-hook/files/encrypt_partition.script b/recipes-initramfs/initramfs-crypt-hook/files/encrypt_partition.script
>> new file mode 100644
>> index 0000000..e58794e
>> --- /dev/null
>> +++ b/recipes-initramfs/initramfs-crypt-hook/files/encrypt_partition.script
>> @@ -0,0 +1,135 @@
>> +#!/bin/sh
>> +#
>> +# CIP Core, generic profile
>> +#
>> +# Copyright (c) Siemens AG, 2023
>> +#
>> +# Authors:
>> +#  Quirin Gylstorff <quirin.gylstorff@siemens.com>
>> +#
>> +# SPDX-License-Identifier: MIT
>> +prereqs()
>> +{
>> +	# Make sure that this script is run last in local-top
>> +	local req
>> +	for req in "${0%/*}"/*; do
>> +		script="${req##*/}"
>> +		if [ "$script" != "${0##*/}" ]; then
>> +			printf '%s\n' "$script"
>> +		fi
>> +	done
>> +}
>> +case $1 in
>> +prereqs)
>> +	prereqs
>> +	exit 0
>> +	;;
>> +esac
>> +
>> +. /scripts/functions
>> +
>> +# get configuration variables
>> +. /usr/share/encrypt_partition/encrypt_partition.env
>> +
>> +# load necessary kernel modules:
>> +modprobe tpm_tis
>> +modprobe tpm_crb
>> +
>> +# fixed tpm device or do we need to find it
>> +tpm_device=/dev/tpmrm0
>> +partition_sets="$PARTITIONS"
>> +
>> +open_tpm2_partition() {
>> +	if ! /usr/lib/systemd/systemd-cryptsetup attach "$crypt_mount_name" \
>> +		 "$1" - tpm2-device="$tpm_device"; then
>> +		panic "Can't decrypt '$1' !"
>> +	fi
>> +}
>> +
>> +enroll_tpm2_token() {
>> +	#check systemd version and export password if necessary
>> +	if [ -x /usr/bin/systemd-cryptenroll ]; then
> 
> And if that file does not exist? No error? No bail-out?
>

I will remove that check and move it to the hook and fail during build 
time. Is that better?

>> +		systemd_version=$(systemd-cryptenroll --version | \
>> +							  awk -F " " 'NR==1{print $2 }')
>> +		#check systemd version and export password if necessary
>> +		if [ "$systemd_version" -ge "251" ]; then
>> +			PASSWORD=$(cat "$2" )
>> +			export PASSWORD
>> +			/usr/bin/systemd-cryptenroll --tpm2-device="$tpm_device" \
>> +										 --tpm2-pcrs=7 "$1"
>> +			PASSWORD=
>> +		else
>> +			panic "Unknown systemd version: '$systemd_version'!"
>> +		fi
>> +	fi
>> +}
>> +
>> +reencrypt_existing_partition() {
>> +	part_device=$(readlink -f "$partition")
>> +	part_size_blocks=$(cat /sys/class/block/"$(awk -v dev=$part_device 'BEGIN{split(dev,a,"/"); print a[3]}' )"/size)
>> +	reduced_size=$(expr $part_size_blocks - 65536 )
>> +	reduced_size_in_byte=$(expr $reduced_size \* 512)
>> +	reduced_size_in_kb=$(expr $reduced_size_in_byte / 1024)K
>> +	resize2fs "$1" "$reduced_size_in_kb"
>> +	if [ -x cryptsetup-reencrypt ]; then
>> +		/usr/sbin/cryptsetup-reencrypt --new --reduce-device-size "${REDUDUCE_DEVICE_SIZE}" $1 < $2
>> +	else
>> +		/usr/sbin/cryptsetup reencrypt --encrypt --reduce-device-size "${REDUDUCE_DEVICE_SIZE}" $1 < $2
>> +	fi
>> +
>> +}
>> +
>> +
> 
> One newline should be enough here.
Will fix in v3.

> 
>> +if [ ! -e "$tpm_device" ]; then
>> +	panic "tpm device '$tpm_device' does not exists - cannot create a encrypted device!"
>> +fi
>> +
>> +for partition_set in $partition_sets; do
>> +	partition_label=$(awk -v var=$partition_set 'BEGIN{split(var,a,":"); print a[1]}')
>> +	partition_mountpoint=$(awk -v var=$partition_set 'BEGIN{split(var,a,":"); print a[2]}')
>> +	partition_format=$(awk -v var=$partition_set 'BEGIN{split(var,a,":"); print a[3]}')
>> +	partition=/dev/disk/by-partlabel/$partition_label
>> +	crypt_mount_name="encrypted_$partition_label"
>> +	decrypted_part=/dev/mapper/"$crypt_mount_name"
>> +
>> +	# check if partition is already encrypted with systemd-tpm2
>> +	if /usr/sbin/cryptsetup luksDump --batch-mode "$partition" \
>> +			| grep -q "systemd-tpm2"; then
>> +		open_tpm2_partition "$partition"
>> +		if ! mount -t $(get_fstype "${decrypted_part}") "${decrypted_part}" "${rootmnt}${partition_mountpoint}"; then
>> +		    panic "Can't mount encrypted partition '${decrypted_part}'!"
>> +		fi
>> +		continue
>> +	fi
>> +
>> +	# create random password for initial encryption
>> +	# this will be dropped after reboot
>> +	tmp_key=/tmp/"$partition_label-lukskey"
>> +	openssl rand -base64 32 > "$tmp_key"
>> +
>> +	case "${partition_format}" in
>> +        "reencrypt")
>> +		    reencrypt_existing_partition "$partition" "$tmp_key"
>> +		    enroll_tpm2_token "$partition" "$tmp_key"
>> +		    open_tpm2_partition "$partition"
>> +        ;;
>> +        "format")
>> +		    /usr/sbin/cryptsetup luksFormat --batch-mode \
>> +							     --type luks2 "$partition" < "$tmp_key"
>> +		    enroll_tpm2_token "$partition" "$tmp_key"
>> +		    open_tpm2_partition_tpm2_partition "$partition"
>> +		    mke2fs -t ext4 "${decrypted_part}"
> 
> We likely want to make this line configurable at recipe-level, both
> regarding fs options as well as the fs type itself.

I will add an additional field to the configuration string.

> 
>> +        ;;
>> +        *)
>> +		    panic "Unknown value ${partition_format}. Cannot create a encrypted partition !"
>> +         ;;
>> +	esac
>> +
>> +	if ! mount -t $(get_fstype "${decrypted_part}") "${decrypted_part}" "${rootmnt}${partition_mountpoint}"; then
>> +		panic "Can't mount encrypted partition '${decrypted_part}'!"
>> +	fi
>> +
>> +	# delete initial key
>> +	# afterwards no new keys can be enrolled
>> +	/usr/bin/systemd-cryptenroll "$partition" --wipe-slot=0
>> +done
>> diff --git a/recipes-initramfs/initramfs-crypt-hook/initramfs-crypt-hook_0.1.bb b/recipes-initramfs/initramfs-crypt-hook/initramfs-crypt-hook_0.1.bb
>> new file mode 100644
>> index 0000000..ba2b884
>> --- /dev/null
>> +++ b/recipes-initramfs/initramfs-crypt-hook/initramfs-crypt-hook_0.1.bb
>> @@ -0,0 +1,37 @@
>> +#
>> +# CIP Core, generic profile
>> +#
>> +# Copyright (c) Siemens AG, 2020-2022
>> +#
>> +# Authors:
>> +#  Quirin Gylstorff <quirin.gylstorff@siemens.com>
>> +#
>> +# SPDX-License-Identifier: MIT
>> +
>> +
>> +inherit dpkg-raw
>> +
>> +DEBIAN_DEPENDS = "initramfs-tools, cryptsetup, systemd(>= 251), \
>> +    awk, openssl, libtss2-esys-3.0.2-0, libtss2-rc0, libtss2-mu0, e2fsprogs"
>> +
>> +SRC_URI += "file://encrypt_partition.hook \
>> +            file://encrypt_partition.script \
>> +    file://encrypt_partition.env.tmpl"
>> +
>> +CRYPT_PARTITIONS ??= "home:/home:reencrypt var:/var:reencrypt"
>> +CRYPT_REDUDUCE_DEVICE_SIZE="32M"
>> +
>> +TEMPLATE_VARS = "CRYPT_PARTITIONS CRYPT_REDUDUCE_DEVICE_SIZE"
>> +TEMPLATE_FILES = "encrypt_partition.env.tmpl"
>> +
>> +do_install[cleandirs] += " \
>> +    ${D}/usr/share/initramfs-tools/hooks \
>> +    ${D}/usr/share/encrypt_partition \
>> +    ${D}/usr/share/initramfs-tools/scripts/local-bottom"
>> +do_install() {
>> +    install -m 0600 "${WORKDIR}/encrypt_partition.env" "${D}/usr/share/encrypt_partition/encrypt_partition.env"
>> +    install -m 0755 "${WORKDIR}/encrypt_partition.script" \
>> +        "${D}/usr/share/initramfs-tools/scripts/local-bottom/encrypt_partition"
>> +    install -m 0755 "${WORKDIR}/encrypt_partition.hook" \
>> +        "${D}/usr/share/initramfs-tools/hooks/encrypt_partition"
>> +}
>> diff --git a/wic/x86-efibootguard.wks.in b/wic/x86-efibootguard.wks.in
>> index b635a8b..1d29583 100644
>> --- a/wic/x86-efibootguard.wks.in
>> +++ b/wic/x86-efibootguard.wks.in
>> @@ -7,7 +7,7 @@ part --source rawcopy --sourceparams "file=${IMAGE_FULLNAME}.squashfs" --align 1
>>   part --source empty --align 1024 --fixed-size 1G --uuid "${ABROOTFS_PART_UUID_B}"
>>   
>>   # home and var are extra partitions
>> -part /home --source rootfs --rootfs-dir=${IMAGE_ROOTFS}/home --fstype=ext4 --label home --align 1024  --size 1G
>> -part /var --source rootfs --rootfs-dir=${IMAGE_ROOTFS}/var --fstype=ext4 --label var --align 1024  --size 2G
>> +part /home --source rootfs --rootfs-dir=${IMAGE_ROOTFS}/home --fstype=ext4 --label home --align 1024  --size 1G --extra-space=100M
>> +part /var --source rootfs --rootfs-dir=${IMAGE_ROOTFS}/var --fstype=ext4 --label var --align 1024  --size 2G --extra-space=100M
>>   
>>   bootloader --ptable gpt --append="console=tty0 console=ttyS0,115200 rootwait earlyprintk"
> 
> Jan
> 
Thanks for the review
Quirin

[-- Attachment #2: S/MIME Cryptographic Signature --]
[-- Type: application/pkcs7-signature, Size: 7613 bytes --]

  reply	other threads:[~2023-02-17 15:07 UTC|newest]

Thread overview: 19+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-02-17 13:05 [cip-dev][isar-cip-core][RFC v2 0/7] Encrypt Partition in initramfs Quirin Gylstorff
2023-02-17 13:05 ` [cip-dev][isar-cip-core][RFC v2 1/7] linux-cip: update kernel configuration for tpm2 support Quirin Gylstorff
2023-02-17 13:05 ` [cip-dev][isar-cip-core][RFC v2 2/7] use bullseye backports for systemd-cryptenroll Quirin Gylstorff
2023-02-18  8:04   ` Moessbauer, Felix
2023-02-17 13:05 ` [cip-dev][isar-cip-core][RFC v2 3/7] start-qemu: If swtpm is available create a tpm2 device Quirin Gylstorff
2023-02-17 14:49   ` Jan Kiszka
2023-02-17 15:11     ` Gylstorff Quirin
2023-02-17 13:05 ` [cip-dev][isar-cip-core][RFC v2 4/7] Add initramfs hook to encrypt a partition Quirin Gylstorff
2023-02-17 14:53   ` Jan Kiszka
2023-02-17 15:06     ` Gylstorff Quirin [this message]
2023-02-17 15:10       ` Jan Kiszka
2023-02-17 15:20         ` Gylstorff Quirin
2023-02-18  8:10   ` Moessbauer, Felix
2023-02-22 16:30     ` Gylstorff Quirin
2023-02-17 13:05 ` [cip-dev][isar-cip-core][RFC v2 5/7] overlay: add prerequisite 'encrypt_partition' Quirin Gylstorff
2023-02-17 13:05 ` [cip-dev][isar-cip-core][RFC v2 6/7] KConfig: add tpm option Quirin Gylstorff
2023-02-17 13:05 ` [cip-dev][isar-cip-core][RFC v2 7/7] .gitlabci: Add ci build Quirin Gylstorff
2023-02-17 14:55   ` Jan Kiszka
2023-02-17 14:59     ` Gylstorff Quirin

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=00766d63-d1cc-c792-c5bd-8c7d4a3778d4@siemens.com \
    --to=quirin.gylstorff@siemens.com \
    --cc=christian.storm@siemens.com \
    --cc=cip-dev@lists.cip-project.org \
    --cc=jan.kiszka@siemens.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.