Skip to contentSkip to conversation

xen/memory: Reject out-of-range resource 'frame' values

PATCH Superseded Andrew Cooper 4 participants 10 messages Jan 28, 2021 – Jan 29, 2021

v1 of 9 Superseded Latest is v9 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 xen/memory: Reject out-of-range resource 'frame' values

The ABI is unfortunate, and frame being 64 bits leads to all kinds of problems performing correct overflow checks.

Furthermore, the mixed use of unsigned int and unsigned long in the call tree is buggy on arm32 where the two are the same size, and certain out-of-range combinations will truncated and be permitted.

---
CC: Jan Beulich <[email protected]>
CC: Roger Pau Monné <[email protected]>
CC: Wei Liu <[email protected]>
CC: Stefano Stabellini <[email protected]>
CC: Julien Grall <[email protected]>
CC: Volodymyr Babchuk <[email protected]>
CC: Paul Durrant <[email protected]>
CC: Oleksandr Tyshchenko <[email protected]>

Posted ahead of my full v8 series, in the hope that it catches some people before the end of the day.

---
 xen/arch/x86/mm.c        |  2 +-
 xen/common/memory.c      | 15 ++++++++++++++-
 xen/include/asm-x86/mm.h |  2 +-
 xen/include/xen/mm.h     |  2 +-
 4 files changed, 17 insertions(+), 4 deletions(-)
