將唯讀 LUKS 分割區重新對應為讀寫

將唯讀 LUKS 分割區重新對應為讀寫

cryptsetup 可以使用--readonly-r選項調用,這將設定一個唯讀映射:

cryptsetup --readonly luksOpen /dev/sdb1 sdb1

將裝置開啟為唯讀後,我可以稍後將其重新映射為讀寫嗎?顯然,我的意思是將其映射為讀寫,而不先關閉它,然後再次打開它。我可以重新映射它而無需再次輸入密碼嗎?

如果這是不可能的,這只是 cryptsetup 不支援這一點,還是有一些更基本的等級?

答案1

用命令好像不行cryptsetup。不幸的cryptsetup是有一些這樣的不可變標誌......--allow-discards也是其中之一。如果您在開啟容器時未設定此項,則以後無法新增它。

至少,不是用cryptsetup指令。但是,由於cryptsetup建立了常規裝置映射器目標,因此您可以對其dmsetup進行修改。當然,出於各種原因,不建議這樣做:這就像更改正在使用的分割區的分割表 - 如果搞砸了,您可能會丟失所有資料。

設備映射器允許在運行時動態重新映射所有設備,並且它根本不關心資料的安全性;這就是為什麼此功能通常包含在 LVM 層後面,LVM 層保留必要的元資料以確保其安全性。

建立唯讀 LUKS 設備:

# truncate -s 100M foobar.img
# cryptsetup luksFormat foobar.img
# cryptsetup luksOpen --read-only foobar.img foobar

方式dmsetup看到它:

# dmsetup info foobar
Name:              foobar
State:             ACTIVE (READ-ONLY)
Read Ahead:        256
Tables present:    LIVE
[...]
# dmsetup table --showkeys foobar
0 200704 crypt aes-xts-plain64 ef434503c1874d65d33b1c23a088bdbbf52cb76c7f7771a23ce475f8823f47df 0 7:0 4096

請注意通常不應洩露的主金鑰,因為它會破壞 LUKS 提供的任何強力保護。不幸的是,我還沒有找到一種不使用它的方法,因為dmsetup也缺乏直接的--make-this-read-write選擇。但是dmsetup reload允許完全替換映射,因此我們將在讀寫模式下將其替換為自身。

# dmsetup table --showkeys foobar | dmsetup reload foobar
# dmsetup info foobar
Name:              foobar
State:             ACTIVE (READ-ONLY)
Read Ahead:        256
Tables present:    LIVE & INACTIVE

重新載入後它仍然是唯讀的,因為重新載入會進入非活動表。

若要使非活動表處於活動狀態,請使用dmsetup resume

# dmsetup resume foobar
# dmsetup info foobar
Name:              foobar
State:             ACTIVE
Read Ahead:        256
Tables present:    LIVE

這樣我們就有了一個可讀寫的 LUKS 裝置。

它可以與即時檔案系統一起使用嗎?

# cryptsetup luksOpen --readonly foobar.img foobar
# mount /dev/mapper/foobar /mnt/foobar
mount: /mnt/foobar: WARNING: device write-protected, mounted read-only.
# mount -o remount,rw /mnt/foobar
mount: /mnt/foobar: cannot remount /dev/mapper/foobar read-write, is write-protected.

所以它是唯讀的。使其可讀寫並重新掛載:

# dmsetup table --showkeys foobar | dmsetup reload foobar
# dmsetup resume foobar
# mount -o remount,rw /mnt/foobar
# echo hey it works > /mnt/foobar/amazing.txt

我們可以回到只讀狀態嗎?

# mount -o remount,ro /mnt/foobar
# dmsetup table --showkeys foobar | dmsetup reload foobar --readonly
# dmsetup resume foobar
# mount -o remount,rw /mnt/foobar
mount: /mnt/foobar: cannot remount /dev/mapper/foobar read-write, is write-protected.

所以它可能有效。將標誌新增至現有 crypt 對應的過程allow_discards類似 - 您必須重新載入包含此標誌的表。然而,已經檢測到不存在丟棄支援的檔案系統可能不會被說服動態地重新偵測到這一點。所以目前還不清楚它的實用性如何。


不過,除非您有充分的理由不這樣做,否則您應該堅持使用常規命令重新打開cryptsetup,即使這意味著卸載並重新提供密碼。它更安全,更重要的是,不會規避 LUKS 安全概念。

答案2

開啟磁碟區後無法從唯讀變更為讀寫。我在 cryptsetup 原始碼中沒有看到任何選項可以做到這一點。

相關內容