dm-crypt/スワップの暗号化

提供: ArchWiki
2017年12月24日 (日) 00:24時点におけるKusakata (トーク | 投稿記録)による版 (戻るリンクの削除)
ナビゲーションに移動 検索に移動

必要に応じて、以下で説明する様々な方法を使ってスワップパーティションを暗号化することができます。再起動時にスワップパーティションを (新しい暗号を使って) 初期化することで、過去にスワップアウトされた重要なファイルの断片がそのまま残ってしまうのを防ぐことができ、強固なデータの保護が可能になります。ただし、スワップを暗号化すると基本的にハイバネートが使えなくなってしまいます。

suspend-to-disk を使用しない

suspend-to-disk (ハイバネーション) が必要ない場合、/etc/crypttab を設定することで plain dm-crypt を使って起動時にランダムなパスワードでスワップパーティションを復号化することができます。ランダムなパスワードはシャットダウン時に破棄されるため、スワップデバイス内のデータには二度とアクセスできなくなります。

/etc/crypttabswap から始まる行をアンコメントするだけで機能を有効にできます。<device> パラメータはあなたの使用しているスワップデバイスの名前に変更してください。例:

/etc/crypttab
# <name>  <device>     <password>     <options>
swap      /dev/sdX#    /dev/urandom   swap,cipher=aes-cbc-essiv:sha256,size=256

上記の設定で /dev/sdX#/dev/mapper/swap にスワップパーティションとしてマッピングされ、通常のスワップと同じように /etc/fstab に追加できるようになります。既にスワップパーティションを使っていた場合、無効化するか fstab エントリを再利用してデバイスを /dev/mapper/swap に変更してください。大抵の場合はデフォルトのオプションで問題ありません。他のオプションやカラムの説明については man 5 crypttab および point cryptsetup FAQ 2.3 を見てください。

警告: 名前付きデバイスの中身は永久に削除されます。スワップデバイスに対してカーネルのシンプルな命名を使うのは危険です。名前の順番が起動時に変わってしまう可能性があるためです (例: /dev/sda, /dev/sdb)。以下の方法があります:
  • by-idby-path を使用する。ただし、どちらもハードウェアを変更すると変わる可能性があります。永続的なブロックデバイスの命名#by-id と by-path を参照。
  • LVM の論理ボリュームの名前を使用する。
  • #UUID と LABEL に書かれている方法を使う。ラベルや UUIDmkswap によって起動時にスワップデバイスを再作成・暗号化するため直接使われることはありません [1]

カーネルの命名の代わりに by-id による命名を使う場合、まずスワップデバイスを確認してください:

# ls -l /dev/disk/*/* | grep sdaX
lrwxrwxrwx 1 root root 10 Oct 12 16:54 /dev/disk/by-id/ata-WDC_WD2500BEVT-22ZCT0_WD-WXE908VF0470-partX -> ../../sdaX
lrwxrwxrwx 1 root root 10 Oct 12 16:54 /dev/disk/by-id/wwn-0x60015ee0000b237f-partX -> ../../sdaX

そして出力された行の中から適切なデバイスを選択して以下のように設定してください:

/etc/crypttab
# <name>  <device>                                                         <password>     <options>
swap      /dev/disk/by-id/ata-WDC_WD2500BEVT-22ZCT0_WD-WXE908VF0470-partX  /dev/urandom   swap,cipher=aes-cbc-essiv:sha256,size=256

再起動後 swapon -s を実行することで任意のデバイスマッパーエントリ (例: /dev/dm-1) が表示され、lsblk コマンドで FSTYPE カラムの crypt が表示されます。起動時に新しく暗号化するため、/dev/mapper/swap の UUID は毎回変わります。

ノート:
  • スワップに選択したパーティションが LUKS パーティションだった場合、crypttab はスワップパーティションを作成するのにパーティションを上書きしません。crypttab で間違ってスワップパーティションを指定したときにデータが消失しないようにするための安全機構です。そのようなパーティションを使うときは LUKS ヘッダーを上書きしてください。
  • ルートファイルシステムで sd-encrypt フックと luks.* カーネルパラメータを使用する場合、systemd は "Not creating device 'swap' because it was not specified on the kernel command line." とエラーを吐きます。問題を解決するには rd.luks.* パラメータを使ってください。

UUID と LABEL

ノート: 以下の方法は systemd と sd-encrypt init フックを使用している場合は機能しません。

/dev/sdX# あるいは /dev/disk/by-id/ata-SERIAL-partX などのシンプルなカーネルデバイス名で crypttab のスワップを使うのは危険です。デバイス名やパーティションレイアウトに少しでも変更があると /etc/crypttab は次回の起動時に重要なデータをフォーマットしてしまいます。PARTUUID を使用した場合も同じです。

It is more reliable to identify the correct partition by giving it a genuine UUID or LABEL. By default that does not work because dm-crypt and mkswap would simply overwrite any content on that partition which would remove the UUID and LABEL too; however, it is possible to specify a swap offset. This allows you to create a very small, empty, bogus filesystem with no other purpose than providing a persistent UUID or LABEL for the swap encryption.

