Received: by 2002:a05:6a10:1d13:0:0:0:0 with SMTP id pp19csp2921026pxb; Tue, 24 Aug 2021 10:38:20 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzFImGod1ucLIYQgudkY5DTGcu7XQf2IRJPfl0BlUT/U8LuECr3hnOTxNBjzBb7s4E/isth X-Received: by 2002:a5e:9901:: with SMTP id t1mr12728490ioj.92.1629826699853; Tue, 24 Aug 2021 10:38:19 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1629826699; cv=none; d=google.com; s=arc-20160816; b=R/GH9K5D3NvihFtouO3BP9j7eU0RTRrq555fYcO4uvVB0JTYP8Qgb1BZmskQaYXCOE GBsUtCbo0sKiHZiNmGbEkMK5PBfnI1gh0ywX/yVh8ncM3S91YNzFPLfMsPHeBq6lNRHj 2jH64TnFP3HATkF98167s9SQtCJ714g89O8hIbfMNR/ohFmuMbrM1T4LKTpBs6MCD8xR wk49t8/k80fRqpmMQkiv17q+v4LXLa38zNd03+A+myhPs0YxaKP3K7OEx6QuIkhhmpL9 JugD6C/Imp5K05BxGsB8WOfbbLisCwYhI6z+joLCdKWCEAICdJ7vQQRKL1cqE0W16ifi pufw== 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=q5nle1glSlzGd0QhN3TsgeRZgFWaYeqFGASqUkYHvE4=; b=yg9uocAkqZP0AILEH1vMyzQiv/XXUh6TxBSOU88hBGsK+BuZwf+xF3cERy0xhIrZKB H1LfZa7Op4O82wQCbhtnvFsZp32ssTp5vvy788NNqfqdOZO7702CY1YyVhgsT9NiGbIA 9+CRLhb1VGLIQjFbOCqoNRR//QNDvLw8DQ5HysOkP3EUnW9BPHTxvWis4G5sDNnbBbbl d7LZKEAlcaXYrCo95e0C4cuPQ/IUwrwmYCOHIrEIP5m8vnBZbNedZTVxcKw74DuuhaA5 lhC9X4zB/di/C2vOsvU+N7RftgnAPsPFb/RgkZZZmL6P2jx99TAsONdq9xwbA2o4x3Mr 6NhQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=JPiJtJFX; 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 y12si18339324ilg.98.2021.08.24.10.38.08; Tue, 24 Aug 2021 10:38:19 -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=JPiJtJFX; 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 S234276AbhHXRiK (ORCPT + 99 others); Tue, 24 Aug 2021 13:38:10 -0400 Received: from mail.kernel.org ([198.145.29.99]:39250 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S242126AbhHXRei (ORCPT ); Tue, 24 Aug 2021 13:34:38 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id 6AC3961BA8; Tue, 24 Aug 2021 17:06:41 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1629824802; bh=oGeabWi9WM/jRq9Qk4TZ4TG8oYIuRbdR5vL5RuLGkIM=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=JPiJtJFXwfgdve5WiLy1EQH+t1WKPdsgs67j7t0PGUAI0TcaqNLGIAknIbXEgA/l0 9Bz/ayroY8nia6CU0dR082rcLXSXWymKc1DmAqWf/oMRi5g/k9s4zoLsGtyByl0JVo IElYmZ3/syH2H7831Gp5LAsNEfRVas9RiNSwOvSaB0/kt7usS0fn7qZ5b/llXANV/R lhiFSvYT79gutVvPmv4V7mewxhWAHWIihGIc/kkebWwle9Rjjtilplqk9B+6+8ZW9S RrPQu+5YijwRrxAJXwq0/GjEROmw/Q1BBhHFzVr650iUnivWco8UPL0mkPR5z/rz5z 0Nb2vutzSL7pg== 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 4.9 26/43] scsi: megaraid_mm: Fix end of loop tests for list_for_each_entry() Date: Tue, 24 Aug 2021 13:05:57 -0400 Message-Id: <20210824170614.710813-27-sashal@kernel.org> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20210824170614.710813-1-sashal@kernel.org> References: <20210824170614.710813-1-sashal@kernel.org> MIME-Version: 1.0 X-KernelTest-Patch: http://kernel.org/pub/linux/kernel/v4.x/stable-review/patch-4.9.281-rc1.gz X-KernelTest-Tree: git://git.kernel.org/pub/scm/linux/kernel/git/stable/linux-stable-rc.git X-KernelTest-Branch: linux-4.9.y X-KernelTest-Patches: git://git.kernel.org/pub/scm/linux/kernel/git/stable/stable-queue.git X-KernelTest-Version: 4.9.281-rc1 X-KernelTest-Deadline: 2021-08-26T17:06+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 4cf9ed96414f..d61df49e4e1b 100644 --- a/drivers/scsi/megaraid/megaraid_mm.c +++ b/drivers/scsi/megaraid/megaraid_mm.c @@ -250,7 +250,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; @@ -266,12 +266,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; } @@ -739,6 +743,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 @@ -761,19 +766,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