From c2f2371697744b4a7b44910ebbda9762fb5cb487 Mon Sep 17 00:00:00 2001 From: bluhm Date: Mon, 25 Oct 2021 18:25:01 +0000 Subject: [PATCH] Call a locked variant of tdb_unlink() from tdb_walk(). Fixes a mutex locking against myself panic introduced by my previous commit. OK beck@ patrick@ --- sys/net/pfkeyv2.c | 8 +++++--- sys/netinet/ip_ipsp.c | 14 +++++++++++--- sys/netinet/ip_ipsp.h | 3 ++- 3 files changed, 18 insertions(+), 7 deletions(-) diff --git a/sys/net/pfkeyv2.c b/sys/net/pfkeyv2.c index 336619f4c89..f34bd29a7f5 100644 --- a/sys/net/pfkeyv2.c +++ b/sys/net/pfkeyv2.c @@ -1,4 +1,4 @@ -/* $OpenBSD: pfkeyv2.c,v 1.220 2021/10/22 12:30:53 bluhm Exp $ */ +/* $OpenBSD: pfkeyv2.c,v 1.221 2021/10/25 18:25:01 bluhm Exp $ */ /* * @(#)COPYRIGHT 1.1 (NRL) 17 January 1995 @@ -1042,8 +1042,10 @@ int pfkeyv2_sa_flush(struct tdb *tdb, void *satype_vp, int last) { if (!(*((u_int8_t *) satype_vp)) || - tdb->tdb_satype == *((u_int8_t *) satype_vp)) - tdb_delete(tdb); + tdb->tdb_satype == *((u_int8_t *) satype_vp)) { + tdb_unlink_locked(tdb); + tdb_free(tdb); + } return (0); } diff --git a/sys/netinet/ip_ipsp.c b/sys/netinet/ip_ipsp.c index 01dec0abc8f..25801a57df1 100644 --- a/sys/netinet/ip_ipsp.c +++ b/sys/netinet/ip_ipsp.c @@ -1,4 +1,4 @@ -/* $OpenBSD: ip_ipsp.c,v 1.247 2021/10/25 16:00:12 bluhm Exp $ */ +/* $OpenBSD: ip_ipsp.c,v 1.248 2021/10/25 18:25:01 bluhm Exp $ */ /* * The authors of this code are John Ioannidis (ji@tla.org), * Angelos D. Keromytis (kermit@csd.uch.gr), @@ -732,11 +732,20 @@ puttdb(struct tdb *tdbp) void tdb_unlink(struct tdb *tdbp) +{ + mtx_enter(&tdb_sadb_mtx); + tdb_unlink_locked(tdbp); + mtx_leave(&tdb_sadb_mtx); +} + +void +tdb_unlink_locked(struct tdb *tdbp) { struct tdb *tdbpp; u_int32_t hashval; - mtx_enter(&tdb_sadb_mtx); + MUTEX_ASSERT_LOCKED(&tdb_sadb_mtx); + hashval = tdb_hash(tdbp->tdb_spi, &tdbp->tdb_dst, tdbp->tdb_sproto); if (tdbh[hashval] == tdbp) { @@ -793,7 +802,6 @@ tdb_unlink(struct tdb *tdbp) ipsecstat_inc(ipsec_prevtunnels); } #endif /* IPSEC */ - mtx_leave(&tdb_sadb_mtx); } void diff --git a/sys/netinet/ip_ipsp.h b/sys/netinet/ip_ipsp.h index 1fcd6ca87ff..efe90827f59 100644 --- a/sys/netinet/ip_ipsp.h +++ b/sys/netinet/ip_ipsp.h @@ -1,4 +1,4 @@ -/* $OpenBSD: ip_ipsp.h,v 1.218 2021/10/24 23:33:37 tobhe Exp $ */ +/* $OpenBSD: ip_ipsp.h,v 1.219 2021/10/25 18:25:01 bluhm Exp $ */ /* * The authors of this code are John Ioannidis (ji@tla.org), * Angelos D. Keromytis (kermit@csd.uch.gr), @@ -558,6 +558,7 @@ struct tdb *tdb_alloc(u_int); void tdb_free(struct tdb *); int tdb_init(struct tdb *, u_int16_t, struct ipsecinit *); void tdb_unlink(struct tdb *); +void tdb_unlink_locked(struct tdb *); int tdb_walk(u_int, int (*)(struct tdb *, void *, int), void *); /* XF_IP4 */ -- 2.20.1