I stumbled upon this while trying to wipe my whole disk using plain dm-crypt as suggested, e.g., here (arch linux wiki):
# cryptsetup open --type plain -d /dev/urandom /dev/sda to_be_wiped
# dd if=/dev/zero of=/dev/mapper/to_be_wiped status=progress
EDIT: See below, I checked that everything is set up correctly
I checked the first few bytes of my disk /dev/sda
and found some old data on it, so I tried the following (after having run the above dd
command for 2 hours):
A.1 Wiping the first part of the disk manually using /dev/zero
:
dd if=/dev/zero of=/dev/sda bs=512 count=2
A.2 Reading the first part of the disk to verify it's zeroed. It is:
dd if=/dev/sda bs=512 count=2 | xxd
A.3 Reading the first part of the mapped device. (This seemed to be zeroed as well - WHY? shouldn't this be the "decrypted zeroes"?)
dd if=/dev/mapper/to_be_wiped bs=512 count=2 | xxd
After that, I did the following:
B.1 Zeroing the mapped device /dev/mapper/to_be_wiped
:
dd if=/dev/zero of=/dev/mapper/to_be_wiped bs=512 count=2
B.2 Reading /dev/sda
as A.2 above - still zeroes (WHY? shouldn't this be the encrypted zeroes, i.e. random data?).
B.3 Reading the encrypted device as A.3 above - also zeroes (as expected).
Even sync
ing doesn't change the result for my hdd. So I tried the same on a file. Here everything seems to work as expected. So I am wondering...
My (main) question is:
- Where is my error, am I doing something wrong? I assumed a sector-by-sector encryption, so that in the first case 3. shouldn't be zeroed, while in the second case 2 should be randomized.
This automatically and inevitably leads to the follow-up question
- Is it even safe then to assume you can securely wipe a disk using this method?
What could help on the way to get there:
- Isn't the mapping of a plain dm-crypt device sector by sector, as stated in the manual?
- Is there a difference between using a file for dm-crypt rather than a disk?
EDIT: I verified, as suggested by Xen2050, that everything is set up correctly:
# cryptsetup -v status to_be_wiped
/dev/mapper/to_be_wiped is active.
type: PLAIN
cipher: aes-cbc-essiv:sha256
keysize: 256 bits
key location: dm-crypt
device: /dev/sda
sector size: 512
offset: 0 sectors
size: 976773168 sectors
mode: read/write
Command successful.
# dmsetup ls --target crypt
to_be_wiped (254, 0)
# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
loop0 7:0 0 487.9M 1 loop /run/archiso/sfs/airootfs
sda 8:0 0 465.8G 0 disk
└─to_be_wiped 254:0 0 465.8G 0 crypt
[...]
The loop device is from the arch linux live stick I use, I removed the other present disks from lsblk
.
EDIT2: Short example with output added (below). Is dm-crypt not writing to disk if one does not close it? If so, why and how to change it? Have a look at the following:
root@archiso ~ # dd if=/dev/zero of=/dev/sda bs=8 count=2 status=none
root@archiso ~ # xxd -l 16 /dev/sda
00000000: 0000 0000 0000 0000 0000 0000 0000 0000 ................
root@archiso ~ # cryptsetup open --type plain -d /dev/urandom /dev/sda to_be_wiped
root@archiso ~ # xxd -l 16 /dev/mapper/to_be_wiped
00000000: df52 0cc9 082a 0de2 1df7 b55f 7626 c45b .R...*....._v&.[
root@archiso ~ # dd if=/dev/zero of=/dev/mapper/to_be_wiped bs=8 count=1 status=none
root@archiso ~ # xxd -l 16 /dev/mapper/to_be_wiped
00000000: 0000 0000 0000 0000 1df7 b55f 7626 c45b ..........._v&.[
root@archiso ~ # xxd -l 16 /dev/sda
00000000: 0000 0000 0000 0000 0000 0000 0000 0000 ................
root@archiso ~ # sync
root@archiso ~ # grep -e Dirty: -e Writeback /proc/meminfo
Dirty: 0 kB
Writeback: 0 kB
WritebackTmp: 0 kB
root@archiso ~ # cat /sys/dev/block/8:0/stat
1153 0 49276 19035 9 0 72 6763 0 19010 25747 0 0 0 0
root@archiso ~ # xxd -l 16 /dev/sda
00000000: 0000 0000 0000 0000 0000 0000 0000 0000 ................
root@archiso ~ # cryptsetup close to_be_wiped
root@archiso ~ # xxd -l 16 /dev/sda
00000000: c6d2 1e9b abf4 16a2 7d1a b1bd 8a28 63d8 ........}....(c.
EDIT3: I did a similar test as in EDIT2 with more data and found the following: It seems that plain dm-crypt is not writing the first half megabyte unless closed. If I close the mapped device (cryptsetup close to_be_wiped
), everything is randomized. Weird stuff without any explanation, especially because files seem to be handled correctly/immediately - does anyone have one?
EDIT4: I used cryptsetup 2.0.6
on the arch iso 2019.02.01
from February.