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

RE: Xen Memory Sharing Query


  • To: Andrew Cooper <Andrew.Cooper3@xxxxxxxxxx>, "xen-devel@xxxxxxxxxxxxxxxxxxxx" <xen-devel@xxxxxxxxxxxxxxxxxxxx>
  • From: Marc Bonnici <Marc.Bonnici@xxxxxxx>
  • Date: Mon, 18 Jul 2022 13:40:11 +0000
  • Accept-language: en-GB, en-US
  • Arc-authentication-results: i=2; mx.microsoft.com 1; spf=pass (sender ip is 63.35.35.123) smtp.rcpttodomain=lists.xenproject.org smtp.mailfrom=arm.com; dmarc=pass (p=none sp=none pct=100) action=none header.from=arm.com; dkim=pass (signature was verified) header.d=armh.onmicrosoft.com; arc=pass (0 oda=1 ltdi=1 spf=[1,1,smtp.mailfrom=arm.com] dkim=[1,1,header.d=arm.com] dmarc=[1,1,header.from=arm.com])
  • Arc-authentication-results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=arm.com; dmarc=pass action=none header.from=arm.com; dkim=pass header.d=arm.com; arc=none
  • Arc-message-signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=rOirScCeSzXvj4um928qzdIzv7TtuIW2pHcKkfkWtFI=; b=XPbGwoMlNSHsTkyiEJRHZQSePeC1HChncLVL5eD9hYHZNQQHbhPTkXNcO4YSL6kP3wIuJ9ZZWxKIsLccuWBb1Juk44xjcFlJvsbgbDlmYq9aHMOQx6ddT7DYTV8xNrL072E0KXwQtxsVFKPvtDK+eSaYVSI7LZ472wP9Ni/S4RxkG+Rv6etSNb+QtwtMmM/xuxPqdvVWzuNSXwyd4+TOxEI5ElD04m81JkfZZ+ubQtmEjMNrcxDrN0iB8/fnzgGVqy7QnDce2sH8DtwoYGN24bLiXUXdKrw6jxLpQBCydXZcg+AG22qeInOd7nidYNgh0dk9Zk+imhpRpEq/ry7LEw==
  • 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-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=rOirScCeSzXvj4um928qzdIzv7TtuIW2pHcKkfkWtFI=; b=TQb7H/d1SHxqvyjhbycAEGuRAu1epX3CEbWD8V4xPzl3DLFVYxZdR26xkLSrgGg61ZSecJgH9W0xo4EaBkNwBdPGobHsNfON9wAt/6VBpOV9cVZXI66fr1jBwJ9TkVtAfnF/3lzurNrAFvDKp5nP9K1GTbI36pGOTulEkpfjVd1V34hSry2fwDozx0CRVz3z/+zGyRVlRnofYfGfVVssZ1Q0FsFFHTVI3YyjGyKL+xUMuB307vzE5bxgOWE2NH9EkwCEMOqwmQpQ9Ckpswgwg/UD270HBVvdHiB5yps7D/GmeIQE2paQXqOvRfFuCGWCbTKOxe1MwVLy/zpmtyCAKQ==
  • Arc-seal: i=2; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=pass; b=F1mBLisaIzBcaR7+8sZc2Hw68ctVPKDj/tcThv5OPn/2Ox7nTUs6R4Qd/Vqe9J0k6DbNeRh0NM6lZUrRnpZBo4QEhpOia4QAle4Ydbg8d8zk5Qqflc/RXW9E0+DIVIE5wjyjmRv/2I6PjokzMOwMNQ/9JfQg2bw7Hg9O6V3qrM8QJ2DBrQ5hcC06SsM7O/6Q7F4m3hGJ4MHU40LgxPSYG2oHOUwcoeeXTjKYrydx9cWEo33ob6HkPrXvEEoIrm0fGTywufzN+3ZjtmRgEkdiGu2oUYzjUiVi8Iftjg9C8moOaBtL0q39H6XQxmEVgjfQZc93PRsopP7QG/5cDkFeYw==
  • Arc-seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=IYnZptiSLpqnZX0emNTvrI+JSjSaZmRYNPjVcjoPRYs6X/bVlRX1J9P8YGKGQCDqVCTawGuadV3fHQXB7zjz1g+sUQ4KL4s6Nws6bVBmPn6vU2ZRwqp4963hUfR1Iy8+rnFqqkJB/yktCvH01syQwEfiqssd1GaB4mFgjATjVkaekqX/NtSa4+TkQYHO9C3I/3DhorPMjSfyjp+R4BuoyWVkIk2yxS4qyCICFWUZ98OG03qEg12p8b540AgZZhtqhfvI/h3qIGIKl7rvFif+T2H5/dLPMfk2elDkUQebAxt8/iRbclwZj8TgVG6PmO1jPHCU9xTWXEtnExurf0QUmQ==
  • Authentication-results-original: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=arm.com;
  • Delivery-date: Mon, 18 Jul 2022 13:40:31 +0000
  • List-id: Xen developer discussion <xen-devel.lists.xenproject.org>
  • Nodisclaimer: true
  • Original-authentication-results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=arm.com;
  • Thread-index: AdiXcIvKzj1u3ooYRvOwLKlCsLdCEAA/89UAAIdcHqA=
  • Thread-topic: Xen Memory Sharing Query

