Skip to contentSkip to conversation

xen/arm: Don't use _end in is_xen_fixed_mfn()

PATCH Superseded Julien Grall 5 participants 16 messages Oct 15, 2019 – Oct 16, 2019

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 xen/arm: Don't use _end in is_xen_fixed_mfn()

virt_to_maddr() is using the hardware page-table walk instructions to translate a virtual address to physical address. The function should only be called on virtual address mapped.

_end points past the end of Xen binary and may not be mapped when the binary size is page-aligned. This means virt_to_maddr() will not be able to do the translation and therefore crash Xen.

Note there is also an off-by-one issue in this code, but the panic will trump that.

Both issues can be fixed by using _end - 1 in the check.

---

Cc Andrew Cooper <[email protected]>
Cc George Dunlap <[email protected]>
Cc Ian Jackson <[email protected]>
Cc Jan Beulich <[email protected]>
Cc Julien Grall <[email protected]>
Cc Konrad Rzeszutek Wilk <[email protected]>
Cc Stefano Stabellini <[email protected]>
Cc Tim Deegan <[email protected]>
Cc Wei Liu <[email protected]>

x86 seems to be affected by the off-by-one issue. Jan, Andrew?

This could be reached by a domain via XEN_SYSCTL_page_offline_op. However, the operation is not security supported (see XSA-77). So we are fine here.

---
 xen/include/asm-arm/mm.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
xen/include/asm-arm/mm.h +1 -1
index 262d92f18d..174acd8859 100644
@@ -153,7 +153,7 @@ extern unsigned long xenheap_base_pdx;
 
 #define is_xen_fixed_mfn(mfn)                                   \
     ((mfn_to_maddr(mfn) >= virt_to_maddr(&_start)) &&           \
-     (mfn_to_maddr(mfn) <= virt_to_maddr(&_end)))
+     (mfn_to_maddr(mfn) <= virt_to_maddr(_end - 1)))
 
 #define page_get_owner(_p)    (_p)->v.inuse.domain
 #define page_set_owner(_p,_d) ((_p)->v.inuse.domain = (_d))
Signature · 7 lines
2.11.0


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
#1 lore raw
Quoted 42 lines · Julien Grall

virt_to_maddr() is using the hardware page-table walk instructions to translate a virtual address to physical address. The function should only be called on virtual address mapped.

_end points past the end of Xen binary and may not be mapped when the binary size is page-aligned. This means virt_to_maddr() will not be able to do the translation and therefore crash Xen.

Note there is also an off-by-one issue in this code, but the panic will trump that.

Both issues can be fixed by using _end - 1 in the check.

---

Cc Andrew Cooper <[email protected]>
Cc George Dunlap <[email protected]>
Cc Ian Jackson <[email protected]>
Cc Jan Beulich <[email protected]>
Cc Julien Grall <[email protected]>
Cc Konrad Rzeszutek Wilk <[email protected]>
Cc Stefano Stabellini <[email protected]>
Cc Tim Deegan <[email protected]>
Cc Wei Liu <[email protected]>

x86 seems to be affected by the off-by-one issue. Jan, Andrew?

This could be reached by a domain via XEN_SYSCTL_page_offline_op. However, the operation is not security supported (see XSA-77). So we are fine here.

---
 xen/include/asm-arm/mm.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
xen/include/asm-arm/mm.h +0 -0
index 262d92f18d..174acd8859 100644

xen/include/asm-arm/mm.h extern unsigned long xenheap_base_pdx; · line 156

Quoted 5 lines
xen/include/asm-arm/mm.h +1 -1
@@ -153,7 +153,7 @@ extern unsigned long xenheap_base_pdx;
 
 #define is_xen_fixed_mfn(mfn)                                   \
     ((mfn_to_maddr(mfn) >= virt_to_maddr(&_start)) &&           \
-     (mfn_to_maddr(mfn) <= virt_to_maddr(&_end)))
+     (mfn_to_maddr(mfn) <= virt_to_maddr(_end - 1)))

Thank you for sending the patch and I think that "_end - 1" is the right fix. I am just wondering whether we want/need an explicit cast of some sort here, because technically _end is a char[] and 1 is a integer. Maybe:

((vaddr_t)_end - 1)

?

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#2 lore raw

Hi,

Quoted 54 lines · Stefano Stabellini
Quoted 42 lines · Julien Grall

virt_to_maddr() is using the hardware page-table walk instructions to translate a virtual address to physical address. The function should only be called on virtual address mapped.

_end points past the end of Xen binary and may not be mapped when the binary size is page-aligned. This means virt_to_maddr() will not be able to do the translation and therefore crash Xen.

Note there is also an off-by-one issue in this code, but the panic will trump that.

Both issues can be fixed by using _end - 1 in the check.

---

Cc Andrew Cooper <[email protected]>
Cc George Dunlap <[email protected]>
Cc Ian Jackson <[email protected]>
Cc Jan Beulich <[email protected]>
Cc Julien Grall <[email protected]>
Cc Konrad Rzeszutek Wilk <[email protected]>
Cc Stefano Stabellini <[email protected]>
Cc Tim Deegan <[email protected]>
Cc Wei Liu <[email protected]>

