From nobody@FreeBSD.org  Wed Oct 22 22:47:58 2008
Return-Path: <nobody@FreeBSD.org>
Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34])
	by hub.freebsd.org (Postfix) with ESMTP id 8FC8E106566C
	for <freebsd-gnats-submit@FreeBSD.org>; Wed, 22 Oct 2008 22:47:58 +0000 (UTC)
	(envelope-from nobody@FreeBSD.org)
Received: from www.freebsd.org (www.freebsd.org [IPv6:2001:4f8:fff6::21])
	by mx1.freebsd.org (Postfix) with ESMTP id 7EDF08FC12
	for <freebsd-gnats-submit@FreeBSD.org>; Wed, 22 Oct 2008 22:47:58 +0000 (UTC)
	(envelope-from nobody@FreeBSD.org)
Received: from www.freebsd.org (localhost [127.0.0.1])
	by www.freebsd.org (8.14.3/8.14.3) with ESMTP id m9MMlwbi067346
	for <freebsd-gnats-submit@FreeBSD.org>; Wed, 22 Oct 2008 22:47:58 GMT
	(envelope-from nobody@www.freebsd.org)
Received: (from nobody@localhost)
	by www.freebsd.org (8.14.3/8.14.3/Submit) id m9MMlwWX067345;
	Wed, 22 Oct 2008 22:47:58 GMT
	(envelope-from nobody)
Message-Id: <200810222247.m9MMlwWX067345@www.freebsd.org>
Date: Wed, 22 Oct 2008 22:47:58 GMT
From: Doug White <dwhite@FreeBSD.org>
To: freebsd-gnats-submit@FreeBSD.org
Subject: vn_pollrecord() derefs NULL if v_addpollinfo() fails
X-Send-Pr-Version: www-3.1
X-GNATS-Notify:

>Number:         128304
>Category:       kern
>Synopsis:       vn_pollrecord(3) derefs NULL if v_addpollinfo() fails
>Confidential:   no
>Severity:       serious
>Priority:       medium
>Responsible:    freebsd-bugs
>State:          open
>Quarter:        
>Keywords:       
>Date-Required:  
>Class:          sw-bug
>Submitter-Id:   current-users
>Arrival-Date:   Wed Oct 22 22:50:00 UTC 2008
>Closed-Date:    
>Last-Modified:  Thu Oct 23 12:09:06 UTC 2008
>Originator:     Doug White
>Release:        FreeBSD 7.0-RELEASE
>Organization:
Personal
>Environment:
FreeBSD mail1.sjc1.bitgravity.com 7.0-RELEASE-p4 FreeBSD 7.0-RELEASE-p4 #1: Mon Sep 29 18:30:06 PDT 2008     dwhite@mail1.sjc1.bitgravity.com:/usr/obj/usr/src/sys/BITGRAVITY  amd64

>Description:
The first few lines of vn_pollrecord() are:

	if (vp->v_pollinfo == NULL)
		v_addpollinfo(vp);
	mtx_lock(&vp->v_pollinfo->vpi_lock);

v_addpollinfo() leaves vp->v_pollinfo NULL if the malloc attempt fails. vfs_kqfilter() checks for failure, but this function does not.  If allocation were to fail then the mtx_lock() call would result in a null pointer dereference panic. 
>How-To-Repeat:
Problem was found by code inspection.
>Fix:
vn_pollrecord() should ensure v_addpollinfo() succeeds in allocating the pollinfo lists. However vn_pollrecord() uses its return value to convey the original event mask so there does not appear to be a method to return an error value (ENOMEM, etc.) in the current implementation.

>Release-Note:
>Audit-Trail:

From: Mateusz Guzik <mjguzik@gmail.com>
To: bug-followup@freebsd.org
Cc:  
Subject: Re: kern/128304: vn_pollrecord() derefs NULL if v_addpollinfo() fails
Date: Thu, 23 Oct 2008 13:57:43 +0200

 Hi.
 
 failure of uma_zalloc() would cause null pointer dereference in
 v_addpollinfo() anyway (due to mtx_init() call).
 
 From commit message for rev 1.142 of sys/vm/uma_core.c:
 
 Remove uma_zalloc_arg() hack, which coerced M_WAITOK to M_NOWAIT when
 allocations were made using improper flags in interrupt context.
 Replace with a simple WITNESS warning call.  This restores the
 invariant that M_WAITOK allocations will always succeed or die
 horribly trying, which is relied on by many UMA consumers.
 
 So it's ok to rely on M_WAITOK (which v_addpollinfo() does) and additional
 null check in vfs_kqfilter() can be removed as being meaningles.
 
 Thanks,
 --
 Mateusz Guzik
>Unformatted:
