Received: by 2002:a25:d7c1:0:0:0:0:0 with SMTP id o184csp1152529ybg; Fri, 18 Oct 2019 12:54:12 -0700 (PDT) X-Google-Smtp-Source: APXvYqx8lWqXLLRibvp/wjnDN82Sj/RBEx6KggW1XcInsI9t85uXlejD5u2pebt9iHWE6/N2C73X X-Received: by 2002:a17:906:524b:: with SMTP id y11mr10134538ejm.82.1571428452211; Fri, 18 Oct 2019 12:54:12 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1571428452; cv=none; d=google.com; s=arc-20160816; b=abfru85io+Vh0U8cSewEVrFb0t16bPkyVexxdWrLXw0qAU1lJexLVPPkjWXLAmQPaA j4/fnaRh470QHLjGq9Ezgh15XrkznU1sq/aqnVRyD5ASRtwOdelpY4zAyyKnu2WktiMB x6zQVeWCVP740poR68U72yT0znOEEE84SucE7/WMGsKPP4VBrE4Lx8ZV1G7bGgRvVTB4 2HGYjXSdu6KdTIjFLORRviksn2qi+1XuPIdR2nsPP/m+Q4iMk76y7xlQe8cY97Bt6oSe xjneteZKnKQSdAlbASKwNZib6AIzeEio2/P8VRSWIk1r0SWzm1tGK3I9XB8g9dhQjC38 qevw== 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 :message-id:date:subject:cc:to:from; bh=XDZpyPJATQeZPREVOytow//7DKe62j0L4PuwOpeIvMg=; b=myCV5eLVONH7tNN2OJPgbQP9jncCD2nBLLZ5nRWZJ/OR3eiOXB9ja4rBhlgX7wYIEy rTR00VFQ/JSlnr3fIicb36xuCVme95EWy44pxt9Yu59AmUFDZd3FnHnt0zT1LgRTiW59 7NJAy6FmRqI3jXJX4PLL5pEfmGwIPrN0q6PE1Uhrh1MvXJIJsvFk5DnOtsxa8fEYxLBk SewFXhQ3GN0v8C3k+qFzA5mF7V3qkwr/1jr8vQgFE343IkZO6NsNW3N53IudH2mLsu/m xFpSAeHeBduQQ5jxUHOp+UD0+LILAm6RBzMoccSZ2wV9cxfIgZ6zP8BUenkIY1+oOtDK z9hA== 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 g90si4932332edd.329.2019.10.18.12.53.49; Fri, 18 Oct 2019 12:54:12 -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 S2440338AbfJQOqm (ORCPT + 99 others); Thu, 17 Oct 2019 10:46:42 -0400 Received: from mx2.suse.de ([195.135.220.15]:38618 "EHLO mx1.suse.de" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S2403882AbfJQOqm (ORCPT ); Thu, 17 Oct 2019 10:46:42 -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 66C19B6AD; Thu, 17 Oct 2019 14:46:38 +0000 (UTC) From: Luis Henriques To: Jeff Layton , Sage Weil , Ilya Dryomov Cc: ceph-devel@vger.kernel.org, linux-kernel@vger.kernel.org, Luis Henriques Subject: [PATCH] ceph: Fix use-after-free in __ceph_remove_cap Date: Thu, 17 Oct 2019 15:46:36 +0100 Message-Id: <20191017144636.28617-1-lhenriques@suse.com> 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 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);