Skip to contentSkip to conversation

xen/guest_access: Harden *copy_to_guest_offset() to prevent const dest operand

PATCH Superseded Julien Grall 2 participants 4 messages Apr 4, 2020 – Apr 7, 2020

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 xen/guest_access: Harden *copy_to_guest_offset() to prevent const dest operand

From: Julien Grall <[email protected]>

At the moment, *copy_to_guest_offset() will allow the hypervisor to copy data to guest handle marked const.

Thankfully, no users of the helper will do that. Rather than hoping this can be caught during review, harden copy_to_guest_offset() so the build will fail if such users are introduced.

There is no easy way to check whether a const is NULL in C99. The approach used is to introduce an unused variable that is non-const and assign the handle. If the handle were const, this would fail at build because without an explicit cast, it is not possible to assign a const variable to a non-const variable.

---
Changes in v2:
- Use a void * variable to check the handle is not const.

---
 xen/include/asm-arm/guest_access.h | 9 +++++++++
 xen/include/asm-x86/guest_access.h | 9 +++++++++
 2 files changed, 18 insertions(+)
xen/include/asm-arm/guest_access.h +9 -0
index 8997a1cbfe..4046d50347 100644
@@ -74,10 +74,14 @@ int access_guest_memory_by_ipa(struct domain *d, paddr_t ipa, void *buf,
 /*
  * Copy an array of objects to guest context via a guest handle,
  * specifying an offset into the guest array.
+ *
+ * The variable _t is only here to catch at build time whether we are
+ * copying back to a const guest handle.
  */
 #define copy_to_guest_offset(hnd, off, ptr, nr) ({      \
     const typeof(*(ptr)) *_s = (ptr);                   \
     char (*_d)[sizeof(*_s)] = (void *)(hnd).p;          \
+    void *__maybe_unused _t = (hnd).p;                  \
     ((void)((hnd).p == (ptr)));                         \
     raw_copy_to_guest(_d+(off), _s, sizeof(*_s)*(nr));  \
 })
@@ -124,9 +128,14 @@ int access_guest_memory_by_ipa(struct domain *d, paddr_t ipa, void *buf,
 #define guest_handle_okay(hnd, nr) (1)
 #define guest_handle_subrange_okay(hnd, first, last) (1)
 
+/*
+ * The variable _t is only here to catch at build time whether we are
+ * copying back to a const guest handle.
+ */
 #define __copy_to_guest_offset(hnd, off, ptr, nr) ({    \
     const typeof(*(ptr)) *_s = (ptr);                   \
     char (*_d)[sizeof(*_s)] = (void *)(hnd).p;          \
+    void *__maybe_unused _t = (hnd).p;                  \
     ((void)((hnd).p == (ptr)));                         \
     __raw_copy_to_guest(_d+(off), _s, sizeof(*_s)*(nr));\
 })
xen/include/asm-x86/guest_access.h +9 -0
index ca700c959a..0b58f2baee 100644
@@ -83,10 +83,14 @@
 /*
  * Copy an array of objects to guest context via a guest handle,
  * specifying an offset into the guest array.
+ *
+ * The variable _t is only here to catch at build time whether we are
+ * copying back to a const guest handle.
  */
 #define copy_to_guest_offset(hnd, off, ptr, nr) ({      \
     const typeof(*(ptr)) *_s = (ptr);                   \
     char (*_d)[sizeof(*_s)] = (void *)(hnd).p;          \
+    void *__maybe_unused _t = (hnd).p;                  \
     ((void)((hnd).p == (ptr)));                         \
     raw_copy_to_guest(_d+(off), _s, sizeof(*_s)*(nr));  \
 })
@@ -134,9 +138,14 @@
                      (last)-(first)+1,                  \
                      sizeof(*(hnd).p)))
 
+/*
+ * The variable _t is only here to catch at build time whether we are
+ * copying back to a const guest handle.
+ */
 #define __copy_to_guest_offset(hnd, off, ptr, nr) ({    \
     const typeof(*(ptr)) *_s = (ptr);                   \
     char (*_d)[sizeof(*_s)] = (void *)(hnd).p;          \
+    void *__maybe_unused _t = (hnd).p;                  \
     ((void)((hnd).p == (ptr)));                         \
     __raw_copy_to_guest(_d+(off), _s, sizeof(*_s)*(nr));\
 })
