Skip to contentSkip to conversation

pull request (net): ipsec 2021-01-21

PATCH New Steffen Klassert 2 participants 7 messages Jan 21, 2021

Subsystems (4)
lore
Share & tools
lore

Steffen Klassert

author maintainer
#0 lore raw

1) Fix a rare panic on SMP systems when packet reordering happens between anti replay check and update. From Shmulik Ladkani.

2) Fix disable_xfrm sysctl when used on xfrm interfaces. From Eyal Birger.

3) Fix a race in PF_KEY when the availability of crypto algorithms is set. From Cong Wang.

4) Fix a return value override in the xfrm policy selftests. From Po-Hsu Lin.

5) Fix an integer wraparound in xfrm_policy_addr_delta. From Visa Hankala.

Please pull or let me know if there are problems.

Thanks!

The following changes since commit 7f376f1917d7461e05b648983e8d2aea9d0712b2:

Merge tag 'mtd/fixes-for-5.10-rc8' of git://git.kernel.org/pub/scm/linux/kernel/git/mtd/linux (2020-12-11 14:29:46 -0800)

are available in the Git repository at:

git://git.kernel.org/pub/scm/linux/kernel/git/klassert/ipsec.git master

for you to fetch changes up to da64ae2d35d3673233f0403b035d4c6acbf71965:

xfrm: Fix wraparound in xfrm_policy_addr_delta() (2021-01-04 10:35:09 +0100)

----------------------------------------------------------------
Cong Wang (1):
af_key: relax availability checks for skb size calculation

Eyal Birger (1):
xfrm: fix disable_xfrm sysctl when used on xfrm interfaces

Po-Hsu Lin (1):
selftests: xfrm: fix test return value override issue in xfrm_policy.sh

Shmulik Ladkani (1):
xfrm: Fix oops in xfrm_replay_advance_bmp

Visa Hankala (1):
xfrm: Fix wraparound in xfrm_policy_addr_delta()

 net/key/af_key.c                           |  6 ++--
 net/xfrm/xfrm_input.c                      |  2 +-
 net/xfrm/xfrm_policy.c                     | 30 +++++++++++++-------
 tools/testing/selftests/net/xfrm_policy.sh | 45 +++++++++++++++++++++++++++++-
 4 files changed, 68 insertions(+), 15 deletions(-)
author maintainer
#1 lore raw

PATCH 1/5 xfrm: Fix oops in xfrm_replay_advance_bmp

From: Shmulik Ladkani <[email protected]>

