git: 057b43b26235 - stable/13 - bpf: Fix BIOCPROMISC locking

From: Mark Johnston <markj_at_FreeBSD.org>
Date: Fri, 19 Aug 2022 11:54:32 UTC
The branch stable/13 has been updated by markj:

URL: https://cgit.FreeBSD.org/src/commit/?id=057b43b262350c60409fc1eaa34a61259b64f57f

commit 057b43b262350c60409fc1eaa34a61259b64f57f
Author:     Mark Johnston <markj@FreeBSD.org>
AuthorDate: 2022-08-05 20:25:05 +0000
Commit:     Mark Johnston <markj@FreeBSD.org>
CommitDate: 2022-08-19 11:54:04 +0000

    bpf: Fix BIOCPROMISC locking
    
    BPF might put an interface in promiscuous mode when handling the
    BIOCSDLT ioctl.  When this happens, a flag is set in the BPF descriptor
    so that the old interface can be restored when the BPF descriptor is
    destroyed.
    
    The BIOCPROMISC ioctl can also be used to put a BPF descriptor's
    interface into promiscuous mode, but there was nothing synchronizing the
    flag.  Fix this by modifying the ioctl handler to acquire the global BPF
    mutex, which is used to synchronize ifpromisc() calls elsewhere in BPF.
    
    Reviewed by:    kp, melifaro
    Sponsored by:   The FreeBSD Foundation
    
    (cherry picked from commit 220818ac030726c24cbf9df6df5c9d019993b875)
---
 sys/net/bpf.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/sys/net/bpf.c b/sys/net/bpf.c
index 81d88305e1a1..ff00112d6538 100644
--- a/sys/net/bpf.c
+++ b/sys/net/bpf.c
@@ -1503,18 +1503,18 @@ bpfioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flags,
 	 * Put interface into promiscuous mode.
 	 */
 	case BIOCPROMISC:
+		BPF_LOCK();
 		if (d->bd_bif == NULL) {
 			/*
 			 * No interface attached yet.
 			 */
 			error = EINVAL;
-			break;
-		}
-		if (d->bd_promisc == 0) {
+		} else if (d->bd_promisc == 0) {
 			error = ifpromisc(d->bd_bif->bif_ifp, 1);
 			if (error == 0)
 				d->bd_promisc = 1;
 		}
+		BPF_UNLOCK();
 		break;
 
 	/*