Received: by 2002:a25:4158:0:0:0:0:0 with SMTP id o85csp949004yba; Wed, 24 Apr 2019 12:16:51 -0700 (PDT) X-Google-Smtp-Source: APXvYqyHMsSFS9cBFrpXhotw9MzYxLFoShUSV2UrV3IYk+DxrtnwvQfrK6+S58w4L3zixrln8KMS X-Received: by 2002:a63:d250:: with SMTP id t16mr32331042pgi.288.1556133410967; Wed, 24 Apr 2019 12:16:50 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1556133410; cv=none; d=google.com; s=arc-20160816; b=y5aM+KHci3Yz/wQl1NRf1vf82gEl4vITW1xXdX74oCXHaI+tuSGG+X+WRoKCKDSyFe s3gOfnTAoi+XiRAp+K1K2VnxNOCcgjTn5oUavu/K3N1bbIDGkASZ5TEFHd9AjJlZnpOl IjPzSR4pknSftjomM14vTSh1WHup6Lie2wAVmzRhlVChfAo6alUQThlDl5caBwF/hcUE t5m1EJ8eb8nEmdWew9VdBPfxlp5J2QGXT4x6nGEixouXCaykV8Sfbg+HrkKj8xZazjre W9c7u6pPUmBJptmDteV6NSFtbTOcTKitjrVFKnZPSDrsQF4KVISWIlXjNSw1XsWTH+a5 Z0LQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-language :content-transfer-encoding:in-reply-to:mime-version:user-agent:date :message-id:from:references:cc:to:subject:dmarc-filter :dkim-signature:dkim-signature; bh=L0WsjQRBDaFNTRySUJmbj0DUUIKg6HMWCYhwuQOV2fY=; b=FckIAdbXUEGjl8wKpGOhZKOKKQ99gzIliJJHi/hcfW9HAOvg8fkvmFrFQXC6UdwDSV V673iBXikCiiEXb8I86HGffqAFkGrinBFDGldTfokLDuxwb+oXEH7rASx1SM9qP/tiss RWT0B+hnxshl6QJQwe6eOzC7Fppn60JLFwgTwepaRQk9F09ZZ1mWjj88/+Mv4gCs9yTg 5ue15PMDA8Jgq1y0MHYGfAGnEwn5BII/7jzYC/cDUhgTLmhvVqe4HsscRmBGHX3QDzNV q50EfKvRlRNc9aBmMzjmmDACW55xt736KrmVhqFxtXaxZObNoO5upw4q4Y1e33Xfqjkf 8CiA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@codeaurora.org header.s=default header.b=e4J+mssY; dkim=pass header.i=@codeaurora.org header.s=default header.b=e4J+mssY; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id d9si19554576pls.12.2019.04.24.12.16.35; Wed, 24 Apr 2019 12:16:50 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@codeaurora.org header.s=default header.b=e4J+mssY; dkim=pass header.i=@codeaurora.org header.s=default header.b=e4J+mssY; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728517AbfDXOJL (ORCPT + 99 others); Wed, 24 Apr 2019 10:09:11 -0400 Received: from smtp.codeaurora.org ([198.145.29.96]:56186 "EHLO smtp.codeaurora.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725893AbfDXOJK (ORCPT ); Wed, 24 Apr 2019 10:09:10 -0400 Received: by smtp.codeaurora.org (Postfix, from userid 1000) id 6BA6F6053D; Wed, 24 Apr 2019 14:09:09 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=codeaurora.org; s=default; t=1556114949; bh=MHVdlAoAhKQSKrc5bbXQ5+gLT+VeIgHuzo/MaJlw7Og=; h=Subject:To:Cc:References:From:Date:In-Reply-To:From; b=e4J+mssYhr7N0eH/yf7PC9gDFqta2jYIzUK9qxr6cObNm4QSoPFnvfbrlWKvHXGUd Ll6bdCx5jTtAFAp9Mpi0N/lgQL7X0DwPZ2qQ6PA3S2ckhwX/uPpzXBGuRyMCsrLWMj LN48X5YeGDOGNWto3QtNceNo9jE1623bsJ5u6bmk= X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on pdx-caf-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-2.7 required=2.0 tests=ALL_TRUSTED,BAYES_00, DKIM_INVALID,DKIM_SIGNED autolearn=no autolearn_force=no version=3.4.0 Received: from [10.204.79.15] (blr-c-bdr-fw-01_globalnat_allzones-outside.qualcomm.com [103.229.19.19]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) (Authenticated sender: mojha@smtp.codeaurora.org) by smtp.codeaurora.org (Postfix) with ESMTPSA id D1F20601C4; Wed, 24 Apr 2019 14:09:05 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=codeaurora.org; s=default; t=1556114949; bh=MHVdlAoAhKQSKrc5bbXQ5+gLT+VeIgHuzo/MaJlw7Og=; h=Subject:To:Cc:References:From:Date:In-Reply-To:From; b=e4J+mssYhr7N0eH/yf7PC9gDFqta2jYIzUK9qxr6cObNm4QSoPFnvfbrlWKvHXGUd Ll6bdCx5jTtAFAp9Mpi0N/lgQL7X0DwPZ2qQ6PA3S2ckhwX/uPpzXBGuRyMCsrLWMj LN48X5YeGDOGNWto3QtNceNo9jE1623bsJ5u6bmk= DMARC-Filter: OpenDMARC Filter v1.3.2 smtp.codeaurora.org D1F20601C4 Authentication-Results: pdx-caf-mail.web.codeaurora.org; dmarc=none (p=none dis=none) header.from=codeaurora.org Authentication-Results: pdx-caf-mail.web.codeaurora.org; spf=none smtp.mailfrom=mojha@codeaurora.org Subject: Re: [PATCH v2] Input: uinput: Avoid Object-Already-Free with a global lock To: Al Viro Cc: "dmitry.torokhov@gmail.com" , linux-input@vger.kernel.org, linux-kernel@vger.kernel.org, Gaurav Kohli , Peter Hutterer , Martin Kepplinger , "Paul E. McKenney" References: <20190418014321.dptin7tpxpldhsns@penguin> <20190419071152.x5ghvbybjhv76uxt@penguin> <20190423032839.xvbldglrmjxkdntj@penguin> <17f4a0be-ab04-8537-9197-32fbca807f3f@codeaurora.org> <20190423084944.gj2boxfcg7lp4zad@penguin> <20190423110611.GL2217@ZenIV.linux.org.uk> <5614f04f-827d-1668-9ed0-60d93e110b8e@codeaurora.org> <20190424130711.GP2217@ZenIV.linux.org.uk> From: Mukesh Ojha Message-ID: <5b02ac1e-df3e-d9cd-ecf3-fe60cda2cece@codeaurora.org> Date: Wed, 24 Apr 2019 19:39:03 +0530 User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:60.0) Gecko/20100101 Thunderbird/60.6.1 MIME-Version: 1.0 In-Reply-To: <20190424130711.GP2217@ZenIV.linux.org.uk> Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 8bit Content-Language: en-US Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 4/24/2019 6:37 PM, Al Viro wrote: > On Wed, Apr 24, 2019 at 05:40:40PM +0530, Mukesh Ojha wrote: >> Al, >> >> i tried to put traceprintk inside ioctl after fdget and fdput on a simple >> call of open  => ioctl => close > in a loop, and multithreaded, presumably? > >> on /dev/uinput. >> >>           uinput-532   [002] ....    45.312044: SYSC_ioctl: 2     <= f_count >>>     >           uinput-532   [002] ....    45.312055: SYSC_ioctl: 2 >> >           uinput-532   [004] ....    45.313766: uinput_open: uinput: 1 >>           uinput-532   [004] ....    45.313783: SYSC_ioctl: 1 >>           uinput-532   [004] ....    45.313788: uinput_ioctl_handler: >> uinput: uinput_ioctl_handler, 1 >>           uinput-532   [004] ....    45.313835: SYSC_ioctl: 1 >>           uinput-532   [004] ....    45.313843: uinput_release: uinput:  0 >> >> >> So while a ioctl is running the f_count is 1, so a fput could be run and do >> atomic_long_dec_and_test >> this could call release right ? > Look at ksys_ioctl(): > int ksys_ioctl(unsigned int fd, unsigned int cmd, unsigned long arg) > { > int error; > struct fd f = fdget(fd); > an error or refcount bumped > if (!f.file) > return -EBADF; > not an error, then. We know that ->release() won't be called > until we drop the reference we've just acquired. > error = security_file_ioctl(f.file, cmd, arg); > if (!error) > error = do_vfs_ioctl(f.file, fd, cmd, arg); > ... and we are done with calling ->ioctl(), so > fdput(f); > ... we drop the reference we'd acquired. > > Seeing refcount 1 inside ->ioctl() is possible, all right: > > CPU1: ioctl(2) resolves fd to struct file *, refcount 2 > CPU2: close(2) rips struct file * from descriptor table and does fput() to drop it; > refcount reaches 1 and fput() is done; no call of ->release() yet. > CPU1: we get arouund to ->ioctl(), where your trace sees refcount 1 > CPU1: done with ->ioctl(), drop our reference. *NOW* refcount gets to 0, and > ->release() is called. Thanks for the detail reply, Al This was my simple program no multithreading just to understand f_counting int main() {         int fd = open("/dev/uinput", O_WRONLY | O_NONBLOCK);         ioctl(fd, UI_SET_EVBIT, EV_KEY);         close(fd);         return 0; }            uinput-532   [002] ....    45.312044: SYSC_ioctl: 2   <= f_count >    >>>                         /* All the above calls happened for the open() in userspace*/           uinput-532   [004] ....    45.313783: SYSC_ioctl: 1 /* This print is for the trace, i put after fdget */           uinput-532   [004] ....    45.313788: uinput_ioctl_handler: uinput: uinput_ioctl_handler, 1 /* This print is from the uinput_ioctl driver */           uinput-532   [004] ....    45.313835: SYSC_ioctl: 1 /* This print is for the trace, i put after fdput*/           uinput-532   [004] ....    45.313843: uinput_release: uinput:  0 /* And this is from the close()  */ Should fdget not suppose to increment the f_count here, as it is coming 1 ? This f_count to one is done at the open, but i have no idea how this  below f_count 2 came before open() for this simple program.          uinput-532   [002] ....    45.312044: SYSC_ioctl: 2 <= f_count >    IOW, in your trace fput() has already been run by close(2); having somebody else > do that again while we are in ->ioctl() would be a bug (to start with, where > did they get that struct file * and why wasn't that reference contributing to > struct file refcount?) > > In all cases we only call ->release() once all references gone - both > the one(s) in descriptor tables and any transient ones acquired by > fdget(), etc. > > I would really like to see a reproducer for the original use-after-free report...