Skip to content

Commit 1355680

Browse files
committed
sunvdc: Remove VLA usage
In the quest to remove all stack VLA usage from the kernel[1], this moves the math for cookies calculation into macros and allocates a fixed size array for the maximum number of cookies and adds a runtime sanity check. (Note that the size was always fixed, but just hidden from the compiler.) [1] https://lkml.kernel.org/r/CA+55aFzCG-zNmZwX4A2FQpadafLfEzK6CC=qPXydAacU1RqZWA@mail.gmail.com Cc: Jens Axboe <axboe@kernel.dk> Cc: linux-block@vger.kernel.org Signed-off-by: Kees Cook <keescook@chromium.org>
1 parent e6e097f commit 1355680

File tree

1 file changed

+10
-4
lines changed

1 file changed

+10
-4
lines changed

drivers/block/sunvdc.c

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@ MODULE_VERSION(DRV_MODULE_VERSION);
3636
#define VDC_TX_RING_SIZE 512
3737
#define VDC_DEFAULT_BLK_SIZE 512
3838

39+
#define MAX_XFER_BLKS (128 * 1024)
40+
#define MAX_XFER_SIZE (MAX_XFER_BLKS / VDC_DEFAULT_BLK_SIZE)
41+
#define MAX_RING_COOKIES ((MAX_XFER_BLKS / PAGE_SIZE) + 2)
42+
3943
#define WAITING_FOR_LINK_UP 0x01
4044
#define WAITING_FOR_TX_SPACE 0x02
4145
#define WAITING_FOR_GEN_CMD 0x04
@@ -450,14 +454,17 @@ static int __send_request(struct request *req)
450454
{
451455
struct vdc_port *port = req->rq_disk->private_data;
452456
struct vio_dring_state *dr = &port->vio.drings[VIO_DRIVER_TX_RING];
453-
struct scatterlist sg[port->ring_cookies];
457+
struct scatterlist sg[MAX_RING_COOKIES];
454458
struct vdc_req_entry *rqe;
455459
struct vio_disk_desc *desc;
456460
unsigned int map_perm;
457461
int nsg, err, i;
458462
u64 len;
459463
u8 op;
460464

465+
if (WARN_ON(port->ring_cookies > MAX_RING_COOKIES))
466+
return -EINVAL;
467+
461468
map_perm = LDC_MAP_SHADOW | LDC_MAP_DIRECT | LDC_MAP_IO;
462469

463470
if (rq_data_dir(req) == READ) {
@@ -984,9 +991,8 @@ static int vdc_port_probe(struct vio_dev *vdev, const struct vio_device_id *id)
984991
goto err_out_free_port;
985992

986993
port->vdisk_block_size = VDC_DEFAULT_BLK_SIZE;
987-
port->max_xfer_size = ((128 * 1024) / port->vdisk_block_size);
988-
port->ring_cookies = ((port->max_xfer_size *
989-
port->vdisk_block_size) / PAGE_SIZE) + 2;
994+
port->max_xfer_size = MAX_XFER_SIZE;
995+
port->ring_cookies = MAX_RING_COOKIES;
990996

991997
err = vio_ldc_alloc(&port->vio, &vdc_ldc_cfg, port);
992998
if (err)

0 commit comments

Comments
 (0)