Skip to contentSkip to conversation

integrity: Use static_assert() to check struct sizes

PATCH In review Gustavo A. R. Silva 3 participants 3 messages Aug 8, 2024 – Oct 10, 2024

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 integrity: Use static_assert() to check struct sizes

Commit 38aa3f5ac6d2 ("integrity: Avoid -Wflex-array-member-not-at-end warnings") introduced tagged struct evm_ima_xattr_data_hdr and struct ima_digest_data_hdr. We want to ensure that when new members need to be added to the flexible structures, they are always included within these tagged structs.

So, we use static_assert() to ensure that the memory layout for both the flexible structure and the tagged struct is the same after any changes.

---
 security/integrity/integrity.h | 4 ++++
 1 file changed, 4 insertions(+)
security/integrity/integrity.h +4 -0
index 660f76cb69d3..c2c2da691123 100644
@@ -37,6 +37,8 @@ struct evm_ima_xattr_data {
 	);
 	u8 data[];
 } __packed;
+static_assert(offsetof(struct evm_ima_xattr_data, data) == sizeof(struct evm_ima_xattr_data_hdr),
+	      "struct member likely outside of __struct_group()");
 
 /* Only used in the EVM HMAC code. */
 struct evm_xattr {
@@ -65,6 +67,8 @@ struct ima_digest_data {
 	);
 	u8 digest[];
 } __packed;
+static_assert(offsetof(struct ima_digest_data, digest) == sizeof(struct ima_digest_data_hdr),
+	      "struct member likely outside of __struct_group()");
 
 /*
  * Instead of wrapping the ima_digest_data struct inside a local structure
Signature · 1 line
2.34.1
Tested-by Reviewed-by on v1 1/1
#1 lore raw
Quoted 9 lines · Gustavo A. R. Silva

Commit 38aa3f5ac6d2 ("integrity: Avoid -Wflex-array-member-not-at-end warnings") introduced tagged struct evm_ima_xattr_data_hdr and struct ima_digest_data_hdr. We want to ensure that when new members need to be added to the flexible structures, they are always included within these tagged structs.

So, we use static_assert() to ensure that the memory layout for both the flexible structure and the tagged struct is the same after any changes.

Looks good to me.

Thanks

Roberto

Quoted 27 lines
---
 security/integrity/integrity.h | 4 ++++
 1 file changed, 4 insertions(+)
security/integrity/integrity.h +4 -0
index 660f76cb69d3..c2c2da691123 100644
@@ -37,6 +37,8 @@ struct evm_ima_xattr_data {
 	);
 	u8 data[];
 } __packed;
+static_assert(offsetof(struct evm_ima_xattr_data, data) == sizeof(struct evm_ima_xattr_data_hdr),
+	      "struct member likely outside of __struct_group()");
 
 /* Only used in the EVM HMAC code. */
 struct evm_xattr {
@@ -65,6 +67,8 @@ struct ima_digest_data {
 	);
 	u8 digest[];
 } __packed;
+static_assert(offsetof(struct ima_digest_data, digest) == sizeof(struct ima_digest_data_hdr),
+	      "struct member likely outside of __struct_group()");
 
 /*
  * Instead of wrapping the ima_digest_data struct inside a local structure
maintainer
#2 lore raw
In reply to Gustavo A. R. Silva
Quoted 11 lines · Gustavo A. R. Silva

Commit 38aa3f5ac6d2 ("integrity: Avoid -Wflex-array-member-not-at-end warnings") introduced tagged struct evm_ima_xattr_data_hdr and struct ima_digest_data_hdr. We want to ensure that when new members need to be added to the flexible structures, they are always included within these tagged structs.

So, we use static_assert() to ensure that the memory layout for both the flexible structure and the tagged struct is the same after any changes.

Sorry for the delay. It's now queued in next-integrity.

thanks,

Mimi