Received: by 2002:a25:ab43:0:0:0:0:0 with SMTP id u61csp3832859ybi; Tue, 18 Jun 2019 07:13:38 -0700 (PDT) X-Google-Smtp-Source: APXvYqyRtAJvyWv2EloGliH3YaMe32RPR1m2LUAVMAGzF5F4+xQIUjP5sRw2jdRidafYKdqOdKNW X-Received: by 2002:a17:902:6a88:: with SMTP id n8mr22730918plk.70.1560867217998; Tue, 18 Jun 2019 07:13:37 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1560867217; cv=none; d=google.com; s=arc-20160816; b=JFyZIuzVpaHkc2ez+STz/kSRAzTTLpPKeIhTmzshhV+ByU0i/jBxIRTHS9njv1gxnE gBxkT2GmUxtLiJEUe+msCWvOqSlXmbZ/fe49US7kF6CWXR+ifUrhDMpthXokVwW2ADhX kTqDs0dBHBVONW7AtLH+1+inP8pl4xFeRvSmq+PYMdcy4hQY3j4bOdUv1XU0b/d+FEYt aUr+tncWIY2G8vDrfebrasNWDO2xmWYyaXKun2FNbeO8b4o3hfqwyBy4cy8z12YEa6mt WGMCHuUVqRWblsMgF3uDqJhkY2awoj2gPM35LXRjCxFPteYalw/3YuQCmRpwbtXCSzOJ uvlg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :references:in-reply-to:date:cc:to:from:subject:message-id; bh=MYth/3KmeIPApeWRgT77PamSH1110Uc1Bk7IBzFpEgg=; b=RId1FknUrTltjGvgQ/ewgoloTzzro0EpvomfHfu5PhaNrGrQdl47CCEhkxxsUOfT/U RdBdFJeWZcCY8mG3CVLHw6Vj5JFBqNF4zYVXFZV3JkWhSPlMd2AdFR9DAJggXzL7izkd Xl8Jcagd2nRrry4/Sx+ashFFDrCT7RnDpIOC1FmzNlNXGJhLSA4z3Myb7c0cx6fSnsUU XaeHgzHdM6qH7k7dWwiL3OCaGhoS6Tqu67z4i2Z/up5FihbTedxV+JubojdOawQ8HVJ1 snTEuAbdoAxJGiAI6E6eCcSNFgUug/zGDak0XLA0c3s/Wyb5cNWeDfWEuijEBKAAl0zx 8wlw== 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 x20si309885pgf.118.2019.06.18.07.13.22; Tue, 18 Jun 2019 07:13:37 -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 S1729267AbfFROLS (ORCPT + 99 others); Tue, 18 Jun 2019 10:11:18 -0400 Received: from gate.crashing.org ([63.228.1.57]:32992 "EHLO gate.crashing.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729743AbfFROLR (ORCPT ); Tue, 18 Jun 2019 10:11:17 -0400 Received: from localhost (localhost.localdomain [127.0.0.1]) by gate.crashing.org (8.14.1/8.14.1) with ESMTP id x5IEB0wY002489; Tue, 18 Jun 2019 09:11:01 -0500 Message-ID: <03865a8c403d3f26aab65d758daa900ab175de08.camel@kernel.crashing.org> Subject: Re: [PATCH v4] driver core: Fix use-after-free and double free on glue directory From: Benjamin Herrenschmidt To: Muchun Song , Greg KH Cc: "Rafael J. Wysocki" , Prateek Sood , Mukesh Ojha , gkohli@codeaurora.org, linux-kernel , linux-arm-msm , zhaowuyun@wingtech.com Date: Wed, 19 Jun 2019 00:11:00 +1000 In-Reply-To: References: <20190516142342.28019-1-smuchun@gmail.com> <20190524190443.GB29565@kroah.com> Content-Type: text/plain; charset="UTF-8" X-Mailer: Evolution 3.28.5-0ubuntu0.18.04.1 Mime-Version: 1.0 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Tue, 2019-06-18 at 21:40 +0800, Muchun Song wrote: > Ping guys ? I think this is worth fixing. I agree :-) My opinion hasn't changed though, the right fix isn't making guesses based on the refcount but solve the actual race which is the mutex being dropped between looking for the object existence and deciding to create it :-) Cheers, Ben. > Muchun Song 于2019年5月25日周六 下午8:15写道: > > > > > Hi greg k-h, > > > > Greg KH 于2019年5月25日周六 上午3:04写道: > > > > > > On Thu, May 16, 2019 at 10:23:42PM +0800, Muchun Song wrote: > > > > There is a race condition between removing glue directory and > > > > adding a new > > > > device under the glue directory. It can be reproduced in > > > > following test: > > > > > > > > > > > > Is this related to: > > > Subject: [PATCH v3] drivers: core: Remove glue dirs early > > > only when refcount is 1 > > > > > > ? > > > > > > If so, why is the solution so different? > > > > In the v1 patch, the solution is that remove glue dirs early only > > when > > refcount is 1. So > > the v1 patch like below: > > > > @@ -1825,7 +1825,7 @@ static void cleanup_glue_dir(struct device > > *dev, > > struct kobject *glue_dir) > > return; > > > > mutex_lock(&gdp_mutex); > > - if (!kobject_has_children(glue_dir)) > > + if (!kobject_has_children(glue_dir) && kref_read(&glue_dir- > > >kref) == 1) > > kobject_del(glue_dir); > > kobject_put(glue_dir); > > mutex_unlock(&gdp_mutex); > > ----------------------------------------------------------------- > > ------ > > > > But from Ben's suggestion as below: > > > > I find relying on the object count for such decisions rather > > fragile as > > it could be taken temporarily for other reasons, couldn't it ? In > > which > > case we would just fail... > > > > Ideally, the looking up of the glue dir and creation of its child > > should be protected by the same lock instance (the gdp_mutex in > > that > > case). > > ----------------------------------------------------------------- > > ------ > > > > So another solution is used from Ben's suggestion in the v2 patch. > > But > > I forgot to update the commit message until the v4 patch. Thanks. > > > > Yours, > > Muchun