Skip to contentSkip to conversation

tools/libxencall: enforce proper alignment of hypercall buffers

PATCH In review Juergen Gross 3 participants 5 messages Jun 8, 2018 – Jul 10, 2018

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 tools/libxencall: enforce proper alignment of hypercall buffers

xencall_alloc_buffer() is used throughout Xen tools for allocating hypercall buffers. Allocation is done at page granularity. For simple administration each allocated set of pages contains a small header holding the number of pages of that set. The hypercall buffer is located directly after the 4 byte sized header, leading to a wrong alignment for e.g. pointers.

Repair that by using a 16 byte sized header enforcing the same alignment as malloc().

---
Not sure whether this wants to be backported. In case the answer is "yes" I think it should be part of 4.11.

---
 tools/libs/call/buffer.c | 1 +
 1 file changed, 1 insertion(+)
tools/libs/call/buffer.c +1 -0
index 2d8fc29ac6..0b6af2db60 100644
@@ -151,6 +151,7 @@ void xencall_free_buffer_pages(xencall_handle *xcall, void *p, size_t nr_pages)
 
 struct allocation_header {
     int nr_pages;
+    int pad[3];
 };
 
 void *xencall_alloc_buffer(xencall_handle *xcall, size_t size)
Signature · 7 lines
2.13.7


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

xencall_alloc_buffer() is used throughout Xen tools for allocating hypercall buffers. Allocation is done at page granularity. For simple administration each allocated set of pages contains a small header holding the number of pages of that set. The hypercall buffer is located directly after the 4 byte sized header, leading to a wrong alignment for e.g. pointers.

Repair that by using a 16 byte sized header enforcing the same alignment as malloc().

---
Not sure whether this wants to be backported. In case the answer is "yes" I think it should be part of 4.11.

---
 tools/libs/call/buffer.c | 1 +
 1 file changed, 1 insertion(+)
tools/libs/call/buffer.c +0 -0
index 2d8fc29ac6..0b6af2db60 100644

tools/libs/call/buffer.c void xencall_free_buffer_pages(xencall_handle *xcall, void *p, size_t nr_pages) · line 153

