Skip to contentSkip to conversation

block: Add ioctl for setting default inline crypto key

RFC PATCH In review Israel Rukshin 5 participants 10 messages Jul 20, 2022 – Jul 28, 2022

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

cover block: Add ioctl for setting default inline crypto key

Hi Jens/Christoph/Eric

I am working to add support for inline encryption/decryption at storage protocols like nvmf over RDMA. Right now, the only way to use the inline crypto feature is via fs-crypt. This patch allows to use this feature also directly on a block device. This patch comes after my former dm-crypt patch for inline encryption was rejected ("[PATCH 1/1] dm crypt: Add inline encryption support") by the maintainers. This alternative solution of a new block ioctl was suggested by Christoph Hellwig and Eric Biggers.

I tested this patch with modified nvme-rdma as the block device. I would like to collect some comments on this approach and I'll send the entire patch set including NVMe/RDMA changes for inline encryption/decryption.

Israel Rukshin (1):
block: Add support for setting inline encryption key per block device

 block/blk-core.c                |   4 +
 block/blk-crypto-internal.h     |  19 +++-
 block/blk-crypto-profile.c      |   1 +
 block/blk-crypto.c              | 156 ++++++++++++++++++++++++++++++++
 block/blk-sysfs.c               |   7 ++
 block/ioctl.c                   |   3 +
 include/linux/blk-crypto.h      |  11 +--
 include/linux/blkdev.h          |   2 +
 include/uapi/linux/blk-crypto.h |  14 +++
 include/uapi/linux/fs.h         |   9 ++
 10 files changed, 217 insertions(+), 9 deletions(-)
 create mode 100644 include/uapi/linux/blk-crypto.h
Signature · 1 line
2.18.2
#1 lore raw

PATCH 1/1 block: Add support for setting inline encryption key per block device

Until now, using inline encryption key has been possible only at filesystem level via fs-crypt. The patch allows to set a default inline encryption key per block device. Once the key is set, all the read commands will be decrypted, and all the write commands will be encrypted. The key can be overridden by another key from a higher level (FS/DM).

To add/remove a key, the patch introduces a block device ioctl:
- BLKCRYPTOSETKEY: set a key with the following attributes:
- crypto_mode: identifier for the encryption algorithm to use
- raw_key_ptr: pointer to a buffer of the raw key
- raw_key_size: size in bytes of the raw key
- data_unit_size: the data unit size to use for en/decryption (must be <= device logical block size)
To remove the key, use the same ioctl with raw_key_size = 0. It is not possible to remove the key when it is in use by any in-flight IO or when the block device is open.

---
 block/blk-core.c                |   4 +
 block/blk-crypto-internal.h     |  19 +++-
 block/blk-crypto-profile.c      |   1 +
 block/blk-crypto.c              | 156 ++++++++++++++++++++++++++++++++
 block/blk-sysfs.c               |   7 ++
 block/ioctl.c                   |   3 +
 include/linux/blk-crypto.h      |  11 +--
 include/linux/blkdev.h          |   2 +
 include/uapi/linux/blk-crypto.h |  14 +++
 include/uapi/linux/fs.h         |   9 ++
 10 files changed, 217 insertions(+), 9 deletions(-)
 create mode 100644 include/uapi/linux/blk-crypto.h
block/blk-core.c +4 -0
index 27fb1357ad4b..80fed8c794d7 100644
@@ -474,6 +474,10 @@ struct request_queue *blk_alloc_queue(int node_id, bool alloc_srcu)
 				PERCPU_REF_INIT_ATOMIC, GFP_KERNEL))
 		goto fail_stats;
 
+#ifdef CONFIG_BLK_INLINE_ENCRYPTION
+	atomic_set(&q->blk_key_use_count, 0);
+#endif
+
 	blk_queue_dma_alignment(q, 511);
 	blk_set_default_limits(&q->limits);
 	q->nr_requests = BLKDEV_DEFAULT_RQ;
block/blk-crypto-internal.h +18 -1
index e6818ffaddbf..33a162697e53 100644
@@ -65,6 +65,11 @@ static inline bool blk_crypto_rq_is_encrypted(struct request *rq)
 	return rq->crypt_ctx;
 }
 
+bool blk_crypto_bio_set_default_ctx(struct bio *bio);
+
+int blk_crypto_ioctl(struct block_device *bdev, fmode_t mode, unsigned int cmd,
+		     void __user *argp);
+
 #else /* CONFIG_BLK_INLINE_ENCRYPTION */
 
 static inline int blk_crypto_sysfs_register(struct request_queue *q)
@@ -105,6 +110,17 @@ static inline bool blk_crypto_rq_is_encrypted(struct request *rq)
 	return false;
 }
 
+static bool blk_crypto_bio_set_default_ctx(struct bio *bio)
+{
+	return false;
+}
+
+static inline int blk_crypto_ioctl(struct block_device *bdev, fmode_t mode,
+				   unsigned int cmd, void __user *argp)
+{
+	return -ENOTTY;
+}
+
 #endif /* CONFIG_BLK_INLINE_ENCRYPTION */
 
 void __bio_crypt_advance(struct bio *bio, unsigned int bytes);
