From owner-dev-commits-src-branches@freebsd.org Fri Sep 24 13:32:12 2021 Return-Path: Delivered-To: dev-commits-src-branches@mailman.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mailman.nyi.freebsd.org (Postfix) with ESMTP id 196B966DA4E; Fri, 24 Sep 2021 13:32:12 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "R3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4HGCcq2QBxz4qSD; Fri, 24 Sep 2021 13:32:11 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 206A617A4C; Fri, 24 Sep 2021 13:32:11 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.16.1/8.16.1) with ESMTP id 18ODWBxX087030; Fri, 24 Sep 2021 13:32:11 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.16.1/8.16.1/Submit) id 18ODWB08087029; Fri, 24 Sep 2021 13:32:11 GMT (envelope-from git) Date: Fri, 24 Sep 2021 13:32:11 GMT Message-Id: <202109241332.18ODWB08087029@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-branches@FreeBSD.org From: Mark Johnston Subject: git: e9e4f8092c2e - stable/13 - socket: Fix a use-after-free in soclose() MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: markj X-Git-Repository: src X-Git-Refname: refs/heads/stable/13 X-Git-Reftype: branch X-Git-Commit: e9e4f8092c2e0ccac922f930072cd14b22ee7c1c Auto-Submitted: auto-generated X-BeenThere: dev-commits-src-branches@freebsd.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: Commits to the stable branches of the FreeBSD src repository List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 24 Sep 2021 13:32:12 -0000 The branch stable/13 has been updated by markj: URL: https://cgit.FreeBSD.org/src/commit/?id=e9e4f8092c2e0ccac922f930072cd14b22ee7c1c commit e9e4f8092c2e0ccac922f930072cd14b22ee7c1c Author: Mark Johnston AuthorDate: 2021-09-17 16:26:06 +0000 Commit: Mark Johnston CommitDate: 2021-09-24 13:01:51 +0000 socket: Fix a use-after-free in soclose() After releasing the fd reference to a socket "so", we should avoid testing SOLISTENING(so) since the socket may have been freed. Instead, directly test whether the list of unaccepted sockets is empty. Fixes: f4bb1869ddd2 ("Consistently use the SOLISTENING() macro") Pointy hat: markj Sponsored by: The FreeBSD Foundation (cherry picked from commit dfcef8771484271f2bccdb1dbc088a838441c5a7) --- sys/kern/uipc_socket.c | 24 ++++++++++-------------- 1 file changed, 10 insertions(+), 14 deletions(-) diff --git a/sys/kern/uipc_socket.c b/sys/kern/uipc_socket.c index dc7407808535..13482fce5980 100644 --- a/sys/kern/uipc_socket.c +++ b/sys/kern/uipc_socket.c @@ -1176,6 +1176,7 @@ int soclose(struct socket *so) { struct accept_queue lqueue; + struct socket *sp, *tsp; int error = 0; KASSERT(!(so->so_state & SS_NOFDREF), ("soclose: SS_NOFDREF on enter")); @@ -1210,11 +1211,9 @@ drop: if (so->so_proto->pr_usrreqs->pru_close != NULL) (*so->so_proto->pr_usrreqs->pru_close)(so); + TAILQ_INIT(&lqueue); SOCK_LOCK(so); if (SOLISTENING(so)) { - struct socket *sp; - - TAILQ_INIT(&lqueue); TAILQ_SWAP(&lqueue, &so->sol_incomp, socket, so_list); TAILQ_CONCAT(&lqueue, &so->sol_comp, so_list); @@ -1232,17 +1231,14 @@ drop: KASSERT((so->so_state & SS_NOFDREF) == 0, ("soclose: NOFDREF")); so->so_state |= SS_NOFDREF; sorele(so); - if (SOLISTENING(so)) { - struct socket *sp, *tsp; - - TAILQ_FOREACH_SAFE(sp, &lqueue, so_list, tsp) { - SOCK_LOCK(sp); - if (sp->so_count == 0) { - SOCK_UNLOCK(sp); - soabort(sp); - } else - /* sp is now in sofree() */ - SOCK_UNLOCK(sp); + TAILQ_FOREACH_SAFE(sp, &lqueue, so_list, tsp) { + SOCK_LOCK(sp); + if (refcount_load(&sp->so_count) == 0) { + SOCK_UNLOCK(sp); + soabort(sp); + } else { + /* sp is now in sofree() */ + SOCK_UNLOCK(sp); } } CURVNET_RESTORE();