Quoted 3 lines
tools/libs/call/buffer.c +0 -0
@@ -151,6 +151,7 @@ void xencall_free_buffer_pages(xencall_handle *xcall, void *p, size_t nr_pages)
 
 struct allocation_header {
     int nr_pages;

This hunk reveals another bug...

There are a rather large number of size_t => int truncations in the code, which is surely going to catch people by surprise.  (Again - I was certain I commented on this during the original review of this library.)

tools/libs/call/buffer.c void xencall_free_buffer_pages(xencall_handle *xcall, void *p, size_t nr_pages) · line 154

Quoted 1 line
tools/libs/call/buffer.c +1 -0
+    int pad[3];

This brings it to 16 byte alignment, but is that enough?  An xsave state block in x86 for example has 64 byte alignment.  I suppose we mostly care about ARM here, as its memcpy() will be most impacted by this misalignment.

Irrespective, might be easier to accomplish with an __attribute__((__aligned__($N))) rather than explicit padding?

~Andrew

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

#2 lore raw
Quoted 25 lines · Andrew Cooper
Quoted 24 lines · Juergen Gross

xencall_alloc_buffer() is used throughout Xen tools for allocating hypercall buffers. Allocation is done at page granularity. For simple administration each allocated set of pages contains a small header holding the number of pages of that set. The hypercall buffer is located directly after the 4 byte sized header, leading to a wrong alignment for e.g. pointers.

Repair that by using a 16 byte sized header enforcing the same alignment as malloc().

---
Not sure whether this wants to be backported. In case the answer is "yes" I think it should be part of 4.11.

---
 tools/libs/call/buffer.c | 1 +
 1 file changed, 1 insertion(+)
tools/libs/call/buffer.c +0 -0
index 2d8fc29ac6..0b6af2db60 100644
@@ -151,6 +151,7 @@ void xencall_free_buffer_pages(xencall_handle *xcall, void *p, size_t nr_pages)
 
Quoted 8 lines

tools/libs/call/buffer.c void xencall_free_buffer_pages(xencall_handle *xcall, void *p, size_t nr_pages) · line 153

Quoted 2 lines
tools/libs/call/buffer.c +0 -0
 struct allocation_header {
     int nr_pages;

This hunk reveals another bug...

There are a rather large number of size_t => int truncations in the code, which is surely going to catch people by surprise.  (Again - I was certain I commented on this during the original review of this library.)

On the practical side I don't think this really matters here. Passing more than 2GB as a hypercall buffer is nothing we want to support...

Quoted 7 lines

tools/libs/call/buffer.c void xencall_free_buffer_pages(xencall_handle *xcall, void *p, size_t nr_pages) · line 154

Quoted 2 lines
tools/libs/call/buffer.c +1 -0
+    int pad[3];

This brings it to 16 byte alignment, but is that enough?  An xsave state block in x86 for example has 64 byte alignment.  I suppose we mostly care about ARM here, as its memcpy() will be most impacted by this misalignment.

Special purpose buffers can still be directly allocated via xencall_alloc_buffer_pages(). I don't think we'll need alignment better than malloc().

Quoted 2 lines

Irrespective, might be easier to accomplish with an __attribute__((__aligned__($N))) rather than explicit padding?

I don't mind which way to go here. I'll leave the decision for the maintainers. :-)

Juergen

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

#3 lore raw
Quoted 50 lines · Juergen Gross
Quoted 25 lines · Andrew Cooper
Quoted 24 lines · Juergen Gross

xencall_alloc_buffer() is used throughout Xen tools for allocating hypercall buffers. Allocation is done at page granularity. For simple administration each allocated set of pages contains a small header holding the number of pages of that set. The hypercall buffer is located directly after the 4 byte sized header, leading to a wrong alignment for e.g. pointers.

Repair that by using a 16 byte sized header enforcing the same alignment as malloc().

---
Not sure whether this wants to be backported. In case the answer is "yes" I think it should be part of 4.11.

---
 tools/libs/call/buffer.c | 1 +
 1 file changed, 1 insertion(+)
tools/libs/call/buffer.c +0 -0
index 2d8fc29ac6..0b6af2db60 100644
@@ -151,6 +151,7 @@ void xencall_free_buffer_pages(xencall_handle *xcall, void *p, size_t nr_pages)
 
Quoted 8 lines

tools/libs/call/buffer.c void xencall_free_buffer_pages(xencall_handle *xcall, void *p, size_t nr_pages) · line 153

Quoted 2 lines
tools/libs/call/buffer.c +0 -0
 struct allocation_header {
     int nr_pages;

This hunk reveals another bug...

There are a rather large number of size_t => int truncations in the code, which is surely going to catch people by surprise.  (Again - I was certain I commented on this during the original review of this library.)

On the practical side I don't think this really matters here. Passing more than 2GB as a hypercall buffer is nothing we want to support...

Quoted 7 lines

tools/libs/call/buffer.c void xencall_free_buffer_pages(xencall_handle *xcall, void *p, size_t nr_pages) · line 154

Quoted 2 lines
tools/libs/call/buffer.c +1 -0
+    int pad[3];

This brings it to 16 byte alignment, but is that enough?  An xsave state block in x86 for example has 64 byte alignment.  I suppose we mostly care about ARM here, as its memcpy() will be most impacted by this misalignment.

Special purpose buffers can still be directly allocated via xencall_alloc_buffer_pages(). I don't think we'll need alignment better than malloc().

Quoted 5 lines
Quoted 2 lines

Irrespective, might be easier to accomplish with an __attribute__((__aligned__($N))) rather than explicit padding?

I don't mind which way to go here. I'll leave the decision for the maintainers. :-)

Ping?

Juergen

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

#4 lore raw
Quoted 51 lines · Juergen Gross
Quoted 50 lines · Juergen Gross
Quoted 25 lines · Andrew Cooper
Quoted 24 lines · Juergen Gross

xencall_alloc_buffer() is used throughout Xen tools for allocating hypercall buffers. Allocation is done at page granularity. For simple administration each allocated set of pages contains a small header holding the number of pages of that set. The hypercall buffer is located directly after the 4 byte sized header, leading to a wrong alignment for e.g. pointers.

Repair that by using a 16 byte sized header enforcing the same alignment as malloc().

---
Not sure whether this wants to be backported. In case the answer is "yes" I think it should be part of 4.11.

---
 tools/libs/call/buffer.c | 1 +
 1 file changed, 1 insertion(+)
tools/libs/call/buffer.c +0 -0
index 2d8fc29ac6..0b6af2db60 100644
@@ -151,6 +151,7 @@ void xencall_free_buffer_pages(xencall_handle *xcall, void *p, size_t nr_pages)
 
Quoted 8 lines

tools/libs/call/buffer.c void xencall_free_buffer_pages(xencall_handle *xcall, void *p, size_t nr_pages) · line 153

Quoted 2 lines
tools/libs/call/buffer.c +0 -0
 struct allocation_header {
     int nr_pages;

This hunk reveals another bug...

There are a rather large number of size_t => int truncations in the code, which is surely going to catch people by surprise.  (Again - I was certain I commented on this during the original review of this library.)

On the practical side I don't think this really matters here. Passing more than 2GB as a hypercall buffer is nothing we want to support...

Quoted 7 lines

tools/libs/call/buffer.c void xencall_free_buffer_pages(xencall_handle *xcall, void *p, size_t nr_pages) · line 154

Quoted 2 lines
tools/libs/call/buffer.c +1 -0
+    int pad[3];

This brings it to 16 byte alignment, but is that enough?  An xsave state block in x86 for example has 64 byte alignment.  I suppose we mostly care about ARM here, as its memcpy() will be most impacted by this misalignment.

Special purpose buffers can still be directly allocated via xencall_alloc_buffer_pages(). I don't think we'll need alignment better than malloc().

Quoted 7 lines
Quoted 5 lines
Quoted 2 lines

Irrespective, might be easier to accomplish with an __attribute__((__aligned__($N))) rather than explicit padding?

I don't mind which way to go here. I'll leave the decision for the maintainers. :-)

Ping?

Either is fine. I will apply this patch as-is.

Wei.

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