x86 seems to be affected by the off-by-one issue. Jan, Andrew?

This could be reached by a domain via XEN_SYSCTL_page_offline_op. However, the operation is not security supported (see XSA-77). So we are fine here.

---
  xen/include/asm-arm/mm.h | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
xen/include/asm-arm/mm.h +0 -0
index 262d92f18d..174acd8859 100644

xen/include/asm-arm/mm.h extern unsigned long xenheap_base_pdx; · line 156

Quoted 5 lines
xen/include/asm-arm/mm.h +1 -1
@@ -153,7 +153,7 @@ extern unsigned long xenheap_base_pdx;
 
  #define is_xen_fixed_mfn(mfn)                                   \
      ((mfn_to_maddr(mfn) >= virt_to_maddr(&_start)) &&           \
-     (mfn_to_maddr(mfn) <= virt_to_maddr(&_end)))
+     (mfn_to_maddr(mfn) <= virt_to_maddr(_end - 1)))

Thank you for sending the patch and I think that "_end - 1" is the right fix. I am just wondering whether we want/need an explicit cast of some sort here, because technically _end is a char[] and 1 is a integer. Maybe:

Quoted 3 lines
   ((vaddr_t)_end - 1)

?

I vaguely remember a lengthy discussion about it last year. But I don't think there was any conclusion in it.

In this case, what are you trying to prevent with the cast? Is it underflow of an array? If so, how the cast is actually going to prevent the compiler to do the wrong thing?

Cheers,

Signature · 5 lines
Julien Grall
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
#3 lore raw
Quoted 59 lines · Julien Grall

Hi,

Quoted 56 lines · Stefano Stabellini
Quoted 42 lines · Julien Grall

virt_to_maddr() is using the hardware page-table walk instructions to translate a virtual address to physical address. The function should only be called on virtual address mapped.

_end points past the end of Xen binary and may not be mapped when the binary size is page-aligned. This means virt_to_maddr() will not be able to do the translation and therefore crash Xen.

Note there is also an off-by-one issue in this code, but the panic will trump that.

Both issues can be fixed by using _end - 1 in the check.

---

Cc Andrew Cooper <[email protected]>
Cc George Dunlap <[email protected]>
Cc Ian Jackson <[email protected]>
Cc Jan Beulich <[email protected]>
Cc Julien Grall <[email protected]>
Cc Konrad Rzeszutek Wilk <[email protected]>
Cc Stefano Stabellini <[email protected]>
Cc Tim Deegan <[email protected]>
Cc Wei Liu <[email protected]>

x86 seems to be affected by the off-by-one issue. Jan, Andrew?

This could be reached by a domain via XEN_SYSCTL_page_offline_op. However, the operation is not security supported (see XSA-77). So we are fine here.

---
  xen/include/asm-arm/mm.h | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
xen/include/asm-arm/mm.h +0 -0
index 262d92f18d..174acd8859 100644

xen/include/asm-arm/mm.h extern unsigned long xenheap_base_pdx; · line 156

Quoted 5 lines
xen/include/asm-arm/mm.h +1 -1
@@ -153,7 +153,7 @@ extern unsigned long xenheap_base_pdx;
 
  #define is_xen_fixed_mfn(mfn)                                   \
      ((mfn_to_maddr(mfn) >= virt_to_maddr(&_start)) &&           \
-     (mfn_to_maddr(mfn) <= virt_to_maddr(&_end)))
+     (mfn_to_maddr(mfn) <= virt_to_maddr(_end - 1)))

Thank you for sending the patch and I think that "_end - 1" is the right fix. I am just wondering whether we want/need an explicit cast of some sort here, because technically _end is a char[] and 1 is a integer. Maybe:

   ((vaddr_t)_end - 1)
Quoted 8 lines
Quoted 1 line

?

I vaguely remember a lengthy discussion about it last year. But I don't think there was any conclusion in it.

In this case, what are you trying to prevent with the cast? Is it underflow of an array? If so, how the cast is actually going to prevent the compiler to do the wrong thing?

Yes, there was a long discussion at the beginning of the year; it was about how to define _start and _end so that we could avoid compilers undefined behavior. The main underlying issue is that comparisons between pointers to different objects are undefined [1] (_start and _end can be interpreted as pointers to different objects).

This case is a bit different, and easier. The issue is that, because the result of "_end - 1" is not within the boundaries of the _end array, then the operation is "undefined" by the C specification (C99 6.5.6). Undefined is not good.

So, I am not really asking for any complex fix, or hypervisor-wide rework. I am only asking to avoid introducing new undefined behavior. Casting to vaddr_t should solve it I think.

[1] https://marc.info/?l=xen-devel&m=154904722227312

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#4 lore raw
Quoted 75 lines · Stefano Stabellini
Quoted 59 lines · Julien Grall

Hi,

