Skip to contentSkip to conversation

Patch "string.h: workaround for increased stack usage" has been added to the 4.14-stable tree

PATCH [email protected] 1 participant 1 message Dec 18, 2017

Subsystems (2)
lore
Share & tools
lore

[email protected]

author maintainer Applied
#0 lore raw

This is a note to let you know that I've just added the patch titled

string.h: workaround for increased stack usage

to the 4.14-stable tree which can be found at:
http://www.kernel.org/git/?p=linux/kernel/git/stable/stable-queue.git;a=summary

The filename of the patch is:
string.h-workaround-for-increased-stack-usage.patch
and it can be found in the queue-4.14 subdirectory.

If you, or anyone else, feels it should not be added to the stable tree, please let <[email protected]> know about it.

Quoted 1 line

From 146734b091430c80d80bb96b1139a96fb4bc830e Mon Sep 17 00:00:00 2001

From: Arnd Bergmann <[email protected]>
Date: Thu, 14 Dec 2017 15:32:34 -0800
Subject: string.h: workaround for increased stack usage

From: Arnd Bergmann <[email protected]>

commit 146734b091430c80d80bb96b1139a96fb4bc830e upstream.

The hardened strlen() function causes rather large stack usage in at least one file in the kernel, in particular when CONFIG_KASAN is enabled:

drivers/media/usb/em28xx/em28xx-dvb.c: In function 'em28xx_dvb_init':
drivers/media/usb/em28xx/em28xx-dvb.c:2062:1: error: the frame size of 3256 bytes is larger than 204 bytes [-Werror=frame-larger-than=]

Analyzing this problem led to the discovery that gcc fails to merge the stack slots for the i2c_board_info[] structures after we strlcpy() into them, due to the 'noreturn' attribute on the source string length check.

I reported this as a gcc bug, but it is unlikely to get fixed for gcc-8, since it is relatively easy to work around, and it gets triggered rarely. An earlier workaround I did added an empty inline assembly statement before the call to fortify_panic(), which works surprisingly well, but is really ugly and unintuitive.

This is a new approach to the same problem, this time addressing it by not calling the 'extern __real_strnlen()' function for string constants where __builtin_strlen() is a compile-time constant and therefore known to be safe.

We do this by checking if the last character in the string is a compile-time constant '\0'. If it is, we can assume that strlen() of the string is also constant.

As a side-effect, this should also improve the object code output for any other call of strlen() on a string constant.

[[email protected]: add comment]
Fixes 6974f0c4555 ("include/linux/string.h: add the option of fortified string.h functions")
Cc Kees Cook <[email protected]>
Cc Mauro Carvalho Chehab <[email protected]>
Cc Dmitry Vyukov <[email protected]>
Cc Alexander Potapenko <[email protected]>
Cc Andrey Ryabinin <[email protected]>
Cc Daniel Micay <[email protected]>
Cc Greg Kroah-Hartman <[email protected]>
Cc Martin Wilck <[email protected]>
Cc Dan Williams <[email protected]>
---
 include/linux/string.h |    5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
include/linux/string.h +4 -1
@@ -259,7 +259,10 @@ __FORTIFY_INLINE __kernel_size_t strlen(
 {
 	__kernel_size_t ret;
 	size_t p_size = __builtin_object_size(p, 0);
-	if (p_size == (size_t)-1)
+
+	/* Work around gcc excess stack consumption issue */
+	if (p_size == (size_t)-1 ||
+	    (__builtin_constant_p(p[p_size - 1]) && p[p_size - 1] == '\0'))
 		return __builtin_strlen(p);
 	ret = strnlen(p, p_size);
 	if (p_size <= ret)

Patches currently in stable-queue which might be from [email protected] are

queue-4.14/string.h-workaround-for-increased-stack-usage.patch