Received: by 2002:ad5:474a:0:0:0:0:0 with SMTP id i10csp762827imu; Thu, 3 Jan 2019 06:44:36 -0800 (PST) X-Google-Smtp-Source: ALg8bN4HXAzI7K2+MSU6BM37N/nMzC4San4xMzXjDO1NhhXDPqEJQaqi3srmapsChWQS1yU8MtCC X-Received: by 2002:a17:902:b943:: with SMTP id h3mr47840023pls.12.1546526676560; Thu, 03 Jan 2019 06:44:36 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1546526676; cv=none; d=google.com; s=arc-20160816; b=sTQx4kYA9LWJuzkaSntApd1gJVQy6HyiHEwroFbdhGU1BrVrXD6AUyuLcCU6c0TePW 3BIbl7pMJjgfGRJuaW3lf/gxJGMW8oyVOR1pehCZUc9PMOR5HdSWJ5Xw2qYn5P/xkvVV QqE7Kyd9MXvkbCblc0oNLqhERR0tR5GtuJKMP2jZoJcY5Unt8bI5Bsr+V7L22dmka+sD 8f/WYkJg9xL2hbs4UsowvwaI5lVq5Dwl49Eu7Gp2j/PXQ/AI1JMwBxiydr9wP2x6UfGh wnMkLuX3WrOtdk2JQvSbhfxHXUlKXcRxjr05/oTCPMPhhDi+8O4hDDDp25fr+4LXvxFT 19Rw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:message-id:date:subject:cc:to:from :dmarc-filter:dkim-signature:dkim-signature; bh=z4p0IoKgtqUalNCQG0eK6LWiPQIht6OPwuahbtzNuFQ=; b=Smb3/042U/Fr5w9eetkRS5QRqrHA8Ziyb+mDg7bR3GFVjVePHZmHZuN9P0izzBblnu 1aFtua9vCL/zksGlEmaBKGOIkerDfbe8KCz+cv1d3AJRY53FcHFboN69pLeblivSCnu0 J2UAtwMKYZiRUjZBSV3eMbUjXNIOVKLRccYe48+0OY+0kBGsQOG19n7HOcjLCR8g0Ok5 OhNqlDLvZabcOi8+ON7f7MoZhdt+ruRuF4RqdNE9WuMzAMu5OYTrBSXGK++uuxnGGQDJ Z7QcBRAW0IL6gP8/f5+rfgHI7doh36KM9hDUXwT8ClPaImdqRmgwqUqYOthd46YRbZQt tFZQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@codeaurora.org header.s=default header.b=XWAzDK4Y; dkim=pass header.i=@codeaurora.org header.s=default header.b=XWAzDK4Y; 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 x24si30417036plr.379.2019.01.03.06.44.21; Thu, 03 Jan 2019 06:44:36 -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; dkim=pass header.i=@codeaurora.org header.s=default header.b=XWAzDK4Y; dkim=pass header.i=@codeaurora.org header.s=default header.b=XWAzDK4Y; 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 S1731095AbfACLSZ (ORCPT + 99 others); Thu, 3 Jan 2019 06:18:25 -0500 Received: from smtp.codeaurora.org ([198.145.29.96]:60142 "EHLO smtp.codeaurora.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728592AbfACLSZ (ORCPT ); Thu, 3 Jan 2019 06:18:25 -0500 Received: by smtp.codeaurora.org (Postfix, from userid 1000) id 84D2960709; Thu, 3 Jan 2019 11:18:24 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=codeaurora.org; s=default; t=1546514304; bh=aJfnIiQNSaw1dbo7wmmrt7VZ8Ql1xrSUme4F5Nz2bGE=; h=From:To:Cc:Subject:Date:From; b=XWAzDK4YvUWbiJHMy+POGsZBqFuCFziKi93rM6O2Pv6yHgai28Bm2fnD5yz8so+es T24zHQiZQd/N+A0mxMnn1HKjMHY9eqzOAPKJOmKXWT5nkMGJGRzwgh0nlBO4/Gb6ep 55fSjC60QAbIN+PqpeLeFJJNdFgu+wceT0Fpqxtg= X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on pdx-caf-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-2.7 required=2.0 tests=ALL_TRUSTED,BAYES_00, DKIM_INVALID,DKIM_SIGNED autolearn=no autolearn_force=no version=3.4.0 Received: from codeaurora.org (blr-c-bdr-fw-01_globalnat_allzones-outside.qualcomm.com [103.229.19.19]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-SHA256 (128/128 bits)) (No client certificate requested) (Authenticated sender: stummala@smtp.codeaurora.org) by smtp.codeaurora.org (Postfix) with ESMTPSA id 04E1360364; Thu, 3 Jan 2019 11:18:21 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=codeaurora.org; s=default; t=1546514304; bh=aJfnIiQNSaw1dbo7wmmrt7VZ8Ql1xrSUme4F5Nz2bGE=; h=From:To:Cc:Subject:Date:From; b=XWAzDK4YvUWbiJHMy+POGsZBqFuCFziKi93rM6O2Pv6yHgai28Bm2fnD5yz8so+es T24zHQiZQd/N+A0mxMnn1HKjMHY9eqzOAPKJOmKXWT5nkMGJGRzwgh0nlBO4/Gb6ep 55fSjC60QAbIN+PqpeLeFJJNdFgu+wceT0Fpqxtg= DMARC-Filter: OpenDMARC Filter v1.3.2 smtp.codeaurora.org 04E1360364 Authentication-Results: pdx-caf-mail.web.codeaurora.org; dmarc=none (p=none dis=none) header.from=codeaurora.org Authentication-Results: pdx-caf-mail.web.codeaurora.org; spf=none smtp.mailfrom=stummala@codeaurora.org From: Sahitya Tummala To: Junxiao Bi , Joel Becker , Christoph Hellwig , Al Viro , linux-kernel@vger.kernel.org Cc: Sahitya Tummala Subject: [PATCH v2] configfs: Fix use-after-free when accessing sd->s_dentry Date: Thu, 3 Jan 2019 16:48:15 +0530 Message-Id: <1546514295-24818-1-git-send-email-stummala@codeaurora.org> X-Mailer: git-send-email 1.9.1 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org In the vfs_statx() context, during path lookup, the dentry gets added to sd->s_dentry via configfs_attach_attr(). In the end, vfs_statx() kills the dentry by calling path_put(), which invokes configfs_d_iput(). Ideally, this dentry must be removed from sd->s_dentry but it doesn't if the sd->s_count >= 3. As a result, sd->s_dentry is holding reference to a stale dentry pointer whose memory is already freed up. This results in use-after-free issue, when this stale sd->s_dentry is accessed later in configfs_readdir() path. This issue can be easily reproduced, by running the LTP test case - sh fs_racer_file_list.sh /config (https://github.com/linux-test-project/ltp/blob/master/testcases/kernel/fs/racer/fs_racer_file_list.sh) Fixes: 76ae281f6307 ('configfs: fix race between dentry put and lookup') Signed-off-by: Sahitya Tummala --- v2: - update comments relevant to the code. fs/configfs/dir.c | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/fs/configfs/dir.c b/fs/configfs/dir.c index 39843fa..f113101 100644 --- a/fs/configfs/dir.c +++ b/fs/configfs/dir.c @@ -58,15 +58,14 @@ static void configfs_d_iput(struct dentry * dentry, if (sd) { /* Coordinate with configfs_readdir */ spin_lock(&configfs_dirent_lock); - /* Coordinate with configfs_attach_attr where will increase - * sd->s_count and update sd->s_dentry to new allocated one. - * Only set sd->dentry to null when this dentry is the only - * sd owner. + /* + * Set sd->s_dentry to null only when this dentry is the + * one that is going to be killed. * If not do so, configfs_d_iput may run just after * configfs_attach_attr and set sd->s_dentry to null * even it's still in use. */ - if (atomic_read(&sd->s_count) <= 2) + if (sd->s_dentry == dentry) sd->s_dentry = NULL; spin_unlock(&configfs_dirent_lock); -- Qualcomm India Private Limited, on behalf of Qualcomm Innovation Center, Inc. Qualcomm Innovation Center, Inc. is a member of Code Aurora Forum, a Linux Foundation Collaborative Project.