@@ -134,7 +150,8 @@ static inline void bio_crypt_do_front_merge(struct request *rq,
 bool __blk_crypto_bio_prep(struct bio **bio_ptr);
 static inline bool blk_crypto_bio_prep(struct bio **bio_ptr)
 {
-	if (bio_has_crypt_ctx(*bio_ptr))
+	if (bio_has_crypt_ctx(*bio_ptr) ||
+	    blk_crypto_bio_set_default_ctx(*bio_ptr))
 		return __blk_crypto_bio_prep(bio_ptr);
 	return true;
 }
block/blk-crypto-profile.c +1 -0
index 96c511967386..4ca75520b466 100644
@@ -459,6 +459,7 @@ bool blk_crypto_register(struct blk_crypto_profile *profile,
 		return false;
 	}
 	q->crypto_profile = profile;
+	q->blk_key = NULL;
 	return true;
 }
 EXPORT_SYMBOL_GPL(blk_crypto_register);
block/blk-crypto.c +156 -0

179 lines not on this page · raw message

block/blk-sysfs.c +7 -0
index 9b905e9443e4..2d28b17393db 100644
@@ -765,6 +765,13 @@ static void blk_release_queue(struct kobject *kobj)
 
 	might_sleep();
 
+#ifdef CONFIG_BLK_INLINE_ENCRYPTION
+	if (q->blk_key) {
+		blk_crypto_evict_key(q, q->blk_key);
+		kfree_sensitive(q->blk_key);
+	}
+#endif
+
 	percpu_ref_exit(&q->q_usage_counter);
 
 	if (q->poll_stat)
block/ioctl.c +3 -0
index 46949f1b0dba..b5358819de6a 100644
@@ -12,6 +12,7 @@
 #include <linux/pr.h>
 #include <linux/uaccess.h>
 #include "blk.h"
+#include "blk-crypto-internal.h"
 
 static int blkpg_do_ioctl(struct block_device *bdev,
 			  struct blkpg_partition __user *upart, int op)
