Received: by 10.213.65.68 with SMTP id h4csp44414imn; Mon, 19 Mar 2018 19:00:57 -0700 (PDT) X-Google-Smtp-Source: AG47ELtyS3v25H5Tm5zz709MvKHHjMAR8SIuHlh3dMrntK5i2OQGIkeOKPrcIL4meOwjc3VYFFzP X-Received: by 2002:a17:902:7d08:: with SMTP id z8-v6mr2253453pll.210.1521511257780; Mon, 19 Mar 2018 19:00:57 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1521511257; cv=none; d=google.com; s=arc-20160816; b=k8Jd2iK8NfbkPqNDIo8F9BMn840T0SXY5+TABZGjjwG514oSV2mgjGYeMNwWrYBd/B pDle4iaVUO1Ye9eXKwtQj9FvISAfHGAy1nPdMl2AAYYQU0Fi1HSMdN+1rKi6cboXmS1C ETPkLhyca/+ykCuXexwD0lHSJOn6kOPvYFHOhFyKzd6qyczS52R9ip2a7bidVEX1f+bb GlS/YmT7mavk6xKHOm7caoghTqO2vyZlEduudL0jGhtJL0Qh1N8xfjTsq66R6L3UeDIL qrKmgRx2Bw9B0qBOqRb7wV4D8NU17e8IgPwQ3lID600Q6K8iRSAIzLM0tP/QiTvYwTa7 INhg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:mime-version:content-transfer-encoding :spamdiagnosticmetadata:spamdiagnosticoutput:content-language :accept-language:in-reply-to:references:message-id:date:thread-index :thread-topic:subject:cc:to:from:dkim-signature :arc-authentication-results; bh=mIJuwrjFuy00NElqImksnC9O3kkCJ6r8PR1wnoSdw/A=; b=RUFRicG+f7LsbDfFjLtsC/cIZAG+Jyxx5L/0KjqfE016WbqDIdZWM3OiBUceBI2t7e Z8ciGOWWddPmtodITA2waACn1fn8iARnGczmLyGjGyqPB5MfZsLcoyeDSJajUCoIiUQT ZxDXRMN7OJc6pNujeUVuPVLeXtdwWT4wGaqyVUxfBevtmySlNvkblyvRiifZrcJdiqt2 8dob1owJC1zEaB0LBk/TeUibvFmtnb/eXO+xtQL22vKudjxag2vMufSUGXW9cIlqThs4 +yuI7ATEr5Z/72K0r2qAP2IJwkEPyhVIBqpQSmF5iP4wTjXK7LlZ1BGhBZDpkMv42IMt mqXQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@microsoft.com header.s=selector1 header.b=F26pXLH1; 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; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=microsoft.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id ay10-v6si616750plb.258.2018.03.19.19.00.43; Mon, 19 Mar 2018 19:00:57 -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; dkim=pass header.i=@microsoft.com header.s=selector1 header.b=F26pXLH1; 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; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=microsoft.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1032696AbeCSWjP (ORCPT + 99 others); Mon, 19 Mar 2018 18:39:15 -0400 Received: from mail-co1nam03on0122.outbound.protection.outlook.com ([104.47.40.122]:59200 "EHLO NAM03-CO1-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S933327AbeCSPr2 (ORCPT ); Mon, 19 Mar 2018 11:47:28 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version; bh=mIJuwrjFuy00NElqImksnC9O3kkCJ6r8PR1wnoSdw/A=; b=F26pXLH1zVN93wmiJ6nAcF8X4dZK6/p0uBzOFqjKCBiAFEfUDtjTUQt0CbpOkvWmeGB+E2jMzHmPh5kVh6LFHHUYfeQnjKte951PlmlTfpLvLMvbperW4aQJrBTYblUHNEPxJtbnrlzC6+mTwkmvTb1AXVE+hyOYbQV4FzDwKc0= Received: from DM5PR2101MB1032.namprd21.prod.outlook.com (52.132.128.13) by DM5PR2101MB0998.namprd21.prod.outlook.com (52.132.133.32) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384_P256) id 15.20.631.0; Mon, 19 Mar 2018 15:47:26 +0000 Received: from DM5PR2101MB1032.namprd21.prod.outlook.com ([fe80::3d9b:79e7:94eb:5d62]) by DM5PR2101MB1032.namprd21.prod.outlook.com ([fe80::3d9b:79e7:94eb:5d62%5]) with mapi id 15.20.0631.004; Mon, 19 Mar 2018 15:47:26 +0000 From: Sasha Levin To: "linux-kernel@vger.kernel.org" , "stable@vger.kernel.org" CC: NeilBrown , Al Viro , Sasha Levin Subject: [PATCH AUTOSEL for 4.15 021/124] VFS: close race between getcwd() and d_move() Thread-Topic: [PATCH AUTOSEL for 4.15 021/124] VFS: close race between getcwd() and d_move() Thread-Index: AQHTv5mQmSW1GNUcFEax9DZp3kcERQ== Date: Mon, 19 Mar 2018 15:47:20 +0000 Message-ID: <20180319154645.11350-21-alexander.levin@microsoft.com> References: <20180319154645.11350-1-alexander.levin@microsoft.com> In-Reply-To: <20180319154645.11350-1-alexander.levin@microsoft.com> Accept-Language: en-US Content-Language: en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: x-originating-ip: [52.168.54.252] x-ms-publictraffictype: Email x-microsoft-exchange-diagnostics: 1;DM5PR2101MB0998;7:Vv/O+Rpegaa7wB1svYzaSzjrdp33bAtb7GN/M75QFMzVuzT59LzVu2wlu2djxMkia5uk50bTs9grpTDJH/S03liTM7rp0lmfnmbPHn/+0SBH/+ONxROdo3oXm5ZJkgZNlWOO1cuIbr0iAHMZ3fLqcFGNsiykmRDUEN48YZ0wezTBWN5tR0UGjXJTvqe0JfdQBjcXmRgaK6X0xlBLgUIS9Htngj1ZyEjIHXCEH8i3WWMroJ8V6x39J0s4+4sDdR3L;20:e4MfnvHM4SCkODx53hNujlvGpFz9XzkRqh38kPgXakrydh79N0guBo5/1eixSbFvRPDiBX7XTerNu1uSHNQYVlz7ORv1AYR8Vg1UGBUE+UiYKl4NKRprsvg4cGhsprWWBDi0BA7fJeOGOhT+9YeLpepwbHXaQeKJBJ/T1GqmSEY= x-ms-office365-filtering-ht: Tenant x-ms-office365-filtering-correlation-id: 49cbd878-b24b-4bc9-afc1-08d58db0b6ed x-microsoft-antispam: UriScan:;BCL:0;PCL:0;RULEID:(7020095)(4652020)(48565401081)(5600026)(4604075)(3008032)(4534165)(4627221)(201703031133081)(201702281549075)(2017052603328)(7193020);SRVR:DM5PR2101MB0998; x-ms-traffictypediagnostic: DM5PR2101MB0998: authentication-results: spf=none (sender IP is ) smtp.mailfrom=Alexander.Levin@microsoft.com; x-microsoft-antispam-prvs: x-exchange-antispam-report-test: UriScan:(28532068793085)(20558992708506)(89211679590171)(228905959029699); x-exchange-antispam-report-cfa-test: BCL:0;PCL:0;RULEID:(8211001083)(61425038)(6040522)(2401047)(8121501046)(5005006)(93006095)(93001095)(10201501046)(3231221)(944501300)(52105095)(3002001)(6055026)(61426038)(61427038)(6041310)(201703131423095)(201702281528075)(20161123555045)(201703061421075)(201703061406153)(20161123564045)(20161123562045)(20161123558120)(20161123560045)(6072148)(201708071742011);SRVR:DM5PR2101MB0998;BCL:0;PCL:0;RULEID:;SRVR:DM5PR2101MB0998; x-forefront-prvs: 06167FAD59 x-forefront-antispam-report: SFV:NSPM;SFS:(10019020)(39380400002)(39860400002)(366004)(376002)(396003)(346002)(199004)(189003)(3846002)(6116002)(966005)(8936002)(6486002)(102836004)(68736007)(6506007)(2900100001)(22452003)(25786009)(3660700001)(7736002)(76176011)(6512007)(72206003)(86362001)(53936002)(8676002)(81156014)(6436002)(81166006)(305945005)(6306002)(478600001)(99286004)(2950100002)(14454004)(6666003)(10290500003)(186003)(107886003)(2501003)(106356001)(26005)(86612001)(97736004)(5660300001)(10090500001)(110136005)(54906003)(316002)(2906002)(66066001)(105586002)(4326008)(36756003)(3280700002)(1076002)(5250100002)(59450400001)(22906009)(217873001);DIR:OUT;SFP:1102;SCL:1;SRVR:DM5PR2101MB0998;H:DM5PR2101MB1032.namprd21.prod.outlook.com;FPR:;SPF:None;LANG:en;PTR:InfoNoRecords;A:1;MX:1; received-spf: None (protection.outlook.com: microsoft.com does not designate permitted sender hosts) x-microsoft-antispam-message-info: /8eyMea0azY8MLfPA0E6vjSit6jWhjv1Tbm5JKjqCU3+yMdk39cZKEC9knYo7yhev+0/TeD0ISY/U5avQwdlaX0ZEZaUvkkd5iGJIYvdPb8WbA2pkLw2ocP+PZAR5iaZNKZeYU4AccJdetxDspNZW2pEhodnz3KSAFJx19pmwP2NPZyvdJvjSEqgHAIJF9MZDyQpIB64wchYuoeVnbMHYPCigXb3+XOAWAF8ONQV+Nf3w82UY/du1mRYG1bebNigy8lQISnaWThEeri5610nYFgmWusBnKfSi3HvV/NHpauYhPMoczRYd80+aJO1eLCeE6GJyNW/mFRzDVL7EEv86A== spamdiagnosticoutput: 1:99 spamdiagnosticmetadata: NSPM Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 X-OriginatorOrg: microsoft.com X-MS-Exchange-CrossTenant-Network-Message-Id: 49cbd878-b24b-4bc9-afc1-08d58db0b6ed X-MS-Exchange-CrossTenant-originalarrivaltime: 19 Mar 2018 15:47:20.5482 (UTC) X-MS-Exchange-CrossTenant-fromentityheader: Hosted X-MS-Exchange-CrossTenant-id: 72f988bf-86f1-41af-91ab-2d7cd011db47 X-MS-Exchange-Transport-CrossTenantHeadersStamped: DM5PR2101MB0998 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: NeilBrown [ Upstream commit 61647823aa920e395afcce4b57c32afb51456cab ] d_move() will call __d_drop() and then __d_rehash() on the dentry being moved. This creates a small window when the dentry appears to be unhashed. Many tests of d_unhashed() are made under ->d_lock and so are safe from racing with this window, but some aren't. In particular, getcwd() calls d_unlinked() (which calls d_unhashed()) without d_lock protection, so it can race. This races has been seen in practice with lustre, which uses d_move() as part of name lookup. See: https://jira.hpdd.intel.com/browse/LU-9735 It could race with a regular rename(), and result in ENOENT instead of either the 'before' or 'after' name. The race can be demonstrated with a simple program which has two threads, one renaming a directory back and forth while another calls getcwd() within that directory: it should never fail, but does. See: https://patchwork.kernel.org/patch/9455345/ We could fix this race by taking d_lock and rechecking when d_unhashed() reports true. Alternately when can remove the window, which is the approach this patch takes. ___d_drop() is introduce which does *not* clear d_hash.pprev so the dentry still appears to be hashed. __d_drop() calls ___d_drop(), then clears d_hash.pprev. __d_move() now uses ___d_drop() and only clears d_hash.pprev when not rehashing. Signed-off-by: NeilBrown Signed-off-by: Al Viro Signed-off-by: Sasha Levin --- fs/dcache.c | 23 ++++++++++++++++------- 1 file changed, 16 insertions(+), 7 deletions(-) diff --git a/fs/dcache.c b/fs/dcache.c index 5c7df1df81ff..10b47d16a8a4 100644 --- a/fs/dcache.c +++ b/fs/dcache.c @@ -468,9 +468,11 @@ static void dentry_lru_add(struct dentry *dentry) * d_drop() is used mainly for stuff that wants to invalidate a dentry for= some * reason (NFS timeouts or autofs deletes). * - * __d_drop requires dentry->d_lock. + * __d_drop requires dentry->d_lock + * ___d_drop doesn't mark dentry as "unhashed" + * (dentry->d_hash.pprev will be LIST_POISON2, not NULL). */ -void __d_drop(struct dentry *dentry) +static void ___d_drop(struct dentry *dentry) { if (!d_unhashed(dentry)) { struct hlist_bl_head *b; @@ -486,12 +488,17 @@ void __d_drop(struct dentry *dentry) =20 hlist_bl_lock(b); __hlist_bl_del(&dentry->d_hash); - dentry->d_hash.pprev =3D NULL; hlist_bl_unlock(b); /* After this call, in-progress rcu-walk path lookup will fail. */ write_seqcount_invalidate(&dentry->d_seq); } } + +void __d_drop(struct dentry *dentry) +{ + ___d_drop(dentry); + dentry->d_hash.pprev =3D NULL; +} EXPORT_SYMBOL(__d_drop); =20 void d_drop(struct dentry *dentry) @@ -2381,7 +2388,7 @@ EXPORT_SYMBOL(d_delete); static void __d_rehash(struct dentry *entry) { struct hlist_bl_head *b =3D d_hash(entry->d_name.hash); - BUG_ON(!d_unhashed(entry)); + hlist_bl_lock(b); hlist_bl_add_head_rcu(&entry->d_hash, b); hlist_bl_unlock(b); @@ -2816,9 +2823,9 @@ static void __d_move(struct dentry *dentry, struct de= ntry *target, write_seqcount_begin_nested(&target->d_seq, DENTRY_D_LOCK_NESTED); =20 /* unhash both */ - /* __d_drop does write_seqcount_barrier, but they're OK to nest. */ - __d_drop(dentry); - __d_drop(target); + /* ___d_drop does write_seqcount_barrier, but they're OK to nest. */ + ___d_drop(dentry); + ___d_drop(target); =20 /* Switch the names.. */ if (exchange) @@ -2830,6 +2837,8 @@ static void __d_move(struct dentry *dentry, struct de= ntry *target, __d_rehash(dentry); if (exchange) __d_rehash(target); + else + target->d_hash.pprev =3D NULL; =20 /* ... and switch them in the tree */ if (IS_ROOT(dentry)) { --=20 2.14.1