Signature · 1 line
2.17.1
#1 lore raw
Quoted 9 lines · Julien Grall

From: Julien Grall <[email protected]>

At the moment, *copy_to_guest_offset() will allow the hypervisor to copy data to guest handle marked const.

Thankfully, no users of the helper will do that. Rather than hoping this can be caught during review, harden copy_to_guest_offset() so the build will fail if such users are introduced.

Quoted 8 lines

There is no easy way to check whether a const is NULL in C99. The approach used is to introduce an unused variable that is non-const and assign the handle. If the handle were const, this would fail at build because without an explicit cast, it is not possible to assign a const variable to a non-const variable.

I'm not convinced it is a good idea to add (recurring) comments like you do - there are more aspects of these macros that would be worth commenting on, and commenting on some but not all may give the wrong impression of all subtleties being covered (also for others).

In any event I'd like to ask that each header gain such a comment only once, with the other being a tiny reference to the one "complete" instance.

Jan

#2 lore raw

Hi Jan,

Quoted 16 lines · Jan Beulich
Quoted 15 lines · Julien Grall

From: Julien Grall <[email protected]>

At the moment, *copy_to_guest_offset() will allow the hypervisor to copy data to guest handle marked const.

Thankfully, no users of the helper will do that. Rather than hoping this can be caught during review, harden copy_to_guest_offset() so the build will fail if such users are introduced.

There is no easy way to check whether a const is NULL in C99. The approach used is to introduce an unused variable that is non-const and assign the handle. If the handle were const, this would fail at build because without an explicit cast, it is not possible to assign a const variable to a non-const variable.

Quoted 8 lines
Quoted 2 lines

I'm not convinced it is a good idea to add (recurring) comments like you do - there are more aspects of these macros that would be worth commenting on, and commenting on some but not all may give the wrong impression of all subtleties being covered (also for others).

I thought you would say that, but I don't think I am the best person to describe all the other subtetly of the code. Yet I didn't want to not comment the oddity of using a maybe_unused variable.

Quoted 4 lines

In any event I'd like to ask that each header gain such a comment only once, with the other being a tiny reference to the one "complete" instance.

I am not entirely sure how this would look like. We would need to rely on _t having the same meaning across all the headers. This is quite easy to miss during review, so my preference still sticks to multiple comments.

Although I can reduce the size of the comment to one on top of the definition of _t. Something like: "Check if the handler is not const".

Cheers,

Signature · 1 line
Julien Grall
#3 lore raw
Quoted 28 lines · Julien Grall

Hi Jan,

Quoted 16 lines · Jan Beulich
Quoted 15 lines · Julien Grall

From: Julien Grall <[email protected]>

At the moment, *copy_to_guest_offset() will allow the hypervisor to copy data to guest handle marked const.

Thankfully, no users of the helper will do that. Rather than hoping this can be caught during review, harden copy_to_guest_offset() so the build will fail if such users are introduced.

There is no easy way to check whether a const is NULL in C99. The approach used is to introduce an unused variable that is non-const and assign the handle. If the handle were const, this would fail at build because without an explicit cast, it is not possible to assign a const variable to a non-const variable.

Quoted 8 lines
Quoted 2 lines

I'm not convinced it is a good idea to add (recurring) comments like you do - there are more aspects of these macros that would be worth commenting on, and commenting on some but not all may give the wrong impression of all subtleties being covered (also for others).

Quoted 4 lines

I thought you would say that, but I don't think I am the best person to describe all the other subtetly of the code. Yet I didn't want to not comment the oddity of using a maybe_unused variable.

Well, to me the "__maybe_unused" is telling enough.

Quoted 8 lines
Quoted 3 lines

In any event I'd like to ask that each header gain such a comment only once, with the other being a tiny reference to the one "complete" instance.

I am not entirely sure how this would look like. We would need to rely on _t having the same meaning across all the headers. This is quite easy to miss during review, so my preference still sticks to multiple comments.

Well, I did say "each header" exactly because of this.

Quoted 3 lines

Although I can reduce the size of the comment to one on top of the definition of _t. Something like: "Check if the handler is not const".

Ah yes, that would seem better (with s/handler/handle/ of course).

Jan