Received: by 2002:a05:6a10:f347:0:0:0:0 with SMTP id d7csp1248049pxu; Fri, 27 Nov 2020 03:18:14 -0800 (PST) X-Google-Smtp-Source: ABdhPJzWb4yLDWX49ZZM1vuhhdmaDJymLjalZw5iU2xLFr00gnT5GJzKNxGHz8aDeIwCmW09rD8D X-Received: by 2002:a17:907:265c:: with SMTP id ar28mr7044675ejc.172.1606475894532; Fri, 27 Nov 2020 03:18:14 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1606475894; cv=none; d=google.com; s=arc-20160816; b=V33tS5RJD6i42Q8IdTPMDwnkd2CQtU/0nV08x3Np1OInTcBwdBXqHOstTf8fAWKvzB 9s8FWj6UxuNZdqogiR8X/gLk1rIKP6WZWBB0UDNHrg0oyPZBcVfocXhwka+mwAc5ooAi gxKyFJneapIi5xmJBzMf5pjtW0FAnvKkMCVxxKqf/m8mx+RPlZ7E2azecQNO8SJGxp2Y 7aHJETIFEJrBzxM6k9B4yt0SnfysZ4lqiq6IAyWRtRrm4h7+gR/IHfhntuLD2QK7/t2L PB0NPWROsRIZQ1iXNXE59VW/+rnBbmG9YakjnLFrOtNXE67qZZF8ziFZEeqj4jw+w1KP RzDA== 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:subject:cc:to:from:date :dkim-signature; bh=T92iN0lwvej2mpBB65nqz4F/Ax7wx/obhCdQ07Gga84=; b=CFRspkKPOE4SpJZgKhdn+Kg49ZBLrPUC4Qowb5+tm1TM9XWP2oibW+EtRwPHgPbk/U trc3l6Qk6ziM8eftYlM5AtqBLMWb/Fxftrd55XEwwk6+io8h4cM/yDcmb5mSoG5ZIq6p yevqqMXo7I1T1PYjIhFqcdNPexYluGFluuz5mntbFuu0WWDPqL8eEc1FNqis4yijtBT5 t4FHPgAY01TSlTAWR/LWMdU04QIMaGTma5MyVaiMyWLyXMUDUa2VAsUrMena9k4Hv5VX yOWlA9N37C9+Q2ohGzDV8mQKzswHw2O+YOL4xs3o1ikkLA4flNRKA2AauT14LVT71Gyt gqtw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=R5uRv8Im; 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 h20si4811522edw.304.2020.11.27.03.17.50; Fri, 27 Nov 2020 03:18:14 -0800 (PST) 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=R5uRv8Im; 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 S1728089AbgK0LOT (ORCPT + 99 others); Fri, 27 Nov 2020 06:14:19 -0500 Received: from mail.kernel.org ([198.145.29.99]:53532 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725616AbgK0LOS (ORCPT ); Fri, 27 Nov 2020 06:14:18 -0500 Received: from coco.lan (ip5f5ad5a6.dynamic.kabel-deutschland.de [95.90.213.166]) (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 EA54721527; Fri, 27 Nov 2020 11:14:15 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1606475657; bh=UUHBLygREjHYXn3z1M033GRc6uSd/b7ObYwCOHWwod8=; h=Date:From:To:Cc:Subject:In-Reply-To:References:From; b=R5uRv8ImrJl1TIvMjjrXi8QUS5/ZkIsqaHATSP0jbwCGdIG8Q8vxZSarNQXImurFe JWekhIK4QpTFZolQrm1dzdBZ81/7MoQ/YtIDH33G8DZ3DCOPztbu/7/zld5/UtxzIx NXvscXKSJB6dHftOxfMyRxN4Z0vcc+7UBlacXV84= Date: Fri, 27 Nov 2020 12:14:12 +0100 From: Mauro Carvalho Chehab To: Jia-Ju Bai Cc: stanimir.varbanov@linaro.org, agross@kernel.org, bjorn.andersson@linaro.org, linux-media@vger.kernel.org, linux-arm-msm@vger.kernel.org, linux-kernel@vger.kernel.org Subject: Re: [PATCH] media: venus: fix possible buffer overlow casued bad DMA value in venus_sfr_print() Message-ID: <20201127121412.2c982188@coco.lan> In-Reply-To: <20200530024117.24613-1-baijiaju1990@gmail.com> References: <20200530024117.24613-1-baijiaju1990@gmail.com> X-Mailer: Claws Mail 3.17.8 (GTK+ 2.24.32; x86_64-redhat-linux-gnu) MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Em Sat, 30 May 2020 10:41:17 +0800 Jia-Ju Bai escreveu: > The value hdev->sfr.kva is stored in DMA memory, and it is assigned to > sfr, so sfr->buf_size can be modified at anytime by malicious hardware. > In this case, a buffer overflow may happen when the code > "sfr->data[sfr->buf_size - 1]" is executed. > > To fix this possible bug, sfr->buf_size is assigned to a local variable, > and then this variable is checked before being used. > > Signed-off-by: Jia-Ju Bai > --- > drivers/media/platform/qcom/venus/hfi_venus.c | 9 +++++++-- > 1 file changed, 7 insertions(+), 2 deletions(-) > > diff --git a/drivers/media/platform/qcom/venus/hfi_venus.c b/drivers/media/platform/qcom/venus/hfi_venus.c > index 0d8855014ab3..4251a9e47a1b 100644 > --- a/drivers/media/platform/qcom/venus/hfi_venus.c > +++ b/drivers/media/platform/qcom/venus/hfi_venus.c > @@ -960,18 +960,23 @@ static void venus_sfr_print(struct venus_hfi_device *hdev) > { > struct device *dev = hdev->core->dev; > struct hfi_sfr *sfr = hdev->sfr.kva; > + u32 buf_size; > void *p; > > if (!sfr) > return; > > - p = memchr(sfr->data, '\0', sfr->buf_size); > + buf_size = sfr->buf_size; > + if (buf_size > 1) That seems plain wrong to me... I suspect you wanted to do, instead: if (buf_size < 1) or even: if (buf_size < 1 || buf_size >= maximum_size_of_data) > + return; > + > + p = memchr(sfr->data, '\0', buf_size); > /* > * SFR isn't guaranteed to be NULL terminated since SYS_ERROR indicates > * that Venus is in the process of crashing. > */ > if (!p) > - sfr->data[sfr->buf_size - 1] = '\0'; > + sfr->data[buf_size - 1] = '\0'; Well, a malicious hardware with DMA access could simply write 0 to some random address, without needing to rely on the value of sfr->buf_size. I can't see how a change like that would prevent that. A check like that only makes sense if the driver can ever call this function with an invalid value for sfr->buf_size. > > dev_err_ratelimited(dev, "SFR message from FW: %s\n", sfr->data); > } Thanks, Mauro