xen/arch/x86/mm.c +1 -1
index 63e9fae919..2ce00a8ece 100644
@@ -4588,7 +4588,7 @@ static int handle_iomem_range(unsigned long s, unsigned long e, void *p)
 }
 
 int arch_acquire_resource(struct domain *d, unsigned int type,
-                          unsigned int id, unsigned long frame,
+                          unsigned int id, unsigned int frame,
                           unsigned int nr_frames, xen_pfn_t mfn_list[])
 {
     int rc;
xen/common/memory.c +14 -1
index ccb4d49fc6..33ab36eb72 100644
@@ -1054,7 +1054,7 @@ static long xatp_permission_check(struct domain *d, unsigned int space)
 }
 
 static int acquire_grant_table(struct domain *d, unsigned int id,
-                               unsigned long frame,
+                               unsigned int frame,
                                unsigned int nr_frames,
                                xen_pfn_t mfn_list[])
 {
@@ -1134,6 +1134,19 @@ static int acquire_resource(
     if ( xmar.nr_frames > ARRAY_SIZE(mfn_list) )
         return -E2BIG;
 
+    /*
+     * The ABI is rather unfortunate.  nr_frames (and therefore the total size
+     * of the resource) is 32bit, while frame (the offset within the resource
+     * we'd like to start at) is 64bit.
+     *
+     * Reject values oustide the of the range of nr_frames, as well as
+     * combinations of frame and nr_frame which overflow, to simplify the rest
+     * of the logic.
+     */
+    if ( (xmar.frame >> 32) ||
+         ((xmar.frame + xmar.nr_frames) >> 32) )
+        return -EINVAL;
+
     rc = rcu_lock_remote_domain_by_id(xmar.domid, &d);
     if ( rc )
         return rc;
xen/include/asm-x86/mm.h +1 -1
index 1fdb4eb835..3fe47dcd8d 100644
@@ -641,7 +641,7 @@ static inline bool arch_mfn_in_directmap(unsigned long mfn)
 }
 
 int arch_acquire_resource(struct domain *d, unsigned int type,
-                          unsigned int id, unsigned long frame,
+                          unsigned int id, unsigned int frame,
                           unsigned int nr_frames, xen_pfn_t mfn_list[]);
 
 #endif /* __ASM_X86_MM_H__ */
xen/include/xen/mm.h +1 -1
index 636a1254ae..66a4d10695 100644
@@ -614,7 +614,7 @@ static inline void put_page_alloc_ref(struct page_info *page)
 
 #ifndef CONFIG_ARCH_ACQUIRE_RESOURCE
 static inline int arch_acquire_resource(
-    struct domain *d, unsigned int type, unsigned int id, unsigned long frame,
+    struct domain *d, unsigned int type, unsigned int id, unsigned int frame,
     unsigned int nr_frames, xen_pfn_t mfn_list[])
 {
     return -EOPNOTSUPP;
Signature · 1 line
2.11.0
Reviewed-by on v1 1/1
#1 lore raw
Quoted 28 lines

-----Original Message-----
From: Andrew Cooper <[email protected]>
Sent: 28 January 2021 16:06
To: Xen-devel <[email protected]>
Cc: Andrew Cooper <[email protected]>; Jan Beulich <[email protected]>; Roger Pau Monné <[email protected]>; Wei Liu <[email protected]>; Stefano Stabellini <[email protected]>; Julien Grall <[email protected]>; Volodymyr Babchuk <[email protected]>; Paul Durrant <[email protected]>; Oleksandr Tyshchenko <[email protected]>
Subject: [PATCH] xen/memory: Reject out-of-range resource 'frame' values

The ABI is unfortunate, and frame being 64 bits leads to all kinds of problems performing correct overflow checks.

Furthermore, the mixed use of unsigned int and unsigned long in the call tree is buggy on arm32 where the two are the same size, and certain out-of-range combinations will truncated and be permitted.

---
CC: Jan Beulich <[email protected]>
CC: Roger Pau Monné <[email protected]>
CC: Wei Liu <[email protected]>
CC: Stefano Stabellini <[email protected]>
CC: Julien Grall <[email protected]>
CC: Volodymyr Babchuk <[email protected]>
CC: Paul Durrant <[email protected]>
CC: Oleksandr Tyshchenko <[email protected]>

Quoted 2 lines

Posted ahead of my full v8 series, in the hope that it catches some people before the end of the day.

#2 lore raw
In reply to Andrew Cooper

xen/common/memory.c static long xatp_permission_check(struct domain *d, unsigned int space) · line 1060

Quoted 11 lines · Andrew Cooper
xen/common/memory.c +1 -1
@@ -1054,7 +1054,7 @@ static long xatp_permission_check(struct domain *d, unsigned int space)
 }
 
 static int acquire_grant_table(struct domain *d, unsigned int id,
-                               unsigned long frame,
+                               unsigned int frame,
                                unsigned int nr_frames,
                                xen_pfn_t mfn_list[])
 {

Doesn't this want carrying forward into gnttab_get_{shared,status}_frame() as well? Of course further cleanup here can also be done at a later point, but it leaves things in a somewhat inconsistent state. I'd like to leave it up to you to commit with Paul's R-b as is, or extend the adjustments and then also add mine.

xen/include/asm-x86/mm.h static inline bool arch_mfn_in_directmap(unsigned long mfn) · line 645

Quoted 9 lines
xen/include/asm-x86/mm.h +1 -1
@@ -641,7 +641,7 @@ static inline bool arch_mfn_in_directmap(unsigned long mfn)
 }
 
 int arch_acquire_resource(struct domain *d, unsigned int type,
-                          unsigned int id, unsigned long frame,
+                          unsigned int id, unsigned int frame,
                           unsigned int nr_frames, xen_pfn_t mfn_list[]);

Same here wrt hvm_get_ioreq_server_frame().

Jan

#3 lore raw
Quoted 6 lines · Jan Beulich
Quoted 5 lines · Andrew Cooper
xen/common/memory.c +0 -0
@@ -1054,7 +1054,7 @@ static long xatp_permission_check(struct domain *d, unsigned int space)
 }
 
Quoted 12 lines

xen/common/memory.c static long xatp_permission_check(struct domain *d, unsigned int space) · line 1060

