さくら VPS は、AWS LightSail のようにディスクの暗号化をサービス側でしているという情報がありませんでした。昨今のハードディスク廃棄問題も気になるところで、あまり漏れたくない情報を保存したいのでディスクをまるごと(/boot は除く)暗号化してみます。
Ubuntu 18.04 のインストール
コントロールパネルから カスタムOSインストール Ubuntu 18.04 を行います。
途中の「Partition disks」で Guided - use entire disk and set up encrypted lvm を選択します。そしてパスフレーズを設定します。
OS セットアップ後
VPSのコントロールパネルで緑の起動ボタンをクリックして、さらにコンソールボタンメニューのシリアルコンソールも即座に開いておきます。
シリアルコンソール画面がパスフレーズ入力で止まるので、ここで先ほどのパスフレーズを入力して起動させます。
暗号化状態の確認
1 2 3 4 5 6 7 8 9 10 11 12
| # cryptsetup status /dev/mapper/vda5_crypt /dev/mapper/vda5_crypt is active and is in use. type: LUKS1 cipher: aes-xts-plain64 keysize: 512 bits key location: dm-crypt device: /dev/vda5 sector size: 512 offset: 4096 sectors size: 208207872 sectors mode: read/write flags: discards
|
起動時のアンロック設定
現状は起動時にパスフレーズを入力するしかありません。ここにさらに別途鍵ファイルをbootパーティション内に追加して自動起動するようにします。
ここからは Debian/Ubuntuで暗号化 LVM を使いつつ自動起動する - @znz blog の受け売りが大半です。大変参考になりました。ありがとうございます。
鍵ファイルを追加
鍵ファイルの作成
/boot/keyfile
として鍵ファイルを作成します。
1 2 3 4
| # touch /boot/keyfile # chmod 400 /boot/keyfile # dd if=/dev/urandom of=/boot/keyfile bs=1024 count=4 # chattr +i /boot/keyfile
|
鍵ファイルの追加
1
| # cryptsetup -v luksAddKey /dev/vda5 /boot/keyfile
|
自動起動の設定
bootパーティションのデバイスを確認
1 2 3 4 5 6 7 8 9 10
| # df Filesystem 1K-blocks Used Available Use% Mounted on udev 1988928 0 1988928 0% /dev tmpfs 403916 604 403312 1% /run /dev/mapper/www1042gj--vg-root 100953120 1509740 94272180 2% / tmpfs 2019560 0 2019560 0% /dev/shm tmpfs 5120 0 5120 0% /run/lock tmpfs 2019560 0 2019560 0% /sys/fs/cgroup /dev/vda1 720368 75964 592012 12% /boot tmpfs 403912 0 403912 0% /run/user/1000
|
デバイスのUUIDマッピングを確認
1 2 3 4 5 6
| # ls -l /dev/disk/by-uuid total 0 lrwxrwxrwx 1 root root 10 Feb 2 13:30 00000000-0000-0000-0000-000000000005 -> ../../vda5 lrwxrwxrwx 1 root root 10 Feb 2 13:20 00000000-0000-0000-0000-000000000001 -> ../../vda1 lrwxrwxrwx 1 root root 10 Feb 2 13:20 00000000-0000-0000-0000-000000000012 -> ../../dm-2 lrwxrwxrwx 1 root root 10 Feb 2 13:20 00000000-0000-0000-0000-000000000011 -> ../../dm-1
|
暗号化を解除する鍵を指定
/etc/crypttab
を書き換えます。
1
| vda5_crypt UUID=00000000-0000-0000-0000-000000000005 none luks,discard
|
を下記のように、none と discard を置き換えます。
1
| vda5_crypt UUID=00000000-0000-0000-0000-000000000005 /dev/disk/by-uuid/00000000-0000-0000-0000-000000000001:/keyfile luks,keyscript=/lib/cryptsetup/scripts/passdev
|
ブートプロセスに反映
1 2
| # update-initramfs -u update-initramfs: Generating /boot/initrd.img-4.15.0-76-generic
|
Systemd の起動タイムアウトを短縮
このままリブートすると途中 boot パーティションの準備ができるまで待ち状態になって、タイムアウトまで90秒も起動プロセスが止まってしまう問題があります。
そのためタイムアウトを短くします。 /etc/systemd/system.conf
の [Manager] セクションの DefaultTimeoutStartSec を有効にして編集します。
1 2
| #DefaultTimeoutStopSec=90s DefaultTimeoutStartSec=10s
|
OS を再起動します。
再起動後
正常に起動しない場合は、シリアルコンソールから編集する必要があります。
下記のように LUKS の適用状態がわかります。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
| # cryptsetup luksDump /dev/vda5 LUKS header information for /dev/vda5
Version: 1 Cipher name: aes Cipher mode: xts-plain64 Hash spec: sha256 Payload offset: 4096 MK bits: 512 MK digest: 00 11 22 ... MK salt: 00 11 22 ... MK iterations: 21361 UUID: 00000000-0000-0000-0000-000000000005
Key Slot 0: ENABLED Iterations: 341778 Salt: 00 11 22 ... Key material offset: 8 AF stripes: 4000 Key Slot 1: ENABLED Iterations: 937902 Salt: 00 11 22 ... Key material offset: 512 AF stripes: 4000 Key Slot 2: DISABLED Key Slot 3: DISABLED Key Slot 4: DISABLED Key Slot 5: DISABLED Key Slot 6: DISABLED Key Slot 7: DISABLED
|
最後に
厳密にはキー自体が生で保存されているため、機密が完全とは言い難いです。単にディスクをRAW読み取りして、データが見えたりしないようにするのが、この対応の目的となります。
参考文献
tilfin
freelance software engineer