ディスクの暗号化

はじめに

第22章はじめに

ファイルシステムは、そこに含まれるデータをのぞき見と破壊の両方から保護するために、暗号化することができます。暗号化はインストール時に選択するか、後で組み込むこともできます。Linuxディストリビューションは、ほとんどの場合LUKSメソッドを使用し、cryptsetupを使用して暗号化に関わるタスクを実行します。


学習目標
この章の終わりまでに、次のことができるようになります。

暗号化を使用する正当な理由と暗号化がいつ要求されるかを示すことができます。
cryptsetupを使用してLUKSがどのように動作するかを理解できます。
暗号化されたファイルシステムとパーティションの設定と使用ができます。
起動時に暗号化されたパーティションをマウントするようにシステムを構成できます。​


ディスクの暗号化

なぜ暗号化を使用するのか？
機密データを保存して送信する場合は、常に暗号化を使用する必要があります。ブロック デバイス レベルの暗号化を構成して使用することにより、ハード ドライブやその他のメディアに含まれるデータの損失や侵害によって引き起こされる損害に対して、最も強力な保護の1つが提供されます。

最新のLinuxディストリビューションでは、すべてまたは一部のディスク パーティションを暗号化することを、インストール中に選択できます。暗号化パーティションを後で作成してフォーマットすることも簡単にできますが、データ コピー操作を行わずに既存のパーティションを暗号化することはできません。


LUKS
最新のLinuxディストリビューションは、主にLUKS（Linux Unified Key Setup）を使用してブロック デバイス レベルの暗号化を提供します。ラップトップ、タブレット、スマートフォンなどのポータブル システムでは、ブロック デバイスへの暗号化を強くお勧めします。

LUKSはcryptsetupでインストールできます。cryptsetupは、plainモードのdm-cryptボリューム、loop-AES、TrueCrypt互換などの他のメソッドも使える、強力なユーティリティです。LUKSはLinuxで最もよく使用される標準的な方法であるため、これらの選択肢については説明しません。

dm-cryptカーネル モジュールは、LVMでも頻繁に使用されるデバイス マッパーというカーネルの仕組みを使用します。これについては後で説明します。

LUKSはすべての必要な情報をパーティション ヘッダー自体に保存するため、パーティションを他のディスクやシステムに簡単に移行できます。

LUKSは、スワップ パーティションを透過的に暗号化することもできます。


cryptsetup
基本的に、すべてはSwiss army knifeプログラムであるcryptsetupで行われます。暗号化されたボリュームは、一度セットアップすれば、通常のディスク ユーティリティを使用してマウントおよびアンマウントできます。

コマンドの一般的な書式は次のとおりです。

cryptsetup [OPTION...] <action> <action-specific>

そして、cryptsetupでできる内容は、以下のコマンドで参照できます。

$ cryptsetup --help

cryptsetup 2.0.6
Usage: cryptsetup [OPTION...] <action> <action-specific>
      --version                  Print package version
  -v, --verbose                  Shows more detailed error messages
      --debug                    Show debug messages
  -c, --cipher=STRING            The cipher used to encrypt the disk (see /proc/crypto)
  -h, --hash=STRING              The hash used to create the encryption key from the passphrase
  -y, --verify-passphrase        Verifies the passphrase by asking for it twice
  -d, --key-file=STRING          Read the key from a file
      --master-key-file=STRING   Read the volume (master) key from file.
      --dump-master-key          Dump volume (master) key instead of keyslots info
....


暗号化されたパーティションの使用
パーティション/dev/sdc12が既に存在する場合、次のコマンドは、暗号化を設定し、それをLUKSで使用できるようにし、フォーマットし、マウントし、使用し、そしてアンマウントします。

まず、LUKSでパーティションを初期化する必要があります。

$ sudo cryptsetup luksFormat /dev/sdc12

暗号化されたボリュームを後で使用する時に必要となる、パスフレーズの入力を求められます。暗号化を設定するときは、この手順を1回だけ行う必要があります。あなたが使っているカーネルは、cryptsetupで使用されるデフォルトの暗号化方式をサポートしていない場合があります。その場合には、/proc/cryptoを調べてシステムがサポートしている方式を確認します。それを適用する場合：

$ sudo cryptsetup luksFormat --cipher aes /dev/sdc12

ボリュームをいつでも使用可能にする場合：

$ sudo cryptsetup --verbose luksOpen /dev/sdc12 SECRET

この時、パスフレーズを入力するように求められます。パーティションをフォーマットする場合：

$ sudo mkfs.ext4 /dev/mapper/SECRET

マウントする場合：

$ sudo mount /dev/mapper/SECRET /mnt

これにより、まるで暗号化されていないパーティションであるかのように、自由に使用できます。完了したら、次のコマンドでアンマウントします。

$ sudo umount /mnt

そして、次のコマンドで今の関連付けを削除します。

$ sudo cryptsetup --verbose luksClose SECRET


ブート時のマウント
ブート時に暗号化されたパーティションをマウントするには、2つの条件を満たす必要があります。

1つ目は/etc/fstabに適切なエントリを作成することです。これについて特別なことは何もなく、暗号化については一切触れる必要がありません。これは次のように簡単にできます。

/dev/mapper/SECRET /mnt ext4 defaults 0 0

2つ目は、/etc/crypttabにエントリを追加することです。これも次のように簡単にできます。

SECRET ​/dev/sdc12

このファイルでは、起動時にプロンプトを表示したくない場合にパスワードを指定するなど、さらに多くのことを実行できます（ただ、セキュリティ面では逆効果と思われます）。このファイルで何ができるかを調べるには、man crypttabを参照してください。


デモ：暗号化されたループバック パーティションのセットアップ

このビデオでは、暗号化されたループバック パーティションを設定する方法を示します。


演習

課題 22.1ディスクの暗号化

🚩
以下のPDFドキュメントに埋め込まれた外部URLにアクセスする場合は、常に右クリックして新しいタブまたはウィンドウで開いてください。直接クリックしてURLを開こうとすると、コース ウィンドウ／タブが閉じます。

【【これ以降は橋本さんの訳を挿入】】

In this exercise, you will encrypt a partition on the disk in order to provide a measure of security in the event that the hard drive or laptop is stolen. Reviewing the cryptsetup documentation first would be a good idea (man cryptsetup and cryptsetup --help).

1.  Create a new partition for the encrypted block device with fdisk.  Make sure the kernel is aware of the new partitiontable. A reboot will do this but there are other methods.
2.  Format the partition with cryptsetup using LUKS for the crypto layer.
3.  Create the un-encrypted pass through device by opening the encrypted block device, i.e.,secret-disk.
4.  Add an entry to /etc/crypttab so that the system prompts for the passphrase on reboot.
5.  Format the filesystem as an ext4 filesystem.
6.  Create a mount point for the new filesystem, i.e. /secret.
7.  Add an entry to /etc/fstab so that the filesystem is mounted on boot.
8.  Try and mount the encrypted filesystem.
9.  Validate the entire configuration by rebooting.

Solution 22.1

1.$ sudo fdisk /dev/sda
Create a new partition (in the below /dev/sda4 to be concrete) and then either issue:
$ sudo partprobe -s
to have the system re-read the modified partition table, or reboot (which is far safer).
Note:If you can’t use a real partition, use the technique in the previous chapter to use a loop device or image file forthe same purpose.
2.$ sudo cryptsetup luksFormat /dev/sda4
3.$ sudo cryptsetup luksOpen /dev/sda4 secret-disk
4.  Add the following to /etc/crypttab:

in /etc/crypttab
secret-disk    /dev/sda4

5.$ sudo mkfs -t ext4 /dev/mapper/secret-disk
6.$ sudo mkdir -p /secret
7.  Add the following to/etc/fstab:

in /etc/fstab
/dev/mapper/secret-disk    /secret    ext4    defaults 1 2

8.  Mount just the one filesystem:
$ sudo mount /secret
or mount all filesystems mentioned in/etc/fstab:
$ sudo mount -a
9.  Reboot.


課題 22.2. Swap領域のスワップ

