Skip to contentSkip to conversation

Encrypted logical volumes for LVM2

RFC PATCH New Milan Broz 1 participant 16 messages Jan 21, 2009 Library code

lore
Share & tools
lore

Milan Broz

author
#0 lore raw

cover Encrypted logical volumes for LVM2

This patch series implements encrypted volume handling in LVM2.

Not all functions are finished but it should provide basic design overview for review and discussion.

(I know that patches are big, but I see no better alternative for posting...)

Milan

Basic idea is provide standard encrypted volume management in LVM2 but keep key management (and userspace crypto handling) outside lvm core.

Then we can support simple key management (equivalent to cryptsetup/luks) but also library for connecting to some infrastructure. The metadata format is still the same.

This patches implements these subsystems, needed for correct operation:
(see particular patches for description, this is just short overview)

- new "crypt" and "crypt-keystore" lv segments (crypt is equivalent to linear segment, just with encrypted areas, crypt-keystore is area which include special metadata interpreted by particular driver - e.g. LUKS metadata area)

- key handlers interface, for now
"simple" handler
"luks1" handler

- internal master key cache (lvm need temporarily store master key used in dm-crypt maping table for various mapping table operations)

- encrypted LVs & cryptostore LVs manipulation code

- lvcreate support, lvconvert to allow importing already prepared encrypted volumes

- some helper functions (like password entry etc.) (is linking to other library worth to replace these?)

Key management inside lvm

For manipulating with volume inside kernel device-mapper, lvm needs to know master key and cipher parameters.

Crypt store says which method (crytpto store type) is used to obtain these attributes, key handler implement these methods (resp. should provide interface to proper tools for key management).

Examples of Key handlers:

- The simple one is "plain" handler, where the used cipher is stored
in LVM metadata directly and driver asks user for master key.

- Second one (not yet implemented) is "hashed" where the key is
hashed value of user entered password.
(equivalent of crypsetup in non-luks mode).

- More sophisticated is use some key management system, and simple example is support for "LUKS".
(here equivalent of cryptsetup-luks keystore)

- For enterprise segment, it need implement key handler which will communicate with an infrastructure to obtain encryption parameters and key.

...

So the key handlers should be separated from basic lvm core, I expect that it should be dynamically loaded library [RFC]. (library loading is not yet implemented in patchset, but code should be already separate modules)

Metadata representation:

- everything new is in crypt and crypt-keystore segment, so even old code should read partial metadata properly, just ignoring these segments.

- new "crypt" segment has the same format as linear, just it adds "crypto_store" attribute, which is name of special LV withing the VG.
Only one area is allowed in this segment (the same like linear) (it uses "areas" instead of "stripes" in segment metadata)

- special cryptostore LV containing "crypt-keystore" segment. Here is stored information how to obtian crypt attributes needed for encrypted LV activation

- crypostore LV can be shared between encrypted segments

- the special name "cryptostoreN" is now reserved

- it defines some basic metadata atttributes
(handler name, cipher, key_size, key_hash)
which are retained (if present) even if particular key handler is not loaded.
(Basically this concept allows manipulation with metadata in some rescue mode, but without specialized key handlers loaded.)

- encrypted LV can consist of several encrypted segments, In normal situation all segments will use the same cryptostore.
But it should support online reencryption in future (in this situation it temporary uses different crypto stores for segments).

- cryptostore LV is special, invisible LV with only crypt-keystore segments. To simplify code, if the last lv segment referencing this LV is removed, cryptostore LV is removed too.

Best to see a METADATA example:

vg { ... # no change here

	logical_volumes {

		# simple encrypted volume
		tst {
			id = "ONqIFB-p2vX-rQ2V-B9Yg-0kPB-uKbh-NWIssL"
			status = ["READ", "WRITE", "VISIBLE"]
			flags = []
			segment_count = 1

			segment1 {
				start_extent = 0
				extent_count = 4        # 16 Megabytes

				type = "crypt"
				crypto_store = "cryptostore1"

				areas = [
					"pv2", 12
				]
			}
		}

		# imported LUKS1 volume
		luks1 {
			id = "G0NURa-cD93-6VnU-zHGP-Affz-Hu8Q-3MU1vt"
			status = ["READ", "WRITE", "VISIBLE"]
			flags = []
			segment_count = 1

			segment1 {
				start_extent = 0
				extent_count = 3        # 12 Megabytes

				type = "crypt"
				crypto_store = "cryptostore2"

				areas = [
					"pv0", 4
				]
			}
		}

		# plain cryptostore
		cryptostore1 {
			id = "5WYknM-Vhti-pGqU-yIN7-UgY1-I3KW-Hnmb08"
			status = ["READ"]
			flags = []
			allocation_policy = "normal"
			segment_count = 1

			segment1 {
				start_extent = 0
				extent_count = 0        # 0 Kilobytes

				type = "crypt-keystore"
				handler = "plain"
				cipher = "aes-xts-plain"
				key_size = 256  # 32 Bytes
			}
		}

		# LUKS1 cryptostore
		cryptostore2 {
			id = "qbo213-gxbu-NeI3-n63K-vnOT-JtNB-RXmOUk"
			status = ["READ"]
			flags = []
			allocation_policy = "normal"
			segment_count = 1

			segment1 {
				start_extent = 0
				extent_count = 1        # 4 Megabytes

				type = "crypt-keystore"
				handler = "luks1"

				# LUKS keyslots live here
				areas = [
					"pv0", 3
				]
			}
		}
	}
}

This is the (huge) patchset statistic :-)

Milan Broz (15):
Support crypt segment in libdevmapper tree.
Add lvm-crypto header file with basic crypto struct definintions:
Add master key cache to LVM.
Add simple password helpers.
Add key store handlers machinery.
Prepare source for recognising crypt segment.
Add "crypt" and "crypt-keystore" segment implementation. Add crypto_store to LV segment allocation functions. Add encrypted LV manipulation functions.
Cache key from crypt mapping table if segment is active. Add lvcreate crypto LV implementation.
Add lvconcert crypt implementation.
Add --keyfile option.
Add *testing* LUKS1 keystore implementation.
Add simple test for crypto volumes.

 configure                        |  131 ++++++++++-
 configure.in                     |   51 ++++
 include/.symlinks                |    1 +
 lib/Makefile.in                  |   14 +
 lib/activate/activate.c          |   47 ++++
 lib/activate/activate.h          |    5 +
 lib/activate/dev_manager.c       |   59 +++++
 lib/activate/dev_manager.h       |    4 +
 lib/commands/toolcontext.c       |   37 +++
 lib/crypt/crypt.c                |  372 ++++++++++++++++++++++++++++
 lib/crypt/key_handlers.c         |   88 +++++++
 lib/crypt/key_luks.c             |  495 ++++++++++++++++++++++++++++++++++++++
 lib/crypt/lvm-crypto.h           |  126 ++++++++++
 lib/crypt/masterkey.c            |  152 ++++++++++++
 lib/crypt/password.c             |  127 ++++++++++
 lib/crypt/pbkdf2.c               |  199 +++++++++++++++
 lib/crypt/pbkdf2.h               |   36 +++
 lib/format1/format1.c            |    1 +
 lib/format1/import-extents.c     |    4 +-
 lib/format_pool/format_pool.c    |    1 +
 lib/format_pool/import_export.c  |    4 +-
 lib/format_text/import_vsn1.c    |    3 +-
 lib/metadata/crypt_manip.c       |  266 ++++++++++++++++++++
 lib/metadata/lv_alloc.h          |    3 +-
 lib/metadata/lv_manip.c          |   29 ++-
 lib/metadata/merge.c             |   32 +++-
 lib/metadata/metadata-exported.h |   20 ++
 lib/metadata/metadata.c          |    2 +
 lib/metadata/segtype.h           |    6 +
 lib/misc/configure.h.in          |    6 +
 libdm/.exported_symbols          |    1 +
 libdm/libdevmapper.h             |    4 +
 libdm/libdm-deptree.c            |   31 +++-
 test/t-crypto-usage.sh           |   54 ++++
 tools/args.h                     |    5 +
 tools/commands.h                 |   30 ++-
 tools/lvchange.c                 |    7 +
 tools/lvconvert.c                |  125 ++++++++++
 tools/lvcreate.c                 |  167 +++++++++++++-
 tools/toollib.c                  |    6 +
 tools/vgchange.c                 |    7 +
 41 files changed, 2723 insertions(+), 35 deletions(-)
 create mode 100644 lib/crypt/crypt.c
 create mode 100644 lib/crypt/key_handlers.c
 create mode 100644 lib/crypt/key_luks.c
 create mode 100644 lib/crypt/lvm-crypto.h
 create mode 100644 lib/crypt/masterkey.c
 create mode 100644 lib/crypt/password.c
 create mode 100644 lib/crypt/pbkdf2.c
 create mode 100644 lib/crypt/pbkdf2.h
 create mode 100644 lib/metadata/crypt_manip.c
 create mode 100755 test/t-crypto-usage.sh
author
#1 lore raw

PATCH 1/15 Support crypt segment in libdevmapper tree.

- it can support multiple segments, but note that
to work properly, correct IV (initialization vector) offset parameter must be set properly.

Both cipher and key must be supplied in correct dm-crypt format,

---
 libdm/.exported_symbols |    1 +
 libdm/libdevmapper.h    |    4 ++++
 libdm/libdm-deptree.c   |   31 ++++++++++++++++++++++++++++---
 3 files changed, 33 insertions(+), 3 deletions(-)
libdm/.exported_symbols +1 -0
index a6e04f8..f8dba1b 100644
@@ -66,6 +66,7 @@ dm_tree_node_add_error_target
 dm_tree_node_add_zero_target
 dm_tree_node_add_linear_target
 dm_tree_node_add_striped_target
+dm_tree_node_add_crypt_target
 dm_tree_node_add_mirror_target
 dm_tree_node_add_mirror_target_log
 dm_tree_node_add_target_area