@@ -532,6 +533,8 @@ static int blkdev_common_ioctl(struct block_device *bdev, fmode_t mode,
 	case BLKTRACESTOP:
 	case BLKTRACETEARDOWN:
 		return blk_trace_ioctl(bdev, cmd, argp);
+	case BLKCRYPTOSETKEY:
+		return blk_crypto_ioctl(bdev, mode, cmd, argp);
 	case IOC_PR_REGISTER:
 		return blkdev_pr_register(bdev, argp);
 	case IOC_PR_RESERVE:
include/linux/blk-crypto.h +3 -8
index 69b24fe92cbf..cea421c9374f 100644
@@ -7,14 +7,7 @@
 #define __LINUX_BLK_CRYPTO_H
 
 #include <linux/types.h>
-
-enum blk_crypto_mode_num {
-	BLK_ENCRYPTION_MODE_INVALID,
-	BLK_ENCRYPTION_MODE_AES_256_XTS,
-	BLK_ENCRYPTION_MODE_AES_128_CBC_ESSIV,
-	BLK_ENCRYPTION_MODE_ADIANTUM,
-	BLK_ENCRYPTION_MODE_MAX,
-};
+#include <uapi/linux/blk-crypto.h>
 
 #define BLK_CRYPTO_MAX_KEY_SIZE		64
 /**
@@ -39,6 +32,7 @@ struct blk_crypto_config {
  * @data_unit_size_bits: log2 of data_unit_size
  * @size: size of this key in bytes (determined by @crypto_cfg.crypto_mode)
  * @raw: the raw bytes of this key.  Only the first @size bytes are used.
+ * @q: if set, this key is used as a default key for this queue.
  *
  * A blk_crypto_key is immutable once created, and many bios can reference it at
  * the same time.  It must not be freed until all bios using it have completed
@@ -49,6 +43,7 @@ struct blk_crypto_key {
 	unsigned int data_unit_size_bits;
 	unsigned int size;
 	u8 raw[BLK_CRYPTO_MAX_KEY_SIZE];
+	struct request_queue *q;
 };
 
 #define BLK_CRYPTO_MAX_IV_SIZE		32
include/linux/blkdev.h +2 -0
index dd552479a15c..b45b13ea2f7d 100644
@@ -429,6 +429,8 @@ struct request_queue {
 #ifdef CONFIG_BLK_INLINE_ENCRYPTION
 	struct blk_crypto_profile *crypto_profile;
 	struct kobject *crypto_kobject;
+	struct blk_crypto_key *blk_key;
+	atomic_t blk_key_use_count;
 #endif
 
 	unsigned int		rq_timeout;
include/uapi/linux/blk-crypto.h new file +14 -0
new file mode 100644
index 000000000000..5a65ebaf6038
@@ -0,0 +1,14 @@
+/* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */
+
+#ifndef __UAPI_LINUX_BLK_CRYPTO_H
+#define __UAPI_LINUX_BLK_CRYPTO_H
+
+enum blk_crypto_mode_num {
+	BLK_ENCRYPTION_MODE_INVALID,
+	BLK_ENCRYPTION_MODE_AES_256_XTS,
+	BLK_ENCRYPTION_MODE_AES_128_CBC_ESSIV,
+	BLK_ENCRYPTION_MODE_ADIANTUM,
+	BLK_ENCRYPTION_MODE_MAX,
+};
+
+#endif /* __UAPI_LINUX_BLK_CRYPTO_H */
include/uapi/linux/fs.h +9 -0
index bdf7b404b3e7..398a77895e96 100644
@@ -121,6 +121,14 @@ struct fsxattr {
 	unsigned char	fsx_pad[8];
 };
 
+struct blk_crypto_set_key_arg {
+	__u32 crypto_mode;
+	__u64 raw_key_ptr;
+	__u32 raw_key_size;
+	__u32 data_unit_size;
+	__u32 reserved[9];	/* must be zero */
+};
+
 /*
  * Flags for the fsx_xflags field
  */
@@ -185,6 +193,7 @@ struct fsxattr {
 #define BLKROTATIONAL _IO(0x12,126)
 #define BLKZEROOUT _IO(0x12,127)
 #define BLKGETDISKSEQ _IOR(0x12,128,__u64)
+#define BLKCRYPTOSETKEY _IOW(0x12, 129, struct blk_crypto_set_key_arg)
 /*
  * A jump here: 130-136 are reserved for zoned block devices
  * (see uapi/linux/blkzoned.h)
Signature · 1 line
2.18.2
maintainer
#2 lore raw

Re: [PATCH 1/1] block: Add support for setting inline encryption key per block device

Hi Israel,

Quoted 18 lines · Israel Rukshin

Until now, using inline encryption key has been possible only at filesystem level via fs-crypt. The patch allows to set a default inline encryption key per block device. Once the key is set, all the read commands will be decrypted, and all the write commands will be encrypted. The key can be overridden by another key from a higher level (FS/DM).

To add/remove a key, the patch introduces a block device ioctl:
- BLKCRYPTOSETKEY: set a key with the following attributes:
- crypto_mode: identifier for the encryption algorithm to use
- raw_key_ptr: pointer to a buffer of the raw key
- raw_key_size: size in bytes of the raw key
- data_unit_size: the data unit size to use for en/decryption (must be <= device logical block size)
To remove the key, use the same ioctl with raw_key_size = 0. It is not possible to remove the key when it is in use by any in-flight IO or when the block device is open.

Quoted 1 line

Thanks for sending this out. I've added [email protected] to Cc, as I think the device-mapper developers need to be aware of this. I also added [email protected], as this is relevant there too.

I'm glad to see a proposal in this area -- this is something that is greatly needed. Chrome OS is looking for something like "dm-crypt with inline crypto support", which this should work for. Android is also looking for something similar with the additional property that filesystems can override the key used.

Some high-level comments about this approach (I'll send some more routine code review nits in a separate email):

block/blk-crypto-internal.h static inline void bio_crypt_do_front_merge(struct request *rq, · line 156

Quoted 9 lines
block/blk-crypto-internal.h +2 -1
@@ -134,7 +150,8 @@ static inline void bio_crypt_do_front_merge(struct request *rq,
 bool __blk_crypto_bio_prep(struct bio **bio_ptr);
 static inline bool blk_crypto_bio_prep(struct bio **bio_ptr)
 {
-	if (bio_has_crypt_ctx(*bio_ptr))
+	if (bio_has_crypt_ctx(*bio_ptr) ||
+	    blk_crypto_bio_set_default_ctx(*bio_ptr))
 		return __blk_crypto_bio_prep(bio_ptr);
 	return true;

This allows upper layers to override the block device key, which as I've mentioned is very useful -- it allows block device encryption and file encryption to be used together without the performance cost of double encryption. Android already needs and uses this type of encryption.

Still, it's important to understand the limitations of this particular way to achieve this type of encryption, since I want to make sure everyone is on board.

First, this implementation currently doesn't provide a way to skip the default key without setting an encryption context. There are actually two cases where the default key must be skipped when there is no encryption context. The first is when the filesystem is doing I/O to an encrypted file, but the filesystem wasn't mounted with the "inlinecrypt" mount option. This could be worked around by requiring "inlinecrypt" if a default key is set; that loses some flexibility, but it could be done. The second case, which can't be worked around at the filesystem level, is that the f2fs garbage collector sometimes has to move the data of encrypted files on-disk without having access to their encryption key.

So we'll actually need a separate indicator for "skip the default key". The simplest solution would be a new flag in the bio. However, to avoid using space in struct bio, it could instead be a special value of the encryption context.

Second, both this solution and dm-based solutions have the property that they allow the default key to be *arbitrarily* overridden by upper layers. That means that there is no *general* guarantee that all data on the device is protected at least as well as the default key. Consider e.g. the default key being overridden by an all-zeros key. Concerns about this sort of architecture were raised on a dm-crypt patchset several years ago; see
https://lore.kernel.org/r/[email protected] and
https://lore.kernel.org/r/[email protected].

The alternative architecture for this that I've had in mind, and which has been prototyped for f2fs
(https://lore.kernel.org/linux-f2fs-devel/[email protected]/T/#u),
is for the filesystem to manage *all* the encryption, and to mix the default key into all file keys. Then, all data on the block device would always be protected by the default key or better, regardless of userspace.

On the other hand, I'd personally be fine with saying that this isn't actually needed, i.e. that allowing arbitrary overriding of the default key is fine, since userspace should just set up the keys properly. For example, Android doesn't need this at all, as it sets up all its keys properly. But I want to make sure that everyone is generally okay with this now, as I personally don't see a fundamental difference between this and what the dm-crypt developers had rejected *very* forcefully before. Perhaps it's acceptable now simply because it wouldn't be part of dm-crypt; it's a new thing, so it can have new semantics.

Quoted 19 lines
block/blk-crypto.c +19 -0
+static int blk_crypto_ioctl_create_key(struct request_queue *q,
+				       void __user *argp)
+{
+	struct blk_crypto_set_key_arg arg;
+	u8 raw_key[BLK_CRYPTO_MAX_KEY_SIZE];
+	struct blk_crypto_key *blk_key;
+	int ret;
+
+	if (copy_from_user(&arg, argp, sizeof(arg)))
+		return -EFAULT;
+
+	if (memchr_inv(arg.reserved, 0, sizeof(arg.reserved)))
+		return -EINVAL;
+
+	if (!arg.raw_key_size)
+		return blk_crypto_destroy_default_key(q);
+
+	if (q->blk_key) {
+		pr_err("Crypto key is already configured\n");

block/blk-crypto.c int blk_crypto_evict_key(struct request_queue *q, · line 501

Quoted 10 lines
block/blk-crypto.c +10 -0
+		return -EBUSY;
+	}
+
+	if (arg.raw_key_size > sizeof(raw_key))
+		return -EINVAL;
+
+	if (arg.data_unit_size > queue_logical_block_size(q)) {
+		pr_err("Data unit size is bigger than logical block size\n");
+		return -EINVAL;
+	}

This is forbidding data_unit_size greater than logical_block_size. I see why you did this: the block layer doesn't know what is above it, and it could receive I/O requests targeting individual logical blocks. However, this can result in a big efficiency loss; a common example is when a filesystem with a 4096-byte block size is used on a block device with a logical block size of 512 bytes. Since such a filesystem only submits I/O in 4096-byte blocks, it's safe for the data_unit_size to be 4096 bytes as well. This is much more efficient than 512 bytes, since there is overhead for every data unit. Note that some of this overhead is intrinsic in the crypto algorithms themselves and cannot be optimized out by better hardware.

The device-mapper based solution wouldn't have this problem, as the device-mapper target (dm-inlinecrypt or whatever) would just advertise the crypto data unit size as the logical block size, like dm-crypt does.

I'm wondering if anyone any thoughts about how to allow data_unit_size > logical_block_size with this patch's approach. I suppose that the ioctl could just allow setting it, and the block layer could fail any I/O that isn't properly aligned to the data_unit_size.

- Eric

#3 lore raw

Re: [PATCH 1/1] block: Add support for setting inline encryption key per block device

Quoted 8 lines · Eric Biggers

On the other hand, I'd personally be fine with saying that this isn't actually needed, i.e. that allowing arbitrary overriding of the default key is fine, since userspace should just set up the keys properly. For example, Android doesn't need this at all, as it sets up all its keys properly. But I want to make sure that everyone is generally okay with this now, as I personally don't see a fundamental difference between this and what the dm-crypt developers had rejected *very* forcefully before. Perhaps it's acceptable now simply because it wouldn't be part of dm-crypt; it's a new thing, so it can have new semantics.

I agree with both the dm-crypt maintainer and you on this. dm-crypt is a full disk encryption solution and has to provide guarantees, so it can't let upper layers degrade the cypher. The block layer support on the other hand is just a building block an can as long as it is carefully documented.

Quoted 4 lines

I'm wondering if anyone any thoughts about how to allow data_unit_size > logical_block_size with this patch's approach. I suppose that the ioctl could just allow setting it, and the block layer could fail any I/O that isn't properly aligned to the data_unit_size.

We could do that, but we'd need to comunicate the limit to the upper layers both in the kernel an user space. Effectively this changes the logical block size for all practical purposes.

#4 lore raw

Re: [dm-devel] [PATCH 1/1] block: Add support for setting inline encryption key per block device

Quoted 10 lines · Christoph Hellwig
Quoted 8 lines · Eric Biggers

On the other hand, I'd personally be fine with saying that this isn't actually needed, i.e. that allowing arbitrary overriding of the default key is fine, since userspace should just set up the keys properly. For example, Android doesn't need this at all, as it sets up all its keys properly. But I want to make sure that everyone is generally okay with this now, as I personally don't see a fundamental difference between this and what the dm-crypt developers had rejected *very* forcefully before. Perhaps it's acceptable now simply because it wouldn't be part of dm-crypt; it's a new thing, so it can have new semantics.

Quoted 5 lines

I agree with both the dm-crypt maintainer and you on this. dm-crypt is a full disk encryption solution and has to provide guarantees, so it can't let upper layers degrade the cypher. The block layer support on the other hand is just a building block an can as long as it is carefully documented.

Yes, that is what I meant when complaining about the last dm-crypt patch.

I think inline encryption for block devices is a good idea; my complaint was integration with dm-crypt - as it can dilute expectations and create a new threat model here.

But I also think that implementing this directly in the block layer makes sense. Perhaps it should be generic enough.
(I can imagine dynamic inline encryption integrated into LVM ... :-)

Quoted 8 lines
Quoted 4 lines

I'm wondering if anyone any thoughts about how to allow data_unit_size > logical_block_size with this patch's approach. I suppose that the ioctl could just allow setting it, and the block layer could fail any I/O that isn't properly aligned to the data_unit_size.

We could do that, but we'd need to comunicate the limit to the upper layers both in the kernel an user space. Effectively this changes the logical block size for all practical purposes.

I think you should support setting logical encryption size from the beginning, at least prepare API for that. It has a big impact on performance. The dm-crypt also requires aligned IO here. We propagate new logical size to upper layers (and also to loop device below, if used).
(Also this revealed hacks in USB enclosures mapping unaligned devices...)

Milan

#5 lore raw

Re: [PATCH 1/1] block: Add support for setting inline encryption key per block device

In reply to Eric Biggers
Quoted 4 lines · Eric Biggers

I'm glad to see a proposal in this area -- this is something that is greatly needed. Chrome OS is looking for something like "dm-crypt with inline crypto support", which this should work for. Android is also looking for something similar with the additional property that filesystems can override the key used.

Yes, this is exciting to see proposals in this area. In ChromeOS we were contemplating ways to upstream Eric's work for Android. This solution should work generally for our use-case, however I would like to add a few extra pieces we were considering.

One thing we were looking for is having an option to pass inline encryption keys via keyrings, similarly to how dm-crypt allows suuplying keys both ways: raw and keyring attached. I would assume that is something that should still be possible with the IOCTL-based approach, though proposed API can make it a bit obscure. I was wondering whether there should be a separate field to allow this kind of differentiation?

The other aspect is the key lifetime. Current implementation doesn't allow to unset the key once set. This is something that would still work with dm setups, presumably, since the key lifetime is tied to the lifetime of the device itself, but may render problematic if this is applied to a raw device or partition of a raw device, I would assume - allowing no ways to rotate the key without reboot. I am not sure if this is a particularly important issue, but something that I wanted to raise for the discussion. This also becomes relevant in the context of the keyring usages, i.e. whether to revoke the key from the block device when the key is removed from the keyring, or assume it is bound at the time of device setup. The dm-crypt follows the latter model, AFAIU, and it is fine to keep it consistent, but then the question comes back to inability to remove the key in the current API in general.

And speaking about dm, the other thing we were looking into is compatibility of inline encryption key setup with dm stacks. Current kernel implementaiton propagates the crypto context through linear and flakey target, we also had initial tests enabling it on thin pools by adding DM_TARGET_PASSES_CRYPTO, which didn't show any problems at first glance (but more testing is required). We believe that an ability to setup multiple different dm targets with different keys over the same physical device is an important use case - and as far as I can tell proposed approach supports it, but wanted to highlight that as well.

--Daniil

#6 lore raw

Re: [PATCH 1/1] block: Add support for setting inline encryption key per block device

In reply to Israel Rukshin

block/blk-crypto.c void bio_crypt_set_ctx(struct bio *bio, const struct blk_crypto_key *key, · line 103

Quoted 3 lines
block/blk-crypto.c +2 -0
+	if (bc->bc_key->q)
+		atomic_inc(&bc->bc_key->q->blk_key_use_count);
 }

I don't think a global atomic for each I/O is a good idea.

block/blk-crypto.c int blk_crypto_evict_key(struct request_queue *q, · line 458

Quoted 10 lines
block/blk-crypto.c +10 -0
+static int blk_crypto_destroy_default_key(struct request_queue *q)
+{
+	int ret;
+
+	if (!q->blk_key)
+		return 0;
+
+	blk_mq_freeze_queue(q);
+	blk_mq_quiesce_queue(q);
+	if (atomic_read(&q->blk_key_use_count)) {

But once the queue is frozen all I/O is gone at least for blk-mq drivers, so I don't think we'll need this as long as we limit the feature to blk-mq drivers. Eventually we'll need to make blk_mq_freeze_queue also track outstanding I/Os on bio based drivers, in which case it will work there as well.

Alternatively we can support evicting the key only if the device is not otherwise opened only.

block/blk-crypto.c int blk_crypto_evict_key(struct request_queue *q, · line 493

Quoted 4 lines
block/blk-crypto.c +4 -0
+	if (q->blk_key) {
+		pr_err("Crypto key is already configured\n");
+		return -EBUSY;
+	}

Don't we nee locking to make this check race free?

block/blk-crypto.c int blk_crypto_evict_key(struct request_queue *q, · line 565

Quoted 10 lines
block/blk-crypto.c +10 -0
+	switch (cmd) {
+	case BLKCRYPTOSETKEY:
+		if (mode & FMODE_EXCL)
+			return blk_crypto_ioctl_create_key(q, argp);
+
+		if (IS_ERR(blkdev_get_by_dev(bdev->bd_dev, mode | FMODE_EXCL,
+					     &bdev)))
+			return -EBUSY;
+		ret = blk_crypto_ioctl_create_key(q, argp);
+		blkdev_put(bdev, mode | FMODE_EXCL);

I think we can just safely require an FMODE_EXCL open here.

include/linux/blkdev.h struct request_queue { · line 434

Quoted 6 lines

#ifdef CONFIG_BLK_INLINE_ENCRYPTION

include/linux/blkdev.h +2 -0
 	struct blk_crypto_profile *crypto_profile;
 	struct kobject *crypto_kobject;
+	struct blk_crypto_key *blk_key;
+	atomic_t blk_key_use_count;
 #endif

Both the existing and new fields really should go into the gendisk, but I think we need to clean up the existing bits first

include/uapi/linux/blk-crypto.h line 12

Quoted 10 lines
include/uapi/linux/blk-crypto.h new file +10 -0
+#ifndef __UAPI_LINUX_BLK_CRYPTO_H
+#define __UAPI_LINUX_BLK_CRYPTO_H
+
+enum blk_crypto_mode_num {
+	BLK_ENCRYPTION_MODE_INVALID,
+	BLK_ENCRYPTION_MODE_AES_256_XTS,
+	BLK_ENCRYPTION_MODE_AES_128_CBC_ESSIV,
+	BLK_ENCRYPTION_MODE_ADIANTUM,
+	BLK_ENCRYPTION_MODE_MAX,
+};

Please make these #defines so that userspace can probe for newly added ones. a _MAX value should not be part of the uapi headers.

Quoted 1 line

+struct blk_crypto_set_key_arg {

Why is this in fs.h and not the new blk-cryto uapi header?

maintainer
#7 lore raw

Re: [PATCH 1/1] block: Add support for setting inline encryption key per block device

In reply to Israel Rukshin
Quoted 11 lines · Israel Rukshin
block/blk-crypto.c +11 -0
+static int blk_crypto_ioctl_create_key(struct request_queue *q,
+				       void __user *argp)
+{
+	struct blk_crypto_set_key_arg arg;
+	u8 raw_key[BLK_CRYPTO_MAX_KEY_SIZE];
+	struct blk_crypto_key *blk_key;
+	int ret;
+
+	if (copy_from_user(&arg, argp, sizeof(arg)))
+		return -EFAULT;
+

block/blk-crypto.c int blk_crypto_evict_key(struct request_queue *q, · line 493

Quoted 10 lines
block/blk-crypto.c +10 -0
+	if (memchr_inv(arg.reserved, 0, sizeof(arg.reserved)))
+		return -EINVAL;
+
+	if (!arg.raw_key_size)
+		return blk_crypto_destroy_default_key(q);
+
+	if (q->blk_key) {
+		pr_err("Crypto key is already configured\n");
+		return -EBUSY;
+	}

Doesn't this need locking so that multiple executions of this ioctl can't run on the block device at the same time?

Also, the key is actually being associated with the request_queue. I thought it was going to be the block_device? Doesn't it have to be the block_device so that different partitions on the same disk can have different settings?

Also, the pr_err should be removed. Likewise in several places below.

Quoted 23 lines
block/blk-crypto.c +23 -0
+
+	if (arg.raw_key_size > sizeof(raw_key))
+		return -EINVAL;
+
+	if (arg.data_unit_size > queue_logical_block_size(q)) {
+		pr_err("Data unit size is bigger than logical block size\n");
+		return -EINVAL;
+	}
+
+	if (copy_from_user(raw_key, u64_to_user_ptr(arg.raw_key_ptr),
+			   arg.raw_key_size)) {
+		ret = -EFAULT;
+		goto err;
+	}
+
+	blk_key = kzalloc(sizeof(*blk_key), GFP_KERNEL);
+	if (!blk_key) {
+		ret = -ENOMEM;
+		goto err;
+	}
+
+	ret = blk_crypto_init_key(blk_key, raw_key, arg.crypto_mode,
+				  sizeof(u64), arg.data_unit_size);

block/blk-crypto.c int blk_crypto_evict_key(struct request_queue *q, · line 526

Quoted 10 lines
block/blk-crypto.c +10 -0
+	if (ret) {
+		pr_err("Failed to init inline encryption key\n");
+		goto key_err;
+	}
+
+	/* Block key size is taken from crypto mode */
+	if (arg.raw_key_size != blk_key->size) {
+		ret = -EINVAL;
+		goto key_err;
+	}

The key size check above happens too late. The easiest solution would be to add the key size as an argument to blk_crypto_init_key(), and make it validate the key size.

block/blk-crypto.c int blk_crypto_evict_key(struct request_queue *q, · line 534

Quoted 3 lines
block/blk-crypto.c +3 -0
+	}
+	blk_key->q = q;
+	q->blk_key = blk_key;

How does this interact with concurrent I/O? Also, doesn't the block device's page cache need to be invalidated when a key is set or removed?

Quoted 8 lines
include/uapi/linux/blk-crypto.h new file +2 -0
new file mode 100644
index 000000000000..5a65ebaf6038
@@ -0,0 +1,14 @@
+/* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */
+

include/uapi/linux/blk-crypto.h line 12

Quoted 10 lines
include/uapi/linux/blk-crypto.h new file +10 -0
+#ifndef __UAPI_LINUX_BLK_CRYPTO_H
+#define __UAPI_LINUX_BLK_CRYPTO_H
+
+enum blk_crypto_mode_num {
+	BLK_ENCRYPTION_MODE_INVALID,
+	BLK_ENCRYPTION_MODE_AES_256_XTS,
+	BLK_ENCRYPTION_MODE_AES_128_CBC_ESSIV,
+	BLK_ENCRYPTION_MODE_ADIANTUM,
+	BLK_ENCRYPTION_MODE_MAX,
+};

Instead of an enum, these should use #define's with explicit numbers. Also, INVALID and MAX shouldn't be included.

Quoted 7 lines
include/uapi/linux/blk-crypto.h new file +2 -0
+
+#endif /* __UAPI_LINUX_BLK_CRYPTO_H */
include/uapi/linux/fs.h +0 -0
index bdf7b404b3e7..398a77895e96 100644

include/uapi/linux/fs.h struct fsxattr { · line 130

Quoted 10 lines
include/uapi/linux/fs.h +7 -0
@@ -121,6 +121,14 @@ struct fsxattr {
 	unsigned char	fsx_pad[8];
 };
 
+struct blk_crypto_set_key_arg {
+	__u32 crypto_mode;
+	__u64 raw_key_ptr;
+	__u32 raw_key_size;
+	__u32 data_unit_size;
+	__u32 reserved[9];	/* must be zero */
+};

The reserved fields should be __u64 so that it's easy to use them for pointers or other 64-bit data later if it becomes necessary. Also, reserved fields should also be prefixed with "__". Also, a padding field is needed between crypto_mode and raw_key_ptr. Also, it would be a good idea to make the reserved space even larger, as people may want to add all sorts of weird settings here in the future (look at dm-crypt).

The following might be good; it makes the whole struct a nice round size, 128 bytes:

struct blk_crypto_set_key_arg {
	__u32 crypto_mode;
	__u32 __reserved1;
	__u64 raw_key_ptr;
	__u32 raw_key_size;
	__u32 data_unit_size;
	__u64 __reserved2[13];
};

- Eric

#8 lore raw

Re: [PATCH 1/1] block: Add support for setting inline encryption key per block device

Hi Eric,

Thanks for the review!

Quoted 16 lines · Eric Biggers
Quoted 15 lines · Israel Rukshin
block/blk-crypto.c +15 -0
+static int blk_crypto_ioctl_create_key(struct request_queue *q,
+				       void __user *argp)
+{
+	struct blk_crypto_set_key_arg arg;
+	u8 raw_key[BLK_CRYPTO_MAX_KEY_SIZE];
+	struct blk_crypto_key *blk_key;
+	int ret;
+
+	if (copy_from_user(&arg, argp, sizeof(arg)))
+		return -EFAULT;
+
+	if (memchr_inv(arg.reserved, 0, sizeof(arg.reserved)))
+		return -EINVAL;
+
+	if (!arg.raw_key_size)
Quoted 8 lines

block/blk-crypto.c int blk_crypto_evict_key(struct request_queue *q, · line 493

Quoted 6 lines
block/blk-crypto.c +6 -0
+		return blk_crypto_destroy_default_key(q);
+
+	if (q->blk_key) {
+		pr_err("Crypto key is already configured\n");
+		return -EBUSY;
+	}

Doesn't this need locking so that multiple executions of this ioctl can't run on the block device at the same time?

Yes, it should and I will add on V2.

Quoted 4 lines

Also, the key is actually being associated with the request_queue. I thought it was going to be the block_device? Doesn't it have to be the block_device so that different partitions on the same disk can have different settings?

I will check your suggestion.
Currently, all the partitions on the disk have the same key.

Quoted 40 lines

Also, the pr_err should be removed. Likewise in several places below.

Quoted 23 lines
block/blk-crypto.c +23 -0
+
+	if (arg.raw_key_size > sizeof(raw_key))
+		return -EINVAL;
+
+	if (arg.data_unit_size > queue_logical_block_size(q)) {
+		pr_err("Data unit size is bigger than logical block size\n");
+		return -EINVAL;
+	}
+
+	if (copy_from_user(raw_key, u64_to_user_ptr(arg.raw_key_ptr),
+			   arg.raw_key_size)) {
+		ret = -EFAULT;
+		goto err;
+	}
+
+	blk_key = kzalloc(sizeof(*blk_key), GFP_KERNEL);
+	if (!blk_key) {
+		ret = -ENOMEM;
+		goto err;
+	}
+
+	ret = blk_crypto_init_key(blk_key, raw_key, arg.crypto_mode,
+				  sizeof(u64), arg.data_unit_size);

block/blk-crypto.c int blk_crypto_evict_key(struct request_queue *q, · line 526

Quoted 10 lines
block/blk-crypto.c +10 -0
+	if (ret) {
+		pr_err("Failed to init inline encryption key\n");
+		goto key_err;
+	}
+
+	/* Block key size is taken from crypto mode */
+	if (arg.raw_key_size != blk_key->size) {
+		ret = -EINVAL;
+		goto key_err;
+	}

The key size check above happens too late. The easiest solution would be to add the key size as an argument to blk_crypto_init_key(), and make it validate the key size.

Quoted 5 lines

block/blk-crypto.c int blk_crypto_evict_key(struct request_queue *q, · line 534

Quoted 3 lines
block/blk-crypto.c +3 -0
+	}
+	blk_key->q = q;
+	q->blk_key = blk_key;

How does this interact with concurrent I/O? Also, doesn't the block device's page cache need to be invalidated when a key is set or removed?

In case of removing the key, I am freezing the queue to avoid concurrent I/O. I can do the same thing when setting the key.
Setting a key when running I/O is not well defined.
At this implementation, only part the concurrent I/O will be encrypted. After the ioctl is done then all the new I/O will be encrypted.

Quoted 57 lines
Quoted 8 lines
include/uapi/linux/blk-crypto.h new file +2 -0
new file mode 100644
index 000000000000..5a65ebaf6038
@@ -0,0 +1,14 @@
+/* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */
+

include/uapi/linux/blk-crypto.h line 12

Quoted 10 lines
include/uapi/linux/blk-crypto.h new file +10 -0
+#ifndef __UAPI_LINUX_BLK_CRYPTO_H
+#define __UAPI_LINUX_BLK_CRYPTO_H
+
+enum blk_crypto_mode_num {
+	BLK_ENCRYPTION_MODE_INVALID,
+	BLK_ENCRYPTION_MODE_AES_256_XTS,
+	BLK_ENCRYPTION_MODE_AES_128_CBC_ESSIV,
+	BLK_ENCRYPTION_MODE_ADIANTUM,
+	BLK_ENCRYPTION_MODE_MAX,
+};

Instead of an enum, these should use #define's with explicit numbers. Also, INVALID and MAX shouldn't be included.

Quoted 7 lines
include/uapi/linux/blk-crypto.h new file +2 -0
+
+#endif /* __UAPI_LINUX_BLK_CRYPTO_H */
include/uapi/linux/fs.h +0 -0
index bdf7b404b3e7..398a77895e96 100644

include/uapi/linux/fs.h struct fsxattr { · line 130

Quoted 10 lines
include/uapi/linux/fs.h +7 -0
@@ -121,6 +121,14 @@ struct fsxattr {
  	unsigned char	fsx_pad[8];
  };
 
+struct blk_crypto_set_key_arg {
+	__u32 crypto_mode;
+	__u64 raw_key_ptr;
+	__u32 raw_key_size;
+	__u32 data_unit_size;
+	__u32 reserved[9];	/* must be zero */
+};

The reserved fields should be __u64 so that it's easy to use them for pointers or other 64-bit data later if it becomes necessary. Also, reserved fields should also be prefixed with "__". Also, a padding field is needed between crypto_mode and raw_key_ptr. Also, it would be a good idea to make the reserved space even larger, as people may want to add all sorts of weird settings here in the future (look at dm-crypt).

The following might be good; it makes the whole struct a nice round size, 128 bytes:

struct blk_crypto_set_key_arg {
	__u32 crypto_mode;
	__u32 __reserved1;
	__u64 raw_key_ptr;
	__u32 raw_key_size;
	__u32 data_unit_size;
	__u64 __reserved2[13];
};
Quoted 1 line

- Eric

Israel

#9 lore raw
In reply to Israel Rukshin
Quoted 4 lines · Israel Rukshin

Hi Jens/Christoph/Eric

I am working to add support for inline encryption/decryption at storage protocols like nvmf over RDMA.

What would that be? nvmf over RDMA suggest on-the-wire transport encryption, which really is a different think that what fscrypt and blk-crypto deal with. NVMe has a Key per I/O TP, which is still waiting for the messy TCG counter part, but as far as I can tell it is in many ways designed to more or less intentionally not fit the fscrypt model.