Missing endianness conversions in user mode
v3 cover Missing endianness conversions in user mode
From: Mathis Marion <[email protected]>
For a bit of context, I was trying to test a network border router [1] daemon using the MIPS architecture (see [2]). I didn't have access to real MIPS hardware so I figured I would emulate it using QEMU user mode. I ran into a couple of problems all related to endianness conversion for syscalls between host and target as MIPS is big endian and my x86 host is little.
[1]: https://github.com/SiliconLabs/wisun-br-linux
[2]: https://github.com/SiliconLabs/wisun-br-linux/issues/5
v3:
- remove patches "linux-user: fix timerfd read endianness conversion" and "linux-user: add target to host netlink conversions" as they were integrated.
- remove sin6_flowinfo endianness conversion
- use NLA_TYPE_MASK instead of ~NLA_F_NESTED
v2:
- remove context from target_to_host_for_each_nlattr()
Mathis Marion (2):
linux-user: fix sockaddr_in6 endianness
linux-user: handle netlink flag NLA_F_NESTED
linux-user/fd-trans.c | 4 ++-- linux-user/syscall.c | 5 +++++ 2 files changed, 7 insertions(+), 2 deletions(-)
Signature · 1 line
2.39.2
PATCH v3 1/2 linux-user: fix sockaddr_in6 endianness
From: Mathis Marion <[email protected]>
The sin6_scope_id field uses the host byte order, so there is a conversion to be made when host and target endianness differ.
--- linux-user/syscall.c | 5 +++++ 1 file changed, 5 insertions(+)
linux-user/syscall.c +5 -0
@@ -1713,6 +1713,11 @@ static inline abi_long target_to_host_sockaddr(int fd, struct sockaddr *addr, lladdr = (struct target_sockaddr_ll *)addr; lladdr->sll_ifindex =tswap32 (lladdr->sll_ifindex); lladdr->sll_hatype =tswap16 (lladdr->sll_hatype); + }else if (sa_family ==AF_INET6 ) { +struct sockaddr_in6 *in6addr; + + in6addr = (struct sockaddr_in6 *)addr; + in6addr->sin6_scope_id =tswap32 (in6addr->sin6_scope_id); }unlock_user (target_saddr, target_addr,0 );
Signature · 1 line
2.39.2
Re: [PATCH v3 1/2] linux-user: fix sockaddr_in6 endianness
Le 07/03/2023 à 16:42, Mathis Marion a écrit :
Quoted 16 lines
From: Mathis Marion <[email protected]>
The
sin6_scope_idfield uses the host byte order, so there is a conversion to be made when host and target endianness differ.Signed-off-by Mathis Marion <[email protected]>--- linux-user/syscall.c | 5 +++++ 1 file changed, 5 insertions(+)linux-user/syscall.c +0
-0@@ -1713,6 +1713,11 @@ static inline abi_long target_to_host_sockaddr(int fd, struct sockaddr *addr, lladdr = (struct target_sockaddr_ll *)addr;
linux-user/syscall.c static inline abi_long target_to_host_sockaddr(int fd, struct sockaddr *addr, · line 1723
Quoted 10 lines
linux-user/syscall.c +5
-0lladdr->sll_ifindex =tswap32 (lladdr->sll_ifindex); lladdr->sll_hatype =tswap16 (lladdr->sll_hatype); + }else if (sa_family ==AF_INET6 ) { +struct sockaddr_in6 *in6addr; + + in6addr = (struct sockaddr_in6 *)addr; + in6addr->sin6_scope_id =tswap32 (in6addr->sin6_scope_id); }unlock_user (target_saddr, target_addr,0 );
Laurent Vivier
AppliedRe: [PATCH v3 1/2] linux-user: fix sockaddr_in6 endianness
In reply to Mathis MarionLe 07/03/2023 à 16:42, Mathis Marion a écrit :
Quoted 16 lines
From: Mathis Marion <[email protected]>
The
sin6_scope_idfield uses the host byte order, so there is a conversion to be made when host and target endianness differ.Signed-off-by Mathis Marion <[email protected]>--- linux-user/syscall.c | 5 +++++ 1 file changed, 5 insertions(+)linux-user/syscall.c +0
-0@@ -1713,6 +1713,11 @@ static inline abi_long target_to_host_sockaddr(int fd, struct sockaddr *addr, lladdr = (struct target_sockaddr_ll *)addr;
linux-user/syscall.c static inline abi_long target_to_host_sockaddr(int fd, struct sockaddr *addr, · line 1723
Quoted 10 lines
linux-user/syscall.c +5
-0lladdr->sll_ifindex =tswap32 (lladdr->sll_ifindex); lladdr->sll_hatype =tswap16 (lladdr->sll_hatype); + }else if (sa_family ==AF_INET6 ) { +struct sockaddr_in6 *in6addr; + + in6addr = (struct sockaddr_in6 *)addr; + in6addr->sin6_scope_id =tswap32 (in6addr->sin6_scope_id); }unlock_user (target_saddr, target_addr,0 );
Applied to my linux-user-for-8.0 branch.
Thanks,
Laurent
Re: [PATCH v3 1/2] linux-user: fix sockaddr_in6 endianness
In reply to Mathis MarionQuoted 9 lines · Mathis Marion
From: Mathis Marion <[email protected]>
The
sin6_scope_idfield uses the host byte order, so there is a conversion to be made when host and target endianness differ.Signed-off-by Mathis Marion <[email protected]>--- linux-user/syscall.c | 5 +++++ 1 file changed, 5 insertions(+)
PATCH v3 2/2 linux-user: handle netlink flag NLA_F_NESTED
In reply to Mathis MarionFrom: Mathis Marion <[email protected]>
Newer kernel versions require this flag to be present contrary to older ones. Depending on the libnl version it is added or not.
Typically when using rtnl_link_inet6_set_addr_gen_mode, the netlink packet generated may contain the following attribute:
with libnl 3.4
{nla_len=16, nla_type=IFLA_AF_SPEC},
[
{nla_len=12, nla_type=AF_INET6},
[{nla_len=5, nla_type=IFLA_INET6_ADDR_GEN_MODE}, IN6_ADDR_GEN_MODE_NONE]]
with libnl 3.7
{nla_len=16, nla_type=NLA_F_NESTED|IFLA_AF_SPEC}, [
{nla_len=12, nla_type=NLA_F_NESTED|AF_INET6},
[{nla_len=5, nla_type=IFLA_INET6_ADDR_GEN_MODE}, IN6_ADDR_GEN_MODE_NONE]]]
Masking the type is likely needed in other places. Only the above cases are implemented in this patch.
--- linux-user/fd-trans.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
linux-user/fd-trans.c +2 -2
@@ -1359,7 +1359,7 @@ static abi_long target_to_host_for_each_rtattr(struct rtattr *rtattr,static abi_long target_to_host_data_spec_nlattr (struct nlattr *nlattr) { -switch (nlattr->nla_type) { +switch (nlattr->nla_type &NLA_TYPE_MASK ) {case AF_INET6 :return target_to_host_for_each_nlattr (NLA_DATA (nlattr), nlattr->nla_len, target_to_host_data_inet6_nlattr);
@@ -1375,7 +1375,7 @@ static abi_long target_to_host_data_link_rtattr(struct rtattr *rtattr) {uint32_t *u32; -switch (rtattr->rta_type) { +switch (rtattr->rta_type &NLA_TYPE_MASK ) {/* uint32_t */ case QEMU_IFLA_MTU :case QEMU_IFLA_TXQLEN :
Signature · 1 line
2.39.2
Re: [PATCH v3 2/2] linux-user: handle netlink flag NLA_F_NESTED
Le 07/03/2023 à 16:42, Mathis Marion a écrit :
Quoted 46 lines
From: Mathis Marion <[email protected]>
Newer kernel versions require this flag to be present contrary to older ones. Depending on the libnl version it is added or not.
Typically when using
rtnl_link_inet6_set_addr_gen_mode, the netlink packet generated may contain the following attribute:with libnl 3.4
{nla_len=16, nla_type=IFLA_AF_SPEC}, [ {nla_len=12, nla_type=AF_INET6}, [{nla_len=5, nla_type=IFLA_INET6_ADDR_GEN_MODE}, IN6_ADDR_GEN_MODE_NONE] ]with libnl 3.7
{nla_len=16, nla_type=NLA_F_NESTED|IFLA_AF_SPEC}, [ {nla_len=12, nla_type=NLA_F_NESTED|AF_INET6}, [{nla_len=5, nla_type=IFLA_INET6_ADDR_GEN_MODE}, IN6_ADDR_GEN_MODE_NONE]] ]Masking the type is likely needed in other places. Only the above cases are implemented in this patch.
Signed-off-by Mathis Marion <[email protected]>--- linux-user/fd-trans.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)linux-user/fd-trans.c +1
-1@@ -1359,7 +1359,7 @@ static abi_long target_to_host_for_each_rtattr(struct rtattr *rtattr,static abi_long target_to_host_data_spec_nlattr (struct nlattr *nlattr) { -switch (nlattr->nla_type) { +switch (nlattr->nla_type &NLA_TYPE_MASK ) {case AF_INET6 :return target_to_host_for_each_nlattr (NLA_DATA (nlattr), nlattr->nla_len, target_to_host_data_inet6_nlattr);
linux-user/fd-trans.c static abi_long target_to_host_data_link_rtattr(struct rtattr *rtattr) · line 1381
Quoted 8 lines
linux-user/fd-trans.c +1
-1@@ -1375,7 +1375,7 @@ static abi_long target_to_host_data_link_rtattr(struct rtattr *rtattr) {uint32_t *u32; -switch (rtattr->rta_type) { +switch (rtattr->rta_type &NLA_TYPE_MASK ) {/* uint32_t */ case QEMU_IFLA_MTU :case QEMU_IFLA_TXQLEN :
Laurent Vivier
AppliedRe: [PATCH v3 2/2] linux-user: handle netlink flag NLA_F_NESTED
In reply to Mathis MarionLe 07/03/2023 à 16:42, Mathis Marion a écrit :
Quoted 46 lines
From: Mathis Marion <[email protected]>
Newer kernel versions require this flag to be present contrary to older ones. Depending on the libnl version it is added or not.
Typically when using
rtnl_link_inet6_set_addr_gen_mode, the netlink packet generated may contain the following attribute:with libnl 3.4
{nla_len=16, nla_type=IFLA_AF_SPEC}, [ {nla_len=12, nla_type=AF_INET6}, [{nla_len=5, nla_type=IFLA_INET6_ADDR_GEN_MODE}, IN6_ADDR_GEN_MODE_NONE] ]with libnl 3.7
{nla_len=16, nla_type=NLA_F_NESTED|IFLA_AF_SPEC}, [ {nla_len=12, nla_type=NLA_F_NESTED|AF_INET6}, [{nla_len=5, nla_type=IFLA_INET6_ADDR_GEN_MODE}, IN6_ADDR_GEN_MODE_NONE]] ]Masking the type is likely needed in other places. Only the above cases are implemented in this patch.
Signed-off-by Mathis Marion <[email protected]>--- linux-user/fd-trans.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)linux-user/fd-trans.c +1
-1@@ -1359,7 +1359,7 @@ static abi_long target_to_host_for_each_rtattr(struct rtattr *rtattr,static abi_long target_to_host_data_spec_nlattr (struct nlattr *nlattr) { -switch (nlattr->nla_type) { +switch (nlattr->nla_type &NLA_TYPE_MASK ) {case AF_INET6 :return target_to_host_for_each_nlattr (NLA_DATA (nlattr), nlattr->nla_len, target_to_host_data_inet6_nlattr);
linux-user/fd-trans.c static abi_long target_to_host_data_link_rtattr(struct rtattr *rtattr) · line 1381
Quoted 8 lines
linux-user/fd-trans.c +1
-1@@ -1375,7 +1375,7 @@ static abi_long target_to_host_data_link_rtattr(struct rtattr *rtattr) {uint32_t *u32; -switch (rtattr->rta_type) { +switch (rtattr->rta_type &NLA_TYPE_MASK ) {/* uint32_t */ case QEMU_IFLA_MTU :case QEMU_IFLA_TXQLEN :
Applied to my linux-user-for-8.0 branch.
Thanks,
Laurent