When setting xfrm replay_window to values higher than 32, a rare page-fault occurs in xfrm_replay_advance_bmp:

  BUG: unable to handle page fault for address: ffff8af350ad7920
  #PF: supervisor write access in kernel mode
  #PF: error_code(0x0002) - not-present page
  PGD ad001067 P4D ad001067 PUD 0
  Oops: 0002 [#1] SMP PTI
  CPU: 3 PID: 30 Comm: ksoftirqd/3 Kdump: loaded Not tainted 5.4.52-050452-generic #202007160732
  Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS 1.11.0-2.el7 04/01/2014
  RIP: 0010:xfrm_replay_advance_bmp+0xbb/0x130
  RSP: 0018:ffffa1304013ba40 EFLAGS: 00010206
  RAX: 000000000000010d RBX: 0000000000000002 RCX: 00000000ffffff4b
  RDX: 0000000000000018 RSI: 00000000004c234c RDI: 00000000ffb3dbff
  RBP: ffffa1304013ba50 R08: ffff8af330ad7920 R09: 0000000007fffffa
  R10: 0000000000000800 R11: 0000000000000010 R12: ffff8af29d6258c0
  R13: ffff8af28b95c700 R14: 0000000000000000 R15: ffff8af29d6258fc
  FS:  0000000000000000(0000) GS:ffff8af339ac0000(0000) knlGS:0000000000000000
  CS:  0010 DS: 0000 ES: 0000 CR0: 0000000080050033
  CR2: ffff8af350ad7920 CR3: 0000000015ee4000 CR4: 00000000001406e0
  Call Trace:
   xfrm_input+0x4e5/0xa10
   xfrm4_rcv_encap+0xb5/0xe0
   xfrm4_udp_encap_rcv+0x140/0x1c0

Analysis revealed offending code is when accessing:

	replay_esn->bmp[nr] |= (1U << bitnr);

with 'nr' being 0x07fffffa.

This happened in an SMP system when reordering of packets was present; A packet arrived with a "too old" sequence number (outside the window, i.e 'diff > replay_window'), and therefore the following calculation:

			bitnr = replay_esn->replay_window - (diff - pos);

yields a negative result, but since bitnr is u32 we get a large unsigned quantity (in crash dump above: 0xffffff4b seen in ecx).

This was supposed to be protected by xfrm_input()'s former call to:

		if (x->repl->check(x, skb, seq)) {

However, the state's spinlock x->lock is *released* after '->check()' is performed, and gets re-acquired before '->advance()' - which gives a chance for a different core to update the xfrm state, e.g. by advancing 'replay_esn->seq' when it encounters more packets - leading to a 'diff > replay_window' situation when original core continues to xfrm_replay_advance_bmp().

An attempt to fix this issue was suggested in commit bcf66bf54aab ("xfrm: Perform a replay check after return from async codepaths"), by calling 'x->repl->recheck()' after lock is re-acquired, but fix applied only to asyncronous crypto algorithms.

Augment the fix, by *always* calling 'recheck()' - irrespective if we're using async crypto.

Fixes 0ebea8ef3559 ("[IPSEC]: Move state lock into x->type->input")
---
 net/xfrm/xfrm_input.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
net/xfrm/xfrm_input.c +1 -1
index 37456d022cfa..61e6220ddd5a 100644
@@ -660,7 +660,7 @@ int xfrm_input(struct sk_buff *skb, int nexthdr, __be32 spi, int encap_type)
 		/* only the first xfrm gets the encap type */
 		encap_type = 0;
 
-		if (async && x->repl->recheck(x, skb, seq)) {
+		if (x->repl->recheck(x, skb, seq)) {
 			XFRM_INC_STATS(net, LINUX_MIB_XFRMINSTATESEQERROR);
 			goto drop_unlock;
 		}
Signature · 1 line
2.25.1
author maintainer
#2 lore raw

PATCH 2/5 xfrm: fix disable_xfrm sysctl when used on xfrm interfaces

In reply to Steffen Klassert

From: Eyal Birger <[email protected]>

The disable_xfrm flag signals that xfrm should not be performed during routing towards a device before reaching device xmit.

For xfrm interfaces this is usually desired as they perform the outbound policy lookup as part of their xmit using their if_id.

Before this change enabling this flag on xfrm interfaces prevented them from xmitting as xfrm_lookup_with_ifid() would not perform a policy lookup in case the original dst had the DST_NOXFRM flag.

This optimization is incorrect when the lookup is done by the xfrm interface xmit logic.

Fix by performing policy lookup when invoked by xfrmi as if_id != 0.

Similarly it's unlikely for the 'no policy exists on net' check to yield any performance benefits when invoked from xfrmi.

Fixes f203b76d7809 ("xfrm: Add virtual xfrm interfaces")
---
 net/xfrm/xfrm_policy.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
net/xfrm/xfrm_policy.c +2 -2
index d622c2548d22..2f84136af48a 100644
@@ -3078,8 +3078,8 @@ struct dst_entry *xfrm_lookup_with_ifid(struct net *net,
 		xflo.flags = flags;
 
 		/* To accelerate a bit...  */
-		if ((dst_orig->flags & DST_NOXFRM) ||
-		    !net->xfrm.policy_count[XFRM_POLICY_OUT])
+		if (!if_id && ((dst_orig->flags & DST_NOXFRM) ||
+			       !net->xfrm.policy_count[XFRM_POLICY_OUT]))
 			goto nopol;
 
 		xdst = xfrm_bundle_lookup(net, fl, family, dir, &xflo, if_id);
Signature · 1 line
2.25.1
author maintainer
#3 lore raw

PATCH 3/5 af_key: relax availability checks for skb size calculation

In reply to Steffen Klassert

From: Cong Wang <[email protected]>

xfrm_probe_algs() probes kernel crypto modules and changes the availability of struct xfrm_algo_desc. But there is a small window where ealg->available and aalg->available get changed between count_ah_combs()/count_esp_combs() and dump_ah_combs()/dump_esp_combs(), in this case we may allocate a smaller skb but later put a larger amount of data and trigger the panic in skb_put().

Fix this by relaxing the checks when counting the size, that is, skipping the test of ->available. We may waste some memory for a few of sizeof(struct sadb_comb), but it is still much better than a panic.

---
 net/key/af_key.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
net/key/af_key.c +3 -3
index c12dbc51ef5f..ef9b4ac03e7b 100644
@@ -2902,7 +2902,7 @@ static int count_ah_combs(const struct xfrm_tmpl *t)
 			break;
 		if (!aalg->pfkey_supported)
 			continue;
-		if (aalg_tmpl_set(t, aalg) && aalg->available)
+		if (aalg_tmpl_set(t, aalg))
 			sz += sizeof(struct sadb_comb);
 	}
 	return sz + sizeof(struct sadb_prop);
@@ -2920,7 +2920,7 @@ static int count_esp_combs(const struct xfrm_tmpl *t)
 		if (!ealg->pfkey_supported)
 			continue;
 
-		if (!(ealg_tmpl_set(t, ealg) && ealg->available))
+		if (!(ealg_tmpl_set(t, ealg)))
 			continue;
 
 		for (k = 1; ; k++) {
@@ -2931,7 +2931,7 @@ static int count_esp_combs(const struct xfrm_tmpl *t)
 			if (!aalg->pfkey_supported)
 				continue;
 
-			if (aalg_tmpl_set(t, aalg) && aalg->available)
+			if (aalg_tmpl_set(t, aalg))
 				sz += sizeof(struct sadb_comb);
 		}
 	}
Signature · 1 line
2.25.1
author maintainer
#4 lore raw

PATCH 4/5 selftests: xfrm: fix test return value override issue in xfrm_policy.sh

In reply to Steffen Klassert

From: Po-Hsu Lin <[email protected]>

When running this xfrm_policy.sh test script, even with some cases marked as FAIL, the overall test result will still be PASS:

$ sudo ./xfrm_policy.sh
PASS: policy before exception matches
FAIL: expected ping to .254 to fail (exceptions)
PASS: direct policy matches (exceptions)
PASS: policy matches (exceptions)
FAIL: expected ping to .254 to fail (exceptions and block policies)
PASS: direct policy matches (exceptions and block policies)
PASS: policy matches (exceptions and block policies)
FAIL: expected ping to .254 to fail (exceptions and block policies after hresh changes)
PASS: direct policy matches (exceptions and block policies after hresh changes)
PASS: policy matches (exceptions and block policies after hresh changes)
FAIL: expected ping to .254 to fail (exceptions and block policies after hthresh change in ns3)
PASS: direct policy matches (exceptions and block policies after hthresh change in ns3)
PASS: policy matches (exceptions and block policies after hthresh change in ns3)
FAIL: expected ping to .254 to fail (exceptions and block policies after htresh change to normal)
PASS: direct policy matches (exceptions and block policies after htresh change to normal)
PASS: policy matches (exceptions and block policies after htresh change to normal)
PASS: policies with repeated htresh change
$ echo $?
0

This is because the $lret in check_xfrm() is not a local variable. Therefore when a test failed in check_exceptions(), the non-zero $lret will later get reset to 0 when the next test calls check_xfrm().

With this fix, the final return value will be 1. Make it easier for testers to spot this failure.

Fixes 39aa6928d462d0 ("xfrm: policy: fix netlink/pf_key policy lookups")
---
 tools/testing/selftests/net/xfrm_policy.sh | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
tools/testing/selftests/net/xfrm_policy.sh +1 -1
index 7a1bf94c5bd3..5922941e70c6 100755
@@ -202,7 +202,7 @@ check_xfrm() {
 	# 1: iptables -m policy rule count != 0
 	rval=$1
 	ip=$2
-	lret=0
+	local lret=0
 
 	ip netns exec ns1 ping -q -c 1 10.0.2.$ip > /dev/null
 
Signature · 1 line
2.25.1
author maintainer
#5 lore raw

PATCH 5/5 xfrm: Fix wraparound in xfrm_policy_addr_delta()

In reply to Steffen Klassert

From: Visa Hankala <[email protected]>

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 9cf545ebd591d ("xfrm: policy: store inexact policies in a tree ordered by destination address")
---
 net/xfrm/xfrm_policy.c                     | 26 +++++++++----
 tools/testing/selftests/net/xfrm_policy.sh | 43 ++++++++++++++++++++++
 2 files changed, 61 insertions(+), 8 deletions(-)
net/xfrm/xfrm_policy.c +18 -8
index 2f84136af48a..b74f28cabe24 100644
@@ -793,15 +793,22 @@ static int xfrm_policy_addr_delta(const xfrm_address_t *a,
 				  const xfrm_address_t *b,
 				  u8 prefixlen, u16 family)
 {
+	u32 ma, mb, mask;
 	unsigned int pdw, pbi;
 	int delta = 0;
 
 	switch (family) {
 	case AF_INET:
-		if (sizeof(long) == 4 && prefixlen == 0)
-			return ntohl(a->a4) - ntohl(b->a4);
-		return (ntohl(a->a4) & ((~0UL << (32 - prefixlen)))) -
-		       (ntohl(b->a4) & ((~0UL << (32 - prefixlen))));
+		if (prefixlen == 0)
+			return 0;
+		mask = ~0U << (32 - prefixlen);
+		ma = ntohl(a->a4) & mask;
+		mb = ntohl(b->a4) & mask;
+		if (ma < mb)
+			delta = -1;
+		else if (ma > mb)
+			delta = 1;
+		break;
 	case AF_INET6:
 		pdw = prefixlen >> 5;
 		pbi = prefixlen & 0x1f;
@@ -812,10 +819,13 @@ static int xfrm_policy_addr_delta(const xfrm_address_t *a,
 				return delta;
 		}
 		if (pbi) {
-			u32 mask = ~0u << (32 - pbi);
-
-			delta = (ntohl(a->a6[pdw]) & mask) -
-				(ntohl(b->a6[pdw]) & mask);
+			mask = ~0U << (32 - pbi);
+			ma = ntohl(a->a6[pdw]) & mask;
+			mb = ntohl(b->a6[pdw]) & mask;
+			if (ma < mb)
+				delta = -1;
+			else if (ma > mb)
+				delta = 1;
 		}
 		break;
 	default:
tools/testing/selftests/net/xfrm_policy.sh +43 -0

55 lines not on this page · raw message

Signature · 1 line
2.25.1
#6 lore raw
In reply to Steffen Klassert
This pull request was applied to netdev/net.git (refs/heads/master):

Hello:

This pull request was applied to netdev/net.git (refs/heads/master):

Quoted 8 lines · On Thu, 21 Jan 2021 13:15:53 +0100 you wrote:

1) Fix a rare panic on SMP systems when packet reordering happens between anti replay check and update. From Shmulik Ladkani.

2) Fix disable_xfrm sysctl when used on xfrm interfaces. From Eyal Birger.

[...]

Here is the summary with links:
- pull request (net): ipsec 2021-01-21

    https://git.kernel.org/netdev/net/c/35c715c30b95

You are awesome, thank you!
--
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html