|
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] Re: [PING] Re: [PATCH] xen/arm: optee: Allocate anonymous domheap pages
Hi Stefano, On 07/10/2021 23:14, Stefano Stabellini wrote: On Thu, 7 Oct 2021, Volodymyr Babchuk wrote:Hi Stefano, Stefano Stabellini <sstabellini@xxxxxxxxxx> writes:On Wed, 6 Oct 2021, Oleksandr wrote:Many thanks for the ping, this patch fell off my radar.Hello all Gentle reminder.On 23.09.21 23:57, Volodymyr Babchuk wrote:Hi Stefano, Stefano Stabellini <sstabellini@xxxxxxxxxx> writes:On Mon, 6 Sep 2021, Oleksandr Tyshchenko wrote:From: Oleksandr Tyshchenko <oleksandr_tyshchenko@xxxxxxxx> Allocate anonymous domheap pages as there is no strict need to account them to a particular domain. Since XSA-383 "xen/arm: Restrict the amount of memory that dom0less domU and dom0 can allocate" the dom0 cannot allocate memory outside of the pre-allocated region. This means if we try to allocate non-anonymous page to be accounted to dom0 we will get an over-allocation issue when assigning that page to the domain. The anonymous page, in turn, is not assigned to any domain. CC: Julien Grall <jgrall@xxxxxxxxxx> Signed-off-by: Oleksandr Tyshchenko <oleksandr_tyshchenko@xxxxxxxx> Acked-by: Volodymyr Babchuk <volodymyr_babchuk@xxxxxxxx>Only one question, which is more architectural: given that these pages are "unlimited", could the guest exploit the interface somehow to force Xen to allocate an very high number of anonymous pages? E.g. could a domain call OPTEE_SMC_RPC_FUNC_ALLOC in a loop to force Xen to exaust all memory pages?Generally, OP-TEE mediator tracks all resources allocated and imposes limits on them. OPTEE_SMC_RPC_FUNC_ALLOC case is a bit different, because it is issued not by domain, but by OP-TEE itself. As OP-TEE is more trusted piece of system we allow it to request as many buffers as it wants. Also, we know that OP-TEE asks only for one such buffer per every standard call. And number of simultaneous calls is limited by number of OP-TEE threads, which is quite low: typically only two.So let me repeat it differently to see if I understood correctly: - OPTEE_SMC_RPC_FUNC_ALLOC is only called by OP-TEE, not by the domain - OPTEE is trusted and only call it twice anywayCorrect.I am OK with this argument, but do we have a check to make sure a domU cannot issue OPTEE_SMC_RPC_FUNC_ALLOC?domU can't issue any RPC, because all RPCs are issued from OP-TEE side. This is the nature of RPC - OP-TEE requests Normal World for some service. But of course, Normal World can perform certain actions that will make OP-TEE to issue a RPC. I discuss this in depth below.Looking at the patch, there are other two places, in addition to OPTEE_SMC_RPC_FUNC_ALLOC, where the anonymous memory pages can be allocated: 1) copy_std_request 2) translate_noncontig We need to prove that neither 1) or 2) can result in a domU exausting Xen memory. In the case of 1), it looks like the memory is freed before returning to the DomU, right? If so, it should be no problem?Yes, mediator makes shadow copy of every request buffer to hide translated addresses from the guest. Number of requests is limited by number of OP-TEE threads.In the case of 2), it looks like the memory could outlive the call where it is allocated. Is there any kind of protection against issuing something like OPTEE_MSG_ATTR_TYPE_TMEM_INOUT in a loop? Is it OP-TEE itself that would refuse the attempt? Thus, the idea is that do_call_with_arg will return error and we'll just free the memory there?Well, translate_noncontig() calls allocate_optee_shm_buf() which counts all allocated buffers. So you can't call it more than MAX_SHM_BUFFER_COUNT times, without de-allocating previous memory. But, thanks to your question, I have found a bug there: memory is not freed if allocate_optee_shm_buf() fails. I'll prepare patch later today.I cannot see a check for errors returned by do_call_with_arg and memory freeing done because of that. Sorry I am not super familiar with the code, I am just trying to make sure we are not offering to DomUs an easy way to crash the system. Not really. The worst outcome is still a DoS of the host because we don't pre-allocate memory or even check that the total allocation will not exhaust the memory. The only difference is I would argue this would be a misconfiguration of the system. But if the allocations are using anonymous memory, then the whole platform might run out of memory. We have issued XSAs for things like that in the past. This is why I am worried about this patch: if we apply it we really become reliant on these limits being implemented correctly. A bug can have much more severe consequences. This is not a problem specific to OP-TEE. Any anymous allocation (xmalloc,...) done in Xen on behalf of the guest has, in theory, the same problem (see more below). As you are the maintainer for this code, and this code is not security supported, I'll leave it up to you (also see the other email about moving optee to "supported, not security supported"). However, maybe a different solution would be to increase max_pages for a domain when optee is enabled? Maybe just by a few pages (as many as needed by the optee mediator)? Because if we did that, we wouldn't risk exposing DOS attack vectors for every bug in the mediator limits checks. I think we need to differentiate two sorts of allocation: 1) Memory used by Xen on behalf of the guest 2) Memory used by the guest itselfd->max_pages is only meant to refer to the latter (in fact, a guest can balloon memory up to d->max_pages). In this case, we are discussing about the latter and therefore I think the should be accounted differently as the memory is not exposed to the guest. Today, Xen doesn't have this facility. I know this has been discussed a few times in the past, but AFAIK, a patch series never materialized for it. However, to me, this sounds more an hardening work for the whole Xen rather than OP-TEE itself. So I think the patch provided by Oleksandr is probably the best way to go for this release.
Regardless what I wrote above, this change would be incorrect because TEE is initialized the when domain is created. However, d->max_pages is set afterwards via DOMCTL_max_mem, so the value will get overridden. However, I don't think OP-TEE code should modify d->max_pages. Instead, this should be accounted by the toolstack (or domain_build for dom0/domU created by Xen). Cheers, -- Julien Grall
|
![]() |
Lists.xenproject.org is hosted with RackSpace, monitoring our |