Received: by 2002:a05:6a10:22f:0:0:0:0 with SMTP id 15csp1058324pxk; Mon, 31 Aug 2020 08:41:41 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyJz4fotTaIqfW/ZOMQRG0bPtuFnP5kdaAO1CnOJE+wzAOxafHSkCH5Om3/rhfDk9vDoAnE X-Received: by 2002:a05:6402:354:: with SMTP id r20mr1769383edw.153.1598888501141; Mon, 31 Aug 2020 08:41:41 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1598888501; cv=none; d=google.com; s=arc-20160816; b=gU99KFn4BNVyLQUk2huD6D0IpMmjOABjDu/gKaMnwb4Nd8qCj3y6LCRwx0i3OVWatc C9HujYSk4mym44I2rjgyXN69xNHRXyOiXFvVo2BDeofOqsjBYF9vDGF3/umc7Mqwh67/ SVbbYSl0t1DFUob1bIG/gp9KqGhal3bdsCsPeN8RlgDdkXDad1yX2SR46DVfulNpTBEM 9a7pXmpSj7ECUzdZpG6IWyQZhJfQEifZCCZNJz8RNEG8r8ezM8pWwVRcdfKwSIa51JGb K0IkvLqG6PnKvc2ygIqbziSOMFp0z07+u6LcnJs5XvK160ZlahOa3AWYv6cUntqsc+96 AW9g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=GLB1PH2qhCv/Loni3JgLKjudRdJ6Dj5hMMhBJNnOhZ4=; b=Vftm7kZm6ApBlU6hRJtMRhonWYGw49nH9w2ftoO/vIzSJ+8bWlpCjf/Bohoruv8fqi Dx253iUHig8AaMJA9r4hn8ogx//pxvcQ91ualvNoJQgjRWl1Bu6gMhwd7IN41qsnU+f2 kYS4dqXQzn2VKBzSf3vZf6tiZLJRuXBwSsaLUnlNgCIr4Lepmb0oQefH/npX8y0eTXOe 3592V6QX5P9JQFLScpYNqn7erNZgdOIZYssUPsxC5WbWfuatUixWS5kUXTiwwz0O0lgq eMhieW29OzKABn9DRJRnArCVtnMUb2mzqIp4QAmbkAohuI9hjCzTVAUqkvw5b5duPd4G 4ZYw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=vZq8q3yu; 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 e14si3062498edm.126.2020.08.31.08.41.18; Mon, 31 Aug 2020 08:41:41 -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=default header.b=vZq8q3yu; 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 S1729400AbgHaPjh (ORCPT + 99 others); Mon, 31 Aug 2020 11:39:37 -0400 Received: from mail.kernel.org ([198.145.29.99]:39258 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728529AbgHaPaF (ORCPT ); Mon, 31 Aug 2020 11:30:05 -0400 Received: from sasha-vm.mshome.net (c-73-47-72-35.hsd1.nh.comcast.net [73.47.72.35]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 1C9092083E; Mon, 31 Aug 2020 15:30:04 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1598887804; bh=Ii+wToDJCWwOATgsfBK0PbupmDe0ZDk97QrrJqJwOqI=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=vZq8q3yuI8ZV9V54d0ym8et4fGq646utWRnju0m3ZnzitoqbjHxoyDb9ZUedida2B /HVQl/8Y4U53xU/rPoQWFiavVsUX7rlytvKvM9dfytLhhdeaTq+Xtb3glMxTFaSYx6 5Jyg9nNtzP6I4oKKOkRAvhI0v1Nasg5uokr2rFX8= From: Sasha Levin To: linux-kernel@vger.kernel.org, stable@vger.kernel.org Cc: Ofir Bitton , Oded Gabbay , Sasha Levin Subject: [PATCH AUTOSEL 5.8 20/42] habanalabs: check correct vmalloc return code Date: Mon, 31 Aug 2020 11:29:12 -0400 Message-Id: <20200831152934.1023912-20-sashal@kernel.org> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20200831152934.1023912-1-sashal@kernel.org> References: <20200831152934.1023912-1-sashal@kernel.org> MIME-Version: 1.0 X-stable: review X-Patchwork-Hint: Ignore Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Ofir Bitton [ 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 Signed-off-by: Oded Gabbay Signed-off-by: Sasha Levin --- 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 47da84a177197..e30b1b1877efa 100644 --- a/drivers/misc/habanalabs/memory.c +++ b/drivers/misc/habanalabs/memory.c @@ -66,6 +66,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) { @@ -93,7 +98,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; } @@ -683,7 +688,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 a290d6b49d788..eb582bd4937ba 100644 --- a/drivers/misc/habanalabs/mmu.c +++ b/drivers/misc/habanalabs/mmu.c @@ -450,7 +450,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