[PATCH] uapi: add bit-field padding for field alignment

James Tucker jftucker at gmail.com
Tue Jan 10 04:47:29 CET 2017


The C language does not specify the layout behavior of bit-fields. As a
consequence, compilers are free to do interesting things with these
fields, including reorder, split, pick size, etc. In this case, it was
observed that on x64, gcc and clang were using only two bytes between
tx_bytes and num_ipmasks in wgpeer, causing interoperability issues.

While non-native clients (i.e. wireguard-go) could be fixed, the problem
will keep on giving, as the layouts can be changed by compiler flags and
architectures, which will make portability for all non-C implementations
extremely difficult. Compilers also do not document their behavior well
in this regard.

Signed-off-by: James Tucker <jftucker at gmail.com>
---
 src/uapi.h | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/src/uapi.h b/src/uapi.h
index cd4b86b..bf38f48 100644
--- a/src/uapi.h
+++ b/src/uapi.h
@@ -107,6 +107,7 @@ struct wgpeer {
 
 	__u32 remove_me : 1; /* Set */
 	__u32 replace_ipmasks : 1; /* Set */
+	__u32 : 32-2; /* pad to force portable field size */
 
 	__u16 num_ipmasks; /* Get/Set */
 	__u16 persistent_keepalive_interval; /* Get/Set -- 0 = off, 0xffff = unset */
@@ -124,6 +125,7 @@ struct wgdevice {
 	__u32 replace_peer_list : 1; /* Set */
 	__u32 remove_private_key : 1; /* Set */
 	__u32 remove_preshared_key : 1; /* Set */
+	__u32 : 32-3; /* pad to force portable field size */
 
 	union {
 		__u16 num_peers; /* Get/Set */
-- 
2.11.0



More information about the WireGuard mailing list