block: add allocation size check in blkdev_pr_read_keys()
PATCH v3 1/1 block: add allocation size check in blkdev_pr_read_keys()
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 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.
---
v3:
- Renamed PR_KEYS_MAX_NUM to PR_KEYS_MAX
- Moved define to include/uapi/linux/pr.h
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 | 9 +++++---- include/uapi/linux/pr.h | 2 ++ 2 files changed, 7 insertions(+), 4 deletions(-)
block/ioctl.c +5 -4
@@ -442,11 +442,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 )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 +474,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; }
include/uapi/linux/pr.h +2 -0
@@ -79,4 +79,6 @@ struct pr_read_reservation {#define IOC_PR_READ_KEYS _IOWR('p', 206, struct pr_read_keys)#define IOC_PR_READ_RESERVATION _IOR('p', 207, struct pr_read_reservation) +#define PR_KEYS_MAX (1u << 16) +#endif /* _UAPI_PR_H */
Signature · 1 line
2.43.0
Quoted 28 lines · Deepanshu Kartikey
blkdev_pr_read_keys()takesnum_keysfrom userspace and uses it to calculate the allocation size forkeys_infoviastruct_size(). While there is a check forSIZE_MAX(integer overflow), there is no upper bound validation on the allocation size itself.A malicious or buggy userspace can pass a large
num_keysvalue that doesn't trigger overflow but still results in an excessive allocation attempt, causing a warning in the page allocator when the order exceedsMAX_PAGE_ORDER.Fix this by introducing
PR_KEYS_MAXto limit the number of keys to a sane value. This makes theSIZE_MAXcheck 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 [email protected]Reported-by [email protected]Signed-off-by Deepanshu Kartikey <[email protected]>---
v3:
- RenamedPR_KEYS_MAX_NUMtoPR_KEYS_MAX
- Moved define toinclude/uapi/linux/pr.h
v2:
- AddedPR_KEYS_MAX_NUM(64K) limit instead of checkingKMALLOC_MAX_SIZE
- Removed redundantSIZE_MAXcheck
- Switched to kvzalloc/kvfree
Quoted 4 lines
--- block/ioctl.c | 9 +++++---- include/uapi/linux/pr.h | 2 ++ 2 files changed, 7 insertions(+), 4 deletions(-)
Deepanshu,
Quoted 4 lines
blkdev_pr_read_keys()takesnum_keysfrom userspace and uses it to calculate the allocation size forkeys_infoviastruct_size(). While there is a check forSIZE_MAX(integer overflow), there is no upper bound validation on the allocation size itself.
LGTM.
Signature · 1 line
Martin K. Petersen
Quoted 11 lines · Deepanshu Kartikey
blkdev_pr_read_keys()takesnum_keysfrom userspace and uses it to calculate the allocation size forkeys_infoviastruct_size(). While there is a check forSIZE_MAX(integer overflow), there is no upper bound validation on the allocation size itself.A malicious or buggy userspace can pass a large
num_keysvalue that doesn't trigger overflow but still results in an excessive allocation attempt, causing a warning in the page allocator when the order exceedsMAX_PAGE_ORDER.[...]
Applied, thanks!
[1/1] block: add allocation size check in blkdev_pr_read_keys()
commit: a58383fa45c706bda3bf4a1955c3a0327dbec7e7
Best regards,
Signature · 1 line
Jens Axboe