Skip to contentSkip to conversation

add macro VLA_SAFE

RFC In review Tobin C. Harding 4 participants 7 messages Mar 7, 2018 Staging

lore
Share & tools
lore
#0 lore raw

cover add macro VLA_SAFE

We would like to get rid of VLAs all together from the kernel but in the mean time we can make their usage safer.

Patch 1 defines a macro for declaring a VLA.
Patch 2 shows example usage.

There are a number of issues with this set

1. It is opt-in security - that's bad.
2. The compiler still warns for VLA use.

thanks,
Tobin.

Tested with a test module - I'm not really sure if this sort of test module should go into the kernel (in general I mean). Posting here for comment.

/*
* Tests for VLA macro.
*/

#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt

#include <linux/init.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/printk.h>

/* newly added header file */
/* #include <linux/vla.h> */
#include "vla.h"

#define MAX_VLA_SIZE 32

static unsigned total_tests __initdata;
static unsigned failed_tests __initdata;

/* verify there are no compiler warnings */
static void __init verify_declaration(void)

{
	int size = 10;
	VLA_SAFE(int, myvla, size, VLA_DEFAULT_MAX);
	int dummy;

	VLA_WARN_OVERSIZE(myvla, size);

	/* quiet compiler */
	dummy = 1;
	myvla[0] = 'a';

}

static int __init test_vla_less_than_max(void)

{
	int want = MAX_VLA_SIZE / 2;
	VLA_SAFE(char, myvla, want, MAX_VLA_SIZE);
	int got = sizeof(myvla);

	total_tests++;

	if (want != got) {
		pr_warn("VLA_SAFE() declaration error: want=%d got=%d max=%d\n",
			want, got, MAX_VLA_SIZE);
		return 1;
	}

	/* quiet compiler warning */
	myvla[0] = 'a';

	return 0;
}

static int __init test_vla_greater_than_max(void)

{
	int want = MAX_VLA_SIZE * 2;
	VLA_SAFE(char, myvla, want, MAX_VLA_SIZE);
	int got = sizeof(myvla);

	total_tests++;

	if (got != MAX_VLA_SIZE) {
		pr_warn("VLA_SAFE() declaration error: want=%d got=%d max=%d\n",
			want, got, MAX_VLA_SIZE);
		return 1;
	}

	/* quiet compiler warning */
	myvla[0] = 'a';

	return 0;
}

static void __init test_vla_int_type_usage(void)

{
	int size = 100;
	VLA_SAFE(int, myvla, size, MAX_VLA_SIZE);
	int i;

	total_tests++;

	for (i = 0; i < size; i++)
		myvla[i] = 0;
}

static int __init test_vla_init(void)
{

	int err;

	verify_declaration();

	err = test_vla_less_than_max();
	if (err)
		failed_tests++;

	err = test_vla_greater_than_max();
	if (err)
		failed_tests++;

	test_vla_int_type_usage();

	if (failed_tests == 0)
		pr_info("all %u tests passed\n", total_tests);
	else
		pr_warn("failed %u out of %u tests\n",
			failed_tests, total_tests);

	return failed_tests ? -EINVAL : 0;
}

module_init(test_vla_init);

static void hello_exit(void)

{
	pr_alert("Removing module: vla\n");
}

module_exit(hello_exit);

MODULE_AUTHOR("Tobin C. Harding <[email protected]>");
MODULE_LICENSE("GPL");

Tobin C. Harding (2):
vla: define new safe vla macros
lustre: use VLA_SAFE

 drivers/staging/lustre/lustre/llite/xattr.c |  4 +++-
 include/linux/vla.h                         | 15 +++++++++++++++
 2 files changed, 18 insertions(+), 1 deletion(-)
 create mode 100644 include/linux/vla.h
Signature · 1 line
2.7.4
#1 lore raw

PATCH 1/2 vla: define new safe vla macros

We would like to get rid of VLAs all together but in the mean time we can make their usage safer.

Define a macro to use for declaring a VLA. Macro includes requested 'size' and 'max' allowed. Macro allocates storage for an array equivalent to

	int array[min(size, max)];

We also define a macro to check whether maximum size was reached.

---
 include/linux/vla.h | 15 +++++++++++++++
 1 file changed, 15 insertions(+)
 create mode 100644 include/linux/vla.h
include/linux/vla.h new file +15 -0
new file mode 100644
index 000000000000..ca0510d5e416
@@ -0,0 +1,15 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+#ifndef _LINUX_VLA_H
+#define _LINUX_VLA_H
+
+#define VLA_DEFAULT_MAX 256
+
+/* evaluates 'size' and 'max' twice */
+#define VLA_SAFE(type, vla, size, max) type vla[(size) < (max) ? (size) : (max)]
+
+#define VLA_WARN_OVERSIZE(vla, size)				\
+do {								\
+	WARN(sizeof(vla) < size, "vla maximum exceeded\n");	\
+} while (0)
+
+#endif	/* _LINUX_VLA_H */
Signature · 1 line
2.7.4
#2 lore raw