Quoted 56 lines · Stefano Stabellini
Quoted 42 lines · Julien Grall

virt_to_maddr() is using the hardware page-table walk instructions to translate a virtual address to physical address. The function should only be called on virtual address mapped.

_end points past the end of Xen binary and may not be mapped when the binary size is page-aligned. This means virt_to_maddr() will not be able to do the translation and therefore crash Xen.

Note there is also an off-by-one issue in this code, but the panic will trump that.

Both issues can be fixed by using _end - 1 in the check.

---

Cc Andrew Cooper <[email protected]>
Cc George Dunlap <[email protected]>
Cc Ian Jackson <[email protected]>
Cc Jan Beulich <[email protected]>
Cc Julien Grall <[email protected]>
Cc Konrad Rzeszutek Wilk <[email protected]>
Cc Stefano Stabellini <[email protected]>
Cc Tim Deegan <[email protected]>
Cc Wei Liu <[email protected]>

x86 seems to be affected by the off-by-one issue. Jan, Andrew?

This could be reached by a domain via XEN_SYSCTL_page_offline_op. However, the operation is not security supported (see XSA-77). So we are fine here.

---
   xen/include/asm-arm/mm.h | 2 +-
   1 file changed, 1 insertion(+), 1 deletion(-)
xen/include/asm-arm/mm.h +0 -0
index 262d92f18d..174acd8859 100644

xen/include/asm-arm/mm.h extern unsigned long xenheap_base_pdx; · line 156

Quoted 5 lines
xen/include/asm-arm/mm.h +1 -1
@@ -153,7 +153,7 @@ extern unsigned long xenheap_base_pdx;
 
   #define is_xen_fixed_mfn(mfn)                                   \
       ((mfn_to_maddr(mfn) >= virt_to_maddr(&_start)) &&           \
-     (mfn_to_maddr(mfn) <= virt_to_maddr(&_end)))
+     (mfn_to_maddr(mfn) <= virt_to_maddr(_end - 1)))

Thank you for sending the patch and I think that "_end - 1" is the right fix. I am just wondering whether we want/need an explicit cast of some sort here, because technically _end is a char[] and 1 is a integer. Maybe:

    ((vaddr_t)_end - 1)
Quoted 8 lines
Quoted 1 line

?

I vaguely remember a lengthy discussion about it last year. But I don't think there was any conclusion in it.

In this case, what are you trying to prevent with the cast? Is it underflow of an array? If so, how the cast is actually going to prevent the compiler to do the wrong thing?

Yes, there was a long discussion at the beginning of the year; it was about how to define _start and _end so that we could avoid compilers undefined behavior. The main underlying issue is that comparisons between pointers to different objects are undefined [1] (_start and _end can be interpreted as pointers to different objects).

Quoted 8 lines

This case is a bit different, and easier. The issue is that, because the result of "_end - 1" is not within the boundaries of the _end array, then the operation is "undefined" by the C specification (C99 6.5.6). Undefined is not good.

So, I am not really asking for any complex fix, or hypervisor-wide rework. I am only asking to avoid introducing new undefined behavior. Casting to vaddr_t should solve it I think.

I agree that we should not add more undefined behavior in Xen. However, I don't like cast if they can't be justified.

In this particular case, you seem to be unsure this is going to remove an undefined behavior. IIRC, I pointed out in the past that compiler can see through cast.

So can we have some certainty that your suggestion is going to work?

Cheers,

Quoted 3 lines

[1] https://marc.info/?l=xen-devel&m=154904722227312

Signature · 5 lines
Julien Grall
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
#5 lore raw
Quoted 85 lines · Julien Grall
Quoted 75 lines · Stefano Stabellini
Quoted 59 lines · Julien Grall

Hi,

Quoted 56 lines · Stefano Stabellini
Quoted 42 lines · Julien Grall

virt_to_maddr() is using the hardware page-table walk instructions to translate a virtual address to physical address. The function should only be called on virtual address mapped.

_end points past the end of Xen binary and may not be mapped when the binary size is page-aligned. This means virt_to_maddr() will not be able to do the translation and therefore crash Xen.

Note there is also an off-by-one issue in this code, but the panic will trump that.

Both issues can be fixed by using _end - 1 in the check.

---

Cc Andrew Cooper <[email protected]>
Cc George Dunlap <[email protected]>
Cc Ian Jackson <[email protected]>
Cc Jan Beulich <[email protected]>
Cc Julien Grall <[email protected]>
Cc Konrad Rzeszutek Wilk <[email protected]>
Cc Stefano Stabellini <[email protected]>
Cc Tim Deegan <[email protected]>
Cc Wei Liu <[email protected]>

x86 seems to be affected by the off-by-one issue. Jan, Andrew?

This could be reached by a domain via XEN_SYSCTL_page_offline_op. However, the operation is not security supported (see XSA-77). So we are fine here.

---
   xen/include/asm-arm/mm.h | 2 +-
   1 file changed, 1 insertion(+), 1 deletion(-)
