aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLinus Torvalds <torvalds@linux-foundation.org>2017-11-29 13:19:29 -0500
committerLinus Torvalds <torvalds@linux-foundation.org>2017-11-29 13:19:29 -0500
commitda6af54dc0014d733ce014a196e4c84ee43cabec (patch)
tree39785a08f5eb4783caec2ac1e51d6084117b3357
parentf55e1014f9e567d830eb3a7f57d879a34872af4b (diff)
parent6424f6bb432752c7eb90cbeeb1c31d6125bba39a (diff)
Merge tag 'printk-hash-pointer-4.15-rc2' of git://github.com/tcharding/linux
Pull printk pointer hashing update from Tobin Harding: "Here is the patch set that implements hashing of printk specifier %p. First we have two clean up patches then we do the hashing. Hashing is done via the SipHash algorithm. The next patch adds printk specifier %px for printing pointers when we _really_ want to see the address i.e %px is functionally equivalent to %lx. Final patch in the set fixes KASAN since we break it by hashing %p. For the record here is the justification for the series: Currently there exist approximately 14 000 places in the Kernel where addresses are being printed using an unadorned %p. This potentially leaks sensitive information about the Kernel layout in memory. Many of these calls are stale, instead of fixing every call we hash the address by default before printing. We then add %px to provide a way to print the actual address. Although this is achievable using %lx, using %px will assist us if we ever want to change pointer printing behaviour. %px is more uniquely grep'able (there are already >50 000 uses of %lx). The added advantage of hashing %p is that security is now opt-out, if you _really_ want the address you have to work a little harder and use %px. This will of course break some users, forcing code printing needed addresses to be updated" [ I do expect this to be an annoyance, and a number of %px users to be added for debuggability. But nobody is willing to audit existing %p users for information leaks, and a number of places really only use the pointer as an object identifier rather than really 'I need the address'. IOW - sorry for the inconvenience, but it's the least inconvenient of the options. - Linus ] * tag 'printk-hash-pointer-4.15-rc2' of git://github.com/tcharding/linux: kasan: use %px to print addresses instead of %p vsprintf: add printk specifier %px printk: hash addresses printed with %p vsprintf: refactor %pK code out of pointer() docs: correct documentation for %pK
-rw-r--r--Documentation/printk-formats.txt31
-rw-r--r--lib/test_printf.c108
-rw-r--r--lib/vsprintf.c194
-rw-r--r--mm/kasan/report.c8
-rwxr-xr-xscripts/checkpatch.pl2
5 files changed, 248 insertions, 95 deletions
diff --git a/Documentation/printk-formats.txt b/Documentation/printk-formats.txt
index 361789df51ec..aa0a776c817a 100644
--- a/Documentation/printk-formats.txt
+++ b/Documentation/printk-formats.txt
@@ -5,7 +5,6 @@ How to get printk format specifiers right
5:Author: Randy Dunlap <rdunlap@infradead.org> 5:Author: Randy Dunlap <rdunlap@infradead.org>
6:Author: Andrew Murray <amurray@mpc-data.co.uk> 6:Author: Andrew Murray <amurray@mpc-data.co.uk>
7 7
8
9Integer types 8Integer types
10============= 9=============
11 10
@@ -45,6 +44,18 @@ return from vsnprintf.
45Raw pointer value SHOULD be printed with %p. The kernel supports 44Raw pointer value SHOULD be printed with %p. The kernel supports
46the following extended format specifiers for pointer types: 45the following extended format specifiers for pointer types:
47 46
47Pointer Types
48=============
49
50Pointers printed without a specifier extension (i.e unadorned %p) are
51hashed to give a unique identifier without leaking kernel addresses to user
52space. On 64 bit machines the first 32 bits are zeroed. If you _really_
53want the address see %px below.
54
55::
56
57 %p abcdef12 or 00000000abcdef12
58
48Symbols/Function Pointers 59Symbols/Function Pointers
49========================= 60=========================
50 61
@@ -85,18 +96,32 @@ Examples::
85 printk("Faulted at %pS\n", (void *)regs->ip); 96 printk("Faulted at %pS\n", (void *)regs->ip);
86 printk(" %s%pB\n", (reliable ? "" : "? "), (void *)*stack); 97 printk(" %s%pB\n", (reliable ? "" : "? "), (void *)*stack);
87 98
88
89Kernel Pointers 99Kernel Pointers
90=============== 100===============
91 101
92:: 102::
93 103
94 %pK 0x01234567 or 0x0123456789abcdef 104 %pK 01234567 or 0123456789abcdef
95 105
96For printing kernel pointers which should be hidden from unprivileged 106For printing kernel pointers which should be hidden from unprivileged
97users. The behaviour of ``%pK`` depends on the ``kptr_restrict sysctl`` - see 107users. The behaviour of ``%pK`` depends on the ``kptr_restrict sysctl`` - see
98Documentation/sysctl/kernel.txt for more details. 108Documentation/sysctl/kernel.txt for more details.
99 109
110Unmodified Addresses
111====================
112
113::
114
115 %px 01234567 or 0123456789abcdef
116
117For printing pointers when you _really_ want to print the address. Please
118consider whether or not you are leaking sensitive information about the
119Kernel layout in memory before printing pointers with %px. %px is
120functionally equivalent to %lx. %px is preferred to %lx because it is more
121uniquely grep'able. If, in the future, we need to modify the way the Kernel
122handles printing pointers it will be nice to be able to find the call
123sites.
124
100Struct Resources 125Struct Resources
101================ 126================
102 127
diff --git a/lib/test_printf.c b/lib/test_printf.c
index 563f10e6876a..71ebfa43ad05 100644
--- a/lib/test_printf.c
+++ b/lib/test_printf.c
@@ -24,24 +24,6 @@
24#define PAD_SIZE 16 24#define PAD_SIZE 16
25#define FILL_CHAR '$' 25#define FILL_CHAR '$'
26 26
27#define PTR1 ((void*)0x01234567)
28#define PTR2 ((void*)(long)(int)0xfedcba98)
29
30#if BITS_PER_LONG == 64
31#define PTR1_ZEROES "000000000"
32#define PTR1_SPACES " "
33#define PTR1_STR "1234567"
34#define PTR2_STR "fffffffffedcba98"
35#define PTR_WIDTH 16
36#else
37#define PTR1_ZEROES "0"
38#define PTR1_SPACES " "
39#define PTR1_STR "1234567"
40#define PTR2_STR "fedcba98"
41#define PTR_WIDTH 8
42#endif
43#define PTR_WIDTH_STR stringify(PTR_WIDTH)
44
45static unsigned total_tests __initdata; 27static unsigned total_tests __initdata;
46static unsigned failed_tests __initdata; 28static unsigned failed_tests __initdata;
47static char *test_buffer __initdata; 29static char *test_buffer __initdata;
@@ -217,30 +199,79 @@ test_string(void)
217 test("a | | ", "%-3.s|%-3.0s|%-3.*s", "a", "b", 0, "c"); 199 test("a | | ", "%-3.s|%-3.0s|%-3.*s", "a", "b", 0, "c");
218} 200}
219 201
202#define PLAIN_BUF_SIZE 64 /* leave some space so we don't oops */
203
204#if BITS_PER_LONG == 64
205
206#define PTR_WIDTH 16
207#define PTR ((void *)0xffff0123456789ab)
208#define PTR_STR "ffff0123456789ab"
209#define ZEROS "00000000" /* hex 32 zero bits */
210
211static int __init
212plain_format(void)
213{
214 char buf[PLAIN_BUF_SIZE];
215 int nchars;
216
217 nchars = snprintf(buf, PLAIN_BUF_SIZE, "%p", PTR);
218
219 if (nchars != PTR_WIDTH || strncmp(buf, ZEROS, strlen(ZEROS)) != 0)
220 return -1;
221
222 return 0;
223}
224
225#else
226
227#define PTR_WIDTH 8
228#define PTR ((void *)0x456789ab)
229#define PTR_STR "456789ab"
230
231static int __init
232plain_format(void)
233{
234 /* Format is implicitly tested for 32 bit machines by plain_hash() */
235 return 0;
236}
237
238#endif /* BITS_PER_LONG == 64 */
239
240static int __init
241plain_hash(void)
242{
243 char buf[PLAIN_BUF_SIZE];
244 int nchars;
245
246 nchars = snprintf(buf, PLAIN_BUF_SIZE, "%p", PTR);
247
248 if (nchars != PTR_WIDTH || strncmp(buf, PTR_STR, PTR_WIDTH) == 0)
249 return -1;
250
251 return 0;
252}
253
254/*
255 * We can't use test() to test %p because we don't know what output to expect
256 * after an address is hashed.
257 */
220static void __init 258static void __init
221plain(void) 259plain(void)
222{ 260{
223 test(PTR1_ZEROES PTR1_STR " " PTR2_STR, "%p %p", PTR1, PTR2); 261 int err;
224 /*
225 * The field width is overloaded for some %p extensions to
226 * pass another piece of information. For plain pointers, the
227 * behaviour is slightly odd: One cannot pass either the 0
228 * flag nor a precision to %p without gcc complaining, and if
229 * one explicitly gives a field width, the number is no longer
230 * zero-padded.
231 */
232 test("|" PTR1_STR PTR1_SPACES " | " PTR1_SPACES PTR1_STR "|",
233 "|%-*p|%*p|", PTR_WIDTH+2, PTR1, PTR_WIDTH+2, PTR1);
234 test("|" PTR2_STR " | " PTR2_STR "|",
235 "|%-*p|%*p|", PTR_WIDTH+2, PTR2, PTR_WIDTH+2, PTR2);
236 262
237 /* 263 err = plain_hash();
238 * Unrecognized %p extensions are treated as plain %p, but the 264 if (err) {
239 * alphanumeric suffix is ignored (that is, does not occur in 265 pr_warn("plain 'p' does not appear to be hashed\n");
240 * the output.) 266 failed_tests++;
241 */ 267 return;
242 test("|"PTR1_ZEROES PTR1_STR"|", "|%p0y|", PTR1); 268 }
243 test("|"PTR2_STR"|", "|%p0y|", PTR2); 269
270 err = plain_format();
271 if (err) {
272 pr_warn("hashing plain 'p' has unexpected format\n");
273 failed_tests++;
274 }
244} 275}
245 276
246static void __init 277static void __init
@@ -251,6 +282,7 @@ symbol_ptr(void)
251static void __init 282static void __init
252kernel_ptr(void) 283kernel_ptr(void)
253{ 284{
285 /* We can't test this without access to kptr_restrict. */
254} 286}
255 287
256static void __init 288static void __init
diff --git a/lib/vsprintf.c b/lib/vsprintf.c
index 1746bae94d41..d960aead0336 100644
--- a/lib/vsprintf.c
+++ b/lib/vsprintf.c
@@ -33,6 +33,8 @@
33#include <linux/uuid.h> 33#include <linux/uuid.h>
34#include <linux/of.h> 34#include <linux/of.h>
35#include <net/addrconf.h> 35#include <net/addrconf.h>
36#include <linux/siphash.h>
37#include <linux/compiler.h>
36#ifdef CONFIG_BLOCK 38#ifdef CONFIG_BLOCK
37#include <linux/blkdev.h> 39#include <linux/blkdev.h>
38#endif 40#endif
@@ -1343,6 +1345,59 @@ char *uuid_string(char *buf, char *end, const u8 *addr,
1343 return string(buf, end, uuid, spec); 1345 return string(buf, end, uuid, spec);
1344} 1346}
1345 1347
1348int kptr_restrict __read_mostly;
1349
1350static noinline_for_stack
1351char *restricted_pointer(char *buf, char *end, const void *ptr,
1352 struct printf_spec spec)
1353{
1354 spec.base = 16;
1355 spec.flags |= SMALL;
1356 if (spec.field_width == -1) {
1357 spec.field_width = 2 * sizeof(ptr);
1358 spec.flags |= ZEROPAD;
1359 }
1360
1361 switch (kptr_restrict) {
1362 case 0:
1363 /* Always print %pK values */
1364 break;
1365 case 1: {
1366 const struct cred *cred;
1367
1368 /*
1369 * kptr_restrict==1 cannot be used in IRQ context
1370 * because its test for CAP_SYSLOG would be meaningless.
1371 */
1372 if (in_irq() || in_serving_softirq() || in_nmi())
1373 return string(buf, end, "pK-error", spec);
1374
1375 /*
1376 * Only print the real pointer value if the current
1377 * process has CAP_SYSLOG and is running with the
1378 * same credentials it started with. This is because
1379 * access to files is checked at open() time, but %pK
1380 * checks permission at read() time. We don't want to
1381 * leak pointer values if a binary opens a file using
1382 * %pK and then elevates privileges before reading it.
1383 */
1384 cred = current_cred();
1385 if (!has_capability_noaudit(current, CAP_SYSLOG) ||
1386 !uid_eq(cred->euid, cred->uid) ||
1387 !gid_eq(cred->egid, cred->gid))
1388 ptr = NULL;
1389 break;
1390 }
1391 case 2:
1392 default:
1393 /* Always print 0's for %pK */
1394 ptr = NULL;
1395 break;
1396 }
1397
1398 return number(buf, end, (unsigned long)ptr, spec);
1399}
1400
1346static noinline_for_stack 1401static noinline_for_stack
1347char *netdev_bits(char *buf, char *end, const void *addr, const char *fmt) 1402char *netdev_bits(char *buf, char *end, const void *addr, const char *fmt)
1348{ 1403{
@@ -1591,7 +1646,86 @@ char *device_node_string(char *buf, char *end, struct device_node *dn,
1591 return widen_string(buf, buf - buf_start, end, spec); 1646 return widen_string(buf, buf - buf_start, end, spec);
1592} 1647}
1593 1648
1594int kptr_restrict __read_mostly; 1649static noinline_for_stack
1650char *pointer_string(char *buf, char *end, const void *ptr,
1651 struct printf_spec spec)
1652{
1653 spec.base = 16;
1654 spec.flags |= SMALL;
1655 if (spec.field_width == -1) {
1656 spec.field_width = 2 * sizeof(ptr);
1657 spec.flags |= ZEROPAD;
1658 }
1659
1660 return number(buf, end, (unsigned long int)ptr, spec);
1661}
1662
1663static bool have_filled_random_ptr_key __read_mostly;
1664static siphash_key_t ptr_key __read_mostly;
1665
1666static void fill_random_ptr_key(struct random_ready_callback *unused)
1667{
1668 get_random_bytes(&ptr_key, sizeof(ptr_key));
1669 /*
1670 * have_filled_random_ptr_key==true is dependent on get_random_bytes().
1671 * ptr_to_id() needs to see have_filled_random_ptr_key==true
1672 * after get_random_bytes() returns.
1673 */
1674 smp_mb();
1675 WRITE_ONCE(have_filled_random_ptr_key, true);
1676}
1677
1678static struct random_ready_callback random_ready = {
1679 .func = fill_random_ptr_key
1680};
1681
1682static int __init initialize_ptr_random(void)
1683{
1684 int ret = add_random_ready_callback(&random_ready);
1685
1686 if (!ret) {
1687 return 0;
1688 } else if (ret == -EALREADY) {
1689 fill_random_ptr_key(&random_ready);
1690 return 0;
1691 }
1692
1693 return ret;
1694}
1695early_initcall(initialize_ptr_random);
1696
1697/* Maps a pointer to a 32 bit unique identifier. */
1698static char *ptr_to_id(char *buf, char *end, void *ptr, struct printf_spec spec)
1699{
1700 unsigned long hashval;
1701 const int default_width = 2 * sizeof(ptr);
1702
1703 if (unlikely(!have_filled_random_ptr_key)) {
1704 spec.field_width = default_width;
1705 /* string length must be less than default_width */
1706 return string(buf, end, "(ptrval)", spec);
1707 }
1708
1709#ifdef CONFIG_64BIT
1710 hashval = (unsigned long)siphash_1u64((u64)ptr, &ptr_key);
1711 /*
1712 * Mask off the first 32 bits, this makes explicit that we have
1713 * modified the address (and 32 bits is plenty for a unique ID).
1714 */
1715 hashval = hashval & 0xffffffff;
1716#else
1717 hashval = (unsigned long)siphash_1u32((u32)ptr, &ptr_key);
1718#endif
1719
1720 spec.flags |= SMALL;
1721 if (spec.field_width == -1) {
1722 spec.field_width = default_width;
1723 spec.flags |= ZEROPAD;
1724 }
1725 spec.base = 16;
1726
1727 return number(buf, end, hashval, spec);
1728}
1595 1729
1596/* 1730/*
1597 * Show a '%p' thing. A kernel extension is that the '%p' is followed 1731 * Show a '%p' thing. A kernel extension is that the '%p' is followed
@@ -1698,11 +1832,16 @@ int kptr_restrict __read_mostly;
1698 * c major compatible string 1832 * c major compatible string
1699 * C full compatible string 1833 * C full compatible string
1700 * 1834 *
1835 * - 'x' For printing the address. Equivalent to "%lx".
1836 *
1701 * ** Please update also Documentation/printk-formats.txt when making changes ** 1837 * ** Please update also Documentation/printk-formats.txt when making changes **
1702 * 1838 *
1703 * Note: The difference between 'S' and 'F' is that on ia64 and ppc64 1839 * Note: The difference between 'S' and 'F' is that on ia64 and ppc64
1704 * function pointers are really function descriptors, which contain a 1840 * function pointers are really function descriptors, which contain a
1705 * pointer to the real address. 1841 * pointer to the real address.
1842 *
1843 * Note: The default behaviour (unadorned %p) is to hash the address,
1844 * rendering it useful as a unique identifier.
1706 */ 1845 */
1707static noinline_for_stack 1846static noinline_for_stack
1708char *pointer(const char *fmt, char *buf, char *end, void *ptr, 1847char *pointer(const char *fmt, char *buf, char *end, void *ptr,
@@ -1792,47 +1931,7 @@ char *pointer(const char *fmt, char *buf, char *end, void *ptr,
1792 return buf; 1931 return buf;
1793 } 1932 }
1794 case 'K': 1933 case 'K':
1795 switch (kptr_restrict) { 1934 return restricted_pointer(buf, end, ptr, spec);
1796 case 0:
1797 /* Always print %pK values */
1798 break;
1799 case 1: {
1800 const struct cred *cred;
1801
1802 /*
1803 * kptr_restrict==1 cannot be used in IRQ context
1804 * because its test for CAP_SYSLOG would be meaningless.
1805 */
1806 if (in_irq() || in_serving_softirq() || in_nmi()) {
1807 if (spec.field_width == -1)
1808 spec.field_width = default_width;
1809 return string(buf, end, "pK-error", spec);
1810 }
1811
1812 /*
1813 * Only print the real pointer value if the current
1814 * process has CAP_SYSLOG and is running with the
1815 * same credentials it started with. This is because
1816 * access to files is checked at open() time, but %pK
1817 * checks permission at read() time. We don't want to
1818 * leak pointer values if a binary opens a file using
1819 * %pK and then elevates privileges before reading it.
1820 */
1821 cred = current_cred();
1822 if (!has_capability_noaudit(current, CAP_SYSLOG) ||
1823 !uid_eq(cred->euid, cred->uid) ||
1824 !gid_eq(cred->egid, cred->gid))
1825 ptr = NULL;
1826 break;
1827 }
1828 case 2:
1829 default:
1830 /* Always print 0's for %pK */
1831 ptr = NULL;
1832 break;
1833 }
1834 break;
1835
1836 case 'N': 1935 case 'N':
1837 return netdev_bits(buf, end, ptr, fmt); 1936 return netdev_bits(buf, end, ptr, fmt);
1838 case 'a': 1937 case 'a':
@@ -1857,15 +1956,12 @@ char *pointer(const char *fmt, char *buf, char *end, void *ptr,
1857 case 'F': 1956 case 'F':
1858 return device_node_string(buf, end, ptr, spec, fmt + 1); 1957 return device_node_string(buf, end, ptr, spec, fmt + 1);
1859 } 1958 }
1959 case 'x':
1960 return pointer_string(buf, end, ptr, spec);
1860 } 1961 }
1861 spec.flags |= SMALL;
1862 if (spec.field_width == -1) {
1863 spec.field_width = default_width;
1864 spec.flags |= ZEROPAD;
1865 }
1866 spec.base = 16;
1867 1962
1868 return number(buf, end, (unsigned long) ptr, spec); 1963 /* default is to _not_ leak addresses, hash before printing */
1964 return ptr_to_id(buf, end, ptr, spec);
1869} 1965}
1870 1966
1871/* 1967/*
diff --git a/mm/kasan/report.c b/mm/kasan/report.c
index 6bcfb01ba038..410c8235e671 100644
--- a/mm/kasan/report.c
+++ b/mm/kasan/report.c
@@ -134,7 +134,7 @@ static void print_error_description(struct kasan_access_info *info)
134 134
135 pr_err("BUG: KASAN: %s in %pS\n", 135 pr_err("BUG: KASAN: %s in %pS\n",
136 bug_type, (void *)info->ip); 136 bug_type, (void *)info->ip);
137 pr_err("%s of size %zu at addr %p by task %s/%d\n", 137 pr_err("%s of size %zu at addr %px by task %s/%d\n",
138 info->is_write ? "Write" : "Read", info->access_size, 138 info->is_write ? "Write" : "Read", info->access_size,
139 info->access_addr, current->comm, task_pid_nr(current)); 139 info->access_addr, current->comm, task_pid_nr(current));
140} 140}
@@ -206,7 +206,7 @@ static void describe_object_addr(struct kmem_cache *cache, void *object,
206 const char *rel_type; 206 const char *rel_type;
207 int rel_bytes; 207 int rel_bytes;
208 208
209 pr_err("The buggy address belongs to the object at %p\n" 209 pr_err("The buggy address belongs to the object at %px\n"
210 " which belongs to the cache %s of size %d\n", 210 " which belongs to the cache %s of size %d\n",
211 object, cache->name, cache->object_size); 211 object, cache->name, cache->object_size);
212 212
@@ -225,7 +225,7 @@ static void describe_object_addr(struct kmem_cache *cache, void *object,
225 } 225 }
226 226
227 pr_err("The buggy address is located %d bytes %s of\n" 227 pr_err("The buggy address is located %d bytes %s of\n"
228 " %d-byte region [%p, %p)\n", 228 " %d-byte region [%px, %px)\n",
229 rel_bytes, rel_type, cache->object_size, (void *)object_addr, 229 rel_bytes, rel_type, cache->object_size, (void *)object_addr,
230 (void *)(object_addr + cache->object_size)); 230 (void *)(object_addr + cache->object_size));
231} 231}
@@ -302,7 +302,7 @@ static void print_shadow_for_address(const void *addr)
302 char shadow_buf[SHADOW_BYTES_PER_ROW]; 302 char shadow_buf[SHADOW_BYTES_PER_ROW];
303 303
304 snprintf(buffer, sizeof(buffer), 304 snprintf(buffer, sizeof(buffer),
305 (i == 0) ? ">%p: " : " %p: ", kaddr); 305 (i == 0) ? ">%px: " : " %px: ", kaddr);
306 /* 306 /*
307 * We should not pass a shadow pointer to generic 307 * We should not pass a shadow pointer to generic
308 * function, because generic functions may try to 308 * function, because generic functions may try to
diff --git a/scripts/checkpatch.pl b/scripts/checkpatch.pl
index 95cda3ecc66b..040aa79e1d9d 100755
--- a/scripts/checkpatch.pl
+++ b/scripts/checkpatch.pl
@@ -5753,7 +5753,7 @@ sub process {
5753 for (my $count = $linenr; $count <= $lc; $count++) { 5753 for (my $count = $linenr; $count <= $lc; $count++) {
5754 my $fmt = get_quoted_string($lines[$count - 1], raw_line($count, 0)); 5754 my $fmt = get_quoted_string($lines[$count - 1], raw_line($count, 0));
5755 $fmt =~ s/%%//g; 5755 $fmt =~ s/%%//g;
5756 if ($fmt =~ /(\%[\*\d\.]*p(?![\WFfSsBKRraEhMmIiUDdgVCbGNO]).)/) { 5756 if ($fmt =~ /(\%[\*\d\.]*p(?![\WFfSsBKRraEhMmIiUDdgVCbGNOx]).)/) {
5757 $bad_extension = $1; 5757 $bad_extension = $1;
5758 last; 5758 last;
5759 } 5759 }