device mapper crypt target
Subsystems (2)
RFC 1/1 device mapper crypt target
The last days I've written a target for the device-mapper in the 2.5 kernel that uses the in-kernel cryptoapi to do transparent encryption on a block device.
It works for me and doesn't show any strange corruptions. Joe (the device-mapper guy) also looked over it and made some modifications.
The target doesn't have any special user space tool yet, so you have to use the dmsetup tool (in the libdevmapper-1.0 package you'll find on the Sistina ftp server) to set up an encrypted device. It also means that it doesn't store a passhrase encrypted key, you'll have to directly specify the key for the symmetric encryption/decrypt operation.
With the dmsetup tool you can create a device like this:
dmsetup create <device name> <table file>
The device will be created in /dev/mapper/<device name> and can be removed with "dmsetup remove <device name>".
The table file can contain several lines, but if you just want a device that doesn't use several other devices as backend (like LVM can do), specifying only one line is fine. The syntax for the crypt target is:
<cipher> <key> <iv_offset> <dev_path> <start>
<cipher> is one of the ciphers in the cryptoapi like aes or des. You can optionally specify the block chaining mode like "aes-cbc" or "aes-ecb" (inspired by cryptoloop, but no copied code). The CBC mode is the default, but it doesn't really do cipher block chaining, it only additonally perturbs the encryption by munging the sector number so that the same sector gets always encrypted differently, like zero-filled ones.
<key> is the actual key. It's hex encrypted, two digits per byte.
<iv_offset> is used with CBC and you can specify a sector offset for the perturbation. For example if you want to map only a part of your encrypted device and start at some offset, you can specify it here, so that you're still able to read your data.
<dev_path> is the device path of the device you want to encrypt (can also be a loop device if you want to test things on a file).
<start> is the sector offset in the device where you want the mapping to start.
So a complete line for the table file might look like:
0 204800 crypt aes 0123456789abcdef0123456789abcdef 0 /dev/hda7 0
(the second parameter is the size of the device you want to create in sectors, this would give a 100 MB device).
If something fails look into your log file (and don't forget to remove the device that was created with dmsetup remove <devname>).
My tests suggest that it's fairly stable. I've created different filesystems on it (ext2, ext3, reiserfs) and did a lot of copy operations from/to/on it. A lot of small files, large files. Unmounting/remounting, etc... I didn't find any corruptions. It even works when putting swap space on it and it didn't deadlock my machine under extreme memory pressure.
I also added a lot of temporary debug statements and poisoning code to make sure that allocations and frees are correctly balanced.
The attached patch adds a dm-daemon.c/.h, this is a forward-ported version from 2.4 and the actual dm-crypt.c.
It can be compiled in statically or as a module (kernel configuration:
Multiple Devices -> Device mapper -> crypt target).
The patch should apply cleanly on top of 2.6.0-test1. I've appended it as an attachement because my mailer adds additional newlines. :(
--
Christophe Saout <[email protected]>
Please avoid sending me Word or PowerPoint attachments. See http://www.fsf.org/philosophy/no-word-attachments.html
drivers/md/dm.c +3 -0
@@ -124,6 +124,9 @@ xx(dm_linear) xx(dm_stripe) xx(dm_interface) +#ifdef CONFIG_BLK_DM_CRYPT + xx(dm_crypt) +#endif #undef xx };
drivers/md/dm-crypt.c +784 -0
784 lines not on this page · raw message
drivers/md/dm-daemon.c +104 -0
104 lines not on this page · raw message
drivers/md/dm-daemon.h +29 -0
@@ -0,0 +1,29 @@ +/* +* Copyright (C) 2003 Sistina Software +* +* This file is released under the LGPL. +*/ + +#ifndef DM_DAEMON_H +#define DM_DAEMON_H + +#include <asm/atomic.h> +#include <asm/semaphore.h> + +struct dm_daemon { +void (*fn)(void ); +char name[16 ]; +atomic_t please_die; +struct semaphore start_lock; +struct semaphore run_lock; + +atomic_t woken; +wait_queue_head_t job_queue; +}; + +int dm_daemon_start (struct dm_daemon *dd,const char *name,void (*fn)(void )); +void dm_daemon_stop (struct dm_daemon *dd); +void dm_daemon_wake (struct dm_daemon *dd); +int dm_daemon_running (struct dm_daemon *dd); + +#endif
drivers/md/dm.h +6 -1
@@ -159,7 +159,7 @@void dm_interface_exit (void );/* -* Targets for linear and striped mappings +* Targets for the mappings */ int dm_linear_init (void );void dm_linear_exit (void );
@@ -167,4 +167,9 @@int dm_stripe_init (void );void dm_stripe_exit (void ); +#ifdef DM_TARGET_CRYPT +int dm_crypt_init (void ); +void dm_crypt_exit (void ); +#endif +#endif
drivers/md/Kconfig +10 -0
@@ -152,5 +152,15 @@ Recent tools use a new version of the ioctl interface, only select this option if you intend using such tools. +config BLK_DM_CRYPT + tristate "dm-crypt target" + depends on BLK_DEV_DM && EXPERIMENTAL + select CRYPTO + ---help--- + This device-mapper target allows you to create a device that + transparently encrypts the data on it. You'll need to activate + the required ciphers in the cryptoapi configuration in order to + be able to use it. + endmenu
drivers/md/Makefile +2 -1
@@ -3,7 +3,7 @@ # dm-mod-objs := dm.o dm-table.o dm-target.o dm-linear.o dm-stripe.o \ - dm-ioctl.o + dm-ioctl.o dm-daemon.o # Note: link order is important. All raid personalities # and xor.o must come before md.o, as they each initialise
@@ -17,3 +17,4 @@ obj-$(CONFIG_MD_MULTIPATH) += multipath.o obj-$(CONFIG_BLK_DEV_MD) += md.o obj-$(CONFIG_BLK_DEV_DM) += dm-mod.o +obj-$(CONFIG_BLK_DM_CRYPT) += dm-crypt.o