xen/include/asm-arm/mm.h +0 -0
index 262d92f18d..174acd8859 100644

xen/include/asm-arm/mm.h extern unsigned long xenheap_base_pdx; · line 156

Quoted 5 lines
xen/include/asm-arm/mm.h +1 -1
@@ -153,7 +153,7 @@ extern unsigned long xenheap_base_pdx;
 
   #define is_xen_fixed_mfn(mfn)                                   \
       ((mfn_to_maddr(mfn) >= virt_to_maddr(&_start)) &&           \
-     (mfn_to_maddr(mfn) <= virt_to_maddr(&_end)))
+     (mfn_to_maddr(mfn) <= virt_to_maddr(_end - 1)))

Thank you for sending the patch and I think that "_end - 1" is the right fix. I am just wondering whether we want/need an explicit cast of some sort here, because technically _end is a char[] and 1 is a integer. Maybe:

    ((vaddr_t)_end - 1)
Quoted 8 lines
Quoted 1 line

?

I vaguely remember a lengthy discussion about it last year. But I don't think there was any conclusion in it.

In this case, what are you trying to prevent with the cast? Is it underflow of an array? If so, how the cast is actually going to prevent the compiler to do the wrong thing?

Yes, there was a long discussion at the beginning of the year; it was about how to define _start and _end so that we could avoid compilers undefined behavior. The main underlying issue is that comparisons between pointers to different objects are undefined [1] (_start and _end can be interpreted as pointers to different objects).

Quoted 8 lines

This case is a bit different, and easier. The issue is that, because the result of "_end - 1" is not within the boundaries of the _end array, then the operation is "undefined" by the C specification (C99 6.5.6). Undefined is not good.

So, I am not really asking for any complex fix, or hypervisor-wide rework. I am only asking to avoid introducing new undefined behavior. Casting to vaddr_t should solve it I think.

Quoted 8 lines

I agree that we should not add more undefined behavior in Xen. However, I don't like cast if they can't be justified.

In this particular case, you seem to be unsure this is going to remove an undefined behavior. IIRC, I pointed out in the past that compiler can see through cast.

So can we have some certainty that your suggestion is going to work?

My suggestion is going to work: "the compiler sees through casts" referred to comparisons between pointers, where we temporarily casted both pointers to integers and back to pointers via a MACRO. That case was iffy because the MACRO was clearly a workaround the spec.

Here the situation is different. For one, we are doing arithmetic. Also virt_to_maddr already takes a vaddr_t as argument. So instead of doing pointers arithmetic, then converting to vaddr_t, we are converting to vaddr_t first, then doing arithmetics, which is fine both from a C99 point of view and even a MISRA C point of view. I can't see a problem with that. I am sure as I reasonable can be :-)

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#6 lore raw

Stefano Stabellini writes ("Re: [PATCH for-4.13] xen/arm: Don't use _end in is_xen_fixed_mfn()"):

Quoted 11 lines

My suggestion is going to work: "the compiler sees through casts" referred to comparisons between pointers, where we temporarily casted both pointers to integers and back to pointers via a MACRO. That case was iffy because the MACRO was clearly a workaround the spec.

Here the situation is different. For one, we are doing arithmetic. Also virt_to_maddr already takes a vaddr_t as argument. So instead of doing pointers arithmetic, then converting to vaddr_t, we are converting to vaddr_t first, then doing arithmetics, which is fine both from a C99 point of view and even a MISRA C point of view. I can't see a problem with that. I am sure as I reasonable can be :-)

FTAOD I think you are suggesting this:
- + (mfn_to_maddr(mfn) <= virt_to_maddr(_end - 1)))
+ + (mfn_to_maddr(mfn) <= virt_to_maddr(((vaddr_t)_end - 1)))

virt_to_maddr(va) is a macro which expands to
__virt_to_maddr((vaddr_t)(va))

So what is happening here is that the cast to an integer type is being done before the subtraction.

Without the cast, you are calculating the pointer value _end-1 from the value _end, which is UB. With the cast you are calculating an integer value. vaddr_t is unsigned, so all arithmetic operations are defined. Nothing casts the result back to the "forbidden" (with this provenance) pointer value, so all is well.

(With the macro expansion the cast happens twice. This is probably better than using __virt_to_maddr here.)

Ie, in this case I agree with Stefano. The cast is both necessary and sufficient.

Ian.

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#7 lore raw
Quoted 29 lines · Ian Jackson

Stefano Stabellini writes ("Re: [PATCH for-4.13] xen/arm: Don't use _end in is_xen_fixed_mfn()"):

Quoted 11 lines

My suggestion is going to work: "the compiler sees through casts" referred to comparisons between pointers, where we temporarily casted both pointers to integers and back to pointers via a MACRO. That case was iffy because the MACRO was clearly a workaround the spec.

Here the situation is different. For one, we are doing arithmetic. Also virt_to_maddr already takes a vaddr_t as argument. So instead of doing pointers arithmetic, then converting to vaddr_t, we are converting to vaddr_t first, then doing arithmetics, which is fine both from a C99 point of view and even a MISRA C point of view. I can't see a problem with that. I am sure as I reasonable can be :-)

