Skip to contentSkip to conversation

vla: add VLA macro and testing

RFC In review Tobin C. Harding 5 participants 9 messages Nov 1, 2017 – Nov 13, 2017

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

RFC 1/1 vla: add VLA macro and testing

Variable Length Arrays (VLA) pose a risk to the stack if the variable passed into the array declaration is too large. If the variable used can be controlled by a malicious party then this poses a security risk to the kernel.

Add a macro for declaring VLA's. Macro includes a requested size and a maximum size, if requested size is larger than maximum size then requested size is capped at maximum. Requested size is passed by reference and updated by macro so caller has access to size of array after declaration.

---

I was unable to get the test module to integrate with the kernel build system correctly. The attempt was to mirror the way lib/test_printf.c functions. This effort was unsuccessful, it is included in the patch in the hope of getting better suggestions. To test, the test module was built out of tree and all tests pass.

The macro needs some work. It functions as intended but

Checkpatch emits ERROR: Macros with multiple statements should be enclosed in a do - while loop.

Also for each use of VLA() checkpatch emits WARNING: Missing a blank line after declarations.

Also I was unsure where to put the macro definition, appreciate any suggestions.

thanks,
Tobin.

 include/linux/vla.h                | 24 ++++++++++
 lib/Kconfig.debug                  |  3 ++
 lib/Makefile                       |  1 +
 lib/test_vla.c                     | 98 ++++++++++++++++++++++++++++++++++++++
 tools/testing/selftests/lib/config |  1 +
 5 files changed, 127 insertions(+)
 create mode 100644 include/linux/vla.h
 create mode 100644 lib/test_vla.c
include/linux/vla.h new file +24 -0
new file mode 100644
index 000000000000..e8f1572bbf42
@@ -0,0 +1,24 @@
+/*
+ * This software is licensed under the terms of the GNU General Public
+ * License version 2, as published by the Free Software Foundation, and
+ * may be copied, distributed, and modified under those terms.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ */
+#ifndef __LINUX_VLA_H
+#define __LINUX_VLA_H
+
+/*
+ * When declaring a local variable using VLA(), VLA() must be the last
+ * declaration otherwise a compiler warning [-Wdeclaration-after-statement] will
+ * be generated
+ */
+#define VLA(type, name, size, max) \
+	type name[(*size < max ? *size : max)]; \
+	*size = (*size < max ? *size : max)
+
+#endif	/* __LINUX_VLA_H */
lib/Kconfig.debug +3 -0
index dfdad67d8f6c..3b8eb2789235 100644
@@ -1917,6 +1917,9 @@ config TEST_DEBUG_VIRTUAL
 
 	  If unsure, say N.
 
+config TEST_VLA
+	tristate "Test VLA macro at runtime"
+
 endmenu # runtime tests
 
 config MEMTEST
lib/Makefile +1 -0
index dafa79613fb4..b1e8f7c54c5b 100644
@@ -63,6 +63,7 @@ obj-$(CONFIG_TEST_UUID) += test_uuid.o
 obj-$(CONFIG_TEST_PARMAN) += test_parman.o
 obj-$(CONFIG_TEST_KMOD) += test_kmod.o
 obj-$(CONFIG_TEST_DEBUG_VIRTUAL) += test_debug_virtual.o
+obj-$(CONFIG_TEST_VLA) += test_vla.o
 
 ifeq ($(CONFIG_DEBUG_KOBJECT),y)
 CFLAGS_kobject.o += -DDEBUG
lib/test_vla.c new file +98 -0

98 lines not on this page · raw message

tools/testing/selftests/lib/config +1 -0
index 126933bcc950..0776156f881d 100644
@@ -1,3 +1,4 @@
 CONFIG_TEST_PRINTF=m
 CONFIG_TEST_BITMAP=m
 CONFIG_PRIME_NUMBERS=m
+CONFIG_TEST_VLA=m
Signature · 1 line
2.7.4
#1 lore raw
Quoted 61 lines · Tobin C. Harding

Variable Length Arrays (VLA) pose a risk to the stack if the variable passed into the array declaration is too large. If the variable used can be controlled by a malicious party then this poses a security risk to the kernel.

Add a macro for declaring VLA's. Macro includes a requested size and a maximum size, if requested size is larger than maximum size then requested size is capped at maximum. Requested size is passed by reference and updated by macro so caller has access to size of array after declaration.

---

I was unable to get the test module to integrate with the kernel build system correctly. The attempt was to mirror the way lib/test_printf.c functions. This effort was unsuccessful, it is included in the patch in the hope of getting better suggestions. To test, the test module was built out of tree and all tests pass.

The macro needs some work. It functions as intended but

Checkpatch emits ERROR: Macros with multiple statements should be enclosed in a do - while loop.

Also for each use of VLA() checkpatch emits WARNING: Missing a blank line after declarations.

Also I was unsure where to put the macro definition, appreciate any suggestions.

