Skip to content

Commit 6974f0c

Browse files
thestingertorvalds
authored andcommitted
include/linux/string.h: add the option of fortified string.h functions
This adds support for compiling with a rough equivalent to the glibc _FORTIFY_SOURCE=1 feature, providing compile-time and runtime buffer overflow checks for string.h functions when the compiler determines the size of the source or destination buffer at compile-time. Unlike glibc, it covers buffer reads in addition to writes. GNU C __builtin_*_chk intrinsics are avoided because they would force a much more complex implementation. They aren't designed to detect read overflows and offer no real benefit when using an implementation based on inline checks. Inline checks don't add up to much code size and allow full use of the regular string intrinsics while avoiding the need for a bunch of _chk functions and per-arch assembly to avoid wrapper overhead. This detects various overflows at compile-time in various drivers and some non-x86 core kernel code. There will likely be issues caught in regular use at runtime too. Future improvements left out of initial implementation for simplicity, as it's all quite optional and can be done incrementally: * Some of the fortified string functions (strncpy, strcat), don't yet place a limit on reads from the source based on __builtin_object_size of the source buffer. * Extending coverage to more string functions like strlcat. * It should be possible to optionally use __builtin_object_size(x, 1) for some functions (C strings) to detect intra-object overflows (like glibc's _FORTIFY_SOURCE=2), but for now this takes the conservative approach to avoid likely compatibility issues. * The compile-time checks should be made available via a separate config option which can be enabled by default (or always enabled) once enough time has passed to get the issues it catches fixed. Kees said: "This is great to have. While it was out-of-tree code, it would have blocked at least CVE-2016-3858 from being exploitable (improper size argument to strlcpy()). I've sent a number of fixes for out-of-bounds-reads that this detected upstream already" [[email protected]: x86: fix fortified memcpy] Link: http://lkml.kernel.org/r/[email protected] [[email protected]: avoid panic() in favor of BUG()] Link: http://lkml.kernel.org/r/20170626235122.GA25261@beast [[email protected]: move from -mm, add ARCH_HAS_FORTIFY_SOURCE, tweak Kconfig help] Link: http://lkml.kernel.org/r/[email protected] Link: http://lkml.kernel.org/r/[email protected] Signed-off-by: Daniel Micay <[email protected]> Signed-off-by: Kees Cook <[email protected]> Signed-off-by: Arnd Bergmann <[email protected]> Acked-by: Kees Cook <[email protected]> Cc: Mark Rutland <[email protected]> Cc: Daniel Axtens <[email protected]> Cc: Rasmus Villemoes <[email protected]> Cc: Andy Shevchenko <[email protected]> Cc: Chris Metcalf <[email protected]> Cc: Thomas Gleixner <[email protected]> Cc: "H. Peter Anvin" <[email protected]> Cc: Ingo Molnar <[email protected]> Signed-off-by: Andrew Morton <[email protected]> Signed-off-by: Linus Torvalds <[email protected]>
1 parent c69a48c commit 6974f0c

File tree

12 files changed

+250
-1
lines changed

12 files changed

+250
-1
lines changed

arch/Kconfig

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,12 @@ config GENERIC_SMP_IDLE_THREAD
223223
config GENERIC_IDLE_POLL_SETUP
224224
bool
225225

226+
config ARCH_HAS_FORTIFY_SOURCE
227+
bool
228+
help
229+
An architecture should select this when it can successfully
230+
build and run with CONFIG_FORTIFY_SOURCE.
231+
226232
# Select if arch has all set_memory_ro/rw/x/nx() functions in asm/cacheflush.h
227233
config ARCH_HAS_SET_MEMORY
228234
bool

arch/arm64/Kconfig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ config ARM64
1212
select ARCH_HAS_DEVMEM_IS_ALLOWED
1313
select ARCH_HAS_ACPI_TABLE_UPGRADE if ACPI
1414
select ARCH_HAS_ELF_RANDOMIZE
15+
select ARCH_HAS_FORTIFY_SOURCE
1516
select ARCH_HAS_GCOV_PROFILE_ALL
1617
select ARCH_HAS_GIGANTIC_PAGE if (MEMORY_ISOLATION && COMPACTION) || CMA
1718
select ARCH_HAS_KCOV

arch/arm64/include/asm/string.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,11 @@ extern int memcmp(const void *, const void *, size_t);
6363
#define memcpy(dst, src, len) __memcpy(dst, src, len)
6464
#define memmove(dst, src, len) __memmove(dst, src, len)
6565
#define memset(s, c, n) __memset(s, c, n)
66+
67+
#ifndef __NO_FORTIFY
68+
#define __NO_FORTIFY /* FORTIFY_SOURCE uses __builtin_memcpy, etc. */
69+
#endif
70+
6671
#endif
6772

6873
#endif

arch/powerpc/Kconfig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@ config PPC
125125
select ARCH_HAS_DEVMEM_IS_ALLOWED
126126
select ARCH_HAS_DMA_SET_COHERENT_MASK
127127
select ARCH_HAS_ELF_RANDOMIZE
128+
select ARCH_HAS_FORTIFY_SOURCE
128129
select ARCH_HAS_GCOV_PROFILE_ALL
129130
select ARCH_HAS_SCALED_CPUTIME if VIRT_CPU_ACCOUNTING_NATIVE
130131
select ARCH_HAS_SG_CHAIN

arch/x86/Kconfig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ config X86
5050
select ARCH_HAS_DEVMEM_IS_ALLOWED
5151
select ARCH_HAS_ELF_RANDOMIZE
5252
select ARCH_HAS_FAST_MULTIPLIER
53+
select ARCH_HAS_FORTIFY_SOURCE
5354
select ARCH_HAS_GCOV_PROFILE_ALL
5455
select ARCH_HAS_KCOV if X86_64
5556
select ARCH_HAS_MMIO_FLUSH

arch/x86/boot/compressed/misc.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -411,3 +411,8 @@ asmlinkage __visible void *extract_kernel(void *rmode, memptr heap,
411411
debug_putstr("done.\nBooting the kernel.\n");
412412
return output;
413413
}
414+
415+
void fortify_panic(const char *name)
416+
{
417+
error("detected buffer overflow");
418+
}

arch/x86/include/asm/string_32.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,9 @@ static __always_inline void *__constant_memcpy(void *to, const void *from,
142142
}
143143

144144
#define __HAVE_ARCH_MEMCPY
145+
extern void *memcpy(void *, const void *, size_t);
145146

147+
#ifndef CONFIG_FORTIFY_SOURCE
146148
#ifdef CONFIG_X86_USE_3DNOW
147149

148150
#include <asm/mmx.h>
@@ -195,11 +197,15 @@ static inline void *__memcpy3d(void *to, const void *from, size_t len)
195197
#endif
196198

197199
#endif
200+
#endif /* !CONFIG_FORTIFY_SOURCE */
198201

199202
#define __HAVE_ARCH_MEMMOVE
200203
void *memmove(void *dest, const void *src, size_t n);
201204

205+
extern int memcmp(const void *, const void *, size_t);
206+
#ifndef CONFIG_FORTIFY_SOURCE
202207
#define memcmp __builtin_memcmp
208+
#endif
203209

204210
#define __HAVE_ARCH_MEMCHR
205211
extern void *memchr(const void *cs, int c, size_t count);
@@ -321,6 +327,8 @@ void *__constant_c_and_count_memset(void *s, unsigned long pattern,
321327
: __memset_generic((s), (c), (count)))
322328

323329
#define __HAVE_ARCH_MEMSET
330+
extern void *memset(void *, int, size_t);
331+
#ifndef CONFIG_FORTIFY_SOURCE
324332
#if (__GNUC__ >= 4)
325333
#define memset(s, c, count) __builtin_memset(s, c, count)
326334
#else
@@ -330,6 +338,7 @@ void *__constant_c_and_count_memset(void *s, unsigned long pattern,
330338
(count)) \
331339
: __memset((s), (c), (count)))
332340
#endif
341+
#endif /* !CONFIG_FORTIFY_SOURCE */
333342