Create a filesystem with label of your choice:

# mkfs.ext2 -L cryptswap /dev/sdX# 1M

The unusual parameter after the device name limits the filesystem size to 1 MiB, leaving room for encrypted swap behind it.

# blkid /dev/sdX#
/dev/sdX#: LABEL="cryptswap" UUID="b72c384e-bd3c-49aa-b7a7-a28ea81a2605" TYPE="ext2"

With this, /dev/sdX# now can easily be identified either by UUID or LABEL, regardless of how its device name or even partition number might change in the future. All that's left is the /etc/crypttab and /etc/fstab entries:

/etc/crypttab
# <name> <device>         <password>    <options>
swap     LABEL=cryptswap  /dev/urandom  swap,offset=2048,cipher=aes-xts-plain64,size=256

Note the offset: it's 2048 sectors of 512 bytes, thus 1 MiB. This way the encrypted swap will not affect the filesystem LABEL/UUID, and data alignment works out as well.

/etc/fstab
# <filesystem>    <dir>  <type>  <options>  <dump>  <pass>
/dev/mapper/swap  none   swap    defaults   0       0

Using this setup, the cryptswap will only try to use the partition with the corresponding LABEL, regardless of what its device name may be. Should you decide to use the partition for something else, by formatting it the cryptswap LABEL would also be gone, so cryptswap won't overwrite it on your next boot.

suspend-to-disk を使用する

コンピュータをディスクにサスペンド (ハイバネート) して復帰できるようにするには、スワップ領域を残す必要があります。あらかじめ LUKS スワップパーティションを用意して、ディスクに保存するか起動時に手動で入力できるようにしなければなりません。

以下の3つの方法はハイバネートのためにスワップの暗号化を設定する方法です。以下の方法を使う場合、スワップアウトされたデータがしばらくスワップに残り続ける可能性があるため注意してください。システムをシャットダウンするときにスワップを再暗号化するシステムジョブを設定することでリスクを減らすことができます。

LVM on LUKS

suspend-to-disk のサポートを暗号化されたスワップで実現するシンプルな方法は暗号化レイヤーで LVM を使うことです。暗号化されたパーティションにいくらでもファイルシステムを含めることができるようになります (root, swap, home, ...)。Dm-crypt/システム全体の暗号化#LVM on LUKS の手順に従ってから必要なカーネルパラメータを設定してください。

Assuming you have setup LVM on LUKS with a swap logical volume (at /dev/MyStorage/swap for example), all you need to do is add the resume mkinitcpio hook, and add the resume=/dev/MyStorage/swap kernel parameter to your boot loader. For GRUB, this can be done by appending it to the GRUB_CMDLINE_LINUX_DEFAULT variable in /etc/default/grub.

/etc/default/grub
GRUB_CMDLINE_LINUX_DEFAULT="... resume=/dev/MyStorage/swap"

then run grub-mkconfig -o /boot/grub/grub.cfg to update GRUB's configuration file. To add the mkinitcpio hook, edit the following line in mkinitcpio.conf

/etc/mkinitcpio.conf
HOOKS="... encrypt lvm2 resume ... filesystems ..."

then run mkinitcpio -p linux to update the initramfs image.

mkinitcpio フック

