>From dcca9cecaa77cf362e694031de080540f55d6daf Mon Sep 17 00:00:00 2001 From: Hyeonjun Ahn <guswns0863@xxxxxxxxx> Date: Thu, 9 Nov 2023 11:41:02 +0900 Subject: [PATCH] nbd: limit the number of connections per config Add max_connections to prevent out-of-memory in nbd_add_socket. Signed-off-by: Hyeonjun Ahn <guswns0863@xxxxxxxxx> Reviewed-by: Josef Bacik <josef@xxxxxxxxxxxxxx> Reviewed-by: Jens Axboe <axboe@xxxxxxxxx> --- drivers/block/nbd.c | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/drivers/block/nbd.c b/drivers/block/nbd.c index 800f131222fc..69f7fe0d07d6 100644 --- a/drivers/block/nbd.c +++ b/drivers/block/nbd.c @@ -162,6 +162,7 @@ static struct dentry *nbd_dbg_dir; static unsigned int nbds_max = 16; static int max_part = 16; static int part_shift; +static unsigned long max_connections = PAGE_SIZE / sizeof(struct nbd_sock *); static int nbd_dev_dbg_init(struct nbd_device *nbd); static void nbd_dev_dbg_close(struct nbd_device *nbd); @@ -1117,6 +1118,13 @@ static int nbd_add_socket(struct nbd_device *nbd, unsigned long arg, /* Arg will be cast to int, check it to avoid overflow */ if (arg > INT_MAX) return -EINVAL; + + if (config->num_connections >= max_connections) { + dev_err(disk_to_dev(nbd->disk), + "Number of socket connections exceeded limit.\n"); + return -ENOMEM; + } + sock = nbd_get_socket(nbd, arg, &err); if (!sock) return err; -- 2.34.1 2023년 11월 9일 (목) 오전 12:34, Jens Axboe <axboe@xxxxxxxxx>님이 작성: > > On 11/7/23 9:30 PM, Hyeonjun Ahn wrote: > > Thanks for your reviews. > > Below is the modified patch. > > Please send it with a CC to linux-block, as that is the proper list. > You'll need to ensure it's just plain text, looks like it's html right > now. > > And also add a proper commit message. > > -- > Jens Axboe >
Attachment:
0001-nbd-limit-the-number-of-connections-per-config.patch
Description: Binary data