public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: Sasha Levin <sashal@kernel.org>
To: linux-kernel@vger.kernel.org, stable@vger.kernel.org
Cc: Ofir Bitton <obitton@habana.ai>,
	Oded Gabbay <oded.gabbay@gmail.com>,
	Sasha Levin <sashal@kernel.org>
Subject: [PATCH AUTOSEL 5.4 11/23] habanalabs: check correct vmalloc return code
Date: Mon, 31 Aug 2020 11:30:27 -0400	[thread overview]
Message-ID: <20200831153039.1024302-11-sashal@kernel.org> (raw)
In-Reply-To: <20200831153039.1024302-1-sashal@kernel.org>

From: Ofir Bitton <obitton@habana.ai>

[ Upstream commit 0839152f8c1efc1cc2d515d1ff1e253ca9402ad3 ]

vmalloc can return different return code than NULL and a valid
pointer. We must validate it in order to dereference a non valid
pointer.

Signed-off-by: Ofir Bitton <obitton@habana.ai>
Signed-off-by: Oded Gabbay <oded.gabbay@gmail.com>
Signed-off-by: Sasha Levin <sashal@kernel.org>
---
 drivers/misc/habanalabs/memory.c | 9 +++++++--
 drivers/misc/habanalabs/mmu.c    | 2 +-
 2 files changed, 8 insertions(+), 3 deletions(-)

