From nobody@FreeBSD.org  Sat Jan 17 23:49:58 2004
Return-Path: <nobody@FreeBSD.org>
Received: from mx1.FreeBSD.org (mx1.freebsd.org [216.136.204.125])
	by hub.freebsd.org (Postfix) with ESMTP id C5CD716A4CE
	for <freebsd-gnats-submit@FreeBSD.org>; Sat, 17 Jan 2004 23:49:58 -0800 (PST)
Received: from www.freebsd.org (www.freebsd.org [216.136.204.117])
	by mx1.FreeBSD.org (Postfix) with ESMTP id EEAB243D31
	for <freebsd-gnats-submit@FreeBSD.org>; Sat, 17 Jan 2004 23:49:57 -0800 (PST)
	(envelope-from nobody@FreeBSD.org)
Received: from www.freebsd.org (localhost [127.0.0.1])
	by www.freebsd.org (8.12.10/8.12.10) with ESMTP id i0I7nvdL081245
	for <freebsd-gnats-submit@FreeBSD.org>; Sat, 17 Jan 2004 23:49:57 -0800 (PST)
	(envelope-from nobody@www.freebsd.org)
Received: (from nobody@localhost)
	by www.freebsd.org (8.12.10/8.12.10/Submit) id i0I7nvVZ081244;
	Sat, 17 Jan 2004 23:49:57 -0800 (PST)
	(envelope-from nobody)
Message-Id: <200401180749.i0I7nvVZ081244@www.freebsd.org>
Date: Sat, 17 Jan 2004 23:49:57 -0800 (PST)
From: Katsuhisa ABE <katsuhisa.abe@nifty.com>
To: freebsd-gnats-submit@FreeBSD.org
Subject: kernel gets into a panic to put invalid value in setsockopt
X-Send-Pr-Version: www-2.0

>Number:         61513
>Category:       kern
>Synopsis:       kernel gets into a panic to put invalid value in setsockopt
>Confidential:   no
>Severity:       non-critical
>Priority:       low
>Responsible:    cperciva
>State:          closed
>Quarter:        
>Keywords:       
>Date-Required:  
>Class:          sw-bug
>Submitter-Id:   current-users
>Arrival-Date:   Sat Jan 17 23:50:12 PST 2004
>Closed-Date:    Mon Mar 29 17:54:54 PST 2004
>Last-Modified:  Mon Mar 29 17:54:54 PST 2004
>Originator:     Katsuhisa ABE
>Release:        5.2-RELEASE
>Organization:
>Environment:
FreeBSD unzen 5.2-RELEASE FreeBSD 5.2-RELEASE #30: Mon Jan 12 13:46:54 JST 2004     root@unzen:/mnt1/usr/obj/usr/src/sys/UNZEN  i386
      
>Description:
When I put unproper value (uninitialzed pointer) into the 4th argument, "optval" for setsockopt,
then kernel starts to reboot (not generate core).
>How-To-Repeat:
When I execute the following code as normal user.
#include <stdio.h>
#include <stdlib.h>

#include <sys/types.h>
#include <sys/socket.h>

#include <netinet/in.h>
#include <arpa/inet.h>

int
main(int argc, char *argv[]) {

    int sd;
    int retval;
    struct in6_pktinfo *pktinfo;

    if ((sd = socket(PF_INET6, SOCK_STREAM, IPPROTO_TCP)) < 0 ) {
        fprintf(stderr, "cannot create TCP socket\n");
        exit(1);
    }
    
    if ((retval = setsockopt(sd, IPPROTO_IPV6, IPV6_PKTINFO,
                             pktinfo, sizeof(struct in6_pktinfo))) != 0) {
        fprintf(stderr, "cannot set IPV6_PKTINFO\n");
        exit(1);
    }

}





>Fix:
      
>Release-Note:
>Audit-Trail:

From: Redmar Kerkhoff <redmar@interrupt.nl>
To: freebsd-gnats-submit@FreeBSD.org, katsuhisa.abe@nifty.com
Cc: redmar@interrupt.nl
Subject: Re: kern/61513: kernel gets into a panic to put invalid value in setsockopt
Date: Fri, 23 Jan 2004 15:44:42 +0100

 >Description:
 "When I put unproper value (uninitialzed pointer) into the 4th argument, "optval" for setsockopt,
 then kernel starts to reboot (not generate core)."
 
 i have traced the problem down to ip6_setpktoption() but the actual
 problem lies in unitialized pointer assignment in setsockopt.
 (found in /sys/kern/uipc_syscalls.c) The setsockopt call is forwarding
 de optval information from userland without handling any address errors.
 ( sopt.sopt_val = uap->val; ) finally exploding in this case at 
 ip6_setpktoption(). 
 
 i looked at netbsd and openbsd and they we're both resistent to
 this bug. the fix is more or less derived from their getsockopt handling.
 
 here's the fix against 5.2-RELEASE. (but can also be applied to current afaik)
 (the only little problem now is the double assignment of sopt.sopt_val, options are
 removing the assignment in the mutex lock or putting the copyin in the mutex
 lock )
 
 
 >Fix:
 
 
 --- uipc_syscalls.orig  Fri Jan 23 10:38:46 2004
 +++ uipc_syscalls.c     Fri Jan 23 10:27:33 2004
 @@ -1308,6 +1308,10 @@
                 return (EFAULT);
         if (uap->valsize < 0)
                 return (EINVAL);
 +       
 +       error = copyin(uap->val, &sopt.sopt_val, sizeof(sopt.sopt_val));
 +       if (error)
 +               return (error);
  
         mtx_lock(&Giant);
         if ((error = fgetsock(td, uap->s, &so, NULL)) == 0) {
         
 
 
Responsible-Changed-From-To: freebsd-bugs->cperciva 
Responsible-Changed-By: cperciva 
Responsible-Changed-When: Thu Mar 18 21:55:32 PST 2004 
Responsible-Changed-Why:  
I'll take this. 

http://www.freebsd.org/cgi/query-pr.cgi?pr=61513 
State-Changed-From-To: open->closed 
State-Changed-By: cperciva 
State-Changed-When: Mon Mar 29 17:54:38 PST 2004 
State-Changed-Why:  
Fixed. 

http://www.freebsd.org/cgi/query-pr.cgi?pr=61513 
>Unformatted:
