From owner-svn-src-all@freebsd.org Wed Dec 2 20:45:58 2015 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 1D870A3F95B; Wed, 2 Dec 2015 20:45:58 +0000 (UTC) (envelope-from hps@selasky.org) Received: from mail.turbocat.net (mail.turbocat.net [IPv6:2a01:4f8:d16:4514::2]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client did not present a certificate) by mx1.freebsd.org (Postfix) with ESMTPS id D84B71F7A; Wed, 2 Dec 2015 20:45:57 +0000 (UTC) (envelope-from hps@selasky.org) Received: from laptop015.home.selasky.org (unknown [62.141.129.119]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.turbocat.net (Postfix) with ESMTPSA id 9597F1FE023; Wed, 2 Dec 2015 21:45:54 +0100 (CET) Subject: Re: svn commit: r291481 - head/sys/compat/linuxkpi/common/include/linux To: Mateusz Guzik References: <201511300924.tAU9OC7o049788@repo.freebsd.org> <20151202202958.GA30250@dft-labs.eu> Cc: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org From: Hans Petter Selasky Message-ID: <565F58EF.1030707@selasky.org> Date: Wed, 2 Dec 2015 21:47:43 +0100 User-Agent: Mozilla/5.0 (X11; FreeBSD amd64; rv:38.0) Gecko/20100101 Thunderbird/38.2.0 MIME-Version: 1.0 In-Reply-To: <20151202202958.GA30250@dft-labs.eu> Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit 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: Wed, 02 Dec 2015 20:45:58 -0000 On 12/02/15 21:29, Mateusz Guzik wrote: > On Mon, Nov 30, 2015 at 09:24:12AM +0000, Hans Petter Selasky wrote: >> Author: hselasky >> Date: Mon Nov 30 09:24:12 2015 >> New Revision: 291481 >> URL: https://svnweb.freebsd.org/changeset/base/291481 >> >> Log: >> Add more functions and types to the LinuxKPI. >> >> MFC after: 1 week >> Sponsored by: Mellanox Technologies >> >> Modified: >> head/sys/compat/linuxkpi/common/include/linux/file.h >> head/sys/compat/linuxkpi/common/include/linux/workqueue.h >> >> Modified: head/sys/compat/linuxkpi/common/include/linux/file.h >> ============================================================================== >> --- head/sys/compat/linuxkpi/common/include/linux/file.h Mon Nov 30 09:13:04 2015 (r291480) >> +++ head/sys/compat/linuxkpi/common/include/linux/file.h Mon Nov 30 09:24:12 2015 (r291481) >> @@ -2,7 +2,7 @@ >> * Copyright (c) 2010 Isilon Systems, Inc. >> * Copyright (c) 2010 iX Systems, Inc. >> * Copyright (c) 2010 Panasas, Inc. >> - * Copyright (c) 2013 Mellanox Technologies, Ltd. >> + * Copyright (c) 2013-2015 Mellanox Technologies, Ltd. >> * All rights reserved. >> * >> * Redistribution and use in source and binary forms, with or without >> @@ -125,6 +125,21 @@ get_unused_fd(void) >> return fd; >> } >> >> +static inline int >> +get_unused_fd_flags(int flags) >> +{ >> + struct file *file; >> + int error; >> + int fd; >> + >> + error = falloc(curthread, &file, &fd, flags); >> + if (error) >> + return -error; >> + /* drop the extra reference */ >> + fdrop(file, curthread); >> + return fd; >> +} >> + > > This does not look right. > > AFAIR Linux drivers are not going to install fds into kernel threads. So > this would be used for a userspace thread, but then it would completely > insecure. > > Linux model is to reserve a slot in the fd table, obtain a 'file' object > and install it as the last step. > > FreeBSD installs the file right away, but this means an extra reference > has to be held in case something else using the table closes the fd. > > As such, this fdrop can lead to a use-after-free as the file can be > freed from this poin. > > I'm afraid there is no way around patching improted consumers. > Hi Mateusz, Thanks for your input. Yes, there is a potential race there, but no use-after-free from what I can see, because the LinuxKPI always retrieve the file pointer by the file number using "fget_unlocked()". I'll look into if we can delay the fdrop() until after the fd_install(). --HPS