それマグで!

知識はカップより、マグでゆっくり頂きます。 takuya_1stのブログ

習慣に早くから配慮した者は、 おそらく人生の実りも大きい。

ubuntuの仮想マシンを作って、ecryptfs を実験した。

仮想マシンを作って、encfs・ecryptfs を実験する

ecryptfs

https://abillyz.com/moco/studies/276

ecryptfs制限

  • ファイルの名前の長さに制限(割と辛い)
  • スパース・ファイルが作れない(割と辛い)

代替案との比較

  • dm-crypt ブロック全体を暗号化する。TPMチップがないと解除が大変
  • Encfs ほとんど同じ。 ecryptfsのUbuntu実装の機能を使わないならこれでもいい。

目的をはっきりさせる。

LUKSでシステム全体を暗号化してしまうのがベター

特定のパスワードやパスフレーズを暗号化するのであれば、ecrypts を持ち出さなくてもいい。SSHパスフレーズをつけたり、特定ファイルをOpenSSLで暗号化してしまえばいい。またこの目的でBitwarden / 1passwordにデータを入れてしまうのもいい。

ホームディレクトリ全体の暗号化は、SSH公開鍵認証と相性が悪い。

以上のことより、ecryptfs を使うのであれば、特定ディレクトリを対象にするとメリットが得られる。たとえばChromeFirefoxのキャッシュストレージを暗号化するなど特定のフォルダを暗号化してしまうのが良い。と判断している。

ホームディレクトリを暗号化してしまう場合。とルートファイルシステムを暗号化することを考えると、ecryptfsでは、どうしても中途半端になるイメージが拭えない

仮想マシンの作成

NAME=u04
### 
qemu-img create -f qcow2 /var/lib/libvirt/images/$NAME.qcow2 10G
##
virt-install\
 --connect=qemu:///system  \
 --name $NAME \
 --ram 8192  \
 --disk path=/var/lib/libvirt/images/$NAME.qcow2  \
 --location /var/lib/libvirt/images/ubuntu-20.04.5-live-server-amd64.iso,kernel=casper/hwe-vmlinuz,initrd=casper/hwe-initrd \
 --vcpus 8  \
 --virt-type kvm  \
 --os-type linux  \
 --os-variant ubuntu20.04  \
 --graphics none \
 --extra-args "console=tty0 console=ttyS0,115200n8" \

インストール

ssh \
 -o UserKnownHostsFile=/dev/null \
 -o StrictHostKeyChecking=no \
installer@192.168.2.195

ユーザーの作成

sudo adduser takuya01
sudo usermod -aG sudo takuya01

別のユーザーでログインし直す

Ubuntu 20.04.5 LTS u04 ttyS0
    
u04 login: takuya01

暗号化ツールをインストール

sudo apt-get install ecryptfs-utils
sudo ecryptfs-migrate-home -u takuya

実際にやったものがこちら

takuya01@u04:~$ sudo ecryptfs-migrate-home -u takuya
INFO:  Checking disk space, this may take a few moments.  Please be patient.
INFO:  Checking for open files in /home/takuya
Enter your login passphrase [takuya]:

************************************************************************
YOU SHOULD RECORD YOUR MOUNT PASSPHRASE AND STORE IT IN A SAFE LOCATION.
  ecryptfs-unwrap-passphrase ~/.ecryptfs/wrapped-passphrase
THIS WILL BE REQUIRED IF YOU NEED TO RECOVER YOUR DATA AT A LATER TIME.
************************************************************************

Done configuring.