FTAOD I think you are suggesting this:
- + (mfn_to_maddr(mfn) <= virt_to_maddr(_end - 1)))
+ + (mfn_to_maddr(mfn) <= virt_to_maddr(((vaddr_t)_end - 1)))

virt_to_maddr(va) is a macro which expands to
__virt_to_maddr((vaddr_t)(va))

So what is happening here is that the cast to an integer type is being done before the subtraction.

Without the cast, you are calculating the pointer value _end-1 from the value _end, which is UB. With the cast you are calculating an integer value. vaddr_t is unsigned, so all arithmetic operations are defined. Nothing casts the result back to the "forbidden" (with this provenance) pointer value, so all is well.

Quoted 5 lines

(With the macro expansion the cast happens twice. This is probably better than using __virt_to_maddr here.)

Ie, in this case I agree with Stefano. The cast is both necessary and sufficient.

Maybe I missed something, but why are we using <= at all? Why not use <?

Or is this some weird C pointer comparison UB thing?

-George

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#8 lore raw

Hi George,

Quoted 33 lines · George Dunlap
Quoted 32 lines · Ian Jackson

Stefano Stabellini writes ("Re: [PATCH for-4.13] xen/arm: Don't use _end in is_xen_fixed_mfn()"):

Quoted 11 lines

My suggestion is going to work: "the compiler sees through casts" referred to comparisons between pointers, where we temporarily casted both pointers to integers and back to pointers via a MACRO. That case was iffy because the MACRO was clearly a workaround the spec.

Here the situation is different. For one, we are doing arithmetic. Also virt_to_maddr already takes a vaddr_t as argument. So instead of doing pointers arithmetic, then converting to vaddr_t, we are converting to vaddr_t first, then doing arithmetics, which is fine both from a C99 point of view and even a MISRA C point of view. I can't see a problem with that. I am sure as I reasonable can be :-)

FTAOD I think you are suggesting this:
- + (mfn_to_maddr(mfn) <= virt_to_maddr(_end - 1)))
+ + (mfn_to_maddr(mfn) <= virt_to_maddr(((vaddr_t)_end - 1)))

virt_to_maddr(va) is a macro which expands to

    __virt_to_maddr((vaddr_t)(va))

So what is happening here is that the cast to an integer type is being done before the subtraction.

Without the cast, you are calculating the pointer value _end-1 from the value _end, which is UB. With the cast you are calculating an integer value. vaddr_t is unsigned, so all arithmetic operations are defined. Nothing casts the result back to the "forbidden" (with this provenance) pointer value, so all is well.

(With the macro expansion the cast happens twice. This is probably better than using __virt_to_maddr here.)

Quoted 7 lines
Quoted 2 lines

Ie, in this case I agree with Stefano. The cast is both necessary and sufficient.

Maybe I missed something, but why are we using <= at all? Why not use <?

Or is this some weird C pointer comparison UB thing?

_end may not be mapped in the virtual address space. This is the case when the size of Xen is page-aligned. So _end will point to the next page.

virt_to_maddr() cannot fail so it should only be called on valid virtual address. The behavior is undefined in all the other cases.

On x86, you might be able to get away because you translate the virtual address to physical address in software.

On Arm, we are using the hardware instruction to do the translation. As _end is not always mapped, then the translation may fail. In other word, Xen will crash.

Cheers,

Signature · 6 lines
Julien Grall

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
#9 lore raw
Quoted 51 lines · Julien Grall

Hi George,

Quoted 34 lines · George Dunlap
Quoted 33 lines · Ian Jackson

Stefano Stabellini writes ("Re: [PATCH for-4.13] xen/arm: Don't use _end in is_xen_fixed_mfn()"):

Quoted 11 lines

My suggestion is going to work: "the compiler sees through casts" referred to comparisons between pointers, where we temporarily casted both pointers to integers and back to pointers via a MACRO. That case was iffy because the MACRO was clearly a workaround the spec.

Here the situation is different. For one, we are doing arithmetic. Also virt_to_maddr already takes a vaddr_t as argument. So instead of doing pointers arithmetic, then converting to vaddr_t, we are converting to vaddr_t first, then doing arithmetics, which is fine both from a C99 point of view and even a MISRA C point of view. I can't see a problem with that. I am sure as I reasonable can be :-)

FTAOD I think you are suggesting this:
  - +     (mfn_to_maddr(mfn) <= virt_to_maddr(_end - 1)))
  + +     (mfn_to_maddr(mfn) <= virt_to_maddr(((vaddr_t)_end - 1)))

virt_to_maddr(va) is a macro which expands to
    __virt_to_maddr((vaddr_t)(va))

So what is happening here is that the cast to an integer type is being done before the subtraction.

Without the cast, you are calculating the pointer value _end-1 from the value _end, which is UB.  With the cast you are calculating an integer value.  vaddr_t is unsigned, so all arithmetic operations are defined.  Nothing casts the result back to the "forbidden" (with this provenance) pointer value, so all is well.

