Received: by 2002:a05:7412:2a8c:b0:e2:908c:2ebd with SMTP id u12csp2235120rdh; Tue, 26 Sep 2023 17:44:32 -0700 (PDT) X-Google-Smtp-Source: AGHT+IF0rX6C1l8JXfqE9tz++wQNcDdf7UO5+uLnWLF0M0j1YQiiA2DvR+WHJxndest2irkJtHhd X-Received: by 2002:a17:902:a415:b0:1c6:1679:e035 with SMTP id p21-20020a170902a41500b001c61679e035mr281246plq.21.1695775472076; Tue, 26 Sep 2023 17:44:32 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1695775472; cv=none; d=google.com; s=arc-20160816; b=TFFxUJqpPDr+L772Z/8+Na8TH3wyP55yNHt0sgj2845L6bw75Me4thG2pMmvjFujMK YGTWOmZGkrq44agWkDDJ+3Dj+ODfcCzz6jwmlF/HO1dO7M1EsjBanzd3f4fzJseBhRtD 02KvwehRExp++QFbHyfI7p9wK8iqJFrgHaJhzFf7ZrrZgkHSVoxc7jPFGzBf9/0xcdhc 9bpAM7+uWmD2ELTBHQyrqAQ2tyET7wdbOlT1Ik+xFmg7IUvJ+4f1dzndAGkbuHksixy6 NhjafPJApkZXqMM8wq8qqaoK4qV2b9+nejTc797C2KoSepUM+GqiXY9PwzeZ2Ti0rznM EJcw== 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=TA45zhytFPggJ/KNc3nT/s2rZmIFeSaR3+xLqtcK0a4=; fh=yQjTp1/By1PHkbDKyUSuSQNoq7bDOc4pvZZbcWS396o=; b=MQIYYbeB4j+xu0GZsaHoB7fy2l6cBw8rjksiqjNlOLiyq5LnDZP1g1CGaUMEEcS5GG jYWV1urzKPJe4QDobxjYUj/W9f2Dxv/rokhVGuE+hB07b4vB9uJFVV2aklvu1kW6Ph8C jXWw1ijel9hjqZBI3S+DiM5CcgE3Heibx94mtVdh2u4SY6REl/YJRHdREc6Q2aSqgOWh 6sXlkXlZeP/iYWc2M1XK2iNfdahe9QyWt54P7wYTLf1s5g2DAa4dyi0YaQyN5ELI0pb0 Gf57yLJgQA/K0fMyJroi8Pz7PVcYY1X9LBrfwtyaVFMBQj5+/km+jzWiX+vojb+B0oI1 Yibg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=oyelkEXO; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.31 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 morse.vger.email (morse.vger.email. [23.128.96.31]) by mx.google.com with ESMTPS id im15-20020a170902bb0f00b001c62b9d56f8si3394806plb.606.2023.09.26.17.44.31 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 26 Sep 2023 17:44:32 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.31 as permitted sender) client-ip=23.128.96.31; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=oyelkEXO; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.31 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 morse.vger.email (Postfix) with ESMTP id 358EB821148B; Tue, 26 Sep 2023 15:46:06 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at morse.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231561AbjIZWp5 (ORCPT + 99 others); Tue, 26 Sep 2023 18:45:57 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36314 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231582AbjIZWn4 (ORCPT ); Tue, 26 Sep 2023 18:43:56 -0400 Received: from mail-il1-x129.google.com (mail-il1-x129.google.com [IPv6:2607:f8b0:4864:20::129]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 915B81BE5 for ; Tue, 26 Sep 2023 14:00:05 -0700 (PDT) Received: by mail-il1-x129.google.com with SMTP id e9e14a558f8ab-351367c1c24so13655ab.1 for ; Tue, 26 Sep 2023 14:00:05 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1695762005; x=1696366805; 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=TA45zhytFPggJ/KNc3nT/s2rZmIFeSaR3+xLqtcK0a4=; b=oyelkEXOrVN4kgQ2Y+/AjDXZOeYSXja0GiLIWcu97fTGoD6ajiIUNYoWWaJ3vjE9s0 VOvGgeIsh9I8Q5cn2e3gswt3A5jCewomhdodu/7ez2yUxsSOy2GavMOxVlcLFe8Wyk+q +ZzPhhUwQDKiXF6bnjm2X3X0Dvc+xVkDL0atxPn91y7GT2+LY7eyKmQ5UYOW6SoKu9XG GOJZwj5U9lfEZroUs4YiNJa57qs6sYsA/kp0zTJojB7WPZJPnpAUknU8YeL5eXTIxJ16 fA9qAzFxPeCJX86I2Ij2k4HxJhOzXvgBPmxDT3Cj2Gl48PU0xFbOUJufBAwoHfqjXXpt rcgA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1695762005; x=1696366805; 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=TA45zhytFPggJ/KNc3nT/s2rZmIFeSaR3+xLqtcK0a4=; b=RgfXfQJdqEWdNm2G4mh++iJWT+PEs1Eh0dU9CcQVnYiOkbLfuojz4tqI7kGI+YTOpp w6IKpGzoqmaFs2AtHECki4/C/4tpFstCMLztz9Xoj+Iz+wLQ5NeIVIHus8vNSWAbBZk4 nW5GFNQbeUgndL9Y7E5mhgik7jucfhxCx1YzQHaQHWJ1WicKKGysFoJps8pVHy9FPyEo sPW+247CDZ7rhVuCfGUvvOup/BcAB8+NiH/9SYrTfi0NCKJnw2aXBGgmApb1MCEfD7pn s4Qo2m31p6xQcDerT45mRf/jRgTVAeGO5e5mxAFV/ACyV4PSA5g5RcuUwgXQXCDPxlBM Ea7w== X-Gm-Message-State: AOJu0YwCsUJLh8brGmxdpuRSY1nSxO7JEBzHlcEzuqM42ADSD8Bemmai xq3wIBXqpxvgIayYB1tYDKQp3f+Qn5ldCkVVliz1 X-Received: by 2002:a92:c562:0:b0:351:4dad:4738 with SMTP id b2-20020a92c562000000b003514dad4738mr367127ilj.9.1695762004652; Tue, 26 Sep 2023 14:00:04 -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: From: Jeffrey Kardatzke Date: Tue, 26 Sep 2023 13:59:50 -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=-8.4 required=5.0 tests=DKIMWL_WL_MED,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_IN_DEF_DKIM_WL autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on morse.vger.email 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 (morse.vger.email [0.0.0.0]); Tue, 26 Sep 2023 15:46:06 -0700 (PDT) Hans, I've been looking through the v4l2/vbuf2 code to get an idea of the details for implementing a new memory type for secure buffers. What it comes down to essentially is that it would behave just like V4L2_MEMORY_DMABUF, but then there would be an extra check in __prepare_dmabuf (in videobuf2-core.c) when the memory type is SECURE to ensure that it is actually from a secure dma-buf allocation. So I'm thinking an alternate solution might be cleaner so we don't have two memory types that are handled nearly identically in most of the code. What do you think about a new memory flag like V4L2_MEMORY_FLAG_SECURE? This would be set in vb2_queue struct like the other existing memory flag. Then when it gets into __prepare_dmabuf and invokes attach_dmabuf on each buffer...that call could then check for the existence of that flag, and if it's there it could validate it is actually secure memory. Then in various other dmabuf vb2_mem_ops (maybe alloc, get_userptr, vaddr and mmap) those could also check for the secure flag, and if present return an error/null. Then also in the driver specific vb2_ops for queue_setup, the MTK driver could recognize the flag there and then configure itself for secure mode. How does that sound as an overall strategy? Cheers, Jeff On Mon, Sep 25, 2023 at 9:51=E2=80=AFAM Jeffrey Kardatzke wrote: > > 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 usin= g > > >>>> 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). Tha= t > > >>>> 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 t= he > > >>>> 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 a= re > > >>>> 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) an= d > > >>>> have internal > > >>>>> allocation for reconstruction buffer and some hw specific referen= ce > > >>>> 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 driv= er > > >>>> 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 = non- > > >>> secure are the same, whether just need to add one memory type in v4= l2 > > >>> 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, = who > > >> will check that it can't write the data to that fd? Since doing so w= ould > > >> expose the video. Presumably at some point the tee code will prevent= that? > > >> (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 t= he > > >> driver that 1) it can expect secure dmabuf fds, 2) it can configure = itself > > >> for that (that avoids using a control to toggle between normal and s= ecure mode), > > >> and at VIDIOC_QBUF time it is easy for the V4L2 core to verify that = the > > >> 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, = but > > >> it catches userspace errors early on. > > >> > > >> Also, while for this hardware the DMA operation is the same, that mi= ght > > >> 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 tha= t > > > 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 i= n > > > 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 > > >>> > > >> > >