Skip to contentSkip to conversation

ptr_ring: wrap back ->producer in __ptr_ring_swap_queue()

PATCH In review Cong Wang 3 participants 4 messages Dec 30, 2018 – Jan 1, 2019

Subsystems (2)
lore
Share & tools
lore

Cong Wang

author
#0 lore raw

PATCH 1/1 ptr_ring: wrap back ->producer in __ptr_ring_swap_queue()

__ptr_ring_swap_queue() tries to move pointers from the old ring to the new one, but it forgets to check if ->producer is beyond the new size at the end of the operation. This leads to an out-of-bound access in __ptr_ring_produce() as reported by syzbot.

Reported-by [email protected]
Fixes 5d49de532002 ("ptr_ring: resize support")
Cc "Michael S. Tsirkin" <[email protected]>
Cc John Fastabend <[email protected]>
Cc Jason Wang <[email protected]>
---
 include/linux/ptr_ring.h | 2 ++
 1 file changed, 2 insertions(+)
include/linux/ptr_ring.h +2 -0
index 6894976b54e3..186cd8e970c7 100644
@@ -573,6 +573,8 @@ static inline void **__ptr_ring_swap_queue(struct ptr_ring *r, void **queue,
 		else if (destroy)
 			destroy(ptr);
 
+	if (producer >= size)
+		producer = 0;
 	__ptr_ring_set_size(r, size);
 	r->producer = producer;
 	r->consumer_head = 0;
Signature · 1 line
2.19.2
Acked-by on v1 1/1
#1 lore raw
Quoted 31 lines · Cong Wang

__ptr_ring_swap_queue() tries to move pointers from the old ring to the new one, but it forgets to check if ->producer is beyond the new size at the end of the operation. This leads to an out-of-bound access in __ptr_ring_produce() as reported by syzbot.

Reported-by [email protected]
Fixes 5d49de532002 ("ptr_ring: resize support")
Cc "Michael S. Tsirkin" <[email protected]>
Cc John Fastabend <[email protected]>
Cc Jason Wang <[email protected]>
---
 include/linux/ptr_ring.h | 2 ++
 1 file changed, 2 insertions(+)
include/linux/ptr_ring.h +2 -0
index 6894976b54e3..186cd8e970c7 100644
@@ -573,6 +573,8 @@ static inline void **__ptr_ring_swap_queue(struct ptr_ring *r, void **queue,
 		else if (destroy)
 			destroy(ptr);
 
+	if (producer >= size)
+		producer = 0;
 	__ptr_ring_set_size(r, size);
 	r->producer = producer;
 	r->consumer_head = 0;
Signature · 1 line
2.19.2

To clarify the commit log a little bit:

if (producer < size)
queue[producer++] = ptr;

if the new size is smaller than the old one, we producer can end up being equal to the new size thus pointing one beyond the end of the array.

So when we allocated one extra entry it was fine, thus maybe we should rather list:

Fixes 9fb582b67072 ("Revert "net: ptr_ring: otherwise safe empty checks can overrun array bounds"")

The patch itself is good though. So

and pls queue this for stable.

Signature · 1 line
MST
author
#2 lore raw
Quoted 5 lines · Michael S. Tsirkin

So when we allocated one extra entry it was fine, thus maybe we should rather list:

Fixes 9fb582b67072 ("Revert "net: ptr_ring: otherwise safe empty checks can overrun array bounds"")

Good point, I totally forgot we once allocated one extra entry.

However, prior to commit bcecb4bbf88a which introduced the extra one entry, this bug exists too, IIUC. Commit bcecb4bbf88a is merged 18 months after commit 5d49de532002 which is supposed to the one introduced this bug.

On the other hand, it does not harm even if we backport this to a kernel release with commit bcecb4bbf88a.

Thanks.

maintainer
#3 lore raw
In reply to Cong Wang

From: Cong Wang <[email protected]>
Date: Sun, 30 Dec 2018 12:43:42 -0800

Quoted 6 lines

__ptr_ring_swap_queue() tries to move pointers from the old ring to the new one, but it forgets to check if ->producer is beyond the new size at the end of the operation. This leads to an out-of-bound access in __ptr_ring_produce() as reported by syzbot.

Quoted 6 lines
Reported-by [email protected]
Fixes 5d49de532002 ("ptr_ring: resize support")
Cc "Michael S. Tsirkin" <[email protected]>
Cc John Fastabend <[email protected]>
Cc Jason Wang <[email protected]>

Applied and queued up for -stable.