From patchwork Wed Apr 19 22:11:00 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Cong Wang X-Patchwork-Id: 9689115 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 9887560375 for ; Wed, 19 Apr 2017 22:11:26 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 8ABA8282E2 for ; Wed, 19 Apr 2017 22:11:26 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 7F4C728433; Wed, 19 Apr 2017 22:11:26 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.5 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, FREEMAIL_FROM, RCVD_IN_DNSWL_HI, RCVD_IN_SORBS_SPAM autolearn=unavailable version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 2140428418 for ; Wed, 19 Apr 2017 22:11:25 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S936262AbdDSWLG (ORCPT ); Wed, 19 Apr 2017 18:11:06 -0400 Received: from mail-io0-f193.google.com ([209.85.223.193]:34156 "EHLO mail-io0-f193.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1765435AbdDSWLF (ORCPT ); Wed, 19 Apr 2017 18:11:05 -0400 Received: by mail-io0-f193.google.com with SMTP id h41so8099863ioi.1; Wed, 19 Apr 2017 15:11:05 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id; bh=Fk7oS7vH+0s9Drkc+cKuEwzekTQQKxb0oRuMGi+5SRk=; b=tL1pz9nPCTlg6zBnUXzQqRqEn4p417f8TAHF0D84SYilqkTB+h7nhVfaytFgh6lKXK FprruQEmME69Fg9FXT8mpgwM4U6/8j2xICcjogwXIbtb92RVJTzURRy01f2bHZootjbK UgPBVW/LEtnBIDpHwHbI/rZ/UsofI3xVdk4yw3YES3QILCW7rwAEg1Ll5cTczWKU4odx PLWR09PLj06b5qOOteqOz0x5gtT9HQjxj0h98b/bg5w1RoVCOLpGMrYBYnoNKVb7Ijln G6uVPKUzrLwu1f+AhWZKaJ9LQAWsZME3X5hJhurCZvd6Huq5rBrkTjPUWWhVaXJatXSu OFmg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id; bh=Fk7oS7vH+0s9Drkc+cKuEwzekTQQKxb0oRuMGi+5SRk=; b=ikzdmS0rgPzrotTr/TrlLdfmlrt5vapD1Uhy3iHbXqHgq4L9KWT0SUbRXsE616xF3p /ZRO9SfZdRr+Hwaaep61PO0dXnlWjZHOcmmqiB3jAV/lGvPSDWWFn95iZ4jL97rL+NpG AwcrxWxGROws84gXVJYLdUNEUI17RMQFJK19Tc8c+vKezwv06IH1TE6mIwhBwOYcICDG 7/7EzSypFhyJIyZK2eLDLSZKnw1SyjyHQYaj+mJXhoiywdfzNFeRV23bUqoH73+ZrY8I 8gR1DgHWHzywj+7sSnpWfqoMH0HmC/AMnmJlExKDGfZndrr0qtCXqY/92SIqwkOttm5R V4oQ== X-Gm-Message-State: AN3rC/6/jA84TvfxNzKSWRMSl/zh08N1DafzgZQUShCr8nSr9oElJCFp 47xixD0Fo0dFmw== X-Received: by 10.98.160.144 with SMTP id p16mr5142612pfl.236.1492639864537; Wed, 19 Apr 2017 15:11:04 -0700 (PDT) Received: from tw-172-25-29-91.office.twttr.net ([8.25.197.27]) by smtp.gmail.com with ESMTPSA id 74sm6293028pfn.102.2017.04.19.15.11.02 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 19 Apr 2017 15:11:03 -0700 (PDT) From: Cong Wang To: linux-fsdevel@vger.kernel.org Cc: linux-kernel@vger.kernel.org, andreyknvl@google.com, Cong Wang , Alexander Viro , Andrew Morton , Linus Torvalds Subject: [Patch resend] nsfs: mark dentry with DCACHE_RCUACCESS Date: Wed, 19 Apr 2017 15:11:00 -0700 Message-Id: <1492639860-19821-1-git-send-email-xiyou.wangcong@gmail.com> X-Mailer: git-send-email 2.5.5 Sender: linux-fsdevel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-fsdevel@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP Andrey reported a use-after-free in __ns_get_path(): spin_lock include/linux/spinlock.h:299 [inline] lockref_get_not_dead+0x19/0x80 lib/lockref.c:179 __ns_get_path+0x197/0x860 fs/nsfs.c:66 open_related_ns+0xda/0x200 fs/nsfs.c:143 sock_ioctl+0x39d/0x440 net/socket.c:1001 vfs_ioctl fs/ioctl.c:45 [inline] do_vfs_ioctl+0x1bf/0x1780 fs/ioctl.c:685 SYSC_ioctl fs/ioctl.c:700 [inline] SyS_ioctl+0x8f/0xc0 fs/ioctl.c:691 We are under rcu read lock protection at that point: rcu_read_lock(); d = atomic_long_read(&ns->stashed); if (!d) goto slow; dentry = (struct dentry *)d; if (!lockref_get_not_dead(&dentry->d_lockref)) goto slow; rcu_read_unlock(); but don't use a proper RCU API on the free path, therefore a parallel __d_free() could free it at the same time. We need to mark the stashed dentry with DCACHE_RCUACCESS so that __d_free() will be called after all readers leave RCU. Fixes: e149ed2b805f ("take the targets of /proc/*/ns/* symlinks to separate fs") Cc: Alexander Viro Cc: Andrew Morton Cc: Linus Torvalds Reported-by: Andrey Konovalov Signed-off-by: Cong Wang --- fs/nsfs.c | 1 + 1 file changed, 1 insertion(+) diff --git a/fs/nsfs.c b/fs/nsfs.c index 1656843..323f492 100644 --- a/fs/nsfs.c +++ b/fs/nsfs.c @@ -91,6 +91,7 @@ static void *__ns_get_path(struct path *path, struct ns_common *ns) return ERR_PTR(-ENOMEM); } d_instantiate(dentry, inode); + dentry->d_flags |= DCACHE_RCUACCESS; dentry->d_fsdata = (void *)ns->ops; d = atomic_long_cmpxchg(&ns->stashed, 0, (unsigned long)dentry); if (d) {