334343
/*
335344
* find the first occurrence of byte 'c', or 1 past the area if none

arch/x86/include/asm/string_64.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ static __always_inline void *__inline_memcpy(void *to, const void *from, size_t
3131
extern void *memcpy(void *to, const void *from, size_t len);
3232
extern void *__memcpy(void *to, const void *from, size_t len);
3333

34+
#ifndef CONFIG_FORTIFY_SOURCE
3435
#ifndef CONFIG_KMEMCHECK
3536
#if (__GNUC__ == 4 && __GNUC_MINOR__ < 3) || __GNUC__ < 4
3637
#define memcpy(dst, src, len) \
@@ -51,6 +52,7 @@ extern void *__memcpy(void *to, const void *from, size_t len);
5152
*/
5253
#define memcpy(dst, src, len) __inline_memcpy((dst), (src), (len))
5354
#endif
55+
#endif /* !CONFIG_FORTIFY_SOURCE */
5456

5557
#define __HAVE_ARCH_MEMSET
5658
void *memset(void *s, int c, size_t n);
@@ -77,6 +79,11 @@ int strcmp(const char *cs, const char *ct);
7779
#define memcpy(dst, src, len) __memcpy(dst, src, len)
7880
#define memmove(dst, src, len) __memmove(dst, src, len)
7981
#define memset(s, c, n) __memset(s, c, n)
82+
83+
#ifndef __NO_FORTIFY
84+
#define __NO_FORTIFY /* FORTIFY_SOURCE uses __builtin_memcpy, etc. */
85+
#endif
86+
8087
#endif
8188

