aboutsummaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
authorLinus Torvalds <torvalds@linux-foundation.org>2015-02-14 12:22:35 -0500
committerLinus Torvalds <torvalds@linux-foundation.org>2015-02-14 12:22:35 -0500
commit83e047c104aa95a8a683d6bd421df1551c17dbd2 (patch)
tree494aef444659ebe5bcbd0e423f91e3eff33100c4 /lib
parent18320f2a6871aaf2522f793fee4a67eccf5e131a (diff)
parenta3b30e7210c870d79d3c3cedb80f8cfaab0f4e2e (diff)
Merge branch 'akpm' (patches from Andrew)
Merge fourth set of updates from Andrew Morton: - the rest of lib/ - checkpatch updates - a few misc things - kasan: kernel address sanitizer - the rtc tree * emailed patches from Andrew Morton <akpm@linux-foundation.org>: (108 commits) ARM: mvebu: enable Armada 38x RTC driver in mvebu_v7_defconfig ARM: mvebu: add Device Tree description of RTC on Armada 38x MAINTAINERS: add the RTC driver for the Armada38x drivers/rtc/rtc-armada38x: add a new RTC driver for recent mvebu SoCs rtc: armada38x: add the device tree binding documentation rtc: rtc-ab-b5ze-s3: add sub-minute alarm support rtc: add support for Abracon AB-RTCMC-32.768kHz-B5ZE-S3 I2C RTC chip of: add vendor prefix for Abracon Corporation drivers/rtc/rtc-rk808.c: fix rtc time reading issue drivers/rtc/rtc-isl12057.c: constify struct regmap_config drivers/rtc/rtc-at91sam9.c: constify struct regmap_config drivers/rtc/rtc-imxdi.c: add more known register bits drivers/rtc/rtc-imxdi.c: trivial clean up code ARM: mvebu: ISL12057 rtc chip can now wake up RN102, RN102 and RN2120 rtc: rtc-isl12057: add isil,irq2-can-wakeup-machine property for in-tree users drivers/rtc/rtc-isl12057.c: add alarm support to Intersil ISL12057 RTC driver drivers/rtc/rtc-pcf2123.c: add support for devicetree kprobes: makes kprobes/enabled works correctly for optimized kprobes. kprobes: set kprobes_all_disarmed earlier to enable re-optimization. init: remove CONFIG_INIT_FALLBACK ...
Diffstat (limited to 'lib')
-rw-r--r--lib/Kconfig.debug2
-rw-r--r--lib/Kconfig.kasan54
-rw-r--r--lib/Makefile7
-rw-r--r--lib/bitmap.c160
-rw-r--r--lib/gen_crc32table.c6
-rw-r--r--lib/genalloc.c2
-rw-r--r--lib/seq_buf.c36
-rw-r--r--lib/string.c12
-rw-r--r--lib/test_kasan.c277
-rw-r--r--lib/vsprintf.c94
10 files changed, 470 insertions, 180 deletions
diff --git a/lib/Kconfig.debug b/lib/Kconfig.debug
index 79a9bb67aeaf..ecb3516f6546 100644
--- a/lib/Kconfig.debug
+++ b/lib/Kconfig.debug
@@ -651,6 +651,8 @@ config DEBUG_STACKOVERFLOW
651 651
652source "lib/Kconfig.kmemcheck" 652source "lib/Kconfig.kmemcheck"
653 653
654source "lib/Kconfig.kasan"
655
654endmenu # "Memory Debugging" 656endmenu # "Memory Debugging"
655 657
656config DEBUG_SHIRQ 658config DEBUG_SHIRQ
diff --git a/lib/Kconfig.kasan b/lib/Kconfig.kasan
new file mode 100644
index 000000000000..4fecaedc80a2
--- /dev/null
+++ b/lib/Kconfig.kasan
@@ -0,0 +1,54 @@
1config HAVE_ARCH_KASAN
2 bool
3
4if HAVE_ARCH_KASAN
5
6config KASAN
7 bool "KASan: runtime memory debugger"
8 depends on SLUB_DEBUG
9 select CONSTRUCTORS
10 help
11 Enables kernel address sanitizer - runtime memory debugger,
12 designed to find out-of-bounds accesses and use-after-free bugs.
13 This is strictly debugging feature. It consumes about 1/8
14 of available memory and brings about ~x3 performance slowdown.
15 For better error detection enable CONFIG_STACKTRACE,
16 and add slub_debug=U to boot cmdline.
17
18config KASAN_SHADOW_OFFSET
19 hex
20 default 0xdffffc0000000000 if X86_64
21
22choice
23 prompt "Instrumentation type"
24 depends on KASAN
25 default KASAN_OUTLINE
26
27config KASAN_OUTLINE
28 bool "Outline instrumentation"
29 help
30 Before every memory access compiler insert function call
31 __asan_load*/__asan_store*. These functions performs check
32 of shadow memory. This is slower than inline instrumentation,
33 however it doesn't bloat size of kernel's .text section so
34 much as inline does.
35
36config KASAN_INLINE
37 bool "Inline instrumentation"
38 help
39 Compiler directly inserts code checking shadow memory before
40 memory accesses. This is faster than outline (in some workloads
41 it gives about x2 boost over outline instrumentation), but
42 make kernel's .text size much bigger.
43
44endchoice
45
46config TEST_KASAN
47 tristate "Module for testing kasan for bug detection"
48 depends on m && KASAN
49 help
50 This is a test module doing various nasty things like
51 out of bounds accesses, use after free. It is useful for testing
52 kernel debugging features like kernel address sanitizer.
53
54endif
diff --git a/lib/Makefile b/lib/Makefile
index e456defd1021..87eb3bffc283 100644
--- a/lib/Makefile
+++ b/lib/Makefile
@@ -32,12 +32,13 @@ obj-$(CONFIG_TEST_STRING_HELPERS) += test-string_helpers.o
32obj-y += hexdump.o 32obj-y += hexdump.o
33obj-$(CONFIG_TEST_HEXDUMP) += test-hexdump.o 33obj-$(CONFIG_TEST_HEXDUMP) += test-hexdump.o
34obj-y += kstrtox.o 34obj-y += kstrtox.o
35obj-$(CONFIG_TEST_KSTRTOX) += test-kstrtox.o
36obj-$(CONFIG_TEST_LKM) += test_module.o
37obj-$(CONFIG_TEST_USER_COPY) += test_user_copy.o
38obj-$(CONFIG_TEST_BPF) += test_bpf.o 35obj-$(CONFIG_TEST_BPF) += test_bpf.o
39obj-$(CONFIG_TEST_FIRMWARE) += test_firmware.o 36obj-$(CONFIG_TEST_FIRMWARE) += test_firmware.o
37obj-$(CONFIG_TEST_KASAN) += test_kasan.o
38obj-$(CONFIG_TEST_KSTRTOX) += test-kstrtox.o
39obj-$(CONFIG_TEST_LKM) += test_module.o
40obj-$(CONFIG_TEST_RHASHTABLE) += test_rhashtable.o 40obj-$(CONFIG_TEST_RHASHTABLE) += test_rhashtable.o
41obj-$(CONFIG_TEST_USER_COPY) += test_user_copy.o
41 42
42ifeq ($(CONFIG_DEBUG_KOBJECT),y) 43ifeq ($(CONFIG_DEBUG_KOBJECT),y)
43CFLAGS_kobject.o += -DDEBUG 44CFLAGS_kobject.o += -DDEBUG
diff --git a/lib/bitmap.c b/lib/bitmap.c
index ad161a6c82db..d456f4c15a9f 100644
--- a/lib/bitmap.c
+++ b/lib/bitmap.c
@@ -104,18 +104,18 @@ EXPORT_SYMBOL(__bitmap_complement);
104 * @dst : destination bitmap 104 * @dst : destination bitmap
105 * @src : source bitmap 105 * @src : source bitmap
106 * @shift : shift by this many bits 106 * @shift : shift by this many bits
107 * @bits : bitmap size, in bits 107 * @nbits : bitmap size, in bits
108 * 108 *
109 * Shifting right (dividing) means moving bits in the MS -> LS bit 109 * Shifting right (dividing) means moving bits in the MS -> LS bit
110 * direction. Zeros are fed into the vacated MS positions and the 110 * direction. Zeros are fed into the vacated MS positions and the
111 * LS bits shifted off the bottom are lost. 111 * LS bits shifted off the bottom are lost.
112 */ 112 */
113void __bitmap_shift_right(unsigned long *dst, 113void __bitmap_shift_right(unsigned long *dst, const unsigned long *src,
114 const unsigned long *src, int shift, int bits) 114 unsigned shift, unsigned nbits)
115{ 115{
116 int k, lim = BITS_TO_LONGS(bits), left = bits % BITS_PER_LONG; 116 unsigned k, lim = BITS_TO_LONGS(nbits);
117 int off = shift/BITS_PER_LONG, rem = shift % BITS_PER_LONG; 117 unsigned off = shift/BITS_PER_LONG, rem = shift % BITS_PER_LONG;
118 unsigned long mask = (1UL << left) - 1; 118 unsigned long mask = BITMAP_LAST_WORD_MASK(nbits);
119 for (k = 0; off + k < lim; ++k) { 119 for (k = 0; off + k < lim; ++k) {
120 unsigned long upper, lower; 120 unsigned long upper, lower;
121 121
@@ -127,17 +127,15 @@ void __bitmap_shift_right(unsigned long *dst,
127 upper = 0; 127 upper = 0;
128 else { 128 else {
129 upper = src[off + k + 1]; 129 upper = src[off + k + 1];
130 if (off + k + 1 == lim - 1 && left) 130 if (off + k + 1 == lim - 1)
131 upper &= mask; 131 upper &= mask;
132 upper <<= (BITS_PER_LONG - rem);
132 } 133 }
133 lower = src[off + k]; 134 lower = src[off + k];
134 if (left && off + k == lim - 1) 135 if (off + k == lim - 1)
135 lower &= mask; 136 lower &= mask;
136 dst[k] = lower >> rem; 137 lower >>= rem;
137 if (rem) 138 dst[k] = lower | upper;
138 dst[k] |= upper << (BITS_PER_LONG - rem);
139 if (left && k == lim - 1)
140 dst[k] &= mask;
141 } 139 }
142 if (off) 140 if (off)
143 memset(&dst[lim - off], 0, off*sizeof(unsigned long)); 141 memset(&dst[lim - off], 0, off*sizeof(unsigned long));
@@ -150,18 +148,19 @@ EXPORT_SYMBOL(__bitmap_shift_right);
150 * @dst : destination bitmap 148 * @dst : destination bitmap
151 * @src : source bitmap 149 * @src : source bitmap
152 * @shift : shift by this many bits 150 * @shift : shift by this many bits
153 * @bits : bitmap size, in bits 151 * @nbits : bitmap size, in bits
154 * 152 *
155 * Shifting left (multiplying) means moving bits in the LS -> MS 153 * Shifting left (multiplying) means moving bits in the LS -> MS
156 * direction. Zeros are fed into the vacated LS bit positions 154 * direction. Zeros are fed into the vacated LS bit positions
157 * and those MS bits shifted off the top are lost. 155 * and those MS bits shifted off the top are lost.
158 */ 156 */
159 157
160void __bitmap_shift_left(unsigned long *dst, 158void __bitmap_shift_left(unsigned long *dst, const unsigned long *src,
161 const unsigned long *src, int shift, int bits) 159 unsigned int shift, unsigned int nbits)
162{ 160{
163 int k, lim = BITS_TO_LONGS(bits), left = bits % BITS_PER_LONG; 161 int k;
164 int off = shift/BITS_PER_LONG, rem = shift % BITS_PER_LONG; 162 unsigned int lim = BITS_TO_LONGS(nbits);
163 unsigned int off = shift/BITS_PER_LONG, rem = shift % BITS_PER_LONG;
165 for (k = lim - off - 1; k >= 0; --k) { 164 for (k = lim - off - 1; k >= 0; --k) {
166 unsigned long upper, lower; 165 unsigned long upper, lower;
167 166
@@ -170,17 +169,11 @@ void __bitmap_shift_left(unsigned long *dst,
170 * word below and make them the bottom rem bits of result. 169 * word below and make them the bottom rem bits of result.
171 */ 170 */
172 if (rem && k > 0) 171 if (rem && k > 0)
173 lower = src[k - 1]; 172 lower = src[k - 1] >> (BITS_PER_LONG - rem);
174 else 173 else
175 lower = 0; 174 lower = 0;
176 upper = src[k]; 175 upper = src[k] << rem;
177 if (left && k == lim - 1) 176 dst[k + off] = lower | upper;
178 upper &= (1UL << left) - 1;
179 dst[k + off] = upper << rem;
180 if (rem)
181 dst[k + off] |= lower >> (BITS_PER_LONG - rem);
182 if (left && k + off == lim - 1)
183 dst[k + off] &= (1UL << left) - 1;
184 } 177 }
185 if (off) 178 if (off)
186 memset(dst, 0, off*sizeof(unsigned long)); 179 memset(dst, 0, off*sizeof(unsigned long));
@@ -377,45 +370,6 @@ EXPORT_SYMBOL(bitmap_find_next_zero_area_off);
377#define BASEDEC 10 /* fancier cpuset lists input in decimal */ 370#define BASEDEC 10 /* fancier cpuset lists input in decimal */
378 371
379/** 372/**
380 * bitmap_scnprintf - convert bitmap to an ASCII hex string.
381 * @buf: byte buffer into which string is placed
382 * @buflen: reserved size of @buf, in bytes
383 * @maskp: pointer to bitmap to convert
384 * @nmaskbits: size of bitmap, in bits
385 *
386 * Exactly @nmaskbits bits are displayed. Hex digits are grouped into
387 * comma-separated sets of eight digits per set. Returns the number of
388 * characters which were written to *buf, excluding the trailing \0.
389 */
390int bitmap_scnprintf(char *buf, unsigned int buflen,
391 const unsigned long *maskp, int nmaskbits)
392{
393 int i, word, bit, len = 0;
394 unsigned long val;
395 const char *sep = "";
396 int chunksz;
397 u32 chunkmask;
398
399 chunksz = nmaskbits & (CHUNKSZ - 1);
400 if (chunksz == 0)
401 chunksz = CHUNKSZ;
402
403 i = ALIGN(nmaskbits, CHUNKSZ) - CHUNKSZ;
404 for (; i >= 0; i -= CHUNKSZ) {
405 chunkmask = ((1ULL << chunksz) - 1);
406 word = i / BITS_PER_LONG;
407 bit = i % BITS_PER_LONG;
408 val = (maskp[word] >> bit) & chunkmask;
409 len += scnprintf(buf+len, buflen-len, "%s%0*lx", sep,
410 (chunksz+3)/4, val);
411 chunksz = CHUNKSZ;
412 sep = ",";
413 }
414 return len;
415}
416EXPORT_SYMBOL(bitmap_scnprintf);
417
418/**
419 * __bitmap_parse - convert an ASCII hex string into a bitmap. 373 * __bitmap_parse - convert an ASCII hex string into a bitmap.
420 * @buf: pointer to buffer containing string. 374 * @buf: pointer to buffer containing string.
421 * @buflen: buffer size in bytes. If string is smaller than this 375 * @buflen: buffer size in bytes. If string is smaller than this
@@ -528,65 +482,6 @@ int bitmap_parse_user(const char __user *ubuf,
528} 482}
529EXPORT_SYMBOL(bitmap_parse_user); 483EXPORT_SYMBOL(bitmap_parse_user);
530 484
531/*
532 * bscnl_emit(buf, buflen, rbot, rtop, bp)
533 *
534 * Helper routine for bitmap_scnlistprintf(). Write decimal number
535 * or range to buf, suppressing output past buf+buflen, with optional
536 * comma-prefix. Return len of what was written to *buf, excluding the
537 * trailing \0.
538 */
539static inline int bscnl_emit(char *buf, int buflen, int rbot, int rtop, int len)
540{
541 if (len > 0)
542 len += scnprintf(buf + len, buflen - len, ",");
543 if (rbot == rtop)
544 len += scnprintf(buf + len, buflen - len, "%d", rbot);
545 else
546 len += scnprintf(buf + len, buflen - len, "%d-%d", rbot, rtop);
547 return len;
548}
549
550/**
551 * bitmap_scnlistprintf - convert bitmap to list format ASCII string
552 * @buf: byte buffer into which string is placed
553 * @buflen: reserved size of @buf, in bytes
554 * @maskp: pointer to bitmap to convert
555 * @nmaskbits: size of bitmap, in bits
556 *
557 * Output format is a comma-separated list of decimal numbers and
558 * ranges. Consecutively set bits are shown as two hyphen-separated
559 * decimal numbers, the smallest and largest bit numbers set in
560 * the range. Output format is compatible with the format
561 * accepted as input by bitmap_parselist().
562 *
563 * The return value is the number of characters which were written to *buf
564 * excluding the trailing '\0', as per ISO C99's scnprintf.
565 */
566int bitmap_scnlistprintf(char *buf, unsigned int buflen,
567 const unsigned long *maskp, int nmaskbits)
568{
569 int len = 0;
570 /* current bit is 'cur', most recently seen range is [rbot, rtop] */
571 int cur, rbot, rtop;
572
573 if (buflen == 0)
574 return 0;
575 buf[0] = 0;
576
577 rbot = cur = find_first_bit(maskp, nmaskbits);
578 while (cur < nmaskbits) {
579 rtop = cur;
580 cur = find_next_bit(maskp, nmaskbits, cur+1);
581 if (cur >= nmaskbits || cur > rtop + 1) {
582 len = bscnl_emit(buf, buflen, rbot, rtop, len);
583 rbot = cur;
584 }
585 }
586 return len;
587}
588EXPORT_SYMBOL(bitmap_scnlistprintf);
589
590/** 485/**
591 * bitmap_print_to_pagebuf - convert bitmap to list or hex format ASCII string 486 * bitmap_print_to_pagebuf - convert bitmap to list or hex format ASCII string
592 * @list: indicates whether the bitmap must be list 487 * @list: indicates whether the bitmap must be list
@@ -605,8 +500,8 @@ int bitmap_print_to_pagebuf(bool list, char *buf, const unsigned long *maskp,
605 int n = 0; 500 int n = 0;
606 501
607 if (len > 1) { 502 if (len > 1) {
608 n = list ? bitmap_scnlistprintf(buf, len, maskp, nmaskbits) : 503 n = list ? scnprintf(buf, len, "%*pbl", nmaskbits, maskp) :
609 bitmap_scnprintf(buf, len, maskp, nmaskbits); 504 scnprintf(buf, len, "%*pb", nmaskbits, maskp);
610 buf[n++] = '\n'; 505 buf[n++] = '\n';
611 buf[n] = '\0'; 506 buf[n] = '\0';
612 } 507 }
@@ -1191,16 +1086,17 @@ EXPORT_SYMBOL(bitmap_allocate_region);
1191 * 1086 *
1192 * Require nbits % BITS_PER_LONG == 0. 1087 * Require nbits % BITS_PER_LONG == 0.
1193 */ 1088 */
1194void bitmap_copy_le(void *dst, const unsigned long *src, int nbits) 1089#ifdef __BIG_ENDIAN
1090void bitmap_copy_le(unsigned long *dst, const unsigned long *src, unsigned int nbits)
1195{ 1091{
1196 unsigned long *d = dst; 1092 unsigned int i;
1197 int i;
1198 1093
1199 for (i = 0; i < nbits/BITS_PER_LONG; i++) { 1094 for (i = 0; i < nbits/BITS_PER_LONG; i++) {
1200 if (BITS_PER_LONG == 64) 1095 if (BITS_PER_LONG == 64)
1201 d[i] = cpu_to_le64(src[i]); 1096 dst[i] = cpu_to_le64(src[i]);
1202 else 1097 else
1203 d[i] = cpu_to_le32(src[i]); 1098 dst[i] = cpu_to_le32(src[i]);
1204 } 1099 }
1205} 1100}
1206EXPORT_SYMBOL(bitmap_copy_le); 1101EXPORT_SYMBOL(bitmap_copy_le);
1102#endif
diff --git a/lib/gen_crc32table.c b/lib/gen_crc32table.c
index 71fcfcd96410..d83a372fa76f 100644
--- a/lib/gen_crc32table.c
+++ b/lib/gen_crc32table.c
@@ -109,7 +109,7 @@ int main(int argc, char** argv)
109 109
110 if (CRC_LE_BITS > 1) { 110 if (CRC_LE_BITS > 1) {
111 crc32init_le(); 111 crc32init_le();
112 printf("static u32 __cacheline_aligned " 112 printf("static const u32 ____cacheline_aligned "
113 "crc32table_le[%d][%d] = {", 113 "crc32table_le[%d][%d] = {",
114 LE_TABLE_ROWS, LE_TABLE_SIZE); 114 LE_TABLE_ROWS, LE_TABLE_SIZE);
115 output_table(crc32table_le, LE_TABLE_ROWS, 115 output_table(crc32table_le, LE_TABLE_ROWS,
@@ -119,7 +119,7 @@ int main(int argc, char** argv)
119 119
120 if (CRC_BE_BITS > 1) { 120 if (CRC_BE_BITS > 1) {
121 crc32init_be(); 121 crc32init_be();
122 printf("static u32 __cacheline_aligned " 122 printf("static const u32 ____cacheline_aligned "
123 "crc32table_be[%d][%d] = {", 123 "crc32table_be[%d][%d] = {",
124 BE_TABLE_ROWS, BE_TABLE_SIZE); 124 BE_TABLE_ROWS, BE_TABLE_SIZE);
125 output_table(crc32table_be, LE_TABLE_ROWS, 125 output_table(crc32table_be, LE_TABLE_ROWS,
@@ -128,7 +128,7 @@ int main(int argc, char** argv)
128 } 128 }
129 if (CRC_LE_BITS > 1) { 129 if (CRC_LE_BITS > 1) {
130 crc32cinit_le(); 130 crc32cinit_le();
131 printf("static u32 __cacheline_aligned " 131 printf("static const u32 ____cacheline_aligned "
132 "crc32ctable_le[%d][%d] = {", 132 "crc32ctable_le[%d][%d] = {",
133 LE_TABLE_ROWS, LE_TABLE_SIZE); 133 LE_TABLE_ROWS, LE_TABLE_SIZE);
134 output_table(crc32ctable_le, LE_TABLE_ROWS, 134 output_table(crc32ctable_le, LE_TABLE_ROWS,
diff --git a/lib/genalloc.c b/lib/genalloc.c
index 0fe1cbe87700..d214866eeea2 100644
--- a/lib/genalloc.c
+++ b/lib/genalloc.c
@@ -586,6 +586,8 @@ struct gen_pool *devm_gen_pool_create(struct device *dev, int min_alloc_order,
586 struct gen_pool **ptr, *pool; 586 struct gen_pool **ptr, *pool;
587 587
588 ptr = devres_alloc(devm_gen_pool_release, sizeof(*ptr), GFP_KERNEL); 588 ptr = devres_alloc(devm_gen_pool_release, sizeof(*ptr), GFP_KERNEL);
589 if (!ptr)
590 return NULL;
589 591
590 pool = gen_pool_create(min_alloc_order, nid); 592 pool = gen_pool_create(min_alloc_order, nid);
591 if (pool) { 593 if (pool) {
diff --git a/lib/seq_buf.c b/lib/seq_buf.c
index 4eedfedb9e31..88c0854bd752 100644
--- a/lib/seq_buf.c
+++ b/lib/seq_buf.c
@@ -91,42 +91,6 @@ int seq_buf_printf(struct seq_buf *s, const char *fmt, ...)
91 return ret; 91 return ret;
92} 92}
93 93
94/**
95 * seq_buf_bitmask - write a bitmask array in its ASCII representation
96 * @s: seq_buf descriptor
97 * @maskp: points to an array of unsigned longs that represent a bitmask
98 * @nmaskbits: The number of bits that are valid in @maskp
99 *
100 * Writes a ASCII representation of a bitmask string into @s.
101 *
102 * Returns zero on success, -1 on overflow.
103 */
104int seq_buf_bitmask(struct seq_buf *s, const unsigned long *maskp,
105 int nmaskbits)
106{
107 unsigned int len = seq_buf_buffer_left(s);
108 int ret;
109
110 WARN_ON(s->size == 0);
111
112 /*
113 * Note, because bitmap_scnprintf() only returns the number of bytes
114 * written and not the number that would be written, we use the last
115 * byte of the buffer to let us know if we overflowed. There's a small
116 * chance that the bitmap could have fit exactly inside the buffer, but
117 * it's not that critical if that does happen.
118 */
119 if (len > 1) {
120 ret = bitmap_scnprintf(s->buffer + s->len, len, maskp, nmaskbits);
121 if (ret < len) {
122 s->len += ret;
123 return 0;
124 }
125 }
126 seq_buf_set_overflow(s);
127 return -1;
128}
129
130#ifdef CONFIG_BINARY_PRINTF 94#ifdef CONFIG_BINARY_PRINTF
131/** 95/**
132 * seq_buf_bprintf - Write the printf string from binary arguments 96 * seq_buf_bprintf - Write the printf string from binary arguments
diff --git a/lib/string.c b/lib/string.c
index 3206d0178296..cdd97f431ae2 100644
--- a/lib/string.c
+++ b/lib/string.c
@@ -313,12 +313,12 @@ EXPORT_SYMBOL(strchrnul);
313 */ 313 */
314char *strrchr(const char *s, int c) 314char *strrchr(const char *s, int c)
315{ 315{
316 const char *p = s + strlen(s); 316 const char *last = NULL;
317 do { 317 do {
318 if (*p == (char)c) 318 if (*s == (char)c)
319 return (char *)p; 319 last = s;
320 } while (--p >= s); 320 } while (*s++);
321 return NULL; 321 return (char *)last;
322} 322}
323EXPORT_SYMBOL(strrchr); 323EXPORT_SYMBOL(strrchr);
324#endif 324#endif
diff --git a/lib/test_kasan.c b/lib/test_kasan.c
new file mode 100644
index 000000000000..098c08eddfab
--- /dev/null
+++ b/lib/test_kasan.c
@@ -0,0 +1,277 @@
1/*
2 *
3 * Copyright (c) 2014 Samsung Electronics Co., Ltd.
4 * Author: Andrey Ryabinin <a.ryabinin@samsung.com>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
9 *
10 */
11
12#define pr_fmt(fmt) "kasan test: %s " fmt, __func__
13
14#include <linux/kernel.h>
15#include <linux/printk.h>
16#include <linux/slab.h>
17#include <linux/string.h>
18#include <linux/module.h>
19
20static noinline void __init kmalloc_oob_right(void)
21{
22 char *ptr;
23 size_t size = 123;
24
25 pr_info("out-of-bounds to right\n");
26 ptr = kmalloc(size, GFP_KERNEL);
27 if (!ptr) {
28 pr_err("Allocation failed\n");
29 return;
30 }
31
32 ptr[size] = 'x';
33 kfree(ptr);
34}
35
36static noinline void __init kmalloc_oob_left(void)
37{
38 char *ptr;
39 size_t size = 15;
40
41 pr_info("out-of-bounds to left\n");
42 ptr = kmalloc(size, GFP_KERNEL);
43 if (!ptr) {
44 pr_err("Allocation failed\n");
45 return;
46 }
47
48 *ptr = *(ptr - 1);
49 kfree(ptr);
50}
51
52static noinline void __init kmalloc_node_oob_right(void)
53{
54 char *ptr;
55 size_t size = 4096;
56
57 pr_info("kmalloc_node(): out-of-bounds to right\n");
58 ptr = kmalloc_node(size, GFP_KERNEL, 0);
59 if (!ptr) {
60 pr_err("Allocation failed\n");
61 return;
62 }
63
64 ptr[size] = 0;
65 kfree(ptr);
66}
67
68static noinline void __init kmalloc_large_oob_rigth(void)
69{
70 char *ptr;
71 size_t size = KMALLOC_MAX_CACHE_SIZE + 10;
72
73 pr_info("kmalloc large allocation: out-of-bounds to right\n");
74 ptr = kmalloc(size, GFP_KERNEL);
75 if (!ptr) {
76 pr_err("Allocation failed\n");
77 return;
78 }
79
80 ptr[size] = 0;
81 kfree(ptr);
82}
83
84static noinline void __init kmalloc_oob_krealloc_more(void)
85{
86 char *ptr1, *ptr2;
87 size_t size1 = 17;
88 size_t size2 = 19;
89
90 pr_info("out-of-bounds after krealloc more\n");
91 ptr1 = kmalloc(size1, GFP_KERNEL);
92 ptr2 = krealloc(ptr1, size2, GFP_KERNEL);
93 if (!ptr1 || !ptr2) {
94 pr_err("Allocation failed\n");
95 kfree(ptr1);
96 return;
97 }
98
99 ptr2[size2] = 'x';
100 kfree(ptr2);
101}
102
103static noinline void __init kmalloc_oob_krealloc_less(void)
104{
105 char *ptr1, *ptr2;
106 size_t size1 = 17;
107 size_t size2 = 15;
108
109 pr_info("out-of-bounds after krealloc less\n");
110 ptr1 = kmalloc(size1, GFP_KERNEL);
111 ptr2 = krealloc(ptr1, size2, GFP_KERNEL);
112 if (!ptr1 || !ptr2) {
113 pr_err("Allocation failed\n");
114 kfree(ptr1);
115 return;
116 }
117 ptr2[size1] = 'x';
118 kfree(ptr2);
119}
120
121static noinline void __init kmalloc_oob_16(void)
122{
123 struct {
124 u64 words[2];
125 } *ptr1, *ptr2;
126
127 pr_info("kmalloc out-of-bounds for 16-bytes access\n");
128 ptr1 = kmalloc(sizeof(*ptr1) - 3, GFP_KERNEL);
129 ptr2 = kmalloc(sizeof(*ptr2), GFP_KERNEL);
130 if (!ptr1 || !ptr2) {
131 pr_err("Allocation failed\n");
132 kfree(ptr1);
133 kfree(ptr2);
134 return;
135 }
136 *ptr1 = *ptr2;
137 kfree(ptr1);
138 kfree(ptr2);
139}
140
141static noinline void __init kmalloc_oob_in_memset(void)
142{
143 char *ptr;
144 size_t size = 666;
145
146 pr_info("out-of-bounds in memset\n");
147 ptr = kmalloc(size, GFP_KERNEL);
148 if (!ptr) {
149 pr_err("Allocation failed\n");
150 return;
151 }
152
153 memset(ptr, 0, size+5);
154 kfree(ptr);
155}
156
157static noinline void __init kmalloc_uaf(void)
158{
159 char *ptr;
160 size_t size = 10;
161
162 pr_info("use-after-free\n");
163 ptr = kmalloc(size, GFP_KERNEL);
164 if (!ptr) {
165 pr_err("Allocation failed\n");
166 return;
167 }
168
169 kfree(ptr);
170 *(ptr + 8) = 'x';
171}
172
173static noinline void __init kmalloc_uaf_memset(void)
174{
175 char *ptr;
176 size_t size = 33;
177
178 pr_info("use-after-free in memset\n");
179 ptr = kmalloc(size, GFP_KERNEL);
180 if (!ptr) {
181 pr_err("Allocation failed\n");
182 return;
183 }
184
185 kfree(ptr);
186 memset(ptr, 0, size);
187}
188
189static noinline void __init kmalloc_uaf2(void)
190{
191 char *ptr1, *ptr2;
192 size_t size = 43;
193
194 pr_info("use-after-free after another kmalloc\n");
195 ptr1 = kmalloc(size, GFP_KERNEL);
196 if (!ptr1) {
197 pr_err("Allocation failed\n");
198 return;
199 }
200
201 kfree(ptr1);
202 ptr2 = kmalloc(size, GFP_KERNEL);
203 if (!ptr2) {
204 pr_err("Allocation failed\n");
205 return;
206 }
207
208 ptr1[40] = 'x';
209 kfree(ptr2);
210}
211
212static noinline void __init kmem_cache_oob(void)
213{
214 char *p;
215 size_t size = 200;
216 struct kmem_cache *cache = kmem_cache_create("test_cache",
217 size, 0,
218 0, NULL);
219 if (!cache) {
220 pr_err("Cache allocation failed\n");
221 return;
222 }
223 pr_info("out-of-bounds in kmem_cache_alloc\n");
224 p = kmem_cache_alloc(cache, GFP_KERNEL);
225 if (!p) {
226 pr_err("Allocation failed\n");
227 kmem_cache_destroy(cache);
228 return;
229 }
230
231 *p = p[size];
232 kmem_cache_free(cache, p);
233 kmem_cache_destroy(cache);
234}
235
236static char global_array[10];
237
238static noinline void __init kasan_global_oob(void)
239{
240 volatile int i = 3;
241 char *p = &global_array[ARRAY_SIZE(global_array) + i];
242
243 pr_info("out-of-bounds global variable\n");
244 *(volatile char *)p;
245}
246
247static noinline void __init kasan_stack_oob(void)
248{
249 char stack_array[10];
250 volatile int i = 0;
251 char *p = &stack_array[ARRAY_SIZE(stack_array) + i];
252
253 pr_info("out-of-bounds on stack\n");
254 *(volatile char *)p;
255}
256
257static int __init kmalloc_tests_init(void)
258{
259 kmalloc_oob_right();
260 kmalloc_oob_left();
261 kmalloc_node_oob_right();
262 kmalloc_large_oob_rigth();
263 kmalloc_oob_krealloc_more();
264 kmalloc_oob_krealloc_less();
265 kmalloc_oob_16();
266 kmalloc_oob_in_memset();
267 kmalloc_uaf();
268 kmalloc_uaf_memset();
269 kmalloc_uaf2();
270 kmem_cache_oob();
271 kasan_stack_oob();
272 kasan_global_oob();
273 return -EAGAIN;
274}
275
276module_init(kmalloc_tests_init);
277MODULE_LICENSE("GPL");
diff --git a/lib/vsprintf.c b/lib/vsprintf.c
index 602d2081e713..b235c96167d3 100644
--- a/lib/vsprintf.c
+++ b/lib/vsprintf.c
@@ -794,6 +794,87 @@ char *hex_string(char *buf, char *end, u8 *addr, struct printf_spec spec,
794} 794}
795 795
796static noinline_for_stack 796static noinline_for_stack
797char *bitmap_string(char *buf, char *end, unsigned long *bitmap,
798 struct printf_spec spec, const char *fmt)
799{
800 const int CHUNKSZ = 32;
801 int nr_bits = max_t(int, spec.field_width, 0);
802 int i, chunksz;
803 bool first = true;
804
805 /* reused to print numbers */
806 spec = (struct printf_spec){ .flags = SMALL | ZEROPAD, .base = 16 };
807
808 chunksz = nr_bits & (CHUNKSZ - 1);
809 if (chunksz == 0)
810 chunksz = CHUNKSZ;
811
812 i = ALIGN(nr_bits, CHUNKSZ) - CHUNKSZ;
813 for (; i >= 0; i -= CHUNKSZ) {
814 u32 chunkmask, val;
815 int word, bit;
816
817 chunkmask = ((1ULL << chunksz) - 1);
818 word = i / BITS_PER_LONG;
819 bit = i % BITS_PER_LONG;
820 val = (bitmap[word] >> bit) & chunkmask;
821
822 if (!first) {
823 if (buf < end)
824 *buf = ',';
825 buf++;
826 }
827 first = false;
828
829 spec.field_width = DIV_ROUND_UP(chunksz, 4);
830 buf = number(buf, end, val, spec);
831
832 chunksz = CHUNKSZ;
833 }
834 return buf;
835}
836
837static noinline_for_stack
838char *bitmap_list_string(char *buf, char *end, unsigned long *bitmap,
839 struct printf_spec spec, const char *fmt)
840{
841 int nr_bits = max_t(int, spec.field_width, 0);
842 /* current bit is 'cur', most recently seen range is [rbot, rtop] */
843 int cur, rbot, rtop;
844 bool first = true;
845
846 /* reused to print numbers */
847 spec = (struct printf_spec){ .base = 10 };
848
849 rbot = cur = find_first_bit(bitmap, nr_bits);
850 while (cur < nr_bits) {
851 rtop = cur;
852 cur = find_next_bit(bitmap, nr_bits, cur + 1);
853 if (cur < nr_bits && cur <= rtop + 1)
854 continue;
855
856 if (!first) {
857 if (buf < end)
858 *buf = ',';
859 buf++;
860 }
861 first = false;
862
863 buf = number(buf, end, rbot, spec);
864 if (rbot < rtop) {
865 if (buf < end)
866 *buf = '-';
867 buf++;
868
869 buf = number(buf, end, rtop, spec);
870 }
871
872 rbot = cur;
873 }
874 return buf;
875}
876
877static noinline_for_stack
797char *mac_address_string(char *buf, char *end, u8 *addr, 878char *mac_address_string(char *buf, char *end, u8 *addr,
798 struct printf_spec spec, const char *fmt) 879 struct printf_spec spec, const char *fmt)
799{ 880{
@@ -1258,6 +1339,10 @@ int kptr_restrict __read_mostly;
1258 * - 'B' For backtraced symbolic direct pointers with offset 1339 * - 'B' For backtraced symbolic direct pointers with offset
1259 * - 'R' For decoded struct resource, e.g., [mem 0x0-0x1f 64bit pref] 1340 * - 'R' For decoded struct resource, e.g., [mem 0x0-0x1f 64bit pref]
1260 * - 'r' For raw struct resource, e.g., [mem 0x0-0x1f flags 0x201] 1341 * - 'r' For raw struct resource, e.g., [mem 0x0-0x1f flags 0x201]
1342 * - 'b[l]' For a bitmap, the number of bits is determined by the field
1343 * width which must be explicitly specified either as part of the
1344 * format string '%32b[l]' or through '%*b[l]', [l] selects
1345 * range-list format instead of hex format
1261 * - 'M' For a 6-byte MAC address, it prints the address in the 1346 * - 'M' For a 6-byte MAC address, it prints the address in the
1262 * usual colon-separated hex notation 1347 * usual colon-separated hex notation
1263 * - 'm' For a 6-byte MAC address, it prints the hex address without colons 1348 * - 'm' For a 6-byte MAC address, it prints the hex address without colons
@@ -1354,6 +1439,13 @@ char *pointer(const char *fmt, char *buf, char *end, void *ptr,
1354 return resource_string(buf, end, ptr, spec, fmt); 1439 return resource_string(buf, end, ptr, spec, fmt);
1355 case 'h': 1440 case 'h':
1356 return hex_string(buf, end, ptr, spec, fmt); 1441 return hex_string(buf, end, ptr, spec, fmt);
1442 case 'b':
1443 switch (fmt[1]) {
1444 case 'l':
1445 return bitmap_list_string(buf, end, ptr, spec, fmt);
1446 default:
1447 return bitmap_string(buf, end, ptr, spec, fmt);
1448 }
1357 case 'M': /* Colon separated: 00:01:02:03:04:05 */ 1449 case 'M': /* Colon separated: 00:01:02:03:04:05 */
1358 case 'm': /* Contiguous: 000102030405 */ 1450 case 'm': /* Contiguous: 000102030405 */
1359 /* [mM]F (FDDI) */ 1451 /* [mM]F (FDDI) */
@@ -1689,6 +1781,8 @@ qualifier:
1689 * %pB output the name of a backtrace symbol with its offset 1781 * %pB output the name of a backtrace symbol with its offset
1690 * %pR output the address range in a struct resource with decoded flags 1782 * %pR output the address range in a struct resource with decoded flags
1691 * %pr output the address range in a struct resource with raw flags 1783 * %pr output the address range in a struct resource with raw flags
1784 * %pb output the bitmap with field width as the number of bits
1785 * %pbl output the bitmap as range list with field width as the number of bits
1692 * %pM output a 6-byte MAC address with colons 1786 * %pM output a 6-byte MAC address with colons
1693 * %pMR output a 6-byte MAC address with colons in reversed order 1787 * %pMR output a 6-byte MAC address with colons in reversed order
1694 * %pMF output a 6-byte MAC address with dashes 1788 * %pMF output a 6-byte MAC address with dashes