Hi Andrew,

Thank you very much for your detailed reply, that make things 
a lot clearer. I did have a few follow up questions.

> gnttab v2 is horribly more complicated.  

With v2, do the high level behaviours change much from 
what you have outlined here? Do you expect in the long term 
for v2 to be the preferred implementation or are they more like 
alternatives?


> While a gref is mapped, domA is not permitted to edit the 
> associated entry.  

So IIUC once the gref is mapped then domA can't make any changes 
to the entry at all, (or at least they won't be reflected). So if 
domA wants to make any modifications (extend the shared memory 
region, change permissions etc.), then it would just create another
entry and share the new gref? 


> From a grant perspective, Xen doesn't enforce any policy.  domA's grefs
> can be mapped in the manner permitted by what domA wrote into the grant
> table.

So does this mean that if domA grants access to domB for a given frame, 
and then added a new entry in its grant table with the same frame details 
but with "domid = domC" instead. This would be allowed? And if so, would this 
result in a 3-way shared buffer?

And finally a similar scenario, if a frame was shared from domA to domB, 
would domB be able to add an entry in its own grant table to share the 
same frame with domC and end up with the same outcome?

Thanks

Kind Regards
Marc Bonnici

> -----Original Message-----
> From: Andrew Cooper <Andrew.Cooper3@xxxxxxxxxx>
> Sent: 15 July 2022 18:29
> To: Marc Bonnici <Marc.Bonnici@xxxxxxx>; xen-
> devel@xxxxxxxxxxxxxxxxxxxx
> Subject: Re: Xen Memory Sharing Query
> 
> On 15/07/2022 16:56, Marc Bonnici wrote:
> > Hi All,
> >
> > I was wondering if someone could help me understand some of the
> rules of the
> > memory sharing implementation in Xen?
> >
> > Specifically I'm looking to understand what restrictions Xen
> places on
> > granting access from one Dom to another from Xen's perspective,
> and what types
> > of grant requests would be allowed/rejected by Xen?
> >
> > I.e. How would the situation be handled if the same frame of
> memory was attempted
> > to be shared multiple times?
> >
> > As an example scenario, DomA shares 1 physical page of memory in a
> transaction
> > with DomB. And then without releasing any memory, DomA attempts to
> share
> > another region of memory, which includes the same physical page of
> the previous share
> > with DomB again. This would result in two concurrent shares
> containing an overlap.
> >
> > Apologies if I've missed something but is there any documentation
> / threat model
> > that would cover these types of scenarios? So far I have been
> trying to read through
> > the code but was wondering if there is something else I could
> refer to help me
> > with my understanding?
> 
> There's nothing adequately written down.  It ought to live in sphinx
> docs, but my copious free time is non-existent for speculative
> security
> reasons.
> 
> This all pertains to gnttab v1 which is the only supported one on
> ARM
> right now.  gnttab v2 is horribly more complicated.  Refer to
> https://github.com/xen-
> project/xen/blob/master/xen/include/public/grant_table.h#L132-L186
> 
> When DomA and DomB are set up and running, they each have a grant
> table.  The grant table is some shared memory (of Xen's) mapped into
> the
> guest, and is a bidirectional communication interface between the
> guest
> kernel and Xen.
> 
> The guest kernel logically owns the grant table, and it's a simple
> array
> of grant entries.  Entries 0 thru 7 are reserved for system use, and
> indeed two entries (one for xenstore, one for xenconsole) are set up
> on
> the guest kernel's behalf by the domain builder.  Entries 8 thru $N
> are
> entirely under the guest's control.
> 
> A guest kernel (domA) creates a grant by filling in a grant table
> entry,
> and passing the grant reference (the entry's index in the table) to
> some
> other entity in the system (in this case, domB).
> 
> The grant table entry is formed of:
> 
> u16 flags
> u16 domid
> u32 frame
> 
> so for domA to grant a frame to domB, it would pick a free gref (any
> entry in the table with flags=0) and fill in:
> 
> frame = f
> domid = domB
> smp_wmb()
> flags = GTF_permit_access (think "grant usable")
> 
> GFT_readonly is another relevant flag that domA might choose to set.
> 
> Then, domB would take the gref it has been given by domA, and make a
> gnttab_op_map() hypercall, passing {domA, gref} as an input.
> 
> Xen looks up gref in domA's grant table, checks e.g. domA granted
> access
> to domB, and if everything is happy, sets the the
> GFT_{reading,writing}
> flags (as appropriate) in flags.  This tells domA that the grant is
> currently mapped readably and/or writeably.
> 
> Later, when domB unmaps the grant, Xen clears the
> GFT_{reading,writing}
> bits, telling domA that the grant is no longer in use.
> 
> DomA then clears GTF_permit_access to mark this as gref as invalid,
> and
> can then free the frame.
> 
> 
> Now, that's the simple overview.  To answer some of your specific
> questions:
> 
> DomA is perfectly free to grant away the same frame multiple times.
> DomA does this by writing multiple different grefs with the same
> frame
> field.  These grefs could be to the same, or different domains, and
> can
> have any (valid) combination of flags.
> 
> DomB is perfectly free to map the same gref multiple times.  This is
> actually a necessity for x86 PV guests, because of how we reference
> count pagetable entries.  It is not necessary for any kind of guest
> of
> HVM guest (x86 or ARM) because of how guest physical address space
> works.
> 
> IMO it should have been restricted when the HVM ABI was designed,
> but
> alas.  In practice, Xen has an internal refcount which prevents a
> gref
> being mapped more than 127 times IIRC.
> 
> While a gref is mapped, domA is not permitted to edit the associated
> entry.  Doing so shouldn't cause a security violation (Xen has a
> local
> copy of the entry in the maptrack table), but will at least confuse
> diagnostics of the granted state.
> 
> Importantly, and what may come as a surprise, is that domA has no
> way to
> revoke a currently-mapped grant.  Fixing this limitation has been
> discussed several times; there are some very complicated corner
> cases,
> and I'm not aware of any work having started in earnest.
> 
> Xen does have logic to unmap grants of VMs which have shut down (for
> whatever reason) with grants still mapped.  This prevents deadlocks
> (e.g. two domains grant to each other, then both crash deliberately).
> 
> 
> From a grant perspective, Xen doesn't enforce any policy.  domA's
> grefs
> can be mapped in the manner permitted by what domA wrote into the
> grant
> table.
> 
> If you want to get into policies that Xen may enforce, that would be
> a
> discussion about XSM, Xen Security Modules.
> 
> Does any of this help?
> 
> ~Andrew

 


Rackspace

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