xen crash in tmem: checking a xen pfn for domain ownership
Does the construct:
xen_pfn_t gpfn;
p2m_type_t t;
unsigned long mfn;
mfn = mfn_x(gfn_to_mfn(current->domain, gpfn, &t));
if (t != p2m_ram_rw || cli_mfn == INVALID_MFN)
return NULL; /* bad */
return map_domain_page(mfn)somehow check to ensure that pfn belongs to current->domain? (See cli_mfn_to_va() in common/tmem_xen.c.)
If not, is there an easy way to perform that check? (preferably one that works for both HVM and PV guests)
In debugging a tmem Linux-side guest patch, I discovered that a bad mfn passed by the guest can crash Xen and I think this assumption might be the problem.
Thanks,
Dan
If you could be doing memory sharing then you might need to use gfn_to_mfn_unshare()? Otherwise it looks pretty plausible, and that one flaw is pretty minor as you're probably not using memshr.
-- Keir
Quoted 23 lines · Dan Magenheimer
Does the construct:
xen_pfn_tgpfn;
p2m_type_tt;
unsigned long mfn;mfn = mfn_x(gfn_to_mfn(current->domain, gpfn, &t)); if (t != p2m_ram_rw || cli_mfn == INVALID_MFN) return NULL; /* bad */ return map_domain_page(mfn)somehow check to ensure that pfn belongs to
current->domain? (Seecli_mfn_to_va()incommon/tmem_xen.c.)If not, is there an easy way to perform that check? (preferably one that works for both HVM and PV guests)
In debugging a tmem Linux-side guest patch, I discovered that a bad mfn passed by the guest can crash Xen and I think this assumption might be the problem.
Thanks,
Dan
Thanks for the reply, but I'm not sure I understand.
Ignore memory sharing for now...
Are you saying, yes, the ownership check IS performed? E.g. if gpfn is a random number, NULL will always be returned (unless of course the random number happens to be a valid gfn for current->domain)?
Or are you saying its plausible that this IS the problem (that I am not checking for ownership)?
Now bring memory sharing back in...
Since tmem and memory sharing are supposed to be complementary (though I don't think anybody has ever tried using both together), are you saying I should change this one call from gfn_to_mfn() to gfn_to_mfn_unshare() for some reason (e.g. maybe to avoid a race)? Note
that this code is just getting a virtual address
to copy a page to/from the guest.
Thanks,
Dan
Quoted 42 lines
-----Original Message-----
From: Keir Fraser [mailto:[email protected]]
Sent: Friday, September 17, 2010 10:35 AM
To: Dan Magenheimer; Jan Beulich
Cc: Xen-devel
Subject: Re: xen crash in tmem: checking a xen pfn for domain ownershipIf you could be doing memory sharing then you might need to use
gfn_to_mfn_unshare()? Otherwise it looks pretty plausible, and that one flaw
is pretty minor as you're probably not using memshr.-- Keir
Quoted 23 lines · Dan Magenheimer
Does the construct:
xen_pfn_tgpfn;
p2m_type_tt;
unsigned long mfn;mfn = mfn_x(gfn_to_mfn(current->domain, gpfn, &t)); if (t != p2m_ram_rw || cli_mfn == INVALID_MFN) return NULL; /* bad */ return map_domain_page(mfn)somehow check to ensure that pfn belongs to
current->domain? (Seecli_mfn_to_va()incommon/tmem_xen.c.)If not, is there an easy way to perform that check? (preferably one that works for both HVM and PV guests)
In debugging a tmem Linux-side guest patch, I discovered that a bad mfn passed by the guest can crash Xen and I think this assumption might be the problem.
Thanks,
Dan
Gfn_to_mfn() takes a domain as a parameter. It looks up gfn in that domain's p2m. The only RAM-typed pfns that can be present in a domain's p2m, if it is not sharing pages via memshr, are the domain's own pages. As far as I know, at least. It does no harm for you to switch to gfn_to_mfn_unshare(), but I doubt this is the fix for your current problem.
-- Keir
Quoted 67 lines · Dan Magenheimer
Thanks for the reply, but I'm not sure I understand.
Ignore memory sharing for now...
Are you saying, yes, the ownership check IS performed? E.g. if gpfn is a random number, NULL will always be returned (unless of course the random number happens to be a valid gfn for
current->domain)?Or are you saying its plausible that this IS the problem (that I am not checking for ownership)?
Now bring memory sharing back in...
Since tmem and memory sharing are supposed to be complementary (though I don't think anybody has ever tried using both together), are you saying I should change this one call from
gfn_to_mfn()togfn_to_mfn_unshare()for some reason (e.g. maybe to avoid a race)? Note
that this code is just getting a virtual address
to copy a page to/from the guest.Thanks,
DanQuoted 42 lines
-----Original Message-----
From: Keir Fraser [mailto:[email protected]]
Sent: Friday, September 17, 2010 10:35 AM
To: Dan Magenheimer; Jan Beulich
Cc: Xen-devel
Subject: Re: xen crash in tmem: checking a xen pfn for domain ownershipIf you could be doing memory sharing then you might need to use
gfn_to_mfn_unshare()? Otherwise it looks pretty plausible, and that one flaw
is pretty minor as you're probably not using memshr.-- Keir
Quoted 23 lines · Dan Magenheimer
Does the construct:
xen_pfn_tgpfn;
p2m_type_tt;
unsigned long mfn;mfn = mfn_x(gfn_to_mfn(current->domain, gpfn, &t)); if (t != p2m_ram_rw || cli_mfn == INVALID_MFN) return NULL; /* bad */ return map_domain_page(mfn)somehow check to ensure that pfn belongs to
current->domain? (Seecli_mfn_to_va()incommon/tmem_xen.c.)If not, is there an easy way to perform that check? (preferably one that works for both HVM and PV guests)
In debugging a tmem Linux-side guest patch, I discovered that a bad mfn passed by the guest can crash Xen and I think this assumption might be the problem.
Thanks,
Dan
Oh, depending on what you want to do with the page you may well want to get_page(current->domain, page). You don't hold a lock on the domain's p2m, so page ownerships can change under your feet, and hence getting a reference to the page, and checking the page's ownership at the same time, might be wise. And if you want to modify the page you should probably use get_page_and_type(..., PGT_writable_page).
-- Keir
Quoted 84 lines · Keir Fraser
Gfn_to_mfn()takes a domain as a parameter. It looks up gfn in that domain's p2m. The only RAM-typed pfns that can be present in a domain's p2m, if it is not sharing pages via memshr, are the domain's own pages. As far as I know, at least. It does no harm for you to switch togfn_to_mfn_unshare(), but I doubt this is the fix for your current problem.-- Keir
Quoted 22 lines · Dan Magenheimer
Thanks for the reply, but I'm not sure I understand.
Ignore memory sharing for now...
Are you saying, yes, the ownership check IS performed? E.g. if gpfn is a random number, NULL will always be returned (unless of course the random number happens to be a valid gfn for
current->domain)?Or are you saying its plausible that this IS the problem (that I am not checking for ownership)?
Now bring memory sharing back in...
Since tmem and memory sharing are supposed to be complementary (though I don't think anybody has ever tried using both together), are you saying I should change this one call from
gfn_to_mfn()togfn_to_mfn_unshare()for some reason (e.g. maybe to avoid a race)? Note
that this code is just getting a virtual address
to copy a page to/from the guest.Quoted 45 lines
Thanks,
DanQuoted 42 lines
-----Original Message-----
From: Keir Fraser [mailto:[email protected]]
Sent: Friday, September 17, 2010 10:35 AM
To: Dan Magenheimer; Jan Beulich
Cc: Xen-devel
Subject: Re: xen crash in tmem: checking a xen pfn for domain ownershipIf you could be doing memory sharing then you might need to use
gfn_to_mfn_unshare()? Otherwise it looks pretty plausible, and that one flaw
is pretty minor as you're probably not using memshr.-- Keir
Quoted 23 lines · Dan Magenheimer
Does the construct:
xen_pfn_tgpfn;
p2m_type_tt;
unsigned long mfn;mfn = mfn_x(gfn_to_mfn(current->domain, gpfn, &t)); if (t != p2m_ram_rw || cli_mfn == INVALID_MFN) return NULL; /* bad */ return map_domain_page(mfn)somehow check to ensure that pfn belongs to
current->domain? (Seecli_mfn_to_va()incommon/tmem_xen.c.)If not, is there an easy way to perform that check? (preferably one that works for both HVM and PV guests)
In debugging a tmem Linux-side guest patch, I discovered that a bad mfn passed by the guest can crash Xen and I think this assumption might be the problem.
Thanks,
Dan_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xensource.com/xen-devel
Quoted 7 lines
Quoted 1 line
Quoted 1 line · Keir Fraser
Oh, depending on what you want to do with the page you may well want to
get_page(current->domain, page). You don't hold a lock on the domain's p2m, so page ownerships can change under your feet, and hence getting a reference to the page, and checking the page's ownership at the same time, might be wise. And if you want to modify the page you should probably useget_page_and_type(..., PGT_writable_page).
That's particularly important for the pv case, where gfn_to_mfn() is a no-op.
Jan
Quoted 10 lines · Jan Beulich
Quoted 7 lines
Quoted 1 line
Quoted 1 line · Keir Fraser
Oh, depending on what you want to do with the page you may well want to
get_page(current->domain, page). You don't hold a lock on the domain's p2m, so page ownerships can change under your feet, and hence getting a reference to the page, and checking the page's ownership at the same time, might be wise. And if you want to modify the page you should probably useget_page_and_type(..., PGT_writable_page).That's particularly important for the pv case, where
gfn_to_mfn()is a no-op.
Yes, I actually forgot about the PV case, and I think that's the only case that matters for tmem. :-)
-- Keir
Quoted 17 lines · Jan Beulich
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
On 17.09.10 at 21:32, Keir Fraser <[email protected]>
Quoted 1 line · wrote:
Quoted 1 line
Oh, depending on what you want to do with the page you may well want
to
Quoted 1 line
Quoted 1 line
get_page(current->domain, page). You don't hold a lock on thedomain's p2m,
Quoted 1 line
Quoted 1 line
so page ownerships can change under your feet, and hence getting a
reference
Quoted 1 line
Quoted 1 line
to the page, and checking the page's ownership at the same time,
might be
Quoted 5 lines
Quoted 2 lines
wise. And if you want to modify the page you should probably use
get_page_and_type(..., PGT_writable_page).That's particularly important for the pv case, where
gfn_to_mfn()is a no-op.
Thanks, Jan and Keir, I'll take a look at it.
Quoted 3 lines
Yes, I actually forgot about the PV case, and I think that's the only case
that matters for tmem. :-)
Tmem works for HVM guests now too (on top of Stefano's PV-on-HVM Linux kernel patches), though I haven't tested it in awhile.
Wow, gfn_to_mfn() really is a no-op for the pv case. I thought you meant that it didn't do any translation, but it appears it also doesn't even set the return value to INVALID_MFN, so for pv I have to add a separate check for mfn_valid() to see if it is even a valid Xen mfn! THAT was what was causing the crash.
Also, it appears that gfn_to_mfn_unshare is not suitable for tmem... it appears the existing code checks for "must_succeed" but gives no indication of success if must_succeed is NOT set but mem_sharing_unshare_page fails, correct? If I do plan to write to the page but I am willing to accept failure if the page is shared (since tmem is resilient to this scenario), it appears I will have to add a new variation of gfn_to_mfn_unshare. Or maybe I should just overload must_succeed, and if must_succeed is 2 and unsharing fails, return INVALID_MFN?
P.S. Here's what the relevant tmem code looks like now (below and attached). Quick review appreciated.
static inline void *cli_get_page(tmem_cli_mfn_t cmfn, unsigned long *pcli_mfn,
pfp_t **pcli_pfp, bool_t cli_write)
{
unsigned long xmfn;
p2m_type_t t;
struct page_info *page;
int ret;
if ( is_hvm_domain(current->domain) )
{
xmfn = mfn_x(gfn_to_mfn_unshare(current->domain, cmfn, &t, 2));
if (t != p2m_ram_rw || xmfn == INVALID_MFN)
return NULL;
}
else
{
xmfn = cmfn;
if (!mfn_valid(xmfn))
return NULL;
}
page = mfn_to_page(xmfn);
if ( cli_write )
ret = get_page_and_type(page, current->domain, PGT_writable_page);
else
ret = get_page(page, current->domain);
if ( ret <= 0 )
return NULL;
*pcli_mfn = xmfn;
*pcli_pfp = (pfp_t *)page;
return map_domain_page(xmfn);
}static inline void cli_put_page(void *cli_va, pfp_t *cli_pfp,
unsigned long cli_mfn, bool_t mark_dirty)
{
if ( mark_dirty )
paging_mark_dirty(current->domain,cli_mfn);
put_page((struct page_info *)cli_pfp);
unmap_domain_page(cli_va);
}Quoted 28 lines
-----Original Message-----
From: Dan Magenheimer
Sent: Monday, September 20, 2010 8:13 AM
To: Keir Fraser; Jan Beulich
Cc: Xen-devel
Subject: RE: [Xen-devel] Re: xen crash in tmem: checking a xen pfn for domain ownershipQuoted 5 lines · Jan Beulich
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
On 17.09.10 at 21:32, Keir Fraser <[email protected]>
Quoted 1 line · wrote:
Quoted 1 line
Oh, depending on what you want to do with the page you may well
want
Quoted 11 lines
to
Quoted 1 line
Quoted 1 line
get_page(current->domain, page). You don't hold a lock on thedomain's p2m,
Quoted 1 line
Quoted 1 line
so page ownerships can change under your feet, and hence getting a
reference
Quoted 1 line
Quoted 1 line
to the page, and checking the page's ownership at the same time,
might be
Quoted 4 lines
Quoted 2 lines
wise. And if you want to modify the page you should probably use
get_page_and_type(..., PGT_writable_page).That's particularly important for the pv case, where
gfn_to_mfn()is
Quoted 1 line
Quoted 1 line
a no-op.
Quoted 8 lines
Thanks, Jan and Keir, I'll take a look at it.
Quoted 3 lines
Yes, I actually forgot about the PV case, and I think that's the only case
that matters for tmem. :-)Tmem works for HVM guests now too (on top of Stefano's PV-on-HVM Linux kernel patches), though I haven't tested it in awhile.
_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xensource.com/xen-devel
Quoted 6 lines
Also, it appears that
gfn_to_mfn_unshareis not suitable for tmem... it appears the existing code checks for "must_succeed" but gives no indication of success ifmust_succeedis NOT set butmem_sharing_unshare_pagefails, correct? If I do plan to write to the page but I am willing to accept failure if the page is shared (since tmem is resilient to this scenario),
Ignore this part. There IS at least one scenario (persistent get) where tmem would not be resilient to a failed unshare. So I think I will skip unshare for now and just submit a patch for the crash-on-bad-PV-mfn.
Quoted 111 lines
-----Original Message-----
From: Dan Magenheimer
Sent: Monday, September 20, 2010 5:04 PM
To: Keir Fraser; Jan Beulich
Cc: Xen-devel
Subject: RE: [Xen-devel] Re: xen crash in tmem: checking a xen pfn for domain ownershipWow,
gfn_to_mfn()really is a no-op for the pv case. I thought you meant that it didn't do any translation, but it appears it also doesn't even set the return value toINVALID_MFN, so for pv I have to add a separate check formfn_valid()to see if it is even a valid Xen mfn! THAT was what was causing the crash.Also, it appears that
gfn_to_mfn_unshareis not suitable for tmem... it appears the existing code checks for "must_succeed" but gives no indication of success ifmust_succeedis NOT set butmem_sharing_unshare_pagefails, correct? If I do plan to write to the page but I am willing to accept failure if the page is shared (since tmem is resilient to this scenario), it appears I will have to add a new variation ofgfn_to_mfn_unshare. Or maybe I should just overloadmust_succeed, and ifmust_succeedis 2 and unsharing fails, returnINVALID_MFN?
P.S. Here's what the relevant tmem code looks like now (below and attached). Quick review appreciated.static inline void *cli_get_page(tmem_cli_mfn_t cmfn, unsigned long
*pcli_mfn,
pfp_t**pcli_pfp,bool_tcli_write){ unsigned long xmfn; p2m_type_t t; struct page_info *page; int ret; if ( is_hvm_domain(current->domain) ) { xmfn = mfn_x(gfn_to_mfn_unshare(current->domain, cmfn, &t, 2)); if (t != p2m_ram_rw || xmfn == INVALID_MFN) return NULL; } else { xmfn = cmfn; if (!mfn_valid(xmfn)) return NULL; } page = mfn_to_page(xmfn); if ( cli_write ) ret = get_page_and_type(page, current->domain, PGT_writable_page); else ret = get_page(page, current->domain); if ( ret <= 0 ) return NULL; *pcli_mfn = xmfn; *pcli_pfp = (pfp_t *)page; return map_domain_page(xmfn); }static inline void cli_put_page(void
*cli_va,pfp_t*cli_pfp,
unsigned longcli_mfn,bool_tmark_dirty){ if ( mark_dirty ) paging_mark_dirty(current->domain,cli_mfn); put_page((struct page_info *)cli_pfp); unmap_domain_page(cli_va); }Quoted 6 lines
-----Original Message-----
From: Dan Magenheimer
Sent: Monday, September 20, 2010 8:13 AM
To: Keir Fraser; Jan Beulich
Cc: Xen-devel
Subject: RE: [Xen-devel] Re: xen crash in tmem: checking a xen pfnfor
Quoted 12 lines
domain ownership
Quoted 5 lines · Jan Beulich
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
On 17.09.10 at 21:32, Keir Fraser <[email protected]>
Quoted 1 line · wrote:
Quoted 1 line
Oh, depending on what you want to do with the page you may well
want
Quoted 4 lines
to
Quoted 1 line
Quoted 1 line
get_page(current->domain, page). You don't hold a lock on thedomain's p2m,
Quoted 1 line
Quoted 1 line
so page ownerships can change under your feet, and hence getting
a
Quoted 13 lines
Quoted 7 lines
reference
Quoted 1 line
Quoted 1 line
to the page, and checking the page's ownership at the same time,
might be
Quoted 4 lines
Quoted 2 lines
wise. And if you want to modify the page you should probably use
get_page_and_type(..., PGT_writable_page).That's particularly important for the pv case, where
gfn_to_mfn()is
Quoted 1 line
Quoted 1 line
a no-op.
Thanks, Jan and Keir, I'll take a look at it.
Quoted 1 line
Yes, I actually forgot about the PV case, and I think that's the
only
Quoted 5 lines
Quoted 2 lines
case
that matters for tmem. :-)Tmem works for HVM guests now too (on top of Stefano's PV-on-HVM Linux kernel patches), though I haven't tested it in awhile.
Quoted 4 lines · Dan Magenheimer
if ( is_hvm_domain(current->domain) )
{
xmfn = mfn_x(gfn_to_mfn_unshare(current->domain, cmfn, &t, 2));
if (t != p2m_ram_rw || xmfn == INVALID_MFN)Quoted 8 lines
return NULL;
}
else
{
xmfn = cmfn;
if (!mfn_valid(xmfn))
return NULL;
}This is needlessly cumbersome. You can do it without the if(is_hvm):
xmfn = mfn_x(gfn_to_mfn(current->domain, cmfn, &t);
if ((t != p2m_ram_rw) || !mfn_valid(xmfn))
return NULL;(Didn't use hfn_to_mfn_unshare as you have decided against it.)
Rest of the code looks fine.
-- Keir
Quoted 1 line
Rest of the code looks fine.
Except for a couple of bugs that took awhile to track down :-(
First (stupid one), get_page returns 1 for success and 0 for failure and I was testing for failure by <= 0 (the Linux convention, oops).
Second, and much more subtle, apparently get_page_and_type must always be bracketed with a put_page_and_type, and get_page must always be bracketed with a put_page, i.e. these can't be mixed. It appears that the "and_type" versions take two references, not one so mixing them results in bad non-obvious BUGs when pages are cycled through the heap.
Quoted 38 lines
-----Original Message-----
From: Keir Fraser [mailto:[email protected]]
Sent: Tuesday, September 21, 2010 1:44 AM
To: Dan Magenheimer; Jan Beulich
Cc: Xen-devel
Subject: Re: [Xen-devel] Re: xen crash in tmem: checking a xen pfn for domain ownershipQuoted 3 lines · Dan Magenheimer
if ( is_hvm_domain(current->domain) ) { xmfn = mfn_x(gfn_to_mfn_unshare(current->domain, cmfn, &t,2));
Quoted 9 lines
if (t != p2m_ram_rw || xmfn == INVALID_MFN) return NULL; } else { xmfn = cmfn; if (!mfn_valid(xmfn)) return NULL; }This is needlessly cumbersome. You can do it without the
if(is_hvm):xmfn = mfn_x(gfn_to_mfn(current->domain, cmfn, &t); if ((t != p2m_ram_rw) || !mfn_valid(xmfn)) return NULL;(Didn't use
hfn_to_mfn_unshareas you have decided against it.)Rest of the code looks fine.
-- Keir