Subject: [PATCH] net: netdevsim: fix byte order on ipsec debugfs file

When adding a new xfrm state, the data provided via struct xfrm_state
is stored in network byte order. This needs to be taken into
consideration when exporting the SAs data to userspace via debugfs,
otherwise the content will depend on the system endianness. Fix this by
converting all multi-byte fields from network to host order.

Also fix the selftest script which was expecting the data as exported by
a little-endian system, which was inverted.

Fixes: 7699353da875 ("netdevsim: add ipsec offload testing")
Fixes: 2766a11161cc ("selftests: rtnetlink: add ipsec offload API test")
Signed-off-by: Kleber Sacilotto de Souza <[email protected]>
---
drivers/net/netdevsim/ipsec.c | 13 +++++++------
tools/testing/selftests/net/rtnetlink.sh | 10 +++++-----
2 files changed, 12 insertions(+), 11 deletions(-)

diff --git a/drivers/net/netdevsim/ipsec.c b/drivers/net/netdevsim/ipsec.c
index b80ed2ffd45e..03fb8b56edc4 100644
--- a/drivers/net/netdevsim/ipsec.c
+++ b/drivers/net/netdevsim/ipsec.c
@@ -41,16 +41,17 @@ static ssize_t nsim_dbg_netdev_ops_read(struct file *filp,

p += scnprintf(p, bufsize - (p - buf),
"sa[%i] %cx ipaddr=0x%08x %08x %08x %08x\n",
- i, (sap->rx ? 'r' : 't'), sap->ipaddr[0],
- sap->ipaddr[1], sap->ipaddr[2], sap->ipaddr[3]);
+ i, (sap->rx ? 'r' : 't'), ntohl(sap->ipaddr[0]),
+ ntohl(sap->ipaddr[1]), ntohl(sap->ipaddr[2]),
+ ntohl(sap->ipaddr[3]));
p += scnprintf(p, bufsize - (p - buf),
"sa[%i] spi=0x%08x proto=0x%x salt=0x%08x crypt=%d\n",
- i, be32_to_cpu(sap->xs->id.spi),
- sap->xs->id.proto, sap->salt, sap->crypt);
+ i, ntohl(sap->xs->id.spi),
+ sap->xs->id.proto, ntohl(sap->salt), sap->crypt);
p += scnprintf(p, bufsize - (p - buf),
"sa[%i] key=0x%08x %08x %08x %08x\n",
- i, sap->key[0], sap->key[1],
- sap->key[2], sap->key[3]);
+ i, ntohl(sap->key[0]), ntohl(sap->key[1]),
+ ntohl(sap->key[2]), ntohl(sap->key[3]));
}

len = simple_read_from_buffer(buffer, count, ppos, buf, p - buf);
diff --git a/tools/testing/selftests/net/rtnetlink.sh b/tools/testing/selftests/net/rtnetlink.sh
index c9ce3dfa42ee..8b1f20947673 100755
--- a/tools/testing/selftests/net/rtnetlink.sh
+++ b/tools/testing/selftests/net/rtnetlink.sh
@@ -814,11 +814,11 @@ kci_test_ipsec_offload()
diff $sysfsf - << EOF
SA count=2 tx=3
sa[0] tx ipaddr=0x00000000 00000000 00000000 00000000
-sa[0] spi=0x00000009 proto=0x32 salt=0x61626364 crypt=1
-sa[0] key=0x34333231 38373635 32313039 36353433
-sa[1] rx ipaddr=0x00000000 00000000 00000000 037ba8c0
-sa[1] spi=0x00000009 proto=0x32 salt=0x61626364 crypt=1
-sa[1] key=0x34333231 38373635 32313039 36353433
+sa[0] spi=0x00000009 proto=0x32 salt=0x64636261 crypt=1
+sa[0] key=0x31323334 35363738 39303132 33343536
+sa[1] rx ipaddr=0x00000000 00000000 00000000 c0a87b03
+sa[1] spi=0x00000009 proto=0x32 salt=0x64636261 crypt=1
+sa[1] key=0x31323334 35363738 39303132 33343536
EOF
if [ $? -ne 0 ] ; then
echo "FAIL: ipsec_offload incorrect driver data"
--
2.32.0


2022-03-09 06:10:57

by Jakub Kicinski

[permalink] [raw]
Subject: Re: [PATCH] net: netdevsim: fix byte order on ipsec debugfs file

On Tue, 8 Mar 2022 14:51:06 +0100 Kleber Sacilotto de Souza wrote:
> When adding a new xfrm state, the data provided via struct xfrm_state
> is stored in network byte order. This needs to be taken into
> consideration when exporting the SAs data to userspace via debugfs,
> otherwise the content will depend on the system endianness. Fix this by
> converting all multi-byte fields from network to host order.
>
> Also fix the selftest script which was expecting the data as exported by
> a little-endian system, which was inverted.
>
> Fixes: 7699353da875 ("netdevsim: add ipsec offload testing")
> Fixes: 2766a11161cc ("selftests: rtnetlink: add ipsec offload API test")
> Signed-off-by: Kleber Sacilotto de Souza <[email protected]>

Then the struct members need to have the correct types,
as is this patch adds sparse warnings (build with C=1).

Subject: Re: [PATCH] net: netdevsim: fix byte order on ipsec debugfs file

On 09.03.22 06:58, Jakub Kicinski wrote:
> On Tue, 8 Mar 2022 14:51:06 +0100 Kleber Sacilotto de Souza wrote:
>> When adding a new xfrm state, the data provided via struct xfrm_state
>> is stored in network byte order. This needs to be taken into
>> consideration when exporting the SAs data to userspace via debugfs,
>> otherwise the content will depend on the system endianness. Fix this by
>> converting all multi-byte fields from network to host order.
>>
>> Also fix the selftest script which was expecting the data as exported by
>> a little-endian system, which was inverted.
>>
>> Fixes: 7699353da875 ("netdevsim: add ipsec offload testing")
>> Fixes: 2766a11161cc ("selftests: rtnetlink: add ipsec offload API test")
>> Signed-off-by: Kleber Sacilotto de Souza <[email protected]>
>
> Then the struct members need to have the correct types,
> as is this patch adds sparse warnings (build with C=1).

Hi Jakub,

Thank you for the review. I'll fix it and send a v2 shortly.

Kleber