Skip to content

Commit 54edeb0

Browse files
committed
Merge branch 'for-4.20-vsprintf-hash-fixes' into for-linus
2 parents d2130e8 + 431bca2 commit 54edeb0

File tree

2 files changed

+110
-111
lines changed

2 files changed

+110
-111
lines changed

Documentation/core-api/printk-formats.rst

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -420,9 +420,8 @@ struct clk
420420
%pC pll1
421421
%pCn pll1
422422

423-
For printing struct clk structures. %pC and %pCn print the name
424-
(Common Clock Framework) or address (legacy clock framework) of the
425-
structure.
423+
For printing struct clk structures. %pC and %pCn print the name of the clock
424+
(Common Clock Framework) or a unique 32-bit ID (legacy clock framework).
426425

427426
Passed by reference.
428427

lib/vsprintf.c

Lines changed: 108 additions & 108 deletions
Original file line numberDiff line numberDiff line change
@@ -612,6 +612,109 @@ char *string(char *buf, char *end, const char *s, struct printf_spec spec)
612612
return widen_string(buf, len, end, spec);
613613
}
614614

615+
static noinline_for_stack
616+
char *pointer_string(char *buf, char *end, const void *ptr,
617+
struct printf_spec spec)
618+
{
619+
spec.base = 16;
620+
spec.flags |= SMALL;
621+
if (spec.field_width == -1) {
622+
spec.field_width = 2 * sizeof(ptr);
623+
spec.flags |= ZEROPAD;
624+
}
625+
626+
return number(buf, end, (unsigned long int)ptr, spec);
627+
}
628+
629+
/* Make pointers available for printing early in the boot sequence. */
630+
static int debug_boot_weak_hash __ro_after_init;
631+
632+
static int __init debug_boot_weak_hash_enable(char *str)
633+
{
634+
debug_boot_weak_hash = 1;
635+
pr_info("debug_boot_weak_hash enabled\n");
636+
return 0;
637+
}
638+
early_param("debug_boot_weak_hash", debug_boot_weak_hash_enable);
639+
640+
static DEFINE_STATIC_KEY_TRUE(not_filled_random_ptr_key);
641+
static siphash_key_t ptr_key __read_mostly;
642+
643+
static void enable_ptr_key_workfn(struct work_struct *work)
644+
{
645+
get_random_bytes(&ptr_key, sizeof(ptr_key));
646+
/* Needs to run from preemptible context */
647+
static_branch_disable(&not_filled_random_ptr_key);
648+
}
649+
650+
static DECLARE_WORK(enable_ptr_key_work, enable_ptr_key_workfn);
651+
652+
static void fill_random_ptr_key(struct random_ready_callback *unused)
653+
{
654+
/* This may be in an interrupt handler. */
655+
queue_work(system_unbound_wq, &enable_ptr_key_work);
656+
}
657+
658+
static struct random_ready_callback random_ready = {
659+
.func = fill_random_ptr_key
660+
};
661+
662+
static int __init initialize_ptr_random(void)
663+
{
664+
int key_size = sizeof(ptr_key);
665+
int ret;
666+
667+
/* Use hw RNG if available. */
668+
if (get_random_bytes_arch(&ptr_key, key_size) == key_size) {
669+
static_branch_disable(&not_filled_random_ptr_key);
670+
return 0;
671+
}
672+
673+
ret = add_random_ready_callback(&random_ready);
674+
if (!ret) {
675+
return 0;
676+
} else if (ret == -EALREADY) {
677+
/* This is in preemptible context */
678+
enable_ptr_key_workfn(&enable_ptr_key_work);
679+
return 0;
680+
}
681+
682+
return ret;
683+
}
684+
early_initcall(initialize_ptr_random);
685+
686+
/* Maps a pointer to a 32 bit unique identifier. */
687+
static char *ptr_to_id(char *buf, char *end, const void *ptr,
688+
struct printf_spec spec)
689+
{
690+
const char *str = sizeof(ptr) == 8 ? "(____ptrval____)" : "(ptrval)";
691+
unsigned long hashval;
692+
693+
/* When debugging early boot use non-cryptographically secure hash. */
694+
if (unlikely(debug_boot_weak_hash)) {
695+
hashval = hash_long((unsigned long)ptr, 32);
696+
return pointer_string(buf, end, (const void *)hashval, spec);
697+
}
698+
699+
if (static_branch_unlikely(&not_filled_random_ptr_key)) {
700+
spec.field_width = 2 * sizeof(ptr);
701+
/* string length must be less than default_width */
702+
return string(buf, end, str, spec);
703+
}
704+
705+
#ifdef CONFIG_64BIT
706+
hashval = (unsigned long)siphash_1u64((u64)ptr, &ptr_key);
707+
/*
708+
* Mask off the first 32 bits, this makes explicit that we have
709+
* modified the address (and 32 bits is plenty for a unique ID).
710+
*/
711+
hashval = hashval & 0xffffffff;
712+
#else
713+
hashval = (unsigned long)siphash_1u32((u32)ptr, &ptr_key);
714+
#endif
715+
return pointer_string(buf, end, (const void *)hashval, spec);
716+
}
717+
615718
static noinline_for_stack
616719
char *dentry_name(char *buf, char *end, const struct dentry *d, struct printf_spec spec,
617720
const char *fmt)
@@ -1357,20 +1460,6 @@ char *uuid_string(char *buf, char *end, const u8 *addr,
13571460
return string(buf, end, uuid, spec);
13581461
}
13591462

