net: check the existence of peer before trying to pad
PATCH 1/1 net: check the existence of peer before trying to pad
There could be case that peer is NULL. This can happen when during network device hot-add where net device needs to be added first. So the patch check the existence of peer before trying to do the pad.
--- include/net/net.h | 5 +++++ net/slirp.c | 2 +- net/tap-win32.c | 2 +- net/tap.c | 2 +- 4 files changed, 8 insertions(+), 3 deletions(-)
include/net/net.h +5 -0
@@ -241,4 +241,9 @@ uint32_t net_crc32_le(const uint8_t *p, int len); .offset = vmstate_offset_macaddr(_state, _field), \ } +static inline bool net_peer_needs_padding (NetClientState *nc) +{ +return nc->peer && !nc->peer->do_not_pad; +} +#endif
net/slirp.c +1 -1
@@ -119,7 +119,7 @@ static ssize_t net_slirp_send_packet(const void *pkt, size_t pkt_len,uint8_t min_pkt[ETH_ZLEN ];size_t min_pktsz =sizeof (min_pkt); -if (!s->nc.peer->do_not_pad) { +if (net_peer_needs_padding (&s->nc)) {if (eth_pad_short_frame (min_pkt, &min_pktsz, pkt, pkt_len)) { pkt = min_pkt; pkt_len = min_pktsz;
net/tap-win32.c +1 -1
@@ -696,7 +696,7 @@ static void tap_win32_send(void *opaque)if (size >0 ) { orig_buf = buf; -if (!s->nc.peer->do_not_pad) { +if (net_peer_needs_padding (&s->nc)) {if (eth_pad_short_frame (min_pkt, &min_pktsz, buf, size)) { buf = min_pkt; size = min_pktsz;
net/tap.c +1 -1
@@ -203,7 +203,7 @@ static void tap_send(void *opaque) size -= s->host_vnet_hdr_len; } -if (!s->nc.peer->do_not_pad) { +if (net_peer_needs_padding (&s->nc)) {if (eth_pad_short_frame (min_pkt, &min_pktsz, buf, size)) { buf = min_pkt; size = min_pktsz;
Signature · 1 line
2.25.1
Quoted 5 lines · Jason Wang
There could be case that peer is NULL. This can happen when during network device hot-add where net device needs to be added first. So the patch check the existence of peer before trying to do the pad.
Quoted 9 lines
Fixes 969e50b61a285 ("net: Pad short frames to minimum size before sending from SLiRP/TAP")Signed-off-by Jason Wang <[email protected]>--- include/net/net.h | 5 +++++ net/slirp.c | 2 +- net/tap-win32.c | 2 +- net/tap.c | 2 +- 4 files changed, 8 insertions(+), 3 deletions(-)
Am 23.04.21 um 05:18 schrieb Jason Wang:
Quoted 18 lines
There could be case that peer is NULL. This can happen when during network device hot-add where net device needs to be added first. So the patch check the existence of peer before trying to do the pad.
Fixes 969e50b61a285 ("net: Pad short frames to minimum size before sending from SLiRP/TAP")Signed-off-by Jason Wang <[email protected]>--- include/net/net.h | 5 +++++ net/slirp.c | 2 +- net/tap-win32.c | 2 +- net/tap.c | 2 +- 4 files changed, 8 insertions(+), 3 deletions(-)include/net/net.h +0
-0
include/net/net.h uint32_t net_crc32_le(const uint8_t *p, int len); · line 241
Quoted 1 line
include/net/net.h +0
-0@@ -241,4 +241,9 @@ uint32_t net_crc32_le(const uint8_t *p, int len); .offset = vmstate_offset_macaddr(_state, _field),
\
Quoted 39 lines
include/net/net.h +5
-0} +static inline bool net_peer_needs_padding (NetClientState *nc) +{ +return nc->peer && !nc->peer->do_not_pad; +} +#endif net/slirp.c +1
-1@@ -119,7 +119,7 @@ static ssize_t net_slirp_send_packet(const void *pkt, size_t pkt_len,uint8_t min_pkt[ETH_ZLEN ];size_t min_pktsz =sizeof (min_pkt); -if (!s->nc.peer->do_not_pad) { +if (net_peer_needs_padding (&s->nc)) {if (eth_pad_short_frame (min_pkt, &min_pktsz, pkt, pkt_len)) { pkt = min_pkt; pkt_len = min_pktsz;net/tap-win32.c +1
-1@@ -696,7 +696,7 @@ static void tap_win32_send(void *opaque)if (size >0 ) { orig_buf = buf; -if (!s->nc.peer->do_not_pad) { +if (net_peer_needs_padding (&s->nc)) {if (eth_pad_short_frame (min_pkt, &min_pktsz, buf, size)) { buf = min_pkt; size = min_pktsz;net/tap.c +0
-0
net/tap.c static void tap_send(void *opaque) · line 209
Quoted 8 lines
net/tap.c +1
-1@@ -203,7 +203,7 @@ static void tap_send(void *opaque) size -= s->host_vnet_hdr_len; } -if (!s->nc.peer->do_not_pad) { +if (net_peer_needs_padding (&s->nc)) {if (eth_pad_short_frame (min_pkt, &min_pktsz, buf, size)) { buf = min_pkt; size = min_pktsz;
I assume that you had a test case which triggered that null pointer access? If yes, than this should indeed be applied before releasing 6.0.
The modification is simple enough for a last minute change.
Quoted 61 lines · Stefan Weil
Am 23.04.21 um 05:18 schrieb Jason Wang:
Quoted 18 lines
There could be case that peer is NULL. This can happen when during network device hot-add where net device needs to be added first. So the patch check the existence of peer before trying to do the pad.
Fixes 969e50b61a285 ("net: Pad short frames to minimum size before sending from SLiRP/TAP")Signed-off-by Jason Wang <[email protected]>---
include/net/net.h| 5 +++++
net/slirp.c | 2 +-
net/tap-win32.c | 2 +-
net/tap.c | 2 +-4 files changed, 8 insertions(+), 3 deletions(-)include/net/net.h +0
-0include/net/net.h uint32_t net_crc32_le(const uint8_t *p, int len); · line 241
Quoted 1 line
include/net/net.h +0
-0@@ -241,4 +241,9 @@ uint32_t net_crc32_le(const uint8_t *p, int len); .offset = vmstate_offset_macaddr(_state, _field),\
Quoted 39 lines
include/net/net.h +4
-0} +static inline bool net_peer_needs_padding(NetClientState *nc) +{ + return nc->peer && !nc->peer->do_not_pad; +} + #endifnet/slirp.c +1
-0@@ -119,7 +119,7 @@ static ssize_t net_slirp_send_packet(const void *pkt,size_t pkt_len, uint8_t min_pkt[ETH_ZLEN]; size_t min_pktsz = sizeof(min_pkt); - if (!s->nc.peer->do_not_pad) { + if (net_peer_needs_padding(&s->nc)) { if (eth_pad_short_frame(min_pkt, &min_pktsz, pkt, pkt_len)) { pkt = min_pkt;
pkt_len=min_pktsz;net/tap-win32.c +1
-0@@ -696,7 +696,7 @@ static void tap_win32_send(void *opaque) if (size > 0) { orig_buf = buf; - if (!s->nc.peer->do_not_pad) { + if (net_peer_needs_padding(&s->nc)) { if (eth_pad_short_frame(min_pkt, &min_pktsz, buf, size)) { buf = min_pkt; size = min_pktsz;net/tap.c +0
-0@@ -203,7 +203,7 @@ static void tap_send(void *opaque) size -= s->host_vnet_hdr_len; }
Quoted 8 lines
net/tap.c static void tap_send(void *opaque) · line 209
Quoted 5 lines
net/tap.c +1
-0- if (!s->nc.peer->do_not_pad) { + if (net_peer_needs_padding(&s->nc)) { if (eth_pad_short_frame(min_pkt, &min_pktsz, buf, size)) { buf = min_pkt; size = min_pktsz;I assume that you had a test case which triggered that null pointer access?
Yes, it's simple to trigger by just adding a tap device and assign an IP to that.
Thanks
Quoted 9 lines
If yes, than this should indeed be applied before releasing 6.0.
The modification is simple enough for a last minute change.
Reviewed-by Stefan Weil <[email protected]>
Peter Maydell
AppliedQuoted 5 lines · Jason Wang
There could be case that peer is NULL. This can happen when during network device hot-add where net device needs to be added first. So the patch check the existence of peer before trying to do the pad.
Quoted 8 lines
Fixes 969e50b61a285 ("net: Pad short frames to minimum size before sending from SLiRP/TAP")Signed-off-by Jason Wang <[email protected]>--- include/net/net.h | 5 +++++ net/slirp.c | 2 +- net/tap-win32.c | 2 +- net/tap.c | 2 +- 4 files changed, 8 insertions(+), 3 deletions(-)
Applied to master for 6.0 rc5; thanks.
-- PMM