aboutsummaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
Diffstat (limited to 'lib')
-rw-r--r--lib/Kconfig.debug48
-rw-r--r--lib/Kconfig.ubsan7
-rw-r--r--lib/Makefile3
-rw-r--r--lib/list_debug.c14
-rw-r--r--lib/lockref.c28
-rw-r--r--lib/radix-tree.c3
-rw-r--r--lib/sha256.c283
-rw-r--r--lib/swiotlb.c2
-rw-r--r--lib/test_bitmap.c10
-rw-r--r--lib/test_kasan.c8
-rw-r--r--lib/test_ubsan.c144
-rw-r--r--lib/vsprintf.c22
12 files changed, 531 insertions, 41 deletions
diff --git a/lib/Kconfig.debug b/lib/Kconfig.debug
index 51c6bf0d93c6..c40c7b734cd1 100644
--- a/lib/Kconfig.debug
+++ b/lib/Kconfig.debug
@@ -800,6 +800,30 @@ config SOFTLOCKUP_DETECTOR
800 chance to run. The current stack trace is displayed upon 800 chance to run. The current stack trace is displayed upon
801 detection and the system will stay locked up. 801 detection and the system will stay locked up.
802 802
803config BOOTPARAM_SOFTLOCKUP_PANIC
804 bool "Panic (Reboot) On Soft Lockups"
805 depends on SOFTLOCKUP_DETECTOR
806 help
807 Say Y here to enable the kernel to panic on "soft lockups",
808 which are bugs that cause the kernel to loop in kernel
809 mode for more than 20 seconds (configurable using the watchdog_thresh
810 sysctl), without giving other tasks a chance to run.
811
812 The panic can be used in combination with panic_timeout,
813 to cause the system to reboot automatically after a
814 lockup has been detected. This feature is useful for
815 high-availability systems that have uptime guarantees and
816 where a lockup must be resolved ASAP.
817
818 Say N if unsure.
819
820config BOOTPARAM_SOFTLOCKUP_PANIC_VALUE
821 int
822 depends on SOFTLOCKUP_DETECTOR
823 range 0 1
824 default 0 if !BOOTPARAM_SOFTLOCKUP_PANIC
825 default 1 if BOOTPARAM_SOFTLOCKUP_PANIC
826
803config HARDLOCKUP_DETECTOR_PERF 827config HARDLOCKUP_DETECTOR_PERF
804 bool 828 bool
805 select SOFTLOCKUP_DETECTOR 829 select SOFTLOCKUP_DETECTOR
@@ -849,30 +873,6 @@ config BOOTPARAM_HARDLOCKUP_PANIC_VALUE
849 default 0 if !BOOTPARAM_HARDLOCKUP_PANIC 873 default 0 if !BOOTPARAM_HARDLOCKUP_PANIC
850 default 1 if BOOTPARAM_HARDLOCKUP_PANIC 874 default 1 if BOOTPARAM_HARDLOCKUP_PANIC
851 875
852config BOOTPARAM_SOFTLOCKUP_PANIC
853 bool "Panic (Reboot) On Soft Lockups"
854 depends on SOFTLOCKUP_DETECTOR
855 help
856 Say Y here to enable the kernel to panic on "soft lockups",
857 which are bugs that cause the kernel to loop in kernel
858 mode for more than 20 seconds (configurable using the watchdog_thresh
859 sysctl), without giving other tasks a chance to run.
860
861 The panic can be used in combination with panic_timeout,
862 to cause the system to reboot automatically after a
863 lockup has been detected. This feature is useful for
864 high-availability systems that have uptime guarantees and
865 where a lockup must be resolved ASAP.
866
867 Say N if unsure.
868
869config BOOTPARAM_SOFTLOCKUP_PANIC_VALUE
870 int
871 depends on SOFTLOCKUP_DETECTOR
872 range 0 1
873 default 0 if !BOOTPARAM_SOFTLOCKUP_PANIC
874 default 1 if BOOTPARAM_SOFTLOCKUP_PANIC
875
876config DETECT_HUNG_TASK 876config DETECT_HUNG_TASK
877 bool "Detect Hung Tasks" 877 bool "Detect Hung Tasks"
878 depends on DEBUG_KERNEL 878 depends on DEBUG_KERNEL
diff --git a/lib/Kconfig.ubsan b/lib/Kconfig.ubsan
index a669c193b878..19d42ea75ec2 100644
--- a/lib/Kconfig.ubsan
+++ b/lib/Kconfig.ubsan
@@ -46,3 +46,10 @@ config UBSAN_NULL
46 help 46 help
47 This option enables detection of memory accesses via a 47 This option enables detection of memory accesses via a
48 null pointer. 48 null pointer.
49
50config TEST_UBSAN
51 tristate "Module for testing for undefined behavior detection"
52 depends on m && UBSAN
53 help
54 This is a test module for UBSAN.
55 It triggers various undefined behavior, and detect it.
diff --git a/lib/Makefile b/lib/Makefile
index 8fc0d3a9b34f..ce20696d5a92 100644
--- a/lib/Makefile
+++ b/lib/Makefile
@@ -53,6 +53,9 @@ obj-$(CONFIG_TEST_FIRMWARE) += test_firmware.o
53obj-$(CONFIG_TEST_SYSCTL) += test_sysctl.o 53obj-$(CONFIG_TEST_SYSCTL) += test_sysctl.o
54obj-$(CONFIG_TEST_HASH) += test_hash.o test_siphash.o 54obj-$(CONFIG_TEST_HASH) += test_hash.o test_siphash.o
55obj-$(CONFIG_TEST_KASAN) += test_kasan.o 55obj-$(CONFIG_TEST_KASAN) += test_kasan.o
56CFLAGS_test_kasan.o += -fno-builtin
57obj-$(CONFIG_TEST_UBSAN) += test_ubsan.o
58UBSAN_SANITIZE_test_ubsan.o := y
56obj-$(CONFIG_TEST_KSTRTOX) += test-kstrtox.o 59obj-$(CONFIG_TEST_KSTRTOX) += test-kstrtox.o
57obj-$(CONFIG_TEST_LIST_SORT) += test_list_sort.o 60obj-$(CONFIG_TEST_LIST_SORT) += test_list_sort.o
58obj-$(CONFIG_TEST_LKM) += test_module.o 61obj-$(CONFIG_TEST_LKM) += test_module.o
diff --git a/lib/list_debug.c b/lib/list_debug.c
index a34db8d27667..5d5424b51b74 100644
--- a/lib/list_debug.c
+++ b/lib/list_debug.c
@@ -21,13 +21,13 @@ bool __list_add_valid(struct list_head *new, struct list_head *prev,
21 struct list_head *next) 21 struct list_head *next)
22{ 22{
23 if (CHECK_DATA_CORRUPTION(next->prev != prev, 23 if (CHECK_DATA_CORRUPTION(next->prev != prev,
24 "list_add corruption. next->prev should be prev (%p), but was %p. (next=%p).\n", 24 "list_add corruption. next->prev should be prev (%px), but was %px. (next=%px).\n",
25 prev, next->prev, next) || 25 prev, next->prev, next) ||
26 CHECK_DATA_CORRUPTION(prev->next != next, 26 CHECK_DATA_CORRUPTION(prev->next != next,
27 "list_add corruption. prev->next should be next (%p), but was %p. (prev=%p).\n", 27 "list_add corruption. prev->next should be next (%px), but was %px. (prev=%px).\n",
28 next, prev->next, prev) || 28 next, prev->next, prev) ||
29 CHECK_DATA_CORRUPTION(new == prev || new == next, 29 CHECK_DATA_CORRUPTION(new == prev || new == next,
30 "list_add double add: new=%p, prev=%p, next=%p.\n", 30 "list_add double add: new=%px, prev=%px, next=%px.\n",
31 new, prev, next)) 31 new, prev, next))
32 return false; 32 return false;
33 33
@@ -43,16 +43,16 @@ bool __list_del_entry_valid(struct list_head *entry)
43 next = entry->next; 43 next = entry->next;
44 44
45 if (CHECK_DATA_CORRUPTION(next == LIST_POISON1, 45 if (CHECK_DATA_CORRUPTION(next == LIST_POISON1,
46 "list_del corruption, %p->next is LIST_POISON1 (%p)\n", 46 "list_del corruption, %px->next is LIST_POISON1 (%px)\n",
47 entry, LIST_POISON1) || 47 entry, LIST_POISON1) ||
48 CHECK_DATA_CORRUPTION(prev == LIST_POISON2, 48 CHECK_DATA_CORRUPTION(prev == LIST_POISON2,
49 "list_del corruption, %p->prev is LIST_POISON2 (%p)\n", 49 "list_del corruption, %px->prev is LIST_POISON2 (%px)\n",
50 entry, LIST_POISON2) || 50 entry, LIST_POISON2) ||
51 CHECK_DATA_CORRUPTION(prev->next != entry, 51 CHECK_DATA_CORRUPTION(prev->next != entry,
52 "list_del corruption. prev->next should be %p, but was %p\n", 52 "list_del corruption. prev->next should be %px, but was %px\n",
53 entry, prev->next) || 53 entry, prev->next) ||
54 CHECK_DATA_CORRUPTION(next->prev != entry, 54 CHECK_DATA_CORRUPTION(next->prev != entry,
55 "list_del corruption. next->prev should be %p, but was %p\n", 55 "list_del corruption. next->prev should be %px, but was %px\n",
56 entry, next->prev)) 56 entry, next->prev))
57 return false; 57 return false;
58 58
diff --git a/lib/lockref.c b/lib/lockref.c
index 47169ed7e964..3d468b53d4c9 100644
--- a/lib/lockref.c
+++ b/lib/lockref.c
@@ -81,6 +81,34 @@ int lockref_get_not_zero(struct lockref *lockref)
81EXPORT_SYMBOL(lockref_get_not_zero); 81EXPORT_SYMBOL(lockref_get_not_zero);
82 82
83/** 83/**
84 * lockref_put_not_zero - Decrements count unless count <= 1 before decrement
85 * @lockref: pointer to lockref structure
86 * Return: 1 if count updated successfully or 0 if count would become zero
87 */
88int lockref_put_not_zero(struct lockref *lockref)
89{
90 int retval;
91
92 CMPXCHG_LOOP(
93 new.count--;
94 if (old.count <= 1)
95 return 0;
96 ,
97 return 1;
98 );
99
100 spin_lock(&lockref->lock);
101 retval = 0;
102 if (lockref->count > 1) {
103 lockref->count--;
104 retval = 1;
105 }
106 spin_unlock(&lockref->lock);
107 return retval;
108}
109EXPORT_SYMBOL(lockref_put_not_zero);
110
111/**
84 * lockref_get_or_lock - Increments count unless the count is 0 or dead 112 * lockref_get_or_lock - Increments count unless the count is 0 or dead
85 * @lockref: pointer to lockref structure 113 * @lockref: pointer to lockref structure
86 * Return: 1 if count updated successfully or 0 if count was zero 114 * Return: 1 if count updated successfully or 0 if count was zero
diff --git a/lib/radix-tree.c b/lib/radix-tree.c
index 8e00138d593f..da9e10c827df 100644
--- a/lib/radix-tree.c
+++ b/lib/radix-tree.c
@@ -146,7 +146,7 @@ static unsigned int radix_tree_descend(const struct radix_tree_node *parent,
146 146
147static inline gfp_t root_gfp_mask(const struct radix_tree_root *root) 147static inline gfp_t root_gfp_mask(const struct radix_tree_root *root)
148{ 148{
149 return root->gfp_mask & __GFP_BITS_MASK; 149 return root->gfp_mask & (__GFP_BITS_MASK & ~GFP_ZONEMASK);
150} 150}
151 151
152static inline void tag_set(struct radix_tree_node *node, unsigned int tag, 152static inline void tag_set(struct radix_tree_node *node, unsigned int tag,
@@ -2285,6 +2285,7 @@ void __init radix_tree_init(void)
2285 int ret; 2285 int ret;
2286 2286
2287 BUILD_BUG_ON(RADIX_TREE_MAX_TAGS + __GFP_BITS_SHIFT > 32); 2287 BUILD_BUG_ON(RADIX_TREE_MAX_TAGS + __GFP_BITS_SHIFT > 32);
2288 BUILD_BUG_ON(ROOT_IS_IDR & ~GFP_ZONEMASK);
2288 radix_tree_node_cachep = kmem_cache_create("radix_tree_node", 2289 radix_tree_node_cachep = kmem_cache_create("radix_tree_node",
2289 sizeof(struct radix_tree_node), 0, 2290 sizeof(struct radix_tree_node), 0,
2290 SLAB_PANIC | SLAB_RECLAIM_ACCOUNT, 2291 SLAB_PANIC | SLAB_RECLAIM_ACCOUNT,
diff --git a/lib/sha256.c b/lib/sha256.c
new file mode 100644
index 000000000000..4400c832e2aa
--- /dev/null
+++ b/lib/sha256.c
@@ -0,0 +1,283 @@
1/*
2 * SHA-256, as specified in
3 * http://csrc.nist.gov/groups/STM/cavp/documents/shs/sha256-384-512.pdf
4 *
5 * SHA-256 code by Jean-Luc Cooke <jlcooke@certainkey.com>.
6 *
7 * Copyright (c) Jean-Luc Cooke <jlcooke@certainkey.com>
8 * Copyright (c) Andrew McDonald <andrew@mcdonald.org.uk>
9 * Copyright (c) 2002 James Morris <jmorris@intercode.com.au>
10 * Copyright (c) 2014 Red Hat Inc.
11 *
12 * This program is free software; you can redistribute it and/or modify it
13 * under the terms of the GNU General Public License as published by the Free
14 * Software Foundation; either version 2 of the License, or (at your option)
15 * any later version.
16 */
17
18#include <linux/bitops.h>
19#include <linux/sha256.h>
20#include <linux/string.h>
21#include <asm/byteorder.h>
22
23static inline u32 Ch(u32 x, u32 y, u32 z)
24{
25 return z ^ (x & (y ^ z));
26}
27
28static inline u32 Maj(u32 x, u32 y, u32 z)
29{
30 return (x & y) | (z & (x | y));
31}
32
33#define e0(x) (ror32(x, 2) ^ ror32(x, 13) ^ ror32(x, 22))
34#define e1(x) (ror32(x, 6) ^ ror32(x, 11) ^ ror32(x, 25))
35#define s0(x) (ror32(x, 7) ^ ror32(x, 18) ^ (x >> 3))
36#define s1(x) (ror32(x, 17) ^ ror32(x, 19) ^ (x >> 10))
37
38static inline void LOAD_OP(int I, u32 *W, const u8 *input)
39{
40 W[I] = __be32_to_cpu(((__be32 *)(input))[I]);
41}
42
43static inline void BLEND_OP(int I, u32 *W)
44{
45 W[I] = s1(W[I-2]) + W[I-7] + s0(W[I-15]) + W[I-16];
46}
47
48static void sha256_transform(u32 *state, const u8 *input)
49{
50 u32 a, b, c, d, e, f, g, h, t1, t2;
51 u32 W[64];
52 int i;
53
54 /* load the input */
55 for (i = 0; i < 16; i++)
56 LOAD_OP(i, W, input);
57
58 /* now blend */
59 for (i = 16; i < 64; i++)
60 BLEND_OP(i, W);
61
62 /* load the state into our registers */
63 a = state[0]; b = state[1]; c = state[2]; d = state[3];
64 e = state[4]; f = state[5]; g = state[6]; h = state[7];
65
66 /* now iterate */
67 t1 = h + e1(e) + Ch(e, f, g) + 0x428a2f98 + W[0];
68 t2 = e0(a) + Maj(a, b, c); d += t1; h = t1 + t2;
69 t1 = g + e1(d) + Ch(d, e, f) + 0x71374491 + W[1];
70 t2 = e0(h) + Maj(h, a, b); c += t1; g = t1 + t2;
71 t1 = f + e1(c) + Ch(c, d, e) + 0xb5c0fbcf + W[2];
72 t2 = e0(g) + Maj(g, h, a); b += t1; f = t1 + t2;
73 t1 = e + e1(b) + Ch(b, c, d) + 0xe9b5dba5 + W[3];
74 t2 = e0(f) + Maj(f, g, h); a += t1; e = t1 + t2;
75 t1 = d + e1(a) + Ch(a, b, c) + 0x3956c25b + W[4];
76 t2 = e0(e) + Maj(e, f, g); h += t1; d = t1 + t2;
77 t1 = c + e1(h) + Ch(h, a, b) + 0x59f111f1 + W[5];
78 t2 = e0(d) + Maj(d, e, f); g += t1; c = t1 + t2;
79 t1 = b + e1(g) + Ch(g, h, a) + 0x923f82a4 + W[6];
80 t2 = e0(c) + Maj(c, d, e); f += t1; b = t1 + t2;
81 t1 = a + e1(f) + Ch(f, g, h) + 0xab1c5ed5 + W[7];
82 t2 = e0(b) + Maj(b, c, d); e += t1; a = t1 + t2;
83
84 t1 = h + e1(e) + Ch(e, f, g) + 0xd807aa98 + W[8];
85 t2 = e0(a) + Maj(a, b, c); d += t1; h = t1 + t2;
86 t1 = g + e1(d) + Ch(d, e, f) + 0x12835b01 + W[9];
87 t2 = e0(h) + Maj(h, a, b); c += t1; g = t1 + t2;
88 t1 = f + e1(c) + Ch(c, d, e) + 0x243185be + W[10];
89 t2 = e0(g) + Maj(g, h, a); b += t1; f = t1 + t2;
90 t1 = e + e1(b) + Ch(b, c, d) + 0x550c7dc3 + W[11];
91 t2 = e0(f) + Maj(f, g, h); a += t1; e = t1 + t2;
92 t1 = d + e1(a) + Ch(a, b, c) + 0x72be5d74 + W[12];
93 t2 = e0(e) + Maj(e, f, g); h += t1; d = t1 + t2;
94 t1 = c + e1(h) + Ch(h, a, b) + 0x80deb1fe + W[13];
95 t2 = e0(d) + Maj(d, e, f); g += t1; c = t1 + t2;
96 t1 = b + e1(g) + Ch(g, h, a) + 0x9bdc06a7 + W[14];
97 t2 = e0(c) + Maj(c, d, e); f += t1; b = t1 + t2;
98 t1 = a + e1(f) + Ch(f, g, h) + 0xc19bf174 + W[15];
99 t2 = e0(b) + Maj(b, c, d); e += t1; a = t1+t2;
100
101 t1 = h + e1(e) + Ch(e, f, g) + 0xe49b69c1 + W[16];
102 t2 = e0(a) + Maj(a, b, c); d += t1; h = t1+t2;
103 t1 = g + e1(d) + Ch(d, e, f) + 0xefbe4786 + W[17];
104 t2 = e0(h) + Maj(h, a, b); c += t1; g = t1+t2;
105 t1 = f + e1(c) + Ch(c, d, e) + 0x0fc19dc6 + W[18];
106 t2 = e0(g) + Maj(g, h, a); b += t1; f = t1+t2;
107 t1 = e + e1(b) + Ch(b, c, d) + 0x240ca1cc + W[19];
108 t2 = e0(f) + Maj(f, g, h); a += t1; e = t1+t2;
109 t1 = d + e1(a) + Ch(a, b, c) + 0x2de92c6f + W[20];
110 t2 = e0(e) + Maj(e, f, g); h += t1; d = t1+t2;
111 t1 = c + e1(h) + Ch(h, a, b) + 0x4a7484aa + W[21];
112 t2 = e0(d) + Maj(d, e, f); g += t1; c = t1+t2;
113 t1 = b + e1(g) + Ch(g, h, a) + 0x5cb0a9dc + W[22];
114 t2 = e0(c) + Maj(c, d, e); f += t1; b = t1+t2;
115 t1 = a + e1(f) + Ch(f, g, h) + 0x76f988da + W[23];
116 t2 = e0(b) + Maj(b, c, d); e += t1; a = t1+t2;
117
118 t1 = h + e1(e) + Ch(e, f, g) + 0x983e5152 + W[24];
119 t2 = e0(a) + Maj(a, b, c); d += t1; h = t1+t2;
120 t1 = g + e1(d) + Ch(d, e, f) + 0xa831c66d + W[25];
121 t2 = e0(h) + Maj(h, a, b); c += t1; g = t1+t2;
122 t1 = f + e1(c) + Ch(c, d, e) + 0xb00327c8 + W[26];
123 t2 = e0(g) + Maj(g, h, a); b += t1; f = t1+t2;
124 t1 = e + e1(b) + Ch(b, c, d) + 0xbf597fc7 + W[27];
125 t2 = e0(f) + Maj(f, g, h); a += t1; e = t1+t2;
126 t1 = d + e1(a) + Ch(a, b, c) + 0xc6e00bf3 + W[28];
127 t2 = e0(e) + Maj(e, f, g); h += t1; d = t1+t2;
128 t1 = c + e1(h) + Ch(h, a, b) + 0xd5a79147 + W[29];
129 t2 = e0(d) + Maj(d, e, f); g += t1; c = t1+t2;
130 t1 = b + e1(g) + Ch(g, h, a) + 0x06ca6351 + W[30];
131 t2 = e0(c) + Maj(c, d, e); f += t1; b = t1+t2;
132 t1 = a + e1(f) + Ch(f, g, h) + 0x14292967 + W[31];
133 t2 = e0(b) + Maj(b, c, d); e += t1; a = t1+t2;
134
135 t1 = h + e1(e) + Ch(e, f, g) + 0x27b70a85 + W[32];
136 t2 = e0(a) + Maj(a, b, c); d += t1; h = t1+t2;
137 t1 = g + e1(d) + Ch(d, e, f) + 0x2e1b2138 + W[33];
138 t2 = e0(h) + Maj(h, a, b); c += t1; g = t1+t2;
139 t1 = f + e1(c) + Ch(c, d, e) + 0x4d2c6dfc + W[34];
140 t2 = e0(g) + Maj(g, h, a); b += t1; f = t1+t2;
141 t1 = e + e1(b) + Ch(b, c, d) + 0x53380d13 + W[35];
142 t2 = e0(f) + Maj(f, g, h); a += t1; e = t1+t2;
143 t1 = d + e1(a) + Ch(a, b, c) + 0x650a7354 + W[36];
144 t2 = e0(e) + Maj(e, f, g); h += t1; d = t1+t2;
145 t1 = c + e1(h) + Ch(h, a, b) + 0x766a0abb + W[37];
146 t2 = e0(d) + Maj(d, e, f); g += t1; c = t1+t2;
147 t1 = b + e1(g) + Ch(g, h, a) + 0x81c2c92e + W[38];
148 t2 = e0(c) + Maj(c, d, e); f += t1; b = t1+t2;
149 t1 = a + e1(f) + Ch(f, g, h) + 0x92722c85 + W[39];
150 t2 = e0(b) + Maj(b, c, d); e += t1; a = t1+t2;
151
152 t1 = h + e1(e) + Ch(e, f, g) + 0xa2bfe8a1 + W[40];
153 t2 = e0(a) + Maj(a, b, c); d += t1; h = t1+t2;
154 t1 = g + e1(d) + Ch(d, e, f) + 0xa81a664b + W[41];
155 t2 = e0(h) + Maj(h, a, b); c += t1; g = t1+t2;
156 t1 = f + e1(c) + Ch(c, d, e) + 0xc24b8b70 + W[42];
157 t2 = e0(g) + Maj(g, h, a); b += t1; f = t1+t2;
158 t1 = e + e1(b) + Ch(b, c, d) + 0xc76c51a3 + W[43];
159 t2 = e0(f) + Maj(f, g, h); a += t1; e = t1+t2;
160 t1 = d + e1(a) + Ch(a, b, c) + 0xd192e819 + W[44];
161 t2 = e0(e) + Maj(e, f, g); h += t1; d = t1+t2;
162 t1 = c + e1(h) + Ch(h, a, b) + 0xd6990624 + W[45];
163 t2 = e0(d) + Maj(d, e, f); g += t1; c = t1+t2;
164 t1 = b + e1(g) + Ch(g, h, a) + 0xf40e3585 + W[46];
165 t2 = e0(c) + Maj(c, d, e); f += t1; b = t1+t2;
166 t1 = a + e1(f) + Ch(f, g, h) + 0x106aa070 + W[47];
167 t2 = e0(b) + Maj(b, c, d); e += t1; a = t1+t2;
168
169 t1 = h + e1(e) + Ch(e, f, g) + 0x19a4c116 + W[48];
170 t2 = e0(a) + Maj(a, b, c); d += t1; h = t1+t2;
171 t1 = g + e1(d) + Ch(d, e, f) + 0x1e376c08 + W[49];
172 t2 = e0(h) + Maj(h, a, b); c += t1; g = t1+t2;
173 t1 = f + e1(c) + Ch(c, d, e) + 0x2748774c + W[50];
174 t2 = e0(g) + Maj(g, h, a); b += t1; f = t1+t2;
175 t1 = e + e1(b) + Ch(b, c, d) + 0x34b0bcb5 + W[51];
176 t2 = e0(f) + Maj(f, g, h); a += t1; e = t1+t2;
177 t1 = d + e1(a) + Ch(a, b, c) + 0x391c0cb3 + W[52];
178 t2 = e0(e) + Maj(e, f, g); h += t1; d = t1+t2;
179 t1 = c + e1(h) + Ch(h, a, b) + 0x4ed8aa4a + W[53];
180 t2 = e0(d) + Maj(d, e, f); g += t1; c = t1+t2;
181 t1 = b + e1(g) + Ch(g, h, a) + 0x5b9cca4f + W[54];
182 t2 = e0(c) + Maj(c, d, e); f += t1; b = t1+t2;
183 t1 = a + e1(f) + Ch(f, g, h) + 0x682e6ff3 + W[55];
184 t2 = e0(b) + Maj(b, c, d); e += t1; a = t1+t2;
185
186 t1 = h + e1(e) + Ch(e, f, g) + 0x748f82ee + W[56];
187 t2 = e0(a) + Maj(a, b, c); d += t1; h = t1+t2;
188 t1 = g + e1(d) + Ch(d, e, f) + 0x78a5636f + W[57];
189 t2 = e0(h) + Maj(h, a, b); c += t1; g = t1+t2;
190 t1 = f + e1(c) + Ch(c, d, e) + 0x84c87814 + W[58];
191 t2 = e0(g) + Maj(g, h, a); b += t1; f = t1+t2;
192 t1 = e + e1(b) + Ch(b, c, d) + 0x8cc70208 + W[59];
193 t2 = e0(f) + Maj(f, g, h); a += t1; e = t1+t2;
194 t1 = d + e1(a) + Ch(a, b, c) + 0x90befffa + W[60];
195 t2 = e0(e) + Maj(e, f, g); h += t1; d = t1+t2;
196 t1 = c + e1(h) + Ch(h, a, b) + 0xa4506ceb + W[61];
197 t2 = e0(d) + Maj(d, e, f); g += t1; c = t1+t2;
198 t1 = b + e1(g) + Ch(g, h, a) + 0xbef9a3f7 + W[62];
199 t2 = e0(c) + Maj(c, d, e); f += t1; b = t1+t2;
200 t1 = a + e1(f) + Ch(f, g, h) + 0xc67178f2 + W[63];
201 t2 = e0(b) + Maj(b, c, d); e += t1; a = t1+t2;
202
203 state[0] += a; state[1] += b; state[2] += c; state[3] += d;
204 state[4] += e; state[5] += f; state[6] += g; state[7] += h;
205
206 /* clear any sensitive info... */
207 a = b = c = d = e = f = g = h = t1 = t2 = 0;
208 memset(W, 0, 64 * sizeof(u32));
209}
210
211int sha256_init(struct sha256_state *sctx)
212{
213 sctx->state[0] = SHA256_H0;
214 sctx->state[1] = SHA256_H1;
215 sctx->state[2] = SHA256_H2;
216 sctx->state[3] = SHA256_H3;
217 sctx->state[4] = SHA256_H4;
218 sctx->state[5] = SHA256_H5;
219 sctx->state[6] = SHA256_H6;
220 sctx->state[7] = SHA256_H7;
221 sctx->count = 0;
222
223 return 0;
224}
225
226int sha256_update(struct sha256_state *sctx, const u8 *data, unsigned int len)
227{
228 unsigned int partial, done;
229 const u8 *src;
230
231 partial = sctx->count & 0x3f;
232 sctx->count += len;
233 done = 0;
234 src = data;
235
236 if ((partial + len) > 63) {
237 if (partial) {
238 done = -partial;
239 memcpy(sctx->buf + partial, data, done + 64);
240 src = sctx->buf;
241 }
242
243 do {
244 sha256_transform(sctx->state, src);
245 done += 64;
246 src = data + done;
247 } while (done + 63 < len);
248
249 partial = 0;
250 }
251 memcpy(sctx->buf + partial, src, len - done);
252
253 return 0;
254}
255
256int sha256_final(struct sha256_state *sctx, u8 *out)
257{
258 __be32 *dst = (__be32 *)out;
259 __be64 bits;
260 unsigned int index, pad_len;
261 int i;
262 static const u8 padding[64] = { 0x80, };
263
264 /* Save number of bits */
265 bits = cpu_to_be64(sctx->count << 3);
266
267 /* Pad out to 56 mod 64. */
268 index = sctx->count & 0x3f;
269 pad_len = (index < 56) ? (56 - index) : ((64+56) - index);
270 sha256_update(sctx, padding, pad_len);
271
272 /* Append length (before padding) */
273 sha256_update(sctx, (const u8 *)&bits, sizeof(bits));
274
275 /* Store state in digest */
276 for (i = 0; i < 8; i++)
277 dst[i] = cpu_to_be32(sctx->state[i]);
278
279 /* Zeroize sensitive information. */
280 memset(sctx, 0, sizeof(*sctx));
281
282 return 0;
283}
diff --git a/lib/swiotlb.c b/lib/swiotlb.c
index 47aeb04c1997..de7cc540450f 100644
--- a/lib/swiotlb.c
+++ b/lib/swiotlb.c
@@ -719,7 +719,7 @@ swiotlb_alloc_buffer(struct device *dev, size_t size, dma_addr_t *dma_handle,
719 goto out_warn; 719 goto out_warn;
720 720
721 *dma_handle = __phys_to_dma(dev, phys_addr); 721 *dma_handle = __phys_to_dma(dev, phys_addr);
722 if (dma_coherent_ok(dev, *dma_handle, size)) 722 if (!dma_coherent_ok(dev, *dma_handle, size))
723 goto out_unmap; 723 goto out_unmap;
724 724
725 memset(phys_to_virt(phys_addr), 0, size); 725 memset(phys_to_virt(phys_addr), 0, size);
diff --git a/lib/test_bitmap.c b/lib/test_bitmap.c
index 413367cf569e..de16f7869fb1 100644
--- a/lib/test_bitmap.c
+++ b/lib/test_bitmap.c
@@ -296,15 +296,17 @@ static void __init test_bitmap_parselist(void)
296 } 296 }
297} 297}
298 298
299#define EXP_BYTES (sizeof(exp) * 8)
300
299static void __init test_bitmap_arr32(void) 301static void __init test_bitmap_arr32(void)
300{ 302{
301 unsigned int nbits, next_bit, len = sizeof(exp) * 8; 303 unsigned int nbits, next_bit;
302 u32 arr[sizeof(exp) / 4]; 304 u32 arr[sizeof(exp) / 4];
303 DECLARE_BITMAP(bmap2, len); 305 DECLARE_BITMAP(bmap2, EXP_BYTES);
304 306
305 memset(arr, 0xa5, sizeof(arr)); 307 memset(arr, 0xa5, sizeof(arr));
306 308
307 for (nbits = 0; nbits < len; ++nbits) { 309 for (nbits = 0; nbits < EXP_BYTES; ++nbits) {
308 bitmap_to_arr32(arr, exp, nbits); 310 bitmap_to_arr32(arr, exp, nbits);
309 bitmap_from_arr32(bmap2, arr, nbits); 311 bitmap_from_arr32(bmap2, arr, nbits);
310 expect_eq_bitmap(bmap2, exp, nbits); 312 expect_eq_bitmap(bmap2, exp, nbits);
@@ -316,7 +318,7 @@ static void __init test_bitmap_arr32(void)
316 " tail is not safely cleared: %d\n", 318 " tail is not safely cleared: %d\n",
317 nbits, next_bit); 319 nbits, next_bit);
318 320
319 if (nbits < len - 32) 321 if (nbits < EXP_BYTES - 32)
320 expect_eq_uint(arr[DIV_ROUND_UP(nbits, 32)], 322 expect_eq_uint(arr[DIV_ROUND_UP(nbits, 32)],
321 0xa5a5a5a5); 323 0xa5a5a5a5);
322 } 324 }
diff --git a/lib/test_kasan.c b/lib/test_kasan.c
index 98854a64b014..ec657105edbf 100644
--- a/lib/test_kasan.c
+++ b/lib/test_kasan.c
@@ -567,7 +567,15 @@ static noinline void __init kmem_cache_invalid_free(void)
567 return; 567 return;
568 } 568 }
569 569
570 /* Trigger invalid free, the object doesn't get freed */
570 kmem_cache_free(cache, p + 1); 571 kmem_cache_free(cache, p + 1);
572
573 /*
574 * Properly free the object to prevent the "Objects remaining in
575 * test_cache on __kmem_cache_shutdown" BUG failure.
576 */
577 kmem_cache_free(cache, p);
578
571 kmem_cache_destroy(cache); 579 kmem_cache_destroy(cache);
572} 580}
573 581
diff --git a/lib/test_ubsan.c b/lib/test_ubsan.c
new file mode 100644
index 000000000000..280f4979d00e
--- /dev/null
+++ b/lib/test_ubsan.c
@@ -0,0 +1,144 @@
1// SPDX-License-Identifier: GPL-2.0
2#include <linux/init.h>
3#include <linux/kernel.h>
4#include <linux/module.h>
5
6typedef void(*test_ubsan_fp)(void);
7
8static void test_ubsan_add_overflow(void)
9{
10 volatile int val = INT_MAX;
11
12 val += 2;
13}
14
15static void test_ubsan_sub_overflow(void)
16{
17 volatile int val = INT_MIN;
18 volatile int val2 = 2;
19
20 val -= val2;
21}
22
23static void test_ubsan_mul_overflow(void)
24{
25 volatile int val = INT_MAX / 2;
26
27 val *= 3;
28}
29
30static void test_ubsan_negate_overflow(void)
31{
32 volatile int val = INT_MIN;
33
34 val = -val;
35}
36
37static void test_ubsan_divrem_overflow(void)
38{
39 volatile int val = 16;
40 volatile int val2 = 0;
41
42 val /= val2;
43}
44
45static void test_ubsan_vla_bound_not_positive(void)
46{
47 volatile int size = -1;
48 char buf[size];
49
50 (void)buf;
51}
52
53static void test_ubsan_shift_out_of_bounds(void)
54{
55 volatile int val = -1;
56 int val2 = 10;
57
58 val2 <<= val;
59}
60
61static void test_ubsan_out_of_bounds(void)
62{
63 volatile int i = 4, j = 5;
64 volatile int arr[i];
65
66 arr[j] = i;
67}
68
69static void test_ubsan_load_invalid_value(void)
70{
71 volatile char *dst, *src;
72 bool val, val2, *ptr;
73 char c = 4;
74
75 dst = (char *)&val;
76 src = &c;
77 *dst = *src;
78
79 ptr = &val2;
80 val2 = val;
81}
82
83static void test_ubsan_null_ptr_deref(void)
84{
85 volatile int *ptr = NULL;
86 int val;
87
88 val = *ptr;
89}
90
91static void test_ubsan_misaligned_access(void)
92{
93 volatile char arr[5] __aligned(4) = {1, 2, 3, 4, 5};
94 volatile int *ptr, val = 6;
95
96 ptr = (int *)(arr + 1);
97 *ptr = val;
98}
99
100static void test_ubsan_object_size_mismatch(void)
101{
102 /* "((aligned(8)))" helps this not into be misaligned for ptr-access. */
103 volatile int val __aligned(8) = 4;
104 volatile long long *ptr, val2;
105
106 ptr = (long long *)&val;
107 val2 = *ptr;
108}
109
110static const test_ubsan_fp test_ubsan_array[] = {
111 test_ubsan_add_overflow,
112 test_ubsan_sub_overflow,
113 test_ubsan_mul_overflow,
114 test_ubsan_negate_overflow,
115 test_ubsan_divrem_overflow,
116 test_ubsan_vla_bound_not_positive,
117 test_ubsan_shift_out_of_bounds,
118 test_ubsan_out_of_bounds,
119 test_ubsan_load_invalid_value,
120 //test_ubsan_null_ptr_deref, /* exclude it because there is a crash */
121 test_ubsan_misaligned_access,
122 test_ubsan_object_size_mismatch,
123};
124
125static int __init test_ubsan_init(void)
126{
127 unsigned int i;
128
129 for (i = 0; i < ARRAY_SIZE(test_ubsan_array); i++)
130 test_ubsan_array[i]();
131
132 (void)test_ubsan_null_ptr_deref; /* to avoid unsed-function warning */
133 return 0;
134}
135module_init(test_ubsan_init);
136
137static void __exit test_ubsan_exit(void)
138{
139 /* do nothing */
140}
141module_exit(test_ubsan_exit);
142
143MODULE_AUTHOR("Jinbum Park <jinb.park7@gmail.com>");
144MODULE_LICENSE("GPL v2");
diff --git a/lib/vsprintf.c b/lib/vsprintf.c
index d7a708f82559..30c0cb8cc9bc 100644
--- a/lib/vsprintf.c
+++ b/lib/vsprintf.c
@@ -336,7 +336,7 @@ char *put_dec(char *buf, unsigned long long n)
336 * 336 *
337 * If speed is not important, use snprintf(). It's easy to read the code. 337 * If speed is not important, use snprintf(). It's easy to read the code.
338 */ 338 */
339int num_to_str(char *buf, int size, unsigned long long num) 339int num_to_str(char *buf, int size, unsigned long long num, unsigned int width)
340{ 340{
341 /* put_dec requires 2-byte alignment of the buffer. */ 341 /* put_dec requires 2-byte alignment of the buffer. */
342 char tmp[sizeof(num) * 3] __aligned(2); 342 char tmp[sizeof(num) * 3] __aligned(2);
@@ -350,11 +350,21 @@ int num_to_str(char *buf, int size, unsigned long long num)
350 len = put_dec(tmp, num) - tmp; 350 len = put_dec(tmp, num) - tmp;
351 } 351 }
352 352
353 if (len > size) 353 if (len > size || width > size)
354 return 0; 354 return 0;
355
356 if (width > len) {
357 width = width - len;
358 for (idx = 0; idx < width; idx++)
359 buf[idx] = ' ';
360 } else {
361 width = 0;
362 }
363
355 for (idx = 0; idx < len; ++idx) 364 for (idx = 0; idx < len; ++idx)
356 buf[idx] = tmp[len - idx - 1]; 365 buf[idx + width] = tmp[len - idx - 1];
357 return len; 366
367 return len + width;
358} 368}
359 369
360#define SIGN 1 /* unsigned/signed, must be 1 */ 370#define SIGN 1 /* unsigned/signed, must be 1 */
@@ -2591,6 +2601,8 @@ int vbin_printf(u32 *bin_buf, size_t size, const char *fmt, va_list args)
2591 case 's': 2601 case 's':
2592 case 'F': 2602 case 'F':
2593 case 'f': 2603 case 'f':
2604 case 'x':
2605 case 'K':
2594 save_arg(void *); 2606 save_arg(void *);
2595 break; 2607 break;
2596 default: 2608 default:
@@ -2765,6 +2777,8 @@ int bstr_printf(char *buf, size_t size, const char *fmt, const u32 *bin_buf)
2765 case 's': 2777 case 's':
2766 case 'F': 2778 case 'F':
2767 case 'f': 2779 case 'f':
2780 case 'x':
2781 case 'K':
2768 process = true; 2782 process = true;
2769 break; 2783 break;
2770 default: 2784 default: