Received: by 2002:a05:6a10:22f:0:0:0:0 with SMTP id 15csp4130720pxk; Tue, 8 Sep 2020 11:26:41 -0700 (PDT) X-Google-Smtp-Source: ABdhPJw6U2tgiyk62xaTTQSsqEjX0Lgi+kfY7ZRsM+UOBCV0cRoq2jG0TuiXfIxa074ekh7mp3SR X-Received: by 2002:aa7:cb8f:: with SMTP id r15mr288357edt.356.1599589600835; Tue, 08 Sep 2020 11:26:40 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1599589600; cv=none; d=google.com; s=arc-20160816; b=BB2Wtty5jan0sKCF6J2V1m9Tt9lMoqRGomuxrEh8P/g3i+anvyviFmmihDvuOdkGBq AdQ+EDjYV68Pitb82qG8fZiSlkRwataIEKlYRqEKXrKSsG4Sza8YdF0E/S6nv32Ncan7 h+jBLSaMjFtSZkkS7zlIn2WgugB6CNnTPrYENBYayNQUrBQr0ERUaDhdxA5v703v19rN 7mShWi/xVQ+qXMGv5+xAEMSxnA6yGREU4zYhB7fTdaCXRGP9kcEhjuLMcARTMBqucE1I vdq/jl6yM9N+dG+j0NfNKL2mTrkW88oFVwc9FV7jg+wyLmXNEyWBuEOfYQc+BG7v/Y82 9ZWg== 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 :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=a0qYY3rTc2P7GR1zZ769LM7+i7t9mzWJIS2K86jlurg=; b=q7+/U8Mp7rBl5Bg7VOToGyll+3NR7Z36MNi4nfxauCHYBfGIIF8gQq8MSPJFHfnQZa ATJi2Wb/+xfCmWjjn1Jf9aI+tdUwezPVO0s/Wi18NcG8Al4DQ5cY3JOQb02slaz0InrP 2WSVS42jHldGxGbc8kTDAIBI2m7+bNDWj72tgmTF34F/rC2XSSUK7thOVmJaoSiTqylr n5piIWwfgYNXFZNgRS/DboEGshKPNV37YMZXwn67r98CztvkZVYY+BPEc4sEnb43aX25 IK60YGaxskGbA8lvtpBu9LQTZMYJAnJKT940YdjdLVA4nU8X/u062/KnOIKl+a4G8ah1 aGQA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=AkLg9lCJ; 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=fail (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id b19si12139205eds.387.2020.09.08.11.26.18; Tue, 08 Sep 2020 11:26:40 -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=AkLg9lCJ; 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=fail (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1732271AbgIHSWh (ORCPT + 99 others); Tue, 8 Sep 2020 14:22:37 -0400 Received: from mail.kernel.org ([198.145.29.99]:56076 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1731465AbgIHQLL (ORCPT ); Tue, 8 Sep 2020 12:11:11 -0400 Received: from localhost (83-86-74-64.cable.dynamic.v4.ziggo.nl [83.86.74.64]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 59865247BE; Tue, 8 Sep 2020 15:42:23 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1599579743; bh=v4bypPfOHHDKcx4EgTafloiA7b16Xk5jkU7UZswiyRI=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=AkLg9lCJsrz/D0CuCVMYQmAljVXwYibNbIGyouBwKbSU0bTHmR6sYpbQYjpv1FBaC 0zA+0VQt5o43JhcQa2STypUnV7GwaAjF+y/uaQ6tmU1r8jh3QWkrJgfA3nlMpSL7Gm arBGXT9Bj0+emLOZrglL56JDDPgvoH7gpqdjymos= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Ofir Bitton , Oded Gabbay , Sasha Levin Subject: [PATCH 5.4 009/129] habanalabs: check correct vmalloc return code Date: Tue, 8 Sep 2020 17:24:10 +0200 Message-Id: <20200908152230.156658750@linuxfoundation.org> X-Mailer: git-send-email 2.28.0 In-Reply-To: <20200908152229.689878733@linuxfoundation.org> References: <20200908152229.689878733@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 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 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