Received: by 2002:a25:d7c1:0:0:0:0:0 with SMTP id o184csp3720262ybg; Mon, 28 Oct 2019 18:03:58 -0700 (PDT) X-Google-Smtp-Source: APXvYqw0Qzjwzb9ME1wAeQB9hcyuJo9H6xZY6cwQ9oHoseBg7y0m8uzORQabwPCoTgLf8eMi15rm X-Received: by 2002:a50:9269:: with SMTP id j38mr22639828eda.5.1572311038233; Mon, 28 Oct 2019 18:03:58 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1572311038; cv=none; d=google.com; s=arc-20160816; b=SUbw8iGTCo6ugMveKXzY+mNojvQe9+3tXr0/dVZq1uBKqsh6KSNyE8kCQONNUoZPBB +Gzfn/6Qr3fnucHWjWDvTICkGKHThk5X+T0Hrkie/j8O/AVTn6nlJ7kx/6kkgIOPYAFZ gDslXjoyW1yzPW1VhcwyJxdZ+hFgNO/XFtJbSgFvIRkPZUP4hGD/Hhzp2LKH696mghrk ybkOva9lrIaD3V14XNeJWU2RZVqgR+k5VWbxvWUZYPA0U1DIZW2YEg5J82LOQVXy2fua oAUnG78Ggz+FlQ+/fs/O5iJO8N2VHI4rOq4DAtZGhWulKQ3b5hV1XCdHH9VvguSBhxhO PLOw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:in-reply-to:content-transfer-encoding :content-disposition:mime-version:references:message-id:subject:cc :to:from:date; bh=ZhDW57M7gceW8h+ao3JN4MqKnScv9jFABsWKUIjXbLc=; b=hZwd334TbTGrIXRS0ALpGKKRqeT2WAq73IbrKE/gH1jS2OwgJbtRQo4iPyVpmkgM0Y 8PCV23YRLmAjcqHy/E9ZDOoJcfo3c/w/OyAkwUJ4qyI+wRhSH+pCkPigD3KAKYLytz3h c7Vx+RgaDbZhOM0vZ22OsMnLZ6SNdiZbXKgZh9xAc9Avopz1hXYzbgkx48obnArqcBwT WDlZcfxWZhdXJU75DZArqLcd8KkKdcfex81lFDbRFvSq7FmZxZ6FbOw5j9i2Xw1vFbje rLYvojWoSKt+3bm9JTlI8aNFCnxgnuSjWdTidW7TqT3oMCP4mGBJ7F1xwnDLVyfV12jy 926A== 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 o57si4377324edc.240.2019.10.28.18.03.31; Mon, 28 Oct 2019 18:03:58 -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 S2387890AbfJ1JSk (ORCPT + 99 others); Mon, 28 Oct 2019 05:18:40 -0400 Received: from mx2.suse.de ([195.135.220.15]:57538 "EHLO mx1.suse.de" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1727664AbfJ1JSj (ORCPT ); Mon, 28 Oct 2019 05:18:39 -0400 X-Virus-Scanned: by amavisd-new at test-mx.suse.de Received: from relay2.suse.de (unknown [195.135.220.254]) by mx1.suse.de (Postfix) with ESMTP id 5496BB009; Mon, 28 Oct 2019 09:18:37 +0000 (UTC) Date: Mon, 28 Oct 2019 09:18:36 +0000 From: Luis Henriques To: Jeff Layton Cc: Sage Weil , Ilya Dryomov , "Yan, Zheng" , ceph-devel@vger.kernel.org, linux-kernel@vger.kernel.org Subject: Re: [PATCH v2] ceph: Fix use-after-free in __ceph_remove_cap Message-ID: <20191028091836.GA8558@hermes.olymp> References: <9c1fe73500ca7dece15c73d7534b9e0ec417c83a.camel@kernel.org> <20191025130524.31755-1-lhenriques@suse.com> <1a9ac7d3097efe53ad6f2fda4dd584204dd7eba2.camel@kernel.org> MIME-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: <1a9ac7d3097efe53ad6f2fda4dd584204dd7eba2.camel@kernel.org> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Sun, Oct 27, 2019 at 08:31:49AM -0400, Jeff Layton wrote: > On Fri, 2019-10-25 at 14:05 +0100, Luis Henriques wrote: > > KASAN reports a use-after-free when running xfstest generic/531, with the > > following trace: > > > > [ 293.903362] kasan_report+0xe/0x20 > > [ 293.903365] rb_erase+0x1f/0x790 > > [ 293.903370] __ceph_remove_cap+0x201/0x370 > > [ 293.903375] __ceph_remove_caps+0x4b/0x70 > > [ 293.903380] ceph_evict_inode+0x4e/0x360 > > [ 293.903386] evict+0x169/0x290 > > [ 293.903390] __dentry_kill+0x16f/0x250 > > [ 293.903394] dput+0x1c6/0x440 > > [ 293.903398] __fput+0x184/0x330 > > [ 293.903404] task_work_run+0xb9/0xe0 > > [ 293.903410] exit_to_usermode_loop+0xd3/0xe0 > > [ 293.903413] do_syscall_64+0x1a0/0x1c0 > > [ 293.903417] entry_SYSCALL_64_after_hwframe+0x44/0xa9 > > > > This happens because __ceph_remove_cap() may queue a cap release > > (__ceph_queue_cap_release) which can be scheduled before that cap is > > removed from the inode list with > > > > rb_erase(&cap->ci_node, &ci->i_caps); > > > > And, when this finally happens, the use-after-free will occur. > > > > This can be fixed by removing the cap from the inode list before being > > removed from the session list, and thus eliminating the risk of an UAF. > > > > Signed-off-by: Luis Henriques > > --- > > Hi! > > > > So, after spending some time trying to find possible races throught code > > review and testing, I modified the fix according to Jeff's suggestion. > > > > Cheers, > > Luis > > > > fs/ceph/caps.c | 10 +++++----- > > 1 file changed, 5 insertions(+), 5 deletions(-) > > > > diff --git a/fs/ceph/caps.c b/fs/ceph/caps.c > > index d3b9c9d5c1bd..a9ce858c37d0 100644 > > --- a/fs/ceph/caps.c > > +++ b/fs/ceph/caps.c > > @@ -1058,6 +1058,11 @@ void __ceph_remove_cap(struct ceph_cap *cap, bool queue_release) > > > > dout("__ceph_remove_cap %p from %p\n", cap, &ci->vfs_inode); > > > > + /* remove from inode list */ > > + rb_erase(&cap->ci_node, &ci->i_caps); > > + if (ci->i_auth_cap == cap) > > + ci->i_auth_cap = NULL; > > + > > /* remove from session list */ > > spin_lock(&session->s_cap_lock); > > if (session->s_cap_iterator == cap) { > > @@ -1091,11 +1096,6 @@ void __ceph_remove_cap(struct ceph_cap *cap, bool queue_release) > > > > spin_unlock(&session->s_cap_lock); > > > > - /* remove from inode list */ > > - rb_erase(&cap->ci_node, &ci->i_caps); > > - if (ci->i_auth_cap == cap) > > - ci->i_auth_cap = NULL; > > - > > if (removed) > > ceph_put_cap(mdsc, cap); > > > > Looks good. Merged with a slight modification to the comment: > > + /* remove from inode's cap rbtree, and clear auth cap */ > Awesome, thanks. Regarding CC: stable@, feel free to add that too. I guess it makes sense, even if this issue has been there for a while apparently without any reports. (Although UAFs may have non-obvious side effects, I guess...) Cheers, -- Lu?s > Thanks! > -- > Jeff Layton >