(With the macro expansion the cast happens twice.  This is probably better than using __virt_to_maddr here.)

Quoted 7 lines
Quoted 2 lines

Ie, in this case I agree with Stefano.  The cast is both necessary and sufficient.

Maybe I missed something, but why are we using <= at all?  Why not use <?

Or is this some weird C pointer comparison UB thing?

_end may not be mapped in the virtual address space. This is the case when the size of Xen is page-aligned. So _end will point to the next page.

virt_to_maddr() cannot fail so it should only be called on valid virtual address. The behavior is undefined in all the other cases.

Quoted 6 lines

On x86, you might be able to get away because you translate the virtual address to physical address in software.

On Arm, we are using the hardware instruction to do the translation. As _end is not always mapped, then the translation may fail. In other word, Xen will crash.

None of this explains my question.

Is it not the case that if mfn_to_maddr(mfn) <= virt_to_maddr(_end-1) is true, then mfn_to_maddr(mfn) < virt_to_maddr(_end) will be true, and that if mfn_to_maddr(mfn) <= virt_to_maddr(_end-1) is false, then mfn_to_maddr(mfn) < virt_to_maddr(_end) will also be false?

Under what conditions would they be different?

And if they're the same, then you can just use < instead of <=, and not have to worry about casting before subtracting.

-George

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#10 lore raw
Quoted 59 lines · George Dunlap
Quoted 51 lines · Julien Grall

Hi George,

Quoted 34 lines · George Dunlap
Quoted 33 lines · Ian Jackson

Stefano Stabellini writes ("Re: [PATCH for-4.13] xen/arm: Don't use _end in is_xen_fixed_mfn()"):

Quoted 11 lines

My suggestion is going to work: "the compiler sees through casts" referred to comparisons between pointers, where we temporarily casted both pointers to integers and back to pointers via a MACRO. That case was iffy because the MACRO was clearly a workaround the spec.

Here the situation is different. For one, we are doing arithmetic. Also virt_to_maddr already takes a vaddr_t as argument. So instead of doing pointers arithmetic, then converting to vaddr_t, we are converting to vaddr_t first, then doing arithmetics, which is fine both from a C99 point of view and even a MISRA C point of view. I can't see a problem with that. I am sure as I reasonable can be :-)

FTAOD I think you are suggesting this:
  - +     (mfn_to_maddr(mfn) <= virt_to_maddr(_end - 1)))
  + +     (mfn_to_maddr(mfn) <= virt_to_maddr(((vaddr_t)_end - 1)))

virt_to_maddr(va) is a macro which expands to
    __virt_to_maddr((vaddr_t)(va))

So what is happening here is that the cast to an integer type is being done before the subtraction.

Without the cast, you are calculating the pointer value _end-1 from the value _end, which is UB.  With the cast you are calculating an integer value.  vaddr_t is unsigned, so all arithmetic operations are defined.  Nothing casts the result back to the "forbidden" (with this provenance) pointer value, so all is well.

(With the macro expansion the cast happens twice.  This is probably better than using __virt_to_maddr here.)

Quoted 7 lines
Quoted 2 lines

Ie, in this case I agree with Stefano.  The cast is both necessary and sufficient.

Maybe I missed something, but why are we using <= at all?  Why not use <?

Or is this some weird C pointer comparison UB thing?

_end may not be mapped in the virtual address space. This is the case when the size of Xen is page-aligned. So _end will point to the next page.

virt_to_maddr() cannot fail so it should only be called on valid virtual address. The behavior is undefined in all the other cases.

Quoted 6 lines

On x86, you might be able to get away because you translate the virtual address to physical address in software.

On Arm, we are using the hardware instruction to do the translation. As _end is not always mapped, then the translation may fail. In other word, Xen will crash.

Quoted 8 lines

None of this explains my question.

Is it not the case that if mfn_to_maddr(mfn) <= virt_to_maddr(_end-1) is true, then mfn_to_maddr(mfn) < virt_to_maddr(_end) will be true, and that if mfn_to_maddr(mfn) <= virt_to_maddr(_end-1) is false, then mfn_to_maddr(mfn) < virt_to_maddr(_end) will also be false?

Under what conditions would they be different?

In case virt_to_maddr(_end) is undefined due to no translation being available?

Juergen

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#11 lore raw
Quoted 71 lines · Jürgen Groß
Quoted 70 lines · George Dunlap
Quoted 55 lines · Julien Grall

Hi George,

Quoted 36 lines · George Dunlap
Quoted 35 lines · Ian Jackson

Stefano Stabellini writes ("Re: [PATCH for-4.13] xen/arm: Don't use _end in is_xen_fixed_mfn()"):

Quoted 5 lines

My suggestion is going to work: "the compiler sees through casts" referred to comparisons between pointers, where we temporarily casted both pointers to integers and back to pointers via a MACRO. That case was iffy because the MACRO was clearly a workaround the spec.

