Commit f448e6d9cf for qemu.org
commit f448e6d9cf0a637234ab3c9825c390a35971f7c3
Author: Peter Maydell <peter.maydell@linaro.org>
Date: Tue Nov 10 18:06:54 2026 +0000
net: Pad short packets consistently regardless of backend
Currently our handling of short network packets (i.e. those which
are smaller than the minimum ethernet frame size of 60 bytes) is a
little confused. The intention is that we allow network device
models to transmit short packets, but we protect them from having to
deal with receiving short packets unless they explicitly opt into it.
This means we don't have to have a lot of code in each network device
to try to handle the short packet size -- historically that code was
untested, and often a source of "carefully crafted packet can cause
an out-of-bounds memory access" bugs.
Our implementation of the padding is currently done when each network
backend feeds a packet into the system via one of the
qemu_send_packet family of functions. This mostly works, except that
we missed some of the more obscure backends (e.g. the socket
backend), and it means code duplication in each backend. We'd like
to switch to making qemu_send_packet do the padding itself. To do
that, we first need to make sure that all the NetClientState
implementations which do not want padding set the do_not_pad flag.
We do this by setting the flag for NetClientState instances created
by qemu_new_net_client() and qemu_new_net_control_client(). Emulated
network devices use qemu_new_nic(), which does not set the flag.
Note that this also causes the net/hub client to request no padding.
This is the right behaviour: we want the hub to accept short packets
and forward them on to all destinations. Each destination then gets
to choose whether it sees a padded packet or the short one. The
hub.c code is length-agnostic and doesn't look inside the packets it
is passing on, so it has no issues with short packets.
That change means we can rely on do_not_pad being correct for all
NetClientInfo structs, and so we can pad short packets in the
qemu_send_packet() and qemu_sendv_packet() family of functions.
If the callsite is already padding, then this will have no effect,
but callsites like the socket and dgram backends will now correctly
add padding. This fixes the bug where when two QEMU instances are
connected via one of those backends a short ARP packet transmitted by
one is not correctly padded by the other and gets dropped.
Note that if we pad a packet, our return value on success will
be the size of the padded packet, not the original. This is OK
because this function (like the NetQueueDeliverFunc function type
documented in include/net/queue.h and which we will eventually
end up calling and returning the return value of) returns
>0 - success
0 - queue packet for future redelivery
<0 - failure (discard packet)
and doesn't guarantee to return exactly the size of the packet
sent. None of the callers try to use the return value as an
exact size.
Cc: qemu-stable@nongu.org
Fixes: CVE-2026-118490
Resolves: https://gitlab.com/qemu-project/qemu/-/work_items/2362
Signed-off-by: Peter Maydell <peter.maydell@linaro.org>
Reviewed-by: Marc-André Lureau <marcandre.lureau@redhat.com>
Message-id: 20261110180655.2072265-3-peter.maydell@linaro.org
diff --git a/include/net/eth.h b/include/net/eth.h
index 3e83f5d68c..a1bc0c2fd5 100644
--- a/include/net/eth.h
+++ b/include/net/eth.h
@@ -439,4 +439,26 @@ eth_parse_ipv6_hdr(const struct iovec *pkt, int pkt_frags,
bool eth_pad_short_frame(uint8_t *padded_pkt, size_t *padded_buflen,
const void *pkt, size_t pkt_size);
+/**
+ * eth_pad_short_frame_iov - pad a short frame, iovec version
+ *
+ * This is equivalent to eth_pad_short_frame(), except that it works
+ * on a packet stored in a struct iovec.
+ *
+ * If the Ethernet frame size is shorter than 60 bytes, it will be padded to
+ * 60 bytes at the address @padded_pkt (and the caller will need to handle
+ * it as a not-in-an-iovec packet).
+ *
+ * @padded_pkt: buffer address to hold the padded frame
+ * @padded_buflen: pointer holding length of @padded_pkt. If the frame is
+ * padded, the length will be updated to the padded one.
+ * @iov: iovec holding the original Ethernet frame
+ * @iovcnt: number of elements in @iov
+ * @pkt_size: size of the original Ethernet frame (i.e. iov_size(iov, iovcnt))
+ * @return true if the frame is padded, otherwise false
+ */
+bool eth_pad_short_frame_iov(uint8_t *padded_pkt, size_t *padded_buflen,
+ const struct iovec *iov, int iovcnt,
+ size_t pkt_size);
+
#endif
diff --git a/include/net/net.h b/include/net/net.h
index de4f03c301..c2fd451f17 100644
--- a/include/net/net.h
+++ b/include/net/net.h
@@ -171,7 +171,8 @@ void qemu_net_client_setup(NetClientState *nc,
const char *model,
const char *name,
NetClientDestructor *destructor,
- bool is_datapath);
+ bool is_datapath,
+ bool do_not_pad);
NetClientState *qemu_new_net_client(NetClientInfo *info,
NetClientState *peer,
const char *model,
diff --git a/net/eth.c b/net/eth.c
index 33b61cac41..d5590bb671 100644
--- a/net/eth.c
+++ b/net/eth.c
@@ -615,3 +615,21 @@ bool eth_pad_short_frame(uint8_t *padded_pkt, size_t *padded_buflen,
return true;
}
+
+bool eth_pad_short_frame_iov(uint8_t *padded_pkt, size_t *padded_buflen,
+ const struct iovec *iov, int iovcnt,
+ size_t pkt_size)
+{
+ assert(padded_buflen && *padded_buflen >= ETH_ZLEN);
+
+ if (pkt_size >= ETH_ZLEN) {
+ return false;
+ }
+
+ /* pad to minimum Ethernet frame length */
+ iov_to_buf(iov, iovcnt, 0, padded_pkt, pkt_size);
+ memset(&padded_pkt[pkt_size], 0, ETH_ZLEN - pkt_size);
+ *padded_buflen = ETH_ZLEN;
+
+ return true;
+}
diff --git a/net/net.c b/net/net.c
index bf7e5d9b12..7c7fb4bd30 100644
--- a/net/net.c
+++ b/net/net.c
@@ -267,7 +267,8 @@ void qemu_net_client_setup(NetClientState *nc,
const char *model,
const char *name,
NetClientDestructor *destructor,
- bool is_datapath)
+ bool is_datapath,
+ bool do_not_pad)
{
nc->info = info;
nc->model = g_strdup(model);
@@ -287,6 +288,7 @@ void qemu_net_client_setup(NetClientState *nc,
nc->incoming_queue = qemu_new_net_queue(qemu_deliver_packet_iov, nc);
nc->destructor = destructor;
nc->is_datapath = is_datapath;
+ nc->do_not_pad = do_not_pad;
QTAILQ_INIT(&nc->filters);
}
@@ -301,7 +303,7 @@ NetClientState *qemu_new_net_client(NetClientInfo *info,
nc = g_malloc0(info->size);
qemu_net_client_setup(nc, info, peer, model, name,
- qemu_net_client_destructor, true);
+ qemu_net_client_destructor, true, true);
return nc;
}
@@ -317,7 +319,7 @@ NetClientState *qemu_new_net_control_client(NetClientInfo *info,
nc = g_malloc0(info->size);
qemu_net_client_setup(nc, info, peer, model, name,
- qemu_net_client_destructor, false);
+ qemu_net_client_destructor, false, true);
return nc;
}
@@ -344,7 +346,7 @@ NICState *qemu_new_nic(NetClientInfo *info,
for (i = 0; i < queues; i++) {
qemu_net_client_setup(&nic->ncs[i], info, peers[i], model, name,
- NULL, true);
+ NULL, true, false);
nic->ncs[i].queue_index = i;
}
@@ -742,6 +744,8 @@ static ssize_t qemu_send_packet_async_with_flags(NetClientState *sender,
{
NetQueue *queue;
int ret;
+ uint8_t min_pkt[ETH_ZLEN];
+ size_t min_pktsz = sizeof(min_pkt);
#ifdef DEBUG_NET
printf("qemu_send_packet_async:\n");
@@ -752,6 +756,13 @@ static ssize_t qemu_send_packet_async_with_flags(NetClientState *sender,
return size;
}
+ if (net_peer_needs_padding(sender)) {
+ if (eth_pad_short_frame(min_pkt, &min_pktsz, buf, size)) {
+ buf = min_pkt;
+ size = min_pktsz;
+ }
+ }
+
/* Let filters handle the packet first */
ret = filter_receive(sender, NET_FILTER_DIRECTION_TX,
sender, flags, buf, size, sent_cb);
@@ -906,6 +917,19 @@ ssize_t qemu_sendv_packet_async(NetClientState *sender,
return size;
}
+ if (net_peer_needs_padding(sender)) {
+ uint8_t min_pkt[ETH_ZLEN];
+ size_t min_pktsz = sizeof(min_pkt);
+
+ /*
+ * If we pad the packet, we get back a simple buffer, so handle
+ * it by sending it to the non-iov qemu_send_packet function
+ */
+ if (eth_pad_short_frame_iov(min_pkt, &min_pktsz, iov, iovcnt, size)) {
+ return qemu_send_packet_async(sender, min_pkt, min_pktsz, sent_cb);
+ }
+ }
+
/* Let filters handle the packet first */
ret = filter_receive_iov(sender, NET_FILTER_DIRECTION_TX, sender,
QEMU_NET_PACKET_FLAG_NONE, iov, iovcnt, sent_cb);
diff --git a/net/tap.c b/net/tap.c
index 9bce0d088f..ee6430d34a 100644
--- a/net/tap.c
+++ b/net/tap.c
@@ -627,7 +627,7 @@ static TAPState *new_tap(NetClientState *peer,
TAPState *s = TAP_NETDEV(object_new(TYPE_TAP_NETDEV));
qemu_net_client_setup(&s->nc, &net_tap_info, peer, model, name,
- tap_net_client_destructor, true);
+ tap_net_client_destructor, true, true);
s->queue_index = queue_index;