Received: by 2002:ad5:474a:0:0:0:0:0 with SMTP id i10csp5871868imu; Mon, 26 Nov 2018 06:19:37 -0800 (PST) X-Google-Smtp-Source: AFSGD/UxkWtu1n+Z6BGofljKYm1DhY98npr+gRYpzuOz4p5MC9YhMFz1ckP48jF2oCF3lbrRzylB X-Received: by 2002:a17:902:f082:: with SMTP id go2mr27959352plb.115.1543241977228; Mon, 26 Nov 2018 06:19:37 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1543241977; cv=none; d=google.com; s=arc-20160816; b=B0kRIH94tTx4RzjhP0wrSss1RtVWhA7f7PX/TsRkeNU9/j/jsX1DN8ZKGwZcV+QP6Z hD7Pk69EUZZJGsv2qJ4G+vV42t36jmYqRpspD61lG375oOgzcRI+9Xbg6+xWZm/c6cJE VqvOU/IVgytr4IlSscom+69zcBNsZben0vbDo69rZqlnehFMG9Cqnim9UdvZWGjUNcje fMzTRcYhrlQo3DjeIVZqjlmbGDnTlid6BKzzvXtiNTa4dQK8MVYnSAF4UhfXzs/WH/SQ IGrR2b1gpnTN2Pks4hVSkEK10QApztsZy3GY6V+YTOuu2h26dx+fASYXdMk3F9VxVCmI WQng== 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; bh=D6Q1N+yFFmIBLuIglNHMCC7yNz29eK+E2jJtzio49p0=; b=L+3oTAXf2TFibTCTtkBXNUhxUVbnPZ4AZfHSyknEiB1soCINd6KhGjMRyQpny1k6Ow PrTPLYRlM30UBaZn6GGinyC1Bt8Ogpwn2UvtAleg/+ocUn17W4liJxbj/m0ubyDoHmUc wG7k5fhwNIeLvGhf3IYKfCJDYYZF6oxFiSjb+fnwdgu4NSZOGqm4zRTbchmmAWuhA43F MeeCYgA9ecncz5U+D3vLO+KIitpI7GGZVC7hBkPz0MnA3aKvvlZZpasE7CaWwT6ZLyPQ cKZhWeYE7FFLublx2JggAPP91+07beOET2xXhJOoLt/GWZaD6TQLuYV+J07bIg6eROhE hWIQ== 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; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id u5si371601pgr.316.2018.11.26.06.18.57; Mon, 26 Nov 2018 06:19:37 -0800 (PST) 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; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726625AbeK0BMN (ORCPT + 99 others); Mon, 26 Nov 2018 20:12:13 -0500 Received: from mx1.redhat.com ([209.132.183.28]:55272 "EHLO mx1.redhat.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726200AbeK0BMM (ORCPT ); Mon, 26 Nov 2018 20:12:12 -0500 Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.phx2.redhat.com [10.5.11.23]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 15EAB37E8E; Mon, 26 Nov 2018 14:17:57 +0000 (UTC) Received: from bfoster (dhcp-41-2.bos.redhat.com [10.18.41.2]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 66A0F1A926; Mon, 26 Nov 2018 14:17:52 +0000 (UTC) Date: Mon, 26 Nov 2018 09:17:50 -0500 From: Brian Foster To: PanBian Cc: Carlos Maiolino , "Darrick J. Wong" , linux-xfs@vger.kernel.org, Dave Chinner , linux-kernel@vger.kernel.org Subject: Re: [PATCH] xfs: libxfs: move xfs_perag_put late Message-ID: <20181126141750.GA14585@bfoster> References: <1543052660-58625-1-git-send-email-bianpan2016@163.com> <20181126093139.cgojlmtubzuzdb23@hades.usersys.redhat.com> <20181126103619.GA95394@bp> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20181126103619.GA95394@bp> User-Agent: Mutt/1.10.1 (2018-07-13) X-Scanned-By: MIMEDefang 2.84 on 10.5.11.23 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.29]); Mon, 26 Nov 2018 14:17:57 +0000 (UTC) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Mon, Nov 26, 2018 at 06:36:19PM +0800, PanBian wrote: > On Mon, Nov 26, 2018 at 10:31:39AM +0100, Carlos Maiolino wrote: > > On Sat, Nov 24, 2018 at 05:44:20PM +0800, Pan Bian wrote: > > > The function xfs_alloc_get_freelist calls xfs_perag_put to drop the > > > reference. In this case, pag may be released. However, > > > pag->pagf_btreeblks is read and write after the put operation. This may > > > result in a use-after-free bug. This patch moves the put operation late. > > > > > > > The patch looks reasonable, can you detail more how did you find it? Via code > > inspection of you hit this user-after-free in some way? > > I wrote a tool to check such bugs statically. It first scans the source code > to extract paired alloc/free functions. Equipped with such functions, it > performs an intra-procedural data flow analysis to detect mismatched > alloc/free bugs and use-after-free bugs. > You should probably drop the "use after free" text from your commit log because that's not how the perag reference counting works. If you look at xfs_perag_put(), you'll see it only drops a reference count and returns. We only ever free the perag structs on unmount (or mount failure), where we assert the refcount is zero. It looks like some other serialization mechanism would have to break down for that assert to fail due to this error in the allocation code because the fs is mostly shut down at this point in time. Misleading commit log aside, the change seems fine to me. I think it's appropriate to follow the traditional/implied _get()/_put() pattern. Brian > Best regards, > Pan Bian >