Received: by 2002:ac0:a5b6:0:0:0:0:0 with SMTP id m51-v6csp1658041imm; Thu, 14 Jun 2018 01:30:33 -0700 (PDT) X-Google-Smtp-Source: ADUXVKKzHT6xhejziJ4nrgviVhsQTmLlj69cF5kC+NeYS8i7qj6RN8243bFN6BdnTvhF3c020wzR X-Received: by 2002:a65:510c:: with SMTP id f12-v6mr1432050pgq.288.1528965033147; Thu, 14 Jun 2018 01:30:33 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1528965033; cv=none; d=google.com; s=arc-20160816; b=Lu3eCtEplPkvgUubvy1qG5dD0z241Qqhnx72VrA4o2bEXcOeE5vCCqiK2f7g0X5gXd gUWVD5PhHm8Bb85tAifwsiOGnnnIo1TWeY9w5m3a2uxqo8dSvSJv+cO2IMPtjHUpEJ5x wFyAuMqV2eA0knut2YRfh8d0oizvC4VLOCOh+VJl7MkpBmxGVVLnYNdHkjW0sc+3CEyO 3/AuFXp5f6uc2vdvO/agZQbyMKqACHi33iuBxjKFXGmhpo70vWDs4x4N5y+AR5Q0eIW0 ut7HQaaXLzbSczNVUJTkOLHM41SHCY0alkVPPGj8qQtjnLJvRKeq4kz83BpJQZJEUuK0 Ap+Q== 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:arc-authentication-results; bh=+H0OJjBaWdZ+LK1Lqo53Cwoak7IRli6vVYO1KW7jPF8=; b=i6Ca2lLLnTjAM48XVNTHkIriFq1H+4/IizicUrMxSM7U5plFSrN+9TR71Rm9ZEHkNC 0diz3tQ9gkTaXH/5oGP1h2ktO8Af4SVzVqYdqea/Uu7K4gQJ2edhFWkzeYtq690aCA+0 m76FE3dyji7LquZIuor+8k7RuXv2IDmtDpHnY/+ttS9rbtNc2t81LYs1gNjgQ+N/VgB+ 4WGG1pYMOqy1eSQ0l+QIZ9Y1BtstDxHzBgilJDY5hQuau9R7djc2hiPd9eeO/DeDC+ez ObNu32tizLG7jLvhvNLQETIzKH9gw164swgugvLZ+glLK75Uu1+QJqmEOFCDRgIExFzI rAhQ== ARC-Authentication-Results: i=1; mx.google.com; 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 j33-v6si4828589pld.151.2018.06.14.01.30.19; Thu, 14 Jun 2018 01:30:33 -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; 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 S1754847AbeFNI3t (ORCPT + 99 others); Thu, 14 Jun 2018 04:29:49 -0400 Received: from verein.lst.de ([213.95.11.211]:45136 "EHLO newverein.lst.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752882AbeFNI3r (ORCPT ); Thu, 14 Jun 2018 04:29:47 -0400 Received: by newverein.lst.de (Postfix, from userid 2407) id EB74668C4E; Thu, 14 Jun 2018 10:38:06 +0200 (CEST) Date: Thu, 14 Jun 2018 10:38:06 +0200 From: Christoph Hellwig To: Anatoliy Glagolev Cc: linux-block@vger.kernel.org, "James E.J. Bottomley" , FUJITA Tomonori , Jens Axboe , linux-scsi@vger.kernel.org, Christoph Hellwig , linux-kernel@vger.kernel.org Subject: Re: [PATCH] block: fix bsg_unregister and bsg_open race Message-ID: <20180614083806.GA18966@lst.de> References: <20180613221417.GA22778@xldev-tmpl.dev.purestorage.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20180613221417.GA22778@xldev-tmpl.dev.purestorage.com> User-Agent: Mutt/1.5.17 (2007-11-01) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Wed, Jun 13, 2018 at 04:14:18PM -0600, Anatoliy Glagolev wrote: > The existing implementation allows races between bsg_unregister and > bsg_open paths. bsg_ungegister and request_queue cleanup and > deletion may start and complete right after bsg_get_device (in bsg_open path) > retrieves bsg_class_device and releases the mutex. Then bsg_open path > touches freed memory of bsg_class_device and request_queue. > > One possible fix is to hold the mutex all the way through bsg_get_device > instead of releasing it after bsg_class_device retrieval. This looks generally fine to me. Nitpicks below: > @@ -746,16 +745,18 @@ static struct bsg_device *bsg_get_device(struct inode *inode, struct file *file) > */ > mutex_lock(&bsg_mutex); > bcd = idr_find(&bsg_minor_idr, iminor(inode)); > - mutex_unlock(&bsg_mutex); > > if (!bcd) > return ERR_PTR(-ENODEV); This needs to unlock the mutex. E.g. if (!bcd) { bd = ERR_PTR(-ENODEV); goto out_unlock; } > bd = __bsg_get_device(iminor(inode), bcd->queue); > + if (bd) { > + mutex_unlock(&bsg_mutex); > return bd; > + } > > bd = bsg_add_device(inode, bcd->queue, file); > + mutex_unlock(&bsg_mutex); > > return bd; I'd simply do: bd = __bsg_get_device(iminor(inode), bcd->queue); if (!bd) bd = bsg_add_device(inode, bcd->queue, file); out_unlock: mutex_unlock(&bsg_mutex); return bd;