[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [PATCH] optee: immediately free buffers that are released by OP-TEE


  • To: "julien@xxxxxxx" <julien@xxxxxxx>, "xen-devel@xxxxxxxxxxxxxxxxxxxx" <xen-devel@xxxxxxxxxxxxxxxxxxxx>
  • From: Volodymyr Babchuk <Volodymyr_Babchuk@xxxxxxxx>
  • Date: Mon, 18 May 2020 02:04:49 +0000
  • Accept-language: en-US
  • Arc-authentication-results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=epam.com; dmarc=pass action=none header.from=epam.com; dkim=pass header.d=epam.com; arc=none
  • Arc-message-signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=DXvAl7S+oQcvO3GhrU89tCqWr+yWlU2u6vae9W0gSJ4=; b=CP/ExeLcSCZKG8CETO95h2KRwkTsccSlEALCAsQUjjUE6yfuS15XttRU8zkGi1MJjG61pGSnLS/5Flx5pEUBWIgoIwLFmLr6URPT+u8sihMkbPpZeAtWRGE/ZiSkgpAtY0Y2elr5CTI26JpHDuIBR2JRGUKmIFBtuiDPCMwEXGVeughTJqy65upDefvL9REf5OpwDcSy1vIoyC4+Q7DIdaAtTPO3+GpalNLz79VdqNjYbs/CHzsg62wvbAWTU56RIi2dnRrmVUJzXB02VevJeRSgDs8adsUvghezzrflCQiXkyKwjq3QRtij5gouX9M3k3+I8om9nLO3FF7UBTL02A==
  • Arc-seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=YSlrXsz99LmPn+NccvlaJuWMrx8GY8y/TpHXLRWvLg7eh3pjbaT35GBVc/UsI5UdeEdkH/07prH1RGLc4p/Y3SbWDXxDkaEPMI1cJIanip63ytRVw4478nvuV4nww4s2wesjPJZ/XlHYApPBQsrmieO7YdzdKzP/+um7XBDcQn8yzH7bfExWIw2W/eSOPkmGrj2j0csWIx3vvc+GS6OKAIiPQVQFIgRobAW2FEPnGgMU7HSnOYhdc6cBW7EF2hxv5cJsgP+/vjQw+JTgz/Qc2M1m/pHgGGWMvUn+5lN7+2BQYgvwFFgaHljEWXAe5B1YwvANRuYNQa5HMEzHd2fYaQ==
  • Authentication-results: xen.org; dkim=none (message not signed) header.d=none;xen.org; dmarc=none action=none header.from=epam.com;
  • Cc: "tee-dev@xxxxxxxxxxxxxxxx" <tee-dev@xxxxxxxxxxxxxxxx>, "sstabellini@xxxxxxxxxx" <sstabellini@xxxxxxxxxx>
  • Delivery-date: Mon, 18 May 2020 02:05:03 +0000
  • List-id: Xen developer discussion <xen-devel.lists.xenproject.org>
  • Thread-index: AQHWI0fTRy+rlPvmsUKZMLlR49t2iaiiqEuAgAqCkIA=
  • Thread-topic: [PATCH] optee: immediately free buffers that are released by OP-TEE

Hi Julien,

On Mon, 2020-05-11 at 10:34 +0100, Julien Grall wrote:
> Hi Volodymyr,
> 
> On 06/05/2020 02:44, Volodymyr Babchuk wrote:
> > Normal World can share buffer with OP-TEE for two reasons:
> > 1. Some client application wants to exchange data with TA
> > 2. OP-TEE asks for shared buffer for internal needs
> > 
> > The second case was handle more strictly than necessary:
> > 
> > 1. In RPC request OP-TEE asks for buffer
> > 2. NW allocates buffer and provides it via RPC response
> > 3. Xen pins pages and translates data
> > 4. Xen provides buffer to OP-TEE
> > 5. OP-TEE uses it
> > 6. OP-TEE sends request to free the buffer
> > 7. NW frees the buffer and sends the RPC response
> > 8. Xen unpins pages and forgets about the buffer
> > 
> > The problem is that Xen should forget about buffer in between stages 6
> > and 7. I.e. the right flow should be like this:
> > 
> > 6. OP-TEE sends request to free the buffer
> > 7. Xen unpins pages and forgets about the buffer
> > 8. NW frees the buffer and sends the RPC response
> > 
> > This is because OP-TEE internally frees the buffer before sending the
> > "free SHM buffer" request. So we have no reason to hold reference for
> > this buffer anymore. Moreover, in multiprocessor systems NW have time
> > to reuse buffer cookie for another buffer. Xen complained about this
> > and denied the new buffer registration. I have seen this issue while
> > running tests on iMX SoC.
> > 
> > So, this patch basically corrects that behavior by freeing the buffer
> > earlier, when handling RPC return from OP-TEE.
> > 
> > Signed-off-by: Volodymyr Babchuk <volodymyr_babchuk@xxxxxxxx>
> > ---
> >   xen/arch/arm/tee/optee.c | 24 ++++++++++++++++++++----
> >   1 file changed, 20 insertions(+), 4 deletions(-)
> > 
> > diff --git a/xen/arch/arm/tee/optee.c b/xen/arch/arm/tee/optee.c
> > index 6a035355db..af19fc31f8 100644
> > --- a/xen/arch/arm/tee/optee.c
> > +++ b/xen/arch/arm/tee/optee.c
> > @@ -1099,6 +1099,26 @@ static int handle_rpc_return(struct optee_domain 
> > *ctx,
> >           if ( shm_rpc->xen_arg->cmd == OPTEE_RPC_CMD_SHM_ALLOC )
> >               call->rpc_buffer_type = shm_rpc->xen_arg->params[0].u.value.a;
> >   
> > +        /*
> > +         * OP-TEE signals that it frees the buffer that it requested
> > +         * before. This is the right for us to do the same.
> > +         */
> > +        if ( shm_rpc->xen_arg->cmd == OPTEE_RPC_CMD_SHM_FREE )
> > +        {
> > +            uint64_t cookie = shm_rpc->xen_arg->params[0].u.value.b;
> > +
> > +            free_optee_shm_buf(ctx, cookie);
> > +
> > +            /*
> > +             * This should never happen. We have a bug either in the
> > +             * OP-TEE or in the mediator.
> > +             */
> > +            if ( call->rpc_data_cookie && call->rpc_data_cookie != cookie )
> > +                gprintk(XENLOG_ERR,
> > +                        "Saved RPC cookie does not corresponds to OP-TEE's 
> > (%"PRIx64" != %"PRIx64")\n",
> 
> s/corresponds/correspond/
Will fix in the next version.

> > +                        call->rpc_data_cookie, cookie);
> 
> IIUC, if you free the wrong SHM buffer then your guest is likely to be 
> running incorrectly afterwards. So shouldn't we crash the guest to avoid 
> further issue?
> 

Well, we freed the exact buffer that OP-TEE asked us to free. So guest
didn't anything bad. Moreover, optee driver in Linux kernel does not
have similar check, so it will free this buffer without any complains. 
I'm just being overcautious here. Thus, I see no reason to crash the
guest.

 


Rackspace

Lists.xenproject.org is hosted with RackSpace, monitoring our
servers 24x7x365 and backed by RackSpace's Fanatical Support®.