Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

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.

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>
Reviewed-by: Martin K. Petersen <martin.petersen@oracle.com>
Reviewed-by: Stefan Hajnoczi <stefanha@redhat.com>
Signed-off-by: Jens Axboe <axboe@kernel.dk>

authored by

Deepanshu Kartikey and committed by
Jens Axboe
a58383fa 67d85b06

+7 -4
+5 -4
block/ioctl.c
··· 442 442 if (copy_from_user(&read_keys, arg, sizeof(read_keys))) 443 443 return -EFAULT; 444 444 445 - keys_info_len = struct_size(keys_info, keys, read_keys.num_keys); 446 - if (keys_info_len == SIZE_MAX) 445 + if (read_keys.num_keys > PR_KEYS_MAX) 447 446 return -EINVAL; 448 447 449 - keys_info = kzalloc(keys_info_len, GFP_KERNEL); 448 + keys_info_len = struct_size(keys_info, keys, read_keys.num_keys); 449 + 450 + keys_info = kvzalloc(keys_info_len, GFP_KERNEL); 450 451 if (!keys_info) 451 452 return -ENOMEM; 452 453 ··· 474 473 if (copy_to_user(arg, &read_keys, sizeof(read_keys))) 475 474 ret = -EFAULT; 476 475 out: 477 - kfree(keys_info); 476 + kvfree(keys_info); 478 477 return ret; 479 478 } 480 479
+2
include/uapi/linux/pr.h
··· 79 79 #define IOC_PR_READ_KEYS _IOWR('p', 206, struct pr_read_keys) 80 80 #define IOC_PR_READ_RESERVATION _IOR('p', 207, struct pr_read_reservation) 81 81 82 + #define PR_KEYS_MAX (1u << 16) 83 + 82 84 #endif /* _UAPI_PR_H */