Received: by 2002:a05:6a10:a0d1:0:0:0:0 with SMTP id j17csp2639267pxa; Mon, 17 Aug 2020 15:10:42 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyy/yAhTQzuaXyFqHo+Iqtj2NmLVVhchyd9tlU7qItNe2WQINAynkprimgL8mgmXxLJyHB2 X-Received: by 2002:a17:906:3493:: with SMTP id g19mr18093405ejb.253.1597702242623; Mon, 17 Aug 2020 15:10:42 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1597702242; cv=none; d=google.com; s=arc-20160816; b=PRDui7WtRRcXOH3ZZAbDJthpK+ypaVi+YRo76K/JjfMkAI93e9VbN/rcEHCZ4x77aN i4lejfcG/hH3t+GVP7dtM3aMEmADMM+FymkMr4eHSXnEBdk2FwMvRK37K89IUmCwd0A3 eBpak6Q4E1J9nj7U660p977qjE32+XfVYvoZJNDmaONDemzrRDuFDGI9mcgEkcJWjCYz kRf0TbCYHbdbcTlwrZF41Bdh1AOpsQukDl9yP437sRXQxxBiuAuFcLjlaRY4xRivUOVs Dm4wIncS060O4kwoS4SA/jKShAwWK6uayfVbxy9ElDaOL5FX2slcdopu2yeOXJXw2+BE 748g== 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=Yeqq8iyj2eZKrDyGeSDmIxqXZQlp7/ja+JjCBPiXuCY=; b=fP45Vn40EbtX+foCHLKime86xeSajV8/K0XoT/bs60rOxdCXgoaGpLSvWjNKZi+FZd pDjVnD9fmpN+89+R25NuKBEGS8fqMTtG8P7BWc/ePpfe5O6mudowLLZScIOKS5hLOGJo vuZ4+pQGzkptQ/JIcZVK8UzMICgRlYbsaf7D1rMler74O5TbsB0CUh0R3v0C8iwTrwmd 5fn2iVmFQKDjQ+D4PQzy+wz8gBXPlAsWjT1Td4wKUZiV44nu0vlCgdJjBDDEzM2qt3+Z TbaagkDWzfQWsRMwcD2junyjNJtFQsso77nPVbXV6rSRowVQuomrkBlaOES50AGcotBy ysXg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=nCOVb+mE; 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 bc16si11580360edb.394.2020.08.17.15.10.19; Mon, 17 Aug 2020 15:10:42 -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=nCOVb+mE; 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 S2388868AbgHQQij (ORCPT + 99 others); Mon, 17 Aug 2020 12:38:39 -0400 Received: from mail.kernel.org ([198.145.29.99]:44516 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2388089AbgHQP4y (ORCPT ); Mon, 17 Aug 2020 11:56:54 -0400 Received: from localhost (83-86-89-107.cable.dynamic.v4.ziggo.nl [83.86.89.107]) (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 E16DA207DA; Mon, 17 Aug 2020 15:56:51 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1597679812; bh=KtlD/dHbQX6e0yXtT9SBli8K4ds5ERxe2N5v+WXJC4c=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=nCOVb+mEbBz9gltE6Y2r/hZjvrVKxZYgsq4C/b4yJMTXw4WPpgbK3lf9MWEQaA1ZU W2s61SFYal1ea6/iIVsmQyG7ceq60m4hIP3NuYkfTAggyPH5N1NEBZTWeIvksvsP3G Ok4sARJPncV8gk5XPuKpaIinewFDOPF79yItk+zg= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Tom Rix , Herbert Xu Subject: [PATCH 5.7 342/393] crypto: qat - fix double free in qat_uclo_create_batch_init_list Date: Mon, 17 Aug 2020 17:16:32 +0200 Message-Id: <20200817143836.187159293@linuxfoundation.org> X-Mailer: git-send-email 2.28.0 In-Reply-To: <20200817143819.579311991@linuxfoundation.org> References: <20200817143819.579311991@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: Tom Rix commit c06c76602e03bde24ee69a2022a829127e504202 upstream. clang static analysis flags this error qat_uclo.c:297:3: warning: Attempt to free released memory [unix.Malloc] kfree(*init_tab_base); ^~~~~~~~~~~~~~~~~~~~~ When input *init_tab_base is null, the function allocates memory for the head of the list. When there is problem allocating other list elements the list is unwound and freed. Then a check is made if the list head was allocated and is also freed. Keeping track of the what may need to be freed is the variable 'tail_old'. The unwinding/freeing block is while (tail_old) { mem_init = tail_old->next; kfree(tail_old); tail_old = mem_init; } The problem is that the first element of tail_old is also what was allocated for the list head init_header = kzalloc(sizeof(*init_header), GFP_KERNEL); ... *init_tab_base = init_header; flag = 1; } tail_old = init_header; So *init_tab_base/init_header are freed twice. There is another problem. When the input *init_tab_base is non null the tail_old is calculated by traveling down the list to first non null entry. tail_old = init_header; while (tail_old->next) tail_old = tail_old->next; When the unwinding free happens, the last entry of the input list will be freed. So the freeing needs a general changed. If locally allocated the first element of tail_old is freed, else it is skipped. As a bit of cleanup, reset *init_tab_base if it came in as null. Fixes: b4b7e67c917f ("crypto: qat - Intel(R) QAT ucode part of fw loader") Cc: Signed-off-by: Tom Rix Signed-off-by: Herbert Xu Signed-off-by: Greg Kroah-Hartman --- drivers/crypto/qat/qat_common/qat_uclo.c | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) --- a/drivers/crypto/qat/qat_common/qat_uclo.c +++ b/drivers/crypto/qat/qat_common/qat_uclo.c @@ -332,13 +332,18 @@ static int qat_uclo_create_batch_init_li } return 0; out_err: + /* Do not free the list head unless we allocated it. */ + tail_old = tail_old->next; + if (flag) { + kfree(*init_tab_base); + *init_tab_base = NULL; + } + while (tail_old) { mem_init = tail_old->next; kfree(tail_old); tail_old = mem_init; } - if (flag) - kfree(*init_tab_base); return -ENOMEM; }