Received: by 2002:a05:7412:2a8c:b0:e2:908c:2ebd with SMTP id u12csp1659724rdh; Mon, 25 Sep 2023 22:27:11 -0700 (PDT) X-Google-Smtp-Source: AGHT+IFx3XIKrEpj28/lLEmSxhn8+wPUuYFkgLTdI0Dn6C3/67j/VeXkeArXdbX1EULiIgnalHpA X-Received: by 2002:a05:6358:931a:b0:140:e7af:44c9 with SMTP id x26-20020a056358931a00b00140e7af44c9mr10137920rwa.23.1695706030863; Mon, 25 Sep 2023 22:27:10 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1695706030; cv=none; d=google.com; s=arc-20160816; b=I1CUsRf1bzbLyuMMuQMA90qycG2aEbd49dFxKjHy93h/jwllqDw1rwopqI0uwsVyTz vpmwzg5OsEbGdBfLpV+1wqNfgaS63udQ8O4CQ7Ag7kO+CSh7iMEW9x9indi4Ag43shoH atbT6Yq30dRf2ftizxL5HUJ7HRXgMSV5mhGneYOMlOCB9cUO2Hk0w8gDusKle4jvFZcp 81tBaHSH92fiL5av5zv9nXOnD69sUqJBI1dvOnqxpafdHMY2GyM+4KIC8dN9M+XROfcN KJ/OBxY7uzwD/Wid6vohocchutKem47kcijom5mf2GfzaHkZrONs5/4lXG+2k/rjqKRa k9Dw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:cc:to:subject :message-id:date:from:in-reply-to:references:mime-version :dkim-signature; bh=guHH5XwkYZghkYXHEacdf4rVrJmI+x6l8REN2+UZc1Q=; fh=yQjTp1/By1PHkbDKyUSuSQNoq7bDOc4pvZZbcWS396o=; b=U4jOnjCa+KrzBvW+Ul3ho7iec0wXUas05NSwYVEAF04Lw6S6It3zw1MOtbOuLHjYLk DkK0LFyt/LStuh4WMDzVWPdScuBeFvye1lyKLtj3sbV9zr7Uq4ooyi9G5xAu08l1Hf2P G1ey7iCfIXXLK+NelWuKNjmICZjSrVZsuORPDi5mEBHAZ3bs9KFPkjplTtmN0e9/3GDo tAASGwWKUvxWoCYWq/jrS9HaL42kID+CJfXozYikm+4efR7fVb4Xir07MOmCl/ouCoML 1yty2gP4Xi+ZgPRsvmH1ZVKHB1CXfcObVjoC/bDfHWrzP5U8FMGZ1EkMsYLLHcOYFiyI lTXQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=VDWO5fgp; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:4 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from howler.vger.email (howler.vger.email. [2620:137:e000::3:4]) by mx.google.com with ESMTPS id a35-20020a631a63000000b0056f8fdb4430si11687500pgm.893.2023.09.25.22.27.09 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 25 Sep 2023 22:27:10 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:4 as permitted sender) client-ip=2620:137:e000::3:4; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=VDWO5fgp; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:4 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by howler.vger.email (Postfix) with ESMTP id EAB44803986E; Mon, 25 Sep 2023 09:52:00 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at howler.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232611AbjIYQvk (ORCPT + 99 others); Mon, 25 Sep 2023 12:51:40 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47156 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229905AbjIYQvj (ORCPT ); Mon, 25 Sep 2023 12:51:39 -0400 Received: from mail-il1-x12e.google.com (mail-il1-x12e.google.com [IPv6:2607:f8b0:4864:20::12e]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 22358EE for ; Mon, 25 Sep 2023 09:51:31 -0700 (PDT) Received: by mail-il1-x12e.google.com with SMTP id e9e14a558f8ab-35137ab766dso3805ab.0 for ; Mon, 25 Sep 2023 09:51:31 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1695660690; x=1696265490; darn=vger.kernel.org; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=guHH5XwkYZghkYXHEacdf4rVrJmI+x6l8REN2+UZc1Q=; b=VDWO5fgpNDRgzkudo0Q4fFfqhuv4ZK7kpviqgoPlXVGirLuv8OJfB208yQkoMTcm4J W6gklOhEgFDukTczhNs5nQONJ7HZqxJAtA00FlS0r9Ekh0grmrmoODiyuivDELLXG5jY +AyiO1g/sLd/b91JEJbwQnIi7cFI/wgITVHCLXOs/tG7lfDMSI5Lcdk+npAKdeUJl1cC YXXnxv1QWcIAZp1a9Z6hGTWrib2mEBzF79AxQMtvZZidV1nTr2imx2P12Fv/AlErW3sd FU1tgzgdvx9F29kWXxHjNsyT2Z3GaJ0Mxz5P0b7X8a52enfdXQ+RO8Tmrgry4OtGb/cK IWUA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1695660690; x=1696265490; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=guHH5XwkYZghkYXHEacdf4rVrJmI+x6l8REN2+UZc1Q=; b=ljammiq0lvRoT9FFzBRmowNaqhttu8y/IUkU8ayAFfqTN18ynRSyfIyjDR4ayyNRxl lLvI4Qy3EM8k+BBeU5B4jX+Zol9pNX/9zJgiqZBXr+MyJuNLVk0pHaSWgHEVouSV1En+ 4u0gjKf2cnwqbGFg3M9YVHKa9aFraE3nDGGIsaHvbiChdHajrPA4tTmbGYtvvWiEny5+ LzdnizgxoN+SuoMHYVDisd3uShNz45HHfWdY5to3PmfGraK3LduHhMAEYYJTKPnF1zBU JdE47uwVITJeqSEz83JGfT+Cm7dO7nqqO3m12gn/p8vVSQqre7DdJy9y9s3BqXcEjK2y GV5Q== X-Gm-Message-State: AOJu0YzvWAfsDXFT5pyUVTUAhLNvH8K6IF/IbcmiTHl1qPGL9DhCOx/i 8FaS4abv86MkuaWlvDdpE8+so+aMhcGX5CEvTjke X-Received: by 2002:a92:c545:0:b0:340:502b:1487 with SMTP id a5-20020a92c545000000b00340502b1487mr10796ilj.12.1695660690206; Mon, 25 Sep 2023 09:51:30 -0700 (PDT) MIME-Version: 1.0 References: <20230911125936.10648-1-yunfei.dong@mediatek.com> <20230911125936.10648-13-yunfei.dong@mediatek.com> <1df3e79b84933dda0313d0d9719220dbc06c9022.camel@collabora.com> <5307203d79c0d90cc742a315bb161fa796b9960f.camel@mediatek.com> <00302ac675af858eb11d8398f100921af806bc30.camel@mediatek.com> <3e053387-4ba6-49bc-a59a-46854e0a7c26@xs4all.nl> <71cadec5-06df-4490-9b06-e3af6bb43498@xs4all.nl> In-Reply-To: <71cadec5-06df-4490-9b06-e3af6bb43498@xs4all.nl> From: Jeffrey Kardatzke Date: Mon, 25 Sep 2023 09:51:15 -0700 Message-ID: Subject: Re: [PATCH 12/14] media: medkatek: vcodec: set secure mode to decoder driver To: Hans Verkuil Cc: =?UTF-8?B?WXVuZmVpIERvbmcgKOiRo+S6kemjnik=?= , "nicolas.dufresne@collabora.com" , "linux-kernel@vger.kernel.org" , "linux-mediatek@lists.infradead.org" , "frkoenig@chromium.org" , "stevecho@chromium.org" , "wenst@chromium.org" , "nhebert@chromium.org" , "linux-media@vger.kernel.org" , "devicetree@vger.kernel.org" , "daniel@ffwll.ch" , Project_Global_Chrome_Upstream_Group , "benjamin.gaignard@collabora.com" , "hsinyi@chromium.org" , "linux-arm-kernel@lists.infradead.org" , "angelogioacchino.delregno@collabora.com" , "nfraprado@collabora.com" Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Spam-Status: No, score=-17.6 required=5.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF, ENV_AND_HDR_SPF_MATCH,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS, USER_IN_DEF_DKIM_WL,USER_IN_DEF_SPF_WL autolearn=unavailable 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 X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (howler.vger.email [0.0.0.0]); Mon, 25 Sep 2023 09:52:01 -0700 (PDT) On Mon, Sep 25, 2023 at 2:00=E2=80=AFAM Hans Verkuil wrote: > > On 22/09/2023 21:17, Jeffrey Kardatzke wrote: > > On Fri, Sep 22, 2023 at 1:44=E2=80=AFAM Hans Verkuil wrote: > >> > >> On 22/09/2023 05:28, Yunfei Dong (=E8=91=A3=E4=BA=91=E9=A3=9E) wrote: > >>> Hi Hans, > >>> > >>> Thanks for your help to give some good advice. > >>> On Wed, 2023-09-20 at 09:20 +0200, Hans Verkuil wrote: > >>>> > >>>>>>>> In any case, using a control to switch to secure mode and using > >>>> a control > >>>>>>>> to convert a dmabuf fd to a secure handle seems a poor choice to > >>>> me. > >>>>>>>> > >>>>>>>> I was wondering if it wouldn't be better to create a new > >>>> V4L2_MEMORY_ type, > >>>>>>>> e.g. V4L2_MEMORY_DMABUF_SECURE (or perhaps _DMABUF_OPTEE). That > >>>> ensures that > >>>>>>>> once you create buffers for the first time, the driver can > >>>> switch into secure > >>>>>>>> mode, and until all buffers are released again you know that the > >>>> driver will > >>>>>>>> stay in secure mode. > >>>>>>> > >>>>>>> Why do you think the control for setting secure mode is a poor > >>>> choice? > >>>>>>> There's various places in the driver code where functionality > >>>> changes > >>>>>>> based on being secure/non-secure mode, so this is very much a > >>>> 'global' > >>>>>>> setting for the driver. It could be inferred based off a new > >>>> memory > >>>>>>> type for the queues...which then sets that flag in the driver; > >>>> but > >>>>>>> that seems like it would be more fragile and would require > >>>> checking > >>>>>>> for incompatible output/capture memory types. I'm not against > >>>> another > >>>>>>> way of doing this; but didn't see why you think the proposed > >>>> method is > >>>>>>> a poor choice. > >>>>>> > >>>>>> I assume you are either decoding to secure memory all the time, or > >>>> not > >>>>>> at all. That's something you would want to select the moment you > >>>> allocate > >>>>>> the first buffer. Using the V4L2_MEMORY_ value would be the > >>>> natural place > >>>>>> for that. A control can typically be toggled at any time, and it > >>>> makes > >>>>>> no sense to do that for secure streaming. > >>>>>> > >>>>>> Related to that: if you pass a dmabuf fd you will need to check > >>>> somewhere > >>>>>> if the fd points to secure memory or not. You don't want to mix > >>>> the two > >>>>>> but you want to check that at VIDIOC_QBUF time. > >>>>>> > >>>>>> Note that the V4L2_MEMORY_ value is already checked in the v4l2 > >>>> core, > >>>>>> drivers do not need to do that. > >>>>> > >>>>> Just to clarify a bit, and make sure I understand this too. You are > >>>> proposing to > >>>>> introduce something like: > >>>>> > >>>>> V4L2_MEMORY_SECURE_DMABUF > >>>>> > >>>>> Which like V4L2_MEMORY_DMABUF is meant to import dmabuf, while > >>>> telling the > >>>>> driver that the memory is secure according to the definition of > >>>> "secure" for the > >>>>> platform its running on. > >>>>> > >>>>> This drivers also allocate secure SHM (a standard tee concept) and > >>>> have internal > >>>>> allocation for reconstruction buffer and some hw specific reference > >>>> metadata. So > >>>>> the idea would be that it would keep allocation using the dmabuf > >>>> heap internal > >>>>> APIs ? And decide which type of memory based on the memory type > >>>> found in the > >>>>> queue? > >>>> > >>>> Yes. Once you request the first buffer you basically tell the driver > >>>> whether it > >>>> will operate in secure or non-secure mode, and that stays that way > >>>> until all > >>>> buffers are freed. I think that makes sense. > >>>> > >>> > >>> According to iommu's information, the dma operation for secure and no= n- > >>> secure are the same, whether just need to add one memory type in v4l2 > >>> framework the same as V4L2_MEMORY_DMABUF? The dma operation in > >>> videobuf2-dma-contig.c can use the same functions. > >> > >> So if I pass a non-secure dma fd to the capture queue of the codec, wh= o > >> will check that it can't write the data to that fd? Since doing so wou= ld > >> expose the video. Presumably at some point the tee code will prevent t= hat? > >> (I sincerely hope so!) > > > > It is entirely the job of the TEE to prevent this. Nothing in the > > kernel should allow exploitation of what happens in the TEE no matter > > what goes on in the kernel > > > >> > >> Having a separate V4L2_MEMORY_DMABUF_SECURE type is to indicate to the > >> driver that 1) it can expect secure dmabuf fds, 2) it can configure it= self > >> for that (that avoids using a control to toggle between normal and sec= ure mode), > >> and at VIDIOC_QBUF time it is easy for the V4L2 core to verify that th= e > >> fd that is passed in is for secure memory. This means that mistakes by > >> userspace are caught at QBUF time. > >> > >> Of course, this will not protect you (people can disable this check by > >> recompiling the kernel), that still has to be done by the firmware, bu= t > >> it catches userspace errors early on. > >> > >> Also, while for this hardware the DMA operation is the same, that migh= t > >> not be the case for other hardware. > > > > That's a really good point. So one of the other models that is used > > for secure video decoding is to send the encrypted buffer into the > > video decoder directly (i.e. V4L2_MEMORY_MMAP) and then also send in > > all the corresponding crypto parameters (i.e. algorithm, IV, > > encryption pattern, etc.). Then the video driver internally does the > > decryption and decode in one operation. That's not what we want to > > use here for Mediatek; but I've done other integrations that work that > > way (that was for VAAPI [1], not V4L2...but there are other ARM > > implementations that do operate that way). So if we end up requiring > > V4L2_MEMORY_DMABUF_SECURE to indicate secure mode and enforce it on > > output+capture, that'll close off other potential solutions in the > > future. > > > > Expanding on your point about DMA operations being different on > > various hardware, that also makes me think a general check for this in > > v4l2 code may also be limiting. There are various ways secure video > > pipelines are done, so leaving these checks up to the individual > > drivers that implement secure video decode may be more pragmatic. If > > there's a generic V4L2 _CID_SECURE_MODE control, that makes it more > > general for how drivers can handle secure video decode. > > No, using a control for this is really wrong. > > The reason why I want it as a separate memory type is that that is > what you use when you call VIDIOC_REQBUFS, and that ioctl is also > when things are locked down in a driver. As long as no buffers have > been allocated, you can still change formats, parameters, etc. But > once buffers are allocated, most of that can't be changed, since > changing e.g. the format would also change the buffer sizes. > > It also locks down who owns the buffers by storing the file descriptor. > This prevents other processes from hijacking the I/O streaming, only > the owner can stream buffers. > > So it is a natural point in the sequence for selecting secure > buffers. > > If you request V4L2_MEMORY_DMABUF_SECURE for the output, then the > capture side must also use DMABUF_SECURE. Whether or not you can > use regular DMABUF for the output side and select DMABUF_SECURE > on the capture side is a driver decision. It can be useful to > support this for testing the secure capture using regular video > streams (something Nicolas discussed as well), but it depends on > the hardware whether you can use that technique. OK, that does work for the additional cases I mentioned. And for testing...we would still want to use DMABUF_SECURE on both ends for Mediatek at least (that's the only way they support it). But rather than having to bother with a clearkey implementation...we can just do something that directly copies compressed video into the secure dmabufs and then exercises the whole pipeline from there. This same thing happens with the 'clear lead' that is sometimes there with encrypted video (where the first X seconds are unencrypted and then it switches to encrypted...but you're still using the secure video pipeline on the unencrypted frames in that case). > > Regards, > > Hans > > > > > [1] - https://github.com/intel/libva/blob/master/va/va.h#L2177 > > > >> > >> Regards, > >> > >> Hans > >> > >>> > >>> Best Regards, > >>> Yunfei Dong > >>> > >> >