Skip to content

Commit cf37966

Browse files
Florian Westphalklassert
authored andcommitted
xfrm: do unconditional template resolution before pcpu cache check
Stephen Smalley says: Since 4.14-rc1, the selinux-testsuite has been encountering sporadic failures during testing of labeled IPSEC. git bisect pointed to commit ec30d ("xfrm: add xdst pcpu cache"). The xdst pcpu cache is only checking that the policies are the same, but does not validate that the policy, state, and flow match with respect to security context labeling. As a result, the wrong SA could be used and the receiver could end up performing permission checking and providing SO_PEERSEC or SCM_SECURITY values for the wrong security context. This fix makes it so that we always do the template resolution, and then checks that the found states match those in the pcpu bundle. This has the disadvantage of doing a bit more work (lookup in state hash table) if we can reuse the xdst entry (we only avoid xdst alloc/free) but we don't add a lot of extra work in case we can't reuse. xfrm_pol_dead() check is removed, reasoning is that xfrm_tmpl_resolve does all needed checks. Cc: Paul Moore <[email protected]> Fixes: ec30d78 ("xfrm: add xdst pcpu cache") Reported-by: Stephen Smalley <[email protected]> Tested-by: Stephen Smalley <[email protected]> Signed-off-by: Florian Westphal <[email protected]> Acked-by: Paul Moore <[email protected]> Signed-off-by: Steffen Klassert <[email protected]>
1 parent cb79a18 commit cf37966

File tree

1 file changed

+24
-18
lines changed

1 file changed

+24
-18
lines changed

net/xfrm/xfrm_policy.c

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1787,19 +1787,23 @@ void xfrm_policy_cache_flush(void)
17871787
put_online_cpus();
17881788
}
17891789

1790-
static bool xfrm_pol_dead(struct xfrm_dst *xdst)
1790+
static bool xfrm_xdst_can_reuse(struct xfrm_dst *xdst,
1791+
struct xfrm_state * const xfrm[],
1792+
int num)
17911793
{
1792-
unsigned int num_pols = xdst->num_pols;
1793-
unsigned int pol_dead = 0, i;
1794+
const struct dst_entry *dst = &xdst->u.dst;
1795+
int i;
17941796

1795-
for (i = 0; i < num_pols; i++)
1796-
pol_dead |= xdst->pols[i]->walk.dead;
1797+
if (xdst->num_xfrms != num)
1798+
return false;
17971799

1798-
/* Mark DST_OBSOLETE_DEAD to fail the next xfrm_dst_check() */
1799-
if (pol_dead)
1800-
xdst->u.dst.obsolete = DST_OBSOLETE_DEAD;
1800+
for (i = 0; i < num; i++) {
1801+
if (!dst || dst->xfrm != xfrm[i])
1802+
return false;
1803+
dst = dst->child;
1804+
}
18011805

1802-
return pol_dead;
1806+
return xfrm_bundle_ok(xdst);
18031807
}
18041808

18051809
static struct xfrm_dst *
@@ -1813,26 +1817,28 @@ xfrm_resolve_and_create_bundle(struct xfrm_policy **pols, int num_pols,
18131817
struct dst_entry *dst;
18141818
int err;
18151819

1820+
/* Try to instantiate a bundle */
1821+
err = xfrm_tmpl_resolve(pols, num_pols, fl, xfrm, family);
1822+
if (err <= 0) {
1823+
if (err != 0 && err != -EAGAIN)
1824+
XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTPOLERROR);
1825+
return ERR_PTR(err);
1826+
}
1827+
18161828
xdst = this_cpu_read(xfrm_last_dst);
18171829
if (xdst &&
18181830
xdst->u.dst.dev == dst_orig->dev &&
18191831
xdst->num_pols == num_pols &&
1820-
!xfrm_pol_dead(xdst) &&
18211832
memcmp(xdst->pols, pols,
18221833
sizeof(struct xfrm_policy *) * num_pols) == 0 &&
1823-
xfrm_bundle_ok(xdst)) {
1834+
xfrm_xdst_can_reuse(xdst, xfrm, err)) {
18241835
dst_hold(&xdst->u.dst);
1836+
while (err > 0)
1837+
xfrm_state_put(xfrm[--err]);
18251838
return xdst;
18261839
}
18271840

18281841
old = xdst;
1829-
/* Try to instantiate a bundle */
1830-
err = xfrm_tmpl_resolve(pols, num_pols, fl, xfrm, family);
1831-
if (err <= 0) {
1832-
if (err != 0 && err != -EAGAIN)
1833-
XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTPOLERROR);
1834-
return ERR_PTR(err);
1835-
}
18361842

18371843
dst = xfrm_bundle_create(pols[0], xfrm, err, fl, dst_orig);
18381844
if (IS_ERR(dst)) {

0 commit comments

Comments
 (0)