Quoted 8 lines

Here the situation is different. For one, we are doing arithmetic. Also
virt_to_maddr already takes a vaddr_t as argument. So instead of doing
pointers arithmetic, then converting to vaddr_t, we are converting to vaddr_t first, then doing arithmetics, which is fine both from a C99 point of view and even a MISRA C point of view. I can't see a problem with that. I am sure as I reasonable can be :-)

FTAOD I think you are suggesting this:
   - +     (mfn_to_maddr(mfn) <= virt_to_maddr(_end - 1)))
   + +     (mfn_to_maddr(mfn) <= virt_to_maddr(((vaddr_t)_end - 1)))

virt_to_maddr(va) is a macro which expands to
     __virt_to_maddr((vaddr_t)(va))

So what is happening here is that the cast to an integer type is being done before the subtraction.

Without the cast, you are calculating the pointer value _end-1 from the value _end, which is UB.  With the cast you are calculating an integer value.  vaddr_t is unsigned, so all arithmetic operations are defined.  Nothing casts the result back to the "forbidden" (with this provenance) pointer value, so all is well.

(With the macro expansion the cast happens twice.  This is probably better than using __virt_to_maddr here.)

Quoted 8 lines
Quoted 2 lines

Ie, in this case I agree with Stefano.  The cast is both necessary and sufficient.

Maybe I missed something, but why are we using <= at all?  Why not use
<?

Or is this some weird C pointer comparison UB thing?

_end may not be mapped in the virtual address space. This is the case when the size of Xen is page-aligned. So _end will point to the next page.

virt_to_maddr() cannot fail so it should only be called on valid virtual address. The behavior is undefined in all the other cases.

Quoted 6 lines

On x86, you might be able to get away because you translate the virtual address to physical address in software.

On Arm, we are using the hardware instruction to do the translation. As _end is not always mapped, then the translation may fail. In other word, Xen will crash.

None of this explains my question.

Is it not the case that if mfn_to_maddr(mfn) <= virt_to_maddr(_end-1) is true, then mfn_to_maddr(mfn) < virt_to_maddr(_end) will be true, and that if mfn_to_maddr(mfn) <= virt_to_maddr(_end-1) is false, then mfn_to_maddr(mfn) < virt_to_maddr(_end) will also be false?

Quoted 4 lines
Quoted 1 line

Under what conditions would they be different?

In case virt_to_maddr(_end) is undefined due to no translation being available?

Ah, gotcha. Sorry for the noise.

-George

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#12 lore raw
In reply to George Dunlap

Hi George,

Quoted 64 lines · George Dunlap
Quoted 49 lines · Julien Grall
Quoted 34 lines · George Dunlap
Quoted 33 lines · Ian Jackson

Stefano Stabellini writes ("Re: [PATCH for-4.13] xen/arm: Don't use _end in is_xen_fixed_mfn()"):

Quoted 11 lines

My suggestion is going to work: "the compiler sees through casts" referred to comparisons between pointers, where we temporarily casted both pointers to integers and back to pointers via a MACRO. That case was iffy because the MACRO was clearly a workaround the spec.

Here the situation is different. For one, we are doing arithmetic. Also virt_to_maddr already takes a vaddr_t as argument. So instead of doing pointers arithmetic, then converting to vaddr_t, we are converting to vaddr_t first, then doing arithmetics, which is fine both from a C99 point of view and even a MISRA C point of view. I can't see a problem with that. I am sure as I reasonable can be :-)

FTAOD I think you are suggesting this:
  - +     (mfn_to_maddr(mfn) <= virt_to_maddr(_end - 1)))
  + +     (mfn_to_maddr(mfn) <= virt_to_maddr(((vaddr_t)_end - 1)))

virt_to_maddr(va) is a macro which expands to
    __virt_to_maddr((vaddr_t)(va))

So what is happening here is that the cast to an integer type is being done before the subtraction.

Without the cast, you are calculating the pointer value _end-1 from the value _end, which is UB.  With the cast you are calculating an integer value.  vaddr_t is unsigned, so all arithmetic operations are defined.  Nothing casts the result back to the "forbidden" (with this provenance) pointer value, so all is well.

(With the macro expansion the cast happens twice.  This is probably better than using __virt_to_maddr here.)

Quoted 7 lines
Quoted 2 lines

Ie, in this case I agree with Stefano.  The cast is both necessary and sufficient.

Maybe I missed something, but why are we using <= at all?  Why not use <?

Or is this some weird C pointer comparison UB thing?

_end may not be mapped in the virtual address space. This is the case when the size of Xen is page-aligned. So _end will point to the next page.

virt_to_maddr() cannot fail so it should only be called on valid virtual address. The behavior is undefined in all the other cases.

Quoted 6 lines

On x86, you might be able to get away because you translate the virtual address to physical address in software.

On Arm, we are using the hardware instruction to do the translation. As _end is not always mapped, then the translation may fail. In other word, Xen will crash.

None of this explains my question.

