Skip to contentSkip to conversation

Vulnerability report - af_packet.c - CVE-2020-14386

PATCH In review Or Cohen 5 participants 7 messages Sep 3, 2020 – Sep 21, 2020

Subsystems (4)
lore
Share & tools
lore

Or Cohen

author
#0 lore raw

Hi,
I already reported the issue to [email protected] and [email protected] and CVE-2020-14386 was assigned.

The report is as follows: ( a proposed patch and a reproducer are attached)

I discovered a bug which leads to a memory corruption in (net/packet/af_packet.c). It can be exploited to gain root privileges from unprivileged processes.

To create AF_PACKET sockets you need CAP_NET_RAW in your network namespace, which can be acquired by unprivileged processes on systems where unprivileged namespaces are enabled (Ubuntu, Fedora, etc).

I discovered the vulnerability while auditing the 5.7 kernel sources.

The bug occurs in tpacket_rcv function, when calculating the netoff variable (unsigned short), po->tp_reserve (unsigned int) is added to it which can overflow netoff so it gets a small value.

macoff is calculated using: "macoff = netoff - maclen", we can control macoff so it will receive a small value (specifically, smaller then sizeof(struct virtio_net_hdr)).

Later, when running the following code:
...
if (do_vnet &&
virtio_net_hdr_from_skb(skb, h.raw + macoff -
sizeof(struct virtio_net_hdr),
...

If do_vnet is set, and because macoff < sizeof(struct virtio_net_hdr) a pointer to a memory area before the h.raw buffer will be sent to virtio_net_hdr_from_skb. This can lead to an out-of-bounds write of 1-10 bytes, controlled by the user.

The h.raw buffer is allocated in alloc_pg_vec and it's size is controlled by the user.

The stack trace is as follows at the time of the crash: ( linux v5.7 )

#0 memset_erms () at arch/x86/lib/memset_64.S:66
#1 0xffffffff831934a6 in virtio_net_hdr_from_skb (little_endian=<optimized out>, has_data_valid=<optimized out>,
vlan_hlen=<optimized out>, hdr=<optimized out>, skb=<optimized
out>) at ./include/linux/virtio_net.h:134
#2 tpacket_rcv (skb=0xffff8881ef539940, dev=0xffff8881de534000, pt=<optimized out>, orig_dev=<optimized out>)

        at net/packet/af_packet.c:2287

#3 0xffffffff82c52e47 in dev_queue_xmit_nit (skb=0xffff8881ef5391c0, dev=<optimized out>) at net/core/dev.c:2276
#4 0xffffffff82c5e3d4 in xmit_one (more=<optimized out>, txq=<optimized out>, dev=<optimized out>,

            skb=<optimized out>) at net/core/dev.c:3473
#5  dev_hard_start_xmit (first=0xffffc900001c0ff6, dev=0x0
<fixed_percpu_data>, txq=0xa <fixed_percpu_data+10>,
    ret=<optimized out>) at net/core/dev.c:3493

#6 0xffffffff82c5fc7e in __dev_queue_xmit (skb=0xffff8881ef5391c0, sb_dev=<optimized out>) at net/core/dev.c:4052
#7 0xffffffff831982d3 in packet_snd (len=65536, msg=<optimized out>, sock=<optimized out>) 0001-net-packet-fix-overflow-in-tpacket_rcv at net/packet/af_packet.c:2979
#8 packet_sendmsg (sock=<optimized out>, msg=<optimized out>, len=65536) at net/packet/af_packet.c:3004
#9 0xffffffff82be09ed in sock_sendmsg_nosec (msg=<optimized out>, sock=<optimized out>) at net/socket.c:652
#10 sock_sendmsg (sock=0xffff8881e8ff56c0, msg=0xffff8881de56fd88) at net/socket.c:672

Files attached:
A proposed patch - 0001-net-packet-fix-overflow-in-tpacket_rcv.patch A reproducer for the bug - trigger_bug.c

We are currently working on an exploit for getting root privileges from unprivileged context using this bug.

If there is a problem with the patch please let me know and I will fix it.

