Received: by 2002:a05:6a10:1d13:0:0:0:0 with SMTP id pp19csp2923348pxb; Tue, 24 Aug 2021 10:41:39 -0700 (PDT) X-Google-Smtp-Source: ABdhPJz3voWEZYIh77pTTIoqQqerXVrbA18LiXqIMLaC+7AfHzKFuv3uAP0EA0/3s64EiUvqJkN7 X-Received: by 2002:a17:906:4d4d:: with SMTP id b13mr17660125ejv.528.1629826898835; Tue, 24 Aug 2021 10:41:38 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1629826898; cv=none; d=google.com; s=arc-20160816; b=tQG+MAkCqcGBNSKui7GF5OO/R+yd43puCUgVA3bwGo0ERgtvNkJlS6rC/fFZ7kXNAw Ku6LaCAgHkH7x7nv53dxB4w6lMbNJ1VUeavctAFzONM8aJw0JrTkMl64GBPL5CzsDefv lIWB23oJkrdg83+06rbU47Eg5tYeH6y34cMpthJ2zeeQOdjPBdoaAApD8QsNLxJELxsn aGttV8bXC2kQ9hy9yo3zciNA2efBgUvMtJgHXX/nq+ane9pShLLh4KDVFN8x1ySHajSN CEoDz1nJLOMPFPq7xe+YBuFmTLPHFcvnkV+JzYJrS08VcXLjO34VDj20UzT0UZVIQpHv Hu8w== 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=Bc14fF0Djppn8IouFp0qDj3+kCBmxPw22XpZOKxTLTA=; b=ZUr0SOzdoSqCAMY/5Fwh+E6X0hN0U8OPN50Q1zowO8yVpw3538s8Ng1YddBeF38/s3 GJuZCqcV5lhnvoJ43uT7HPzQAkiy8xJrkDiELz5hczU5Kajy93XlP4by0Jw+9+rqBq4o IDwFYInpEl1du2+QwyIjqKo7W7cEGOIf6WDxP9RmNjAZ4u6UMWoZCrS5QDZGcdi1vFZz 8ptKfvIl0w6TmNA0JJrmXFvV6bODqc+wHoEj1SXzKT8J2T0LIOlpyqYxdoP+AAD31Cta iKr5acbH3W5L9ZRCAs8oyJgZXhy4vZTIgGxIjRpjhUfLwNMYCtb52wFboy+UM2OdFCzB 1cdw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=rxMO8+IO; 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 a11si6634622edx.218.2021.08.24.10.41.15; Tue, 24 Aug 2021 10:41:38 -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=rxMO8+IO; 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 S241364AbhHXRko (ORCPT + 99 others); Tue, 24 Aug 2021 13:40:44 -0400 Received: from mail.kernel.org ([198.145.29.99]:45216 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S241680AbhHXRih (ORCPT ); Tue, 24 Aug 2021 13:38:37 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id 3E7A4619E9; Tue, 24 Aug 2021 17:08:02 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1629824882; bh=JPSvbA6z+4XkcwN21qZQQSJK10TkHR2cb1jPaxsUSkE=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=rxMO8+IOND7sMGeJqH8/VECZEWzBs6CR8r8wyq7+Cx1P+b+851RZHAs7Mqz+3cGHP pFHdgsS6kmRMMZuoW9TIcBlC8nbX12ZKr8pDSyOk76DioFfqi4ZDs/DJJGfzHgufYH c1pPsyCUPMbfEjYOnJ6VjdZiQyQPyYXPON+5cBgoYkk6nmRRWqm1X3JyZygG6RuNgv 5/lI4GkX0FJpXuyt2Zy+OhGI/dOWkkq6c8ARNha3YdTNC81QJzHNYN7X4+raWonXB1 tOkkZqWw2kyQxDg/6odpnf6J6VzHOb9PdLQwvtszgpbnxLIqWeOopmdvelibR7+bBm CkRdOM+oWmshg== 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.4 19/31] scsi: megaraid_mm: Fix end of loop tests for list_for_each_entry() Date: Tue, 24 Aug 2021 13:07:31 -0400 Message-Id: <20210824170743.710957-20-sashal@kernel.org> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20210824170743.710957-1-sashal@kernel.org> References: <20210824170743.710957-1-sashal@kernel.org> MIME-Version: 1.0 X-KernelTest-Patch: http://kernel.org/pub/linux/kernel/v4.x/stable-review/patch-4.4.282-rc1.gz X-KernelTest-Tree: git://git.kernel.org/pub/scm/linux/kernel/git/stable/linux-stable-rc.git X-KernelTest-Branch: linux-4.4.y X-KernelTest-Patches: git://git.kernel.org/pub/scm/linux/kernel/git/stable/stable-queue.git X-KernelTest-Version: 4.4.282-rc1 X-KernelTest-Deadline: 2021-08-26T17:07+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 a70692779a16..34067dfd2841 100644 --- a/drivers/scsi/megaraid/megaraid_mm.c +++ b/drivers/scsi/megaraid/megaraid_mm.c @@ -246,7 +246,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; @@ -262,12 +262,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; } @@ -735,6 +739,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 @@ -757,19 +762,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