Received: by 2002:a05:6a10:1d13:0:0:0:0 with SMTP id pp19csp2890688pxb; Tue, 24 Aug 2021 09:58:50 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwxN1x5tlVCCiATr7n0/c2ebPxxaQYOYkBK2FBN6nBnSSLElFqZSjmLVLcIy+YReBV5Rr+l X-Received: by 2002:a17:906:7f01:: with SMTP id d1mr31017637ejr.318.1629824330777; Tue, 24 Aug 2021 09:58:50 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1629824330; cv=none; d=google.com; s=arc-20160816; b=UUnaJZ1wyRbukDmpWKGyvNgzL5xfVoCvZwVU3xuytCFjsWrAutruSkfKi88vYMX4J3 WZa263ExLVo2VXwnWcIvULEE1JmQp3bQ+C/eoT3WZU0R4wBAVt7zuxwah7dh1bddeib7 8qZoB8quxauVruKZM3iMFF4BVJui5GI1tFhUoRQcBOJw0KUavVxKR2ov4ngn/T10RA6a EdmJ0lPYyNGu0qtri/ma/S5/tR4C1StTQQnsgEZ9InnONM8gEdEhfqdaMwiAZx2skNa8 tuGU7VeaMBIvsOnDo6D2bjK6AQzL+LmHJIcwOg0az9Pg1P2h+/C2l3qdXxFkLmI3o2Xm pMRA== 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 :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=wX5ZhReRQR2QWAuXEuTpV9G6HGG0f20o4/MiE0IqjwY=; b=zVBEpIz3dodJYW/eggqah4k2Yqd+mgqip67RwoE/ME41Uj6Z57+JDSfKHSrCERAf7/ zvbDnVJzIt8A8LffSdan4HN8yMBjzIjZshWZpkKWkCPke7sKPgn+p9ZKr8VASVx7jwI6 5XigUOJy2DR9s3iKkVVPa+bps7Hh+VU3GESceDQbfZ4hghI8pE27KuJgVwUbxHoaBnHE hW+JH5bVID8I0/BSjVX0iOw8TZVtW/Jt6icfNQI64bz+LbfQGGskrxEqITtXFFMxgEEO HI2kGhuHR8BbBHC+K5Scg2W1x73JpFGFGnJl0NF+dqIioIz+tmWJji6RuSl7w3rpl1nd Mo1w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=khXYeM5i; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id ka11si17410191ejc.367.2021.08.24.09.58.27; Tue, 24 Aug 2021 09:58:50 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=khXYeM5i; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234884AbhHXQ5a (ORCPT + 99 others); Tue, 24 Aug 2021 12:57:30 -0400 Received: from mail.kernel.org ([198.145.29.99]:38848 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233902AbhHXQ5F (ORCPT ); Tue, 24 Aug 2021 12:57:05 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id 78C2B613AB; Tue, 24 Aug 2021 16:56:20 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1629824181; bh=P+YzYrlZhUn53bFc5ZxXrutdcR1RWaHonkzVq/K0Vvg=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=khXYeM5i81moUEGX585kRXsfvBADE7bbKIbRLSXJVdMW/4gEOCBxJdmlsxMMPWkd9 MZVzJBki6KUvcZ1PANJq0+Bt/yQvndEew35+EiJvssNmS1hOOl5LGTuMO7uBbSB8nP ir/mnFOIkx2/Ar5rQqSfSi7ADlswE9dDzNscDggGRwj9L0EeyYhz2JTn/qLFnrC3IQ zyQ2oa1r3TDwO+teDCSvO5JCkV1cnh5vEbodDTlChJ25xZx9vQrw4l2dbpdf6GI2/G B6BJKm3p7VaRct4K3CpA0Xg8pEQcGRhLLbbZwFGAfW2S+7vQZv+hmUQUvLFGlwMFCy d7ZpcBtesLvWA== From: Sasha Levin To: linux-kernel@vger.kernel.org, stable@vger.kernel.org Cc: Harshvardhan Jha , Sumit Saxena , "Martin K . Petersen" , Sasha Levin Subject: [PATCH 5.13 012/127] scsi: megaraid_mm: Fix end of loop tests for list_for_each_entry() Date: Tue, 24 Aug 2021 12:54:12 -0400 Message-Id: <20210824165607.709387-13-sashal@kernel.org> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20210824165607.709387-1-sashal@kernel.org> References: <20210824165607.709387-1-sashal@kernel.org> MIME-Version: 1.0 X-KernelTest-Patch: http://kernel.org/pub/linux/kernel/v5.x/stable-review/patch-5.13.13-rc1.gz X-KernelTest-Tree: git://git.kernel.org/pub/scm/linux/kernel/git/stable/linux-stable-rc.git X-KernelTest-Branch: linux-5.13.y X-KernelTest-Patches: git://git.kernel.org/pub/scm/linux/kernel/git/stable/stable-queue.git X-KernelTest-Version: 5.13.13-rc1 X-KernelTest-Deadline: 2021-08-26T16:55+00:00 X-stable: review X-Patchwork-Hint: Ignore Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Harshvardhan Jha [ Upstream commit 77541f78eadfe9fdb018a7b8b69f0f2af2cf4b82 ] The list_for_each_entry() iterator, "adapter" in this code, can never be NULL. If we exit the loop without finding the correct adapter then "adapter" points invalid memory that is an offset from the list head. This will eventually lead to memory corruption and presumably a kernel crash. Link: https://lore.kernel.org/r/20210708074642.23599-1-harshvardhan.jha@oracle.com Acked-by: Sumit Saxena Signed-off-by: Harshvardhan Jha Signed-off-by: Martin K. Petersen Signed-off-by: Sasha Levin --- drivers/scsi/megaraid/megaraid_mm.c | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/drivers/scsi/megaraid/megaraid_mm.c b/drivers/scsi/megaraid/megaraid_mm.c index abf7b401f5b9..c509440bd161 100644 --- a/drivers/scsi/megaraid/megaraid_mm.c +++ b/drivers/scsi/megaraid/megaraid_mm.c @@ -238,7 +238,7 @@ mraid_mm_get_adapter(mimd_t __user *umimd, int *rval) mimd_t mimd; uint32_t adapno; int iterator; - + bool is_found; if (copy_from_user(&mimd, umimd, sizeof(mimd_t))) { *rval = -EFAULT; @@ -254,12 +254,16 @@ mraid_mm_get_adapter(mimd_t __user *umimd, int *rval) adapter = NULL; iterator = 0; + is_found = false; list_for_each_entry(adapter, &adapters_list_g, list) { - if (iterator++ == adapno) break; + if (iterator++ == adapno) { + is_found = true; + break; + } } - if (!adapter) { + if (!is_found) { *rval = -ENODEV; return NULL; } @@ -725,6 +729,7 @@ ioctl_done(uioc_t *kioc) uint32_t adapno; int iterator; mraid_mmadp_t* adapter; + bool is_found; /* * When the kioc returns from driver, make sure it still doesn't @@ -747,19 +752,23 @@ ioctl_done(uioc_t *kioc) iterator = 0; adapter = NULL; adapno = kioc->adapno; + is_found = false; con_log(CL_ANN, ( KERN_WARNING "megaraid cmm: completed " "ioctl that was timedout before\n")); list_for_each_entry(adapter, &adapters_list_g, list) { - if (iterator++ == adapno) break; + if (iterator++ == adapno) { + is_found = true; + break; + } } kioc->timedout = 0; - if (adapter) { + if (is_found) mraid_mm_dealloc_kioc( adapter, kioc ); - } + } else { wake_up(&wait_q); -- 2.30.2