thanks,
Tobin.

 include/linux/vla.h                | 24 ++++++++++
 lib/Kconfig.debug                  |  3 ++
 lib/Makefile                       |  1 +
 lib/test_vla.c                     | 98 ++++++++++++++++++++++++++++++++++++++
 tools/testing/selftests/lib/config |  1 +
 5 files changed, 127 insertions(+)
 create mode 100644 include/linux/vla.h
 create mode 100644 lib/test_vla.c
include/linux/vla.h new file +12 -0
new file mode 100644
index 000000000000..e8f1572bbf42
@@ -0,0 +1,24 @@
+/*
+ * This software is licensed under the terms of the GNU General Public
+ * License version 2, as published by the Free Software Foundation, and
+ * may be copied, distributed, and modified under those terms.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ */
+#ifndef __LINUX_VLA_H

include/linux/vla.h line 22

Quoted 10 lines
include/linux/vla.h new file +10 -0
+#define __LINUX_VLA_H
+
+/*
+ * When declaring a local variable using VLA(), VLA() must be the last
+ * declaration otherwise a compiler warning [-Wdeclaration-after-statement] will
+ * be generated
+ */
+#define VLA(type, name, size, max) \
+	type name[(*size < max ? *size : max)]; \
+	*size = (*size < max ? *size : max)

This requires you to have your VLAs at the end of the declarations, which is annoying and potentially limiting (limited to one VLA, etc.). I also don't think you need the pointer. Something like:

#define VLA(type, name, size, max) \
type name[(size < max ? max : (size = max))]

should work I think.

It would also be nice to see some users of the VLA macro as part of this series.

Cheers,

Tycho

#2 lore raw
Quoted 77 lines · Tycho Andersen
Quoted 61 lines · Tobin C. Harding

Variable Length Arrays (VLA) pose a risk to the stack if the variable passed into the array declaration is too large. If the variable used can be controlled by a malicious party then this poses a security risk to the kernel.

Add a macro for declaring VLA's. Macro includes a requested size and a maximum size, if requested size is larger than maximum size then requested size is capped at maximum. Requested size is passed by reference and updated by macro so caller has access to size of array after declaration.

---

I was unable to get the test module to integrate with the kernel build system correctly. The attempt was to mirror the way lib/test_printf.c functions. This effort was unsuccessful, it is included in the patch in the hope of getting better suggestions. To test, the test module was built out of tree and all tests pass.

The macro needs some work. It functions as intended but

Checkpatch emits ERROR: Macros with multiple statements should be enclosed in a do - while loop.

Also for each use of VLA() checkpatch emits WARNING: Missing a blank line after declarations.

Also I was unsure where to put the macro definition, appreciate any suggestions.

thanks,
Tobin.

 include/linux/vla.h                | 24 ++++++++++
 lib/Kconfig.debug                  |  3 ++
 lib/Makefile                       |  1 +
 lib/test_vla.c                     | 98 ++++++++++++++++++++++++++++++++++++++
 tools/testing/selftests/lib/config |  1 +
 5 files changed, 127 insertions(+)
 create mode 100644 include/linux/vla.h
 create mode 100644 lib/test_vla.c
include/linux/vla.h new file +12 -0
new file mode 100644
index 000000000000..e8f1572bbf42
@@ -0,0 +1,24 @@
+/*
+ * This software is licensed under the terms of the GNU General Public
+ * License version 2, as published by the Free Software Foundation, and
+ * may be copied, distributed, and modified under those terms.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ */
+#ifndef __LINUX_VLA_H

include/linux/vla.h line 22

Quoted 10 lines
include/linux/vla.h new file +10 -0
+#define __LINUX_VLA_H
+
+/*
+ * When declaring a local variable using VLA(), VLA() must be the last
+ * declaration otherwise a compiler warning [-Wdeclaration-after-statement] will
+ * be generated
+ */
+#define VLA(type, name, size, max) \
+	type name[(*size < max ? *size : max)]; \
+	*size = (*size < max ? *size : max)

This requires you to have your VLAs at the end of the declarations, which is annoying and potentially limiting (limited to one VLA, etc.). I also don't think you need the pointer. Something like:

Quoted 4 lines

#define VLA(type, name, size, max) \
type name[(size < max ? max : (size = max))]

should work I think.

type name[(size < max ? size : (size = max))]

Oh, cool. Thanks Tycho. I'll test out your suggestion. At first glance I think that will remove both the checkpatch issues and the declaration annoyance. Nice one!

Quoted 2 lines

It would also be nice to see some users of the VLA macro as part of this series.

Righto, I wondered if that was the done thing. Will add one for the next spin.

thanks,
Tobin.

#3 lore raw
In reply to Tobin C. Harding
Quoted 23 lines · Tobin C. Harding

Variable Length Arrays (VLA) pose a risk to the stack if the variable passed into the array declaration is too large. If the variable used can be controlled by a malicious party then this poses a security risk to the kernel.

Add a macro for declaring VLA's. Macro includes a requested size and a maximum size, if requested size is larger than maximum size then requested size is capped at maximum. Requested size is passed by reference and updated by macro so caller has access to size of array after declaration.

