linux-block.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v2] block: add allocation size check in blkdev_pr_read_keys()
@ 2025-12-16  5:11 Deepanshu Kartikey
  2025-12-16  7:46 ` Christoph Hellwig
  0 siblings, 1 reply; 2+ messages in thread
From: Deepanshu Kartikey @ 2025-12-16  5:11 UTC (permalink / raw)
  To: axboe, martin.petersen, stefanha
  Cc: linux-block, linux-kernel, Deepanshu Kartikey,
	syzbot+660d079d90f8a1baf54d

blkdev_pr_read_keys() takes num_keys from userspace and uses it to
calculate the allocation size for keys_info via struct_size(). While
there is a check for SIZE_MAX (integer overflow), there is no upper
bound validation on the allocation size itself.

A malicious or buggy userspace can pass a large num_keys value that
doesn't trigger overflow but still results in an excessive allocation
attempt, causing a warning in the page allocator when the order exceeds
MAX_PAGE_ORDER.

Fix this by introducing PR_KEYS_MAX_NUM to limit the number of keys to
a sane value. This makes the SIZE_MAX check redundant, so remove it.
Also switch to kvzalloc/kvfree to handle larger allocations gracefully.

Fixes: 22a1ffea5f80 ("block: add IOC_PR_READ_KEYS ioctl")
Tested-by: syzbot+660d079d90f8a1baf54d@syzkaller.appspotmail.com
Reported-by: syzbot+660d079d90f8a1baf54d@syzkaller.appspotmail.com
Closes: https://syzkaller.appspot.com/bug?extid=660d079d90f8a1baf54d
Link: https://lore.kernel.org/all/20251212013510.3576091-1-kartikey406@gmail.com/T/ [v1]
Signed-off-by: Deepanshu Kartikey <kartikey406@gmail.com>
---
v2:
  - Added PR_KEYS_MAX_NUM (64K) limit instead of checking KMALLOC_MAX_SIZE
  - Removed redundant SIZE_MAX check
  - Switched to kvzalloc/kvfree
---
 block/ioctl.c | 11 +++++++----
 1 file changed, 7 insertions(+), 4 deletions(-)

diff --git a/block/ioctl.c b/block/ioctl.c
index 61feed686418..98c4c7b9e7fe 100644
--- a/block/ioctl.c
+++ b/block/ioctl.c
@@ -18,6 +18,8 @@
 #include "blk.h"
 #include "blk-crypto-internal.h"
 
+#define PR_KEYS_MAX_NUM		(1u << 16)
+
 static int blkpg_do_ioctl(struct block_device *bdev,
 			  struct blkpg_partition __user *upart, int op)
 {
@@ -442,11 +444,12 @@ static int blkdev_pr_read_keys(struct block_device *bdev, blk_mode_t mode,
 	if (copy_from_user(&read_keys, arg, sizeof(read_keys)))
 		return -EFAULT;
 
-	keys_info_len = struct_size(keys_info, keys, read_keys.num_keys);
-	if (keys_info_len == SIZE_MAX)
+	if (read_keys.num_keys > PR_KEYS_MAX_NUM)
 		return -EINVAL;
 
-	keys_info = kzalloc(keys_info_len, GFP_KERNEL);
+	keys_info_len = struct_size(keys_info, keys, read_keys.num_keys);
+
+	keys_info = kvzalloc(keys_info_len, GFP_KERNEL);
 	if (!keys_info)
 		return -ENOMEM;
 
@@ -473,7 +476,7 @@ static int blkdev_pr_read_keys(struct block_device *bdev, blk_mode_t mode,
 	if (copy_to_user(arg, &read_keys, sizeof(read_keys)))
 		ret = -EFAULT;
 out:
-	kfree(keys_info);
+	kvfree(keys_info);
 	return ret;
 }
 
-- 
2.43.0


^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH v2] block: add allocation size check in blkdev_pr_read_keys()
  2025-12-16  5:11 [PATCH v2] block: add allocation size check in blkdev_pr_read_keys() Deepanshu Kartikey
@ 2025-12-16  7:46 ` Christoph Hellwig
  0 siblings, 0 replies; 2+ messages in thread
From: Christoph Hellwig @ 2025-12-16  7:46 UTC (permalink / raw)
  To: Deepanshu Kartikey
  Cc: axboe, martin.petersen, stefanha, linux-block, linux-kernel,
	syzbot+660d079d90f8a1baf54d

On Tue, Dec 16, 2025 at 10:41:47AM +0530, Deepanshu Kartikey wrote:
> blkdev_pr_read_keys() takes num_keys from userspace and uses it to
> calculate the allocation size for keys_info via struct_size(). While
> there is a check for SIZE_MAX (integer overflow), there is no upper
> bound validation on the allocation size itself.
> 
> A malicious or buggy userspace can pass a large num_keys value that
> doesn't trigger overflow but still results in an excessive allocation
> attempt, causing a warning in the page allocator when the order exceeds
> MAX_PAGE_ORDER.
> 
> Fix this by introducing PR_KEYS_MAX_NUM to limit the number of keys to
> a sane value. This makes the SIZE_MAX check redundant, so remove it.
> Also switch to kvzalloc/kvfree to handle larger allocations gracefully.
> 
> Fixes: 22a1ffea5f80 ("block: add IOC_PR_READ_KEYS ioctl")
> Tested-by: syzbot+660d079d90f8a1baf54d@syzkaller.appspotmail.com
> Reported-by: syzbot+660d079d90f8a1baf54d@syzkaller.appspotmail.com
> Closes: https://syzkaller.appspot.com/bug?extid=660d079d90f8a1baf54d
> Link: https://lore.kernel.org/all/20251212013510.3576091-1-kartikey406@gmail.com/T/ [v1]
> Signed-off-by: Deepanshu Kartikey <kartikey406@gmail.com>
> ---
> v2:
>   - Added PR_KEYS_MAX_NUM (64K) limit instead of checking KMALLOC_MAX_SIZE
>   - Removed redundant SIZE_MAX check
>   - Switched to kvzalloc/kvfree
> ---
>  block/ioctl.c | 11 +++++++----
>  1 file changed, 7 insertions(+), 4 deletions(-)
> 
> diff --git a/block/ioctl.c b/block/ioctl.c
> index 61feed686418..98c4c7b9e7fe 100644
> --- a/block/ioctl.c
> +++ b/block/ioctl.c
> @@ -18,6 +18,8 @@
>  #include "blk.h"
>  #include "blk-crypto-internal.h"
>  
> +#define PR_KEYS_MAX_NUM		(1u << 16)

I think _NUM is redundant here.  Also this should probably go into
the uapi header.


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2025-12-16  7:46 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-12-16  5:11 [PATCH v2] block: add allocation size check in blkdev_pr_read_keys() Deepanshu Kartikey
2025-12-16  7:46 ` Christoph Hellwig

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).