RFC 2/2 lustre: use VLA_SAFE

In reply to Tobin C. Harding

Currently lustre uses a VLA to store a string on the stack. We can use the newly define VLA_SAFE macro to make this declaration safer.

Use VLA_SAFE to declare VLA.

---
 drivers/staging/lustre/lustre/llite/xattr.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
drivers/staging/lustre/lustre/llite/xattr.c +3 -1
index 532384c91447..6f4099cd4afa 100644
@@ -36,6 +36,7 @@
 #include <linux/mm.h>
 #include <linux/xattr.h>
 #include <linux/selinux.h>
+#include <linux/vla.h>
 
 #define DEBUG_SUBSYSTEM S_LLITE
 
@@ -87,12 +88,13 @@ ll_xattr_set_common(const struct xattr_handler *handler,
 		    const char *name, const void *value, size_t size,
 		    int flags)
 {
-	char fullname[strlen(handler->prefix) + strlen(name) + 1];
 	struct ll_sb_info *sbi = ll_i2sbi(inode);
 	struct ptlrpc_request *req = NULL;
 	const char *pv = value;
 	__u64 valid;
 	int rc;
+	int size = strlen(handler->prefix) + strlen(name) + 1;
+	VLA_SAFE(char, fullname, size, VLA_DEFAULT_MAX);
 
 	if (flags == XATTR_REPLACE) {
 		ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_REMOVEXATTR, 1);
Signature · 1 line
2.7.4
#3 lore raw

Re: [RFC 2/2] lustre: use VLA_SAFE

Quoted 4 lines · Tobin C. Harding

Currently lustre uses a VLA to store a string on the stack. We can use the newly define VLA_SAFE macro to make this declaration safer.

Use VLA_SAFE to declare VLA.

VLA_SAFE implements a max, which is nice, but I think we're just digging ourselves into a bigger hole with this, since now all the maxes must be validated (which isn't done here, what happens if VLA_DEFAULT_MAX is smaller than the strlen() math? We'll overflow the stack buffer in the later sprintf).

Quoted 23 lines
---
 drivers/staging/lustre/lustre/llite/xattr.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
drivers/staging/lustre/lustre/llite/xattr.c +1 -1
index 532384c91447..6f4099cd4afa 100644
@@ -36,6 +36,7 @@
 #include <linux/mm.h>
 #include <linux/xattr.h>
 #include <linux/selinux.h>
+#include <linux/vla.h>
 
 #define DEBUG_SUBSYSTEM S_LLITE
 
@@ -87,12 +88,13 @@ ll_xattr_set_common(const struct xattr_handler *handler,
                    const char *name, const void *value, size_t size,
                    int flags)
 {
-       char fullname[strlen(handler->prefix) + strlen(name) + 1];

drivers/staging/lustre/lustre/llite/xattr.c ll_xattr_set_common(const struct xattr_handler *handler, · line 100

Quoted 10 lines
drivers/staging/lustre/lustre/llite/xattr.c +2 -0
        struct ll_sb_info *sbi = ll_i2sbi(inode);
        struct ptlrpc_request *req = NULL;
        const char *pv = value;
        __u64 valid;
        int rc;
+       int size = strlen(handler->prefix) + strlen(name) + 1;
+       VLA_SAFE(char, fullname, size, VLA_DEFAULT_MAX);
 
        if (flags == XATTR_REPLACE) {
                ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_REMOVEXATTR, 1);

In the lustre case, I think it's better to just remove the stack allocation entirely. (See separate patch...)

-Kees

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

Re: [RFC 2/2] lustre: use VLA_SAFE

Quoted 11 lines · Kees Cook
Quoted 4 lines · Tobin C. Harding

Currently lustre uses a VLA to store a string on the stack. We can use the newly define VLA_SAFE macro to make this declaration safer.

Use VLA_SAFE to declare VLA.

VLA_SAFE implements a max, which is nice, but I think we're just digging ourselves into a bigger hole with this, since now all the maxes must be validated (which isn't done here, what happens if VLA_DEFAULT_MAX is smaller than the strlen() math? We'll overflow the stack buffer in the later sprintf).

ok, lets drop this.

Memory on the stack is always going to be faster than memory from the slub allocator, right? Do you think using kasprintf() is going to be acceptable? Isn't it only going to be acceptable on non-time critical paths? I'm still trying to get my head around how we get rid of VLA when the stack is faster? Is this a speed vs safety trade off that must be tackled on a case by case basis?

thanks,
Tobin.

#5 lore raw

Re: [RFC 2/2] lustre: use VLA_SAFE

Quoted 13 lines · Tobin C. Harding
Quoted 11 lines · Kees Cook
Quoted 4 lines · Tobin C. Harding

Currently lustre uses a VLA to store a string on the stack. We can use the newly define VLA_SAFE macro to make this declaration safer.

Use VLA_SAFE to declare VLA.

VLA_SAFE implements a max, which is nice, but I think we're just digging ourselves into a bigger hole with this, since now all the maxes must be validated (which isn't done here, what happens if VLA_DEFAULT_MAX is smaller than the strlen() math? We'll overflow the stack buffer in the later sprintf).

Quoted 6 lines

ok, lets drop this.

Memory on the stack is always going to be faster than memory from the slub allocator, right? Do you think using kasprintf() is going to be acceptable? Isn't it only going to be acceptable on non-time critical paths?

Probably anything that does string formatting to begin with (using any *printf function) is not performance critical. That e.g. applies to the lustre case. It's true that kasprintf() ends up doing the vsnprintf() twice, but I haven't yet seen an example where that is really a problem. Should it actually come up, I've thought about a few things one might do to partly mitigate that:

(1) Use that the vast majority of kasprintf() results are short'ish strings, so instead of letting the first vsnprintf() write nothing at all, allocate a smallish (32/64) stack buffer and do the first vsnprintf() to that - on "success", just kmemdup() the result instead of doing the formatting again.

(2) Add a

char *kasprintf_buf(buf, len, gfp, fmt, ...)

public API that will use the user-provided (buf, len) as initial target, and return buf on success, otherwise fall back to kmalloc'ing an appropriate buffer and redoing the formatting. It's not totally unheard of to decide whether to kfree() based on comparison with a caller-owned pointer. Typically buf would be stack-allocated, and it's up to the caller to decide what a reasonable size would be - and especially useful in cases such as the lustre one where the string does not outlive the sprintf() caller.

(1) is easily implemented in terms of this.

(3) As (2), but with the rule being that buf must be kmalloc'ed, and kasprintf() will kfree() it and allocate a new buffer if necessary. Probably too hard to use (and exact semantics on failure would need to be decided; like realloc(), or is the passed-in too-small buffer already kfree'd?)

Rasmus

#6 lore raw

Re: [RFC 2/2] lustre: use VLA_SAFE

Quoted 17 lines · Rasmus Villemoes
Quoted 15 lines · Tobin C. Harding
Quoted 11 lines · Kees Cook
Quoted 4 lines · Tobin C. Harding

Currently lustre uses a VLA to store a string on the stack. We can use the newly define VLA_SAFE macro to make this declaration safer.

Use VLA_SAFE to declare VLA.

VLA_SAFE implements a max, which is nice, but I think we're just digging ourselves into a bigger hole with this, since now all the maxes must be validated (which isn't done here, what happens if VLA_DEFAULT_MAX is smaller than the strlen() math? We'll overflow the stack buffer in the later sprintf).

ok, lets drop this.

Quoted 7 lines
Quoted 4 lines

Memory on the stack is always going to be faster than memory from the slub allocator, right? Do you think using kasprintf() is going to be acceptable? Isn't it only going to be acceptable on non-time critical paths?

Probably anything that does string formatting to begin with (using any *printf function) is not performance critical.

Oh cool, good metric. thanks.

Quoted 27 lines

That e.g. applies to the
lustre case. It's true that kasprintf() ends up doing the vsnprintf() twice, but I haven't yet seen an example where that is really a problem. Should it actually come up, I've thought about a few things one might do to partly mitigate that:

(1) Use that the vast majority of kasprintf() results are short'ish strings, so instead of letting the first vsnprintf() write nothing at all, allocate a smallish (32/64) stack buffer and do the first vsnprintf() to that - on "success", just kmemdup() the result instead of doing the formatting again.

(2) Add a

char *kasprintf_buf(buf, len, gfp, fmt, ...)

public API that will use the user-provided (buf, len) as initial target, and return buf on success, otherwise fall back to kmalloc'ing an appropriate buffer and redoing the formatting. It's not totally unheard of to decide whether to kfree() based on comparison with a caller-owned pointer. Typically buf would be stack-allocated, and it's up to the caller to decide what a reasonable size would be - and especially useful in cases such as the lustre one where the string does not outlive the sprintf() caller.

(1) is easily implemented in terms of this.

Quoted 7 lines

(3) As (2), but with the rule being that buf must be kmalloc'ed, and kasprintf() will kfree() it and allocate a new buffer if necessary. Probably too hard to use (and exact semantics on failure would need to be decided; like realloc(), or is the passed-in too-small buffer already kfree'd?)

Rasmus

So, I can go ahead and patch any place that uses *printf() with a VLA to use kasprintf() and iff performance is mentioned look at speeding up kasprintf().

I like it.

thanks,
Tobin.