Or Cohen
Palo Alto Networks

From 3ad04c9555b93ac6a374b0921ad41849caf22067 Mon Sep 17 00:00:00 2001
From: Or Cohen <[email protected]>
Date: Sun, 30 Aug 2020 20:04:51 +0300
Subject: [PATCH] net/packet: fix overflow in tpacket_rcv

Using tp_reserve to calculate netoff can overflow as tp_reserve is unsigned int and netoff is unsigned short.

This may lead to macoff receving a smaller value then sizeof(struct virtio_net_hdr), and if po->has_vnet_hdr is set, an out-of-bounds write will occur when calling virtio_net_hdr_from_skb.

The bug is fixed by converting netoff to unsigned int and checking if it exceeds USHRT_MAX.

Fixes 8913336a7e8d ("packet: add PACKET_RESERVE sockopt")
---
 net/packet/af_packet.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
net/packet/af_packet.c +6 -1
index 29bd405adbbd..d37435906859 100644
@@ -2168,7 +2168,8 @@ static int tpacket_rcv(struct sk_buff *skb, struct net_device *dev,
 	int skb_len = skb->len;
 	unsigned int snaplen, res;
 	unsigned long status = TP_STATUS_USER;
-	unsigned short macoff, netoff, hdrlen;
+	unsigned short macoff, hdrlen;
+	unsigned int netoff;
 	struct sk_buff *copy_skb = NULL;
 	struct timespec64 ts;
 	__u32 ts_status;
@@ -2237,6 +2238,10 @@ static int tpacket_rcv(struct sk_buff *skb, struct net_device *dev,
 		}
 		macoff = netoff - maclen;
 	}
