Skip to contentSkip to conversation

KVM: x86: Replace 0-length arrays with flexible arrays

PATCH In review Kees Cook 2 participants 4 messages Jan 5, 2023 – Jan 12, 2023

Subsystems (3)
lore
Share & tools
lore

Kees Cook

author
#0 lore raw

PATCH 1/1 KVM: x86: Replace 0-length arrays with flexible arrays

Zero-length arrays are deprecated[1]. Replace struct kvm_nested_state's "data" union 0-length arrays with flexible arrays. (How are the sizes of these arrays verified?) Detected with GCC 13, using -fstrict-flex-arrays=3:

arch/x86/kvm/svm/nested.c: In function 'svm_get_nested_state':
arch/x86/kvm/svm/nested.c:1536:17: error: array subscript 0 is outside array bounds of 'struct kvm_svm_nested_state_data[0]' [-Werror=array-bounds=]

 1536 |                 &user_kvm_nested_state->data.svm[0];
      |                 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
In file included from include/uapi/linux/kvm.h:15,
                 from include/linux/kvm_host.h:40,
                 from arch/x86/kvm/svm/nested.c:18:

arch/x86/include/uapi/asm/kvm.h:511:50: note: while referencing 'svm'
511 | struct kvm_svm_nested_state_data svm[0];
| ^~~

[1] https://www.kernel.org/doc/html/latest/process/deprecated.html#zero-length-and-one-element-arrays

Cc Sean Christopherson <[email protected]>
Cc Paolo Bonzini <[email protected]>
Cc Thomas Gleixner <[email protected]>
Cc Ingo Molnar <[email protected]>
Cc Borislav Petkov <[email protected]>
Cc Dave Hansen <[email protected]>
Cc "Gustavo A. R. Silva" <[email protected]>
Cc "H. Peter Anvin" <[email protected]>
---
 arch/x86/include/uapi/asm/kvm.h | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
arch/x86/include/uapi/asm/kvm.h +3 -3
index e48deab8901d..8ec3dfd641b0 100644
@@ -502,13 +502,13 @@ struct kvm_nested_state {
 	} hdr;
 
 	/*
-	 * Define data region as 0 bytes to preserve backwards-compatability
+	 * Define union of flexible arrays to preserve backwards-compatability
 	 * to old definition of kvm_nested_state in order to avoid changing
 	 * KVM_{GET,PUT}_NESTED_STATE ioctl values.
 	 */
 	union {
-		struct kvm_vmx_nested_state_data vmx[0];
-		struct kvm_svm_nested_state_data svm[0];
+		__DECLARE_FLEX_ARRAY(struct kvm_vmx_nested_state_data, vmx);
+		__DECLARE_FLEX_ARRAY(struct kvm_svm_nested_state_data, svm);
 	} data;
 };
 
Signature · 1 line
2.34.1
maintainer Reviewed-by on v1 1/1
#1 lore raw
Quoted 3 lines · Kees Cook

Zero-length arrays are deprecated[1]. Replace struct kvm_nested_state's "data" union 0-length arrays with flexible arrays. (How are the sizes of these arrays verified?)

It's not really interpreted as an array, it's a mandatory single-entry "array".

	if (copy_from_user(vmcs12, user_vmx_nested_state->vmcs12, sizeof(*vmcs12)))
		return -EFAULT;
Quoted 26 lines

Detected with GCC 13, using -fstrict-flex-arrays=3:

arch/x86/kvm/svm/nested.c: In function 'svm_get_nested_state':
arch/x86/kvm/svm/nested.c:1536:17: error: array subscript 0 is outside array bounds of 'struct kvm_svm_nested_state_data[0]' [-Werror=array-bounds=]

 1536 |                 &user_kvm_nested_state->data.svm[0];
      |                 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
In file included from include/uapi/linux/kvm.h:15,
                 from include/linux/kvm_host.h:40,
                 from arch/x86/kvm/svm/nested.c:18:

arch/x86/include/uapi/asm/kvm.h:511:50: note: while referencing 'svm'
511 | struct kvm_svm_nested_state_data svm[0];
| ^~~

[1] https://www.kernel.org/doc/html/latest/process/deprecated.html#zero-length-and-one-element-arrays

Cc Sean Christopherson <[email protected]>
Cc Paolo Bonzini <[email protected]>
Cc Thomas Gleixner <[email protected]>
Cc Ingo Molnar <[email protected]>
Cc Borislav Petkov <[email protected]>
Cc Dave Hansen <[email protected]>
Cc "Gustavo A. R. Silva" <[email protected]>
Cc "H. Peter Anvin" <[email protected]>
Quoted 1 line

---

Nit on the comment aside,

arch/x86/include/uapi/asm/kvm.h struct kvm_nested_state { · line 505

Quoted 13 lines
 arch/x86/include/uapi/asm/kvm.h | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
arch/x86/include/uapi/asm/kvm.h +1 -1
index e48deab8901d..8ec3dfd641b0 100644
@@ -502,13 +502,13 @@ struct kvm_nested_state {
 	} hdr;
 
 	/*
-	 * Define data region as 0 bytes to preserve backwards-compatability
+	 * Define union of flexible arrays to preserve backwards-compatability

I think I'd actually prefer the "as 0 bytes" comment. The important part is that the size of "data" be zero, how that happens is immaterial.

Quoted 14 lines
arch/x86/include/uapi/asm/kvm.h +2 -2
 	 * to old definition of kvm_nested_state in order to avoid changing
 	 * KVM_{GET,PUT}_NESTED_STATE ioctl values.
 	 */
 	union {
-		struct kvm_vmx_nested_state_data vmx[0];
-		struct kvm_svm_nested_state_data svm[0];
+		__DECLARE_FLEX_ARRAY(struct kvm_vmx_nested_state_data, vmx);
+		__DECLARE_FLEX_ARRAY(struct kvm_svm_nested_state_data, svm);
 	} data;
 };
 