1360-
static noinline_for_stack
1361-
char *pointer_string(char *buf, char *end, const void *ptr,
1362-
struct printf_spec spec)
1363-
{
1364-
spec.base = 16;
1365-
spec.flags |= SMALL;
1366-
if (spec.field_width == -1) {
1367-
spec.field_width = 2 * sizeof(ptr);
1368-
spec.flags |= ZEROPAD;
1369-
}
1370-
1371-
return number(buf, end, (unsigned long int)ptr, spec);
1372-
}
1373-
13741463
int kptr_restrict __read_mostly;
13751464

13761465
static noinline_for_stack
@@ -1421,7 +1510,8 @@ char *restricted_pointer(char *buf, char *end, const void *ptr,
14211510
}
14221511

14231512
static noinline_for_stack
1424-
char *netdev_bits(char *buf, char *end, const void *addr, const char *fmt)
1513+
char *netdev_bits(char *buf, char *end, const void *addr,
1514+
struct printf_spec spec, const char *fmt)
14251515
{
14261516
unsigned long long num;
14271517
int size;
@@ -1432,9 +1522,7 @@ char *netdev_bits(char *buf, char *end, const void *addr, const char *fmt)
14321522
size = sizeof(netdev_features_t);
14331523
break;
14341524
default:
1435-
num = (unsigned long)addr;
1436-
size = sizeof(unsigned long);
1437-
break;
1525+
return ptr_to_id(buf, end, addr, spec);
14381526
}
14391527

14401528
return special_hex_number(buf, end, num, size);
@@ -1474,7 +1562,7 @@ char *clock(char *buf, char *end, struct clk *clk, struct printf_spec spec,
14741562
#ifdef CONFIG_COMMON_CLK
14751563
return string(buf, end, __clk_get_name(clk), spec);
14761564
#else
1477-
return special_hex_number(buf, end, (unsigned long)clk, sizeof(unsigned long));
1565+
return ptr_to_id(buf, end, clk, spec);
14781566
#endif
14791567
}
14801568
}
@@ -1651,94 +1739,6 @@ char *device_node_string(char *buf, char *end, struct device_node *dn,
16511739
return widen_string(buf, buf - buf_start, end, spec);
16521740
}
16531741

