Skip to contentSkip to conversation

drivers/misc/vmw_vmci: restrict too big queue size in qp_host_alloc_queue

PATCH Superseded Sabyrzhan Tasbolatov 3 participants 8 messages Feb 6, 2021 – Feb 9, 2021

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 drivers/misc/vmw_vmci: restrict too big queue size in qp_host_alloc_queue

syzbot found WARNING in qp_broker_alloc[1] in qp_host_alloc_queue() when num_pages is 0x100001, giving queue_size + queue_page_size bigger than KMALLOC_MAX_SIZE for kzalloc(), resulting order >= MAX_ORDER condition.

queue_size + queue_page_size=0x8000d8, where KMALLOC_MAX_SIZE=0x400000.

FYI, I've also noticed in vmci_queue_pair.c other SLAB allocations with no length check that might exceed KMALLOC_MAX_SIZE as well,
but syzbot doesn't have reproduces for them.

in qp_alloc_ppn_set():

	produce_ppns =
	    kmalloc_array(num_produce_pages, sizeof(*produce_ppns),
			  GFP_KERNEL);
[..]
	consume_ppns =
	    kmalloc_array(num_consume_pages, sizeof(*consume_ppns),
			  GFP_KERNEL);
[..]
in qp_alloc_hypercall():
	msg_size = sizeof(*alloc_msg) +
	    (size_t) entry->num_ppns * ppn_size;
	alloc_msg = kmalloc(msg_size, GFP_KERNEL);
[..]
in qp_broker_create():
	entry->local_mem = kcalloc(QPE_NUM_PAGES(entry->qp),
					   PAGE_SIZE, GFP_KERNEL);

[1]

Call Trace:
 alloc_pages include/linux/gfp.h:547 [inline]
 kmalloc_order+0x40/0x130 mm/slab_common.c:837
 kmalloc_order_trace+0x15/0x70 mm/slab_common.c:853
 kmalloc_large include/linux/slab.h:481 [inline]
 __kmalloc+0x257/0x330 mm/slub.c:3959
 kmalloc include/linux/slab.h:557 [inline]
 kzalloc include/linux/slab.h:682 [inline]
 qp_host_alloc_queue drivers/misc/vmw_vmci/vmci_queue_pair.c:540 [inline]
 qp_broker_create drivers/misc/vmw_vmci/vmci_queue_pair.c:1351 [inline]
 qp_broker_alloc+0x936/0x2740 drivers/misc/vmw_vmci/vmci_queue_pair.c:1739
---
 drivers/misc/vmw_vmci/vmci_queue_pair.c | 5 +++++
 1 file changed, 5 insertions(+)
drivers/misc/vmw_vmci/vmci_queue_pair.c +5 -0
index c49065887e8f..f6af406fda80 100644
@@ -537,6 +537,11 @@ static struct vmci_queue *qp_host_alloc_queue(u64 size)
 
 	queue_page_size = num_pages * sizeof(*queue->kernel_if->u.h.page);
 
