Skip to content

Commit 8208be9

Browse files
authored
Merge pull request #2781 from OSInside/fix_key_slot_handling_for_reencrypt
Fix key slot selection for luks reencrypt
2 parents 4dd497c + 67ab6b3 commit 8208be9

File tree

5 files changed

+31
-16
lines changed

5 files changed

+31
-16
lines changed

build-tests/x86/tumbleweed/test-image-luks/appliance.kiwi

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,15 +60,15 @@
6060
</type>
6161
</preferences>
6262
<preferences profiles="ReEncryptFullDisk">
63-
<type image="oem" filesystem="ext4" kernelcmdline="console=ttyS0 rd.kiwi.oem.luks.reencrypt" firmware="uefi" luks="linux" luks_version="luks2" luks_pbkdf="pbkdf2" bootpartition="false">
63+
<type image="oem" filesystem="ext4" kernelcmdline="console=ttyS0 rd.kiwi.oem.luks.reencrypt rd.kiwi.oem.luks.reencrypt_randompass quiet" firmware="uefi" luks="linux" luks_version="luks2" luks_pbkdf="pbkdf2" bootpartition="false" eficsm="false">
6464
<luksformat>
6565
<option name="--cipher" value="aes-xts-plain64"/>
6666
<option name="--key-size" value="256"/>
6767
</luksformat>
6868
<oemconfig>
6969
<oem-resize>true</oem-resize>
7070
</oemconfig>
71-
<bootloader name="grub2" console="serial" timeout="10"/>
71+
<bootloader name="grub2" console="serial" timeout="10" use_disk_password="true"/>
7272
</type>
7373
</preferences>
7474
<users>

dracut/modules.d/99kiwi-lib/kiwi-luks-lib.sh

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,17 +25,19 @@ function deactivate_luks {
2525
function reencrypt_luks {
2626
declare kiwi_RootPart=${kiwi_RootPart}
2727
local disk=$1
28+
local keyslot=/root/.luks.slot
2829
local header_checksum_origin=/root/.luks.header
2930
local header_checksum_cur=/root/.luks.header.cur
3031
local keyfile=/root/.root.keyfile
3132
local new_keyfile=/run/.kiwi_reencrypt.keyfile
32-
local passphrase_file=/root/.slot0
33+
local passphrase_file=/root/.slotpass
3334
local progress=/dev/install_progress
3435
local load_text="Reencrypting..."
3536
local title_text="LUKS"
3637
local device
3738
device=$(get_partition_node_name "${disk}" "${kiwi_RootPart}")
3839
read -r header_checksum_origin < "${header_checksum_origin}"
40+
read -r keyslot < "${keyslot}"
3941

4042
# Checksum test if luks header is still the image origin header
4143
cryptsetup luksHeaderBackup \
@@ -66,18 +68,18 @@ function reencrypt_luks {
6668
chmod 0400 "${new_keyfile}"
6769
cryptsetup \
6870
--key-file "${passphrase_file}" \
69-
--key-slot 0 \
71+
--key-slot "${keyslot}" \
7072
luksChangeKey "${device}" "${new_keyfile}"
7173
cp "${new_keyfile}" "${passphrase_file}"
7274
fi
7375
# reencrypt
7476
setup_progress_fifo ${progress}
7577
(
76-
# reencrypt slot0, this will wipe all key slots
78+
# reencrypt, this will overwrite all key slots
7779
cryptsetup reencrypt \
7880
--progress-frequency 1 \
7981
--key-file "${passphrase_file}" \
80-
--key-slot 0 \
82+
--key-slot "${keyslot}" \
8183
"${device}" 2>&1 | sed -u 's/.* \([0-9]*\)[0-9.]*%.*/\1/'
8284
) >"${progress}" &
8385
run_progress_dialog "${load_text}" "${title_text}"

kiwi/builder/disk.py

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1277,15 +1277,19 @@ def _write_luks_header_checksum_to_boot_image(
12771277
self, luks_root: Optional[LuksDevice]
12781278
) -> None:
12791279
if luks_root is not None:
1280-
log.info('Including origin LUKS header checksum')
1281-
filename = ''.join(
1282-
[self.root_dir, '/root/.luks.header']
1283-
)
1284-
self.boot_image.include_file(
1285-
filename=os.sep + os.sep.join(
1286-
['root', os.path.basename(filename)]
1287-
), delete_after_include=True
1280+
log.info(
1281+
'Including origin LUKS header checksum and key slot number'
12881282
)
1283+
filenames = [
1284+
''.join([self.root_dir, '/root/.luks.header']),
1285+
''.join([self.root_dir, '/root/.luks.slot'])
1286+
]
1287+
for filename in filenames:
1288+
self.boot_image.include_file(
1289+
filename=os.sep + os.sep.join(
1290+
['root', os.path.basename(filename)]
1291+
), delete_after_include=True
1292+
)
12891293

12901294
def _write_generic_fstab_to_system_image(
12911295
self, device_map: Dict,

kiwi/storage/luks_device.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ def create_crypto_luks(
9494
to unlock the luks device
9595
:param string root_dir: root dir path
9696
"""
97+
keyslot = '0'
9798
if not options:
9899
options = []
99100
if osname:
@@ -174,6 +175,7 @@ def create_crypto_luks(
174175
'luksAddKey', storage_device, keyfile_path
175176
]
176177
)
178+
keyslot = '1'
177179

178180
# Create backup header checksum as reencryption reference
179181
master_checksum = f'{root_dir}/root/.luks.header'
@@ -190,6 +192,11 @@ def create_crypto_luks(
190192
with open(master_checksum, 'w') as shasum:
191193
shasum.write(checksum)
192194

195+
# Create key slot number as reencryption reference
196+
master_slot = f'{root_dir}/root/.luks.slot'
197+
with open(master_slot, 'w') as slot:
198+
slot.write(keyslot)
199+
193200
# open the pool
194201
Command.run(
195202
[

test/unit/builder/disk_test.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1196,7 +1196,8 @@ def test_create_disk_luks_root(
11961196
call('/root/.root.keyfile'),
11971197
call('/config.partids'),
11981198
call('/etc/crypttab'),
1199-
call(filename='/root/.luks.header', delete_after_include=True)
1199+
call(filename='/root/.luks.header', delete_after_include=True),
1200+
call(filename='/root/.luks.slot', delete_after_include=True)
12001201
]
12011202
self.boot_image_task.write_system_config_file.assert_called_once_with(
12021203
config={'install_items': ['/root/.root.keyfile']},
@@ -1251,7 +1252,8 @@ def test_create_disk_luks_root_with_disk_password(
12511252
call('/root/.root.keyfile'),
12521253
call('/config.partids'),
12531254
call('/etc/crypttab'),
1254-
call(filename='/root/.luks.header', delete_after_include=True)
1255+
call(filename='/root/.luks.header', delete_after_include=True),
1256+
call(filename='/root/.luks.slot', delete_after_include=True)
12551257
]
12561258
self.boot_image_task.write_system_config_file.assert_called_once_with(
12571259
config={'install_items': ['/root/.root.keyfile']},

0 commit comments

Comments
 (0)