From nobody Sun Nov 14 14:04:35 2021 X-Original-To: dev-commits-src-all@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id D7525184C72A; Sun, 14 Nov 2021 14:04:35 +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 4HsYwg5g5cz3MZ3; Sun, 14 Nov 2021 14:04:35 +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 A2697277C9; Sun, 14 Nov 2021 14:04:35 +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 1AEE4ZPL089142; Sun, 14 Nov 2021 14:04:35 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.16.1/8.16.1/Submit) id 1AEE4Zr8089141; Sun, 14 Nov 2021 14:04:35 GMT (envelope-from git) Date: Sun, 14 Nov 2021 14:04:35 GMT Message-Id: <202111141404.1AEE4Zr8089141@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: Michael Tuexen Subject: git: 2f62f92e3745 - main - tcp: Fix a locking issue related to logging List-Id: Commit messages for all branches of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-all List-Help: List-Post: List-Subscribe: List-Unsubscribe: Sender: owner-dev-commits-src-all@freebsd.org X-BeenThere: dev-commits-src-all@freebsd.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: tuexen X-Git-Repository: src X-Git-Refname: refs/heads/main X-Git-Reftype: branch X-Git-Commit: 2f62f92e3745095b64433ed9369b70ccf126328b Auto-Submitted: auto-generated X-ThisMailContainsUnwantedMimeParts: N The branch main has been updated by tuexen: URL: https://cgit.FreeBSD.org/src/commit/?id=2f62f92e3745095b64433ed9369b70ccf126328b commit 2f62f92e3745095b64433ed9369b70ccf126328b Author: Michael Tuexen AuthorDate: 2021-11-14 13:56:43 +0000 Commit: Michael Tuexen CommitDate: 2021-11-14 14:04:27 +0000 tcp: Fix a locking issue related to logging tcp_respond() is sometimes called with only a read lock. The logging however, requires a write lock. So either try to upgrade the lock if needed, or don't log the packet. Reported by: syzbot+8151ef969c170f76706b@syzkaller.appspotmail.com Reported by: syzbot+eb679adb3304c511c1e4@syzkaller.appspotmail.com Reviewed by: markj, rrs Sponsored by: Netflix, Inc. Differential Revision: https://reviews.freebsd.org/D32983 --- sys/netinet/tcp_subr.c | 38 +++++++++++++++++++++++--------------- 1 file changed, 23 insertions(+), 15 deletions(-) diff --git a/sys/netinet/tcp_subr.c b/sys/netinet/tcp_subr.c index 4805d6c80327..5563148a52ad 100644 --- a/sys/netinet/tcp_subr.c +++ b/sys/netinet/tcp_subr.c @@ -1755,7 +1755,7 @@ tcp_respond(struct tcpcb *tp, void *ipgen, struct tcphdr *th, struct mbuf *m, int isipv6; #endif /* INET6 */ int optlen, tlen, win, ulen; - bool incl_opts; + bool incl_opts, lock_upgraded; uint16_t port; int output_ret; @@ -2088,21 +2088,29 @@ tcp_respond(struct tcpcb *tp, void *ipgen, struct tcphdr *th, struct mbuf *m, TCP_PROBE3(debug__output, tp, th, m); if (flags & TH_RST) TCP_PROBE5(accept__refused, NULL, NULL, m, tp, nth); + lock_upgraded = false; + lgb = NULL; if ((tp != NULL) && (tp->t_logstate != TCP_LOG_STATE_OFF)) { union tcp_log_stackspecific log; struct timeval tv; - memset(&log.u_bbr, 0, sizeof(log.u_bbr)); - log.u_bbr.inhpts = tp->t_inpcb->inp_in_hpts; - log.u_bbr.ininput = tp->t_inpcb->inp_in_input; - log.u_bbr.flex8 = 4; - log.u_bbr.pkts_out = tp->t_maxseg; - log.u_bbr.timeStamp = tcp_get_usecs(&tv); - log.u_bbr.delivered = 0; - lgb = tcp_log_event_(tp, nth, NULL, NULL, TCP_LOG_OUT, ERRNO_UNK, - 0, &log, false, NULL, NULL, 0, &tv); - } else - lgb = NULL; + lock_upgraded = !INP_WLOCKED(inp) && INP_TRY_UPGRADE(inp); + /* + *`If we don't already own the write lock and can't upgrade, + * just don't log the event, but still send the response. + */ + if (INP_WLOCKED(inp)) { + memset(&log.u_bbr, 0, sizeof(log.u_bbr)); + log.u_bbr.inhpts = tp->t_inpcb->inp_in_hpts; + log.u_bbr.ininput = tp->t_inpcb->inp_in_input; + log.u_bbr.flex8 = 4; + log.u_bbr.pkts_out = tp->t_maxseg; + log.u_bbr.timeStamp = tcp_get_usecs(&tv); + log.u_bbr.delivered = 0; + lgb = tcp_log_event_(tp, nth, NULL, NULL, TCP_LOG_OUT, ERRNO_UNK, + 0, &log, false, NULL, NULL, 0, &tv); + } + } #ifdef INET6 if (isipv6) { @@ -2119,10 +2127,10 @@ tcp_respond(struct tcpcb *tp, void *ipgen, struct tcphdr *th, struct mbuf *m, output_ret = ip_output(m, NULL, NULL, 0, NULL, inp); } #endif - if (lgb) { + if (lgb != NULL) lgb->tlb_errno = output_ret; - lgb = NULL; - } + if (lock_upgraded) + INP_DOWNGRADE(inp); } /*