Quoted 6 lines
xen/common/memory.c +1 -1
 static int acquire_grant_table(struct domain *d, unsigned int id,
-                               unsigned long frame,
+                               unsigned int frame,
                                unsigned int nr_frames,
                                xen_pfn_t mfn_list[])
 {

Doesn't this want carrying forward into gnttab_get_{shared,status}_frame() as well? Of course further cleanup here can also be done at a later point, but it leaves things in a somewhat inconsistent state. I'd like to leave it up to you to commit with Paul's R-b as is, or extend the adjustments and then also add mine.

In the series, those functions are deleted by the next patch.

What's the likelihood that you'll choose to backport this?  I can extend it if needs be.

Quoted 11 lines

xen/include/asm-x86/mm.h static inline bool arch_mfn_in_directmap(unsigned long mfn) · line 645

Quoted 10 lines
xen/include/asm-x86/mm.h +1 -1
@@ -641,7 +641,7 @@ static inline bool arch_mfn_in_directmap(unsigned long mfn)
 }
 
 int arch_acquire_resource(struct domain *d, unsigned int type,
-                          unsigned int id, unsigned long frame,
+                          unsigned int id, unsigned int frame,
                           unsigned int nr_frames, xen_pfn_t mfn_list[]);

Same here wrt hvm_get_ioreq_server_frame().

This one isn't.  I'll adjust.

~Andrew

#4 lore raw
Quoted 20 lines · Andrew Cooper
Quoted 6 lines · Jan Beulich
Quoted 5 lines · Andrew Cooper
xen/common/memory.c +0 -0
@@ -1054,7 +1054,7 @@ static long xatp_permission_check(struct domain *d, unsigned int space)
 }
 
Quoted 12 lines

xen/common/memory.c static long xatp_permission_check(struct domain *d, unsigned int space) · line 1060