Is it not the case that if mfn_to_maddr(mfn) <= virt_to_maddr(_end-1) is true, then mfn_to_maddr(mfn) < virt_to_maddr(_end) will be true, and that if mfn_to_maddr(mfn) <= virt_to_maddr(_end-1) is false, then mfn_to_maddr(mfn) < virt_to_maddr(_end) will also be false?

Quoted 4 lines

Under what conditions would they be different?

And if they're the same, then you can just use < instead of <=, and not have to worry about casting before subtracting.

_end is not part of the binary but points one past it. So there is no guarantee this address will be mapped in the virtual address space.

As I pointed out in my previous e-mail the result of virt_to_maddr() will be undefined in this case. On Arm, this will actually crash Xen.

So you can't use '<' here.

Cheers,

Signature · 6 lines
Julien Grall

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
#13 lore raw
In reply to Ian Jackson

Hi,

Quoted 29 lines · Ian Jackson

Stefano Stabellini writes ("Re: [PATCH for-4.13] xen/arm: Don't use _end in is_xen_fixed_mfn()"):

Quoted 11 lines

My suggestion is going to work: "the compiler sees through casts" referred to comparisons between pointers, where we temporarily casted both pointers to integers and back to pointers via a MACRO. That case was iffy because the MACRO was clearly a workaround the spec.

Here the situation is different. For one, we are doing arithmetic. Also virt_to_maddr already takes a vaddr_t as argument. So instead of doing pointers arithmetic, then converting to vaddr_t, we are converting to vaddr_t first, then doing arithmetics, which is fine both from a C99 point of view and even a MISRA C point of view. I can't see a problem with that. I am sure as I reasonable can be :-)

FTAOD I think you are suggesting this:
- + (mfn_to_maddr(mfn) <= virt_to_maddr(_end - 1)))
+ + (mfn_to_maddr(mfn) <= virt_to_maddr(((vaddr_t)_end - 1)))

virt_to_maddr(va) is a macro which expands to

    __virt_to_maddr((vaddr_t)(va))

So what is happening here is that the cast to an integer type is being done before the subtraction.

Without the cast, you are calculating the pointer value _end-1 from the value _end, which is UB. With the cast you are calculating an integer value. vaddr_t is unsigned, so all arithmetic operations are defined. Nothing casts the result back to the "forbidden" (with this provenance) pointer value, so all is well.

Quoted 5 lines

(With the macro expansion the cast happens twice. This is probably better than using __virt_to_maddr here.)

Ie, in this case I agree with Stefano. The cast is both necessary and sufficient.

Thank you both for the explanation. I will update the patch and resend it.

Cheers,

Signature · 6 lines
Julien Grall

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
#14 lore raw
In reply to Ian Jackson
Quoted 29 lines · Ian Jackson

Stefano Stabellini writes ("Re: [PATCH for-4.13] xen/arm: Don't use _end in is_xen_fixed_mfn()"):

Quoted 11 lines

My suggestion is going to work: "the compiler sees through casts" referred to comparisons between pointers, where we temporarily casted both pointers to integers and back to pointers via a MACRO. That case was iffy because the MACRO was clearly a workaround the spec.

Here the situation is different. For one, we are doing arithmetic. Also virt_to_maddr already takes a vaddr_t as argument. So instead of doing pointers arithmetic, then converting to vaddr_t, we are converting to vaddr_t first, then doing arithmetics, which is fine both from a C99 point of view and even a MISRA C point of view. I can't see a problem with that. I am sure as I reasonable can be :-)

FTAOD I think you are suggesting this:
- + (mfn_to_maddr(mfn) <= virt_to_maddr(_end - 1)))
+ + (mfn_to_maddr(mfn) <= virt_to_maddr(((vaddr_t)_end - 1)))

virt_to_maddr(va) is a macro which expands to
__virt_to_maddr((vaddr_t)(va))

So what is happening here is that the cast to an integer type is being done before the subtraction.

Without the cast, you are calculating the pointer value _end-1 from the value _end, which is UB. With the cast you are calculating an integer value. vaddr_t is unsigned, so all arithmetic operations are defined. Nothing casts the result back to the "forbidden" (with this provenance) pointer value, so all is well.

Quoted 5 lines

(With the macro expansion the cast happens twice. This is probably better than using __virt_to_maddr here.)

Ie, in this case I agree with Stefano. The cast is both necessary and sufficient.

Thanks Ian for the second pair of eyes!

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#15 lore raw
In reply to Julien Grall
Quoted 8 lines · Julien Grall

virt_to_maddr() is using the hardware page-table walk instructions to translate a virtual address to physical address. The function should only be called on virtual address mapped.

_end points past the end of Xen binary and may not be mapped when the binary size is page-aligned. This means virt_to_maddr() will not be able to do the translation and therefore crash Xen.

Quoted 6 lines

Note there is also an off-by-one issue in this code, but the panic will trump that.

Both issues can be fixed by using _end - 1 in the check.

With or without the cast suggested by Stefano:

Juergen

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel