Received: by 2002:a6b:500f:0:0:0:0:0 with SMTP id e15csp1017855iob; Fri, 13 May 2022 19:48:48 -0700 (PDT) X-Google-Smtp-Source: ABdhPJz31XLQMS5+eFSGT+wTTc6YAKPkDTZ8A58fhJmdT59FpFi0xqUfgT4zcrEaMOPCeStawOrZ X-Received: by 2002:a5d:640f:0:b0:20c:9be6:30c1 with SMTP id z15-20020a5d640f000000b0020c9be630c1mr5976496wru.273.1652496528309; Fri, 13 May 2022 19:48:48 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1652496528; cv=none; d=google.com; s=arc-20160816; b=dPKwFZguwGnZ+FXqRaNieVUesXJ1BeXRBKDBbN857d4EwfekbViVb3Oa78R0uv7OIq Fmlr6jh2zDxVtLzxuZI2tjsP/PFYb5890fmtAwUN5O1Y6gu43rBAWaxAcDla/kqR78ql rw7D8QnlqowikRfzYgf016I3LXdkuCb7BQzHLoFYNPnljsibjIK8OJnKbz2YFlXbvhWX lsrpXrKaCbNfTYqV3U17krmPuJTPA3ikYhjtbx3HNOjKMacicouGuYaiJlOpYYmAY8Y0 PrxXWPS04e39xX5jc3iXfY7fiojomlifJh5g5qQunPy5X/n4D7vLSJjWdJyjq6Th509F to4Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=fMHILrv3F6lh2bJVZBpE+7CmGevLUNqpRuE/33bnuqE=; b=mvIr57eR9FR4z/u6VcI+Df8glPz71ANU+lK/V3gIfXOEMkluJtAmYR+W5407h5zoTK H22muRqm7oXML5uxn9BxR/sx7g4Wb6jsHMWwuUULjWuovs+qGZmgFcDlusbL5ME/f3t/ VoM4i0Qo1KCJrV0Cmjgtq7KI/+BUuM5MGUKum9rbJeNbZP9RdCVtNdOywaFfuyzS6cy+ 0w62IpVEHOq9oRYT+a5JIkNeuFEHa+ZaIie0NJ17ox8DBd/1yaBuy7VE893nM9cS9GYt 1bQ1mmJmCXtDgzlibOJmpER27b1se6kMPEtOJRJvz6AVYhykEHxAnj8ZFpLQl3qKFJbm oM1A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=CpCn8D0Z; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net. [2620:137:e000::1:18]) by mx.google.com with ESMTPS id n9-20020a5d4009000000b0020a83f0048bsi3437229wrp.241.2022.05.13.19.48.47 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 13 May 2022 19:48:48 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) client-ip=2620:137:e000::1:18; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=CpCn8D0Z; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 7FBDA36AE34; Fri, 13 May 2022 16:35:39 -0700 (PDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1380578AbiEMO1O (ORCPT + 99 others); Fri, 13 May 2022 10:27:14 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40500 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1380750AbiEMOZn (ORCPT ); Fri, 13 May 2022 10:25:43 -0400 Received: from ams.source.kernel.org (ams.source.kernel.org [145.40.68.75]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 2525F5F259; Fri, 13 May 2022 07:25:06 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ams.source.kernel.org (Postfix) with ESMTPS id A74C0B83066; Fri, 13 May 2022 14:25:04 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id EFB38C34115; Fri, 13 May 2022 14:25:02 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1652451903; bh=c8Mvs5GwKx6JMeIIjEDEuOroyuuVw9MWMKbMQesBvPk=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=CpCn8D0Zj9F/rcdDJBmyIapeTQDAknYG2Q2Aro4DxLDAOVn3J6er9L6mdlvMyc/SK FqaxFVOI1zy/9eQeBGCz3jOLhVD/BcPYp1yTeNQSSSxV3AftT6CP97Mh9NOIJNx1WI g0WL7S8gYJq/pA0PkQHEQb2C5VkbqPRjH66ObXbM= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, David Howells , ChenXiaoSong Subject: [PATCH 4.14 14/14] VFS: Fix memory leak caused by concurrently mounting fs with subtype Date: Fri, 13 May 2022 16:23:30 +0200 Message-Id: <20220513142227.804773083@linuxfoundation.org> X-Mailer: git-send-email 2.36.1 In-Reply-To: <20220513142227.381154244@linuxfoundation.org> References: <20220513142227.381154244@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-2.6 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,RDNS_NONE,SPF_HELO_NONE,T_SCC_BODY_TEXT_LINE autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: ChenXiaoSong If two processes mount same superblock, memory leak occurs: CPU0 | CPU1 do_new_mount | do_new_mount fs_set_subtype | fs_set_subtype kstrdup | | kstrdup memrory leak | The following reproducer triggers the problem: 1. shell command: mount -t ntfs /dev/sda1 /mnt & 2. c program: mount("/dev/sda1", "/mnt", "fuseblk", 0, "...") with kmemleak report being along the lines of unreferenced object 0xffff888235f1a5c0 (size 8): comm "mount.ntfs", pid 2860, jiffies 4295757824 (age 43.423s) hex dump (first 8 bytes): 00 a5 f1 35 82 88 ff ff ...5.... backtrace: [<00000000656e30cc>] __kmalloc_track_caller+0x16e/0x430 [<000000008e591727>] kstrdup+0x3e/0x90 [<000000008430d12b>] do_mount.cold+0x7b/0xd9 [<0000000078d639cd>] ksys_mount+0xb2/0x150 [<000000006015988d>] __x64_sys_mount+0x29/0x40 [<00000000e0a7c118>] do_syscall_64+0xc1/0x1d0 [<00000000bcea7df5>] entry_SYSCALL_64_after_hwframe+0x44/0xa9 [<00000000803a4067>] 0xffffffffffffffff Linus's tree already have refactoring patchset [1], one of them can fix this bug: c30da2e981a7 ("fuse: convert to use the new mount API") After refactoring, init super_block->s_subtype in fuse_fill_super. Since we did not merge the refactoring patchset in this branch, I create this patch. This patch fix this by adding a write lock while calling fs_set_subtype. [1] https://patchwork.kernel.org/project/linux-fsdevel/patch/20190903113640.7984-3-mszeredi@redhat.com/ Fixes: 79c0b2df79eb ("add filesystem subtype support") Cc: David Howells Cc: Signed-off-by: ChenXiaoSong Signed-off-by: Greg Kroah-Hartman --- v1: Can not mount sshfs ([PATCH linux-4.19.y] VFS: Fix fuseblk memory leak caused by mount concurrency) v2: Use write lock while writing superblock ([PATCH 4.19,v2] VFS: Fix fuseblk memory leak caused by mount concurrency) v3: Update commit message fs/namespace.c | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) --- a/fs/namespace.c +++ b/fs/namespace.c @@ -2570,9 +2570,12 @@ static int do_new_mount(struct path *pat return -ENODEV; mnt = vfs_kern_mount(type, sb_flags, name, data); - if (!IS_ERR(mnt) && (type->fs_flags & FS_HAS_SUBTYPE) && - !mnt->mnt_sb->s_subtype) - mnt = fs_set_subtype(mnt, fstype); + if (!IS_ERR(mnt) && (type->fs_flags & FS_HAS_SUBTYPE)) { + down_write(&mnt->mnt_sb->s_umount); + if (!mnt->mnt_sb->s_subtype) + mnt = fs_set_subtype(mnt, fstype); + up_write(&mnt->mnt_sb->s_umount); + } put_filesystem(type); if (IS_ERR(mnt))