Skip to contentSkip to conversation

net/rose: Fix Use-After-Free in rose_ioctl

PATCH Superseded Hyunwoo Kim 2 participants 4 messages Dec 6, 2023

Subsystems (2)

v2 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 net/rose: Fix Use-After-Free in rose_ioctl

Because rose_ioctl() accesses sk->sk_receive_queue without holding a sk->sk_receive_queue.lock, it can cause a race with rose_accept().
A use-after-free for skb occurs with the following flow.

```
rose_ioctl() -> skb_peek()
rose_accept() -> skb_dequeue() -> kfree_skb()
```

Add sk->sk_receive_queue.lock to rose_ioctl() to fix this issue.

---
v1 -> v2: Use sk->sk_receive_queue.lock instead of lock_sock.

---
 net/rose/af_rose.c | 2 ++
 1 file changed, 2 insertions(+)
net/rose/af_rose.c +2 -0
index 0cc5a4e19900..841c238de222 100644
@@ -1316,8 +1316,10 @@ static int rose_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
 		struct sk_buff *skb;
 		long amount = 0L;
 		/* These two are safe on a single CPU system as only user tasks fiddle here */
+		spin_lock(&sk->sk_receive_queue.lock);
 		if ((skb = skb_peek(&sk->sk_receive_queue)) != NULL)
 			amount = skb->len;
+		spin_unlock(&sk->sk_receive_queue.lock);
 		return put_user(amount, (unsigned int __user *) argp);
 	}
 
Signature · 1 line
2.25.1
#1 lore raw
Quoted 11 lines · Hyunwoo Kim

Because rose_ioctl() accesses sk->sk_receive_queue without holding a sk->sk_receive_queue.lock, it can cause a race with rose_accept().
A use-after-free for skb occurs with the following flow.

```
rose_ioctl() -> skb_peek()
rose_accept() -> skb_dequeue() -> kfree_skb()
```

Add sk->sk_receive_queue.lock to rose_ioctl() to fix this issue.

Please add a Fixes: tag

Quoted 12 lines

---
v1 -> v2: Use sk->sk_receive_queue.lock instead of lock_sock.

---
 net/rose/af_rose.c | 2 ++
 1 file changed, 2 insertions(+)
net/rose/af_rose.c +0 -0
index 0cc5a4e19900..841c238de222 100644

net/rose/af_rose.c static int rose_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg) · line 1319

Quoted 4 lines
net/rose/af_rose.c +1 -0
@@ -1316,8 +1316,10 @@ static int rose_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
                struct sk_buff *skb;
                long amount = 0L;
                /* These two are safe on a single CPU system as only user tasks fiddle here */
+               spin_lock(&sk->sk_receive_queue.lock);

You need interrupt safety here.

sk_receive_queue can be fed from interrupt, that would potentially deadlock.

Quoted 9 lines
net/rose/af_rose.c +1 -1
                if ((skb = skb_peek(&sk->sk_receive_queue)) != NULL)
                        amount = skb->len;
+               spin_unlock(&sk->sk_receive_queue.lock);
                return put_user(amount, (unsigned int __user *) argp);
        }
 
--

2.25.1

#2 lore raw

Dear,

Quoted 34 lines · Eric Dumazet
Quoted 11 lines · Hyunwoo Kim

Because rose_ioctl() accesses sk->sk_receive_queue without holding a sk->sk_receive_queue.lock, it can cause a race with rose_accept().
A use-after-free for skb occurs with the following flow.

```
rose_ioctl() -> skb_peek()
rose_accept() -> skb_dequeue() -> kfree_skb()
```

Add sk->sk_receive_queue.lock to rose_ioctl() to fix this issue.

Please add a Fixes: tag

Quoted 12 lines

---
v1 -> v2: Use sk->sk_receive_queue.lock instead of lock_sock.

---
 net/rose/af_rose.c | 2 ++
 1 file changed, 2 insertions(+)
net/rose/af_rose.c +0 -0
index 0cc5a4e19900..841c238de222 100644

net/rose/af_rose.c static int rose_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg) · line 1319

Quoted 4 lines
net/rose/af_rose.c +1 -0
@@ -1316,8 +1316,10 @@ static int rose_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
                struct sk_buff *skb;
                long amount = 0L;
                /* These two are safe on a single CPU system as only user tasks fiddle here */
+               spin_lock(&sk->sk_receive_queue.lock);

You need interrupt safety here.

Quoted 1 line

sk_receive_queue can be fed from interrupt, that would potentially deadlock.

I want to change spin_lock to spin_lock_irqsave, is this okay?

Regards,
Hyunwoo Kim

Quoted 10 lines
Quoted 10 lines
net/rose/af_rose.c +1 -1
                if ((skb = skb_peek(&sk->sk_receive_queue)) != NULL)
                        amount = skb->len;
+               spin_unlock(&sk->sk_receive_queue.lock);
                return put_user(amount, (unsigned int __user *) argp);
        }
 
--

2.25.1

#3 lore raw
Quoted 38 lines · Hyunwoo Kim

Dear,

Quoted 34 lines · Eric Dumazet
Quoted 11 lines · Hyunwoo Kim

Because rose_ioctl() accesses sk->sk_receive_queue without holding a sk->sk_receive_queue.lock, it can cause a race with rose_accept().
A use-after-free for skb occurs with the following flow.

```
rose_ioctl() -> skb_peek()
rose_accept() -> skb_dequeue() -> kfree_skb()
```

Add sk->sk_receive_queue.lock to rose_ioctl() to fix this issue.

Please add a Fixes: tag

Quoted 12 lines

---
v1 -> v2: Use sk->sk_receive_queue.lock instead of lock_sock.

---
 net/rose/af_rose.c | 2 ++
 1 file changed, 2 insertions(+)
net/rose/af_rose.c +0 -0
index 0cc5a4e19900..841c238de222 100644

net/rose/af_rose.c static int rose_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg) · line 1319

Quoted 4 lines
net/rose/af_rose.c +1 -0
@@ -1316,8 +1316,10 @@ static int rose_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
                struct sk_buff *skb;
                long amount = 0L;
                /* These two are safe on a single CPU system as only user tasks fiddle here */
+               spin_lock(&sk->sk_receive_queue.lock);

You need interrupt safety here.

Quoted 3 lines
Quoted 1 line

sk_receive_queue can be fed from interrupt, that would potentially deadlock.

I want to change spin_lock to spin_lock_irqsave, is this okay?

Either spin_lock_irq() or spin_lock_irqsave() will work.

Quoted 15 lines

Regards,
Hyunwoo Kim

Quoted 10 lines
Quoted 10 lines
net/rose/af_rose.c +1 -1
                if ((skb = skb_peek(&sk->sk_receive_queue)) != NULL)
                        amount = skb->len;
+               spin_unlock(&sk->sk_receive_queue.lock);
                return put_user(amount, (unsigned int __user *) argp);
        }
 
--

2.25.1