🚩
以下のPDFドキュメントに埋め込まれた外部URLにアクセスする場合は、常に右クリックして新しいタブまたはウィンドウで開いてください。直接クリックしてURLを開こうとすると、コース ウィンドウ／タブが閉じます。

【【これ以降は橋本さんの訳を挿入】】

In this exercise, we will be encrypting the swap partition.  Data written to the swap device can contain sensitive information.Because swap is backed by an actual partition, it is important to consider the security implications of having an unencrypted swap partition.

The process for encrypting is similar to the previous exercise, except we will not create a file system on the encrypted blockdevice.

In this case, we are also going to use the existing swap device by first de-activating it and then formatting it for use as anencrypted swap device.  It would be a little bit safer to use a fresh partition below, or you can safely reuse the encrypted partition you set up in the previous exercise. At the end we explain what to do if you have problems restoring.

You may want to revert back to the original unencrypted partition when we are done by just running mkswap on it again whenit is not being used, as well as reverting the changes in the configuration files, /etc/crypttab and /etc/fstab.

1.  Find out what partition you are currently using for swap and then deactivate it:

$ cat /proc/swaps
Filename                                Type            Size    Used    Priority
/dev/sda11                              partition       4193776 0       -1

$ sudo swapoff /dev/sda11

2.  Do the same steps as in the previous exercise to set up encryption:
$ sudo cryptsetup luksFormat /dev/sda11  # may use --cipher aes option
$ sudo cryptsetup luksOpen   /dev/sda11  swapcrypt

3.  Format the encrypted device to use with swap:
$ sudo mkswap /dev/mapper/swapcrypt

4.  Now test to see if it actually works by activating it:
$ sudo swapon /dev/mapper/swapcrypt
$ cat /proc/swaps

5.  To ensure the encrypted swap partition can be activated at boot you need to do two things:
(a)  Add a line to/etc/crypttabso that the system prompts for the passphrase on reboot:

in /etc/crypttab
swapcrypt  /dev/sda11   /dev/urandom  swap,cipher=aes-cbc-essiv:sha256,size=256

(Note /dev/urandom is preferred over /dev/random for reasons involving potential entropy shortages as dis-cussed in the man page forcrypttab.)  You don’t need the detailed options that follow, but we give them as anexample of what more you can do.

(b)  Add an entry to/etc/fstabso that the swap device is activated on boot.

in /etc/fstab

/dev/mapper/swapcrypt  none    swap    defaults 0 0

6.  You can validate the entire configuration by rebooting.

To restore your original unencrypted partition:

$ sudo swapoff /dev/mapper/swapcrypt
$ sudo cyyptsetup luksClose swapcrypt
$ sudo mkswap /dev/sda11
$ sudo swapon -a

If the swapon command fails it is likely because /etc/fstab no longer properly describes the swap partition. If this partitionis described in there by actual device node (/dev/sda11) there won’t be a problem. You can fix either by changing the line inthere to be:

in /etc/fstab
/dev/sda11  swap   swap  defaults 0 0

or by giving a label when formatting and using it as in:
$ sudo mkswap -L SWAP /dev/sda11
and then putting in the file:

in /etc/fstab 
LABEL=SWAP  swap   swap  defaults 0 0


知識チェック

「第22章 - ディスクの暗号化」を完遂しました。おめでとうございます。このクイズに答えて、これまでに学んだ概念の理解度をチェックしてください。

クイズ開始

問題 22.1
LUKSによって、最新のLinuxディストリビューションでは暗号化されたパーティションを簡単に作成できます。暗号化されたパーティションを準備してマウントする手順の、最も正しい順序はどれですか？

i. 暗号化されていないパス スルー デバイスを作成します
ii. 暗号化されたブロック デバイスにファイルシステムをマウントします
iii. ext4などの標準ファイルシステムでフォーマットします
iv. 暗号化して利用するブロック デバイスのパーティションを作成します
v. cryptsetupでフォーマットします

A. iv, v, i, iii, ii
B. iv, iii, i, v, iii
C. i, v, iii, iv, ii
D. iv, v, ii, i, iii



