Received: by 2002:ac0:a594:0:0:0:0:0 with SMTP id m20-v6csp4417709imm; Mon, 14 May 2018 07:18:50 -0700 (PDT) X-Google-Smtp-Source: AB8JxZqAFM8UI/lroh9LVKI7iUx/3EdvvhrtBz5AgrxQ/XfT8mduSXwKWayKnOGqft8Ufisdv11i X-Received: by 2002:a62:3d54:: with SMTP id k81-v6mr10717791pfa.193.1526307530011; Mon, 14 May 2018 07:18:50 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1526307529; cv=none; d=google.com; s=arc-20160816; b=OkPljZyhX1Nor2mO6z2FTOhV6SU9/G3G+wmdCLJSkTuJkM5O2hU69xU4A8fVOKXK8U AgE0nsNfdSIIKVZ1+bkxTiAJka6n2S6nqPzo5Q3OgTaXJli/FeWzH/qoe3j1PNEgOl/f pSI032mxMxVCW8h6ZRaLXIB4W4i3rNbOITGPq8aAKJaSGrl8aQK1R4UTUAeJuYWRfWeP 6WN367c04UbYAxD+4/CGsj2wIEO2r0Xu7Fy0Adn/aTcUP82nc7URE49HVxlz/TNC/2bL fYsJIvyBKcnPa9IUwW6gVr+r77oLNUzm1731jRiuOXJza7h3KeOmOpiZNWuv0bClEKc8 z3pg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:user-agent:in-reply-to :content-disposition:mime-version:references:message-id:subject:cc :to:from:date:dkim-signature:arc-authentication-results; bh=lim2B71cBqVpUQqDSkRWI3T1v98lnU5qVBwEtMtCgtU=; b=iMxPQgYvF+wCpT2k3cL0Gcvqa8Wb6GZvQTl/ZAdbUVnfk4hKrF+5vhusbC+gt6PdJp V47PaMs2lulxrnJPwxT6QDI7a1jlg+MNHuWiplXDxisnNCT3xXLJMEG+4NE0/jK4xhuB co1sOBEWHs2XjKTJpL5gZcrek2HIonRVqYbnJLEJ0AcxPP00/f1np9SvEPqhLEbl7VyA YP8DStLtRBKn7Qqbm5IUhz7V8l5S90WABsTOfoLMKwPt40saIeUg7/PUbh0UZ1o+qoQx 8p6v9MR3fghRY5f4BuB27w4t7Q79jkqCkeQ409SQ63s0aZYdRPU777BxTAQHDq8EUvxe 8Kaw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=V1XhHLci; 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 a185-v6si7503743pgc.586.2018.05.14.07.18.33; Mon, 14 May 2018 07:18:49 -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=@kernel.org header.s=default header.b=V1XhHLci; 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 S1753929AbeENORU (ORCPT + 99 others); Mon, 14 May 2018 10:17:20 -0400 Received: from mail.kernel.org ([198.145.29.99]:34146 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1753841AbeENORS (ORCPT ); Mon, 14 May 2018 10:17:18 -0400 Received: from localhost (LFbn-1-12247-202.w90-92.abo.wanadoo.fr [90.92.61.202]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 8E5A920740; Mon, 14 May 2018 14:17:17 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1526307438; bh=R0lhUpuab0rMS4NPLzX/1Gy9G1j+eUCAvIO4yIUwBrI=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=V1XhHLci8aAm1z+gaurInALrE/zZyaCu/tXvVh7lOqV5v74tkXA3Yk+oC/7qUGPJd FRommNx5pQb8Mfd1xuwZlJMTrKJVHOaS5R0G6dI13tXj4Y8o+hFSKbqhp8XPDNn/Kj zwTvUPS4IbMEZ9qfMzyiXOWb+dIlGpO9cgVq6gR0= Date: Mon, 14 May 2018 16:17:01 +0200 From: Greg KH To: Hamish Martin Cc: linux-kernel@vger.kernel.org Subject: Re: [PATCH v2 0/2] uio: Prevent kernel oops on UIO device remove with open fds Message-ID: <20180514141701.GA24912@kroah.com> References: <20180514013223.4828-1-hamish.martin@alliedtelesis.co.nz> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20180514013223.4828-1-hamish.martin@alliedtelesis.co.nz> User-Agent: Mutt/1.9.5 (2018-04-13) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Mon, May 14, 2018 at 01:32:21PM +1200, Hamish Martin wrote: > If a UIO device is removed while a user space app has an open file > descriptor to that device's /dev/uio* file, a kernel oops can occur when > the file descriptor is ultimately closed. The oops is triggered by > dereferencing either the uio_listener struct's 'dev' pointer, or at the > next level, when dereferencing a stale 'info' pointer on that dev. > > To resolve this we now increment the reference count for the uio_device > and prevent the destruction of the uio_device until all open file > descriptors are closed. > A further consequence of resolving the stale pointers to the uio_device > is that it exposes an issue with the independent life cycles of the > uio_device and its related uio_info. The uio_info struct is allocated by > the user of the uio subsystem and connected to a uio_device at > registration time (see __uio_register_device()). When the device > corresponding to that uio_info is unregistered and the memory for the > uio_info is freed, the uio_device is left with a stale pointer which may > still be used in the file system ops (uio_poll(), uio_read(), etc) > To resolve this second issue, we now lock alteration or access of the > 'info' member of the uio_device struct and alter the accessing code to > handle that pointer being null. > > This patch series contains two patches. The first is a cosmetic change > to the return paths from uio_write to facilitate easier review of the > second patch. The second patch contains the change to prevent destruction > of the uio_device while file descriptors to it remain open and the > additional locking to prevent the use of a stale 'info' pointer. > > This patch series is heavily based on the work done by Brian Russell > (formerly of Brocade) in May 2015. His last version of an attempt to fix > this is seen here: > https://patchwork.kernel.org/patch/6057431/ > My new addition is the locking around use of the info pointer. It is my > proposed solution to Brian's last comment about what he had left > unfinished: > "It needs a bit more work. uio_info needs to live as long as the > corresponding uio_device. Since they seem to always be 1:1, > uio_info could embedded within uio_device (but then all the users > of uio need changed) or uio_info could be a refcounted object." > > For further info here is an example of the kernel oops this patch series > is trying to resolve. Output is from a 4.17.0-rc1 kernel with a test app > opening a uio device and doing select with the fd, then removing the UIO > device while the select is still happening: > > Unable to handle kernel paging request for data at address 0x6b6b6b6b6b6b6d63 > Faulting instruction address: 0xc000000000605c98 > Oops: Kernel access of bad area, sig: 11 [#1] > BE PREEMPT SMP NR_CPUS=8 CoreNet Generic > Modules linked in: > CPU: 6 PID: 282 Comm: uio_tester Not tainted 4.17.0-rc1-at1+ #8 > NIP: c000000000605c98 LR: c000000000211d8c CTR: c000000000605c60 > REGS: c0000000f02f3480 TRAP: 0300 Not tainted (4.17.0-rc1-at1+) > MSR: 000000008202b000 CR: 24284448 XER: 20000000 > DEAR: 6b6b6b6b6b6b6d63 ESR: 0000000000000000 SOFTE: 0 > GPR00: c000000000211d8c c0000000f02f3700 c000000000dc7d00 c0000000ef365bc0 > GPR04: c0000000f02f3800 0000000000000000 c0000000ef4b9b58 0000000000000006 > GPR08: c000000000605c60 6b6b6b6b6b6b6b6b 0000000000000000 0000000000000016 > GPR12: 0000000042284448 c00000003fffd440 0000000000000004 0000000000000003 > GPR16: 0000000000000008 0000000000000000 00000000ef365bc0 0000000000000000 > GPR20: c0000000f02f3c00 0000000000000000 0000000000000000 c0000000ef365bc0 > GPR24: 0000000000000000 0000000000000000 0000000000000000 0000000000000000 > GPR28: c0000000f02f3800 c0000000ef365bc0 c0000000f02c2c68 c0000000efd01d20 > NIP [c000000000605c98] .uio_poll+0x38/0xe0 > LR [c000000000211d8c] .do_select+0x39c/0x7a0 > Call Trace: > [c0000000f02f3700] [c0000000f02f3790] 0xc0000000f02f3790 (unreliable) > [c0000000f02f3790] [c000000000211d8c] .do_select+0x39c/0x7a0 > [c0000000f02f3b90] [c000000000212eac] .core_sys_select+0x22c/0x320 > [c0000000f02f3d70] [c000000000213098] .__se_sys_select+0xf8/0x170 > [c0000000f02f3e30] [c000000000000674] system_call+0x58/0x64 > Instruction dump: > f8010010 fba1ffe8 fbc1fff0 fbe1fff8 f821ff71 7c7d1b78 7c9c2378 60000000 > 60000000 ebdd00c0 ebfe0000 e93f0038 2fa90000 40de0030 3c60ffff > ---[ end trace 8badf75b83f45856 ]--- Very nice work, thanks for doing this. I've now queued it up. greg k-h