8289
#define __HAVE_ARCH_MEMCPY_MCSAFE 1

arch/x86/lib/memcpy_32.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
__visible void *memcpy(void *to, const void *from, size_t n)
88
{
9-
#ifdef CONFIG_X86_USE_3DNOW
9+
#if defined(CONFIG_X86_USE_3DNOW) && !defined(CONFIG_FORTIFY_SOURCE)
1010
return __memcpy3d(to, from, n);
1111
#else
1212
return __memcpy(to, from, n);

include/linux/string.h

Lines changed: 200 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,4 +193,204 @@ static inline const char *kbasename(const char *path)
193193
return tail ? tail + 1 : path;
194194
}
195195

196+
#define __FORTIFY_INLINE extern __always_inline __attribute__((gnu_inline))
197+
#define __RENAME(x) __asm__(#x)
198+
199+
void fortify_panic(const char *name) __noreturn __cold;
200+
void __read_overflow(void) __compiletime_error("detected read beyond size of object passed as 1st parameter");
201+
void __read_overflow2(void) __compiletime_error("detected read beyond size of object passed as 2nd parameter");
202+
void __write_overflow(void) __compiletime_error("detected write beyond size of object passed as 1st parameter");
203+
204+
#if !defined(__NO_FORTIFY) && defined(__OPTIMIZE__) && defined(CONFIG_FORTIFY_SOURCE)
205+
__FORTIFY_INLINE char *strcpy(char *p, const char *q)
206+
{
207+
size_t p_size = __builtin_object_size(p, 0);
208+
size_t q_size = __builtin_object_size(q, 0);
209+
if (p_size == (size_t)-1 && q_size == (size_t)-1)
210+
return __builtin_strcpy(p, q);
211+
if (strscpy(p, q, p_size < q_size ? p_size : q_size) < 0)
212+
fortify_panic(__func__);
213+
return p;
214+
}
215+
216+
__FORTIFY_INLINE char *strncpy(char *p, const char *q, __kernel_size_t size)
217+
{
218+
size_t p_size = __builtin_object_size(p, 0);
219+
if (__builtin_constant_p(size) && p_size < size)
220+
__write_overflow();
221+
if (p_size < size)
222+
fortify_panic(__func__);
223+
return __builtin_strncpy(p, q, size);
224+
}
225+
226+
__FORTIFY_INLINE char *strcat(char *p, const char *q)
227+
{
228+
size_t p_size = __builtin_object_size(p, 0);
229+
if (p_size == (size_t)-1)
230+
return __builtin_strcat(p, q);
231+
if (strlcat(p, q, p_size) >= p_size)
232+
fortify_panic(__func__);
233+
return p;
234+
}
235+
236+
__FORTIFY_INLINE __kernel_size_t strlen(const char *p)
237+
{
238+
__kernel_size_t ret;
239+
size_t p_size = __builtin_object_size(p, 0);
240+
if (p_size == (size_t)-1)
241+
return __builtin_strlen(p);
242+
ret = strnlen(p, p_size);
243+
if (p_size <= ret)
244+
fortify_panic(__func__);
245+
return ret;
246+
}
247+
248+
extern __kernel_size_t __real_strnlen(const char *, __kernel_size_t) __RENAME(strnlen);
249+
__FORTIFY_INLINE __kernel_size_t strnlen(const char *p, __kernel_size_t maxlen)
250+
{
251+
size_t p_size = __builtin_object_size(p, 0);
252+
__kernel_size_t ret = __real_strnlen(p, maxlen < p_size ? maxlen : p_size);
253+
if (p_size <= ret && maxlen != ret)
254+
fortify_panic(__func__);
255+
return ret;
256+
}
257+
258+
/* defined after fortified strlen to reuse it */
259+
extern size_t __real_strlcpy(char *, const char *, size_t) __RENAME(strlcpy);
260+
__FORTIFY_INLINE size_t strlcpy(char *p, const char *q, size_t size)
261+
{
262+
size_t ret;
263+
size_t p_size = __builtin_object_size(p, 0);
264+
size_t q_size = __builtin_object_size(q, 0);
265+
if (p_size == (size_t)-1 && q_size == (size_t)-1)
266+
return __real_strlcpy(p, q, size);
267+
ret = strlen(q);
268+
if (size) {
269+
size_t len = (ret >= size) ? size - 1 : ret;
270+
if (__builtin_constant_p(len) && len >= p_size)
271+
__write_overflow();
272+
if (len >= p_size)
273+
fortify_panic(__func__);
274+
__builtin_memcpy(p, q, len);
275+
p[len] = '\0';
276+
}
277+
return ret;
278+
}
279+
280+
/* defined after fortified strlen and strnlen to reuse them */
281+
__FORTIFY_INLINE char *strncat(char *p, const char *q, __kernel_size_t count)
282+
{
283+
size_t p_len, copy_len;
284+
size_t p_size = __builtin_object_size(p, 0);
285+
size_t q_size = __builtin_object_size(q, 0);
286+
if (p_size == (size_t)-1 && q_size == (size_t)-1)
287+
return __builtin_strncat(p, q, count);
288+
p_len = strlen(p);
289+
copy_len = strnlen(q, count);
290+
if (p_size < p_len + copy_len + 1)
291+
fortify_panic(__func__);
292+
__builtin_memcpy(p + p_len, q, copy_len);
293+
p[p_len + copy_len] = '\0';
294+
return p;
295+
}
296+
297+
__FORTIFY_INLINE void *memset(void *p, int c, __kernel_size_t size)
298+
{
299+
size_t p_size = __builtin_object_size(p, 0);
300+
if (__builtin_constant_p(size) && p_size < size)
301+
__write_overflow();
302+
if (p_size < size)
303+
fortify_panic(__func__);
304+
return __builtin_memset(p, c, size);
305+
}
306+
307+
__FORTIFY_INLINE void *memcpy(void *p, const void *q, __kernel_size_t size)
308+
{
309+
size_t p_size = __builtin_object_size(p, 0);
310+
size_t q_size = __builtin_object_size(q, 0);
311+
if (__builtin_constant_p(size)) {
312+
if (p_size < size)
313+
__write_overflow();
314+
if (q_size < size)
315+
__read_overflow2();
316+
}
317+
if (p_size < size || q_size < size)
318+
fortify_panic(__func__);
319+
return __builtin_memcpy(p, q, size);
320+
}
321+
322+
__FORTIFY_INLINE void *memmove(void *p, const void *q, __kernel_size_t size)
323+
{
324+
size_t p_size = __builtin_object_size(p, 0);
325+
size_t q_size = __builtin_object_size(q, 0);
326+
if (__builtin_constant_p(size)) {
327+
if (p_size < size)
328+
__write_overflow();
329+
if (q_size < size)
330+
__read_overflow2();
331+
}
332+
if (p_size < size || q_size < size)
333+
fortify_panic(__func__);
334+
return __builtin_memmove(p, q, size);
335+
}
336+
337+
extern void *__real_memscan(void *, int, __kernel_size_t) __RENAME(memscan);
338+
__FORTIFY_INLINE void *memscan(void *p, int c, __kernel_size_t size)
339+
{
340+
size_t p_size = __builtin_object_size(p, 0);
341+
if (__builtin_constant_p(size) && p_size < size)
342+
__read_overflow();
343+
if (p_size < size)
344+
fortify_panic(__func__);
345+
return __real_memscan(p, c, size);
346+
}
347+
348+
__FORTIFY_INLINE int memcmp(const void *p, const void *q, __kernel_size_t size)
349+
{
350+
size_t p_size = __builtin_object_size(p, 0);
351+
size_t q_size = __builtin_object_size(q, 0);
352+
if (__builtin_constant_p(size)) {
353+
if (p_size < size)
354+
__read_overflow();
355+
if (q_size < size)
356+
__read_overflow2();
357+
}
358+
if (p_size < size || q_size < size)
359+
fortify_panic(__func__);
360+
return __builtin_memcmp(p, q, size);
361+
}
362+
363+
__FORTIFY_INLINE void *memchr(const void *p, int c, __kernel_size_t size)
364+
{
365+
size_t p_size = __builtin_object_size(p, 0);
366+
if (__builtin_constant_p(size) && p_size < size)
367+
__read_overflow();
368+
if (p_size < size)
369+
fortify_panic(__func__);
370+
return __builtin_memchr(p, c, size);
371+
}
372+
373+
void *__real_memchr_inv(const void *s, int c, size_t n) __RENAME(memchr_inv);
374+
__FORTIFY_INLINE void *memchr_inv(const void *p, int c, size_t size)
375+
{
376+
size_t p_size = __builtin_object_size(p, 0);
377+
if (__builtin_constant_p(size) && p_size < size)
378+
__read_overflow();
379+
if (p_size < size)
380+
fortify_panic(__func__);
381+
return __real_memchr_inv(p, c, size);
382+
}
383+
384+
extern void *__real_kmemdup(const void *src, size_t len, gfp_t gfp) __RENAME(kmemdup);
385+
__FORTIFY_INLINE void *kmemdup(const void *p, size_t size, gfp_t gfp)
386+
{
387+
size_t p_size = __builtin_object_size(p, 0);
388+
if (__builtin_constant_p(size) && p_size < size)
389+
__read_overflow();
390+
if (p_size < size)
391+
fortify_panic(__func__);
392+
return __real_kmemdup(p, size, gfp);
393+
}
394+
#endif
395+
196396
#endif /* _LINUX_STRING_H_ */

lib/string.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -978,3 +978,10 @@ char *strreplace(char *s, char old, char new)
978978
return s;
979979
}
980980
EXPORT_SYMBOL(strreplace);
981+
982+
void fortify_panic(const char *name)
983+
{
984+
pr_emerg("detected buffer overflow in %s\n", name);
985+
BUG();
986+
}
987+
EXPORT_SYMBOL(fortify_panic);

security/Kconfig

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,13 @@ config HARDENED_USERCOPY_PAGESPAN
163163
been removed. This config is intended to be used only while
164164
trying to find such users.
165165

166+
config FORTIFY_SOURCE
167+
bool "Harden common str/mem functions against buffer overflows"
168+
depends on ARCH_HAS_FORTIFY_SOURCE
169+
help
170+
Detect overflows of buffers in common string and memory functions
171+
where the compiler can determine and validate the buffer sizes.
172+
166173
config STATIC_USERMODEHELPER
167174
bool "Force all usermode helper calls through a single binary"
168175
help

0 commit comments

Comments
 (0)