Received: by 2002:ac0:a5b6:0:0:0:0:0 with SMTP id m51-v6csp2233943imm; Mon, 28 May 2018 04:27:55 -0700 (PDT) X-Google-Smtp-Source: AB8JxZoCVxl6BMpyqwVAcxav7LGF6Ua+WmjYvPpSfztACq2mtGgD1PCVKC1tJ4RKP0B8jtHLliTF X-Received: by 2002:a17:902:24e:: with SMTP id 72-v6mr13018832plc.87.1527506875139; Mon, 28 May 2018 04:27:55 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1527506875; cv=none; d=google.com; s=arc-20160816; b=XCAB/8O5abE997OyvS1ovl5Sa4K2rYFkMA23u8QoBIAz3imxluovrK6V/zeJ6DJV5V Vgmlb0OritHT7ds+boyFPUcIV6kXMqvXeRUYL9pbQp+///bJZ++hVZPC+uyCtO5U+mLY HYQqT3eziswO05/JkhhXXYnuIdfqW5eO7kqjAlFW0DoD1kk6mY/Ty4bYqVmQmV3+2MGN EunARF6WZhf7ulhGNG4YPaikgow0MO1YYWoUo/p/ZcMsoh2GvXa56Ch6zCzQheyDAKvo 0mCrWdZIezDW2jxIEEfvvzvQa8ifc0KoWEtkOLREsXcnP0Go7ojpzPET725xMRkirqJ6 EFsg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:mime-version:user-agent:references :in-reply-to:message-id:date:subject:cc:to:from:dkim-signature :arc-authentication-results; bh=/T1lWo/ckcv34ejkK6IUQU8NcsGpSUkMbWNeBpsioNU=; b=iTjYcIxyNdF00rK1fZSRvuvUnms+X7NrKvvcLYXpg59YuLKnYsx9XG3KwT/DNVOnTS L56rAFDZ7CRBTMuJBITfIo76amZIaGnvWNFzr9etkgEa514MorKWg/NVScOMVBRTmocm KWhguTu5vXGsZU5ZIvEx1Y2w+UK7R5dlA62E+0od+Zsy+6p8BiSRwg1trRMWC++wxLep Al0dmVKu5yHqtEBmHknFWpjhpXq+8gxE/fgsbDDzV0YEtKZNb5ongMgwQ3XHUnUKba9j HFOySrrZz9eM8wZFgs36l8AwsGH+p783OtgtR3Szh1rHCpym8aRFi8l8NaYkhIO76Rzn GYKQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=Z5t4Pzq6; 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 q12-v6si33643374pli.419.2018.05.28.04.27.40; Mon, 28 May 2018 04:27:55 -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=@kernel.org header.s=default header.b=Z5t4Pzq6; 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 S938213AbeE1L0u (ORCPT + 99 others); Mon, 28 May 2018 07:26:50 -0400 Received: from mail.kernel.org ([198.145.29.99]:41052 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S932490AbeE1LZW (ORCPT ); Mon, 28 May 2018 07:25:22 -0400 Received: from localhost (LFbn-1-12247-202.w90-92.abo.wanadoo.fr [90.92.61.202]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id D854F20883; Mon, 28 May 2018 11:25:21 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1527506722; bh=9OaOdxCzOuQ3OztzclGdqC4t8wq4TV2Hrd4674XVjVg=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Z5t4Pzq6S9b7licJDqJnHVMOdoJPalRsIDl7vzNoQc3+EXUcZGReQhBXzyRXOESkq PSMtPiG2bFT+8DCpnZ8GxVGtUP8TbcY1Yppg+CAY/GNEbXcPKMmfGf/GCxP96DfCjc O7IP+yOLk6uzOsZUCG4YsXXFEIWAvOF9TWjyKSyg= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Dave Chinner , Al Viro Subject: [PATCH 4.14 006/496] fs: dont scan the inode cache before SB_BORN is set Date: Mon, 28 May 2018 11:56:31 +0200 Message-Id: <20180528100319.812996465@linuxfoundation.org> X-Mailer: git-send-email 2.17.0 In-Reply-To: <20180528100319.498712256@linuxfoundation.org> References: <20180528100319.498712256@linuxfoundation.org> User-Agent: quilt/0.65 X-stable: review MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 4.14-stable review patch. If anyone has any objections, please let me know. ------------------ From: Dave Chinner commit 79f546a696bff2590169fb5684e23d65f4d9f591 upstream. We recently had an oops reported on a 4.14 kernel in xfs_reclaim_inodes_count() where sb->s_fs_info pointed to garbage and so the m_perag_tree lookup walked into lala land. It produces an oops down this path during the failed mount: radix_tree_gang_lookup_tag+0xc4/0x130 xfs_perag_get_tag+0x37/0xf0 xfs_reclaim_inodes_count+0x32/0x40 xfs_fs_nr_cached_objects+0x11/0x20 super_cache_count+0x35/0xc0 shrink_slab.part.66+0xb1/0x370 shrink_node+0x7e/0x1a0 try_to_free_pages+0x199/0x470 __alloc_pages_slowpath+0x3a1/0xd20 __alloc_pages_nodemask+0x1c3/0x200 cache_grow_begin+0x20b/0x2e0 fallback_alloc+0x160/0x200 kmem_cache_alloc+0x111/0x4e0 The problem is that the superblock shrinker is running before the filesystem structures it depends on have been fully set up. i.e. the shrinker is registered in sget(), before ->fill_super() has been called, and the shrinker can call into the filesystem before fill_super() does it's setup work. Essentially we are exposed to both use-after-free and use-before-initialisation bugs here. To fix this, add a check for the SB_BORN flag in super_cache_count. In general, this flag is not set until ->fs_mount() completes successfully, so we know that it is set after the filesystem setup has completed. This matches the trylock_super() behaviour which will not let super_cache_scan() run if SB_BORN is not set, and hence will not allow the superblock shrinker from entering the filesystem while it is being set up or after it has failed setup and is being torn down. Cc: stable@kernel.org Signed-Off-By: Dave Chinner Signed-off-by: Al Viro Signed-off-by: Greg Kroah-Hartman --- fs/super.c | 30 ++++++++++++++++++++++++------ 1 file changed, 24 insertions(+), 6 deletions(-) --- a/fs/super.c +++ b/fs/super.c @@ -120,13 +120,23 @@ static unsigned long super_cache_count(s sb = container_of(shrink, struct super_block, s_shrink); /* - * Don't call trylock_super as it is a potential - * scalability bottleneck. The counts could get updated - * between super_cache_count and super_cache_scan anyway. - * Call to super_cache_count with shrinker_rwsem held - * ensures the safety of call to list_lru_shrink_count() and - * s_op->nr_cached_objects(). + * We don't call trylock_super() here as it is a scalability bottleneck, + * so we're exposed to partial setup state. The shrinker rwsem does not + * protect filesystem operations backing list_lru_shrink_count() or + * s_op->nr_cached_objects(). Counts can change between + * super_cache_count and super_cache_scan, so we really don't need locks + * here. + * + * However, if we are currently mounting the superblock, the underlying + * filesystem might be in a state of partial construction and hence it + * is dangerous to access it. trylock_super() uses a SB_BORN check to + * avoid this situation, so do the same here. The memory barrier is + * matched with the one in mount_fs() as we don't hold locks here. */ + if (!(sb->s_flags & SB_BORN)) + return 0; + smp_rmb(); + if (sb->s_op && sb->s_op->nr_cached_objects) total_objects = sb->s_op->nr_cached_objects(sb, sc); @@ -1232,6 +1242,14 @@ mount_fs(struct file_system_type *type, sb = root->d_sb; BUG_ON(!sb); WARN_ON(!sb->s_bdi); + + /* + * Write barrier is for super_cache_count(). We place it before setting + * SB_BORN as the data dependency between the two functions is the + * superblock structure contents that we just set up, not the SB_BORN + * flag. + */ + smp_wmb(); sb->s_flags |= SB_BORN; error = security_sb_kern_mount(sb, flags, secdata);