+	if (queue_size + queue_page_size > KMALLOC_MAX_SIZE) {
+		pr_warn("too big queue to allocate\n");
+		return NULL;
+	}
+
 	queue = kzalloc(queue_size + queue_page_size, GFP_KERNEL);
 	if (queue) {
 		queue->q_header = NULL;
Signature · 1 line
2.25.1
maintainer
#1 lore raw
Quoted 54 lines · Sabyrzhan Tasbolatov

syzbot found WARNING in qp_broker_alloc[1] in qp_host_alloc_queue() when num_pages is 0x100001, giving queue_size + queue_page_size bigger than KMALLOC_MAX_SIZE for kzalloc(), resulting order >= MAX_ORDER condition.

queue_size + queue_page_size=0x8000d8, where KMALLOC_MAX_SIZE=0x400000.

FYI, I've also noticed in vmci_queue_pair.c other SLAB allocations with no length check that might exceed KMALLOC_MAX_SIZE as well,
but syzbot doesn't have reproduces for them.

in qp_alloc_ppn_set():

	produce_ppns =
	    kmalloc_array(num_produce_pages, sizeof(*produce_ppns),
			  GFP_KERNEL);
[..]
	consume_ppns =
	    kmalloc_array(num_consume_pages, sizeof(*consume_ppns),
			  GFP_KERNEL);
[..]
in qp_alloc_hypercall():
	msg_size = sizeof(*alloc_msg) +
	    (size_t) entry->num_ppns * ppn_size;
	alloc_msg = kmalloc(msg_size, GFP_KERNEL);
[..]
in qp_broker_create():
	entry->local_mem = kcalloc(QPE_NUM_PAGES(entry->qp),
					   PAGE_SIZE, GFP_KERNEL);

[1]

Call Trace:
 alloc_pages include/linux/gfp.h:547 [inline]
 kmalloc_order+0x40/0x130 mm/slab_common.c:837
 kmalloc_order_trace+0x15/0x70 mm/slab_common.c:853
 kmalloc_large include/linux/slab.h:481 [inline]
 __kmalloc+0x257/0x330 mm/slub.c:3959
 kmalloc include/linux/slab.h:557 [inline]
 kzalloc include/linux/slab.h:682 [inline]
 qp_host_alloc_queue drivers/misc/vmw_vmci/vmci_queue_pair.c:540 [inline]
 qp_broker_create drivers/misc/vmw_vmci/vmci_queue_pair.c:1351 [inline]
 qp_broker_alloc+0x936/0x2740 drivers/misc/vmw_vmci/vmci_queue_pair.c:1739
---
 drivers/misc/vmw_vmci/vmci_queue_pair.c | 5 +++++
 1 file changed, 5 insertions(+)
drivers/misc/vmw_vmci/vmci_queue_pair.c +0 -0
index c49065887e8f..f6af406fda80 100644

drivers/misc/vmw_vmci/vmci_queue_pair.c static struct vmci_queue *qp_host_alloc_queue(u64 size) · line 541

Quoted 5 lines
drivers/misc/vmw_vmci/vmci_queue_pair.c +2 -0
@@ -537,6 +537,11 @@ static struct vmci_queue *qp_host_alloc_queue(u64 size)
 
 	queue_page_size = num_pages * sizeof(*queue->kernel_if->u.h.page);
 
+	if (queue_size + queue_page_size > KMALLOC_MAX_SIZE) {
+		pr_warn("too big queue to allocate\n");

As this is controllable by userspace, you just provided a way to flood the kernel logs.

Please make this a dev_dbg() call instead, if you really want to see it. Otherwise just return NULL, no need to report anything, right?

thanks,

greg k-h

#2 lore raw

PATCH v2 1/1 drivers/misc/vmw_vmci: restrict too big queue size in

syzbot found WARNING in qp_broker_alloc[1] in qp_host_alloc_queue() when num_pages is 0x100001, giving queue_size + queue_page_size bigger than KMALLOC_MAX_SIZE for kzalloc(), resulting order >= MAX_ORDER condition.

queue_size + queue_page_size=0x8000d8, where KMALLOC_MAX_SIZE=0x400000.

---

Quoted 5 lines

As this is controllable by userspace, you just provided a way to flood the kernel logs.

Please make this a dev_dbg() call instead, if you really want to see it. Otherwise just return NULL, no need to report anything, right?

Thanks, removed pr_warn().

v2: Removed pr_warn() to avoid flood from user-space

---
 drivers/misc/vmw_vmci/vmci_queue_pair.c | 2 --
 1 file changed, 2 deletions(-)
drivers/misc/vmw_vmci/vmci_queue_pair.c +0 -2
index f6af406fda80..ea16df73cde0 100644
@@ -538,9 +538,7 @@ static struct vmci_queue *qp_host_alloc_queue(u64 size)
 	queue_page_size = num_pages * sizeof(*queue->kernel_if->u.h.page);
 
 	if (queue_size + queue_page_size > KMALLOC_MAX_SIZE) {
-		pr_warn("too big queue to allocate\n");
 		return NULL;
-	}
 
 	queue = kzalloc(queue_size + queue_page_size, GFP_KERNEL);
 	if (queue) {
Signature · 1 line
2.25.1
#3 lore raw

Re: [PATCH v2] drivers/misc/vmw_vmci: restrict too big queue size in

Quoted 9 lines · Sabyrzhan Tasbolatov

syzbot found WARNING in qp_broker_alloc[1] in qp_host_alloc_queue() when num_pages is 0x100001, giving queue_size + queue_page_size bigger than KMALLOC_MAX_SIZE for kzalloc(), resulting order >= MAX_ORDER condition.

queue_size + queue_page_size=0x8000d8, where KMALLOC_MAX_SIZE=0x400000.

Quoted 7 lines

---

Quoted 5 lines

As this is controllable by userspace, you just provided a way to flood the kernel logs.

Please make this a dev_dbg() call instead, if you really want to see it. Otherwise just return NULL, no need to report anything, right?

Thanks, removed pr_warn().

Looks like you forgot to take out the opening brace.

Quoted 20 lines

v2: Removed pr_warn() to avoid flood from user-space

---
  drivers/misc/vmw_vmci/vmci_queue_pair.c | 2 --
  1 file changed, 2 deletions(-)
drivers/misc/vmw_vmci/vmci_queue_pair.c +0 -2
index f6af406fda80..ea16df73cde0 100644
@@ -538,9 +538,7 @@ static struct vmci_queue *qp_host_alloc_queue(u64 size)
  	queue_page_size = num_pages * sizeof(*queue->kernel_if->u.h.page);
 
  	if (queue_size + queue_page_size > KMALLOC_MAX_SIZE) {
-		pr_warn("too big queue to allocate\n");
  		return NULL;
-	}
 
  	queue = kzalloc(queue_size + queue_page_size, GFP_KERNEL);
  	if (queue) {
#4 lore raw

PATCH v3 1/1 drivers/misc/vmw_vmci: restrict too big queue size in

Quoted 7 lines

syzbot found WARNING in qp_broker_alloc[1] in qp_host_alloc_queue() when num_pages is 0x100001, giving queue_size + queue_page_size bigger than KMALLOC_MAX_SIZE for kzalloc(), resulting order >= MAX_ORDER condition.

queue_size + queue_page_size=0x8000d8, where KMALLOC_MAX_SIZE=0x400000.

Quoted 9 lines

---

Quoted 6 lines
Quoted 5 lines

As this is controllable by userspace, you just provided a way to flood the kernel logs.

Please make this a dev_dbg() call instead, if you really want to see it. Otherwise just return NULL, no need to report anything, right?

Thanks, removed pr_warn().

Looks like you forgot to take out the opening brace.

Cringe moment. Sorry, should've checked it properly first.

v3: Removed opening brace.

---
 drivers/misc/vmw_vmci/vmci_queue_pair.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/misc/vmw_vmci/vmci_queue_pair.c +1 -1
index ea16df73cde0..024dcdbd9d01 100644
@@ -537,7 +537,7 @@ static struct vmci_queue *qp_host_alloc_queue(u64 size)
 
 	queue_page_size = num_pages * sizeof(*queue->kernel_if->u.h.page);
 
-	if (queue_size + queue_page_size > KMALLOC_MAX_SIZE) {
+	if (queue_size + queue_page_size > KMALLOC_MAX_SIZE)
 		return NULL;
 
 	queue = kzalloc(queue_size + queue_page_size, GFP_KERNEL);
Signature · 1 line
2.25.1
maintainer
#5 lore raw

Re: [PATCH v3] drivers/misc/vmw_vmci: restrict too big queue size in

Quoted 7 lines · Sabyrzhan Tasbolatov
Quoted 7 lines

syzbot found WARNING in qp_broker_alloc[1] in qp_host_alloc_queue() when num_pages is 0x100001, giving queue_size + queue_page_size bigger than KMALLOC_MAX_SIZE for kzalloc(), resulting order >= MAX_ORDER condition.

queue_size + queue_page_size=0x8000d8, where KMALLOC_MAX_SIZE=0x400000.

Why is this "quoted"?

Quoted 24 lines
Quoted 9 lines

---

Quoted 6 lines
Quoted 5 lines

As this is controllable by userspace, you just provided a way to flood the kernel logs.

Please make this a dev_dbg() call instead, if you really want to see it. Otherwise just return NULL, no need to report anything, right?

Thanks, removed pr_warn().

Looks like you forgot to take out the opening brace.

Cringe moment. Sorry, should've checked it properly first.

v3: Removed opening brace.

---
 drivers/misc/vmw_vmci/vmci_queue_pair.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/misc/vmw_vmci/vmci_queue_pair.c +0 -0
index ea16df73cde0..024dcdbd9d01 100644

drivers/misc/vmw_vmci/vmci_queue_pair.c static struct vmci_queue *qp_host_alloc_queue(u64 size) · line 541

Quoted 6 lines
drivers/misc/vmw_vmci/vmci_queue_pair.c +1 -1
@@ -537,7 +537,7 @@ static struct vmci_queue *qp_host_alloc_queue(u64 size)
 
 	queue_page_size = num_pages * sizeof(*queue->kernel_if->u.h.page);
 
-	if (queue_size + queue_page_size > KMALLOC_MAX_SIZE) {
+	if (queue_size + queue_page_size > KMALLOC_MAX_SIZE)
 		return NULL;

This patch does not apply to the tree...

thanks,

greg k-h

#6 lore raw

PATCH v4 1/1 drivers/misc/vmw_vmci: restrict too big queue size in qp_host_alloc_queue

syzbot found WARNING in qp_broker_alloc[1] in qp_host_alloc_queue() when num_pages is 0x100001, giving queue_size + queue_page_size bigger than KMALLOC_MAX_SIZE for kzalloc(), resulting order >= MAX_ORDER condition.

queue_size + queue_page_size=0x8000d8, where KMALLOC_MAX_SIZE=0x400000.

[1]

Call Trace:
 alloc_pages include/linux/gfp.h:547 [inline]
 kmalloc_order+0x40/0x130 mm/slab_common.c:837
 kmalloc_order_trace+0x15/0x70 mm/slab_common.c:853
 kmalloc_large include/linux/slab.h:481 [inline]
 __kmalloc+0x257/0x330 mm/slub.c:3959
 kmalloc include/linux/slab.h:557 [inline]
 kzalloc include/linux/slab.h:682 [inline]
 qp_host_alloc_queue drivers/misc/vmw_vmci/vmci_queue_pair.c:540 [inline]
 qp_broker_create drivers/misc/vmw_vmci/vmci_queue_pair.c:1351 [inline]
 qp_broker_alloc+0x936/0x2740 drivers/misc/vmw_vmci/vmci_queue_pair.c:1739

---

Quoted 1 line

This patch does not apply to the tree...

Apologies, it was so stupid from my side.
Tested locally and via syzbot.

v4: made a patch based on commit 65f0d2414b("Merge tag 'sound-5.11-rc4' of git://git.kernel.org/pub/scm/linux/kernel/git/tiwai/sound")

---
 drivers/misc/vmw_vmci/vmci_queue_pair.c | 3 +++
 1 file changed, 3 insertions(+)
drivers/misc/vmw_vmci/vmci_queue_pair.c +3 -0
index c49065887e8f..024dcdbd9d01 100644
@@ -537,6 +537,9 @@ static struct vmci_queue *qp_host_alloc_queue(u64 size)
 
 	queue_page_size = num_pages * sizeof(*queue->kernel_if->u.h.page);
 
+	if (queue_size + queue_page_size > KMALLOC_MAX_SIZE)
+		return NULL;
+
 	queue = kzalloc(queue_size + queue_page_size, GFP_KERNEL);
 	if (queue) {
 		queue->q_header = NULL;
Signature · 1 line
2.25.1
maintainer
#7 lore raw

Re: [PATCH v2] drivers/misc/vmw_vmci: restrict too big queue size in

In reply to Sabyrzhan Tasbolatov
Quoted 28 lines · Sabyrzhan Tasbolatov

syzbot found WARNING in qp_broker_alloc[1] in qp_host_alloc_queue() when num_pages is 0x100001, giving queue_size + queue_page_size bigger than KMALLOC_MAX_SIZE for kzalloc(), resulting order >= MAX_ORDER condition.

queue_size + queue_page_size=0x8000d8, where KMALLOC_MAX_SIZE=0x400000.

---

Quoted 5 lines

As this is controllable by userspace, you just provided a way to flood the kernel logs.

Please make this a dev_dbg() call instead, if you really want to see it. Otherwise just return NULL, no need to report anything, right?

Thanks, removed pr_warn().

v2: Removed pr_warn() to avoid flood from user-space

---
 drivers/misc/vmw_vmci/vmci_queue_pair.c | 2 --
 1 file changed, 2 deletions(-)
drivers/misc/vmw_vmci/vmci_queue_pair.c +0 -0
index f6af406fda80..ea16df73cde0 100644

drivers/misc/vmw_vmci/vmci_queue_pair.c static struct vmci_queue *qp_host_alloc_queue(u64 size) · line 542

Quoted 7 lines
drivers/misc/vmw_vmci/vmci_queue_pair.c +0 -2
@@ -538,9 +538,7 @@ static struct vmci_queue *qp_host_alloc_queue(u64 size)
 	queue_page_size = num_pages * sizeof(*queue->kernel_if->u.h.page);
 
 	if (queue_size + queue_page_size > KMALLOC_MAX_SIZE) {
-		pr_warn("too big queue to allocate\n");
 		return NULL;
-	}
 

This patch doesn't apply to the tree, you can't send me a patch that is relative to your previous one, that doesn't work at all.

And also, test-build your patches always first, to not do so is a bit rude to maintainers...

thanks,

greg k-h