Received: by 2002:a05:6358:700f:b0:131:369:b2a3 with SMTP id 15csp688546rwo; Wed, 2 Aug 2023 02:32:04 -0700 (PDT) X-Google-Smtp-Source: APBJJlF8ApQea/g3yMkNx68ZZ++wsty6XwLZKfgM10lJs+dlU45JgkOBzbkwkSVE6t+zvB/dsb0a X-Received: by 2002:a05:6a00:813:b0:666:e621:d83e with SMTP id m19-20020a056a00081300b00666e621d83emr19884485pfk.13.1690968723794; Wed, 02 Aug 2023 02:32:03 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1690968723; cv=none; d=google.com; s=arc-20160816; b=U4TEEUpX/PlPtVEtNQoYUV7neExpZfUvIgdw0W26utIZcTDpSPv6ogtXPGfoNQFea0 F8AuHoGy26AM90c4wVeXnVPZ60LrKTTeRS4jxnMKMps16QD3U3DNBmGHNZpKRx0I7s2L xY8EN8lDOfdF7QOgcwFhGHbstSuG3fVKorPMIqoK5gZfNgYTKl9Wstw56pyskSrr/FfI hVWOVEirU+7NJ/O2+0dd4PQiTbmTK54REnP11eGjDAf+GqMo4kzqkEmemixzbkRAznI8 h+7k7gamDkhHUDmivvybEbLRIR86RxoEIHefjEmipTVQMbmcmQQ/Ro/xy5vBgyOyLnGM aGRg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:in-reply-to:from :references:cc:to:content-language:subject:user-agent:mime-version :date:message-id; bh=wZQ8vdFGyufXNnXlW+rCVqKmh2maLvHvtc7mVIoMgn0=; fh=ZPGckBoHTw1E5mgBda3QQAZzcJyAi8jdlIpQzrNbV94=; b=KmVSDrECVztFp3PwEV+guzJYVi+9aANuSRKmQ5h159HFN4SYxP+oaBG4PigH+j5XXg dgsDaQQOqlVmhm3IUi+HOY1sVWRBSbc4ZN99DfKRuULB3QdlYuO5xO+C5jhAi1C83aSd /ijoNHEr7vI8cE5+Bp2i7UUCbJseJQiZyiIsU1zJd8QWpIqC+qaRTm07JhSHVnalGYRc HwJY/HBsal7A64SaeeMHDm24NCwEKeGoZ8Zjv9Hdp9eexK1VAXqxQFXiNFO2aWT3fqBL aLAkUN37x/ZHhb+DbDcV1Nyb2+rgGh5mxu6vNjpCdeGbaymQcwNUPehUoJsYJyPyOU2i N3Lg== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=xs4all.nl Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id b124-20020a633482000000b00563e87fe3dfsi1438443pga.412.2023.08.02.02.31.49; Wed, 02 Aug 2023 02:32:03 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=xs4all.nl Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232449AbjHBIvf (ORCPT + 99 others); Wed, 2 Aug 2023 04:51:35 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52698 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229589AbjHBIvd (ORCPT ); Wed, 2 Aug 2023 04:51:33 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 78ED8D9; Wed, 2 Aug 2023 01:51:31 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id E4E4061898; Wed, 2 Aug 2023 08:51:30 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 19D5EC433CD; Wed, 2 Aug 2023 08:51:26 +0000 (UTC) Message-ID: Date: Wed, 2 Aug 2023 10:51:25 +0200 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.12.0 Subject: Re: [PATCH v3,1/2] media: mediatek: vcodec: Fix possible invalid memory access for decoder Content-Language: en-US To: Yunfei Dong , =?UTF-8?Q?N=c3=adcolas_F_=2e_R_=2e_A_=2e_Prado?= , Nicolas Dufresne , AngeloGioacchino Del Regno , Benjamin Gaignard , Nathan Hebert Cc: Chen-Yu Tsai , Hsin-Yi Wang , Fritz Koenig , Daniel Vetter , Steve Cho , linux-media@vger.kernel.org, devicetree@vger.kernel.org, linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-mediatek@lists.infradead.org, Project_Global_Chrome_Upstream_Group@mediatek.com References: <20230729034735.17213-1-yunfei.dong@mediatek.com> From: Hans Verkuil In-Reply-To: <20230729034735.17213-1-yunfei.dong@mediatek.com> Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit X-Spam-Status: No, score=-6.8 required=5.0 tests=BAYES_00, HEADER_FROM_DIFFERENT_DOMAINS,NICE_REPLY_A,RCVD_IN_DNSWL_HI, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Hi Yunfei, On 29/07/2023 05:47, Yunfei Dong wrote: > The vpu maybe null pointer or unreasonable value when scp crash, need to > validate that the vpu pointer and the vpu instance within this context is > valid in case of leading to kernel reboot. > > Fixes: 590577a4e525 ("[media] vcodec: mediatek: Add Mediatek V4L2 Video Decoder Driver") > Signed-off-by: Yunfei Dong > Reported-by: Steve Cho > Reviewed-by: Nicolas Dufresne > --- > - compared with v2: > - rewrite the commit message for patch 01 and 02. > - add Reported-by and Fixes tag. > - fix smatch fail for patch 02/2. > --- > .../vcodec/decoder/mtk_vcodec_dec_drv.h | 2 + > .../mediatek/vcodec/decoder/vdec_vpu_if.c | 77 ++++++++++++------- > 2 files changed, 52 insertions(+), 27 deletions(-) > > diff --git a/drivers/media/platform/mediatek/vcodec/decoder/mtk_vcodec_dec_drv.h b/drivers/media/platform/mediatek/vcodec/decoder/mtk_vcodec_dec_drv.h > index 6c318de25a55e..7e36b2c69b7d1 100644 > --- a/drivers/media/platform/mediatek/vcodec/decoder/mtk_vcodec_dec_drv.h > +++ b/drivers/media/platform/mediatek/vcodec/decoder/mtk_vcodec_dec_drv.h > @@ -161,6 +161,7 @@ struct mtk_vcodec_dec_pdata { > * @hw_id: hardware index used to identify different hardware. > * > * @msg_queue: msg queue used to store lat buffer information. > + * @vpu_inst: vpu instance pointer. > * > * @is_10bit_bitstream: set to true if it's 10bit bitstream > */ > @@ -205,6 +206,7 @@ struct mtk_vcodec_dec_ctx { > int hw_id; > > struct vdec_msg_queue msg_queue; > + void *vpu_inst; > > bool is_10bit_bitstream; > }; > diff --git a/drivers/media/platform/mediatek/vcodec/decoder/vdec_vpu_if.c b/drivers/media/platform/mediatek/vcodec/decoder/vdec_vpu_if.c > index 82c3dc8c41273..23cfe5c6c90b7 100644 > --- a/drivers/media/platform/mediatek/vcodec/decoder/vdec_vpu_if.c > +++ b/drivers/media/platform/mediatek/vcodec/decoder/vdec_vpu_if.c > @@ -72,6 +72,21 @@ static void handle_get_param_msg_ack(const struct vdec_vpu_ipi_get_param_ack *ms > } > } > > +static bool vpu_dec_check_ap_inst(struct mtk_vcodec_dec_dev *dec_dev, struct vdec_vpu_inst *vpu) > +{ > + struct mtk_vcodec_dec_ctx *ctx; > + int ret = false; > + > + list_for_each_entry(ctx, &dec_dev->ctx_list, list) { > + if (!IS_ERR_OR_NULL(ctx) && ctx->vpu_inst == vpu) { > + ret = true; > + break; > + } > + } > + > + return ret; > +} > + > /* > * vpu_dec_ipi_handler - Handler for VPU ipi message. > * > @@ -84,44 +99,51 @@ static void handle_get_param_msg_ack(const struct vdec_vpu_ipi_get_param_ack *ms > */ > static void vpu_dec_ipi_handler(void *data, unsigned int len, void *priv) > { > + struct mtk_vcodec_dec_dev *dec_dev; > const struct vdec_vpu_ipi_ack *msg = data; > - struct vdec_vpu_inst *vpu = (struct vdec_vpu_inst *) > - (unsigned long)msg->ap_inst_addr; > + struct vdec_vpu_inst *vpu; > > - if (!vpu) { > + dec_dev = (struct mtk_vcodec_dec_dev *)priv; > + vpu = (struct vdec_vpu_inst *)(unsigned long)msg->ap_inst_addr; > + if (!priv || !vpu) { > mtk_v4l2_vdec_err(vpu->ctx, "ap_inst_addr is NULL, did the SCP hang or crash?"); > return; > } This change results in a smatch error: drivers/media/platform/mediatek/vcodec/decoder/vdec_vpu_if.c:109 vpu_dec_ipi_handler() error: we previously assumed 'vpu' could be null (see line 108) The same happens in the next patch for vpu_enc_ipi_handler. Can you post a v4 fixing this? Thank you! Hans > > - mtk_vdec_debug(vpu->ctx, "+ id=%X", msg->msg_id); > + if (!vpu_dec_check_ap_inst(dec_dev, vpu) || msg->msg_id < VPU_IPIMSG_DEC_INIT_ACK || > + msg->msg_id > VPU_IPIMSG_DEC_GET_PARAM_ACK) { > + mtk_v4l2_vdec_err(vpu->ctx, "vdec msg id not correctly => 0x%x", msg->msg_id); > + vpu->failure = -EINVAL; > + goto error; > + } > > vpu->failure = msg->status; > - vpu->signaled = 1; > + if (msg->status != 0) > + goto error; > > - if (msg->status == 0) { > - switch (msg->msg_id) { > - case VPU_IPIMSG_DEC_INIT_ACK: > - handle_init_ack_msg(data); > - break; > + switch (msg->msg_id) { > + case VPU_IPIMSG_DEC_INIT_ACK: > + handle_init_ack_msg(data); > + break; > > - case VPU_IPIMSG_DEC_START_ACK: > - case VPU_IPIMSG_DEC_END_ACK: > - case VPU_IPIMSG_DEC_DEINIT_ACK: > - case VPU_IPIMSG_DEC_RESET_ACK: > - case VPU_IPIMSG_DEC_CORE_ACK: > - case VPU_IPIMSG_DEC_CORE_END_ACK: > - break; > + case VPU_IPIMSG_DEC_START_ACK: > + case VPU_IPIMSG_DEC_END_ACK: > + case VPU_IPIMSG_DEC_DEINIT_ACK: > + case VPU_IPIMSG_DEC_RESET_ACK: > + case VPU_IPIMSG_DEC_CORE_ACK: > + case VPU_IPIMSG_DEC_CORE_END_ACK: > + break; > > - case VPU_IPIMSG_DEC_GET_PARAM_ACK: > - handle_get_param_msg_ack(data); > - break; > - default: > - mtk_vdec_err(vpu->ctx, "invalid msg=%X", msg->msg_id); > - break; > - } > + case VPU_IPIMSG_DEC_GET_PARAM_ACK: > + handle_get_param_msg_ack(data); > + break; > + default: > + mtk_vdec_err(vpu->ctx, "invalid msg=%X", msg->msg_id); > + break; > } > > - mtk_vdec_debug(vpu->ctx, "- id=%X", msg->msg_id); > +error: > + vpu->signaled = 1; > } > > static int vcodec_vpu_send_msg(struct vdec_vpu_inst *vpu, void *msg, int len) > @@ -182,9 +204,10 @@ int vpu_dec_init(struct vdec_vpu_inst *vpu) > > init_waitqueue_head(&vpu->wq); > vpu->handler = vpu_dec_ipi_handler; > + vpu->ctx->vpu_inst = vpu; > > err = mtk_vcodec_fw_ipi_register(vpu->ctx->dev->fw_handler, vpu->id, > - vpu->handler, "vdec", NULL); > + vpu->handler, "vdec", vpu->ctx->dev); > if (err) { > mtk_vdec_err(vpu->ctx, "vpu_ipi_register fail status=%d", err); > return err; > @@ -193,7 +216,7 @@ int vpu_dec_init(struct vdec_vpu_inst *vpu) > if (vpu->ctx->dev->vdec_pdata->hw_arch == MTK_VDEC_LAT_SINGLE_CORE) { > err = mtk_vcodec_fw_ipi_register(vpu->ctx->dev->fw_handler, > vpu->core_id, vpu->handler, > - "vdec", NULL); > + "vdec", vpu->ctx->dev); > if (err) { > mtk_vdec_err(vpu->ctx, "vpu_ipi_register core fail status=%d", err); > return err;