Received: by 2002:a25:d7c1:0:0:0:0:0 with SMTP id o184csp4360467ybg; Mon, 21 Oct 2019 07:52:01 -0700 (PDT) X-Google-Smtp-Source: APXvYqzoEGeavUIblDGh0F0490gPPHrfyT5M+3oC2uQe71roajcjWnkhg8VVfxLrGVHmAXfLp6bJ X-Received: by 2002:a17:906:d964:: with SMTP id rp4mr21849402ejb.147.1571669520916; Mon, 21 Oct 2019 07:52:00 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1571669520; cv=none; d=google.com; s=arc-20160816; b=Is9rTi1+JkA9J1Zs4Csnb0URjBNhOGUaPi3G4m5GRA4chqDTLEP+2x9ARZc9k75bX2 q83CMb+wN7eZF/HtX/JJVhG/4ixxo4KF1HXsUlyO+gK03j6k+h8o9T9UA10rN/zAkX7a ihiQVWwhRsmkwMYCnlGGezzpQcTZgOtFHm7Yl/tMAcapPcMftJlFWE5mQUGsr8TiD+E1 WMg+sikC3N6Tm4RlEEJqNnVgF1MYhjkbYjPlH5wXzHlYsARYyPtGlkvwUYraL6rkp8Qd 3c6YGarzkfs5G0ZF3Bl54NHom5F7EZmUUO+eedl7GzqKPuK/tcFQ9YTUrcCCgTXEQELp /bXQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:mime-version:message-id:date:in-reply-to :subject:cc:to:from:references; bh=dugJwj+7lC2rM//LKwe47yyQtLxwnIDBtZp0B1pJXJ0=; b=HMOmZpvKJXRa1UJd+GTRpj6dudIqsuGeWeWdbYWZXC24JLFraPj2ggPlmN3wiIudwn 5kaAi8dwqZlJP6DdSz8ncctmoHnWmQEZ8mj5I57WFIu5foV6TOdwY30qkoqLV1F6/MeL 7j1YyuUUHzT1rSu+bykQuktdLO/L4K0XcA4c168c76afUKyanc1t+IM0aKNEIuvPB38y ezRY4OxDBuFKZVJoswDKjJyKgeHZdPuYzqC2buIgOIst1mjL09uNqQGXYojd4o0JS0p4 mNYyu4MQdbnqljUJIxfQQqHlw7+x6ZssmtozhpHPfitTbfACEpdeS2lFy3q12kKutYau KLWw== 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 f19si10221925edb.385.2019.10.21.07.51.37; Mon, 21 Oct 2019 07:52:00 -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 S1728089AbfJUOva (ORCPT + 99 others); Mon, 21 Oct 2019 10:51:30 -0400 Received: from mx2.suse.de ([195.135.220.15]:48498 "EHLO mx1.suse.de" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1727275AbfJUOv3 (ORCPT ); Mon, 21 Oct 2019 10:51:29 -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 EC1D3AB91; Mon, 21 Oct 2019 14:51:27 +0000 (UTC) References: <20191017144636.28617-1-lhenriques@suse.com> From: Luis Henriques To: Jeff Layton Cc: Sage Weil , "Ilya Dryomov" , ceph-devel@vger.kernel.org, linux-kernel@vger.kernel.org Subject: Re: [PATCH] ceph: Fix use-after-free in __ceph_remove_cap In-reply-to: Date: Mon, 21 Oct 2019 15:51:27 +0100 Message-ID: <87a79unocw.fsf@suse.com> MIME-Version: 1.0 Content-Type: text/plain Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Jeff Layton writes: > On Thu, 2019-10-17 at 15:46 +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 protecting the rb_erase with the s_cap_lock spinlock, >> which is used by ceph_send_cap_releases(), before the cap is freed. >> >> Signed-off-by: Luis Henriques >> --- >> fs/ceph/caps.c | 4 ++-- >> 1 file changed, 2 insertions(+), 2 deletions(-) >> >> diff --git a/fs/ceph/caps.c b/fs/ceph/caps.c >> index d3b9c9d5c1bd..21ee38cabe98 100644 >> --- a/fs/ceph/caps.c >> +++ b/fs/ceph/caps.c >> @@ -1089,13 +1089,13 @@ void __ceph_remove_cap(struct ceph_cap *cap, bool queue_release) >> } >> cap->cap_ino = ci->i_vino.ino; >> >> - 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; >> >> + spin_unlock(&session->s_cap_lock); >> + >> if (removed) >> ceph_put_cap(mdsc, cap); >> > > Is there any reason we need to wait until this point to remove it from > the rbtree? ISTM that we ought to just do that at the beginning of the > function, before we take the s_cap_lock. That sounds good to me, at least at a first glace. I spent some time looking for any possible issues in the code, and even run a few tests. However, looking at git log I found commit f818a73674c5 ("ceph: fix cap removal races"), which moved that rb_erase from the beginning of the function to it's current position. So, unless the race mentioned in this commit has disappeared in the meantime (which is possible, this commit is from 2010!), this rbtree operation shouldn't be changed. And I now wonder if my patch isn't introducing a race too... __ceph_remove_cap() is supposed to always be called with the session mutex held, except for the ceph_evict_inode() path. Which is where I'm seeing the UAF. So, maybe what's missing here is the s_mutex. Hmm... Cheers, -- Luis