Skip to contentSkip to conversation

ptr_ring: fix integer overflow

PATCH In review Jason Wang 3 participants 8 messages Jan 25, 2018 – Jan 30, 2018

Subsystems (2)
lore
Share & tools
lore

Jason Wang

author
#0 lore raw

PATCH 1/1 ptr_ring: fix integer overflow

We try to allocate one more entry for lockless peeking. The adding operation may overflow which causes zero to be passed to kmalloc(). In this case, it returns ZERO_SIZE_PTR without any notice by ptr ring. Try to do producing or consuming on such ring will lead NULL dereference. Fix this detect and fail early.

Fixes bcecb4bbf88a ("net: ptr_ring: otherwise safe empty checks can overrun array bounds")
Reported-by [email protected]
Cc John Fastabend <[email protected]>
---
 include/linux/ptr_ring.h | 2 ++
 1 file changed, 2 insertions(+)
include/linux/ptr_ring.h +2 -0
index 9ca1726..3f99484 100644
@@ -453,6 +453,8 @@ static inline int ptr_ring_consume_batched_bh(struct ptr_ring *r,
 
 static inline void **__ptr_ring_init_queue_alloc(unsigned int size, gfp_t gfp)
 {
+	if (unlikely(size + 1 == 0))
+		return NULL;
 	/* Allocate an extra dummy element at end of ring to avoid consumer head
 	 * or produce head access past the end of the array. Possible when
 	 * producer/consumer operations and __ptr_ring_peek operations run in
Signature · 1 line
2.7.4
#1 lore raw
Quoted 10 lines · Jason Wang

We try to allocate one more entry for lockless peeking. The adding operation may overflow which causes zero to be passed to kmalloc(). In this case, it returns ZERO_SIZE_PTR without any notice by ptr ring. Try to do producing or consuming on such ring will lead NULL dereference. Fix this detect and fail early.

Fixes bcecb4bbf88a ("net: ptr_ring: otherwise safe empty checks can overrun array bounds")
Reported-by [email protected]
Cc John Fastabend <[email protected]>

Ugh that's just way too ugly.
I'll work on dropping the extra + 1 - but calling this function with -1 size is the real source of the bug. Do you know how come we do that?

Quoted 19 lines
---
 include/linux/ptr_ring.h | 2 ++
 1 file changed, 2 insertions(+)
include/linux/ptr_ring.h +2 -0
index 9ca1726..3f99484 100644
@@ -453,6 +453,8 @@ static inline int ptr_ring_consume_batched_bh(struct ptr_ring *r,
 
 static inline void **__ptr_ring_init_queue_alloc(unsigned int size, gfp_t gfp)
 {
+	if (unlikely(size + 1 == 0))
+		return NULL;
 	/* Allocate an extra dummy element at end of ring to avoid consumer head
 	 * or produce head access past the end of the array. Possible when
 	 * producer/consumer operations and __ptr_ring_peek operations run in
Signature · 1 line
2.7.4
author
#2 lore raw
Quoted 7 lines · Michael S. Tsirkin
Quoted 6 lines · Jason Wang

We try to allocate one more entry for lockless peeking. The adding operation may overflow which causes zero to be passed to kmalloc(). In this case, it returns ZERO_SIZE_PTR without any notice by ptr ring. Try to do producing or consuming on such ring will lead NULL dereference. Fix this detect and fail early.

Quoted 9 lines
Quoted 4 lines
Fixes bcecb4bbf88a ("net: ptr_ring: otherwise safe empty checks can overrun array bounds")
Reported-by [email protected]
Cc John Fastabend<[email protected]>

Ugh that's just way too ugly.
I'll work on dropping the extra + 1 - but calling this function with -1 size is the real source of the bug. Do you know how come we do that?

It looks e.g try to change tx_queue_len to UINT_MAX. And we probably can't prevent user form trying to do this?

Thanks

#3 lore raw
Quoted 20 lines · Jason Wang
Quoted 7 lines · Michael S. Tsirkin
Quoted 6 lines · Jason Wang

We try to allocate one more entry for lockless peeking. The adding operation may overflow which causes zero to be passed to kmalloc(). In this case, it returns ZERO_SIZE_PTR without any notice by ptr ring. Try to do producing or consuming on such ring will lead NULL dereference. Fix this detect and fail early.

Quoted 9 lines
Quoted 4 lines
Fixes bcecb4bbf88a ("net: ptr_ring: otherwise safe empty checks can overrun array bounds")
Reported-by [email protected]
Cc John Fastabend<[email protected]>

Ugh that's just way too ugly.
I'll work on dropping the extra + 1 - but calling this function with -1 size is the real source of the bug. Do you know how come we do that?

Quoted 4 lines

It looks e.g try to change tx_queue_len to UINT_MAX. And we probably can't prevent user form trying to do this?

Thanks

Right. BTW why net-next? Isn't the crash exploitable in net?

author
#4 lore raw
Quoted 19 lines · Michael S. Tsirkin
Quoted 18 lines · Jason Wang
Quoted 16 lines · Michael S. Tsirkin
Quoted 10 lines · Jason Wang

We try to allocate one more entry for lockless peeking. The adding operation may overflow which causes zero to be passed to kmalloc(). In this case, it returns ZERO_SIZE_PTR without any notice by ptr ring. Try to do producing or consuming on such ring will lead NULL dereference. Fix this detect and fail early.

Fixes bcecb4bbf88a ("net: ptr_ring: otherwise safe empty checks can overrun array bounds")
Reported-by [email protected]
Cc John Fastabend<[email protected]>

Ugh that's just way too ugly.
I'll work on dropping the extra + 1 - but calling this function with -1 size is the real source of the bug. Do you know how come we do that?

Quoted 6 lines
Quoted 4 lines

It looks e.g try to change tx_queue_len to UINT_MAX. And we probably can't prevent user form trying to do this?

Thanks

Right. BTW why net-next? Isn't the crash exploitable in net?

Commit bcecb4bbf88a exists only in net-next. And in net we check r->size before trying to dereference the queue.

Thanks

#5 lore raw
Quoted 22 lines · Jason Wang
Quoted 19 lines · Michael S. Tsirkin
Quoted 18 lines · Jason Wang
Quoted 16 lines · Michael S. Tsirkin
Quoted 10 lines · Jason Wang

We try to allocate one more entry for lockless peeking. The adding operation may overflow which causes zero to be passed to kmalloc(). In this case, it returns ZERO_SIZE_PTR without any notice by ptr ring. Try to do producing or consuming on such ring will lead NULL dereference. Fix this detect and fail early.

Fixes bcecb4bbf88a ("net: ptr_ring: otherwise safe empty checks can overrun array bounds")
Reported-by [email protected]
Cc John Fastabend<[email protected]>

Ugh that's just way too ugly.
I'll work on dropping the extra + 1 - but calling this function with -1 size is the real source of the bug. Do you know how come we do that?

Quoted 8 lines
Quoted 6 lines
Quoted 4 lines

It looks e.g try to change tx_queue_len to UINT_MAX. And we probably can't prevent user form trying to do this?

Thanks

Right. BTW why net-next? Isn't the crash exploitable in net?

Commit bcecb4bbf88a exists only in net-next.

Right you are.

Quoted 4 lines

And in net we check r->size
before trying to dereference the queue.

Thanks

I was wondering what it's about btw. Does anyone really create 0 size rings?

Signature · 1 line
MST
maintainer
#6 lore raw
In reply to Jason Wang

From: Jason Wang <[email protected]>
Date: Thu, 25 Jan 2018 15:31:42 +0800

Quoted 10 lines

We try to allocate one more entry for lockless peeking. The adding operation may overflow which causes zero to be passed to kmalloc(). In this case, it returns ZERO_SIZE_PTR without any notice by ptr ring. Try to do producing or consuming on such ring will lead NULL dereference. Fix this detect and fail early.

Fixes bcecb4bbf88a ("net: ptr_ring: otherwise safe empty checks can overrun array bounds")
Reported-by [email protected]
Cc John Fastabend <[email protected]>

I'm dropping this because I am to understand that Michael Tsirkin's patch series covers this issue.

Let me know if I still need to apply this.

Thanks.

author
#7 lore raw
Quoted 9 lines · David Miller

From: Jason Wang <[email protected]>
Date: Thu, 25 Jan 2018 15:31:42 +0800

Quoted 6 lines

We try to allocate one more entry for lockless peeking. The adding operation may overflow which causes zero to be passed to kmalloc(). In this case, it returns ZERO_SIZE_PTR without any notice by ptr ring. Try to do producing or consuming on such ring will lead NULL dereference. Fix this detect and fail early.

Quoted 6 lines
Quoted 4 lines
Fixes bcecb4bbf88a ("net: ptr_ring: otherwise safe empty checks can overrun array bounds")
Reported-by [email protected]
Cc John Fastabend <[email protected]>

I'm dropping this because I am to understand that Michael Tsirkin's patch series covers this issue.

Yes.

Quoted 4 lines

Let me know if I still need to apply this.

Thanks.

No need for this.

Thanks