Received: by 2002:ab2:60d1:0:b0:1f7:5705:b850 with SMTP id i17csp1520778lqm; Thu, 2 May 2024 19:09:26 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCXXecFCSHgjw7dQa1dFYT16UxcRPvAHksst5QV4c+oaDQX7+VK+m8eDeFEL84F5CV65mf/gw3L4vqam68UKkJIuljUjW/a3j2b2BRinHA== X-Google-Smtp-Source: AGHT+IF3iy97w3nwwpk4zh9ClNtb8k+NBocNcw4BZ4SS+huUbMJXHTxD9v841wuyBEy8OIoexHHU X-Received: by 2002:a2e:3c0b:0:b0:2e1:cb0f:4e1e with SMTP id j11-20020a2e3c0b000000b002e1cb0f4e1emr1064602lja.2.1714702166093; Thu, 02 May 2024 19:09:26 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1714702166; cv=pass; d=google.com; s=arc-20160816; b=JTzZLFfYzaEDGNP4hxv+jwmGO4L32GCvQFbKQmLU/A5syu2QWmiaileL/fPT+nyf/o /eK+h6hGnryDdP0ZDqWemkZlf1BrzwVocy7N5B1l4Ol4mdVFvDOuq6cvzCvC3hTNTOqG Z93a7DxwVN62Ox9hphSbedhOQLLrKKgrNfajkQL5boJ3cMwg9Rue7hKUPF9mHlAY9uuP +Io/SLM2I1+NPNXL9z6e91rZN++RBv5BMrl24mBprVAbPM43uveJXM7kxxpSh/puQYO7 UNhbzUEG9yHont2y/UI52FrmYyvXLKT75yxn4oC4ydYk5qkmK6j7zQEzVRqu9ekbUaQC UO8w== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:in-reply-to:from:references:cc:to :content-language:subject:user-agent:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:date:message-id; bh=3qQBeIeR0P7VnG2bNrXss6j0rkPpKORGSyesxzU0yuM=; fh=3rSq0UbWvn9vapu8tiZxrDWd+f+GiIxDoxp8i4BZKU0=; b=jI48WVROiQ22xhfGM2JRonF1dkLavU28tnWvYpIDBS6lrDvof3JNt3ku7fMCWT2L9a S3dKOGvbZqLS2h1jeg9trS1+JaZhUe26g1HAhxuJlt9f2+m+KC7MIp+FaIzVBLS1tKwM UVMtKpR6/AF4GQfq1hjL5lpG+mshihJsHOtOHqTdzKEpeIkjud1vTE23OTG7aNS8oxvj cHnIlMbdn9SGRhvJbEE27V3shp026ZESpJ5Sj28QkW0kVEaop19D73H5+uiC7P2+OH1Q MGiSGjf4tGGAAIB+XvxGFPxEzdP6CAZLXEAq/8cwe89sj8OLQ/hxDq9HpSvWNhqWKPv9 XRlw==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; arc=pass (i=1 spf=pass spfdomain=huaweicloud.com); spf=pass (google.com: domain of linux-ext4+bounces-2270-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-ext4+bounces-2270-linux.lists.archive=gmail.com@vger.kernel.org" Return-Path: Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [147.75.80.249]) by mx.google.com with ESMTPS id bl21-20020a056402211500b0056e6a7b3ad4si1081105edb.129.2024.05.02.19.09.26 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 02 May 2024 19:09:26 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-ext4+bounces-2270-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) client-ip=147.75.80.249; Authentication-Results: mx.google.com; arc=pass (i=1 spf=pass spfdomain=huaweicloud.com); spf=pass (google.com: domain of linux-ext4+bounces-2270-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-ext4+bounces-2270-linux.lists.archive=gmail.com@vger.kernel.org" Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by am.mirrors.kernel.org (Postfix) with ESMTPS id A44C51F239F3 for ; Fri, 3 May 2024 02:09:25 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 7835714A85; Fri, 3 May 2024 02:09:22 +0000 (UTC) X-Original-To: linux-ext4@vger.kernel.org Received: from dggsgout12.his.huawei.com (dggsgout12.his.huawei.com [45.249.212.56]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id BEA3D134A9; Fri, 3 May 2024 02:09:19 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=45.249.212.56 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714702162; cv=none; b=U7TkN2Pfi3FfZHsQvsCq7CNgk9DAm/Ql41qElNOPO26rB51X18ylcKgzSdrFo6kRgBMwVmR7EBb4SAtO34pUgGKgFBibbp4QqSf/jpo/LDKf1LthZUd+Klih6KYTvUwIQkGr1+f4MrojO1AxkxOqdhtVkZEa6vOlapsTr8A6Nzo= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714702162; c=relaxed/simple; bh=npw/t6DUs7VRxXxX33ZJq9vhUD6Uee9GtuDDHixl3kY=; h=Message-ID:Date:MIME-Version:Subject:To:Cc:References:From: In-Reply-To:Content-Type; b=ssMQLKaDaCfkex8mZqvu4UGFsCfRuzLHSNHKVnkDAqq/rIMsKBaGlY3e6OL1g8TiNvw70AjNKYDTkCN2mkIrv+LNT0hvL6ssA076VRnqorjxe3siFjWq0qDZCei0k0isKuQ8XIro70rEMu6ONXNiLg62mHTAYIKpYf9IKjJOcuA= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=huaweicloud.com; spf=pass smtp.mailfrom=huaweicloud.com; arc=none smtp.client-ip=45.249.212.56 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=huaweicloud.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=huaweicloud.com Received: from mail.maildlp.com (unknown [172.19.93.142]) by dggsgout12.his.huawei.com (SkyGuard) with ESMTP id 4VVvPB3y0lz4f3jd0; Fri, 3 May 2024 10:09:02 +0800 (CST) Received: from mail02.huawei.com (unknown [10.116.40.112]) by mail.maildlp.com (Postfix) with ESMTP id 87D521A0175; Fri, 3 May 2024 10:09:10 +0800 (CST) Received: from [10.174.177.174] (unknown [10.174.177.174]) by APP1 (Coremail) with SMTP id cCh0CgDHlxBDRzRmnSNtLg--.12952S3; Fri, 03 May 2024 10:09:10 +0800 (CST) Message-ID: Date: Fri, 3 May 2024 10:09:07 +0800 Precedence: bulk X-Mailing-List: linux-ext4@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:102.0) Gecko/20100101 Thunderbird/102.1.2 Subject: Re: [PATCH] ext4: fix uninitialized ratelimit_state->lock access in __ext4_fill_super() Content-Language: en-US To: linux-ext4@vger.kernel.org, tytso@mit.edu Cc: adilger.kernel@dilger.ca, jack@suse.cz, ritesh.list@gmail.com, linux-kernel@vger.kernel.org, yangerkun@huawei.com, Baokun Li References: <20240102133730.1098120-1-libaokun1@huawei.com> From: Baokun Li In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit X-CM-TRANSID:cCh0CgDHlxBDRzRmnSNtLg--.12952S3 X-Coremail-Antispam: 1UD129KBjvJXoWxtr1ruFy5Jr4rtFykZFy5urg_yoW7tFW5pr n7JFyxJryUu34kJw4UJF1UJFyUJw1UG3WDJr18XF1UJrWUXr12qr4UXF10gF1UCr48Ar1U Jr1UJrnrZw1UJrJanT9S1TB71UUUUUUqnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDU0xBIdaVrnRJUUUk0b4IE77IF4wAFF20E14v26r4j6ryUM7CY07I20VC2zVCF04k2 6cxKx2IYs7xG6r1S6rWUM7CIcVAFz4kK6r1j6r18M28lY4IEw2IIxxk0rwA2F7IY1VAKz4 vEj48ve4kI8wA2z4x0Y4vE2Ix0cI8IcVAFwI0_Ar0_tr1l84ACjcxK6xIIjxv20xvEc7Cj xVAFwI0_Gr1j6F4UJwA2z4x0Y4vEx4A2jsIE14v26rxl6s0DM28EF7xvwVC2z280aVCY1x 0267AKxVW0oVCq3wAS0I0E0xvYzxvE52x082IY62kv0487Mc02F40EFcxC0VAKzVAqx4xG 6I80ewAv7VC0I7IYx2IY67AKxVWUGVWUXwAv7VC2z280aVAFwI0_Jr0_Gr1lOx8S6xCaFV Cjc4AY6r1j6r4UM4x0Y48IcVAKI48JMxk0xIA0c2IEe2xFo4CEbIxvr21l42xK82IYc2Ij 64vIr41l4I8I3I0E4IkC6x0Yz7v_Jr0_Gr1lx2IqxVAqx4xG67AKxVWUJVWUGwC20s026x 8GjcxK67AKxVWUGVWUWwC2zVAF1VAY17CE14v26r1q6r43MIIYrxkI7VAKI48JMIIF0xvE 2Ix0cI8IcVAFwI0_Jr0_JF4lIxAIcVC0I7IYx2IY6xkF7I0E14v26r4j6F4UMIIF0xvE42 xK8VAvwI8IcIk0rVWrJr0_WFyUJwCI42IY6I8E87Iv67AKxVWUJVW8JwCI42IY6I8E87Iv 6xkF7I0E14v26r4j6r4UJbIYCTnIWIevJa73UjIFyTuYvjxUOyCJDUUUU X-CM-SenderInfo: 5olet0hnxqqx5xdzvxpfor3voofrz/ Hi Ted, Would you consider merging in this patch in the current merge window? This patch has been merged in our internal branch for a while now and so far it is safe. Regards, Baokun On 2024/4/23 9:41, Baokun Li wrote: > Gently ping again. > > On 2024/1/2 21:37, Baokun Li wrote: >> In the following concurrency we will access the uninitialized rs->lock: >> >> ext4_fill_super >>    ext4_register_sysfs >>     // sysfs registered msg_ratelimit_interval_ms >>                               // Other processes modify rs->interval to >>                               // non-zero via msg_ratelimit_interval_ms >>    ext4_orphan_cleanup >>      ext4_msg(sb, KERN_INFO, "Errors on filesystem, " >>        __ext4_msg >> ___ratelimit(&(EXT4_SB(sb)->s_msg_ratelimit_state) >>            if (!rs->interval)  // do nothing if interval is 0 >>              return 1; >>            raw_spin_trylock_irqsave(&rs->lock, flags) >>              raw_spin_trylock(lock) >>                _raw_spin_trylock >>                  __raw_spin_trylock >>                    spin_acquire(&lock->dep_map, 0, 1, _RET_IP_) >>                      lock_acquire >>                        __lock_acquire >>                          register_lock_class >>                            assign_lock_key >>                              dump_stack(); >>    ratelimit_state_init(&sbi->s_msg_ratelimit_state, 5 * HZ, 10); >>      raw_spin_lock_init(&rs->lock); >>      // init rs->lock here >> >> and get the following dump_stack: >> >> ========================================================= >> INFO: trying to register non-static key. >> The code is fine but needs lockdep annotation, or maybe >> you didn't initialize this object before use? >> turning off the locking correctness validator. >> CPU: 12 PID: 753 Comm: mount Tainted: G E 6.7.0-rc6-next-20231222 #504 >> [...] >> Call Trace: >>   dump_stack_lvl+0xc5/0x170 >>   dump_stack+0x18/0x30 >>   register_lock_class+0x740/0x7c0 >>   __lock_acquire+0x69/0x13a0 >>   lock_acquire+0x120/0x450 >>   _raw_spin_trylock+0x98/0xd0 >>   ___ratelimit+0xf6/0x220 >>   __ext4_msg+0x7f/0x160 [ext4] >>   ext4_orphan_cleanup+0x665/0x740 [ext4] >>   __ext4_fill_super+0x21ea/0x2b10 [ext4] >>   ext4_fill_super+0x14d/0x360 [ext4] >> [...] >> ========================================================= >> >> Normally interval is 0 until s_msg_ratelimit_state is initialized, so >> ___ratelimit() does nothing. But registering sysfs precedes initializing >> rs->lock, so it is possible to change rs->interval to a non-zero value >> via the msg_ratelimit_interval_ms interface of sysfs while rs->lock is >> uninitialized, and then a call to ext4_msg triggers the problem by >> accessing an uninitialized rs->lock. Therefore register sysfs after all >> initializations are complete to avoid such problems. >> >> Signed-off-by: Baokun Li >> --- >>   fs/ext4/super.c | 22 ++++++++++------------ >>   1 file changed, 10 insertions(+), 12 deletions(-) >> >> diff --git a/fs/ext4/super.c b/fs/ext4/super.c >> index 0980845c8b8f..1db23b0e8a4f 100644 >> --- a/fs/ext4/super.c >> +++ b/fs/ext4/super.c >> @@ -5564,19 +5564,15 @@ static int __ext4_fill_super(struct >> fs_context *fc, struct super_block *sb) >>       if (err) >>           goto failed_mount6; >>   -    err = ext4_register_sysfs(sb); >> -    if (err) >> -        goto failed_mount7; >> - >>       err = ext4_init_orphan_info(sb); >>       if (err) >> -        goto failed_mount8; >> +        goto failed_mount7; >>   #ifdef CONFIG_QUOTA >>       /* Enable quota usage during mount. */ >>       if (ext4_has_feature_quota(sb) && !sb_rdonly(sb)) { >>           err = ext4_enable_quotas(sb); >>           if (err) >> -            goto failed_mount9; >> +            goto failed_mount8; >>       } >>   #endif  /* CONFIG_QUOTA */ >>   @@ -5602,7 +5598,7 @@ static int __ext4_fill_super(struct >> fs_context *fc, struct super_block *sb) >>           ext4_msg(sb, KERN_INFO, "recovery complete"); >>           err = ext4_mark_recovery_complete(sb, es); >>           if (err) >> -            goto failed_mount10; >> +            goto failed_mount9; >>       } >>         if (test_opt(sb, DISCARD) && >> !bdev_max_discard_sectors(sb->s_bdev)) >> @@ -5619,15 +5615,17 @@ static int __ext4_fill_super(struct >> fs_context *fc, struct super_block *sb) >>       atomic_set(&sbi->s_warning_count, 0); >>       atomic_set(&sbi->s_msg_count, 0); >>   +    /* Register sysfs after all initializations are complete. */ >> +    err = ext4_register_sysfs(sb); >> +    if (err) >> +        goto failed_mount9; >> + >>       return 0; >>   -failed_mount10: >> +failed_mount9: >>       ext4_quotas_off(sb, EXT4_MAXQUOTAS); >> -failed_mount9: __maybe_unused >> +failed_mount8: __maybe_unused >>       ext4_release_orphan_info(sb); >> -failed_mount8: >> -    ext4_unregister_sysfs(sb); >> -    kobject_put(&sbi->s_kobj); >>   failed_mount7: >>       ext4_unregister_li_request(sb); >>   failed_mount6: >