Skip to contentSkip to conversation

linux-user: fix off-by-one in host_to_target_for_each_rtattr()

PATCH In review Yixin Wei 4 participants 6 messages Apr 9 – Apr 23

lore
Share & tools
lore

Yixin Wei

author
#0 lore raw

PATCH 1/1 linux-user: fix off-by-one in host_to_target_for_each_rtattr()

host_to_target_for_each_rtattr() uses "len > sizeof(struct rtattr)" as its loop condition. When the last rtattr in a netlink message has exactly sizeof(struct rtattr) (4) bytes remaining, the loop exits without byte-swapping its rta_len and rta_type. A big-endian guest then reads rta_len in the wrong byte order and fails validation.

The companion function target_to_host_for_each_rtattr() correctly uses ">=" (added in commit fa2229dbf8). The kernel's RTA_OK macro also uses ">=". Fix the host_to_target direction to match.

Resolves: https://gitlab.com/qemu-project/qemu/-/issues/2485

---
 linux-user/fd-trans.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
linux-user/fd-trans.c +1 -1
index 64dd0745d2..7f55a0690b 100644
@@ -480,7 +480,7 @@ static abi_long host_to_target_for_each_rtattr(struct rtattr *rtattr,
     unsigned short aligned_rta_len;
     abi_long ret;
 
-    while (len > sizeof(struct rtattr)) {
+    while (len >= sizeof(struct rtattr)) {
         rta_len = rtattr->rta_len;
         if (rta_len < sizeof(struct rtattr) ||
             rta_len > len) {
Signature · 1 line
2.52.0
Reviewed-by on v1 1/1
#1 lore raw
Quoted 10 lines · Yixin Wei

host_to_target_for_each_rtattr() uses "len > sizeof(struct rtattr)" as its loop condition. When the last rtattr in a netlink message has exactly sizeof(struct rtattr) (4) bytes remaining, the loop exits without byte-swapping its rta_len and rta_type. A big-endian guest then reads rta_len in the wrong byte order and fails validation.

The companion function target_to_host_for_each_rtattr() correctly uses ">=" (added in commit fa2229dbf8). The kernel's RTA_OK macro also uses ">=". Fix the host_to_target direction to match.

10 years old bug, nice!

Fixes 6c5b5645ae0 ("linux-user: add rtnetlink(7) support")
Quoted 5 lines

Resolves: https://gitlab.com/qemu-project/qemu/-/issues/2485

---
  linux-user/fd-trans.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
#2 lore raw

Hi Philippe,

Thanks for the review! Just curious about QEMU's typical merge workflow, trying to get a sense of how long this usually takes to land once it's validated.

It's a niche use case but it affects anyone using qemu-user on big-endian targets, so would be nice to get it into a release at some point.

Thanks,
Yixin

Quoted 25 lines · Philippe Mathieu-Daudé
Quoted 1 line
Quoted 10 lines · Yixin Wei

host_to_target_for_each_rtattr() uses "len > sizeof(struct rtattr)" as its loop condition. When the last rtattr in a netlink message has exactly sizeof(struct rtattr) (4) bytes remaining, the loop exits without byte-swapping its rta_len and rta_type. A big-endian guest then reads rta_len in the wrong byte order and fails validation.

The companion function target_to_host_for_each_rtattr() correctly uses ">=" (added in commit fa2229dbf8). The kernel's RTA_OK macro also uses ">=". Fix the host_to_target direction to match.

10 years old bug, nice!

Fixes 6c5b5645ae0 ("linux-user: add rtnetlink(7) support")
Quoted 1 line

Resolves:

https://urldefense.com/v3/__https://gitlab.com/qemu-project/qemu/-/issues/2485__;!!Bt8RZUm9aw!8GbeoUxJTV2Px--RJ27lNOg6O6xYuOKOwEP614qzPEoNam2D2rtWkp6O6vvuf1vAnD2-g02GCUsn$

Quoted 4 lines
---
  linux-user/fd-trans.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
#3 lore raw

Hi Yixin,

Quoted 3 lines · Yixin Wei

Hi Philippe,

Thanks for the review! Just curious about QEMU's typical merge workflow, trying to get a sense of how long this usually takes to land once it's validated.

Something like:

1/ Patch posted on the list

( delay to next step: 1 day -> never )

2/ Patch reviewed

( usually within 1 week, up to 4, sometimes get lost)

3/ Patch queued

( between 1 week to infinite )

4/ Patch sent in pull request

( 1 to 3 days, rarely 1 week )
( respin on CI failure )

5/ Pull request merged

Delays depend on maintainer workflows.

Note on 4/, before every release the tree enters a "freeze": no new features are merged, only fixed, until the tree is re-opened again.

Luckily for you the tree re-opened as of today!

However this patch belongs to the 'linux-user' subsystem which doesn't have dedicated maintainer paid to do it. Some developers take care of it as 'best effort basis' (thus Cc'ing Helge and Pierrick).

Regards,

Phil.

Quoted 40 lines

It's a niche use case but it affects anyone using qemu-user on big- endian targets, so would be nice to get it into a release at some point.

Thanks,
Yixin

On Thu, Apr 9, 2026 at 9:48 PM Philippe Mathieu-Daudé <[email protected] <mailto:[email protected]>> wrote:

On 9/4/26 18:49, Yixin Wei wrote:
> host_to_target_for_each_rtattr() uses "len > sizeof(struct rtattr)" > as its loop condition. When the last rtattr in a netlink message has > exactly sizeof(struct rtattr) (4) bytes remaining, the loop exits > without byte-swapping its rta_len and rta_type. A big-endian guest > then reads rta_len in the wrong byte order and fails validation. >
> The companion function target_to_host_for_each_rtattr() correctly > uses ">=" (added in commit fa2229dbf8). The kernel's RTA_OK macro > also uses ">=". Fix the host_to_target direction to match. >

10 years old bug, nice!

Fixes 6c5b5645ae0 ("linux-user: add rtnetlink(7) support")

<mailto:[email protected]>>

> Resolves: https://urldefense.com/v3/__https://gitlab.com/qemu-
project/qemu/-/issues/2485__;!!Bt8RZUm9aw!8GbeoUxJTV2Px-- RJ27lNOg6O6xYuOKOwEP614qzPEoNam2D2rtWkp6O6vvuf1vAnD2-g02GCUsn$
<https://urldefense.com/v3/__https://gitlab.com/qemu-project/qemu/-/
issues/2485__;!!Bt8RZUm9aw!8GbeoUxJTV2Px-- RJ27lNOg6O6xYuOKOwEP614qzPEoNam2D2rtWkp6O6vvuf1vAnD2-g02GCUsn$>
> Signed-off-by: Yixin Wei <[email protected]
<mailto:[email protected]>>
> ---
>   linux-user/fd-trans.c | 2 +-
>   1 file changed, 1 insertion(+), 1 deletion(-)

#5 lore raw

Thanks Helge for picking this up!
And thanks Philippe for the info, it's really good to know how the process works.

Have a good rest of the day.

Thanks,
Yixin

Quoted 19 lines · Helge Deller
Quoted 1 line
Quoted 8 lines · Philippe Mathieu-Daudé

Hi Yixin,

Quoted 5 lines · Yixin Wei

Hi Philippe,

Thanks for the review! Just curious about QEMU's typical merge workflow, trying to get a sense of how long this usually takes to land once it's validated.

I've applied your patch to my linux-user-next git tree at

https://urldefense.com/v3/__https://gitlab.com/hdeller/qemu/-/commits/linux-user-next?ref_type=heads__;!!Bt8RZUm9aw!91NbIhGxhNbBVICaGq6ZzURChgxm5bPi1-9X2ZU1-vPiep_6kp8S5qhrV5h0hQzc7S84HM-XX_8Sug$
and will push soon.

Thanks,
Helge