Quoted 6 lines
xen/common/memory.c +1 -1
 static int acquire_grant_table(struct domain *d, unsigned int id,
-                               unsigned long frame,
+                               unsigned int frame,
                                unsigned int nr_frames,
                                xen_pfn_t mfn_list[])
 {

Doesn't this want carrying forward into gnttab_get_{shared,status}_frame() as well? Of course further cleanup here can also be done at a later point, but it leaves things in a somewhat inconsistent state. I'd like to leave it up to you to commit with Paul's R-b as is, or extend the adjustments and then also add mine.

Quoted 1 line

In the series, those functions are deleted by the next patch.

In your submission you talk about a v8 series, which I took to mean the vmtrace one. I understand here you refer to the other series, presently at v3?

Quoted 1 line

What's the likelihood that you'll choose to backport this?

Didn't consider this aspect yet. I think I wouldn't have picked it without anyone asking for it to be backported.

Quoted 1 line

  I can extend it if needs be.

Well, if that deletion of code gets committed in time, then of course there's no real need to fiddle with it here.

Jan

#5 lore raw
Quoted 20 lines · Jan Beulich
Quoted 19 lines · Andrew Cooper
Quoted 18 lines · Jan Beulich

xen/common/memory.c static long xatp_permission_check(struct domain *d, unsigned int space) · line 1060

Quoted 11 lines · Andrew Cooper
xen/common/memory.c +1 -1
@@ -1054,7 +1054,7 @@ static long xatp_permission_check(struct domain *d, unsigned int space)
 }
 
 static int acquire_grant_table(struct domain *d, unsigned int id,
-                               unsigned long frame,
+                               unsigned int frame,
                                unsigned int nr_frames,
                                xen_pfn_t mfn_list[])
 {

Doesn't this want carrying forward into gnttab_get_{shared,status}_frame() as well? Of course further cleanup here can also be done at a later point, but it leaves things in a somewhat inconsistent state. I'd like to leave it up to you to commit with Paul's R-b as is, or extend the adjustments and then also add mine.

Quoted 4 lines
Quoted 1 line

In the series, those functions are deleted by the next patch.

In your submission you talk about a v8 series, which I took to mean the vmtrace one. I understand here you refer to the other series, presently at v3?

The two combined is v8 because of the dependencies (and that's what I'll post all together), but yes - I did mean the thing presently at v3 as posted.

Quoted 8 lines
Quoted 2 lines

What's the likelihood that you'll choose to backport this?

Didn't consider this aspect yet. I think I wouldn't have picked it without anyone asking for it to be backported.

Quoted 1 line

  I can extend it if needs be.

Well, if that deletion of code gets committed in time, then of course there's no real need to fiddle with it here.

This specific patch fixes a real bug on arm32 which will cause unsigned long + unsigned int to truncate together and permit certain values.

If you want to backport it, I should extend the change all the way down the call tree.

The rest of the cleanup really depends on the libxenforeignmem change, and ioctl fixes in the kernel, and probably aren't suitable for backport.

~Andrew

#6 lore raw
Quoted 6 lines · Andrew Cooper
Quoted 5 lines · Jan Beulich
Quoted 1 line · Andrew Cooper

What's the likelihood that you'll choose to backport this?

Didn't consider this aspect yet. I think I wouldn't have picked it without anyone asking for it to be backported.

Quoted 6 lines
Quoted 3 lines
Quoted 1 line

  I can extend it if needs be.

Well, if that deletion of code gets committed in time, then of course there's no real need to fiddle with it here.

This specific patch fixes a real bug on arm32 which will cause unsigned long + unsigned int to truncate together and permit certain values.

Why Arm32 only? Looking at current staging, there's no overflow check at all on the grant part of the path. A suitably large 64-bit "frame" will allow the same behavior on 64-bit (wrapping around through zero), afaict.

Jan

#7 lore raw
Quoted 8 lines · Jan Beulich
Quoted 7 lines · Andrew Cooper
Quoted 6 lines · Jan Beulich
Quoted 1 line · Andrew Cooper

What's the likelihood that you'll choose to backport this?

Didn't consider this aspect yet. I think I wouldn't have picked it without anyone asking for it to be backported.

Quoted 1 line

  I can extend it if needs be.

Quoted 8 lines
Quoted 4 lines
Quoted 2 lines

Well, if that deletion of code gets committed in time, then of course there's no real need to fiddle with it here.

This specific patch fixes a real bug on arm32 which will cause unsigned long + unsigned int to truncate together and permit certain values.

Why Arm32 only? Looking at current staging, there's no overflow check at all on the grant part of the path. A suitably large 64-bit "frame" will allow the same behavior on 64-bit (wrapping around through zero), afaict.

Very good point.  I'd worked the logic through logically at the end of my fixes, rather than at its position in the beginning of the series.

In which case I'll propagate through the whole call-tree.

~Andrew

Reviewed-by
#8 lore raw
Quoted 18 lines · Andrew Cooper
Quoted 8 lines · Jan Beulich
Quoted 7 lines · Andrew Cooper
Quoted 6 lines · Jan Beulich
Quoted 1 line · Andrew Cooper

What's the likelihood that you'll choose to backport this?

Didn't consider this aspect yet. I think I wouldn't have picked it without anyone asking for it to be backported.

Quoted 1 line

  I can extend it if needs be.

Quoted 8 lines
Quoted 4 lines
Quoted 2 lines

Well, if that deletion of code gets committed in time, then of course there's no real need to fiddle with it here.

This specific patch fixes a real bug on arm32 which will cause unsigned long + unsigned int to truncate together and permit certain values.

Why Arm32 only? Looking at current staging, there's no overflow check at all on the grant part of the path. A suitably large 64-bit "frame" will allow the same behavior on 64-bit (wrapping around through zero), afaict.

Quoted 4 lines

Very good point.  I'd worked the logic through logically at the end of my fixes, rather than at its position in the beginning of the series.

In which case I'll propagate through the whole call-tree.

Thanks. And then, as said,

And I guess I'll try to remember to pick this up for backporting once it has landed.

Jan

Reviewed-by on v1 1/1
#9 lore raw
In reply to Andrew Cooper

Hi Andrew,

Quoted 6 lines · Andrew Cooper

The ABI is unfortunate, and frame being 64 bits leads to all kinds of problems performing correct overflow checks.

Furthermore, the mixed use of unsigned int and unsigned long in the call tree is buggy on arm32 where the two are the same size, and certain out-of-range combinations will truncated and be permitted.

set_foreign_p2m_entry() always return -EOPNOTSUPP, so I think this would not need a backport if the issue is Arm only.

Quoted 1 line

Cheers,

Signature · 1 line
Julien Grall