Skip to content

Commit a6c7ab5

Browse files
herbertxDavid S. Miller
authored andcommitted
[IPSEC]: Policy list disorder
The recent hashing introduced an off-by-one bug in policy list insertion. Instead of adding after the last entry with a lesser or equal priority, we're adding after the successor of that entry. This patch fixes this and also adds a warning if we detect a duplicate entry in the policy list. This should never happen due to this if clause. Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au> Signed-off-by: David S. Miller <davem@davemloft.net>
1 parent 8f1adb5 commit a6c7ab5

1 file changed

Lines changed: 5 additions & 11 deletions

File tree

‎net/xfrm/xfrm_policy.c‎

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -650,19 +650,18 @@ int xfrm_policy_insert(int dir, struct xfrm_policy *policy, int excl)
650650
struct xfrm_policy *pol;
651651
struct xfrm_policy *delpol;
652652
struct hlist_head *chain;
653-
struct hlist_node *entry, *newpos, *last;
653+
struct hlist_node *entry, *newpos;
654654
struct dst_entry *gc_list;
655655

656656
write_lock_bh(&xfrm_policy_lock);
657657
chain = policy_hash_bysel(&policy->selector, policy->family, dir);
658658
delpol = NULL;
659659
newpos = NULL;
660-
last = NULL;
661660
hlist_for_each_entry(pol, entry, chain, bydst) {
662-
if (!delpol &&
663-
pol->type == policy->type &&
661+
if (pol->type == policy->type &&
664662
!selector_cmp(&pol->selector, &policy->selector) &&
665-
xfrm_sec_ctx_match(pol->security, policy->security)) {
663+
xfrm_sec_ctx_match(pol->security, policy->security) &&
664+
!WARN_ON(delpol)) {
666665
if (excl) {
667666
write_unlock_bh(&xfrm_policy_lock);
668667
return -EEXIST;
@@ -671,17 +670,12 @@ int xfrm_policy_insert(int dir, struct xfrm_policy *policy, int excl)
671670
if (policy->priority > pol->priority)
672671
continue;
673672
} else if (policy->priority >= pol->priority) {
674-
last = &pol->bydst;
673+
newpos = &pol->bydst;
675674
continue;
676675
}
677-
if (!newpos)
678-
newpos = &pol->bydst;
679676
if (delpol)
680677
break;
681-
last = &pol->bydst;
682678
}
683-
if (!newpos)
684-
newpos = last;
685679
if (newpos)
686680
hlist_add_after(newpos, &policy->bydst);
687681
else

0 commit comments

Comments
 (0)