xen/mm: populate_physmap: validate correctly the gfn for direct mapped domain
PATCH 1/1 xen/mm: populate_physmap: validate correctly the gfn for direct mapped domain
Direct mapped domain has already the memory allocated 1:1, so we are directly using the gfn as mfn to map the RAM in the guest.
While we are validating that the page associated to the first mfn belongs to the domain, the subsequent MFN are not validated when the extent_order is > 0.
This may result to map memory region (MMIO, RAM) which doesn't belong to the domain.
Although, only DOM0 on ARM is using a direct memory mapped. So it doesn't affect any guest (at least on the upstream version) or even x86.
---
Cc: Ian Campbell <[email protected]>
Cc: Ian Jackson <[email protected]>
Cc: Jan Beulich <[email protected]>
Cc: Keir Fraser <[email protected]>
Cc: Tim Deegan <[email protected]>
This patch is a candidate for Xen 4.6 and backport to Xen 4.5 (and maybe 4.4).
The hypercall is used for the ballooning code and only DOM0 on ARM is a direct mapped domain.
The current code to validate the GFN passed by the populate physmap hypercall has been moved in a for loop in order to validate each GFN when the extent order is > 0.
Without it, direct mapping domain may be able to map memory region which doesn't belong to it.
I think the switch to the for loop is pretty safe and contained. The worst thing that could happen is denying mapping more often than we use to do. Although, IIRC Linux is mostly mapping page one by one (i.e extent order = 0).
--- xen/common/memory.c | 30 ++++++++++++++++++------------ 1 file changed, 18 insertions(+), 12 deletions(-)
xen/common/memory.c +18 -12
@@ -126,22 +126,28 @@ static void populate_physmap(struct memop_args *a)if (is_domain_direct_mapped (d) ) { mfn = gpfn; -if ( !mfn_valid (mfn) ) + +for ( j =0 ; j < (1 << a->extent_order); j++, mfn++ ) { -gdprintk (XENLOG_INFO ,"Invalid mfn %#" PRI_xen_pfn"\n" , +if ( !mfn_valid (mfn) ) + { +gdprintk (XENLOG_INFO ,"Invalid mfn %#" PRI_xen_pfn"\n" , mfn); - goto out; + goto out; + } + + page =mfn_to_page (mfn); +if ( !get_page (page, d) ) + { +gdprintk (XENLOG_INFO , +"mfn %#" PRI_xen_pfn" doesn't belong to the" +" domain\n" , mfn); + goto out; + } +put_page (page); } - page =mfn_to_page (mfn); -if ( !get_page (page, d) ) - { -gdprintk (XENLOG_INFO , -"mfn %#" PRI_xen_pfn" doesn't belong to the" -" domain\n" , mfn); - goto out; - } -put_page (page); + page =mfn_to_page (gpfn); }else page =alloc_domheap_pages (d, a->extent_order, a->memflags);
Signature · 1 line
2.1.4
xen/common/memory.c static void populate_physmap(struct memop_args *a) · line 130
Quoted 10 lines
Quoted 1 line
Quoted 1 line · On 11.08.15 at 19:41, <[email protected]> wrote:
xen/common/memory.c +2
-1@@ -126,22 +126,28 @@ static void populate_physmap(struct memop_args *a)if (is_domain_direct_mapped (d) ) { mfn = gpfn; -if ( !mfn_valid (mfn) ) + +for ( j =0 ; j < (1 << a->extent_order); j++, mfn++ )
While benign I think we shouldn't repeat mistakes like this made elsewhere in the code: At the very least this should be 1U, but with j (needlessly, just like i) being unsigned long it would be more consistent for this to be 1UL unless we clean up the variable types (which I think I'll do).
Since the change is so minor, I'd be fine with making it while committing, unless you object.
Jan
Hi Jan,
Quoted 16 lines · Jan Beulich
xen/common/memory.c static void populate_physmap(struct memop_args *a) · line 130
Quoted 10 lines
Quoted 1 line
Quoted 1 line · On 11.08.15 at 19:41, <[email protected]> wrote:
xen/common/memory.c +2
-1@@ -126,22 +126,28 @@ static void populate_physmap(struct memop_args *a)if (is_domain_direct_mapped (d) ) { mfn = gpfn; -if ( !mfn_valid (mfn) ) + +for ( j =0 ; j < (1 << a->extent_order); j++, mfn++ )While benign I think we shouldn't repeat mistakes like this made elsewhere in the code: At the very least this should be 1U, but with j (needlessly, just like i) being unsigned long it would be more consistent for this to be 1UL unless we clean up the variable types (which I think I'll do).
I admit that I copied the loop at then end of the function without thinking if it there was some issue or not.
Quoted 3 lines
Since the change is so minor, I'd be fine with making it while committing, unless you object.
I'm fine with that.
Regards,
Signature · 1 line
Julien Grall
Quoted 22 lines · Julien Grall
Direct mapped domain has already the memory allocated 1:1, so we are directly using the gfn as mfn to map the RAM in the guest.
While we are validating that the page associated to the first mfn belongs to the domain, the subsequent MFN are not validated when the
extent_orderis > 0.This may result to map memory region (MMIO, RAM) which doesn't belong to the domain.
Although, only DOM0 on ARM is using a direct memory mapped. So it doesn't affect any guest (at least on the upstream version) or even x86.
Signed-off-by Julien Grall <[email protected]>---
Cc: Ian Campbell <[email protected]>
Cc: Ian Jackson <[email protected]>
Cc: Jan Beulich <[email protected]>
Cc: Keir Fraser <[email protected]>
Cc: Tim Deegan <[email protected]>
Quoted 3 lines
This patch is a candidate for Xen 4.6 and backport to Xen 4.5 (and maybe 4.4).
Fine by me in principle for this patch to go in 4.6.
Quoted 23 lines · Wei Liu
Quoted 22 lines · Julien Grall
Direct mapped domain has already the memory allocated 1:1, so we are directly using the gfn as mfn to map the RAM in the guest.
While we are validating that the page associated to the first mfn belongs to the domain, the subsequent MFN are not validated when the
extent_orderis > 0.This may result to map memory region (MMIO, RAM) which doesn't belong to the domain.
Although, only DOM0 on ARM is using a direct memory mapped. So it doesn't affect any guest (at least on the upstream version) or even x86.
Signed-off-by Julien Grall <[email protected]>---
Cc: Ian Campbell <[email protected]>
Cc: Ian Jackson <[email protected]>
Cc: Jan Beulich <[email protected]>
Cc: Keir Fraser <[email protected]>
Cc: Tim Deegan <[email protected]>
Quoted 5 lines
Quoted 3 lines
This patch is a candidate for Xen 4.6 and backport to Xen 4.5 (and maybe 4.4).
Fine by me in principle for this patch to go in 4.6.
I shall do this in a formal way.
Quoted 74 lines · Julien Grall
Direct mapped domain has already the memory allocated 1:1, so we are directly using the gfn as mfn to map the RAM in the guest.
While we are validating that the page associated to the first mfn belongs to
the domain, the subsequent MFN are not validated when theextent_orderis > 0.This may result to map memory region (MMIO, RAM) which doesn't belong to the
domain.Although, only DOM0 on ARM is using a direct memory mapped. So it doesn't affect any guest (at least on the upstream version) or even x86.
Signed-off-by Julien Grall <[email protected]>---
Cc: Ian Campbell <[email protected]>
Cc: Ian Jackson <[email protected]>
Cc: Jan Beulich <[email protected]>
Cc: Keir Fraser <[email protected]>
Cc: Tim Deegan <[email protected]>This patch is a candidate for Xen 4.6 and backport to Xen 4.5 (and maybe 4.4).
The hypercall is used for the ballooning code and only DOM0 on ARM is a direct mapped domain.
The current code to validate the GFN passed by the populate physmap hypercall has been moved in a for loop in order to validate each GFN when the extent order is > 0.
Without it, direct mapping domain may be able to map memory region which doesn't belong to it.
I think the switch to the for loop is pretty safe and contained. The worst thing that could happen is denying mapping more often than we use to do. Although, IIRC Linux is mostly mapping page one by one (i.e extent order = 0).
--- xen/common/memory.c | 30 ++++++++++++++++++------------ 1 file changed, 18 insertions(+), 12 deletions(-)xen/common/memory.c +13
-3@@ -126,22 +126,28 @@ static void populate_physmap(struct memop_args *a) if ( is_domain_direct_mapped(d) ) { mfn = gpfn; -if ( !mfn_valid (mfn) ) + + for ( j = 0; j < (1 << a->extent_order); j++, mfn++ ) { -gdprintk (XENLOG_INFO ,"Invalid mfn %#"PRI_xen_pfn"\n", + if ( !mfn_valid(mfn) ) + { + gdprintk(XENLOG_INFO, "Invalid mfn %#"PRI_xen_pfn"\n", mfn); - goto out; + goto out; + } + + page = mfn_to_page(mfn); + if ( !get_page(page, d) ) + { + gdprintk(XENLOG_INFO, + "mfn %#"PRI_xen_pfn" doesn't belong tothe"
xen/common/memory.c static void populate_physmap(struct memop_args *a) · line 145
Quoted 2 lines
xen/common/memory.c +2
-0+ " domain\n", mfn); + goto out;
This will leak the references on any earlier pages, which will happen all the time if you are cross a boundary from RAM into e.g. MMIO or whatever.
A variant of get_page which took an order argument and returned either with 0 or 1<<order refs taken would perhaps make this easier to deal with. I suppose we don't already have one of those, but you could add it.
Oh, am I wrong and we do get_page then immediately put_page for every page? I suppose that is one way to check the current owner, but I think it needs a comment at least (it did before TBH, just iterating over many pages makes it seem even odder).
Is that really the best way to ask if a page is owned by a given domain tho ugh? Does page_get_owner not suffice?
Quoted 18 lines
xen/common/memory.c +3
-10+ } +put_page (page); } - page =mfn_to_page (mfn); -if ( !get_page (page, d) ) - { -gdprintk (XENLOG_INFO , -"mfn %#" PRI_xen_pfn" doesn't belong to the" -" domain\n" , mfn); - goto out; - } -put_page (page); + page =mfn_to_page (gpfn); }else page = alloc_domheap_pages(d, a->extent_order, a ->memflags);
Quoted 23 lines
Quoted 1 line
Quoted 1 line · On 13.08.15 at 11:33, <[email protected]> wrote:
Quoted 21 lines · Julien Grall
xen/common/memory.c +10
-3@@ -126,22 +126,28 @@ static void populate_physmap(struct memop_args *a)if (is_domain_direct_mapped (d) ) { mfn = gpfn; -if ( !mfn_valid (mfn) ) + +for ( j =0 ; j < (1 << a->extent_order); j++, mfn++ ) { -gdprintk (XENLOG_INFO ,"Invalid mfn %#" PRI_xen_pfn"\n" , +if ( !mfn_valid (mfn) ) + { +gdprintk (XENLOG_INFO ,"Invalid mfn %#" PRI_xen_pfn"\n" , mfn); - goto out; + goto out; + } + + page =mfn_to_page (mfn); +if ( !get_page (page, d) )
Quoted 8 lines
xen/common/memory.c static void populate_physmap(struct memop_args *a) · line 145
Quoted 5 lines
xen/common/memory.c +5
-0+ { +gdprintk (XENLOG_INFO , +"mfn %#" PRI_xen_pfn" doesn't belong to the" +" domain\n" , mfn); + goto out;This will leak the references on any earlier pages, which will happen all the time if you are cross a boundary from RAM into e.g. MMIO or whatever.
I don't see why it would - the put_page() (as you say a few lines down in your reply) follows right afterwards.
Quoted 3 lines
A variant of
get_pagewhich took an order argument and returned either with 0 or 1<<order refs taken would perhaps make this easier to deal with. I suppose we don't already have one of those, but you could add it.
That would be useful if we wanted to retain the ref, but that's not the case here.
Quoted 7 lines
Oh, am I wrong and we do
get_pagethen immediatelyput_pagefor every page? I suppose that is one way to check the current owner, but I think it needs a comment at least (it did before TBH, just iterating over many pages makes it seem even odder).Is that really the best way to ask if a page is owned by a given domain tho ugh? Does
page_get_ownernot suffice?
That could produce wrong results when the page previously had no reference (and namely, due to the unions used in struct page_info, was free or in use as a shadow page).
Jan
Quoted 40 lines · Jan Beulich
Quoted 31 lines
Quoted 2 lines
Quoted 2 lines · On 13.08.15 at 11:33, <[email protected]> wrote:
Quoted 28 lines · Julien Grall
xen/common/memory.c +13
-3@@ -126,22 +126,28 @@ static void populate_physmap(struct memop_args *a) if ( is_domain_direct_mapped(d) ) { mfn = gpfn; -if ( !mfn_valid (mfn) ) + + for ( j = 0; j < (1 << a->extent_order); j++, mfn++ ) { -gdprintk (XENLOG_INFO ,"Invalid mfn %#"PRI_xen_pfn"\n", + if ( !mfn_valid(mfn) ) + { + gdprintk(XENLOG_INFO, "Invalid mfn %#"PRI_xen_pfn"\n", mfn); - goto out; + goto out; + } + + page = mfn_to_page(mfn); + if ( !get_page(page, d) ) + { + gdprintk(XENLOG_INFO, + "mfn %#"PRI_xen_pfn" doesn't belongQuoted 8 lines
xen/common/memory.c static void populate_physmap(struct memop_args *a) · line 145
Quoted 3 lines
to the"
xen/common/memory.c +2
-0+ " domain\n", mfn); + goto out;This will leak the references on any earlier pages, which will happen all
the time if you are cross a boundary from RAM into e.g. MMIO or whatever.
Quoted 2 lines
I don't see why it would - the
put_page()(as you say a few lines down in your reply) follows right afterwards.
Right, sorry I should have delete all this once I realised what was going on (I wasn't 100% sure I guess).
Quoted 11 lines
Quoted 7 lines
Oh, am I wrong and we do
get_pagethen immediatelyput_pagefor every page? I suppose that is one way to check the current owner, but I think it needs a comment at least (it did before TBH, just iterating over many pages makes it seem even odder).Is that really the best way to ask if a page is owned by a given domain tho ugh? Does
page_get_ownernot suffice?That could produce wrong results when the page previously had no reference (and namely, due to the unions used in struct
page_info, was free or in use as a shadow page).
I see.
Maybe some new helper to encapsulate the get+put to validate the presence of a current owner would be nice in the future then, but not a blocker for this patch I guess.
There isn't a race here is there? What if the reference were dropped after this check but before the guest_physmap_add_page (which takes new references)? We are implicitly relying on a guarantee made elsewhere that a direct mapped guest never drops the final ref until it is destroyed (which can't be happening in this window I think), but it would be obviously safer against such bugs if we just held the ref until after the p2m was updated.
Another thing for a future/4.7 cleanup I guess.
Ian.
Quoted 7 lines
Quoted 1 line
Quoted 1 line · On 13.08.15 at 12:14, <[email protected]> wrote:
There isn't a race here is there? What if the reference were dropped after this check but before the
guest_physmap_add_page(which takes new references)? We are implicitly relying on a guarantee made elsewhere that a direct mapped guest never drops the final ref until it is destroyed (which can't be happening in this window I think), but it would be obviously safer against such bugs if we just held the ref until after the p2m was updated.
Yeah, we depend on this, and what you suggest would indeed be an improvment.
Quoted 1 line
Another thing for a future/4.7 cleanup I guess.
Right.
But as bottom line I take it that your objections to the patch going in soon have been eliminated?
Jan
Ian Campbell
Acked-byQuoted 17 lines · Jan Beulich
Quoted 13 lines
Quoted 2 lines
Quoted 2 lines · On 13.08.15 at 12:14, <[email protected]> wrote:
There isn't a race here is there? What if the reference were dropped after
this check but before theguest_physmap_add_page(which takes new references)? We are implicitly relying on a guarantee made elsewhere that a
direct mapped guest never drops the final ref until it is destroyed (which
can't be happening in this window I think), but it would be obviously safer
against such bugs if we just held the ref until after the p2m was updated.Yeah, we depend on this, and what you suggest would indeed be an improvment.
Quoted 6 lines
Quoted 1 line
Another thing for a future/4.7 cleanup I guess.
Right.
But as bottom line I take it that your objections to the patch going in soon have been eliminated?
Correct.