hc
2023-12-09 b22da3d8526a935aa31e086e63f60ff3246cb61c
kernel/lib/vsprintf.c
....@@ -1,3 +1,4 @@
1
+// SPDX-License-Identifier: GPL-2.0-only
12 /*
23 * linux/lib/vsprintf.c
34 *
....@@ -17,8 +18,10 @@
1718 */
1819
1920 #include <stdarg.h>
21
+#include <linux/build_bug.h>
2022 #include <linux/clk.h>
2123 #include <linux/clk-provider.h>
24
+#include <linux/errname.h>
2225 #include <linux/module.h> /* for KSYM_SYMBOL_LEN */
2326 #include <linux/types.h>
2427 #include <linux/string.h>
....@@ -30,11 +33,14 @@
3033 #include <linux/ioport.h>
3134 #include <linux/dcache.h>
3235 #include <linux/cred.h>
36
+#include <linux/rtc.h>
37
+#include <linux/time.h>
3338 #include <linux/uuid.h>
3439 #include <linux/of.h>
3540 #include <net/addrconf.h>
3641 #include <linux/siphash.h>
3742 #include <linux/compiler.h>
43
+#include <linux/property.h>
3844 #ifdef CONFIG_BLOCK
3945 #include <linux/blkdev.h>
4046 #endif
....@@ -46,6 +52,10 @@
4652
4753 #include <linux/string_helpers.h>
4854 #include "kstrtox.h"
55
+
56
+/* Disable pointer hashing if requested */
57
+bool no_hash_pointers __ro_after_init;
58
+EXPORT_SYMBOL_GPL(no_hash_pointers);
4959
5060 static unsigned long long simple_strntoull(const char *startp, size_t max_chars,
5161 char **endp, unsigned int base)
....@@ -78,7 +88,7 @@
7888 * @endp: A pointer to the end of the parsed string will be placed here
7989 * @base: The number base to use
8090 *
81
- * This function is obsolete. Please use kstrtoull instead.
91
+ * This function has caveats. Please use kstrtoull instead.
8292 */
8393 unsigned long long simple_strtoull(const char *cp, char **endp, unsigned int base)
8494 {
....@@ -92,7 +102,7 @@
92102 * @endp: A pointer to the end of the parsed string will be placed here
93103 * @base: The number base to use
94104 *
95
- * This function is obsolete. Please use kstrtoul instead.
105
+ * This function has caveats. Please use kstrtoul instead.
96106 */
97107 unsigned long simple_strtoul(const char *cp, char **endp, unsigned int base)
98108 {
....@@ -106,7 +116,7 @@
106116 * @endp: A pointer to the end of the parsed string will be placed here
107117 * @base: The number base to use
108118 *
109
- * This function is obsolete. Please use kstrtol instead.
119
+ * This function has caveats. Please use kstrtol instead.
110120 */
111121 long simple_strtol(const char *cp, char **endp, unsigned int base)
112122 {
....@@ -138,7 +148,7 @@
138148 * @endp: A pointer to the end of the parsed string will be placed here
139149 * @base: The number base to use
140150 *
141
- * This function is obsolete. Please use kstrtoll instead.
151
+ * This function has caveats. Please use kstrtoll instead.
142152 */
143153 long long simple_strtoll(const char *cp, char **endp, unsigned int base)
144154 {
....@@ -401,6 +411,9 @@
401411 #define SMALL 32 /* use lowercase in hex (must be 32 == 0x20) */
402412 #define SPECIAL 64 /* prefix hex with "0x", octal with "0" */
403413
414
+static_assert(ZEROPAD == ('0' - ' '));
415
+static_assert(SMALL == ' ');
416
+
404417 enum format_type {
405418 FORMAT_TYPE_NONE, /* Just a string part */
406419 FORMAT_TYPE_WIDTH,
....@@ -430,6 +443,8 @@
430443 unsigned int base:8; /* number base, 8, 10 or 16 only */
431444 signed int precision:16; /* # of digits/chars */
432445 } __packed;
446
+static_assert(sizeof(struct printf_spec) == 8);
447
+
433448 #define FIELD_WIDTH_MAX ((1 << 23) - 1)
434449 #define PRECISION_MAX ((1 << 15) - 1)
435450
....@@ -446,8 +461,6 @@
446461 bool is_zero = num == 0LL;
447462 int field_width = spec.field_width;
448463 int precision = spec.precision;
449
-
450
- BUILD_BUG_ON(sizeof(struct printf_spec) != 8);
451464
452465 /* locase = 0 or 0x20. ORing digits or letters with 'locase'
453466 * produces same digits or (maybe lowercased) letters */
....@@ -527,7 +540,7 @@
527540 /* zero or space padding */
528541 if (!(spec.flags & LEFT)) {
529542 char c = ' ' + (spec.flags & ZEROPAD);
530
- BUILD_BUG_ON(' ' + ZEROPAD != '0');
543
+
531544 while (--field_width >= 0) {
532545 if (buf < end)
533546 *buf = c;
....@@ -617,14 +630,12 @@
617630 return buf;
618631 }
619632
620
-static noinline_for_stack
621
-char *string(char *buf, char *end, const char *s, struct printf_spec spec)
633
+/* Handle string from a well known address. */
634
+static char *string_nocheck(char *buf, char *end, const char *s,
635
+ struct printf_spec spec)
622636 {
623637 int len = 0;
624
- size_t lim = spec.precision;
625
-
626
- if ((unsigned long)s < PAGE_SIZE)
627
- s = "(null)";
638
+ int lim = spec.precision;
628639
629640 while (lim--) {
630641 char c = *s++;
....@@ -636,6 +647,272 @@
636647 ++len;
637648 }
638649 return widen_string(buf, len, end, spec);
650
+}
651
+
652
+static char *err_ptr(char *buf, char *end, void *ptr,
653
+ struct printf_spec spec)
654
+{
655
+ int err = PTR_ERR(ptr);
656
+ const char *sym = errname(err);
657
+
658
+ if (sym)
659
+ return string_nocheck(buf, end, sym, spec);
660
+
661
+ /*
662
+ * Somebody passed ERR_PTR(-1234) or some other non-existing
663
+ * Efoo - or perhaps CONFIG_SYMBOLIC_ERRNAME=n. Fall back to
664
+ * printing it as its decimal representation.
665
+ */
666
+ spec.flags |= SIGN;
667
+ spec.base = 10;
668
+ return number(buf, end, err, spec);
669
+}
670
+
671
+/* Be careful: error messages must fit into the given buffer. */
672
+static char *error_string(char *buf, char *end, const char *s,
673
+ struct printf_spec spec)
674
+{
675
+ /*
676
+ * Hard limit to avoid a completely insane messages. It actually
677
+ * works pretty well because most error messages are in
678
+ * the many pointer format modifiers.
679
+ */
680
+ if (spec.precision == -1)
681
+ spec.precision = 2 * sizeof(void *);
682
+
683
+ return string_nocheck(buf, end, s, spec);
684
+}
685
+
686
+/*
687
+ * Do not call any complex external code here. Nested printk()/vsprintf()
688
+ * might cause infinite loops. Failures might break printk() and would
689
+ * be hard to debug.
690
+ */
691
+static const char *check_pointer_msg(const void *ptr)
692
+{
693
+ if (!ptr)
694
+ return "(null)";
695
+
696
+ if ((unsigned long)ptr < PAGE_SIZE || IS_ERR_VALUE(ptr))
697
+ return "(efault)";
698
+
699
+ return NULL;
700
+}
701
+
702
+static int check_pointer(char **buf, char *end, const void *ptr,
703
+ struct printf_spec spec)
704
+{
705
+ const char *err_msg;
706
+
707
+ err_msg = check_pointer_msg(ptr);
708
+ if (err_msg) {
709
+ *buf = error_string(*buf, end, err_msg, spec);
710
+ return -EFAULT;
711
+ }
712
+
713
+ return 0;
714
+}
715
+
716
+static noinline_for_stack
717
+char *string(char *buf, char *end, const char *s,
718
+ struct printf_spec spec)
719
+{
720
+ if (check_pointer(&buf, end, s, spec))
721
+ return buf;
722
+
723
+ return string_nocheck(buf, end, s, spec);
724
+}
725
+
726
+static char *pointer_string(char *buf, char *end,
727
+ const void *ptr,
728
+ struct printf_spec spec)
729
+{
730
+ spec.base = 16;
731
+ spec.flags |= SMALL;
732
+ if (spec.field_width == -1) {
733
+ spec.field_width = 2 * sizeof(ptr);
734
+ spec.flags |= ZEROPAD;
735
+ }
736
+
737
+ return number(buf, end, (unsigned long int)ptr, spec);
738
+}
739
+
740
+/* Make pointers available for printing early in the boot sequence. */
741
+static int debug_boot_weak_hash __ro_after_init;
742
+
743
+static int __init debug_boot_weak_hash_enable(char *str)
744
+{
745
+ debug_boot_weak_hash = 1;
746
+ pr_info("debug_boot_weak_hash enabled\n");
747
+ return 0;
748
+}
749
+early_param("debug_boot_weak_hash", debug_boot_weak_hash_enable);
750
+
751
+static DEFINE_STATIC_KEY_TRUE(not_filled_random_ptr_key);
752
+static siphash_key_t ptr_key __read_mostly;
753
+
754
+static void enable_ptr_key_workfn(struct work_struct *work)
755
+{
756
+ get_random_bytes(&ptr_key, sizeof(ptr_key));
757
+ /* Needs to run from preemptible context */
758
+ static_branch_disable(&not_filled_random_ptr_key);
759
+}
760
+
761
+static DECLARE_WORK(enable_ptr_key_work, enable_ptr_key_workfn);
762
+
763
+static int fill_random_ptr_key(struct notifier_block *nb,
764
+ unsigned long action, void *data)
765
+{
766
+ /* This may be in an interrupt handler. */
767
+ queue_work(system_unbound_wq, &enable_ptr_key_work);
768
+ return 0;
769
+}
770
+
771
+static struct notifier_block random_ready = {
772
+ .notifier_call = fill_random_ptr_key
773
+};
774
+
775
+static int __init initialize_ptr_random(void)
776
+{
777
+ int key_size = sizeof(ptr_key);
778
+ int ret;
779
+
780
+ /* Use hw RNG if available. */
781
+ if (get_random_bytes_arch(&ptr_key, key_size) == key_size) {
782
+ static_branch_disable(&not_filled_random_ptr_key);
783
+ return 0;
784
+ }
785
+
786
+ ret = register_random_ready_notifier(&random_ready);
787
+ if (!ret) {
788
+ return 0;
789
+ } else if (ret == -EALREADY) {
790
+ /* This is in preemptible context */
791
+ enable_ptr_key_workfn(&enable_ptr_key_work);
792
+ return 0;
793
+ }
794
+
795
+ return ret;
796
+}
797
+early_initcall(initialize_ptr_random);
798
+
799
+/* Maps a pointer to a 32 bit unique identifier. */
800
+static inline int __ptr_to_hashval(const void *ptr, unsigned long *hashval_out)
801
+{
802
+ unsigned long hashval;
803
+
804
+ if (static_branch_unlikely(&not_filled_random_ptr_key))
805
+ return -EAGAIN;
806
+
807
+#ifdef CONFIG_64BIT
808
+ hashval = (unsigned long)siphash_1u64((u64)ptr, &ptr_key);
809
+ /*
810
+ * Mask off the first 32 bits, this makes explicit that we have
811
+ * modified the address (and 32 bits is plenty for a unique ID).
812
+ */
813
+ hashval = hashval & 0xffffffff;
814
+#else
815
+ hashval = (unsigned long)siphash_1u32((u32)ptr, &ptr_key);
816
+#endif
817
+ *hashval_out = hashval;
818
+ return 0;
819
+}
820
+
821
+int ptr_to_hashval(const void *ptr, unsigned long *hashval_out)
822
+{
823
+ return __ptr_to_hashval(ptr, hashval_out);
824
+}
825
+
826
+static char *ptr_to_id(char *buf, char *end, const void *ptr,
827
+ struct printf_spec spec)
828
+{
829
+ const char *str = sizeof(ptr) == 8 ? "(____ptrval____)" : "(ptrval)";
830
+ unsigned long hashval;
831
+ int ret;
832
+
833
+ /*
834
+ * Print the real pointer value for NULL and error pointers,
835
+ * as they are not actual addresses.
836
+ */
837
+ if (IS_ERR_OR_NULL(ptr))
838
+ return pointer_string(buf, end, ptr, spec);
839
+
840
+ /* When debugging early boot use non-cryptographically secure hash. */
841
+ if (unlikely(debug_boot_weak_hash)) {
842
+ hashval = hash_long((unsigned long)ptr, 32);
843
+ return pointer_string(buf, end, (const void *)hashval, spec);
844
+ }
845
+
846
+ ret = __ptr_to_hashval(ptr, &hashval);
847
+ if (ret) {
848
+ spec.field_width = 2 * sizeof(ptr);
849
+ /* string length must be less than default_width */
850
+ return error_string(buf, end, str, spec);
851
+ }
852
+
853
+ return pointer_string(buf, end, (const void *)hashval, spec);
854
+}
855
+
856
+static char *default_pointer(char *buf, char *end, const void *ptr,
857
+ struct printf_spec spec)
858
+{
859
+ /*
860
+ * default is to _not_ leak addresses, so hash before printing,
861
+ * unless no_hash_pointers is specified on the command line.
862
+ */
863
+ if (unlikely(no_hash_pointers))
864
+ return pointer_string(buf, end, ptr, spec);
865
+
866
+ return ptr_to_id(buf, end, ptr, spec);
867
+}
868
+
869
+int kptr_restrict __read_mostly;
870
+
871
+static noinline_for_stack
872
+char *restricted_pointer(char *buf, char *end, const void *ptr,
873
+ struct printf_spec spec)
874
+{
875
+ switch (kptr_restrict) {
876
+ case 0:
877
+ /* Handle as %p, hash and do _not_ leak addresses. */
878
+ return default_pointer(buf, end, ptr, spec);
879
+ case 1: {
880
+ const struct cred *cred;
881
+
882
+ /*
883
+ * kptr_restrict==1 cannot be used in IRQ context
884
+ * because its test for CAP_SYSLOG would be meaningless.
885
+ */
886
+ if (in_irq() || in_serving_softirq() || in_nmi()) {
887
+ if (spec.field_width == -1)
888
+ spec.field_width = 2 * sizeof(ptr);
889
+ return error_string(buf, end, "pK-error", spec);
890
+ }
891
+
892
+ /*
893
+ * Only print the real pointer value if the current
894
+ * process has CAP_SYSLOG and is running with the
895
+ * same credentials it started with. This is because
896
+ * access to files is checked at open() time, but %pK
897
+ * checks permission at read() time. We don't want to
898
+ * leak pointer values if a binary opens a file using
899
+ * %pK and then elevates privileges before reading it.
900
+ */
901
+ cred = current_cred();
902
+ if (!has_capability_noaudit(current, CAP_SYSLOG) ||
903
+ !uid_eq(cred->euid, cred->uid) ||
904
+ !gid_eq(cred->egid, cred->gid))
905
+ ptr = NULL;
906
+ break;
907
+ }
908
+ case 2:
909
+ default:
910
+ /* Always print 0's for %pK */
911
+ ptr = NULL;
912
+ break;
913
+ }
914
+
915
+ return pointer_string(buf, end, ptr, spec);
639916 }
640917
641918 static noinline_for_stack
....@@ -657,6 +934,11 @@
657934
658935 rcu_read_lock();
659936 for (i = 0; i < depth; i++, d = p) {
937
+ if (check_pointer(&buf, end, d, spec)) {
938
+ rcu_read_unlock();
939
+ return buf;
940
+ }
941
+
660942 p = READ_ONCE(d->d_parent);
661943 array[i] = READ_ONCE(d->d_name.name);
662944 if (p == d) {
....@@ -682,21 +964,34 @@
682964 return widen_string(buf, n, end, spec);
683965 }
684966
967
+static noinline_for_stack
968
+char *file_dentry_name(char *buf, char *end, const struct file *f,
969
+ struct printf_spec spec, const char *fmt)
970
+{
971
+ if (check_pointer(&buf, end, f, spec))
972
+ return buf;
973
+
974
+ return dentry_name(buf, end, f->f_path.dentry, spec, fmt);
975
+}
685976 #ifdef CONFIG_BLOCK
686977 static noinline_for_stack
687978 char *bdev_name(char *buf, char *end, struct block_device *bdev,
688979 struct printf_spec spec, const char *fmt)
689980 {
690
- struct gendisk *hd = bdev->bd_disk;
691
-
981
+ struct gendisk *hd;
982
+
983
+ if (check_pointer(&buf, end, bdev, spec))
984
+ return buf;
985
+
986
+ hd = bdev->bd_disk;
692987 buf = string(buf, end, hd->disk_name, spec);
693
- if (bdev->bd_part->partno) {
988
+ if (bdev->bd_partno) {
694989 if (isdigit(hd->disk_name[strlen(hd->disk_name)-1])) {
695990 if (buf < end)
696991 *buf = 'p';
697992 buf++;
698993 }
699
- buf = number(buf, end, bdev->bd_part->partno, spec);
994
+ buf = number(buf, end, bdev->bd_partno, spec);
700995 }
701996 return buf;
702997 }
....@@ -718,12 +1013,12 @@
7181013 #ifdef CONFIG_KALLSYMS
7191014 if (*fmt == 'B')
7201015 sprint_backtrace(sym, value);
721
- else if (*fmt != 'f' && *fmt != 's')
1016
+ else if (*fmt != 's')
7221017 sprint_symbol(sym, value);
7231018 else
7241019 sprint_symbol_no_offset(sym, value);
7251020
726
- return string(buf, end, sym, spec);
1021
+ return string_nocheck(buf, end, sym, spec);
7271022 #else
7281023 return special_hex_number(buf, end, value, sizeof(void *));
7291024 #endif
....@@ -743,6 +1038,20 @@
7431038 static const struct printf_spec default_dec_spec = {
7441039 .base = 10,
7451040 .precision = -1,
1041
+};
1042
+
1043
+static const struct printf_spec default_dec02_spec = {
1044
+ .base = 10,
1045
+ .field_width = 2,
1046
+ .precision = -1,
1047
+ .flags = ZEROPAD,
1048
+};
1049
+
1050
+static const struct printf_spec default_dec04_spec = {
1051
+ .base = 10,
1052
+ .field_width = 4,
1053
+ .precision = -1,
1054
+ .flags = ZEROPAD,
7461055 };
7471056
7481057 static noinline_for_stack
....@@ -793,29 +1102,32 @@
7931102 int decode = (fmt[0] == 'R') ? 1 : 0;
7941103 const struct printf_spec *specp;
7951104
1105
+ if (check_pointer(&buf, end, res, spec))
1106
+ return buf;
1107
+
7961108 *p++ = '[';
7971109 if (res->flags & IORESOURCE_IO) {
798
- p = string(p, pend, "io ", str_spec);
1110
+ p = string_nocheck(p, pend, "io ", str_spec);
7991111 specp = &io_spec;
8001112 } else if (res->flags & IORESOURCE_MEM) {
801
- p = string(p, pend, "mem ", str_spec);
1113
+ p = string_nocheck(p, pend, "mem ", str_spec);
8021114 specp = &mem_spec;
8031115 } else if (res->flags & IORESOURCE_IRQ) {
804
- p = string(p, pend, "irq ", str_spec);
1116
+ p = string_nocheck(p, pend, "irq ", str_spec);
8051117 specp = &default_dec_spec;
8061118 } else if (res->flags & IORESOURCE_DMA) {
807
- p = string(p, pend, "dma ", str_spec);
1119
+ p = string_nocheck(p, pend, "dma ", str_spec);
8081120 specp = &default_dec_spec;
8091121 } else if (res->flags & IORESOURCE_BUS) {
810
- p = string(p, pend, "bus ", str_spec);
1122
+ p = string_nocheck(p, pend, "bus ", str_spec);
8111123 specp = &bus_spec;
8121124 } else {
813
- p = string(p, pend, "??? ", str_spec);
1125
+ p = string_nocheck(p, pend, "??? ", str_spec);
8141126 specp = &mem_spec;
8151127 decode = 0;
8161128 }
8171129 if (decode && res->flags & IORESOURCE_UNSET) {
818
- p = string(p, pend, "size ", str_spec);
1130
+ p = string_nocheck(p, pend, "size ", str_spec);
8191131 p = number(p, pend, resource_size(res), *specp);
8201132 } else {
8211133 p = number(p, pend, res->start, *specp);
....@@ -826,21 +1138,21 @@
8261138 }
8271139 if (decode) {
8281140 if (res->flags & IORESOURCE_MEM_64)
829
- p = string(p, pend, " 64bit", str_spec);
1141
+ p = string_nocheck(p, pend, " 64bit", str_spec);
8301142 if (res->flags & IORESOURCE_PREFETCH)
831
- p = string(p, pend, " pref", str_spec);
1143
+ p = string_nocheck(p, pend, " pref", str_spec);
8321144 if (res->flags & IORESOURCE_WINDOW)
833
- p = string(p, pend, " window", str_spec);
1145
+ p = string_nocheck(p, pend, " window", str_spec);
8341146 if (res->flags & IORESOURCE_DISABLED)
835
- p = string(p, pend, " disabled", str_spec);
1147
+ p = string_nocheck(p, pend, " disabled", str_spec);
8361148 } else {
837
- p = string(p, pend, " flags ", str_spec);
1149
+ p = string_nocheck(p, pend, " flags ", str_spec);
8381150 p = number(p, pend, res->flags, default_flag_spec);
8391151 }
8401152 *p++ = ']';
8411153 *p = '\0';
8421154
843
- return string(buf, end, sym, spec);
1155
+ return string_nocheck(buf, end, sym, spec);
8441156 }
8451157
8461158 static noinline_for_stack
....@@ -855,9 +1167,8 @@
8551167 /* nothing to print */
8561168 return buf;
8571169
858
- if (ZERO_OR_NULL_PTR(addr))
859
- /* NULL pointer */
860
- return string(buf, end, NULL, spec);
1170
+ if (check_pointer(&buf, end, addr, spec))
1171
+ return buf;
8611172
8621173 switch (fmt[1]) {
8631174 case 'C':
....@@ -904,6 +1215,9 @@
9041215 int i, chunksz;
9051216 bool first = true;
9061217
1218
+ if (check_pointer(&buf, end, bitmap, spec))
1219
+ return buf;
1220
+
9071221 /* reused to print numbers */
9081222 spec = (struct printf_spec){ .flags = SMALL | ZEROPAD, .base = 16 };
9091223
....@@ -945,6 +1259,9 @@
9451259 int cur, rbot, rtop;
9461260 bool first = true;
9471261
1262
+ if (check_pointer(&buf, end, bitmap, spec))
1263
+ return buf;
1264
+
9481265 rbot = cur = find_first_bit(bitmap, nr_bits);
9491266 while (cur < nr_bits) {
9501267 rtop = cur;
....@@ -983,6 +1300,9 @@
9831300 char separator;
9841301 bool reversed = false;
9851302
1303
+ if (check_pointer(&buf, end, addr, spec))
1304
+ return buf;
1305
+
9861306 switch (fmt[1]) {
9871307 case 'F':
9881308 separator = '-';
....@@ -1008,7 +1328,7 @@
10081328 }
10091329 *p = '\0';
10101330
1011
- return string(buf, end, mac_addr, spec);
1331
+ return string_nocheck(buf, end, mac_addr, spec);
10121332 }
10131333
10141334 static noinline_for_stack
....@@ -1171,7 +1491,7 @@
11711491 else
11721492 ip6_string(ip6_addr, addr, fmt);
11731493
1174
- return string(buf, end, ip6_addr, spec);
1494
+ return string_nocheck(buf, end, ip6_addr, spec);
11751495 }
11761496
11771497 static noinline_for_stack
....@@ -1182,7 +1502,7 @@
11821502
11831503 ip4_string(ip4_addr, addr, fmt);
11841504
1185
- return string(buf, end, ip4_addr, spec);
1505
+ return string_nocheck(buf, end, ip4_addr, spec);
11861506 }
11871507
11881508 static noinline_for_stack
....@@ -1244,7 +1564,7 @@
12441564 }
12451565 *p = '\0';
12461566
1247
- return string(buf, end, ip6_addr, spec);
1567
+ return string_nocheck(buf, end, ip6_addr, spec);
12481568 }
12491569
12501570 static noinline_for_stack
....@@ -1279,7 +1599,42 @@
12791599 }
12801600 *p = '\0';
12811601
1282
- return string(buf, end, ip4_addr, spec);
1602
+ return string_nocheck(buf, end, ip4_addr, spec);
1603
+}
1604
+
1605
+static noinline_for_stack
1606
+char *ip_addr_string(char *buf, char *end, const void *ptr,
1607
+ struct printf_spec spec, const char *fmt)
1608
+{
1609
+ char *err_fmt_msg;
1610
+
1611
+ if (check_pointer(&buf, end, ptr, spec))
1612
+ return buf;
1613
+
1614
+ switch (fmt[1]) {
1615
+ case '6':
1616
+ return ip6_addr_string(buf, end, ptr, spec, fmt);
1617
+ case '4':
1618
+ return ip4_addr_string(buf, end, ptr, spec, fmt);
1619
+ case 'S': {
1620
+ const union {
1621
+ struct sockaddr raw;
1622
+ struct sockaddr_in v4;
1623
+ struct sockaddr_in6 v6;
1624
+ } *sa = ptr;
1625
+
1626
+ switch (sa->raw.sa_family) {
1627
+ case AF_INET:
1628
+ return ip4_addr_string_sa(buf, end, &sa->v4, spec, fmt);
1629
+ case AF_INET6:
1630
+ return ip6_addr_string_sa(buf, end, &sa->v6, spec, fmt);
1631
+ default:
1632
+ return error_string(buf, end, "(einval)", spec);
1633
+ }}
1634
+ }
1635
+
1636
+ err_fmt_msg = fmt[0] == 'i' ? "(%pi?)" : "(%pI?)";
1637
+ return error_string(buf, end, err_fmt_msg, spec);
12831638 }
12841639
12851640 static noinline_for_stack
....@@ -1294,9 +1649,8 @@
12941649 if (spec.field_width == 0)
12951650 return buf; /* nothing to print */
12961651
1297
- if (ZERO_OR_NULL_PTR(addr))
1298
- return string(buf, end, NULL, spec); /* NULL pointer */
1299
-
1652
+ if (check_pointer(&buf, end, addr, spec))
1653
+ return buf;
13001654
13011655 do {
13021656 switch (fmt[count++]) {
....@@ -1342,6 +1696,21 @@
13421696 return buf;
13431697 }
13441698
1699
+static char *va_format(char *buf, char *end, struct va_format *va_fmt,
1700
+ struct printf_spec spec, const char *fmt)
1701
+{
1702
+ va_list va;
1703
+
1704
+ if (check_pointer(&buf, end, va_fmt, spec))
1705
+ return buf;
1706
+
1707
+ va_copy(va, *va_fmt->va);
1708
+ buf += vsnprintf(buf, end > buf ? end - buf : 0, va_fmt->fmt, va);
1709
+ va_end(va);
1710
+
1711
+ return buf;
1712
+}
1713
+
13451714 static noinline_for_stack
13461715 char *uuid_string(char *buf, char *end, const u8 *addr,
13471716 struct printf_spec spec, const char *fmt)
....@@ -1352,9 +1721,13 @@
13521721 const u8 *index = uuid_index;
13531722 bool uc = false;
13541723
1724
+ if (check_pointer(&buf, end, addr, spec))
1725
+ return buf;
1726
+
13551727 switch (*(++fmt)) {
13561728 case 'L':
1357
- uc = true; /* fall-through */
1729
+ uc = true;
1730
+ /* fall through */
13581731 case 'l':
13591732 index = guid_index;
13601733 break;
....@@ -1380,77 +1753,18 @@
13801753
13811754 *p = 0;
13821755
1383
- return string(buf, end, uuid, spec);
1756
+ return string_nocheck(buf, end, uuid, spec);
13841757 }
13851758
13861759 static noinline_for_stack
1387
-char *pointer_string(char *buf, char *end, const void *ptr,
1388
- struct printf_spec spec)
1389
-{
1390
- spec.base = 16;
1391
- spec.flags |= SMALL;
1392
- if (spec.field_width == -1) {
1393
- spec.field_width = 2 * sizeof(ptr);
1394
- spec.flags |= ZEROPAD;
1395
- }
1396
-
1397
- return number(buf, end, (unsigned long int)ptr, spec);
1398
-}
1399
-
1400
-int kptr_restrict __read_mostly;
1401
-
1402
-static noinline_for_stack
1403
-char *restricted_pointer(char *buf, char *end, const void *ptr,
1404
- struct printf_spec spec)
1405
-{
1406
- switch (kptr_restrict) {
1407
- case 0:
1408
- /* Always print %pK values */
1409
- break;
1410
- case 1: {
1411
- const struct cred *cred;
1412
-
1413
- /*
1414
- * kptr_restrict==1 cannot be used in IRQ context
1415
- * because its test for CAP_SYSLOG would be meaningless.
1416
- */
1417
- if (in_irq() || in_serving_softirq() || in_nmi()) {
1418
- if (spec.field_width == -1)
1419
- spec.field_width = 2 * sizeof(ptr);
1420
- return string(buf, end, "pK-error", spec);
1421
- }
1422
-
1423
- /*
1424
- * Only print the real pointer value if the current
1425
- * process has CAP_SYSLOG and is running with the
1426
- * same credentials it started with. This is because
1427
- * access to files is checked at open() time, but %pK
1428
- * checks permission at read() time. We don't want to
1429
- * leak pointer values if a binary opens a file using
1430
- * %pK and then elevates privileges before reading it.
1431
- */
1432
- cred = current_cred();
1433
- if (!has_capability_noaudit(current, CAP_SYSLOG) ||
1434
- !uid_eq(cred->euid, cred->uid) ||
1435
- !gid_eq(cred->egid, cred->gid))
1436
- ptr = NULL;
1437
- break;
1438
- }
1439
- case 2:
1440
- default:
1441
- /* Always print 0's for %pK */
1442
- ptr = NULL;
1443
- break;
1444
- }
1445
-
1446
- return pointer_string(buf, end, ptr, spec);
1447
-}
1448
-
1449
-static noinline_for_stack
1450
-char *netdev_bits(char *buf, char *end, const void *addr, const char *fmt)
1760
+char *netdev_bits(char *buf, char *end, const void *addr,
1761
+ struct printf_spec spec, const char *fmt)
14511762 {
14521763 unsigned long long num;
14531764 int size;
1765
+
1766
+ if (check_pointer(&buf, end, addr, spec))
1767
+ return buf;
14541768
14551769 switch (fmt[1]) {
14561770 case 'F':
....@@ -1458,19 +1772,21 @@
14581772 size = sizeof(netdev_features_t);
14591773 break;
14601774 default:
1461
- num = (unsigned long)addr;
1462
- size = sizeof(unsigned long);
1463
- break;
1775
+ return error_string(buf, end, "(%pN?)", spec);
14641776 }
14651777
14661778 return special_hex_number(buf, end, num, size);
14671779 }
14681780
14691781 static noinline_for_stack
1470
-char *address_val(char *buf, char *end, const void *addr, const char *fmt)
1782
+char *address_val(char *buf, char *end, const void *addr,
1783
+ struct printf_spec spec, const char *fmt)
14711784 {
14721785 unsigned long long num;
14731786 int size;
1787
+
1788
+ if (check_pointer(&buf, end, addr, spec))
1789
+ return buf;
14741790
14751791 switch (fmt[1]) {
14761792 case 'd':
....@@ -1488,11 +1804,124 @@
14881804 }
14891805
14901806 static noinline_for_stack
1807
+char *date_str(char *buf, char *end, const struct rtc_time *tm, bool r)
1808
+{
1809
+ int year = tm->tm_year + (r ? 0 : 1900);
1810
+ int mon = tm->tm_mon + (r ? 0 : 1);
1811
+
1812
+ buf = number(buf, end, year, default_dec04_spec);
1813
+ if (buf < end)
1814
+ *buf = '-';
1815
+ buf++;
1816
+
1817
+ buf = number(buf, end, mon, default_dec02_spec);
1818
+ if (buf < end)
1819
+ *buf = '-';
1820
+ buf++;
1821
+
1822
+ return number(buf, end, tm->tm_mday, default_dec02_spec);
1823
+}
1824
+
1825
+static noinline_for_stack
1826
+char *time_str(char *buf, char *end, const struct rtc_time *tm, bool r)
1827
+{
1828
+ buf = number(buf, end, tm->tm_hour, default_dec02_spec);
1829
+ if (buf < end)
1830
+ *buf = ':';
1831
+ buf++;
1832
+
1833
+ buf = number(buf, end, tm->tm_min, default_dec02_spec);
1834
+ if (buf < end)
1835
+ *buf = ':';
1836
+ buf++;
1837
+
1838
+ return number(buf, end, tm->tm_sec, default_dec02_spec);
1839
+}
1840
+
1841
+static noinline_for_stack
1842
+char *rtc_str(char *buf, char *end, const struct rtc_time *tm,
1843
+ struct printf_spec spec, const char *fmt)
1844
+{
1845
+ bool have_t = true, have_d = true;
1846
+ bool raw = false;
1847
+ int count = 2;
1848
+
1849
+ if (check_pointer(&buf, end, tm, spec))
1850
+ return buf;
1851
+
1852
+ switch (fmt[count]) {
1853
+ case 'd':
1854
+ have_t = false;
1855
+ count++;
1856
+ break;
1857
+ case 't':
1858
+ have_d = false;
1859
+ count++;
1860
+ break;
1861
+ }
1862
+
1863
+ raw = fmt[count] == 'r';
1864
+
1865
+ if (have_d)
1866
+ buf = date_str(buf, end, tm, raw);
1867
+ if (have_d && have_t) {
1868
+ /* Respect ISO 8601 */
1869
+ if (buf < end)
1870
+ *buf = 'T';
1871
+ buf++;
1872
+ }
1873
+ if (have_t)
1874
+ buf = time_str(buf, end, tm, raw);
1875
+
1876
+ return buf;
1877
+}
1878
+
1879
+static noinline_for_stack
1880
+char *time64_str(char *buf, char *end, const time64_t time,
1881
+ struct printf_spec spec, const char *fmt)
1882
+{
1883
+ struct rtc_time rtc_time;
1884
+ struct tm tm;
1885
+
1886
+ time64_to_tm(time, 0, &tm);
1887
+
1888
+ rtc_time.tm_sec = tm.tm_sec;
1889
+ rtc_time.tm_min = tm.tm_min;
1890
+ rtc_time.tm_hour = tm.tm_hour;
1891
+ rtc_time.tm_mday = tm.tm_mday;
1892
+ rtc_time.tm_mon = tm.tm_mon;
1893
+ rtc_time.tm_year = tm.tm_year;
1894
+ rtc_time.tm_wday = tm.tm_wday;
1895
+ rtc_time.tm_yday = tm.tm_yday;
1896
+
1897
+ rtc_time.tm_isdst = 0;
1898
+
1899
+ return rtc_str(buf, end, &rtc_time, spec, fmt);
1900
+}
1901
+
1902
+static noinline_for_stack
1903
+char *time_and_date(char *buf, char *end, void *ptr, struct printf_spec spec,
1904
+ const char *fmt)
1905
+{
1906
+ switch (fmt[1]) {
1907
+ case 'R':
1908
+ return rtc_str(buf, end, (const struct rtc_time *)ptr, spec, fmt);
1909
+ case 'T':
1910
+ return time64_str(buf, end, *(const time64_t *)ptr, spec, fmt);
1911
+ default:
1912
+ return error_string(buf, end, "(%pt?)", spec);
1913
+ }
1914
+}
1915
+
1916
+static noinline_for_stack
14911917 char *clock(char *buf, char *end, struct clk *clk, struct printf_spec spec,
14921918 const char *fmt)
14931919 {
1494
- if (!IS_ENABLED(CONFIG_HAVE_CLK) || !clk)
1495
- return string(buf, end, NULL, spec);
1920
+ if (!IS_ENABLED(CONFIG_HAVE_CLK))
1921
+ return error_string(buf, end, "(%pC?)", spec);
1922
+
1923
+ if (check_pointer(&buf, end, clk, spec))
1924
+ return buf;
14961925
14971926 switch (fmt[1]) {
14981927 case 'n':
....@@ -1500,7 +1929,7 @@
15001929 #ifdef CONFIG_COMMON_CLK
15011930 return string(buf, end, __clk_get_name(clk), spec);
15021931 #else
1503
- return special_hex_number(buf, end, (unsigned long)clk, sizeof(unsigned long));
1932
+ return ptr_to_id(buf, end, clk, spec);
15041933 #endif
15051934 }
15061935 }
....@@ -1533,10 +1962,14 @@
15331962 }
15341963
15351964 static noinline_for_stack
1536
-char *flags_string(char *buf, char *end, void *flags_ptr, const char *fmt)
1965
+char *flags_string(char *buf, char *end, void *flags_ptr,
1966
+ struct printf_spec spec, const char *fmt)
15371967 {
15381968 unsigned long flags;
15391969 const struct trace_print_flags *names;
1970
+
1971
+ if (check_pointer(&buf, end, flags_ptr, spec))
1972
+ return buf;
15401973
15411974 switch (fmt[1]) {
15421975 case 'p':
....@@ -1550,43 +1983,35 @@
15501983 names = vmaflag_names;
15511984 break;
15521985 case 'g':
1553
- flags = *(gfp_t *)flags_ptr;
1986
+ flags = (__force unsigned long)(*(gfp_t *)flags_ptr);
15541987 names = gfpflag_names;
15551988 break;
15561989 default:
1557
- WARN_ONCE(1, "Unsupported flags modifier: %c\n", fmt[1]);
1558
- return buf;
1990
+ return error_string(buf, end, "(%pG?)", spec);
15591991 }
15601992
15611993 return format_flags(buf, end, flags, names);
15621994 }
15631995
1564
-static const char *device_node_name_for_depth(const struct device_node *np, int depth)
1565
-{
1566
- for ( ; np && depth; depth--)
1567
- np = np->parent;
1568
-
1569
- return kbasename(np->full_name);
1570
-}
1571
-
15721996 static noinline_for_stack
1573
-char *device_node_gen_full_name(const struct device_node *np, char *buf, char *end)
1997
+char *fwnode_full_name_string(struct fwnode_handle *fwnode, char *buf,
1998
+ char *end)
15741999 {
15752000 int depth;
1576
- const struct device_node *parent = np->parent;
15772001
1578
- /* special case for root node */
1579
- if (!parent)
1580
- return string(buf, end, "/", default_str_spec);
2002
+ /* Loop starting from the root node to the current node. */
2003
+ for (depth = fwnode_count_parents(fwnode); depth >= 0; depth--) {
2004
+ struct fwnode_handle *__fwnode =
2005
+ fwnode_get_nth_parent(fwnode, depth);
15812006
1582
- for (depth = 0; parent->parent; depth++)
1583
- parent = parent->parent;
1584
-
1585
- for ( ; depth >= 0; depth--) {
1586
- buf = string(buf, end, "/", default_str_spec);
1587
- buf = string(buf, end, device_node_name_for_depth(np, depth),
2007
+ buf = string(buf, end, fwnode_get_name_prefix(__fwnode),
15882008 default_str_spec);
2009
+ buf = string(buf, end, fwnode_get_name(__fwnode),
2010
+ default_str_spec);
2011
+
2012
+ fwnode_handle_put(__fwnode);
15892013 }
2014
+
15902015 return buf;
15912016 }
15922017
....@@ -1600,21 +2025,18 @@
16002025 char *buf_start = buf;
16012026 struct property *prop;
16022027 bool has_mult, pass;
1603
- static const struct printf_spec num_spec = {
1604
- .flags = SMALL,
1605
- .field_width = -1,
1606
- .precision = -1,
1607
- .base = 10,
1608
- };
16092028
16102029 struct printf_spec str_spec = spec;
16112030 str_spec.field_width = -1;
16122031
1613
- if (!IS_ENABLED(CONFIG_OF))
1614
- return string(buf, end, "(!OF)", spec);
2032
+ if (fmt[0] != 'F')
2033
+ return error_string(buf, end, "(%pO?)", spec);
16152034
1616
- if ((unsigned long)dn < PAGE_SIZE)
1617
- return string(buf, end, "(null)", spec);
2035
+ if (!IS_ENABLED(CONFIG_OF))
2036
+ return error_string(buf, end, "(%pOF?)", spec);
2037
+
2038
+ if (check_pointer(&buf, end, dn, spec))
2039
+ return buf;
16182040
16192041 /* simple case without anything any more format specifiers */
16202042 fmt++;
....@@ -1622,6 +2044,7 @@
16222044 fmt = "f";
16232045
16242046 for (pass = false; strspn(fmt,"fnpPFcC"); fmt++, pass = true) {
2047
+ int precision;
16252048 if (pass) {
16262049 if (buf < end)
16272050 *buf = ':';
....@@ -1630,16 +2053,21 @@
16302053
16312054 switch (*fmt) {
16322055 case 'f': /* full_name */
1633
- buf = device_node_gen_full_name(dn, buf, end);
2056
+ buf = fwnode_full_name_string(of_fwnode_handle(dn), buf,
2057
+ end);
16342058 break;
16352059 case 'n': /* name */
1636
- buf = string(buf, end, dn->name, str_spec);
2060
+ p = fwnode_get_name(of_fwnode_handle(dn));
2061
+ precision = str_spec.precision;
2062
+ str_spec.precision = strchrnul(p, '@') - p;
2063
+ buf = string(buf, end, p, str_spec);
2064
+ str_spec.precision = precision;
16372065 break;
16382066 case 'p': /* phandle */
1639
- buf = number(buf, end, (unsigned int)dn->phandle, num_spec);
2067
+ buf = number(buf, end, (unsigned int)dn->phandle, default_dec_spec);
16402068 break;
16412069 case 'P': /* path-spec */
1642
- p = kbasename(of_node_full_name(dn));
2070
+ p = fwnode_get_name(of_fwnode_handle(dn));
16432071 if (!p[1])
16442072 p = "/";
16452073 buf = string(buf, end, p, str_spec);
....@@ -1650,7 +2078,7 @@
16502078 tbuf[2] = of_node_check_flag(dn, OF_POPULATED) ? 'P' : '-';
16512079 tbuf[3] = of_node_check_flag(dn, OF_POPULATED_BUS) ? 'B' : '-';
16522080 tbuf[4] = 0;
1653
- buf = string(buf, end, tbuf, str_spec);
2081
+ buf = string_nocheck(buf, end, tbuf, str_spec);
16542082 break;
16552083 case 'c': /* major compatible string */
16562084 ret = of_property_read_string(dn, "compatible", &p);
....@@ -1661,10 +2089,10 @@
16612089 has_mult = false;
16622090 of_property_for_each_string(dn, "compatible", prop, p) {
16632091 if (has_mult)
1664
- buf = string(buf, end, ",", str_spec);
1665
- buf = string(buf, end, "\"", str_spec);
2092
+ buf = string_nocheck(buf, end, ",", str_spec);
2093
+ buf = string_nocheck(buf, end, "\"", str_spec);
16662094 buf = string(buf, end, p, str_spec);
1667
- buf = string(buf, end, "\"", str_spec);
2095
+ buf = string_nocheck(buf, end, "\"", str_spec);
16682096
16692097 has_mult = true;
16702098 }
....@@ -1677,112 +2105,57 @@
16772105 return widen_string(buf, buf - buf_start, end, spec);
16782106 }
16792107
1680
-/* Make pointers available for printing early in the boot sequence. */
1681
-static int debug_boot_weak_hash __ro_after_init;
1682
-
1683
-static int __init debug_boot_weak_hash_enable(char *str)
2108
+static noinline_for_stack
2109
+char *fwnode_string(char *buf, char *end, struct fwnode_handle *fwnode,
2110
+ struct printf_spec spec, const char *fmt)
16842111 {
1685
- debug_boot_weak_hash = 1;
1686
- pr_info("debug_boot_weak_hash enabled\n");
2112
+ struct printf_spec str_spec = spec;
2113
+ char *buf_start = buf;
2114
+
2115
+ str_spec.field_width = -1;
2116
+
2117
+ if (*fmt != 'w')
2118
+ return error_string(buf, end, "(%pf?)", spec);
2119
+
2120
+ if (check_pointer(&buf, end, fwnode, spec))
2121
+ return buf;
2122
+
2123
+ fmt++;
2124
+
2125
+ switch (*fmt) {
2126
+ case 'P': /* name */
2127
+ buf = string(buf, end, fwnode_get_name(fwnode), str_spec);
2128
+ break;
2129
+ case 'f': /* full_name */
2130
+ default:
2131
+ buf = fwnode_full_name_string(fwnode, buf, end);
2132
+ break;
2133
+ }
2134
+
2135
+ return widen_string(buf, buf - buf_start, end, spec);
2136
+}
2137
+
2138
+static int __init no_hash_pointers_enable(char *str)
2139
+{
2140
+ no_hash_pointers = true;
2141
+
2142
+ pr_warn("**********************************************************\n");
2143
+ pr_warn("** NOTICE NOTICE NOTICE NOTICE NOTICE NOTICE NOTICE **\n");
2144
+ pr_warn("** **\n");
2145
+ pr_warn("** This system shows unhashed kernel memory addresses **\n");
2146
+ pr_warn("** via the console, logs, and other interfaces. This **\n");
2147
+ pr_warn("** might reduce the security of your system. **\n");
2148
+ pr_warn("** **\n");
2149
+ pr_warn("** If you see this message and you are not debugging **\n");
2150
+ pr_warn("** the kernel, report this immediately to your system **\n");
2151
+ pr_warn("** administrator! **\n");
2152
+ pr_warn("** **\n");
2153
+ pr_warn("** NOTICE NOTICE NOTICE NOTICE NOTICE NOTICE NOTICE **\n");
2154
+ pr_warn("**********************************************************\n");
2155
+
16872156 return 0;
16882157 }
1689
-early_param("debug_boot_weak_hash", debug_boot_weak_hash_enable);
1690
-
1691
-static DEFINE_STATIC_KEY_TRUE(not_filled_random_ptr_key);
1692
-static siphash_key_t ptr_key __read_mostly;
1693
-
1694
-static void enable_ptr_key_workfn(struct work_struct *work)
1695
-{
1696
- get_random_bytes(&ptr_key, sizeof(ptr_key));
1697
- /* Needs to run from preemptible context */
1698
- static_branch_disable(&not_filled_random_ptr_key);
1699
-}
1700
-
1701
-static DECLARE_WORK(enable_ptr_key_work, enable_ptr_key_workfn);
1702
-
1703
-static void fill_random_ptr_key(struct random_ready_callback *unused)
1704
-{
1705
- /* This may be in an interrupt handler. */
1706
- queue_work(system_unbound_wq, &enable_ptr_key_work);
1707
-}
1708
-
1709
-static struct random_ready_callback random_ready = {
1710
- .func = fill_random_ptr_key
1711
-};
1712
-
1713
-static int __init initialize_ptr_random(void)
1714
-{
1715
- int key_size = sizeof(ptr_key);
1716
- int ret;
1717
-
1718
- /* Use hw RNG if available. */
1719
- if (get_random_bytes_arch(&ptr_key, key_size) == key_size) {
1720
- static_branch_disable(&not_filled_random_ptr_key);
1721
- return 0;
1722
- }
1723
-
1724
- ret = add_random_ready_callback(&random_ready);
1725
- if (!ret) {
1726
- return 0;
1727
- } else if (ret == -EALREADY) {
1728
- /* This is in preemptible context */
1729
- enable_ptr_key_workfn(&enable_ptr_key_work);
1730
- return 0;
1731
- }
1732
-
1733
- return ret;
1734
-}
1735
-early_initcall(initialize_ptr_random);
1736
-
1737
-static inline int __ptr_to_hashval(const void *ptr, unsigned long *hashval_out)
1738
-{
1739
- unsigned long hashval;
1740
-
1741
- if (static_branch_unlikely(&not_filled_random_ptr_key))
1742
- return -EAGAIN;
1743
-
1744
-#ifdef CONFIG_64BIT
1745
- hashval = (unsigned long)siphash_1u64((u64)ptr, &ptr_key);
1746
- /*
1747
- * Mask off the first 32 bits, this makes explicit that we have
1748
- * modified the address (and 32 bits is plenty for a unique ID).
1749
- */
1750
- hashval = hashval & 0xffffffff;
1751
-#else
1752
- hashval = (unsigned long)siphash_1u32((u32)ptr, &ptr_key);
1753
-#endif
1754
- *hashval_out = hashval;
1755
- return 0;
1756
-}
1757
-
1758
-int ptr_to_hashval(const void *ptr, unsigned long *hashval_out)
1759
-{
1760
- return __ptr_to_hashval(ptr, hashval_out);
1761
-}
1762
-EXPORT_SYMBOL_GPL(ptr_to_hashval);
1763
-
1764
-/* Maps a pointer to a 32 bit unique identifier. */
1765
-static char *ptr_to_id(char *buf, char *end, void *ptr, struct printf_spec spec)
1766
-{
1767
- const char *str = sizeof(ptr) == 8 ? "(____ptrval____)" : "(ptrval)";
1768
- unsigned long hashval;
1769
- int ret;
1770
-
1771
- /* When debugging early boot use non-cryptographically secure hash. */
1772
- if (unlikely(debug_boot_weak_hash)) {
1773
- hashval = hash_long((unsigned long)ptr, 32);
1774
- return pointer_string(buf, end, (const void *)hashval, spec);
1775
- }
1776
-
1777
- ret = __ptr_to_hashval(ptr, &hashval);
1778
- if (ret) {
1779
- spec.field_width = 2 * sizeof(ptr);
1780
- /* string length must be less than default_width */
1781
- return string(buf, end, str, spec);
1782
- }
1783
-
1784
- return pointer_string(buf, end, (const void *)hashval, spec);
1785
-}
2158
+early_param("no_hash_pointers", no_hash_pointers_enable);
17862159
17872160 /*
17882161 * Show a '%p' thing. A kernel extension is that the '%p' is followed
....@@ -1796,9 +2169,9 @@
17962169 *
17972170 * - 'S' For symbolic direct pointers (or function descriptors) with offset
17982171 * - 's' For symbolic direct pointers (or function descriptors) without offset
1799
- * - 'F' Same as 'S'
1800
- * - 'f' Same as 's'
1801
- * - '[FfSs]R' as above with __builtin_extract_return_addr() translation
2172
+ * - '[Ss]R' as above with __builtin_extract_return_addr() translation
2173
+ * - '[Ff]' %pf and %pF were obsoleted and later removed in favor of
2174
+ * %ps and %pS. Be careful when re-using these specifiers.
18022175 * - 'B' For backtraced symbolic direct pointers with offset
18032176 * - 'R' For decoded struct resource, e.g., [mem 0x0-0x1f 64bit pref]
18042177 * - 'r' For raw struct resource, e.g., [mem 0x0-0x1f flags 0x201]
....@@ -1826,7 +2199,7 @@
18262199 * [4] or [6] and is able to print port [p], flowinfo [f], scope [s]
18272200 * - '[Ii][4S][hnbl]' IPv4 addresses in host, network, big or little endian order
18282201 * - 'I[6S]c' for IPv6 addresses printed as specified by
1829
- * http://tools.ietf.org/html/rfc5952
2202
+ * https://tools.ietf.org/html/rfc5952
18302203 * - 'E[achnops]' For an escaped buffer, where rules are defined by combination
18312204 * of the following flags (see string_escape_mem() for the
18322205 * details):
....@@ -1868,28 +2241,36 @@
18682241 * - 'd[234]' For a dentry name (optionally 2-4 last components)
18692242 * - 'D[234]' Same as 'd' but for a struct file
18702243 * - 'g' For block_device name (gendisk + partition number)
2244
+ * - 't[RT][dt][r]' For time and date as represented by:
2245
+ * R struct rtc_time
2246
+ * T time64_t
18712247 * - 'C' For a clock, it prints the name (Common Clock Framework) or address
18722248 * (legacy clock framework) of the clock
18732249 * - 'Cn' For a clock, it prints the name (Common Clock Framework) or address
18742250 * (legacy clock framework) of the clock
1875
- * - 'Cr' For a clock, it prints the current rate of the clock
18762251 * - 'G' For flags to be printed as a collection of symbolic strings that would
18772252 * construct the specific value. Supported flags given by option:
18782253 * p page flags (see struct page) given as pointer to unsigned long
18792254 * g gfp flags (GFP_* and __GFP_*) given as pointer to gfp_t
18802255 * v vma flags (VM_*) given as pointer to unsigned long
1881
- * - 'O' For a kobject based struct. Must be one of the following:
1882
- * - 'OF[fnpPcCF]' For a device tree object
1883
- * Without any optional arguments prints the full_name
1884
- * f device node full_name
1885
- * n device node name
1886
- * p device node phandle
1887
- * P device node path spec (name + @unit)
1888
- * F device node flags
1889
- * c major compatible string
1890
- * C full compatible string
1891
- *
2256
+ * - 'OF[fnpPcCF]' For a device tree object
2257
+ * Without any optional arguments prints the full_name
2258
+ * f device node full_name
2259
+ * n device node name
2260
+ * p device node phandle
2261
+ * P device node path spec (name + @unit)
2262
+ * F device node flags
2263
+ * c major compatible string
2264
+ * C full compatible string
2265
+ * - 'fw[fP]' For a firmware node (struct fwnode_handle) pointer
2266
+ * Without an option prints the full name of the node
2267
+ * f full name
2268
+ * P node name, including a possible unit address
18922269 * - 'x' For printing the address. Equivalent to "%lx".
2270
+ * - '[ku]s' For a BPF/tracing related format specifier, e.g. used out of
2271
+ * bpf_trace_printk() where [ku] prefix specifies either kernel (k)
2272
+ * or user (u) memory to probe, and:
2273
+ * s a string, equivalent to "%s" on direct vsnprintf() use
18932274 *
18942275 * ** When making changes please also update:
18952276 * Documentation/core-api/printk-formats.rst
....@@ -1901,25 +2282,11 @@
19012282 char *pointer(const char *fmt, char *buf, char *end, void *ptr,
19022283 struct printf_spec spec)
19032284 {
1904
- const int default_width = 2 * sizeof(void *);
1905
-
1906
- if (!ptr && *fmt != 'K' && *fmt != 'x') {
1907
- /*
1908
- * Print (null) with the same width as a pointer so it makes
1909
- * tabular output look nice.
1910
- */
1911
- if (spec.field_width == -1)
1912
- spec.field_width = default_width;
1913
- return string(buf, end, "(null)", spec);
1914
- }
1915
-
19162285 switch (*fmt) {
1917
- case 'F':
1918
- case 'f':
19192286 case 'S':
19202287 case 's':
19212288 ptr = dereference_symbol_descriptor(ptr);
1922
- /* Fallthrough */
2289
+ /* fall through */
19232290 case 'B':
19242291 return symbol_string(buf, end, ptr, spec, fmt);
19252292 case 'R':
....@@ -1948,77 +2315,56 @@
19482315 * 4: 001.002.003.004
19492316 * 6: 000102...0f
19502317 */
1951
- switch (fmt[1]) {
1952
- case '6':
1953
- return ip6_addr_string(buf, end, ptr, spec, fmt);
1954
- case '4':
1955
- return ip4_addr_string(buf, end, ptr, spec, fmt);
1956
- case 'S': {
1957
- const union {
1958
- struct sockaddr raw;
1959
- struct sockaddr_in v4;
1960
- struct sockaddr_in6 v6;
1961
- } *sa = ptr;
1962
-
1963
- switch (sa->raw.sa_family) {
1964
- case AF_INET:
1965
- return ip4_addr_string_sa(buf, end, &sa->v4, spec, fmt);
1966
- case AF_INET6:
1967
- return ip6_addr_string_sa(buf, end, &sa->v6, spec, fmt);
1968
- default:
1969
- return string(buf, end, "(invalid address)", spec);
1970
- }}
1971
- }
1972
- break;
2318
+ return ip_addr_string(buf, end, ptr, spec, fmt);
19732319 case 'E':
19742320 return escaped_string(buf, end, ptr, spec, fmt);
19752321 case 'U':
19762322 return uuid_string(buf, end, ptr, spec, fmt);
19772323 case 'V':
1978
- {
1979
- va_list va;
1980
-
1981
- va_copy(va, *((struct va_format *)ptr)->va);
1982
- buf += vsnprintf(buf, end > buf ? end - buf : 0,
1983
- ((struct va_format *)ptr)->fmt, va);
1984
- va_end(va);
1985
- return buf;
1986
- }
2324
+ return va_format(buf, end, ptr, spec, fmt);
19872325 case 'K':
1988
- if (!kptr_restrict)
1989
- break;
19902326 return restricted_pointer(buf, end, ptr, spec);
19912327 case 'N':
1992
- return netdev_bits(buf, end, ptr, fmt);
2328
+ return netdev_bits(buf, end, ptr, spec, fmt);
19932329 case 'a':
1994
- return address_val(buf, end, ptr, fmt);
2330
+ return address_val(buf, end, ptr, spec, fmt);
19952331 case 'd':
19962332 return dentry_name(buf, end, ptr, spec, fmt);
2333
+ case 't':
2334
+ return time_and_date(buf, end, ptr, spec, fmt);
19972335 case 'C':
19982336 return clock(buf, end, ptr, spec, fmt);
19992337 case 'D':
2000
- return dentry_name(buf, end,
2001
- ((const struct file *)ptr)->f_path.dentry,
2002
- spec, fmt);
2338
+ return file_dentry_name(buf, end, ptr, spec, fmt);
20032339 #ifdef CONFIG_BLOCK
20042340 case 'g':
20052341 return bdev_name(buf, end, ptr, spec, fmt);
20062342 #endif
20072343
20082344 case 'G':
2009
- return flags_string(buf, end, ptr, fmt);
2345
+ return flags_string(buf, end, ptr, spec, fmt);
20102346 case 'O':
2011
- switch (fmt[1]) {
2012
- case 'F':
2013
- return device_node_string(buf, end, ptr, spec, fmt + 1);
2014
- }
2015
- break;
2347
+ return device_node_string(buf, end, ptr, spec, fmt + 1);
2348
+ case 'f':
2349
+ return fwnode_string(buf, end, ptr, spec, fmt + 1);
20162350 case 'x':
20172351 return pointer_string(buf, end, ptr, spec);
2352
+ case 'e':
2353
+ /* %pe with a non-ERR_PTR gets treated as plain %p */
2354
+ if (!IS_ERR(ptr))
2355
+ return default_pointer(buf, end, ptr, spec);
2356
+ return err_ptr(buf, end, ptr, spec);
2357
+ case 'u':
2358
+ case 'k':
2359
+ switch (fmt[1]) {
2360
+ case 's':
2361
+ return string(buf, end, ptr, spec);
2362
+ default:
2363
+ return error_string(buf, end, "(einval)", spec);
2364
+ }
2365
+ default:
2366
+ return default_pointer(buf, end, ptr, spec);
20182367 }
2019
-
2020
- /* default is to _not_ leak addresses, hash before printing */
2021
- return ptr_to_id(buf, end, ptr, spec);
20222368 }
20232369
20242370 /*
....@@ -2188,7 +2534,7 @@
21882534 * utility, treat it as any other invalid or
21892535 * unsupported format specifier.
21902536 */
2191
- /* Fall-through */
2537
+ /* fall through */
21922538
21932539 default:
21942540 WARN_ONCE(1, "Please remove unsupported %%%c in format string\n", *fmt);
....@@ -2627,11 +2973,13 @@
26272973
26282974 case FORMAT_TYPE_STR: {
26292975 const char *save_str = va_arg(args, char *);
2976
+ const char *err_msg;
26302977 size_t len;
26312978
2632
- if ((unsigned long)save_str > (unsigned long)-PAGE_SIZE
2633
- || (unsigned long)save_str < PAGE_SIZE)
2634
- save_str = "(null)";
2979
+ err_msg = check_pointer_msg(save_str);
2980
+ if (err_msg)
2981
+ save_str = err_msg;
2982
+
26352983 len = strlen(save_str) + 1;
26362984 if (str + len < end)
26372985 memcpy(str, save_str, len);
....@@ -2645,10 +2993,9 @@
26452993 /* Dereference of functions is still OK */
26462994 case 'S':
26472995 case 's':
2648
- case 'F':
2649
- case 'f':
26502996 case 'x':
26512997 case 'K':
2998
+ case 'e':
26522999 save_arg(void *);
26533000 break;
26543001 default:
....@@ -2821,10 +3168,9 @@
28213168 switch (*fmt) {
28223169 case 'S':
28233170 case 's':
2824
- case 'F':
2825
- case 'f':
28263171 case 'x':
28273172 case 'K':
3173
+ case 'e':
28283174 process = true;
28293175 break;
28303176 default: