Received: by 2002:a05:6a10:2726:0:0:0:0 with SMTP id ib38csp4842601pxb; Mon, 28 Mar 2022 04:26:30 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzXznY8tFODWXSACPAg+FU6Z/QaJC9WGzetpcjOdHG1Jqw1INitDt1sqCAIIOAzPF1oufuI X-Received: by 2002:a17:907:86a3:b0:6da:870c:af44 with SMTP id qa35-20020a17090786a300b006da870caf44mr26834917ejc.445.1648466790048; Mon, 28 Mar 2022 04:26:30 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1648466790; cv=none; d=google.com; s=arc-20160816; b=em3qqIq2QSS03XKjSoxrwgV063eWcHZVEQ6tjM0SXPCWJrMWLoW1TifO6+8VOPbsXV LHNjGsPEb+mDMxaZ/Jbu8VFPBT+bRa9FLeLPIsYb0yecVcJNYyjQXGoF4mbxPqEsHbF5 uf0Ie1gY0bf/KvextDJr8QAPJYDC14+1CANjXVc79HVgDwL8/f3vxdZPTSpApBO14kAw rlCRUvbBrX8odad6T/z9dTHXoodnpgvTAmEHWEEYkfnb5m3xpsWKhYeORevQzGG/tsKS JDHDfsxC4yj3Zhwh8LON/CKEChhLOoe/QBB+a1USs3gEPdbDI1oEsgwDJtxC7wAEL7j/ SeRg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:message-id:date:subject:cc:to:from :dkim-signature; bh=CTu5Ckkhnxr3a4Bdessxh982+g7W1LXJx6RoEy52ElQ=; b=lh0abV9NwTDiBCHr9nhWapcB+B3iXkRuwhbAMLGhEo43egQxS483h2ihYn1Pr6ew2F KgtMXvmtKcD3SihCl6CR/URiOZii4mDHqH+hElauyJUTcWptk3LCc9lamg7V/C3GuoN9 eYQ6a6DTcCsHGe0I9/sbkB6HUIEktDaqotvl4JX0d+DjPp/i3Q7thDSP7+1UGAWBMET7 CyGpXA0s00YH+4EkbDlx7zNJzUPBySBxUBFnNw2UBR1ZU0GrlPtAWuKavjqG76zChgvT 4JkMn0uz8bF3qA1CMrO+fTOlHvnOh86CoJce2ssq6X6DCJ06Dn3w8l4LycGvtSIUMiym WoEA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=fRhAkGn6; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id i10-20020a05640200ca00b00418c2b5bf58si12510432edu.570.2022.03.28.04.26.03; Mon, 28 Mar 2022 04:26:30 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=fRhAkGn6; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S239133AbiC1IHu (ORCPT + 99 others); Mon, 28 Mar 2022 04:07:50 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43424 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234283AbiC1IHs (ORCPT ); Mon, 28 Mar 2022 04:07:48 -0400 Received: from mail-pf1-x434.google.com (mail-pf1-x434.google.com [IPv6:2607:f8b0:4864:20::434]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 91D5852E58; Mon, 28 Mar 2022 01:06:08 -0700 (PDT) Received: by mail-pf1-x434.google.com with SMTP id y10so8549422pfa.7; Mon, 28 Mar 2022 01:06:08 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id; bh=CTu5Ckkhnxr3a4Bdessxh982+g7W1LXJx6RoEy52ElQ=; b=fRhAkGn6eq8e9jJGRgqMzcIePzE3ErVy/ps/BdX/QZVWA2+9hgjm2fLRJEzMKvRJZP l8KjfVUSxfq5ifGWAUHGK0UYYtqXluuAtxfLZa+1JaTdNToJx0b6+IbhlwMa3G+UjQxX a3j+sCMAlPCZ7Ml5tAK5jHjl0ANQC7Q2wltz2LD3Eg+ACrryArMJGMZuovzfu39nzDxj T0XBwUJKWj0DbcK0WD00kLb1QFO/VvKqxEyUzlYNrrrc2QZCPLbxgw+M+erJcJ85bBa4 EzxRkfuW9iqBv0EoAm1trzFn7QYF2K4QFnbW3J50ZJZhNki+66ivxsLNSDXpX/0xWtgb oc9Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id; bh=CTu5Ckkhnxr3a4Bdessxh982+g7W1LXJx6RoEy52ElQ=; b=Pf0MjjwzJfBaUkzx+1JhbSEo1BBP16Lpoq8BLA5wH+AvAvEBU1DCS4omIthHKVC7iA R+9o1KYIX/mN2WpdtK3SB9vHKJ0Z8hEefaxW0NnJoAPj7VVxYR3DaxKow2xYDbuThqrD +huXAd+dOjMUT7Bot7Yl466Difwj26gRdhx7RLG2LB97RoI4RKkhJyAQkcC1J3DSWUnK DR9lmkRRV68OipoDMfH38dRDiQIgxxsFCSEywpzoJQx/ORDjkxDvnlfffCpXnXy+FN2W UyHuSCyBy+m4lAVXfslijmrC5GYk+wSmP2JSASVg5gLC3Fgv9jv7eJcuhOnZieDbjM23 lO1A== X-Gm-Message-State: AOAM532LRpqugl3YleS9dlfoY6tGIoaDHW5SSe3k/QRTZEnQFn/oKT+y rMVPGRLNZOSZoffulncNN7s= X-Received: by 2002:a63:2004:0:b0:375:ed63:ab4c with SMTP id g4-20020a632004000000b00375ed63ab4cmr9405531pgg.255.1648454768060; Mon, 28 Mar 2022 01:06:08 -0700 (PDT) Received: from ubuntu.huawei.com ([119.3.119.18]) by smtp.googlemail.com with ESMTPSA id j14-20020a056a00174e00b004f66ce6367bsm16752080pfc.147.2022.03.28.01.06.04 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 28 Mar 2022 01:06:07 -0700 (PDT) From: Xiaomeng Tong To: song@kernel.org Cc: rgoldwyn@suse.com, guoqing.jiang@linux.dev, linux-raid@vger.kernel.org, linux-kernel@vger.kernel.org, Xiaomeng Tong , stable@vger.kernel.org Subject: [PATCH] md: fix an incorrect NULL check in md_reload_sb Date: Mon, 28 Mar 2022 16:05:59 +0800 Message-Id: <20220328080559.25984-1-xiam0nd.tong@gmail.com> X-Mailer: git-send-email 2.17.1 X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM, RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham 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 The bug is here: if (!rdev || rdev->desc_nr != nr) { The list iterator value 'rdev' will *always* be set and non-NULL by rdev_for_each_rcu(), so it is incorrect to assume that the iterator value will be NULL if the list is empty or no element found (In fact, it will be a bogus pointer to an invalid struct object containing the HEAD). Otherwise it will bypass the check and lead to invalid memory access passing the check. To fix the bug, use a new variable 'iter' as the list iterator, while using the original variable 'pdev' as a dedicated pointer to point to the found element. Cc: stable@vger.kernel.org Fixes: 70bcecdb1534 ("amd-cluster: Improve md_reload_sb to be less error prone") Signed-off-by: Xiaomeng Tong --- changes from v1: - rephrase the subject (Guoqing Jiang) v1:https://lore.kernel.org/lkml/20220327080111.12028-1-xiam0nd.tong@gmail.com/ --- drivers/md/md.c | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/drivers/md/md.c b/drivers/md/md.c index 7476fc204172..f156678c08bc 100644 --- a/drivers/md/md.c +++ b/drivers/md/md.c @@ -9794,16 +9794,18 @@ static int read_rdev(struct mddev *mddev, struct md_rdev *rdev) void md_reload_sb(struct mddev *mddev, int nr) { - struct md_rdev *rdev; + struct md_rdev *rdev = NULL, *iter; int err; /* Find the rdev */ - rdev_for_each_rcu(rdev, mddev) { - if (rdev->desc_nr == nr) + rdev_for_each_rcu(iter, mddev) { + if (iter->desc_nr == nr) { + rdev = iter; break; + } } - if (!rdev || rdev->desc_nr != nr) { + if (!rdev) { pr_warn("%s: %d Could not find rdev with nr %d\n", __func__, __LINE__, nr); return; } -- 2.17.1