chown: cannot access '/dev/shm/.ecryptfs-takuya': No such file or directory
INFO:  Encrypted home has been set up, encrypting files now...this may take a while.
sending incremental file list
./
.bash_history
            109 100%    0.00kB/s    0:00:00 (xfr#1, to-chk=8/10)
.bash_logout
            220 100%  107.42kB/s    0:00:00 (xfr#2, to-chk=7/10)
.bashrc
          3,771 100%  920.65kB/s    0:00:00 (xfr#3, to-chk=6/10)
.profile
            807 100%  131.35kB/s    0:00:00 (xfr#4, to-chk=5/10)
.sudo_as_admin_successful
              0 100%    0.00kB/s    0:00:00 (xfr#5, to-chk=4/10)
.cache/
.cache/motd.legal-displayed
              0 100%    0.00kB/s    0:00:00 (xfr#6, to-chk=1/10)
.ssh/
.ssh/authorized_keys
          1,460 100%  109.68kB/s    0:00:00 (xfr#7, to-chk=0/10)
Could not unlink the key(s) from your keying. Please use `keyctl unlink` if you wish to remove the key(s). Proceeding with umount.

========================================================================
Some Important Notes!

 1. The file encryption appears to have completed successfully, however,
    takuya MUST LOGIN IMMEDIATELY, _BEFORE_THE_NEXT_REBOOT_,
    TO COMPLETE THE MIGRATION!!!

 2. If takuya can log in and read and write their files, then the migration is complete,
    and you should remove /home/takuya.sAHJQilw.
    Otherwise, restore /home/takuya.sAHJQilw back to /home/takuya.

 3. takuya should also run 'ecryptfs-unwrap-passphrase' and record
    their randomly generated mount passphrase as soon as possible.

 4. To ensure the integrity of all encrypted data on this system, you
    should also encrypt swap space with 'ecryptfs-setup-swap'.
========================================================================

暗号化前のファイルはコピーされて保持されている。

暗号化前のファイルは必要に応じて消す。

takuya01@u04:~$ sudo ls  /home/
takuya  takuya.sAHJQilw  takuya01

暗号化の設定について。

sudo cat /home/takuya/Access-Your-Private-Data.desktop
[Desktop Entry]
_Name=Access Your Private Data
_GenericName=Access Your Private Data
Exec=/usr/bin/ecryptfs-mount-private
Terminal=true
Type=Application
Categories=System;Security;
X-Ubuntu-Gettext-Domain=ecryptfs-utils

READMEが作られている。

sudo cat /home/takuya/README.txt
THIS DIRECTORY HAS BEEN UNMOUNTED TO PROTECT YOUR DATA.

From the graphical desktop, click on:
 "Access Your Private Data"

or

From the command line, run:
 ecryptfs-mount-private

一度ログインすると。マウントされる。

Ubuntu 20.04.5 LTS u04 ttyS0

u04 login: takuya
Password:
takuya@u04:~$ ls -at
.bash_history  ..                         .cache        .bashrc
.Private       .                          .ssh          .profile
.ecryptfs      .sudo_as_admin_successful  .bash_logout

ログアウトしても、マウントは維持される。*1

takuya@u04:~$ logout

別のユーザーでログイン

Ubuntu 20.04.5 LTS u04 ttyS0

u04 login: takuya01
Password:

マウントされたまま

takuya01@u04:~$ mount  | grep takuya
/home/.ecryptfs/takuya/.Private on /home/takuya type ecryptfs (rw,nosuid,nodev,relatime,ecryptfs_fnek_sig=5f73458653f1ac97,ecryptfs_sig=a910660e088e68f2,ecryptfs_cipher=aes,ecryptfs_key_bytes=16,ecryptfs_unlink_sigs)

再起動後

リカバリ用のパスワードを取り出す

/usr/bin/ecryptfs-unwrap-passphrase
Passphrase:
060ec7342ef1715c66b0dcd0a5f27fa0

ログアウトしてもマウントされたまま残る

これがセキュリティ的に問題を発生させると問われると、どうなんでしょう。私は別に問題ないと思う。

https://askubuntu.com/questions/910484/encrypted-home-folder-still-accessible-after-logout

SSH公開鍵認証でログインできない /etc/ssh/sshd_config

Match user takuya
  AuthorizedKeysFile    /etc/ssh/takuya/authorized_keys .ssh/authorized_keys
sudo midir /etc/ssh/takuya
sudo cp  /home/takuya/.ssh/authorized_keys  /etc/ssh/takuya/authorized_keys
sudo chown takuya. -R /etc/ssh/takuya

これでログインできるようにはなるが、公開鍵認証で秘密鍵でログインしても、ecryptfs は解除されない。

ssh  takuya@192.168.2.195 ls  -alt ~
total 8
dr-x------ 2 takuya takuya 4096  2月  2 09:26 .
lrwxrwxrwx 1 takuya takuya   56  2月  2 09:26 Access-Your-Private-Data.desktop -> /usr/share/ecryptfs-utils/ecryptfs-mount-private.desktop
lrwxrwxrwx 1 takuya takuya   52  2月  2 09:26 README.txt -> /usr/share/ecryptfs-utils/ecryptfs-mount-private.txt
lrwxrwxrwx 1 takuya takuya   31  2月  2 09:26 .Private -> /home/.ecryptfs/takuya/.Private
lrwxrwxrwx 1 takuya takuya   32  2月  2 09:26 .ecryptfs -> /home/.ecryptfs/takuya/.ecryptfs
drwxr-xr-x 6 root   root   4096  2月  2 09:26 ..

SSH経由でログイン・ログアウトしたら・・・?

ログイン手法 ecrypt自動解除 ログアウト時
パスワードログイン 解除される マウントされたまま
ssh pubkey 手動解除 ロックされる

マウント中は、root権限で閲覧できます。マウントはしてるので当然です。

SSH公開鍵認証後に、ecryptfs-mount-private するか、いったんパスワードログインしておくのが良いでしょう。 公開鍵の設置場所を変えると、返ってややこしいと思います。

解決策

  • systemd で ログアウト時にユーザー所有プロセスを全部KILLする参考
  • .profile に ecryptfs-mount-private を追加する。参考
  • .logout に ecryptfs-umount-private を追加する。

参考資料

https://qastack.jp/unix/251902/ecryptfs-auto-umount-does-not-work

*1:後で調べたら pam_systemd.so が動くので自動アンマウントが動かないらしい。