+	if (netoff > USHRT_MAX) {
+		atomic_inc(&po->tp_drops);
+		goto drop_n_restore;
+	}
 	if (po->tp_version <= TPACKET_V2) {
 		if (macoff + snaplen > po->rx_ring.frame_size) {
 			if (po->copy_thresh &&
Signature · 1 line
2.17.1
#1 lore raw

PATCH 1/1 net/packet: fix overflow in tpacket_rcv

From: Or Cohen <[email protected]>

Using tp_reserve to calculate netoff can overflow as tp_reserve is unsigned int and netoff is unsigned short.

This may lead to macoff receving a smaller value then sizeof(struct virtio_net_hdr), and if po->has_vnet_hdr is set, an out-of-bounds write will occur when calling virtio_net_hdr_from_skb.

The bug is fixed by converting netoff to unsigned int and checking if it exceeds USHRT_MAX.

This addresses CVE-2020-14386

Fixes 8913336a7e8d ("packet: add PACKET_RESERVE sockopt")

[ snu: backported to 4.9, changed tp_drops counting/locking ]

---
 net/packet/af_packet.c | 9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)
net/packet/af_packet.c +8 -1
index fb643945e424..b5b79f501541 100644
@@ -2161,7 +2161,8 @@ static int tpacket_rcv(struct sk_buff *skb, struct net_device *dev,
 	int skb_len = skb->len;
 	unsigned int snaplen, res;
 	unsigned long status = TP_STATUS_USER;
-	unsigned short macoff, netoff, hdrlen;
+	unsigned short macoff, hdrlen;
+	unsigned int netoff;
 	struct sk_buff *copy_skb = NULL;
 	struct timespec ts;
 	__u32 ts_status;
@@ -2223,6 +2224,12 @@ static int tpacket_rcv(struct sk_buff *skb, struct net_device *dev,
 		}
 		macoff = netoff - maclen;
 	}
+	if (netoff > USHRT_MAX) {
+		spin_lock(&sk->sk_receive_queue.lock);
+		po->stats.stats1.tp_drops++;
+		spin_unlock(&sk->sk_receive_queue.lock);
+		goto drop_n_restore;
+	}
 	if (po->tp_version <= TPACKET_V2) {
 		if (macoff + snaplen > po->rx_ring.frame_size) {
 			if (po->copy_thresh &&
Signature · 12 lines
2.28.0




Amazon Development Center Germany GmbH
Krausenstr. 38
10117 Berlin
Geschaeftsfuehrung: Christian Schlaeger, Jonathan Weiss
Eingetragen am Amtsgericht Charlottenburg unter HRB 149173 B
Sitz: Berlin
Ust-ID: DE 289 237 879
#2 lore raw

Re: [PATCH] net/packet: fix overflow in tpacket_rcv

Quoted 21 lines · Stefan Nuernberger

From: Or Cohen <[email protected]>

Using tp_reserve to calculate netoff can overflow as tp_reserve is unsigned int and netoff is unsigned short.

This may lead to macoff receving a smaller value then sizeof(struct virtio_net_hdr), and if po->has_vnet_hdr is set, an out-of-bounds write will occur when calling virtio_net_hdr_from_skb.

The bug is fixed by converting netoff to unsigned int and checking if it exceeds USHRT_MAX.

This addresses CVE-2020-14386

Fixes 8913336a7e8d ("packet: add PACKET_RESERVE sockopt")

[ snu: backported to 4.9, changed tp_drops counting/locking ]

Quoted 7 lines
---
 net/packet/af_packet.c | 9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)

What is the git commit id of this patch in Linus's tree?

thanks,

greg k-h

#3 lore raw

Re: [PATCH] net/packet: fix overflow in tpacket_rcv

Quoted 23 lines · Greg Kroah-Hartman
Quoted 22 lines · Stefan Nuernberger

From: Or Cohen <[email protected]>

Using tp_reserve to calculate netoff can overflow as tp_reserve is unsigned int and netoff is unsigned short.

This may lead to macoff receving a smaller value then sizeof(struct virtio_net_hdr), and if po->has_vnet_hdr is set, an out-of-bounds write will occur when calling virtio_net_hdr_from_skb.

The bug is fixed by converting netoff to unsigned int and checking if it exceeds USHRT_MAX.

This addresses CVE-2020-14386

Fixes 8913336a7e8d ("packet: add PACKET_RESERVE sockopt")

[ snu: backported to 4.9, changed tp_drops counting/locking ]

Quoted 9 lines
Quoted 7 lines

---
 net/packet/af_packet.c | 9 ++++++++-

 1 file changed, 8 insertions(+), 1 deletion(-)

What is the git commit id of this patch in Linus's tree?

Sorry, this isn't merged on Linus' tree yet. It's a heads up that the backport isn't straightforward.

Best,
Stefan

Quoted 3 lines

thanks,

greg k-h

Amazon Development Center Germany GmbH
Krausenstr. 38
10117 Berlin
Geschaeftsfuehrung: Christian Schlaeger, Jonathan Weiss Eingetragen am Amtsgericht Charlottenburg unter HRB 149173 B
Sitz: Berlin
Ust-ID: DE 289 237 879

#4 lore raw

Re: [PATCH] net/packet: fix overflow in tpacket_rcv

Quoted 31 lines · Stefan
Quoted 30 lines · Greg Kroah-Hartman
Quoted 29 lines · Stefan Nuernberger

From: Or Cohen <[email protected]>

Using tp_reserve to calculate netoff can overflow as tp_reserve is unsigned int and netoff is unsigned short.

This may lead to macoff receving a smaller value then sizeof(struct virtio_net_hdr), and if po->has_vnet_hdr is set, an out-of-bounds write will occur when calling virtio_net_hdr_from_skb.

The bug is fixed by converting netoff to unsigned int and checking if it exceeds USHRT_MAX.

This addresses CVE-2020-14386

Fixes 8913336a7e8d ("packet: add PACKET_RESERVE sockopt")

[ snu: backported to 4.9, changed tp_drops counting/locking ]

---
 net/packet/af_packet.c | 9 ++++++++-

 1 file changed, 8 insertions(+), 1 deletion(-)
Quoted 5 lines
Quoted 2 lines

What is the git commit id of this patch in Linus's tree?

Sorry, this isn't merged on Linus' tree yet. It's a heads up that the backport isn't straightforward.

Ok, please be more specific about this when sending patches out...

greg k-h

#5 lore raw

Re: [PATCH] net/packet: fix overflow in tpacket_rcv

Hi,

Quoted 32 lines · On Fri, Sep 04, 2020 at 04:36:48PM +0200, [email protected] wrote:
Quoted 31 lines · Stefan
Quoted 30 lines · Greg Kroah-Hartman
Quoted 29 lines · Stefan Nuernberger

From: Or Cohen <[email protected]>

Using tp_reserve to calculate netoff can overflow as tp_reserve is unsigned int and netoff is unsigned short.

This may lead to macoff receving a smaller value then sizeof(struct virtio_net_hdr), and if po->has_vnet_hdr is set, an out-of-bounds write will occur when calling virtio_net_hdr_from_skb.

The bug is fixed by converting netoff to unsigned int and checking if it exceeds USHRT_MAX.

This addresses CVE-2020-14386

Fixes 8913336a7e8d ("packet: add PACKET_RESERVE sockopt")

[ snu: backported to 4.9, changed tp_drops counting/locking ]

---
 net/packet/af_packet.c | 9 ++++++++-

 1 file changed, 8 insertions(+), 1 deletion(-)
Quoted 7 lines
Quoted 5 lines
Quoted 2 lines

What is the git commit id of this patch in Linus's tree?

Sorry, this isn't merged on Linus' tree yet. It's a heads up that the backport isn't straightforward.

Ok, please be more specific about this when sending patches out...

The commit is in Linux' tree now as
acf69c946233259ab4d64f8869d4037a198c7f06 .

Older stable series, which do not have 8e8e2951e309 ("net/packet: make tp_drops atomic") will though need a backport as per above AFICS.

Regards,
Salvatore

#6 lore raw

PATCH 1/1 net/packet: fix overflow in tpacket_rcv

From: Or Cohen <[email protected]>

commit acf69c946233259ab4d64f8869d4037a198c7f06 upstream.

Using tp_reserve to calculate netoff can overflow as tp_reserve is unsigned int and netoff is unsigned short.

This may lead to macoff receving a smaller value then sizeof(struct virtio_net_hdr), and if po->has_vnet_hdr is set, an out-of-bounds write will occur when calling virtio_net_hdr_from_skb.

The bug is fixed by converting netoff to unsigned int and checking if it exceeds USHRT_MAX.

This addresses CVE-2020-14386

Fixes 8913336a7e8d ("packet: add PACKET_RESERVE sockopt")

[ snu: backported to pre-5.3, changed tp_drops counting/locking ]

---
 net/packet/af_packet.c | 9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)
net/packet/af_packet.c +8 -1
index fb643945e424..b5b79f501541 100644
@@ -2161,7 +2161,8 @@ static int tpacket_rcv(struct sk_buff *skb, struct net_device *dev,
 	int skb_len = skb->len;
 	unsigned int snaplen, res;
 	unsigned long status = TP_STATUS_USER;
-	unsigned short macoff, netoff, hdrlen;
+	unsigned short macoff, hdrlen;
+	unsigned int netoff;
 	struct sk_buff *copy_skb = NULL;
 	struct timespec ts;
 	__u32 ts_status;
@@ -2223,6 +2224,12 @@ static int tpacket_rcv(struct sk_buff *skb, struct net_device *dev,
 		}
 		macoff = netoff - maclen;
 	}
+	if (netoff > USHRT_MAX) {
+		spin_lock(&sk->sk_receive_queue.lock);
+		po->stats.stats1.tp_drops++;
+		spin_unlock(&sk->sk_receive_queue.lock);
+		goto drop_n_restore;
+	}
 	if (po->tp_version <= TPACKET_V2) {
 		if (macoff + snaplen > po->rx_ring.frame_size) {
 			if (po->copy_thresh &&
Signature · 12 lines
2.28.0




Amazon Development Center Germany GmbH
Krausenstr. 38
10117 Berlin
Geschaeftsfuehrung: Christian Schlaeger, Jonathan Weiss
Eingetragen am Amtsgericht Charlottenburg unter HRB 149173 B
Sitz: Berlin
Ust-ID: DE 289 237 879