---

I was unable to get the test module to integrate with the kernel build system correctly. The attempt was to mirror the way lib/test_printf.c functions. This effort was unsuccessful, it is included in the patch in the hope of getting better suggestions. To test, the test module was built out of tree and all tests pass.

The macro needs some work. It functions as intended but

Quoted 7 lines

Checkpatch emits ERROR: Macros with multiple statements should be enclosed in a do - while loop.

Also for each use of VLA() checkpatch emits WARNING: Missing a blank line after declarations.

Also I was unsure where to put the macro definition, appreciate any suggestions.

While I'd certainly like to see SOME kind of sanity checking for VLAs, my preference would be to entirely eliminate them at get -Werror=vla added to the Makefile. Even Linus has expressed irritation over the idea of VLAs in structs in the past:

https://lkml.org/lkml/2013/9/23/500

I imagine VLAs make Josh's life harder too. Would objtool have an easier time if there were no VLAs?

Could we just fix the existing VLAs instead? How many are there really?

-Kees

Signature · 2 lines
Kees Cook
Pixel Security
#4 lore raw
Quoted 6 lines · Kees Cook

While I'd certainly like to see SOME kind of sanity checking for VLAs, my preference would be to entirely eliminate them at get -Werror=vla added to the Makefile. Even Linus has expressed irritation over the idea of VLAs in structs in the past:

https://lkml.org/lkml/2013/9/23/500

VLAIS makes it difficult (impossible) to compile allyesconfig with clang. VLA on the stack is fine, but VLAIS is not.

#5 lore raw
Quoted 10 lines · Nick Desaulniers
Quoted 6 lines · Kees Cook

While I'd certainly like to see SOME kind of sanity checking for VLAs, my preference would be to entirely eliminate them at get -Werror=vla added to the Makefile. Even Linus has expressed irritation over the idea of VLAs in structs in the past:

https://lkml.org/lkml/2013/9/23/500

VLAIS makes it difficult (impossible) to compile allyesconfig with clang. VLA on the stack is fine, but VLAIS is not.

What is VLAIS please? I don't get any results when grepping the kernel tree.

$ git grep VLAIS

thanks,
Tobin.

#6 lore raw

Variable Length Array In Struct, a GNU C extension.

Think of a variable length array, as a member of a struct, but not as the final member.

Quoted 19 lines · Tobin C. Harding
Quoted 10 lines · Nick Desaulniers
Quoted 6 lines · Kees Cook

While I'd certainly like to see SOME kind of sanity checking for VLAs, my preference would be to entirely eliminate them at get -Werror=vla added to the Makefile. Even Linus has expressed irritation over the idea of VLAs in structs in the past:

https://lkml.org/lkml/2013/9/23/500

VLAIS makes it difficult (impossible) to compile allyesconfig with clang. VLA on the stack is fine, but VLAIS is not.

What is VLAIS please? I don't get any results when grepping the kernel tree.

$ git grep VLAIS

thanks,
Tobin.

Signature · 2 lines
Thanks,
~Nick Desaulniers
#7 lore raw
Quoted 4 lines · Nick Desaulniers

Variable Length Array In Struct, a GNU C extension.

Think of a variable length array, as a member of a struct, but not as the final member.

thanks Nick

#8 lore raw
In reply to Kees Cook
Quoted 37 lines · Kees Cook
Quoted 23 lines · Tobin C. Harding

Variable Length Arrays (VLA) pose a risk to the stack if the variable passed into the array declaration is too large. If the variable used can be controlled by a malicious party then this poses a security risk to the kernel.

Add a macro for declaring VLA's. Macro includes a requested size and a maximum size, if requested size is larger than maximum size then requested size is capped at maximum. Requested size is passed by reference and updated by macro so caller has access to size of array after declaration.

---

I was unable to get the test module to integrate with the kernel build system correctly. The attempt was to mirror the way lib/test_printf.c functions. This effort was unsuccessful, it is included in the patch in the hope of getting better suggestions. To test, the test module was built out of tree and all tests pass.

The macro needs some work. It functions as intended but

Quoted 7 lines

Checkpatch emits ERROR: Macros with multiple statements should be enclosed in a do - while loop.

Also for each use of VLA() checkpatch emits WARNING: Missing a blank line after declarations.

Also I was unsure where to put the macro definition, appreciate any suggestions.

While I'd certainly like to see SOME kind of sanity checking for VLAs, my preference would be to entirely eliminate them at get -Werror=vla added to the Makefile. Even Linus has expressed irritation over the idea of VLAs in structs in the past:

Quoted 4 lines

https://lkml.org/lkml/2013/9/23/500

I imagine VLAs make Josh's life harder too. Would objtool have an easier time if there were no VLAs?

As it turns out, it doesn't make objtool's life that much harder. GCC sets up a frame pointer before doing the stack allocation anyway.

But, I strongly agree that VLAs should just go away.

Signature · 1 line
Josh