Skip to content

Commit da64ae2

Browse files
vhankalaklassert
authored andcommitted
xfrm: Fix wraparound in xfrm_policy_addr_delta()
Use three-way comparison for address components to avoid integer wraparound in the result of xfrm_policy_addr_delta(). This ensures that the search trees are built and traversed correctly. Treat IPv4 and IPv6 similarly by returning 0 when prefixlen == 0. Prefix /0 has only one equivalence class. Fixes: 9cf545e ("xfrm: policy: store inexact policies in a tree ordered by destination address") Signed-off-by: Visa Hankala <[email protected]> Acked-by: Florian Westphal <[email protected]> Signed-off-by: Steffen Klassert <[email protected]>
1 parent f6e9ceb commit da64ae2

File tree

2 files changed

+61
-8
lines changed

2 files changed

+61
-8
lines changed

net/xfrm/xfrm_policy.c

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -793,15 +793,22 @@ static int xfrm_policy_addr_delta(const xfrm_address_t *a,
793793
const xfrm_address_t *b,
794794
u8 prefixlen, u16 family)
795795
{
796+
u32 ma, mb, mask;
796797
unsigned int pdw, pbi;
797798
int delta = 0;
798799

799800
switch (family) {
800801
case AF_INET:
801-
if (sizeof(long) == 4 && prefixlen == 0)
802-
return ntohl(a->a4) - ntohl(b->a4);
803-
return (ntohl(a->a4) & ((~0UL << (32 - prefixlen)))) -
804-
(ntohl(b->a4) & ((~0UL << (32 - prefixlen))));
802+
if (prefixlen == 0)
803+
return 0;
804+
mask = ~0U << (32 - prefixlen);
805+
ma = ntohl(a->a4) & mask;
806+
mb = ntohl(b->a4) & mask;
807+
if (ma < mb)
808+
delta = -1;
809+
else if (ma > mb)
810+
delta = 1;
811+
break;
805812
case AF_INET6:
806813
pdw = prefixlen >> 5;
807814
pbi = prefixlen & 0x1f;
@@ -812,10 +819,13 @@ static int xfrm_policy_addr_delta(const xfrm_address_t *a,
812819
return delta;
813820
}
814821
if (pbi) {
815-
u32 mask = ~0u << (32 - pbi);
816-
817-
delta = (ntohl(a->a6[pdw]) & mask) -
818-
(ntohl(b->a6[pdw]) & mask);
822+
mask = ~0U << (32 - pbi);
823+
ma = ntohl(a->a6[pdw]) & mask;
824+
mb = ntohl(b->a6[pdw]) & mask;
825+
if (ma < mb)
826+
delta = -1;
827+
else if (ma > mb)
828+
delta = 1;
819829
}
820830
break;
821831
default:

tools/testing/selftests/net/xfrm_policy.sh

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,47 @@ check_hthresh_repeat()
287287
return 0
288288
}
289289

290+
# insert non-overlapping policies in a random order and check that
291+
# all of them can be fetched using the traffic selectors.
292+
check_random_order()
293+
{
294+
local ns=$1
295+
local log=$2
296+
297+
for i in $(seq 100); do
298+
ip -net $ns xfrm policy flush
299+
for j in $(seq 0 16 255 | sort -R); do
300+
ip -net $ns xfrm policy add dst $j.0.0.0/24 dir out priority 10 action allow
301+
done
302+
for j in $(seq 0 16 255); do
303+
if ! ip -net $ns xfrm policy get dst $j.0.0.0/24 dir out > /dev/null; then
304+
echo "FAIL: $log" 1>&2
305+
return 1
306+
fi
307+
done
308+
done
309+
310+
for i in $(seq 100); do
311+
ip -net $ns xfrm policy flush
312+
for j in $(seq 0 16 255 | sort -R); do
313+
local addr=$(printf "e000:0000:%02x00::/56" $j)
314+
ip -net $ns xfrm policy add dst $addr dir out priority 10 action allow
315+
done
316+
for j in $(seq 0 16 255); do
317+
local addr=$(printf "e000:0000:%02x00::/56" $j)
318+
if ! ip -net $ns xfrm policy get dst $addr dir out > /dev/null; then
319+
echo "FAIL: $log" 1>&2
320+
return 1
321+
fi
322+
done
323+
done
324+
325+
ip -net $ns xfrm policy flush
326+
327+
echo "PASS: $log"
328+
return 0
329+
}
330+
290331
#check for needed privileges
291332
if [ "$(id -u)" -ne 0 ];then
292333
echo "SKIP: Need root privileges"
@@ -438,6 +479,8 @@ check_exceptions "exceptions and block policies after htresh change to normal"
438479

439480
check_hthresh_repeat "policies with repeated htresh change"
440481

482+
check_random_order ns3 "policies inserted in random order"
483+
441484
for i in 1 2 3 4;do ip netns del ns$i;done
442485

443486
exit $ret

0 commit comments

Comments
 (0)