ノート: このセクションは encrypt フックを使用する場合にのみ適用され、シングルデバイスしかアンロックできません (FS#23182)。sd-encrypt を使用することで複数のデバイスをアンロックできるようになりますが (Dm-crypt/システム設定#sd-encrypt フックを使うを参照)、スワップの自動認識は使えなくなります [2]

If the swap device is on a different device from that of the root file system, it will not be opened by the encrypt hook, i.e. the resume will take place before /etc/crypttab can be used, therefore it is required to create a hook in /etc/mkinitcpio.conf to open the swap LUKS device before resuming.

現在使用中のパーティションを使いたい場合、先に無効化する必要があります:

# swapoff /dev/<device>

Also make sure you remove any line in /etc/crypttab pointing to this device.

既存のスワップパーティションを再利用する場合、パーティションが GPT パーティションテーブルに載っているときはパーティションを削除してから再作成する必要があります。パーティショニングを見てください。削除することで systemd-gpt-auto-generator によって起動時にパーティションが有効にならないようにします。

The following setup has the disadvantage of having to insert an additional passphrase for the swap partition manually on every boot.

警告: Do not use this setup with a key file if /boot is unencrypted. Please read about the issue reported here. Alternatively, use a gnupg-encrypted keyfile as per https://bbs.archlinux.org/viewtopic.php?id=120181

To format the encrypted container for the swap partition, create a keyslot for a user-memorizable passphrase.

/dev/mapper にパーティションをオープン:

# cryptsetup open --type luks /dev/<device> swapDevice

マッピングされたパーティションにスワップファイルシステムを作成:

# mkswap /dev/mapper/swapDevice

起動時にスワップをオープンにするフックを作成する必要があります。mkinitcpio-openswapAURインストールして設定するか、以下の手順に従って下さい。open コマンドを記述したフックファイルを作成:

/etc/initcpio/hooks/openswap
 run_hook ()
 {
     cryptsetup open --type luks /dev/<device> swapDevice
 }

for opening the swap device by typing your password or

/etc/initcpio/hooks/openswap
 run_hook ()
 {
     ## Optional: To avoid race conditions
     x=0;
     while [ ! -b /dev/mapper/<root-device> ] && [ $x -le 10 ]; do
        x=$((x+1))
        sleep .2
     done
     ## End of optional

     mkdir crypto_key_device
     mount /dev/mapper/<root-device> crypto_key_device
     cryptsetup open --type luks --key-file crypto_key_device/<path-to-the-key> /dev/<device> swapDevice
     umount crypto_key_device
 }

for opening the swap device by loading a keyfile from a crypted root device.

On some computers race conditions may occur when mkinitcpio tries to mount the device before the decryption process and device enumeration is completed. The commented Optional block will delay the boot process up to 2 seconds until the root device is ready to mount.

ノート: スワップがソリッドステートドライブ (SSD) 上にあり Discard/TRIM を使用する場合、--allow-discards オプションを上記の openswap フックの cryptsetup 行に追加する必要があります。discard について詳しくは Dm-crypt/特記事項#ソリッドステートドライブ (SSD) の Discard/TRIM のサポートSSD の記事を見てください。さらに 'discard' オプションをスワップデバイスの fstab エントリに追加してください。

Then create and edit the hook setup file:

/etc/initcpio/install/openswap
build ()
{
   add_runscript
}
help ()
{
cat<<HELPEOF
  This opens the swap encrypted partition /dev/<device> in /dev/mapper/swapDevice
HELPEOF
}

/etc/mkinitcpio.confHOOKS 配列に openswap フックを追加してください (filesystem の前で encrypt の後)。openswap の後には resume フックを追加してください:

HOOKS="... encrypt openswap resume filesystems ..."

ブートイメージを再生成:

# mkinitcpio -p linux

/etc/fstab に以下の行を追加してマッピングされたパーティションを追加:

/dev/mapper/swapDevice swap swap defaults 0 0

Set up your system to resume from /dev/mapper/swapDevice. For example, if you use GRUB with kernel hibernation support, add resume=/dev/mapper/swapDevice to the kernel line in /boot/grub/grub.cfg. A line with encrypted root and swap partitions can look like this:

kernel /vmlinuz-linux cryptdevice=/dev/sda2:rootDevice root=/dev/mapper/rootDevice resume=/dev/mapper/swapDevice ro

To make the parameter persistent on kernel updates, add it to /etc/default/grub.

At boot time, the openswap hook will open the swap partition so the kernel resume may use it. If you use special hooks for resuming from hibernation, make sure they are placed after openswap in the HOOKS array. Please note that because of initrd opening swap, there is no entry for swapDevice in /etc/crypttab needed in this case.

スワップファイルを使う

スワップファイルを使って既存のパーティションにスワップ領域を予約したり暗号化されたブロックデバイスのパーティションの中で設定することができます。スワップファイルから復帰するときは resume フックにパスフレーズを入力してスワップファイルが存在するデバイスをアンロックします。

警告: Btrfs はスワップファイルをサポートしていません。この警告を無視するとファイルシステムが壊れてしまう可能性があります。ループデバイスを使ってマウントすることで Btrfs でスワップファイルを使うことはできますが、スワップのパフォーマンスが大分落ちます。

To create it, first choose a mapped partition (e.g. /dev/mapper/rootDevice) whose mounted filesystem (e.g. /) contains enough free space to create a swapfile with the desired size.

選択したパーティションのマウントしたファイルシステムにスワップファイルを作成 (例: /swapfile) してください。そして swapon でスワップファイルを有効にして /etc/fstab ファイルに追加してください。スワップファイルの前のコンテンツは再起動しても残るので注意してください。

Set up your system to resume from your chosen mapped partition. For example, if you use GRUB with kernel hibernation support, add resume=your chosen mapped partition and resume_offset=see calculation command below to the kernel line in /boot/grub/grub.cfg. A line with encrypted root partition can look like this:

kernel /vmlinuz-linux cryptdevice=/dev/sda2:rootDevice root=/dev/mapper/rootDevice resume=/dev/mapper/rootDevice resume_offset=123456789 ro

The resume_offset of the swap-file points to the start (extent zero) of the file and can be identified like this:

# filefrag -v /swapfile | awk '{if($1=="0:"){print $4}}'

resume フックを etc/mkinitcpio.conf ファイルに追加してイメージを再生成してください:

HOOKS="... encrypt resume ... filesystems ..."

USB キーボードを使って復号化パスワードを入力する場合、以下のように encrypt フックの前に keyboard モジュールが来るようにしてください。モジュールが後になっていると、復号化パスワードを入力できなくなってコンピュータを起動できなくなります (keyboard を追加しても問題が起こる場合は usbinput を試してみてください):

HOOKS="... keyboard encrypt ..."

既知の問題

  • ログに "Stopped (with error) /dev/dm-1" と出力される場合 [3] を見てください。