Signature · 1 line
2.34.1
author
#2 lore raw
Quoted 9 lines · Sean Christopherson
Quoted 3 lines · Kees Cook

Zero-length arrays are deprecated[1]. Replace struct kvm_nested_state's "data" union 0-length arrays with flexible arrays. (How are the sizes of these arrays verified?)

It's not really interpreted as an array, it's a mandatory single-entry "array".

	if (copy_from_user(vmcs12, user_vmx_nested_state->vmcs12, sizeof(*vmcs12)))
		return -EFAULT;

If it's mandatory, why is it [0] instead of just a single struct? i.e. why is it not:

	union {
		struct kvm_vmx_nested_state_data vmx;
		struct kvm_svm_nested_state_data svm;
	};
Quoted 43 lines
Quoted 27 lines

Detected with GCC 13, using -fstrict-flex-arrays=3:

arch/x86/kvm/svm/nested.c: In function 'svm_get_nested_state':
arch/x86/kvm/svm/nested.c:1536:17: error: array subscript 0 is outside array bounds of 'struct kvm_svm_nested_state_data[0]' [-Werror=array-bounds=]

 1536 |                 &user_kvm_nested_state->data.svm[0];
      |                 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
In file included from include/uapi/linux/kvm.h:15,
                 from include/linux/kvm_host.h:40,
                 from arch/x86/kvm/svm/nested.c:18:

arch/x86/include/uapi/asm/kvm.h:511:50: note: while referencing 'svm'
511 | struct kvm_svm_nested_state_data svm[0];
| ^~~

[1] https://www.kernel.org/doc/html/latest/process/deprecated.html#zero-length-and-one-element-arrays

Cc Sean Christopherson <[email protected]>
Cc Paolo Bonzini <[email protected]>
Cc Thomas Gleixner <[email protected]>
Cc Ingo Molnar <[email protected]>
Cc Borislav Petkov <[email protected]>
Cc Dave Hansen <[email protected]>
Cc "Gustavo A. R. Silva" <[email protected]>
Cc "H. Peter Anvin" <[email protected]>
Quoted 1 line

---

Nit on the comment aside,

Quoted 10 lines
 arch/x86/include/uapi/asm/kvm.h | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
arch/x86/include/uapi/asm/kvm.h +0 -0
index e48deab8901d..8ec3dfd641b0 100644
@@ -502,13 +502,13 @@ struct kvm_nested_state {
 	} hdr;
 
Quoted 6 lines

arch/x86/include/uapi/asm/kvm.h struct kvm_nested_state { · line 505

Quoted 3 lines
arch/x86/include/uapi/asm/kvm.h +1 -1
 	/*
-	 * Define data region as 0 bytes to preserve backwards-compatability
+	 * Define union of flexible arrays to preserve backwards-compatability

I think I'd actually prefer the "as 0 bytes" comment. The important part is that the size of "data" be zero, how that happens is immaterial.

Okay, I'll drop this part.

Quoted 15 lines
Quoted 15 lines
arch/x86/include/uapi/asm/kvm.h +2 -2
 	 * to old definition of kvm_nested_state in order to avoid changing
 	 * KVM_{GET,PUT}_NESTED_STATE ioctl values.
 	 */
 	union {
-		struct kvm_vmx_nested_state_data vmx[0];
-		struct kvm_svm_nested_state_data svm[0];
+		__DECLARE_FLEX_ARRAY(struct kvm_vmx_nested_state_data, vmx);
+		__DECLARE_FLEX_ARRAY(struct kvm_svm_nested_state_data, svm);
 	} data;
 };
 
Signature · 1 line
2.34.1
Signature · 1 line
Kees Cook
author
#3 lore raw
In reply to Sean Christopherson
Quoted 6 lines · Sean Christopherson
Quoted 3 lines · Kees Cook

Zero-length arrays are deprecated[1]. Replace struct kvm_nested_state's "data" union 0-length arrays with flexible arrays. (How are the sizes of these arrays verified?)

It's not really interpreted as an array, it's a mandatory single-entry "array".

[...]

Quoted 7 lines

arch/x86/include/uapi/asm/kvm.h struct kvm_nested_state { · line 505

Quoted 4 lines
arch/x86/include/uapi/asm/kvm.h +1 -1
 	/*
-	 * Define data region as 0 bytes to preserve backwards-compatability
+	 * Define union of flexible arrays to preserve backwards-compatability

I think I'd actually prefer the "as 0 bytes" comment. The important part is that the size of "data" be zero, how that happens is immaterial.

Oh, dur, I can read the comment. :)

It has to stay the old size -- this was a way to add an optional extra struct to the end. Got it!

Signature · 1 line
Kees Cook