libdm/libdevmapper.h +4 -0
index 93aecbb..775b4ec 100644
@@ -373,6 +373,10 @@ int dm_tree_node_add_linear_target(struct dm_tree_node *node,
 int dm_tree_node_add_striped_target(struct dm_tree_node *node,
 				       uint64_t size,
 				       uint32_t stripe_size);
+int dm_tree_node_add_crypt_target(struct dm_tree_node *node,
+				  uint64_t size,
+				  const char *cipher,
+				  const char *key);
 int dm_tree_node_add_mirror_target(struct dm_tree_node *node,
 				      uint64_t size);
  
libdm/libdm-deptree.c +28 -3
index 11428ca..310b30e 100644
@@ -28,7 +28,8 @@
 
 /* Supported segment types */
 enum {
-	SEG_ERROR, 
+	SEG_CRYPT,
+	SEG_ERROR,
 	SEG_LINEAR,
 	SEG_MIRRORED,
 	SEG_SNAPSHOT,
@@ -43,6 +44,7 @@ struct {
 	unsigned type;
 	const char *target;
 } dm_segtypes[] = {
+	{ SEG_CRYPT, "crypt" },
 	{ SEG_ERROR, "error" },
 	{ SEG_LINEAR, "linear" },
 	{ SEG_MIRRORED, "mirror" },
@@ -69,8 +71,8 @@ struct load_segment {
 
 	uint64_t size;
 
-	unsigned area_count;		/* Linear + Striped + Mirrored */
-	struct dm_list areas;		/* Linear + Striped + Mirrored */
+	unsigned area_count;		/* Linear + Striped + Mirrored + Crypt */
+	struct dm_list areas;		/* Linear + Striped + Mirrored + Crypt */
 
 	uint32_t stripe_size;		/* Striped */
 
@@ -85,6 +87,9 @@ struct load_segment {
 	unsigned mirror_area_count;	/* Mirror */
 	uint32_t flags;			/* Mirror log */
 	char *uuid;			/* Clustered mirror log */
+
+	const char *cipher;		/* Crypt */
+	const char *key;		/* Crypt */
 };
 
 /* Per-device properties */
@@ -1323,6 +1328,9 @@ static int _emit_segment_line(struct dm_task *dmt, struct load_segment *seg, uin
 	case SEG_STRIPED:
 		EMIT_PARAMS(pos, "%u %u", seg->area_count, seg->stripe_size);
 		break;
+	case SEG_CRYPT:
+		EMIT_PARAMS(pos, "%s %s %" PRIu64, seg->cipher, seg->key, *seg_start);
+		break;
 	}
 
 	switch(seg->type) {
@@ -1331,6 +1339,7 @@ static int _emit_segment_line(struct dm_task *dmt, struct load_segment *seg, uin
 	case SEG_SNAPSHOT_ORIGIN:
 	case SEG_ZERO:
 		break;
+	case SEG_CRYPT:
 	case SEG_LINEAR:
 	case SEG_MIRRORED:
 	case SEG_STRIPED:
@@ -1664,6 +1673,22 @@ int dm_tree_node_add_striped_target(struct dm_tree_node *node,
 	return 1;
 }
 
+int dm_tree_node_add_crypt_target(struct dm_tree_node *node,
+				  uint64_t size,
+				  const char *cipher,
+				  const char *key)
+{
+	struct load_segment *seg;
+
+	if (!(seg = _add_segment(node, SEG_CRYPT, size)))
+		return_0;
+
+	seg->cipher = cipher;
+	seg->key = key;
+
+	return 1;
+}
+
 int dm_tree_node_add_mirror_target_log(struct dm_tree_node *node,
 					  uint32_t region_size,
 					  unsigned clustered, 
Signature · 1 line
1.5.6.5
author
#2 lore raw

PATCH 2/15 Add lvm-crypto header file with basic crypto struct definintions:

- crypto_store_type it the crypto equivalent for segment type, it says how the crypto attributes are retrieved.
In future there will be separate libraries
which can provide various key stores.

- crypto_store represents particular crypto store. Every crypt lv_segment must be linked to some crypto_store. Basically it says "how the cipher attributes and key" can be obtained for this segment.
crypto_store can be shared by several LV segments.

crypto_store can have special areas on disk (where it stores key material in some obfuscatted form - e.g. LUKS slots).

If there is no area on disk, the cipher & key parameters can be stored directly in metadata (or provided through other interface - depends on crypto_store_type.)

There are hardcoded attributes, which are mainly for supporting basic crypto segment operations (compatible mappings created by cryptsetup in non-luks mode) (see following patches).

The keystore is identified by UUID (~= LV UUID) (Becuse crypto_store is represented by special LV,
it is real LV UUID internally.)

---
 include/.symlinks                |    1 +
 lib/crypt/lvm-crypto.h           |   65 ++++++++++++++++++++++++++++++++++++++
 lib/format1/format1.c            |    1 +
 lib/format_pool/format_pool.c    |    1 +
 lib/format_text/import_vsn1.c    |    1 +
 lib/metadata/metadata-exported.h |    5 +++
 lib/metadata/metadata.c          |    2 +
 7 files changed, 76 insertions(+), 0 deletions(-)
 create mode 100644 lib/crypt/lvm-crypto.h
include/.symlinks +1 -0
index 1a4bd93..4033e18 100644
@@ -4,6 +4,7 @@
 ../lib/activate/activate.h
 ../lib/activate/targets.h
 ../lib/cache/lvmcache.h
+../lib/crypt/lvm-crypto.h
 ../lib/commands/errors.h
 ../lib/commands/toolcontext.h
 ../lib/config/config.h
lib/crypt/lvm-crypto.h new file +65 -0

65 lines not on this page · raw message

lib/format1/format1.c +1 -0
index 8ab9363..a9ed088 100644
@@ -130,6 +130,7 @@ static struct volume_group *_build_vg(struct format_instance *fid,
 	vg->seqno = 0;
 	dm_list_init(&vg->pvs);
 	dm_list_init(&vg->lvs);
+	dm_list_init(&vg->crypto_stores);
 	dm_list_init(&vg->tags);
 
 	if (!_check_vgs(pvs))
lib/format_pool/format_pool.c +1 -0
index 0f72bd3..53c995f 100644
@@ -124,6 +124,7 @@ static struct volume_group *_build_vg_from_pds(struct format_instance
 	vg->system_id = NULL;
 	dm_list_init(&vg->pvs);
 	dm_list_init(&vg->lvs);
+	dm_list_init(&vg->crypto_stores);
 	dm_list_init(&vg->tags);
 
 	if (!import_pool_vg(vg, smem, pds))
lib/format_text/import_vsn1.c +1 -0
index b7d06af..b610501 100644
@@ -768,6 +768,7 @@ static struct volume_group *_read_vg(struct format_instance *fid,
 
 	dm_list_init(&vg->lvs);
 	dm_list_init(&vg->tags);
+	dm_list_init(&vg->crypto_stores);
 
 	/* Optional tags */
 	if ((cn = find_config_node(vgn, "tags")) &&
lib/metadata/metadata-exported.h +5 -0
index 40ece6f..16c216a 100644
@@ -22,6 +22,7 @@
 #define _LVM_METADATA_EXPORTED_H
 
 #include "uuid.h"
+#include "lvm-crypto.h"
 
 struct physical_volume;
 typedef struct physical_volume pv_t;
@@ -231,6 +232,8 @@ struct volume_group {
 	uint32_t snapshot_count;
 	struct dm_list lvs;
 
+	struct dm_list crypto_stores;
+
 	struct dm_list tags;
 };
 
@@ -271,6 +274,8 @@ struct lv_segment {
 	uint32_t extents_copied;
 	struct logical_volume *log_lv;
 
+	struct crypto_store *crypto_store;
+
 	struct dm_list tags;
 
 	struct lv_segment_area *areas;
lib/metadata/metadata.c +2 -0
index a49c4d8..cb5dcbd 100644
@@ -546,6 +546,7 @@ struct volume_group *vg_create(struct cmd_context *cmd, const char *vg_name,
 
 	vg->snapshot_count = 0;
 
+	dm_list_init(&vg->crypto_stores);
 	dm_list_init(&vg->tags);
 
 	if (!(vg->fid = cmd->fmt->ops->create_instance(cmd->fmt, vg_name,
@@ -1617,6 +1618,7 @@ static struct volume_group *_vg_read_orphans(struct cmd_context *cmd,
 	}
 	dm_list_init(&vg->pvs);
 	dm_list_init(&vg->lvs);
+	dm_list_init(&vg->crypto_stores);
 	dm_list_init(&vg->tags);
 	vg->cmd = cmd;
 	if (!(vg->name = dm_pool_strdup(cmd->mem, orphan_vgname))) {
Signature · 1 line
1.5.6.5
author
#3 lore raw

PATCH 3/15 Add master key cache to LVM.

Master key is key used directly in crypt segment mapping table.

Because LVM performs various magic with mapping table, we need ask user (for providing master key unlocking information) only once and during the lvm operation cache the master key.

Master key cache provides such temporary store area.

Key is stored directly in dm-crypt format in hash table, key for hash table is crypt_store UUID.

The memory for storing key is allocated using dm_malloc and must be always wiped before releasing.

Master key interface provides these functions:
- init/destroy the whole cache
- insert/remove key from cache using its ID
- query for key

Also this patch provides two helper functions:
lvm_masterkeys_verify - verifies that key is in expected format & length lvm_masterkeys_retrieve - calls the retrieve function
in crypto store if key is not already cached

---
 lib/Makefile.in            |    1 +
 lib/commands/toolcontext.c |   21 ++++++
 lib/crypt/lvm-crypto.h     |   22 +++++++
 lib/crypt/masterkey.c      |  149 ++++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 193 insertions(+), 0 deletions(-)
 create mode 100644 lib/crypt/masterkey.c
lib/Makefile.in +1 -0
index 54092cd..9705580 100644
@@ -37,6 +37,7 @@ SOURCES =\
 	cache/lvmcache.c \
 	commands/toolcontext.c \
 	config/config.c \
+	crypt/masterkey.c \
 	datastruct/btree.c \
 	datastruct/str_list.c \
 	device/dev-cache.c \
lib/commands/toolcontext.c +21 -0
index 0a98325..26a4c83 100644
@@ -997,6 +997,22 @@ static void _init_globals(struct cmd_context *cmd)
 
 }
 
+static int _init_crypto(struct cmd_context *cmd)
+{
+#ifdef CRYPT_INTERNAL
+	if (!lvm_masterkeys_init())
+		return 0;
+#endif
+	return 1;
+}
+
+static void _destroy_crypto(struct cmd_context *cmd)
+{
+#ifdef CRYPT_INTERNAL
+	lvm_masterkeys_destroy();
+#endif
+}
+
 /* Entry point */
 struct cmd_context *create_toolcontext(unsigned is_long_lived)
 {
@@ -1092,6 +1108,9 @@ struct cmd_context *create_toolcontext(unsigned is_long_lived)
 	if (!_init_backup(cmd))
 		goto error;
 
+	if (!_init_crypto(cmd))
+		goto error;
+
 	_init_rand(cmd);
 
 	_init_globals(cmd);
@@ -1164,6 +1183,7 @@ int refresh_toolcontext(struct cmd_context *cmd)
 	dev_cache_exit();
 	_destroy_tags(cmd);
 	_destroy_tag_configs(cmd);
+	_destroy_crypto(cmd);
 
 	cmd->config_valid = 0;
 
@@ -1228,6 +1248,7 @@ void destroy_toolcontext(struct cmd_context *cmd)
 	dev_cache_exit();
 	_destroy_tags(cmd);
 	_destroy_tag_configs(cmd);
+	_destroy_crypto(cmd);
 	dm_pool_destroy(cmd->libmem);
 	dm_free(cmd);
 
lib/crypt/lvm-crypto.h +22 -0
index 7ce3941..9690779 100644
@@ -19,6 +19,15 @@
 #include "uuid.h"
 
 /*
+ * Master key is stored in *text* hexa string, in direct dm-crypt format
+ */
+typedef const char *masterkey_t;
+
+#define cs_handler_name(cs)     ((cs)->type ? (cs)->type->name : (cs)->type_name)
+
+#define cs_key_bytes(cs)        ((cs)->key_size ? ((cs)->key_size * 2 / 8) : 4095)
+
+/*
  * Crypto config & key store
  */
 struct crypto_store_type {
@@ -62,4 +71,17 @@ struct crypto_store_ops {
 	// FIXME: key management, area formatting functions, etc
 };
 
+/*
+ * Master Key cache
+ */
+int lvm_masterkeys_init(void);
+void lvm_masterkeys_destroy(void);
+
+int lvm_masterkeys_insert(const struct id *id, masterkey_t key);
+void lvm_masterkeys_remove(const struct id *id);
+masterkey_t lvm_masterkeys_query(const struct id *id);
+
+int lvm_masterkeys_verify(struct crypto_store *cs, const char *key);
+int lvm_masterkeys_retrieve(const char *for_text, struct crypto_store *cs);
+
 #endif
lib/crypt/masterkey.c new file +149 -0

149 lines not on this page · raw message

Signature · 1 line
1.5.6.5
author
#4 lore raw

PATCH 4/15 Add simple password helpers.

These enable to read password from
- terminal
- standard input
- file

(Key store handlers will use this.)

(Alternative is use some exported funtion provided by some crypto library but I think we should not rely here on external library.)

---
 lib/Makefile.in        |    1 +
 lib/crypt/lvm-crypto.h |    6 ++
 lib/crypt/masterkey.c  |    3 +
 lib/crypt/password.c   |  127 ++++++++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 137 insertions(+), 0 deletions(-)
 create mode 100644 lib/crypt/password.c
lib/Makefile.in +1 -0
index 9705580..d7b2bf2 100644
@@ -38,6 +38,7 @@ SOURCES =\
 	commands/toolcontext.c \
 	config/config.c \
 	crypt/masterkey.c \
+	crypt/password.c \
 	datastruct/btree.c \
 	datastruct/str_list.c \
 	device/dev-cache.c \
lib/crypt/lvm-crypto.h +6 -0
index 9690779..1a7825d 100644
@@ -84,4 +84,10 @@ masterkey_t lvm_masterkeys_query(const struct id *id);
 int lvm_masterkeys_verify(struct crypto_store *cs, const char *key);
 int lvm_masterkeys_retrieve(const char *for_text, struct crypto_store *cs);
 
+/*
+ * Crypto Helper functions
+ */
+int lvm_set_password_dev(const char *password_file);
+int lvm_read_password(char *message, char *pass, size_t len);
+
 #endif
lib/crypt/masterkey.c +3 -0
index 6efefcd..c3870ca 100644
@@ -126,6 +126,7 @@ out:
 
 int lvm_masterkeys_init()
 {
+	(void)lvm_set_password_dev(NULL);
 	//FIXME: clear keys?
 	if (_key_hash)
 		return 1;
@@ -140,6 +141,8 @@ int lvm_masterkeys_init()
 
 void lvm_masterkeys_destroy()
 {
+	(void)lvm_set_password_dev(NULL);
+
 	if (_key_hash) {
 		log_debug("Destroying master key cache (%u).", dm_hash_get_num_entries(_key_hash));
 		dm_hash_iter(_key_hash, (dm_hash_iterate_fn) _masterkeys_destroy_entry);
lib/crypt/password.c new file +127 -0

127 lines not on this page · raw message

Signature · 1 line
1.5.6.5
author
#5 lore raw

PATCH 5/15 Add key store handlers machinery.

This code should load various drivers for manipulating with key for volumes.

Here will be interface between LVM (for volume management) and key management (and related security code).

Currently it only maintains the list of key handlers and provides search for key handler in this list.

Also implements manadatory key handler "simple".
- it stores cipher & keysize in LVM metadata directly
- asks for Master Key (no processig here)

This is direct interface to dm-crypt mapping, and it does not require any cryptography code in userspace.

---
 lib/Makefile.in            |    1 +
 lib/commands/toolcontext.c |    3 ++
 lib/crypt/key_handlers.c   |   85 ++++++++++++++++++++++++++++++++++++++++++++
 lib/crypt/lvm-crypto.h     |   25 ++++++++++++-
 4 files changed, 112 insertions(+), 2 deletions(-)
 create mode 100644 lib/crypt/key_handlers.c
lib/Makefile.in +1 -0
index d7b2bf2..41395ed 100644
@@ -37,6 +37,7 @@ SOURCES =\
 	cache/lvmcache.c \
 	commands/toolcontext.c \
 	config/config.c \
+	crypt/key_handlers.c \
 	crypt/masterkey.c \
 	crypt/password.c \
 	datastruct/btree.c \
lib/commands/toolcontext.c +3 -0
index 26a4c83..f9a0b13 100644
@@ -1002,6 +1002,8 @@ static int _init_crypto(struct cmd_context *cmd)
 #ifdef CRYPT_INTERNAL
 	if (!lvm_masterkeys_init())
 		return 0;
+	if (!lvm_keystore_handlers_init())
+		return 0;
 #endif
 	return 1;
 }
@@ -1010,6 +1012,7 @@ static void _destroy_crypto(struct cmd_context *cmd)
 {
 #ifdef CRYPT_INTERNAL
 	lvm_masterkeys_destroy();
+	lvm_keystore_handlers_destroy();
 #endif
 }
 
lib/crypt/key_handlers.c new file +85 -0

85 lines not on this page · raw message

lib/crypt/lvm-crypto.h +23 -2
index 1a7825d..3ebddce 100644
@@ -23,9 +23,23 @@
  */
 typedef const char *masterkey_t;
 
-#define cs_handler_name(cs)     ((cs)->type ? (cs)->type->name : (cs)->type_name)
+/*
+ * Flags says if attribute is stored in keystore area and not in LVM metadata.
+ * By default, all generic attributes in LVM metadata must be retained
+ * (for example when some keystore handler is not loaded in rescue mode).
+ */
+#define CS_IGNORE_CIPHER	0x00000001U
+#define CS_IGNORE_KEYLEN	0x00000002U
+#define CS_IGNORE_KEYHASH	0x00000004U
+
+#define cs_flags(cs)		((cs)->type ? (cs)->type->flags : 0)
+#define cs_handler_name(cs)	((cs)->type ? (cs)->type->name : (cs)->type_name)
+
+#define cs_store_cipher(cs)	(cs_flags(cs) & CS_IGNORE_CIPHER ? 0 : 1)
+#define cs_store_keylen(cs)	(cs_flags(cs) & CS_IGNORE_KEYLEN ? 0 : 1)
+#define cs_store_keyhash(cs)	(cs_flags(cs) & CS_IGNORE_KEYHASH ? 0 : 1)
 
-#define cs_key_bytes(cs)        ((cs)->key_size ? ((cs)->key_size * 2 / 8) : 4095)
+#define cs_key_bytes(cs)	((cs)->key_size ? ((cs)->key_size * 2 / 8) : 4095)
 
 /*
  * Crypto config & key store
@@ -85,6 +99,13 @@ int lvm_masterkeys_verify(struct crypto_store *cs, const char *key);
 int lvm_masterkeys_retrieve(const char *for_text, struct crypto_store *cs);
 
 /*
+ * Key Store handlers manipulation
+ */
+int lvm_keystore_handlers_init(void);
+void lvm_keystore_handlers_destroy(void);
+struct crypto_store_type *lvm_get_keystore_handler(const char *name);
+
+/*
  * Crypto Helper functions
  */
 int lvm_set_password_dev(const char *password_file);
Signature · 1 line
1.5.6.5
author
#6 lore raw

PATCH 6/15 Prepare source for recognising crypt segment.

- modify autoconf script to define CRYPTO (supporting currently only --with-crypto=none/internal) and add CRYPT_INTERNAL define

- prepare empty segtype constructors
for segtype "crypt" (encrypted area)
for segtype "crypt-keystore" (crypto metadata area)

- define new encrypted segment flag and prepare
macros to work with it.

---
 configure                  |   39 +++++++++++++++++++++++++++++++++++++--
 configure.in               |   21 +++++++++++++++++++++
 lib/Makefile.in            |    4 ++++
 lib/commands/toolcontext.c |   13 +++++++++++++
 lib/crypt/crypt.c          |   44 ++++++++++++++++++++++++++++++++++++++++++++
 lib/metadata/segtype.h     |    6 ++++++
 lib/misc/configure.h.in    |    3 +++
 7 files changed, 128 insertions(+), 2 deletions(-)
 create mode 100644 lib/crypt/crypt.c
configure +37 -2
index 8095090..e4a8f82 100755
@@ -699,6 +699,7 @@ CLUSTER
 CLVMD
 CMDLIB
 COPTIMISE_FLAG
+CRYPTO
 DEBUG
 DEVMAPPER
 DMEVENTD
@@ -1360,6 +1361,8 @@ Optional Packages:
                           TYPE=internal
   --with-mirrors=TYPE     Mirror support: internal/shared/none
                           TYPE=internal
+  --with-crypto=TYPE      Crypto support: internal/none
+                          TYPE=internal
   --with-clvmd=TYPE       Build cluster LVM Daemon: cman/gulm/none/all
                           TYPE=none
   --with-optimisation=OPT C optimisation flag [OPT=-O2]
@@ -8663,6 +8666,36 @@ _ACEOF
 fi
 
 ################################################################################
+{ echo "$as_me:$LINENO: checking whether to include crypto support" >&5
+echo $ECHO_N "checking whether to include crypto support... $ECHO_C" >&6; }
+
+# Check whether --with-crypto was given.
+if test "${with_crypto+set}" = set; then
+  withval=$with_crypto;  CRYPTO="$withval"
+else
+   CRYPTO="internal"
+fi
+
+{ echo "$as_me:$LINENO: result: $CRYPTO" >&5
+echo "${ECHO_T}$CRYPTO" >&6; }
+
+if [ "x$CRYPTO" != xnone -a "x$CRYPTO" != xinternal ];
+ then  { { echo "$as_me:$LINENO: error: --with-crypto parameter invalid
+" >&5
+echo "$as_me: error: --with-crypto parameter invalid
+" >&2;}
+   { (exit 1); exit 1; }; }
+fi;
+
+if test x$CRYPTO = xinternal; then
+
+cat >>confdefs.h <<\_ACEOF
+#define CRYPT_INTERNAL 1
+_ACEOF
+
+fi
+
+################################################################################
 { echo "$as_me:$LINENO: checking whether to enable readline" >&5
 echo $ECHO_N "checking whether to enable readline... $ECHO_C" >&6; }
 # Check whether --enable-readline was given.
@@ -11719,6 +11752,7 @@ LVM_VERSION="\"`cat VERSION 2>/dev/null || echo Unknown`\""
 
 
 
+
 ################################################################################
 ac_config_files="$ac_config_files Makefile make.tmpl daemons/Makefile daemons/clvmd/Makefile daemons/dmeventd/Makefile daemons/dmeventd/libdevmapper-event.pc daemons/dmeventd/plugins/Makefile daemons/dmeventd/plugins/mirror/Makefile daemons/dmeventd/plugins/snapshot/Makefile doc/Makefile include/Makefile lib/Makefile lib/format1/Makefile lib/format_pool/Makefile lib/locking/Makefile lib/mirror/Makefile lib/snapshot/Makefile libdm/Makefile libdm/libdevmapper.pc man/Makefile po/Makefile scripts/clvmd_init_red_hat scripts/Makefile test/Makefile test/api/Makefile tools/Makefile tools/version.h"
 
@@ -12450,6 +12484,7 @@ CLUSTER!$CLUSTER$ac_delim
 CLVMD!$CLVMD$ac_delim
 CMDLIB!$CMDLIB$ac_delim
 COPTIMISE_FLAG!$COPTIMISE_FLAG$ac_delim
+CRYPTO!$CRYPTO$ac_delim
 DEBUG!$DEBUG$ac_delim
 DEVMAPPER!$DEVMAPPER$ac_delim
 DMEVENTD!$DMEVENTD$ac_delim
@@ -12461,7 +12496,6 @@ DM_IOCTLS!$DM_IOCTLS$ac_delim
 DM_LIB_VERSION!$DM_LIB_VERSION$ac_delim
 DM_LIB_PATCHLEVEL!$DM_LIB_PATCHLEVEL$ac_delim
 FSADM!$FSADM$ac_delim
-GROUP!$GROUP$ac_delim
 _ACEOF
 
   if test `sed -n "s/.*$ac_delim\$/X/p" conf$$subs.sed | grep -c X` = 97; then
@@ -12503,6 +12537,7 @@ _ACEOF
 ac_delim='%!_!# '
 for ac_last_try in false false false false false :; do
   cat >conf$$subs.sed <<_ACEOF
+GROUP!$GROUP$ac_delim
 HAVE_LIBDL!$HAVE_LIBDL$ac_delim
 HAVE_REALTIME!$HAVE_REALTIME$ac_delim
 HAVE_SELINUX!$HAVE_SELINUX$ac_delim
@@ -12534,7 +12569,7 @@ usrsbindir!$usrsbindir$ac_delim
 LTLIBOBJS!$LTLIBOBJS$ac_delim
 _ACEOF
 
-  if test `sed -n "s/.*$ac_delim\$/X/p" conf$$subs.sed | grep -c X` = 29; then
+  if test `sed -n "s/.*$ac_delim\$/X/p" conf$$subs.sed | grep -c X` = 30; then
     break
   elif $ac_last_try; then
     { { echo "$as_me:$LINENO: error: could not make $CONFIG_STATUS" >&5
configure.in +21 -0
index 5d28c2c..a5e9236 100644
@@ -306,6 +306,26 @@ if test x$MIRRORS = xinternal; then
 fi
 
 ################################################################################
+dnl -- crypt inclusion type
+AC_MSG_CHECKING(whether to include crypto support)
+AC_ARG_WITH(crypto,
+  [  --with-crypto=TYPE      Crypto support: internal/none
+                          [TYPE=internal] ],
+  [ CRYPTO="$withval" ],
+  [ CRYPTO="internal" ])
+AC_MSG_RESULT($CRYPTO)
+
+if [[ "x$CRYPTO" != xnone -a "x$CRYPTO" != xinternal ]];
+ then  AC_MSG_ERROR(
+--with-crypto parameter invalid
+)
+fi;
+
+if test x$CRYPTO = xinternal; then
+	AC_DEFINE([CRYPT_INTERNAL], 1, [Define to 1 to include built-in support for crypto.])
+fi
+
+################################################################################
 dnl -- Disable readline
 AC_MSG_CHECKING(whether to enable readline)
 AC_ARG_ENABLE([readline],
@@ -734,6 +754,7 @@ AC_SUBST(CLUSTER)
 AC_SUBST(CLVMD)
 AC_SUBST(CMDLIB)
 AC_SUBST(COPTIMISE_FLAG)
+AC_SUBST(CRYPTO)
 AC_SUBST(CSCOPE_CMD)
 AC_SUBST(DEBUG)
 AC_SUBST(DEVMAPPER)
lib/Makefile.in +4 -0
index 41395ed..7971395 100644
@@ -126,6 +126,10 @@ ifeq ("@MIRRORS@", "internal")
   SOURCES += mirror/mirrored.c
 endif
 
+ifeq ("@CRYPTO@", "internal")
+  SOURCES += crypt/crypt.c
+endif
+
 ifeq ("@DEVMAPPER@", "yes")
   SOURCES +=\
 	activate/dev_manager.c \
lib/commands/toolcontext.c +13 -0
index f9a0b13..af7643b 100644
@@ -846,6 +846,19 @@ static int _init_segtypes(struct cmd_context *cmd)
 	dm_list_add(&cmd->segtypes, &segtype->list);
 #endif
 
+#ifdef CRYPT_INTERNAL
+	if (!(segtype = init_crypt_segtype(cmd)))
+		return 0;
+	segtype->library = NULL;
+	dm_list_add(&cmd->segtypes, &segtype->list);
+
+	/* Keystore must be supported if crypt segment is present */
+	if (!(segtype = init_keystore_segtype(cmd)))
+		return 0;
+	segtype->library = NULL;
+	dm_list_add(&cmd->segtypes, &segtype->list);
+#endif
+
 #ifdef HAVE_LIBDL
 	/* Load any formats in shared libs unless static */
 	if (!is_static() &&
lib/crypt/crypt.c new file +44 -0

44 lines not on this page · raw message

lib/metadata/segtype.h +6 -0
index ad274a3..15f4b5b 100644
@@ -33,6 +33,7 @@ struct dev_manager;
 #define SEG_VIRTUAL		0x00000020U
 #define SEG_CANNOT_BE_ZEROED	0x00000040U
 #define SEG_MONITORED		0x00000080U
+#define SEG_AREAS_ENCRYPTED	0x00000100U
 
 #define seg_is_mirrored(seg)	((seg)->segtype->flags & SEG_AREAS_MIRRORED ? 1 : 0)
 #define seg_is_striped(seg)	((seg)->segtype->flags & SEG_AREAS_STRIPED ? 1 : 0)
@@ -41,10 +42,14 @@ struct dev_manager;
 #define seg_can_split(seg)	((seg)->segtype->flags & SEG_CAN_SPLIT ? 1 : 0)
 #define seg_cannot_be_zeroed(seg) ((seg)->segtype->flags & SEG_CANNOT_BE_ZEROED ? 1 : 0)
 #define seg_monitored(seg)	((seg)->segtype->flags & SEG_MONITORED ? 1 : 0)
+#define seg_is_encrypted(seg)	((seg)->segtype->flags & SEG_AREAS_ENCRYPTED ? 1 : 0)
+#define seg_is_keystore(seg)	(seg_is_encrypted(seg) && seg_cannot_be_zeroed(seg))
 
 #define segtype_is_striped(segtype)	((segtype)->flags & SEG_AREAS_STRIPED ? 1 : 0)
 #define segtype_is_mirrored(segtype)	((segtype)->flags & SEG_AREAS_MIRRORED ? 1 : 0)
 #define segtype_is_virtual(segtype)	((segtype)->flags & SEG_VIRTUAL ? 1 : 0)
+#define segtype_is_encrypted(segtype)	((segtype)->flags & SEG_AREAS_ENCRYPTED ? 1 : 0)
+#define segtype_is_keystore(segtype)	(segtype_is_encrypted(segtype) && segtype_cannot_be_zeroed(segtype))
 
 struct segment_type {
 	struct dm_list list;
@@ -106,6 +111,7 @@ struct segment_type *init_mirrored_segtype(struct cmd_context *cmd);
 #endif
 
 #ifdef CRYPT_INTERNAL
+struct segment_type *init_keystore_segtype(struct cmd_context *cmd);
 struct segment_type *init_crypt_segtype(struct cmd_context *cmd);
 #endif
 
lib/misc/configure.h.in +3 -0
index ccab9f5..d171445 100644
@@ -11,6 +11,9 @@
    */
 #undef CRAY_STACKSEG_END
 
+/* Define to 1 to include built-in support for crypto. */
+#undef CRYPT_INTERNAL
+
 /* Define to 1 if using `alloca.c'. */
 #undef C_ALLOCA
 
Signature · 1 line
1.5.6.5
author
#7 lore raw

PATCH 7/15 Add "crypt" and "crypt-keystore" segment implementation.

The "crypt" segment is direct equivalent to "linear" segment, except it is encrypted of course.

For segment are defined the same flags to easily re-use all lvm functions already using linear segment type.

It also implements merging of crypto segments.

New flag SEG_AREAS_ENCRYPTED is used to distinguish between linear and encrypted segment.

The "crypt" segment must have assigned crypto_store (for validation code enforcing this see following patch).

METADATA example:

segment1 {
start_extent = 0
extent_count = 4 # 16 Megabytes

type = "crypt"
crypto_store = "cryptostore3"

areas = [
"pv2", 0
]
}

(crypto_store references special LV by its name in the same VG)

The "crypt-keystore" segment describes special part of disk, which is never activated with any logical volume, except for key management operation.
(Activation is currently simply ignored).

(Following code implements crypto store as special LV, only "crypt-keystore" segments can be included in this special LV.)

Keystore segment should always include only physical areas (but the restriction is not hardcoded).

Currently only one area for crypto segment and one "crypt-keystore" segment per special LV is supported.

METADATA examples:
- "plain" handler, no areas

segment1 {
start_extent = 0
extent_count = 0 # 0 Kilobytes

type = "crypt-keystore" handler = "plain" cipher = "aes-xts-plain" key_size = 256 # 32 Bytes
}

- "luks1" handler using keyslots on disk (cipher, key_size, etc. are stored in this area too)

segment1 {
start_extent = 0
extent_count = 1 # 4 Megabytes

type = "crypt-keystore"
handler = "luks1"

areas = [
"pv0", 3
]
}

For unknown handler it retains these attributes:
cipher, key_size, key_hash

---
 lib/Makefile.in                  |    1 +
 lib/crypt/crypt.c                |  332 +++++++++++++++++++++++++++++++++++++-
 lib/crypt/lvm-crypto.h           |    7 +
 lib/metadata/crypt_manip.c       |  118 ++++++++++++++
 lib/metadata/metadata-exported.h |    9 +
 5 files changed, 465 insertions(+), 2 deletions(-)
 create mode 100644 lib/metadata/crypt_manip.c
lib/Makefile.in +1 -0
index 7971395..7167f04 100644
@@ -69,6 +69,7 @@ SOURCES =\
 	locking/locking.c \
 	locking/no_locking.c \
 	log/log.c \
+	metadata/crypt_manip.c \
 	metadata/lv_manip.c \
 	metadata/merge.c \
 	metadata/metadata.c \
lib/crypt/crypt.c +330 -2

350 lines not on this page · raw message

lib/crypt/lvm-crypto.h +7 -0
index 3ebddce..6ed2d91 100644
@@ -23,6 +23,9 @@
  */
 typedef const char *masterkey_t;
 
+struct lv_segment;
+struct logical_volume;
+
 /*
  * Flags says if attribute is stored in keystore area and not in LVM metadata.
  * By default, all generic attributes in LVM metadata must be retained
@@ -111,4 +114,8 @@ struct crypto_store_type *lvm_get_keystore_handler(const char *name);
 int lvm_set_password_dev(const char *password_file);
 int lvm_read_password(char *message, char *pass, size_t len);
 
+struct device_area *first_crypto_area(struct crypto_store *cs);
+int seg_assign_crypto_store_areas(struct lv_segment *seg);
+int seg_assign_cryptostore(struct lv_segment *seg, const char *name,
+                           struct crypto_store_type *cst);
 #endif
lib/metadata/crypt_manip.c new file +118 -0

118 lines not on this page · raw message

lib/metadata/metadata-exported.h +9 -0
index 16c216a..2aa5a1b 100644
@@ -476,6 +476,9 @@ struct logical_volume *find_lv(const struct volume_group *vg,
 			       const char *lv_name);
 struct physical_volume *find_pv_by_name(struct cmd_context *cmd,
 					const char *pv_name);
+/* Find crypto store in VG */
+struct crypto_store *find_crypto_store_in_vg(struct volume_group *vg,
+					     const char *cs_name);
 
 /* Find LV segment containing given LE */
 struct lv_segment *first_seg(const struct logical_volume *lv);
@@ -567,6 +570,12 @@ char *generate_lv_name(struct volume_group *vg, const char *format,
 		       char *buffer, size_t len);
 
 /*
+ * Crypto functions
+ */
+struct crypto_store *alloc_cryptostore(struct dm_pool *mem, const char *name,
+				       struct crypto_store_type *cst);
+
+/*
 * Begin skeleton for external LVM library
 */
 struct device *pv_dev(const pv_t *pv);
Signature · 1 line
1.5.6.5
author
#8 lore raw

PATCH 8/15 Add crypto_store to LV segment allocation functions.

Also by default inherit crypto store from first segment when allocating new crypt segment.

---
 lib/format1/import-extents.c    |    4 ++--
 lib/format_pool/import_export.c |    4 ++--
 lib/format_text/import_vsn1.c   |    2 +-
 lib/metadata/lv_alloc.h         |    3 ++-
 lib/metadata/lv_manip.c         |   24 ++++++++++++++++--------
 lib/metadata/merge.c            |    2 +-
 6 files changed, 24 insertions(+), 15 deletions(-)
lib/format1/import-extents.c +2 -2
index ded0a8e..937c5bf 100644
@@ -219,7 +219,7 @@ static int _read_linear(struct cmd_context *cmd, struct lv_map *lvm)
 		len = _area_length(lvm, le);
 
 		if (!(seg = alloc_lv_segment(cmd->mem, segtype, lvm->lv, le,
-					     len, 0, 0, NULL, 1, len, 0, 0, 0))) {
+					     len, 0, 0, NULL, 1, len, 0, 0, 0, NULL))) {
 			log_error("Failed to allocate linear segment.");
 			return 0;
 		}
@@ -293,7 +293,7 @@ static int _read_stripes(struct cmd_context *cmd, struct lv_map *lvm)
 					     lvm->stripes * area_len,
 					     0, lvm->stripe_size, NULL,
 					     lvm->stripes,
-					     area_len, 0, 0, 0))) {
+					     area_len, 0, 0, 0, NULL))) {
 			log_error("Failed to allocate striped segment.");
 			return 0;
 		}
lib/format_pool/import_export.c +2 -2
index 4d2163d..a5198de 100644
@@ -220,7 +220,7 @@ static int _add_stripe_seg(struct dm_pool *mem,
 	if (!(seg = alloc_lv_segment(mem, segtype, lv, *le_cur,
 				     area_len * usp->num_devs, 0,
 				     usp->striping, NULL, usp->num_devs,
-				     area_len, 0, 0, 0))) {
+				     area_len, 0, 0, 0, NULL))) {
 		log_error("Unable to allocate striped lv_segment structure");
 		return 0;
 	}
@@ -257,7 +257,7 @@ static int _add_linear_seg(struct dm_pool *mem,
 		if (!(seg = alloc_lv_segment(mem, segtype, lv, *le_cur,
 					     area_len, 0, usp->striping,
 					     NULL, 1, area_len,
-					     POOL_PE_SIZE, 0, 0))) {
+					     POOL_PE_SIZE, 0, 0, NULL))) {
 			log_error("Unable to allocate linear lv_segment "
 				  "structure");
 			return 0;
lib/format_text/import_vsn1.c +1 -1
index b610501..1de6fbc 100644
@@ -336,7 +336,7 @@ static int _read_segment(struct dm_pool *mem, struct volume_group *vg,
 
 	if (!(seg = alloc_lv_segment(mem, segtype, lv, start_extent,
 				     extent_count, 0, 0, NULL, area_count,
-				     extent_count, 0, 0, 0))) {
+				     extent_count, 0, 0, 0, NULL))) {
 		log_error("Segment allocation failed");
 		return 0;
 	}
lib/metadata/lv_alloc.h +2 -1
index f94f503..e21fb08 100644
@@ -26,7 +26,8 @@ struct lv_segment *alloc_lv_segment(struct dm_pool *mem,
 				    uint32_t area_len,
 				    uint32_t chunk_size,
 				    uint32_t region_size,
-				    uint32_t extents_copied);
+				    uint32_t extents_copied,
+				    struct crypto_store *crypto_store);
 
 struct lv_segment *alloc_snapshot_seg(struct logical_volume *lv,
 				      uint32_t status, uint32_t old_le_count);
lib/metadata/lv_manip.c +16 -8
index 2661e0c..2c3fdf5 100644
@@ -173,7 +173,8 @@ struct lv_segment *alloc_lv_segment(struct dm_pool *mem,
 				    uint32_t area_len,
 				    uint32_t chunk_size,
 				    uint32_t region_size,
-				    uint32_t extents_copied)
+				    uint32_t extents_copied,
+				    struct crypto_store *crypto_store)
 {
 	struct lv_segment *seg;
 	uint32_t areas_sz = area_count * sizeof(*seg->areas);
@@ -203,6 +204,7 @@ struct lv_segment *alloc_lv_segment(struct dm_pool *mem,
 	seg->region_size = region_size;
 	seg->extents_copied = extents_copied;
 	seg->log_lv = log_lv;
+	seg->crypto_store = crypto_store;
 	dm_list_init(&seg->tags);
 
 	if (log_lv && !attach_mirror_log(seg, log_lv))
@@ -226,7 +228,7 @@ struct lv_segment *alloc_snapshot_seg(struct logical_volume *lv,
 	if (!(seg = alloc_lv_segment(lv->vg->cmd->mem, segtype, lv, old_le_count,
 				     lv->le_count - old_le_count, status, 0,
 				     NULL, 0, lv->le_count - old_le_count,
-				     0, 0, 0))) {
+				     0, 0, 0, NULL))) {
 		log_error("Couldn't allocate new snapshot segment.");
 		return NULL;
 	}
@@ -655,16 +657,21 @@ static int _setup_alloced_segment(struct logical_volume *lv, uint32_t status,
 {
 	uint32_t s, extents, area_multiple;
 	struct lv_segment *seg;
+	struct crypto_store *cs = NULL;
 
 	area_multiple = calc_area_multiple(segtype, area_count);
 
+	/* Inherit crypto_store from first segment */
+	if (segtype_is_encrypted(segtype) && first_seg(lv))
+		cs = first_seg(lv)->crypto_store;
+
 	/* log_lv gets set up elsehere */
 	if (!(seg = alloc_lv_segment(lv->vg->cmd->mem, segtype, lv,
 				     lv->le_count,
 				     aa[0].len * area_multiple,
 				     status, stripe_size, NULL,
 				     area_count,
-				     aa[0].len, 0u, region_size, 0u))) {
+				     aa[0].len, 0u, region_size, 0u, cs))) {
 		log_error("Couldn't allocate new LV segment.");
 		return 0;
 	}
@@ -673,6 +680,7 @@ static int _setup_alloced_segment(struct logical_volume *lv, uint32_t status,
 		if (!set_lv_segment_area_pv(seg, s, aa[s].pv, aa[s].pe))
 			return_0;
 
+
 	dm_list_add(&lv->segments, &seg->list);
 
 	extents = aa[0].len * area_multiple;
@@ -1260,7 +1268,7 @@ int lv_add_virtual_segment(struct logical_volume *lv, uint32_t status,
 
 	if (!(seg = alloc_lv_segment(lv->vg->cmd->mem, segtype, lv,
 				     lv->le_count, extents, status, 0,
-				     NULL, 0, extents, 0, 0, 0))) {
+				     NULL, 0, extents, 0, 0, 0, NULL))) {
 		log_error("Couldn't allocate new zero segment.");
 		return 0;
 	}
@@ -1393,7 +1401,7 @@ static struct lv_segment *_convert_seg_to_mirror(struct lv_segment *seg,
 					log_lv,
 					seg->area_count, seg->area_len,
 					seg->chunk_size, region_size,
-					seg->extents_copied))) {
+					seg->extents_copied, NULL))) {
 		log_error("Couldn't allocate converted LV segment");
 		return NULL;
 	}
@@ -1534,7 +1542,7 @@ int lv_add_log_segment(struct alloc_handle *ah, struct logical_volume *log_lv)
 				     get_segtype_from_string(log_lv->vg->cmd,
 							     "striped"),
 				     log_lv, 0, ah->log_area.len, MIRROR_LOG,
-				     0, NULL, 1, ah->log_area.len, 0, 0, 0))) {
+				     0, NULL, 1, ah->log_area.len, 0, 0, 0, NULL))) {
 		log_error("Couldn't allocate new mirror log segment.");
 		return 0;
 	}
@@ -2447,7 +2455,7 @@ struct logical_volume *insert_layer_for_lv(struct cmd_context *cmd,
 	if (!(mapseg = alloc_lv_segment(cmd->mem, segtype,
 					lv_where, 0, layer_lv->le_count,
 					status, 0, NULL, 1, layer_lv->le_count,
-					0, 0, 0)))
+					0, 0, 0, NULL)))
 		return_NULL;
 
 	/* map the new segment to the original underlying are */
@@ -2490,7 +2498,7 @@ static int _extend_layer_lv_for_segment(struct logical_volume *layer_lv,
 	if (!(mapseg = alloc_lv_segment(layer_lv->vg->cmd->mem, segtype,
 					layer_lv, layer_lv->le_count,
 					seg->area_len, status, 0,
-					NULL, 1, seg->area_len, 0, 0, 0)))
+					NULL, 1, seg->area_len, 0, 0, 0, NULL)))
 		return_0;
 
 	/* map the new segment to the original underlying are */
lib/metadata/merge.c +1 -1
index 66e9ce0..f1d8b9a 100644
@@ -274,7 +274,7 @@ static int _lv_split_segment(struct logical_volume *lv, struct lv_segment *seg,
 					   seg->log_lv,
 					   seg->area_count, seg->area_len,
 					   seg->chunk_size, seg->region_size,
-					   seg->extents_copied))) {
+					   seg->extents_copied, seg->crypto_store))) {
 		log_error("Couldn't allocate cloned LV segment.");
 		return 0;
 	}