1654-
/* Make pointers available for printing early in the boot sequence. */
1655-
static int debug_boot_weak_hash __ro_after_init;
1656-
1657-
static int __init debug_boot_weak_hash_enable(char *str)
1658-
{
1659-
debug_boot_weak_hash = 1;
1660-
pr_info("debug_boot_weak_hash enabled\n");
1661-
return 0;
1662-
}
1663-
early_param("debug_boot_weak_hash", debug_boot_weak_hash_enable);
1664-
1665-
static DEFINE_STATIC_KEY_TRUE(not_filled_random_ptr_key);
1666-
static siphash_key_t ptr_key __read_mostly;
1667-
1668-
static void enable_ptr_key_workfn(struct work_struct *work)
1669-
{
1670-
get_random_bytes(&ptr_key, sizeof(ptr_key));
1671-
/* Needs to run from preemptible context */
1672-
static_branch_disable(&not_filled_random_ptr_key);
1673-
}
1674-
1675-
static DECLARE_WORK(enable_ptr_key_work, enable_ptr_key_workfn);
1676-
1677-
static void fill_random_ptr_key(struct random_ready_callback *unused)
1678-
{
1679-
/* This may be in an interrupt handler. */
1680-
queue_work(system_unbound_wq, &enable_ptr_key_work);
1681-
}
1682-
1683-
static struct random_ready_callback random_ready = {
1684-
.func = fill_random_ptr_key
1685-
};
1686-
1687-
static int __init initialize_ptr_random(void)
1688-
{
1689-
int key_size = sizeof(ptr_key);
1690-
int ret;
1691-
1692-
/* Use hw RNG if available. */
1693-
if (get_random_bytes_arch(&ptr_key, key_size) == key_size) {
1694-
static_branch_disable(&not_filled_random_ptr_key);
1695-
return 0;
1696-
}
1697-
1698-
ret = add_random_ready_callback(&random_ready);
1699-
if (!ret) {
1700-
return 0;
1701-
} else if (ret == -EALREADY) {
1702-
/* This is in preemptible context */
1703-
enable_ptr_key_workfn(&enable_ptr_key_work);
1704-
return 0;
1705-
}
1706-
1707-
return ret;
1708-
}
1709-
early_initcall(initialize_ptr_random);
1710-
1711-
/* Maps a pointer to a 32 bit unique identifier. */
1712-
static char *ptr_to_id(char *buf, char *end, void *ptr, struct printf_spec spec)
1713-
{
1714-
const char *str = sizeof(ptr) == 8 ? "(____ptrval____)" : "(ptrval)";
1715-
unsigned long hashval;
1716-
1717-
/* When debugging early boot use non-cryptographically secure hash. */
1718-
if (unlikely(debug_boot_weak_hash)) {
1719-
hashval = hash_long((unsigned long)ptr, 32);
1720-
return pointer_string(buf, end, (const void *)hashval, spec);
1721-
}
1722-
1723-
if (static_branch_unlikely(&not_filled_random_ptr_key)) {
1724-
spec.field_width = 2 * sizeof(ptr);
1725-
/* string length must be less than default_width */
1726-
return string(buf, end, str, spec);
1727-
}
1728-
1729-
#ifdef CONFIG_64BIT
1730-
hashval = (unsigned long)siphash_1u64((u64)ptr, &ptr_key);
1731-
/*
1732-
* Mask off the first 32 bits, this makes explicit that we have
1733-
* modified the address (and 32 bits is plenty for a unique ID).
1734-
*/
1735-
hashval = hashval & 0xffffffff;
1736-
#else
1737-
hashval = (unsigned long)siphash_1u32((u32)ptr, &ptr_key);
1738-
#endif
1739-
return pointer_string(buf, end, (const void *)hashval, spec);
1740-
}
1741-
17421742
/*
17431743
* Show a '%p' thing. A kernel extension is that the '%p' is followed
17441744
* by an extra set of alphanumeric characters that are extended format
@@ -1944,7 +1944,7 @@ char *pointer(const char *fmt, char *buf, char *end, void *ptr,
19441944
break;
19451945
return restricted_pointer(buf, end, ptr, spec);
19461946
case 'N':
1947-
return netdev_bits(buf, end, ptr, fmt);
1947+
return netdev_bits(buf, end, ptr, spec, fmt);
19481948
case 'a':
19491949
return address_val(buf, end, ptr, fmt);
19501950
case 'd':

0 commit comments

Comments
 (0)