From nobody@FreeBSD.org Tue Mar 29 20:40:36 2005 Return-Path: Received: from mx1.FreeBSD.org (mx1.freebsd.org [216.136.204.125]) by hub.freebsd.org (Postfix) with ESMTP id 3A5CB16A4CE for ; Tue, 29 Mar 2005 20:40:36 +0000 (GMT) Received: from www.freebsd.org (www.freebsd.org [216.136.204.117]) by mx1.FreeBSD.org (Postfix) with ESMTP id 1822D43D45 for ; Tue, 29 Mar 2005 20:40:36 +0000 (GMT) (envelope-from nobody@FreeBSD.org) Received: from www.freebsd.org (localhost [127.0.0.1]) by www.freebsd.org (8.13.1/8.13.1) with ESMTP id j2TKeZOC029550 for ; Tue, 29 Mar 2005 20:40:35 GMT (envelope-from nobody@www.freebsd.org) Received: (from nobody@localhost) by www.freebsd.org (8.13.1/8.13.1/Submit) id j2TKeZ13029549; Tue, 29 Mar 2005 20:40:35 GMT (envelope-from nobody) Message-Id: <200503292040.j2TKeZ13029549@www.freebsd.org> Date: Tue, 29 Mar 2005 20:40:35 GMT From: Anjali Kulkarni To: freebsd-gnats-submit@FreeBSD.org Subject: When looking for an unused port number for bind or connect, if low & high port range are equal, kernel can trap in divide by zero error X-Send-Pr-Version: www-2.3 >Number: 79342 >Category: kern >Synopsis: [patch] When looking for an unused port number for bind or connect, if low & high port range are equal, kernel can trap in divide by zero error >Confidential: no >Severity: serious >Priority: medium >Responsible: maxim >State: closed >Quarter: >Keywords: >Date-Required: >Class: sw-bug >Submitter-Id: current-users >Arrival-Date: Tue Mar 29 20:50:01 GMT 2005 >Closed-Date: Fri Apr 22 08:10:28 GMT 2005 >Last-Modified: Fri Apr 22 08:10:28 GMT 2005 >Originator: Anjali Kulkarni >Release: 4.10 >Organization: Juniper Networks >Environment: 4.10-RELEASE-p2 >Description: In src/sys/netinet/in_pcb.c, in the function in_pcbbind(), when trying to find an unused port number in the range of ports allowed to be used, there are cases to handle upper port limit > lower port limit and upper port limit < lower port limit, but no case to handle when they are equal. Consequently, if they are equal, the kernel will have a divide by zero trap when the line *lastport = first - (arc4random() % (first - last)) OR the line *lastport = first + (arc4random() % (last - first)) is executed, where first and last are the port ranges in which we check for an unused port number. >How-To-Repeat: set net.inet.ip.portrange.first = net.inet.ip.portrange.last. (say 1024) Now do a bind or a connect with a port number of 0. >Fix: In in_pcbbind(), there should be a check for the case when first & last are equal as follows: /* * Simple check to ensure all ports are not used up causing * a deadlock here. * * We split the two cases (up and down) so that the direction * is not being tested on each round of the loop. */ + if (first == last) { + inp->inp_laddr.s_addr = INADDR_ANY; + return (EADDRNOTAVAIL); + } else if (first > last) { - if (first > last) { >Release-Note: >Audit-Trail: From: Maxim Konovalov To: Anjali Kulkarni Cc: silby@freebsd.org, bug-followup@freebsd.org Subject: Re: kern/79342: When looking for an unused port number for bind or connect, if low & high port range are equal, kernel can trap in divide by zero error Date: Wed, 30 Mar 2005 08:37:19 +0400 (MSD) Hi, > >Synopsis: When looking for an unused port number for bind or > >connect, if low & high port range are equal, kernel can trap in > >divide by zero error [...] > >Release: 4.10 [...] > In src/sys/netinet/in_pcb.c, in the function in_pcbbind(), when > trying to find an unused port number in the range of ports allowed > to be used, there are cases to handle upper port limit > lower port > limit and upper port limit < lower port limit, but no case to handle > when they are equal. Consequently, if they are equal, the kernel > will have a divide by zero trap when the line *lastport = first - > (arc4random() % (first - last)) OR the line *lastport = first + > (arc4random() % (last - first)) is executed, where first and last > are the port ranges in which we check for an unused port number. > > >How-To-Repeat: > > set > > net.inet.ip.portrange.first = net.inet.ip.portrange.last. (say 1024) Just a note the bug is appeared with net.inet.ip.portrange.randomized=1 only. I think we need to stop doing random port allocation if last - first delta is ridiculous small. -- Maxim Konovalov From: Maxim Konovalov To: Anjali Kulkarni Cc: silby@freebsd.org, bug-followup@freebsd.org Subject: Re: kern/79342: When looking for an unused port number for bind or connect, if low & high port range are equal, kernel can trap in divide by zero error Date: Wed, 30 Mar 2005 21:18:27 +0400 (MSD) > Just a note the bug is appeared with > net.inet.ip.portrange.randomized=1 only. > > I think we need to stop doing random port allocation if last - first > delta is ridiculous small. Here is my version of the patch: Index: in_pcb.c =================================================================== RCS file: /home/ncvs/src/sys/netinet/in_pcb.c,v retrieving revision 1.161 diff -u -p -r1.161 in_pcb.c --- in_pcb.c 23 Mar 2005 09:26:38 -0000 1.161 +++ in_pcb.c 30 Mar 2005 16:36:58 -0000 @@ -411,13 +411,19 @@ in_pcbbind_setup(inp, nam, laddrp, lport * For UDP, use random port allocation as long as the user * allows it. For TCP (and as of yet unknown) connections, * use random port allocation only if the user allows it AND - * ipport_tick allows it. + * ipport_tick() allows it. */ if (ipport_randomized && (!ipport_stoprandom || pcbinfo == &udbinfo)) dorandom = 1; else dorandom = 0; + /* + * It makes no sense to do random port allocation if + * we have the only port available. + */ + if (first == last) + dorandom = 0; /* Make sure to not include UDP packets in the count. */ if (pcbinfo != &udbinfo) ipport_tcpallocs++; %%% It's not perfect because it should turn random port allocation off if the diapason of ports is small but I am not sure yet we need an additional sysctl for that. Mike, what is your opinion? As a side note for the original PR: random port allocation was broken in RELENG_4, that is why we turned it off by default in RELENG_4 some time after 4.10-REL and turned it on back right before 4.11-REL when Mike implemented a new algorithm. If you are going to use 4.10-REL you need to turn the port randomization off or import Mike's code. -- Maxim Konovalov From: Mike Silbersack To: Maxim Konovalov Cc: Anjali Kulkarni , bug-followup@freebsd.org Subject: Re: kern/79342: When looking for an unused port number for bind or connect, if low & high port range are equal, kernel can trap in divide by zero error Date: Wed, 30 Mar 2005 14:28:15 -0600 (CST) On Wed, 30 Mar 2005, Maxim Konovalov wrote: > It's not perfect because it should turn random port allocation off if > the diapason of ports is small but I am not sure yet we need an > additional sysctl for that. Mike, what is your opinion? If the port range is very small, turning random port allocation off makes sense. Since we'll have to do a patch either way, we might as well make that change. Mike "Silby" Silbersack State-Changed-From-To: open->patched State-Changed-By: maxim State-Changed-When: Fri Apr 8 08:44:41 GMT 2005 State-Changed-Why: Fixed in HEAD in a different way, thanks for the report! Responsible-Changed-From-To: freebsd-bugs->maxim Responsible-Changed-By: maxim Responsible-Changed-When: Fri Apr 8 08:44:41 GMT 2005 Responsible-Changed-Why: MFC reminder. http://www.freebsd.org/cgi/query-pr.cgi?pr=79342 State-Changed-From-To: patched->closed State-Changed-By: maxim State-Changed-When: Fri Apr 22 08:10:11 GMT 2005 State-Changed-Why: Merged to RELENG_5. http://www.freebsd.org/cgi/query-pr.cgi?pr=79342 >Unformatted: