From owner-svn-src-all@freebsd.org Sun Jan 24 07:56:47 2016 Return-Path: Delivered-To: svn-src-all@mailman.ysv.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:1900:2254:206a::19:1]) by mailman.ysv.freebsd.org (Postfix) with ESMTP id 7EBDBA8FE1D; Sun, 24 Jan 2016 07:56:47 +0000 (UTC) (envelope-from glebius@FreeBSD.org) Received: from cell.glebi.us (glebi.us [96.95.210.25]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client CN "cell.glebi.us", Issuer "cell.glebi.us" (not verified)) by mx1.freebsd.org (Postfix) with ESMTPS id 699AF1B1D; Sun, 24 Jan 2016 07:56:47 +0000 (UTC) (envelope-from glebius@FreeBSD.org) Received: from cell.glebi.us (localhost [127.0.0.1]) by cell.glebi.us (8.15.2/8.15.2) with ESMTPS id u0O7ufpm008889 (version=TLSv1.2 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=NO); Sat, 23 Jan 2016 23:56:41 -0800 (PST) (envelope-from glebius@FreeBSD.org) Received: (from glebius@localhost) by cell.glebi.us (8.15.2/8.15.2/Submit) id u0O7uf8K008888; Sat, 23 Jan 2016 23:56:41 -0800 (PST) (envelope-from glebius@FreeBSD.org) X-Authentication-Warning: cell.glebi.us: glebius set sender to glebius@FreeBSD.org using -f Date: Sat, 23 Jan 2016 23:56:40 -0800 From: Gleb Smirnoff To: Lawrence Stewart Cc: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org Subject: Re: svn commit: r294536 - head/sys/netinet Message-ID: <20160124075640.GD1004@FreeBSD.org> References: <201601212253.u0LMrC7B016136@repo.freebsd.org> <56A1C9F6.2010708@freebsd.org> <20160122180411.GA1004@FreeBSD.org> <56A3271F.6010904@freebsd.org> MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="UlVJffcvxoiEqYs2" Content-Disposition: inline In-Reply-To: <56A3271F.6010904@freebsd.org> User-Agent: Mutt/1.5.24 (2015-08-30) X-BeenThere: svn-src-all@freebsd.org X-Mailman-Version: 2.1.20 Precedence: list List-Id: "SVN commit messages for the entire src tree \(except for " user" and " projects" \)" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sun, 24 Jan 2016 07:56:47 -0000 --UlVJffcvxoiEqYs2 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Lawrence, On Sat, Jan 23, 2016 at 06:09:19PM +1100, Lawrence Stewart wrote: L> > Is that the race you refer to? L> L> No, the TCP_CONGESTION refactoring i.e. this commit, introduced races in L> the get and set cases. I guess I didn't provide enough context in L> Crucible, so here goes... L> L> The post refactoring get code is now: L> L> case TCP_CONGESTION: L> INP_WUNLOCK(inp); L> error = sooptcopyout(sopt, CC_ALGO(tp)->name, TCP_CA_NAME_MAX); L> break; L> L> Consider that tp is using cc_blah and that the cc_blah module is L> unloaded as the copy out is happening. By not holding the INP lock, the L> CC module unload code is able to walk the list of active connections, L> find this connection is using cc_blah, acquire the INP lock, switch this L> connection to cc_newreno, release the lock and finally unload the L> cc_blah module, rendering the pointer passed in to sooptcopyout garbage. L> See cc_deregister_algo() in cc.c and tcp_ccalgounload() in tcp_subr.c L> for details related to CC module unload. Understood. Can you please review this patch? It basicly shifts INP_WLOCK earlier in the SOPT_SET case, and reverts to old code the SOPT_GET case. It returns back the stack based string buffer. -- Totus tuus, Glebius. --UlVJffcvxoiEqYs2 Content-Type: text/x-diff; charset=us-ascii Content-Disposition: attachment; filename="tcp_usrreq.c.diff" Index: tcp_usrreq.c =================================================================== --- tcp_usrreq.c (revision 294658) +++ tcp_usrreq.c (working copy) @@ -1479,7 +1479,7 @@ tcp_default_ctloutput(struct socket *so, struct so u_int ui; struct tcp_info ti; struct cc_algo *algo; - char *buf; + char *pbuf, buf[TCP_CA_NAME_MAX]; /* * For TCP_CCALGOOPT forward the control to CC module, for both @@ -1488,22 +1488,22 @@ tcp_default_ctloutput(struct socket *so, struct so switch (sopt->sopt_name) { case TCP_CCALGOOPT: INP_WUNLOCK(inp); - buf = malloc(sopt->sopt_valsize, M_TEMP, M_WAITOK | M_ZERO); - error = sooptcopyin(sopt, buf, sopt->sopt_valsize, + pbuf = malloc(sopt->sopt_valsize, M_TEMP, M_WAITOK | M_ZERO); + error = sooptcopyin(sopt, pbuf, sopt->sopt_valsize, sopt->sopt_valsize); if (error) { - free(buf, M_TEMP); + free(pbuf, M_TEMP); return (error); } INP_WLOCK_RECHECK(inp); if (CC_ALGO(tp)->ctl_output != NULL) - error = CC_ALGO(tp)->ctl_output(tp->ccv, sopt, buf); + error = CC_ALGO(tp)->ctl_output(tp->ccv, sopt, pbuf); else error = ENOENT; INP_WUNLOCK(inp); if (error == 0 && sopt->sopt_dir == SOPT_GET) - error = sooptcopyout(sopt, buf, sopt->sopt_valsize); - free(buf, M_TEMP); + error = sooptcopyout(sopt, pbuf, sopt->sopt_valsize); + free(pbuf, M_TEMP); return (error); } @@ -1600,12 +1600,10 @@ unlock_and_done: case TCP_CONGESTION: INP_WUNLOCK(inp); - buf = malloc(TCP_CA_NAME_MAX, M_TEMP, M_WAITOK|M_ZERO); error = sooptcopyin(sopt, buf, TCP_CA_NAME_MAX, 1); - if (error) { - free(buf, M_TEMP); + if (error) break; - } + INP_WLOCK_RECHECK(inp); CC_LIST_RLOCK(); STAILQ_FOREACH(algo, &cc_list, entries) if (strncmp(buf, algo->name, @@ -1612,12 +1610,11 @@ unlock_and_done: TCP_CA_NAME_MAX) == 0) break; CC_LIST_RUNLOCK(); - free(buf, M_TEMP); if (algo == NULL) { + INP_WUNLOCK(inp); error = EINVAL; break; } - INP_WLOCK_RECHECK(inp); /* * We hold a write lock over the tcb so it's safe to * do these things without ordering concerns. @@ -1786,9 +1783,10 @@ unlock_and_done: error = sooptcopyout(sopt, &ti, sizeof ti); break; case TCP_CONGESTION: + bzero(buf, sizeof(buf)); + strlcpy(buf, CC_ALGO(tp)->name, TCP_CA_NAME_MAX); INP_WUNLOCK(inp); - error = sooptcopyout(sopt, CC_ALGO(tp)->name, - TCP_CA_NAME_MAX); + error = sooptcopyout(sopt, buf, TCP_CA_NAME_MAX); break; case TCP_KEEPIDLE: case TCP_KEEPINTVL: --UlVJffcvxoiEqYs2--