Skip to content

Commit 28962ec

Browse files
jasowangmstsirkin
authored andcommitted
virtio_console: validate max_nr_ports before trying to use it
We calculate nr_ports based on the max_nr_ports: nr_queues = use_multiport(portdev) ? (nr_ports + 1) * 2 : 2; If the device advertises a large max_nr_ports, we will end up with a integer overflow. Fixing this by validating the max_nr_ports and fail the probe for invalid max_nr_ports in this case. Cc: Amit Shah <[email protected]> Signed-off-by: Jason Wang <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Michael S. Tsirkin <[email protected]>
1 parent 63b4ffa commit 28962ec

File tree

1 file changed

+9
-0
lines changed

1 file changed

+9
-0
lines changed

drivers/char/virtio_console.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
#include "../tty/hvc/hvc_console.h"
2929

3030
#define is_rproc_enabled IS_ENABLED(CONFIG_REMOTEPROC)
31+
#define VIRTCONS_MAX_PORTS 0x8000
3132

3233
/*
3334
* This is a global struct for storing common data for all the devices
@@ -2036,6 +2037,14 @@ static int virtcons_probe(struct virtio_device *vdev)
20362037
virtio_cread_feature(vdev, VIRTIO_CONSOLE_F_MULTIPORT,
20372038
struct virtio_console_config, max_nr_ports,
20382039
&portdev->max_nr_ports) == 0) {
2040+
if (portdev->max_nr_ports == 0 ||
2041+
portdev->max_nr_ports > VIRTCONS_MAX_PORTS) {
2042+
dev_err(&vdev->dev,
2043+
"Invalidate max_nr_ports %d",
2044+
portdev->max_nr_ports);
2045+
err = -EINVAL;
2046+
goto free;
2047+
}
20392048
multiport = true;
20402049
}
20412050

0 commit comments

Comments
 (0)