Signature · 1 line
1.5.6.5
author
#9 lore raw

PATCH 9/15 Add encrypted LV manipulation functions.

- add vg_validate code which checks that all
- crypt segments has referenced crypto_store
- cryptostores have special LV in this VG
- existing crypto_store LVs are referenced

- add reserved LV name "cryptostoreN" used for cryptostore LV

- retrieve segment master key before activate and preload commands (and skip the table loading if we have no key for crypt segments)

- add cryptostore LV add/remove functions.

crypto_store has reference counter
(increased during segment loading or new crypt segment allocation)

If the last segment is removed, automatically remove special crypto_store LV too.

---
 lib/activate/activate.c          |    6 ++
 lib/crypt/lvm-crypto.h           |    2 +
 lib/metadata/crypt_manip.c       |  142 ++++++++++++++++++++++++++++++++++++++
 lib/metadata/lv_manip.c          |    5 ++
 lib/metadata/merge.c             |   30 ++++++++
 lib/metadata/metadata-exported.h |    6 ++
 tools/toollib.c                  |    6 ++
 7 files changed, 197 insertions(+), 0 deletions(-)
lib/activate/activate.c +6 -0
index 2bc1db7..0f13cbd 100644
@@ -863,6 +863,9 @@ static int _lv_suspend(struct cmd_context *cmd, const char *lvid_s,
 
 	/* If VG was precommitted, preload devices for the LV */
 	if ((lv_pre->vg->status & PRECOMMITTED)) {
+		if (!lv_retrieve_password(lv))
+			return 0;
+
 		if (!_lv_preload(lv_pre)) {
 			/* FIXME Revert preloading */
 			return_0;
@@ -1047,6 +1050,9 @@ static int _lv_activate(struct cmd_context *cmd, const char *lvid_s,
 	if (exclusive)
 		lv->status |= ACTIVATE_EXCL;
 
+	if (!lv_retrieve_password(lv))
+		return 0;
+
 	memlock_inc();
 	r = _lv_activate_lv(lv);
 	memlock_dec();
lib/crypt/lvm-crypto.h +2 -0
index 6ed2d91..42004b7 100644
@@ -118,4 +118,6 @@ struct device_area *first_crypto_area(struct crypto_store *cs);
 int seg_assign_crypto_store_areas(struct lv_segment *seg);
 int seg_assign_cryptostore(struct lv_segment *seg, const char *name,
                            struct crypto_store_type *cst);
+int lv_retrieve_password(struct logical_volume *lv);
+
 #endif
lib/metadata/crypt_manip.c +142 -0

151 lines not on this page · raw message

lib/metadata/lv_manip.c +5 -0
index 2c3fdf5..0cdda57 100644
@@ -2056,6 +2056,11 @@ int lv_remove_single(struct cmd_context *cmd, struct logical_volume *lv,
 			return_0;
 	}
 
+	if (!lv_remove_cryptostores(lv)) {
+		log_error("Error removing cryptostores for logical volume \"%s\"", lv->name);
+		return 0;
+	}
+
 	log_verbose("Releasing logical volume \"%s\"", lv->name);
 	if (!lv_remove(lv)) {
 		log_error("Error releasing logical volume \"%s\"", lv->name);
lib/metadata/merge.c +30 -0
index f1d8b9a..403bfbe 100644
@@ -129,6 +129,36 @@ int check_lv_segments(struct logical_volume *lv, int complete_vg)
 			}
 		}
 
+		if (seg_is_encrypted(seg)) {
+			if (!seg->crypto_store) {
+				log_error("LV %s: segment %u is encrypted but have "
+					  "no assigned crypto store",
+					  lv->name, seg_count);
+				r = 0;
+			} else if (!find_crypto_store_in_vg(lv->vg,
+							    seg->crypto_store->name)) {
+				log_error("LV %s: segment %u is encrypted but have "
+					  "assigned nonexisting crypto store %s",
+					  lv->name, seg_count, seg->crypto_store->name);
+				r = 0;
+			}
+			if (seg->area_count > 1) {
+				log_error("LV %s: encrypted segment %u have %u areas, "
+					  "but only one area is supported.",
+					  lv->name, seg_count, seg->area_count);
+				r = 0;
+			}
+		}
+
+		if (seg_is_keystore(seg)) {
+			if (!seg->crypto_store->ref) {
+				log_error("LV %s: segment %u is keystore and "
+					  "is not referenced by any other LV.",
+					  lv->name, seg_count);
+				r = 0;
+			}
+		}
+
 		for (s = 0; s < seg->area_count; s++) {
 			if (seg_type(seg, s) == AREA_UNASSIGNED) {
 				log_error("LV %s: segment %u has unassigned "
lib/metadata/metadata-exported.h +6 -0
index 2aa5a1b..9835308 100644
@@ -575,6 +575,12 @@ char *generate_lv_name(struct volume_group *vg, const char *format,
 struct crypto_store *alloc_cryptostore(struct dm_pool *mem, const char *name,
 				       struct crypto_store_type *cst);
 
+struct crypto_store *vg_add_cryptostore(struct volume_group *vg,
+					struct lv_segment *seg,
+					const char *handler_name);
+int lv_remove_cryptostores(struct logical_volume *lv);
+int lv_add_cryptostore(struct logical_volume *lv, struct crypto_store *cs);
+
 /*
 * Begin skeleton for external LVM library
 */
tools/toollib.c +6 -0
index a0494a1..bada05e 100644
@@ -1160,6 +1160,12 @@ int apply_lvname_restrictions(const char *name)
 		return 0;
 	}
 
+	if (!strncmp(name, "cryptostore", 11)) {
+		log_error("Names starting \"cryptostore\" are reserved. "
+			  "Please choose a different LV name.");
+		return 0;
+	}
+
 	if (strstr(name, "_mlog")) {
 		log_error("Names including \"_mlog\" are reserved. "
 			  "Please choose a different LV name.");
Signature · 1 line
1.5.6.5
author
#10 lore raw

PATCH 10/15 Cache key from crypt mapping table if segment is active.

For some online operations (like lvresize) we need preload new table, thus we need master key.

But the key is already in mapping table, do not ask user and try to retrieve master key from mapping table first.

---
 lib/activate/activate.c    |   41 ++++++++++++++++++++++++++++++
 lib/activate/activate.h    |    5 +++
 lib/activate/dev_manager.c |   59 ++++++++++++++++++++++++++++++++++++++++++++
 lib/activate/dev_manager.h |    4 +++
 lib/metadata/crypt_manip.c |    6 ++++
 5 files changed, 115 insertions(+), 0 deletions(-)
lib/activate/activate.c +41 -0
index 0f13cbd..0b17be8 100644
@@ -1117,6 +1117,47 @@ int pv_uses_vg(struct physical_volume *pv,
 	return dev_manager_device_uses_vg(pv->dev, vg);
 }
 
+/*
+ * Retrieve key for lv segment from dm table and cache it
+ */
+int lv_seg_cache_key(struct lv_segment *seg)
+{
+	char *key;
+	struct dev_manager *dm;
+	struct logical_volume *lv = seg->lv;
+	struct crypto_store *cs = seg->crypto_store;
+	int r;
+
+	if (!(key = dm_malloc(cs_key_bytes(cs) + 1)))
+		return_0;
+
+	if (!(dm = dev_manager_create(lv->vg->cmd, lv->vg->name))) {
+		dm_free(key);
+		return_0;
+	}
+
+	if (!(r = dev_manager_crypt_key(dm, seg, key, cs_key_bytes(cs) + 1)))
+		stack;
+
+	dev_manager_destroy(dm);
+
+	if (!r)
+		goto out;
+
+	if (!lvm_masterkeys_verify(cs, key)) {
+		log_error("Internal error: Key in mapping table "
+			  "is not in expected format.");
+		goto out;
+	}
+
+	return lvm_masterkeys_insert(&cs->id, key);
+out:
+	memset(key, 0, cs_key_bytes(cs));
+	dm_free(key);
+	return 0;
+}
+
+
 void activation_release(void)
 {
 	dev_manager_release();
lib/activate/activate.h +5 -0
index c5bd4b6..cc096a2 100644
@@ -107,4 +107,9 @@ int pv_uses_vg(struct physical_volume *pv,
  */
 int device_is_usable(dev_t dev);
 
+/*
+ * Retrieve key for lv segment from dm table and cache it
+ */
+int lv_seg_cache_key(struct lv_segment *seg);
+
 #endif
lib/activate/dev_manager.c +59 -0

62 lines not on this page · raw message

lib/activate/dev_manager.h +4 -0
index 7a76453..ac7cec9 100644
@@ -17,6 +17,7 @@
 #define _LVM_DEV_MANAGER_H
 
 struct logical_volume;
+struct lv_segment;
 struct volume_group;
 struct cmd_context;
 struct dev_manager;
@@ -57,6 +58,9 @@ int dev_manager_deactivate(struct dev_manager *dm, struct logical_volume *lv);
 int dev_manager_lv_mknodes(const struct logical_volume *lv);
 int dev_manager_lv_rmnodes(const struct logical_volume *lv);
 
+int dev_manager_crypt_key(struct dev_manager *dm, struct lv_segment *seg,
+			  char *buffer, int buffer_len);
+
 /*
  * Put the desired changes into effect.
  */
lib/metadata/crypt_manip.c +6 -0
index ff5c3e2..4b45a07 100644
@@ -242,6 +242,12 @@ static int _seg_retrieve_masterkey(struct lv_segment *seg)
 	if (lvm_masterkeys_query(&seg->crypto_store->id))
 		return 1;
 
+	/*
+	 * Try to retrieve key from dm table
+	 */
+	if (lv_is_active(seg->lv) && lv_seg_cache_key(seg))
+		return 1;
+
 	return lvm_masterkeys_retrieve(seg->lv->name, seg->crypto_store);
 }
 
Signature · 1 line
1.5.6.5
author
#11 lore raw

PATCH 11/15 Add lvcreate crypto LV implementation.

lcvreate now support two modes for creating encrypted LVs:

1) specify encryption directly

	--crypt KeyStoreType --cipher Cipher --keysize KeySize

e.g.

lvcreate -n LV -l 4 --crypt plain --cipher aes-xts-plain --keysize 256 VG

2) use another LV as template (IOW reuse its cryptostore) --crypt TemplateLogicalVolume[Path]

e.g.

lvcreate -n LV2 -l 4 --crypt LV VG

---
 tools/args.h     |    4 ++
 tools/commands.h |    9 ++-
 tools/lvcreate.c |  160 +++++++++++++++++++++++++++++++++++++++++++++++++++++-
 3 files changed, 168 insertions(+), 5 deletions(-)
tools/args.h +4 -0
index 8f026fc..a1347ea 100644
@@ -57,6 +57,10 @@ arg(nameprefixes_ARG, '\0', "nameprefixes", NULL, 0)
 arg(unquoted_ARG, '\0', "unquoted", NULL, 0)
 arg(rows_ARG, '\0', "rows", NULL, 0)
 
+arg(crypt_ARG, '\0', "crypt", string_arg, 0)
+arg(cipher_ARG, '\0', "cipher", string_arg, 0)
+arg(keysize_ARG, '\0', "keysize", int_arg, 0)
+
 /* Allow some variations */
 arg(resizable_ARG, '\0', "resizable", yes_no_arg, 0)
 arg(allocation_ARG, '\0', "allocation", yes_no_arg, 0)
tools/commands.h +6 -3
index 58c6156..aa5814c 100644
@@ -132,6 +132,8 @@ xx(lvcreate,
    "\t -L|--size LogicalVolumeSize[kKmMgGtTpPeE]}\n"
    "\t[-M|--persistent {y|n}] [--major major] [--minor minor]\n"
    "\t[-m|--mirrors Mirrors [--nosync] [{--mirrorlog {disk|core}|--corelog}]]\n"
+   "\t[--crypt KeyStoreType --cipher Cipher --keysize KeySize]\n"
+   "\t[--crypt TemplateLogicalVolume[Path]]\n"
    "\t[-n|--name LogicalVolumeName]\n"
    "\t[-p|--permission {r|rw}]\n"
    "\t[-r|--readahead ReadAheadSectors|auto|none]\n"
@@ -163,9 +165,10 @@ xx(lvcreate,
    "\t[--version]\n"
    "\tOriginalLogicalVolume[Path] [PhysicalVolumePath...]\n\n",
 
-   addtag_ARG, alloc_ARG, autobackup_ARG, chunksize_ARG, contiguous_ARG,
-   corelog_ARG, extents_ARG, major_ARG, minor_ARG, mirrorlog_ARG, mirrors_ARG,
-   name_ARG, nosync_ARG, permission_ARG, persistent_ARG, readahead_ARG,
+   addtag_ARG, alloc_ARG, autobackup_ARG, chunksize_ARG, cipher_ARG,
+   contiguous_ARG, corelog_ARG, crypt_ARG, extents_ARG, keysize_ARG,
+   major_ARG, minor_ARG, mirrorlog_ARG, mirrors_ARG, name_ARG,
+   nosync_ARG, permission_ARG, persistent_ARG, readahead_ARG,
    regionsize_ARG, size_ARG, snapshot_ARG, stripes_ARG, stripesize_ARG,
    test_ARG, type_ARG, zero_ARG)
 
tools/lvcreate.c +158 -2

226 lines not on this page · raw message

Signature · 1 line
1.5.6.5
author
#12 lore raw

PATCH 12/15 Add lvconcert crypt implementation.

lvconvert --crypt KeyStoreType LogicalVolume[Path]

If the defined key handler contains support for conversion (it implements scan function), it converts linear volume into encrypted using the first extent as keystore area.

Note, that there is currently not yet implemented key management interface, so you lost access to keystore for key management operation after conversion.

You can activate volume using only keys defined in the moment when conversion happened.

[basically this allows using LUKS keystore only now]

---
 tools/commands.h  |    8 +++-
 tools/lvconvert.c |  125 +++++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 131 insertions(+), 2 deletions(-)
tools/commands.h +6 -2
index aa5814c..deb300b 100644
@@ -112,9 +112,13 @@ xx(lvconvert,
    "\t[-v|--verbose]\n"
    "\t[-Z|--zero {y|n}]\n"
    "\t[--version]" "\n"
-   "\tOriginalLogicalVolume[Path] SnapshotLogicalVolume[Path]\n",
+   "\tOriginalLogicalVolume[Path] SnapshotLogicalVolume[Path]\n\n"
 
-   alloc_ARG, background_ARG, chunksize_ARG, corelog_ARG, interval_ARG,
+   "lvconvert "
+   "[--crypt KeyStoreType]\n"
+   "\tLogicalVolume[Path]]\n",
+
+   alloc_ARG, background_ARG, chunksize_ARG, corelog_ARG, crypt_ARG, interval_ARG,
    mirrorlog_ARG, mirrors_ARG, regionsize_ARG, snapshot_ARG, test_ARG, zero_ARG)
 
 xx(lvcreate,
tools/lvconvert.c +125 -0

171 lines not on this page · raw message

Signature · 1 line
1.5.6.5
author
#13 lore raw

PATCH 13/15 Add --keyfile option.

Allow input of key from file (or standard input) by specifying --keyfile on commandline.

If the file is "-" stdin is used, e.g.
echo $key | lvcreate ...

(It is not safe, but useful in scripts. Similar approach is used in cryptsetup.)

Currently supported only for lvcreate, vgchange, lvchange.

---
 tools/args.h     |    1 +
 tools/commands.h |   17 +++++++++--------
 tools/lvchange.c |    7 +++++++
 tools/lvcreate.c |    7 +++++++
 tools/vgchange.c |    7 +++++++
 5 files changed, 31 insertions(+), 8 deletions(-)
tools/args.h +1 -0
index a1347ea..cacbb2b 100644
@@ -60,6 +60,7 @@ arg(rows_ARG, '\0', "rows", NULL, 0)
 arg(crypt_ARG, '\0', "crypt", string_arg, 0)
 arg(cipher_ARG, '\0', "cipher", string_arg, 0)
 arg(keysize_ARG, '\0', "keysize", int_arg, 0)
+arg(keyfile_ARG, '\0', "keyfile", string_arg, 0)
 
 /* Allow some variations */
 arg(resizable_ARG, '\0', "resizable", yes_no_arg, 0)
tools/commands.h +9 -8
index deb300b..07c472e 100644
@@ -85,9 +85,10 @@ xx(lvchange,
    "\tLogicalVolume[Path] [LogicalVolume[Path]...]\n",
 
    alloc_ARG, autobackup_ARG, available_ARG, contiguous_ARG, force_ARG,
-   ignorelockingfailure_ARG, ignoremonitoring_ARG, major_ARG, minor_ARG,
-   monitor_ARG, partial_ARG, permission_ARG, persistent_ARG, readahead_ARG,
-   resync_ARG, refresh_ARG, addtag_ARG, deltag_ARG, test_ARG, yes_ARG)
+   ignorelockingfailure_ARG, ignoremonitoring_ARG, keyfile_ARG, major_ARG,
+   minor_ARG, monitor_ARG, partial_ARG, permission_ARG, persistent_ARG,
+   readahead_ARG, resync_ARG, refresh_ARG, addtag_ARG, deltag_ARG, test_ARG,
+   yes_ARG)
 
 xx(lvconvert,
    "Change logical volume layout",
@@ -136,7 +137,7 @@ xx(lvcreate,
    "\t -L|--size LogicalVolumeSize[kKmMgGtTpPeE]}\n"
    "\t[-M|--persistent {y|n}] [--major major] [--minor minor]\n"
    "\t[-m|--mirrors Mirrors [--nosync] [{--mirrorlog {disk|core}|--corelog}]]\n"
-   "\t[--crypt KeyStoreType --cipher Cipher --keysize KeySize]\n"
+   "\t[--crypt KeyStoreType --cipher Cipher --keysize KeySize [--keyfile filename]]\n"
    "\t[--crypt TemplateLogicalVolume[Path]]\n"
    "\t[-n|--name LogicalVolumeName]\n"
    "\t[-p|--permission {r|rw}]\n"
@@ -171,7 +172,7 @@ xx(lvcreate,
 
    addtag_ARG, alloc_ARG, autobackup_ARG, chunksize_ARG, cipher_ARG,
    contiguous_ARG, corelog_ARG, crypt_ARG, extents_ARG, keysize_ARG,
-   major_ARG, minor_ARG, mirrorlog_ARG, mirrors_ARG, name_ARG,
+   keyfile_ARG, major_ARG, minor_ARG, mirrorlog_ARG, mirrors_ARG, name_ARG,
    nosync_ARG, permission_ARG, persistent_ARG, readahead_ARG,
    regionsize_ARG, size_ARG, snapshot_ARG, stripes_ARG, stripesize_ARG,
    test_ARG, type_ARG, zero_ARG)
@@ -689,9 +690,9 @@ xx(vgchange,
 
    addtag_ARG, alloc_ARG, allocation_ARG, autobackup_ARG, available_ARG,
    clustered_ARG, deltag_ARG, ignorelockingfailure_ARG, ignoremonitoring_ARG,
-   logicalvolume_ARG, maxphysicalvolumes_ARG, monitor_ARG, partial_ARG,
-   physicalextentsize_ARG, refresh_ARG, resizeable_ARG, resizable_ARG,
-   test_ARG, uuid_ARG)
+   keyfile_ARG, logicalvolume_ARG, maxphysicalvolumes_ARG, monitor_ARG,
+   partial_ARG, physicalextentsize_ARG, refresh_ARG, resizeable_ARG,
+   resizable_ARG, test_ARG, uuid_ARG)
 
 xx(vgck,
    "Check the consistency of volume group(s)",
tools/lvchange.c +7 -0
index cd0ff5a..0a4778f 100644
@@ -728,6 +728,13 @@ int lvchange(struct cmd_context *cmd, int argc, char **argv)
 		return EINVALID_CMD_LINE;
 	}
 
+	if (arg_count(cmd, keyfile_ARG) &&
+	    !lvm_set_password_dev(arg_value(cmd, keyfile_ARG))) {
+		log_error("--keyfile argument %s is invalid",
+			  arg_value(cmd, keyfile_ARG));
+		return EINVALID_CMD_LINE;
+	}
+
 	return process_each_lv(cmd, argc, argv, LCK_VG_WRITE, NULL,
 			       &lvchange_single);
 }
tools/lvcreate.c +7 -0
index e0f59be..6eea76d 100644
@@ -342,6 +342,13 @@ static int _read_crypt_params(struct lvcreate_params *lp,
 	if (!arg_count(cmd, crypt_ARG))
 		return 1;
 
+	if (arg_count(cmd, keyfile_ARG) &&
+	    !lvm_set_password_dev(arg_value(cmd, keyfile_ARG))) {
+		log_error("--keyfile argument %s is invalid",
+			  arg_value(cmd, keyfile_ARG));
+		return 0;
+	}
+
 	if (arg_count(cmd, cipher_ARG)) {
 		/*
 		 * Explicit cipher specification
tools/vgchange.c +7 -0
index 8831a23..ed00744 100644
@@ -631,6 +631,13 @@ int vgchange(struct cmd_context *cmd, int argc, char **argv)
 		return EINVALID_CMD_LINE;
 	}
 
+	if (arg_count(cmd, keyfile_ARG) &&
+	    !lvm_set_password_dev(arg_value(cmd, keyfile_ARG))) {
+		log_error("--keyfile argument %s is invalid",
+			  arg_value(cmd, keyfile_ARG));
+		return EINVALID_CMD_LINE;
+	}
+
 	return process_each_vg(cmd, argc, argv,
 			       (arg_count(cmd, available_ARG)) ?
 			       LCK_VG_READ : LCK_VG_WRITE, 0, NULL,
Signature · 1 line
1.5.6.5
author
#14 lore raw

PATCH 14/15 Add *testing* LUKS1 keystore implementation.

BIG FAT WARNING: THIS IS JUST BUGGY TEST CODE PROVIDED ONLY FOR TESTING OF PREVIOUS CODE :-)

This patch tries to implement real keystore handling, using LUKS1 format.

If you have linear logical volume, formatted using

     cryptsetup luksFormat /dev/VG/LV --align-offset=<extent sectors>

you can import it to lvm as "luks1" keystore using

    lvconvert --crypt luks1 VG/LV

Now is the volume converted to crypto key store (first extent of former volume) and internal encrypted volume.

LUKS1 key handler then allows volume activation, using on disk LUKS metadata.

(See following patch with test script for examples.)

Notes:
1) The whole LUKS parsing code was rewrited by me using libgcrypt instead of hardcoded SHA1 & PBKDF2 code.

2) Simple configure script *requires* libgcrypt for linking. (In the future, only keystore library should require libgcrypt or nss crypto library).

3) Why I didn't use libluks or libcryptsetup?
- the luks library code need rewrite, API is not stable, some distros removed it from its build
- there is huge amout of duplicated code which LVM already handles better
- SHA1 is hardcoded in cryptsetup-luks, and it can cause serious problem for FIPS certification

Open question is, if the crypsetup code should be fixed and used in lvm through some wrapper (which activates keystore device for it) or lvm should provide an alternative for key management operation.

For now, there is this simple testing alternative.

---
 configure                |   94 +++++++++-
 configure.in             |   30 +++
 lib/Makefile.in          |    6 +
 lib/crypt/key_handlers.c |    3 +
 lib/crypt/key_luks.c     |  495 ++++++++++++++++++++++++++++++++++++++++++++++
 lib/crypt/lvm-crypto.h   |    3 +
 lib/crypt/pbkdf2.c       |  199 +++++++++++++++++++
 lib/crypt/pbkdf2.h       |   36 ++++
 lib/misc/configure.h.in  |    3 +
 9 files changed, 866 insertions(+), 3 deletions(-)
 create mode 100644 lib/crypt/key_luks.c
 create mode 100644 lib/crypt/pbkdf2.c
 create mode 100644 lib/crypt/pbkdf2.h
configure +91 -3
index e4a8f82..2632dbd 100755
@@ -686,6 +686,7 @@ CSCOPE_CMD
 ALLOCA
 LIBOBJS
 POW_LIB
+LIBGCRYPT_CONFIG
 LCOV
 GENHTML
 LVM2CMD_LIB
@@ -700,6 +701,7 @@ CLVMD
 CMDLIB
 COPTIMISE_FLAG
 CRYPTO
+CRYPTO_LUKS1
 DEBUG
 DEVMAPPER
 DMEVENTD
@@ -1328,6 +1330,7 @@ Optional Features:
                           statically.  Default is dynamic linking
   --enable-lvm1_fallback  Use this to fall back and use LVM1 binaries if
                           device-mapper is missing from the kernel
+  --enable-luks1          Enable LUKS1 crypto support
   --disable-readline      Disable readline support
   --disable-realtime      Disable realtime clock support
   --enable-debug          Enable debugging
@@ -8695,6 +8698,88 @@ _ACEOF
 
 fi
 
+{ echo "$as_me:$LINENO: checking whether to enable LUKS1 support" >&5
+echo $ECHO_N "checking whether to enable LUKS1 support... $ECHO_C" >&6; }
+# Check whether --enable-luks1 was given.
+if test "${enable_luks1+set}" = set; then
+  enableval=$enable_luks1; CRYPTO_LUKS1=$enableval
+else
+  CRYPTO_LUKS1=no
+fi
+
+{ echo "$as_me:$LINENO: result: $CRYPTO_LUKS1" >&5
+echo "${ECHO_T}$CRYPTO_LUKS1" >&6; }
+
+if test x$CRYPTO_LUKS1 = xyes; then
+
+cat >>confdefs.h <<\_ACEOF
+#define CRYPT_LUKS1 1
+_ACEOF
+
+
+	if test x$CRYPTO = xnone ; then
+		{ { echo "$as_me:$LINENO: error: Cannot use LUKS without crypto support" >&5
+echo "$as_me: error: Cannot use LUKS without crypto support" >&2;}
+   { (exit 1); exit 1; }; }
+	fi
+
+	# Extract the first word of "libgcrypt-config", so it can be a program name with args.
+set dummy libgcrypt-config; ac_word=$2
+{ echo "$as_me:$LINENO: checking for $ac_word" >&5
+echo $ECHO_N "checking for $ac_word... $ECHO_C" >&6; }
+if test "${ac_cv_path_LIBGCRYPT_CONFIG+set}" = set; then
+  echo $ECHO_N "(cached) $ECHO_C" >&6
+else
+  case $LIBGCRYPT_CONFIG in
+  [\\/]* | ?:[\\/]*)
+  ac_cv_path_LIBGCRYPT_CONFIG="$LIBGCRYPT_CONFIG" # Let the user override the test with a path.
+  ;;
+  *)
+  as_save_IFS=$IFS; IFS=$PATH_SEPARATOR
+for as_dir in $PATH
+do
+  IFS=$as_save_IFS
+  test -z "$as_dir" && as_dir=.
+  for ac_exec_ext in '' $ac_executable_extensions; do
+  if { test -f "$as_dir/$ac_word$ac_exec_ext" && $as_test_x "$as_dir/$ac_word$ac_exec_ext"; }; then
+    ac_cv_path_LIBGCRYPT_CONFIG="$as_dir/$ac_word$ac_exec_ext"
+    echo "$as_me:$LINENO: found $as_dir/$ac_word$ac_exec_ext" >&5
+    break 2
+  fi
+done
+done
+IFS=$as_save_IFS
+
+  test -z "$ac_cv_path_LIBGCRYPT_CONFIG" && ac_cv_path_LIBGCRYPT_CONFIG="no"
+  ;;
+esac
+fi
+LIBGCRYPT_CONFIG=$ac_cv_path_LIBGCRYPT_CONFIG
+if test -n "$LIBGCRYPT_CONFIG"; then
+  { echo "$as_me:$LINENO: result: $LIBGCRYPT_CONFIG" >&5
+echo "${ECHO_T}$LIBGCRYPT_CONFIG" >&6; }
+else
+  { echo "$as_me:$LINENO: result: no" >&5
+echo "${ECHO_T}no" >&6; }
+fi
+
+
+
+	if test x"$LIBGCRYPT_CONFIG" = x"no" ; then
+		{ { echo "$as_me:$LINENO: error: You selected option which require libgcrypt" >&5
+echo "$as_me: error: You selected option which require libgcrypt" >&2;}
+   { (exit 1); exit 1; }; }
+	fi
+
+	# libgcrypt-config fails here, this must go first
+	if test "x$STATIC_LINK" = xyes; then
+		LIBS="-lgpg-error $LIBS"
+	fi
+
+	LIBGCRYPT_LIBS=`$LIBGCRYPT_CONFIG --libs`
+	LIBS="$LIBGCRYPT_LIBS $LIBS"
+fi
+
 ################################################################################
 { echo "$as_me:$LINENO: checking whether to enable readline" >&5
 echo $ECHO_N "checking whether to enable readline... $ECHO_C" >&6; }
@@ -11753,6 +11838,7 @@ LVM_VERSION="\"`cat VERSION 2>/dev/null || echo Unknown`\""
 
 
 
+
 ################################################################################
 ac_config_files="$ac_config_files Makefile make.tmpl daemons/Makefile daemons/clvmd/Makefile daemons/dmeventd/Makefile daemons/dmeventd/libdevmapper-event.pc daemons/dmeventd/plugins/Makefile daemons/dmeventd/plugins/mirror/Makefile daemons/dmeventd/plugins/snapshot/Makefile doc/Makefile include/Makefile lib/Makefile lib/format1/Makefile lib/format_pool/Makefile lib/locking/Makefile lib/mirror/Makefile lib/snapshot/Makefile libdm/Makefile libdm/libdevmapper.pc man/Makefile po/Makefile scripts/clvmd_init_red_hat scripts/Makefile test/Makefile test/api/Makefile tools/Makefile tools/version.h"
 
@@ -12471,6 +12557,7 @@ CSCOPE_CMD!$CSCOPE_CMD$ac_delim
 ALLOCA!$ALLOCA$ac_delim
 LIBOBJS!$LIBOBJS$ac_delim
 POW_LIB!$POW_LIB$ac_delim
+LIBGCRYPT_CONFIG!$LIBGCRYPT_CONFIG$ac_delim
 LCOV!$LCOV$ac_delim
 GENHTML!$GENHTML$ac_delim
 LVM2CMD_LIB!$LVM2CMD_LIB$ac_delim
@@ -12485,6 +12572,7 @@ CLVMD!$CLVMD$ac_delim
 CMDLIB!$CMDLIB$ac_delim
 COPTIMISE_FLAG!$COPTIMISE_FLAG$ac_delim
 CRYPTO!$CRYPTO$ac_delim
+CRYPTO_LUKS1!$CRYPTO_LUKS1$ac_delim
 DEBUG!$DEBUG$ac_delim
 DEVMAPPER!$DEVMAPPER$ac_delim
 DMEVENTD!$DMEVENTD$ac_delim
@@ -12494,8 +12582,6 @@ DM_DEVICE_MODE!$DM_DEVICE_MODE$ac_delim
 DM_DEVICE_UID!$DM_DEVICE_UID$ac_delim
 DM_IOCTLS!$DM_IOCTLS$ac_delim
 DM_LIB_VERSION!$DM_LIB_VERSION$ac_delim
-DM_LIB_PATCHLEVEL!$DM_LIB_PATCHLEVEL$ac_delim
-FSADM!$FSADM$ac_delim
 _ACEOF
 
   if test `sed -n "s/.*$ac_delim\$/X/p" conf$$subs.sed | grep -c X` = 97; then
@@ -12537,6 +12623,8 @@ _ACEOF
 ac_delim='%!_!# '
 for ac_last_try in false false false false false :; do
   cat >conf$$subs.sed <<_ACEOF
+DM_LIB_PATCHLEVEL!$DM_LIB_PATCHLEVEL$ac_delim
+FSADM!$FSADM$ac_delim
 GROUP!$GROUP$ac_delim
 HAVE_LIBDL!$HAVE_LIBDL$ac_delim
 HAVE_REALTIME!$HAVE_REALTIME$ac_delim
@@ -12569,7 +12657,7 @@ usrsbindir!$usrsbindir$ac_delim
 LTLIBOBJS!$LTLIBOBJS$ac_delim
 _ACEOF
 
-  if test `sed -n "s/.*$ac_delim\$/X/p" conf$$subs.sed | grep -c X` = 30; then
+  if test `sed -n "s/.*$ac_delim\$/X/p" conf$$subs.sed | grep -c X` = 32; then
     break
   elif $ac_last_try; then
     { { echo "$as_me:$LINENO: error: could not make $CONFIG_STATUS" >&5
configure.in +30 -0
index a5e9236..a1072fb 100644
@@ -325,6 +325,35 @@ if test x$CRYPTO = xinternal; then
 	AC_DEFINE([CRYPT_INTERNAL], 1, [Define to 1 to include built-in support for crypto.])
 fi
 
+dnl -- LUKS1 keystore support
+AC_MSG_CHECKING(whether to enable LUKS1 support)
+AC_ARG_ENABLE([luks1],
+  [  --enable-luks1          Enable LUKS1 crypto support],
+  [CRYPTO_LUKS1=$enableval], [CRYPTO_LUKS1=no])
+AC_MSG_RESULT($CRYPTO_LUKS1)
+
+if test x$CRYPTO_LUKS1 = xyes; then
+	AC_DEFINE([CRYPT_LUKS1], 1, [Define to 1 to include built-in support for LUKS1 keystore.])
+
+	if test x$CRYPTO = xnone ; then
+		AC_MSG_ERROR(Cannot use LUKS without crypto support)
+	fi
+
+	AC_PATH_PROG(LIBGCRYPT_CONFIG, libgcrypt-config, no)
+
+	if test x"$LIBGCRYPT_CONFIG" = x"no" ; then
+		AC_MSG_ERROR(You selected option which require libgcrypt)
+	fi
+
+	# libgcrypt-config fails here, this must go first
+	if test "x$STATIC_LINK" = xyes; then
+		LIBS="-lgpg-error $LIBS"
+	fi
+
+	LIBGCRYPT_LIBS=`$LIBGCRYPT_CONFIG --libs`
+	LIBS="$LIBGCRYPT_LIBS $LIBS"
+fi
+
 ################################################################################
 dnl -- Disable readline
 AC_MSG_CHECKING(whether to enable readline)
@@ -755,6 +784,7 @@ AC_SUBST(CLVMD)
 AC_SUBST(CMDLIB)
 AC_SUBST(COPTIMISE_FLAG)
 AC_SUBST(CRYPTO)
+AC_SUBST(CRYPTO_LUKS1)
 AC_SUBST(CSCOPE_CMD)
 AC_SUBST(DEBUG)
 AC_SUBST(DEVMAPPER)
lib/Makefile.in +6 -0
index 7167f04..912d1d9 100644
@@ -131,6 +131,12 @@ ifeq ("@CRYPTO@", "internal")
   SOURCES += crypt/crypt.c
 endif
 
+ifeq ("@CRYPTO_LUKS1@", "yes")
+  SOURCES +=\
+	crypt/key_luks.c \
+	crypt/pbkdf2.c
+endif
+
 ifeq ("@DEVMAPPER@", "yes")
   SOURCES +=\
 	activate/dev_manager.c \
lib/crypt/key_handlers.c +3 -0
index 678a260..4380218 100644
@@ -60,6 +60,9 @@ int lvm_keystore_handlers_init()
 
 	// FIXME: here should be library loading
 	dm_list_add(&_keystore_handlers, &_plain_csh.list);
+#ifdef CRYPT_LUKS1
+	dm_list_add(&_keystore_handlers, &keystore_luks1_init()->list);
+#endif
 
 	return 1;
 }
lib/crypt/key_luks.c new file +495 -0

495 lines not on this page · raw message

lib/crypt/lvm-crypto.h +3 -0
index 42004b7..928ff5b 100644
@@ -107,6 +107,9 @@ int lvm_masterkeys_retrieve(const char *for_text, struct crypto_store *cs);
 int lvm_keystore_handlers_init(void);
 void lvm_keystore_handlers_destroy(void);
 struct crypto_store_type *lvm_get_keystore_handler(const char *name);
+#ifdef CRYPT_LUKS1
+struct crypto_store_type *keystore_luks1_init(void);
+#endif
 
 /*
  * Crypto Helper functions
lib/crypt/pbkdf2.c new file +199 -0

199 lines not on this page · raw message

lib/crypt/pbkdf2.h new file +36 -0
new file mode 100644
index 0000000..3b6cc8e
@@ -0,0 +1,36 @@
+/*
+ * pkcs5_pbkdf2 implementation
+ *
+ * Copyright (C) 2002 Simon Josefsson
+ * Copyright (C) 2009 Red Hat, Inc. All rights reserved.
+ *
+ * This file is part of LVM2.
+ *
+ * This copyrighted material is made available to anyone wishing to use,
+ * modify, copy, or redistribute it subject to the terms and conditions
+ * of the GNU Lesser General Public License v.2.1.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ */
+
+#ifndef PBKDF2_H
+#define PBKDF2_H
+
+#include <gcrypt.h>
+
+/* Error codes */
+enum {
+  PKCS5_OK = 0,
+  PKCS5_INVALID_PRF,
+  PKCS5_INVALID_ITERATION_COUNT,
+  PKCS5_INVALID_DERIVED_KEY_LENGTH,
+  PKCS5_DERIVED_KEY_TOO_LONG
+};
+
+int pkcs5_pbkdf2(int PRF, const char *P, size_t Plen,
+		 const unsigned char *S, size_t Slen,
+		 unsigned int c, unsigned int dkLen, char *DK);
+
+#endif /* PBKDF2_H */
lib/misc/configure.h.in +3 -0
index d171445..1150b34 100644
@@ -14,6 +14,9 @@
 /* Define to 1 to include built-in support for crypto. */
 #undef CRYPT_INTERNAL
 
+/* Define to 1 to include built-in support for LUKS1 keystore. */
+#undef CRYPT_LUKS1
+
 /* Define to 1 if using `alloca.c'. */
 #undef C_ALLOCA
 
Signature · 1 line
1.5.6.5
author
#15 lore raw

PATCH 15/15 Add simple test for crypto volumes.

---
 test/t-crypto-usage.sh |   54 ++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 54 insertions(+), 0 deletions(-)
 create mode 100755 test/t-crypto-usage.sh
test/t-crypto-usage.sh new file +54 -0
new file mode 100755
index 0000000..2462931
@@ -0,0 +1,54 @@
+#!/bin/sh
+# Copyright (C) 2009 Red Hat, Inc. All rights reserved.
+#
+# This copyrighted material is made available to anyone wishing to use,
+# modify, copy, or redistribute it subject to the terms and conditions
+# of the GNU General Public License v.2.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software Foundation,
+# Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+
+# 'Exercise some encrypted volumes operations'
+
+. ./test-utils.sh
+
+aux prepare_pvs 2
+aux pvcreate $devs
+vgcreate -cn $vg $devs -s 4M
+
+cipher="aes-cbc-essiv:sha256"
+key_plain="0123456789012345678901234567890101234567890123456789012345678901"
+keysize=256
+password="xxx"
+
+modprobe dm_crypt
+
+# 'lvcreate creates simple encrypted volume'
+echo $key_plain | lvcreate -n $lv1 -l 4 --crypt plain --cipher $cipher --keysize $keysize --keyfile - $vg
+echo $key_plain | lvcreate -n $lv2 -l 4 --crypt $vg/$lv1 --keyfile - $vg
+
+# 'deactivate volumes'
+lvchange -a n $vg/$lv1
+lvchange -a n $vg/$lv2
+echo $key_plain | vgchange -a y --keyfile -
+
+# 'cannot remove used cryptostore directly
+not lvremove $vg/cryptostore0
+
+# 'resize active volume'
+lvresize -l 8 $vg/$lv1
+lvresize -f -l 4 $vg/$lv1
+
+# 'lvremove removes simple encrypted volume'
+lvremove -ff $vg/$lv1
+lvremove -ff $vg/$lv2
+
+# 'import luks volume, using first extent as keystore (extent is 8192 sectors)'
+lvcreate -n $lv3 -L 36M $vg
+echo -n $password | cryptsetup luksFormat -q $G_dev_/$vg/$lv3 --key-size $keysize --align-payload=8192 --key-file -
+lvconvert --crypt luks1 $vg/$lv3
+echo $password | lvchange -a y $vg/$lv3 --keyfile -
+lvchange -a n $vg/$lv3
+lvremove -ff $vg/$lv3
+
Signature · 1 line
1.5.6.5