diff --git a/drivers/misc/habanalabs/memory.c b/drivers/misc/habanalabs/memory.c
index 22566b75ca50c..acfccf32be6b9 100644
--- a/drivers/misc/habanalabs/memory.c
+++ b/drivers/misc/habanalabs/memory.c
@@ -67,6 +67,11 @@ static int alloc_device_memory(struct hl_ctx *ctx, struct hl_mem_in *args,
 	num_pgs = (args->alloc.mem_size + (page_size - 1)) >> page_shift;
 	total_size = num_pgs << page_shift;
 
+	if (!total_size) {
+		dev_err(hdev->dev, "Cannot allocate 0 bytes\n");
+		return -EINVAL;
+	}
+
 	contiguous = args->flags & HL_MEM_CONTIGUOUS;
 
 	if (contiguous) {
@@ -94,7 +99,7 @@ static int alloc_device_memory(struct hl_ctx *ctx, struct hl_mem_in *args,
 	phys_pg_pack->contiguous = contiguous;
 
 	phys_pg_pack->pages = kvmalloc_array(num_pgs, sizeof(u64), GFP_KERNEL);
-	if (!phys_pg_pack->pages) {
+	if (ZERO_OR_NULL_PTR(phys_pg_pack->pages)) {
 		rc = -ENOMEM;
 		goto pages_arr_err;
 	}
@@ -689,7 +694,7 @@ static int init_phys_pg_pack_from_userptr(struct hl_ctx *ctx,
 
 	phys_pg_pack->pages = kvmalloc_array(total_npages, sizeof(u64),
 						GFP_KERNEL);
-	if (!phys_pg_pack->pages) {
+	if (ZERO_OR_NULL_PTR(phys_pg_pack->pages)) {
 		rc = -ENOMEM;
 		goto page_pack_arr_mem_err;
 	}
diff --git a/drivers/misc/habanalabs/mmu.c b/drivers/misc/habanalabs/mmu.c
index 176c315836f12..d66e16de4cda3 100644
--- a/drivers/misc/habanalabs/mmu.c
+++ b/drivers/misc/habanalabs/mmu.c
@@ -422,7 +422,7 @@ int hl_mmu_init(struct hl_device *hdev)
 	hdev->mmu_shadow_hop0 = kvmalloc_array(prop->max_asid,
 					prop->mmu_hop_table_size,
 					GFP_KERNEL | __GFP_ZERO);
-	if (!hdev->mmu_shadow_hop0) {
+	if (ZERO_OR_NULL_PTR(hdev->mmu_shadow_hop0)) {
 		rc = -ENOMEM;
 		goto err_pool_add;
 	}
-- 
2.25.1


  parent reply	other threads:[~2020-08-31 15:35 UTC|newest]

Thread overview: 23+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-08-31 15:30 [PATCH AUTOSEL 5.4 01/23] HID: quirks: Always poll three more Lenovo PixArt mice Sasha Levin
2020-08-31 15:30 ` [PATCH AUTOSEL 5.4 02/23] HID: hiddev: Fix slab-out-of-bounds write in hiddev_ioctl_usage() Sasha Levin
2020-08-31 15:30 ` [PATCH AUTOSEL 5.4 03/23] drm/msm/dpu: Fix scale params in plane validation Sasha Levin
2020-08-31 15:30 ` [PATCH AUTOSEL 5.4 04/23] speakup: Fix wait_for_xmitr for ttyio case Sasha Levin
2020-08-31 15:30 ` [PATCH AUTOSEL 5.4 05/23] tty: serial: qcom_geni_serial: Drop __init from qcom_geni_console_setup Sasha Levin
2020-08-31 15:30 ` [PATCH AUTOSEL 5.4 06/23] drm/msm: add shutdown support for display platform_driver Sasha Levin
2020-08-31 15:30 ` [PATCH AUTOSEL 5.4 07/23] hwmon: (applesmc) check status earlier Sasha Levin
2020-08-31 15:30 ` [PATCH AUTOSEL 5.4 08/23] nvmet: Disable keep-alive timer when kato is cleared to 0h Sasha Levin
2020-08-31 15:30 ` [PATCH AUTOSEL 5.4 09/23] drm/msm: enable vblank during atomic commits Sasha Levin
2020-08-31 15:30 ` [PATCH AUTOSEL 5.4 10/23] habanalabs: validate FW file size Sasha Levin
2020-08-31 15:30 ` Sasha Levin [this message]
2020-08-31 15:30 ` [PATCH AUTOSEL 5.4 12/23] drm/msm/a6xx: fix gmu start on newer firmware Sasha Levin
2020-08-31 15:30 ` [PATCH AUTOSEL 5.4 13/23] ceph: don't allow setlease on cephfs Sasha Levin
2020-08-31 15:30 ` [PATCH AUTOSEL 5.4 14/23] drm/omap: fix incorrect lock state Sasha Levin
2020-08-31 15:30 ` [PATCH AUTOSEL 5.4 15/23] cpuidle: Fixup IRQ state Sasha Levin
2020-08-31 15:30 ` [PATCH AUTOSEL 5.4 16/23] nbd: restore default timeout when setting it to zero Sasha Levin
2020-08-31 15:30 ` [PATCH AUTOSEL 5.4 17/23] s390: don't trace preemption in percpu macros Sasha Levin
2020-08-31 15:30 ` [PATCH AUTOSEL 5.4 18/23] drm/amd/display: Reject overlay plane configurations in multi-display scenarios Sasha Levin
2020-08-31 15:30 ` [PATCH AUTOSEL 5.4 19/23] drivers: gpu: amd: Initialize amdgpu_dm_backlight_caps object to 0 in amdgpu_dm_update_backlight_caps Sasha Levin
2020-08-31 15:30 ` [PATCH AUTOSEL 5.4 20/23] drm/amd/display: Retry AUX write when fail occurs Sasha Levin
2020-08-31 15:30 ` [PATCH AUTOSEL 5.4 21/23] drm/amd/display: Fix memleak in amdgpu_dm_mode_config_init Sasha Levin
2020-08-31 15:30 ` [PATCH AUTOSEL 5.4 22/23] xen/xenbus: Fix granting of vmalloc'd memory Sasha Levin
2020-08-31 15:30 ` [PATCH AUTOSEL 5.4 23/23] fsldma: fix very broken 32-bit ppc ioread64 functionality Sasha Levin

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20200831153039.1024302-11-sashal@kernel.org \
    --to=sashal@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=obitton@habana.ai \
    --cc=oded.gabbay@gmail.com \
    --cc=stable@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox