1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * linux/lib/vsprintf.c 4 * 5 * Copyright (C) 1991, 1992 Linus Torvalds 6 */ 7 8 /* vsprintf.c -- Lars Wirzenius & Linus Torvalds. */ 9 /* 10 * Wirzenius wrote this portably, Torvalds fucked it up :-) 11 */ 12 13 /* 14 * Fri Jul 13 2001 Crutcher Dunnavant <crutcher+kernel@datastacks.com> 15 * - changed to provide snprintf and vsnprintf functions 16 * So Feb 1 16:51:32 CET 2004 Juergen Quade <quade@hsnr.de> 17 * - scnprintf and vscnprintf 18 */ 19 20 #include <linux/stdarg.h> 21 #include <linux/build_bug.h> 22 #include <linux/clk.h> 23 #include <linux/clk-provider.h> 24 #include <linux/errname.h> 25 #include <linux/module.h> /* for KSYM_SYMBOL_LEN */ 26 #include <linux/types.h> 27 #include <linux/string.h> 28 #include <linux/ctype.h> 29 #include <linux/kernel.h> 30 #include <linux/kallsyms.h> 31 #include <linux/math64.h> 32 #include <linux/uaccess.h> 33 #include <linux/ioport.h> 34 #include <linux/dcache.h> 35 #include <linux/cred.h> 36 #include <linux/rtc.h> 37 #include <linux/sprintf.h> 38 #include <linux/time.h> 39 #include <linux/uuid.h> 40 #include <linux/of.h> 41 #include <net/addrconf.h> 42 #include <linux/siphash.h> 43 #include <linux/compiler.h> 44 #include <linux/property.h> 45 #include <linux/notifier.h> 46 #ifdef CONFIG_BLOCK 47 #include <linux/blkdev.h> 48 #endif 49 50 #include "../mm/internal.h" /* For the trace_print_flags arrays */ 51 52 #include <asm/page.h> /* for PAGE_SIZE */ 53 #include <asm/byteorder.h> /* cpu_to_le16 */ 54 #include <linux/unaligned.h> 55 56 #include <linux/string_helpers.h> 57 #include "kstrtox.h" 58 59 /* Disable pointer hashing if requested */ 60 bool no_hash_pointers __ro_after_init; 61 EXPORT_SYMBOL_GPL(no_hash_pointers); 62 63 noinline 64 static unsigned long long simple_strntoull(const char *startp, char **endp, unsigned int base, size_t max_chars) 65 { 66 const char *cp; 67 unsigned long long result = 0ULL; 68 size_t prefix_chars; 69 unsigned int rv; 70 71 cp = _parse_integer_fixup_radix(startp, &base); 72 prefix_chars = cp - startp; 73 if (prefix_chars < max_chars) { 74 rv = _parse_integer_limit(cp, base, &result, max_chars - prefix_chars); 75 /* FIXME */ 76 cp += (rv & ~KSTRTOX_OVERFLOW); 77 } else { 78 /* Field too short for prefix + digit, skip over without converting */ 79 cp = startp + max_chars; 80 } 81 82 if (endp) 83 *endp = (char *)cp; 84 85 return result; 86 } 87 88 /** 89 * simple_strtoull - convert a string to an unsigned long long 90 * @cp: The start of the string 91 * @endp: A pointer to the end of the parsed string will be placed here 92 * @base: The number base to use 93 * 94 * This function has caveats. Please use kstrtoull instead. 95 */ 96 noinline 97 unsigned long long simple_strtoull(const char *cp, char **endp, unsigned int base) 98 { 99 return simple_strntoull(cp, endp, base, INT_MAX); 100 } 101 EXPORT_SYMBOL(simple_strtoull); 102 103 /** 104 * simple_strtoul - convert a string to an unsigned long 105 * @cp: The start of the string 106 * @endp: A pointer to the end of the parsed string will be placed here 107 * @base: The number base to use 108 * 109 * This function has caveats. Please use kstrtoul instead. 110 */ 111 unsigned long simple_strtoul(const char *cp, char **endp, unsigned int base) 112 { 113 return simple_strtoull(cp, endp, base); 114 } 115 EXPORT_SYMBOL(simple_strtoul); 116 117 /** 118 * simple_strtol - convert a string to a signed long 119 * @cp: The start of the string 120 * @endp: A pointer to the end of the parsed string will be placed here 121 * @base: The number base to use 122 * 123 * This function has caveats. Please use kstrtol instead. 124 */ 125 long simple_strtol(const char *cp, char **endp, unsigned int base) 126 { 127 if (*cp == '-') 128 return -simple_strtoul(cp + 1, endp, base); 129 130 return simple_strtoul(cp, endp, base); 131 } 132 EXPORT_SYMBOL(simple_strtol); 133 134 noinline 135 static long long simple_strntoll(const char *cp, char **endp, unsigned int base, size_t max_chars) 136 { 137 /* 138 * simple_strntoull() safely handles receiving max_chars==0 in the 139 * case cp[0] == '-' && max_chars == 1. 140 * If max_chars == 0 we can drop through and pass it to simple_strntoull() 141 * and the content of *cp is irrelevant. 142 */ 143 if (*cp == '-' && max_chars > 0) 144 return -simple_strntoull(cp + 1, endp, base, max_chars - 1); 145 146 return simple_strntoull(cp, endp, base, max_chars); 147 } 148 149 /** 150 * simple_strtoll - convert a string to a signed long long 151 * @cp: The start of the string 152 * @endp: A pointer to the end of the parsed string will be placed here 153 * @base: The number base to use 154 * 155 * This function has caveats. Please use kstrtoll instead. 156 */ 157 long long simple_strtoll(const char *cp, char **endp, unsigned int base) 158 { 159 return simple_strntoll(cp, endp, base, INT_MAX); 160 } 161 EXPORT_SYMBOL(simple_strtoll); 162 163 static inline int skip_atoi(const char **s) 164 { 165 int i = 0; 166 167 do { 168 i = i*10 + *((*s)++) - '0'; 169 } while (isdigit(**s)); 170 171 return i; 172 } 173 174 /* 175 * Decimal conversion is by far the most typical, and is used for 176 * /proc and /sys data. This directly impacts e.g. top performance 177 * with many processes running. We optimize it for speed by emitting 178 * two characters at a time, using a 200 byte lookup table. This 179 * roughly halves the number of multiplications compared to computing 180 * the digits one at a time. Implementation strongly inspired by the 181 * previous version, which in turn used ideas described at 182 * <http://www.cs.uiowa.edu/~jones/bcd/divide.html> (with permission 183 * from the author, Douglas W. Jones). 184 * 185 * It turns out there is precisely one 26 bit fixed-point 186 * approximation a of 64/100 for which x/100 == (x * (u64)a) >> 32 187 * holds for all x in [0, 10^8-1], namely a = 0x28f5c29. The actual 188 * range happens to be somewhat larger (x <= 1073741898), but that's 189 * irrelevant for our purpose. 190 * 191 * For dividing a number in the range [10^4, 10^6-1] by 100, we still 192 * need a 32x32->64 bit multiply, so we simply use the same constant. 193 * 194 * For dividing a number in the range [100, 10^4-1] by 100, there are 195 * several options. The simplest is (x * 0x147b) >> 19, which is valid 196 * for all x <= 43698. 197 */ 198 199 static const u16 decpair[100] = { 200 #define _(x) (__force u16) cpu_to_le16(((x % 10) | ((x / 10) << 8)) + 0x3030) 201 _( 0), _( 1), _( 2), _( 3), _( 4), _( 5), _( 6), _( 7), _( 8), _( 9), 202 _(10), _(11), _(12), _(13), _(14), _(15), _(16), _(17), _(18), _(19), 203 _(20), _(21), _(22), _(23), _(24), _(25), _(26), _(27), _(28), _(29), 204 _(30), _(31), _(32), _(33), _(34), _(35), _(36), _(37), _(38), _(39), 205 _(40), _(41), _(42), _(43), _(44), _(45), _(46), _(47), _(48), _(49), 206 _(50), _(51), _(52), _(53), _(54), _(55), _(56), _(57), _(58), _(59), 207 _(60), _(61), _(62), _(63), _(64), _(65), _(66), _(67), _(68), _(69), 208 _(70), _(71), _(72), _(73), _(74), _(75), _(76), _(77), _(78), _(79), 209 _(80), _(81), _(82), _(83), _(84), _(85), _(86), _(87), _(88), _(89), 210 _(90), _(91), _(92), _(93), _(94), _(95), _(96), _(97), _(98), _(99), 211 #undef _ 212 }; 213 214 /* 215 * This will print a single '0' even if r == 0, since we would 216 * immediately jump to out_r where two 0s would be written but only 217 * one of them accounted for in buf. This is needed by ip4_string 218 * below. All other callers pass a non-zero value of r. 219 */ 220 static noinline_for_stack 221 char *put_dec_trunc8(char *buf, unsigned r) 222 { 223 unsigned q; 224 225 /* 1 <= r < 10^8 */ 226 if (r < 100) 227 goto out_r; 228 229 /* 100 <= r < 10^8 */ 230 q = (r * (u64)0x28f5c29) >> 32; 231 *((u16 *)buf) = decpair[r - 100*q]; 232 buf += 2; 233 234 /* 1 <= q < 10^6 */ 235 if (q < 100) 236 goto out_q; 237 238 /* 100 <= q < 10^6 */ 239 r = (q * (u64)0x28f5c29) >> 32; 240 *((u16 *)buf) = decpair[q - 100*r]; 241 buf += 2; 242 243 /* 1 <= r < 10^4 */ 244 if (r < 100) 245 goto out_r; 246 247 /* 100 <= r < 10^4 */ 248 q = (r * 0x147b) >> 19; 249 *((u16 *)buf) = decpair[r - 100*q]; 250 buf += 2; 251 out_q: 252 /* 1 <= q < 100 */ 253 r = q; 254 out_r: 255 /* 1 <= r < 100 */ 256 *((u16 *)buf) = decpair[r]; 257 buf += r < 10 ? 1 : 2; 258 return buf; 259 } 260 261 #if BITS_PER_LONG == 64 && BITS_PER_LONG_LONG == 64 262 static noinline_for_stack 263 char *put_dec_full8(char *buf, unsigned r) 264 { 265 unsigned q; 266 267 /* 0 <= r < 10^8 */ 268 q = (r * (u64)0x28f5c29) >> 32; 269 *((u16 *)buf) = decpair[r - 100*q]; 270 buf += 2; 271 272 /* 0 <= q < 10^6 */ 273 r = (q * (u64)0x28f5c29) >> 32; 274 *((u16 *)buf) = decpair[q - 100*r]; 275 buf += 2; 276 277 /* 0 <= r < 10^4 */ 278 q = (r * 0x147b) >> 19; 279 *((u16 *)buf) = decpair[r - 100*q]; 280 buf += 2; 281 282 /* 0 <= q < 100 */ 283 *((u16 *)buf) = decpair[q]; 284 buf += 2; 285 return buf; 286 } 287 288 static noinline_for_stack 289 char *put_dec(char *buf, unsigned long long n) 290 { 291 if (n >= 100*1000*1000) 292 buf = put_dec_full8(buf, do_div(n, 100*1000*1000)); 293 /* 1 <= n <= 1.6e11 */ 294 if (n >= 100*1000*1000) 295 buf = put_dec_full8(buf, do_div(n, 100*1000*1000)); 296 /* 1 <= n < 1e8 */ 297 return put_dec_trunc8(buf, n); 298 } 299 300 #elif BITS_PER_LONG == 32 && BITS_PER_LONG_LONG == 64 301 302 static void 303 put_dec_full4(char *buf, unsigned r) 304 { 305 unsigned q; 306 307 /* 0 <= r < 10^4 */ 308 q = (r * 0x147b) >> 19; 309 *((u16 *)buf) = decpair[r - 100*q]; 310 buf += 2; 311 /* 0 <= q < 100 */ 312 *((u16 *)buf) = decpair[q]; 313 } 314 315 /* 316 * Call put_dec_full4 on x % 10000, return x / 10000. 317 * The approximation x/10000 == (x * 0x346DC5D7) >> 43 318 * holds for all x < 1,128,869,999. The largest value this 319 * helper will ever be asked to convert is 1,125,520,955. 320 * (second call in the put_dec code, assuming n is all-ones). 321 */ 322 static noinline_for_stack 323 unsigned put_dec_helper4(char *buf, unsigned x) 324 { 325 uint32_t q = (x * (uint64_t)0x346DC5D7) >> 43; 326 327 put_dec_full4(buf, x - q * 10000); 328 return q; 329 } 330 331 /* Based on code by Douglas W. Jones found at 332 * <http://www.cs.uiowa.edu/~jones/bcd/decimal.html#sixtyfour> 333 * (with permission from the author). 334 * Performs no 64-bit division and hence should be fast on 32-bit machines. 335 */ 336 static 337 char *put_dec(char *buf, unsigned long long n) 338 { 339 uint32_t d3, d2, d1, q, h; 340 341 if (n < 100*1000*1000) 342 return put_dec_trunc8(buf, n); 343 344 d1 = ((uint32_t)n >> 16); /* implicit "& 0xffff" */ 345 h = (n >> 32); 346 d2 = (h ) & 0xffff; 347 d3 = (h >> 16); /* implicit "& 0xffff" */ 348 349 /* n = 2^48 d3 + 2^32 d2 + 2^16 d1 + d0 350 = 281_4749_7671_0656 d3 + 42_9496_7296 d2 + 6_5536 d1 + d0 */ 351 q = 656 * d3 + 7296 * d2 + 5536 * d1 + ((uint32_t)n & 0xffff); 352 q = put_dec_helper4(buf, q); 353 354 q += 7671 * d3 + 9496 * d2 + 6 * d1; 355 q = put_dec_helper4(buf+4, q); 356 357 q += 4749 * d3 + 42 * d2; 358 q = put_dec_helper4(buf+8, q); 359 360 q += 281 * d3; 361 buf += 12; 362 if (q) 363 buf = put_dec_trunc8(buf, q); 364 else while (buf[-1] == '0') 365 --buf; 366 367 return buf; 368 } 369 370 #endif 371 372 /* 373 * Convert passed number to decimal string. 374 * Returns the length of string. On buffer overflow, returns 0. 375 * 376 * If speed is not important, use snprintf(). It's easy to read the code. 377 */ 378 int num_to_str(char *buf, int size, unsigned long long num, unsigned int width) 379 { 380 /* put_dec requires 2-byte alignment of the buffer. */ 381 char tmp[sizeof(num) * 3] __aligned(2); 382 int idx, len; 383 384 /* put_dec() may work incorrectly for num = 0 (generate "", not "0") */ 385 if (num <= 9) { 386 tmp[0] = '0' + num; 387 len = 1; 388 } else { 389 len = put_dec(tmp, num) - tmp; 390 } 391 392 if (len > size || width > size) 393 return 0; 394 395 if (width > len) { 396 width = width - len; 397 for (idx = 0; idx < width; idx++) 398 buf[idx] = ' '; 399 } else { 400 width = 0; 401 } 402 403 for (idx = 0; idx < len; ++idx) 404 buf[idx + width] = tmp[len - idx - 1]; 405 406 return len + width; 407 } 408 409 #define SIGN 1 /* unsigned/signed */ 410 #define LEFT 2 /* left justified */ 411 #define PLUS 4 /* show plus */ 412 #define SPACE 8 /* space if plus */ 413 #define ZEROPAD 16 /* pad with zero, must be 16 == '0' - ' ' */ 414 #define SMALL 32 /* use lowercase in hex (must be 32 == 0x20) */ 415 #define SPECIAL 64 /* prefix hex with "0x", octal with "0" */ 416 417 static_assert(ZEROPAD == ('0' - ' ')); 418 static_assert(SMALL == ('a' ^ 'A')); 419 420 enum format_state { 421 FORMAT_STATE_NONE, /* Just a string part */ 422 FORMAT_STATE_NUM, 423 FORMAT_STATE_WIDTH, 424 FORMAT_STATE_PRECISION, 425 FORMAT_STATE_CHAR, 426 FORMAT_STATE_STR, 427 FORMAT_STATE_PTR, 428 FORMAT_STATE_PERCENT_CHAR, 429 FORMAT_STATE_INVALID, 430 }; 431 432 struct printf_spec { 433 unsigned char flags; /* flags to number() */ 434 unsigned char base; /* number base, 8, 10 or 16 only */ 435 short precision; /* # of digits/chars */ 436 int field_width; /* width of output field */ 437 } __packed; 438 static_assert(sizeof(struct printf_spec) == 8); 439 440 #define FIELD_WIDTH_MAX ((1 << 23) - 1) 441 #define PRECISION_MAX ((1 << 15) - 1) 442 443 static noinline_for_stack 444 char *number(char *buf, char *end, unsigned long long num, 445 struct printf_spec spec) 446 { 447 /* put_dec requires 2-byte alignment of the buffer. */ 448 char tmp[3 * sizeof(num)] __aligned(2); 449 char sign; 450 char locase; 451 int need_pfx = ((spec.flags & SPECIAL) && spec.base != 10); 452 int i; 453 bool is_zero = num == 0LL; 454 int field_width = spec.field_width; 455 int precision = spec.precision; 456 457 /* locase = 0 or 0x20. ORing digits or letters with 'locase' 458 * produces same digits or (maybe lowercased) letters */ 459 locase = (spec.flags & SMALL); 460 if (spec.flags & LEFT) 461 spec.flags &= ~ZEROPAD; 462 sign = 0; 463 if (spec.flags & SIGN) { 464 if ((signed long long)num < 0) { 465 sign = '-'; 466 num = -(signed long long)num; 467 field_width--; 468 } else if (spec.flags & PLUS) { 469 sign = '+'; 470 field_width--; 471 } else if (spec.flags & SPACE) { 472 sign = ' '; 473 field_width--; 474 } 475 } 476 if (need_pfx) { 477 if (spec.base == 16) 478 field_width -= 2; 479 else if (!is_zero) 480 field_width--; 481 } 482 483 /* generate full string in tmp[], in reverse order */ 484 i = 0; 485 if (num < spec.base) 486 tmp[i++] = hex_asc_upper[num] | locase; 487 else if (spec.base != 10) { /* 8 or 16 */ 488 int mask = spec.base - 1; 489 int shift = 3; 490 491 if (spec.base == 16) 492 shift = 4; 493 do { 494 tmp[i++] = (hex_asc_upper[((unsigned char)num) & mask] | locase); 495 num >>= shift; 496 } while (num); 497 } else { /* base 10 */ 498 i = put_dec(tmp, num) - tmp; 499 } 500 501 /* printing 100 using %2d gives "100", not "00" */ 502 if (i > precision) 503 precision = i; 504 /* leading space padding */ 505 field_width -= precision; 506 if (!(spec.flags & (ZEROPAD | LEFT))) { 507 while (--field_width >= 0) { 508 if (buf < end) 509 *buf = ' '; 510 ++buf; 511 } 512 } 513 /* sign */ 514 if (sign) { 515 if (buf < end) 516 *buf = sign; 517 ++buf; 518 } 519 /* "0x" / "0" prefix */ 520 if (need_pfx) { 521 if (spec.base == 16 || !is_zero) { 522 if (buf < end) 523 *buf = '0'; 524 ++buf; 525 } 526 if (spec.base == 16) { 527 if (buf < end) 528 *buf = ('X' | locase); 529 ++buf; 530 } 531 } 532 /* zero or space padding */ 533 if (!(spec.flags & LEFT)) { 534 char c = ' ' + (spec.flags & ZEROPAD); 535 536 while (--field_width >= 0) { 537 if (buf < end) 538 *buf = c; 539 ++buf; 540 } 541 } 542 /* hmm even more zero padding? */ 543 while (i <= --precision) { 544 if (buf < end) 545 *buf = '0'; 546 ++buf; 547 } 548 /* actual digits of result */ 549 while (--i >= 0) { 550 if (buf < end) 551 *buf = tmp[i]; 552 ++buf; 553 } 554 /* trailing space padding */ 555 while (--field_width >= 0) { 556 if (buf < end) 557 *buf = ' '; 558 ++buf; 559 } 560 561 return buf; 562 } 563 564 static noinline_for_stack 565 char *special_hex_number(char *buf, char *end, unsigned long long num, int size) 566 { 567 struct printf_spec spec; 568 569 spec.field_width = 2 + 2 * size; /* 0x + hex */ 570 spec.flags = SPECIAL | SMALL | ZEROPAD; 571 spec.base = 16; 572 spec.precision = -1; 573 574 return number(buf, end, num, spec); 575 } 576 577 static void move_right(char *buf, char *end, unsigned len, unsigned spaces) 578 { 579 size_t size; 580 if (buf >= end) /* nowhere to put anything */ 581 return; 582 size = end - buf; 583 if (size <= spaces) { 584 memset(buf, ' ', size); 585 return; 586 } 587 if (len) { 588 if (len > size - spaces) 589 len = size - spaces; 590 memmove(buf + spaces, buf, len); 591 } 592 memset(buf, ' ', spaces); 593 } 594 595 /* 596 * Handle field width padding for a string. 597 * @buf: current buffer position 598 * @n: length of string 599 * @end: end of output buffer 600 * @spec: for field width and flags 601 * Returns: new buffer position after padding. 602 */ 603 static noinline_for_stack 604 char *widen_string(char *buf, int n, char *end, struct printf_spec spec) 605 { 606 unsigned spaces; 607 608 if (likely(n >= spec.field_width)) 609 return buf; 610 /* we want to pad the sucker */ 611 spaces = spec.field_width - n; 612 if (!(spec.flags & LEFT)) { 613 move_right(buf - n, end, n, spaces); 614 return buf + spaces; 615 } 616 while (spaces--) { 617 if (buf < end) 618 *buf = ' '; 619 ++buf; 620 } 621 return buf; 622 } 623 624 /* Handle string from a well known address. */ 625 static char *string_nocheck(char *buf, char *end, const char *s, 626 struct printf_spec spec) 627 { 628 int len = 0; 629 int lim = spec.precision; 630 631 while (lim--) { 632 char c = *s++; 633 if (!c) 634 break; 635 if (buf < end) 636 *buf = c; 637 ++buf; 638 ++len; 639 } 640 return widen_string(buf, len, end, spec); 641 } 642 643 static char *err_ptr(char *buf, char *end, void *ptr, 644 struct printf_spec spec) 645 { 646 int err = PTR_ERR(ptr); 647 const char *sym = errname(err); 648 649 if (sym) 650 return string_nocheck(buf, end, sym, spec); 651 652 /* 653 * Somebody passed ERR_PTR(-1234) or some other non-existing 654 * Efoo - or perhaps CONFIG_SYMBOLIC_ERRNAME=n. Fall back to 655 * printing it as its decimal representation. 656 */ 657 spec.flags |= SIGN; 658 spec.base = 10; 659 return number(buf, end, err, spec); 660 } 661 662 /* Be careful: error messages must fit into the given buffer. */ 663 static char *error_string(char *buf, char *end, const char *s, 664 struct printf_spec spec) 665 { 666 /* 667 * Hard limit to avoid a completely insane messages. It actually 668 * works pretty well because most error messages are in 669 * the many pointer format modifiers. 670 */ 671 if (spec.precision == -1) 672 spec.precision = 2 * sizeof(void *); 673 674 return string_nocheck(buf, end, s, spec); 675 } 676 677 /* 678 * Do not call any complex external code here. Nested printk()/vsprintf() 679 * might cause infinite loops. Failures might break printk() and would 680 * be hard to debug. 681 */ 682 static const char *check_pointer_msg(const void *ptr) 683 { 684 if (!ptr) 685 return "(null)"; 686 687 if ((unsigned long)ptr < PAGE_SIZE || IS_ERR_VALUE(ptr)) 688 return "(efault)"; 689 690 return NULL; 691 } 692 693 static int check_pointer(char **buf, char *end, const void *ptr, 694 struct printf_spec spec) 695 { 696 const char *err_msg; 697 698 err_msg = check_pointer_msg(ptr); 699 if (err_msg) { 700 *buf = error_string(*buf, end, err_msg, spec); 701 return -EFAULT; 702 } 703 704 return 0; 705 } 706 707 static noinline_for_stack 708 char *string(char *buf, char *end, const char *s, 709 struct printf_spec spec) 710 { 711 if (check_pointer(&buf, end, s, spec)) 712 return buf; 713 714 return string_nocheck(buf, end, s, spec); 715 } 716 717 static char *pointer_string(char *buf, char *end, 718 const void *ptr, 719 struct printf_spec spec) 720 { 721 spec.base = 16; 722 spec.flags |= SMALL; 723 if (spec.field_width == -1) { 724 spec.field_width = 2 * sizeof(ptr); 725 spec.flags |= ZEROPAD; 726 } 727 728 return number(buf, end, (unsigned long int)ptr, spec); 729 } 730 731 /* Make pointers available for printing early in the boot sequence. */ 732 static int debug_boot_weak_hash __ro_after_init; 733 734 static int __init debug_boot_weak_hash_enable(char *str) 735 { 736 debug_boot_weak_hash = 1; 737 pr_info("debug_boot_weak_hash enabled\n"); 738 return 0; 739 } 740 early_param("debug_boot_weak_hash", debug_boot_weak_hash_enable); 741 742 static bool filled_random_ptr_key __read_mostly; 743 static siphash_key_t ptr_key __read_mostly; 744 745 static int fill_ptr_key(struct notifier_block *nb, unsigned long action, void *data) 746 { 747 get_random_bytes(&ptr_key, sizeof(ptr_key)); 748 749 /* Pairs with smp_rmb() before reading ptr_key. */ 750 smp_wmb(); 751 WRITE_ONCE(filled_random_ptr_key, true); 752 return NOTIFY_DONE; 753 } 754 755 static int __init vsprintf_init_hashval(void) 756 { 757 static struct notifier_block fill_ptr_key_nb = { .notifier_call = fill_ptr_key }; 758 execute_with_initialized_rng(&fill_ptr_key_nb); 759 return 0; 760 } 761 subsys_initcall(vsprintf_init_hashval) 762 763 /* Maps a pointer to a 32 bit unique identifier. */ 764 static inline int __ptr_to_hashval(const void *ptr, unsigned long *hashval_out) 765 { 766 unsigned long hashval; 767 768 if (!READ_ONCE(filled_random_ptr_key)) 769 return -EBUSY; 770 771 /* Pairs with smp_wmb() after writing ptr_key. */ 772 smp_rmb(); 773 774 #ifdef CONFIG_64BIT 775 hashval = (unsigned long)siphash_1u64((u64)ptr, &ptr_key); 776 /* 777 * Mask off the first 32 bits, this makes explicit that we have 778 * modified the address (and 32 bits is plenty for a unique ID). 779 */ 780 hashval = hashval & 0xffffffff; 781 #else 782 hashval = (unsigned long)siphash_1u32((u32)ptr, &ptr_key); 783 #endif 784 *hashval_out = hashval; 785 return 0; 786 } 787 788 int ptr_to_hashval(const void *ptr, unsigned long *hashval_out) 789 { 790 return __ptr_to_hashval(ptr, hashval_out); 791 } 792 793 static char *ptr_to_id(char *buf, char *end, const void *ptr, 794 struct printf_spec spec) 795 { 796 const char *str = sizeof(ptr) == 8 ? "(____ptrval____)" : "(ptrval)"; 797 unsigned long hashval; 798 int ret; 799 800 /* 801 * Print the real pointer value for NULL and error pointers, 802 * as they are not actual addresses. 803 */ 804 if (IS_ERR_OR_NULL(ptr)) 805 return pointer_string(buf, end, ptr, spec); 806 807 /* When debugging early boot use non-cryptographically secure hash. */ 808 if (unlikely(debug_boot_weak_hash)) { 809 hashval = hash_long((unsigned long)ptr, 32); 810 return pointer_string(buf, end, (const void *)hashval, spec); 811 } 812 813 ret = __ptr_to_hashval(ptr, &hashval); 814 if (ret) { 815 spec.field_width = 2 * sizeof(ptr); 816 /* string length must be less than default_width */ 817 return error_string(buf, end, str, spec); 818 } 819 820 return pointer_string(buf, end, (const void *)hashval, spec); 821 } 822 823 static char *default_pointer(char *buf, char *end, const void *ptr, 824 struct printf_spec spec) 825 { 826 /* 827 * default is to _not_ leak addresses, so hash before printing, 828 * unless no_hash_pointers is specified on the command line. 829 */ 830 if (unlikely(no_hash_pointers)) 831 return pointer_string(buf, end, ptr, spec); 832 833 return ptr_to_id(buf, end, ptr, spec); 834 } 835 836 int kptr_restrict __read_mostly; 837 838 static noinline_for_stack 839 char *restricted_pointer(char *buf, char *end, const void *ptr, 840 struct printf_spec spec) 841 { 842 switch (kptr_restrict) { 843 case 0: 844 /* Handle as %p, hash and do _not_ leak addresses. */ 845 return default_pointer(buf, end, ptr, spec); 846 case 1: { 847 const struct cred *cred; 848 849 /* 850 * kptr_restrict==1 cannot be used in IRQ context 851 * because its test for CAP_SYSLOG would be meaningless. 852 */ 853 if (in_hardirq() || in_serving_softirq() || in_nmi()) { 854 if (spec.field_width == -1) 855 spec.field_width = 2 * sizeof(ptr); 856 return error_string(buf, end, "pK-error", spec); 857 } 858 859 /* 860 * Only print the real pointer value if the current 861 * process has CAP_SYSLOG and is running with the 862 * same credentials it started with. This is because 863 * access to files is checked at open() time, but %pK 864 * checks permission at read() time. We don't want to 865 * leak pointer values if a binary opens a file using 866 * %pK and then elevates privileges before reading it. 867 */ 868 cred = current_cred(); 869 if (!has_capability_noaudit(current, CAP_SYSLOG) || 870 !uid_eq(cred->euid, cred->uid) || 871 !gid_eq(cred->egid, cred->gid)) 872 ptr = NULL; 873 break; 874 } 875 case 2: 876 default: 877 /* Always print 0's for %pK */ 878 ptr = NULL; 879 break; 880 } 881 882 return pointer_string(buf, end, ptr, spec); 883 } 884 885 static noinline_for_stack 886 char *dentry_name(char *buf, char *end, const struct dentry *d, struct printf_spec spec, 887 const char *fmt) 888 { 889 const char *array[4], *s; 890 const struct dentry *p; 891 int depth; 892 int i, n; 893 894 switch (fmt[1]) { 895 case '2': case '3': case '4': 896 depth = fmt[1] - '0'; 897 break; 898 default: 899 depth = 1; 900 } 901 902 rcu_read_lock(); 903 for (i = 0; i < depth; i++, d = p) { 904 if (check_pointer(&buf, end, d, spec)) { 905 rcu_read_unlock(); 906 return buf; 907 } 908 909 p = READ_ONCE(d->d_parent); 910 array[i] = READ_ONCE(d->d_name.name); 911 if (p == d) { 912 if (i) 913 array[i] = ""; 914 i++; 915 break; 916 } 917 } 918 s = array[--i]; 919 for (n = 0; n != spec.precision; n++, buf++) { 920 char c = *s++; 921 if (!c) { 922 if (!i) 923 break; 924 c = '/'; 925 s = array[--i]; 926 } 927 if (buf < end) 928 *buf = c; 929 } 930 rcu_read_unlock(); 931 return widen_string(buf, n, end, spec); 932 } 933 934 static noinline_for_stack 935 char *file_dentry_name(char *buf, char *end, const struct file *f, 936 struct printf_spec spec, const char *fmt) 937 { 938 if (check_pointer(&buf, end, f, spec)) 939 return buf; 940 941 return dentry_name(buf, end, f->f_path.dentry, spec, fmt); 942 } 943 #ifdef CONFIG_BLOCK 944 static noinline_for_stack 945 char *bdev_name(char *buf, char *end, struct block_device *bdev, 946 struct printf_spec spec, const char *fmt) 947 { 948 struct gendisk *hd; 949 950 if (check_pointer(&buf, end, bdev, spec)) 951 return buf; 952 953 hd = bdev->bd_disk; 954 buf = string(buf, end, hd->disk_name, spec); 955 if (bdev_is_partition(bdev)) { 956 if (isdigit(hd->disk_name[strlen(hd->disk_name)-1])) { 957 if (buf < end) 958 *buf = 'p'; 959 buf++; 960 } 961 buf = number(buf, end, bdev_partno(bdev), spec); 962 } 963 return buf; 964 } 965 #endif 966 967 static noinline_for_stack 968 char *symbol_string(char *buf, char *end, void *ptr, 969 struct printf_spec spec, const char *fmt) 970 { 971 unsigned long value; 972 #ifdef CONFIG_KALLSYMS 973 char sym[KSYM_SYMBOL_LEN]; 974 #endif 975 976 if (fmt[1] == 'R') 977 ptr = __builtin_extract_return_addr(ptr); 978 value = (unsigned long)ptr; 979 980 #ifdef CONFIG_KALLSYMS 981 if (*fmt == 'B' && fmt[1] == 'b') 982 sprint_backtrace_build_id(sym, value); 983 else if (*fmt == 'B') 984 sprint_backtrace(sym, value); 985 else if (*fmt == 'S' && (fmt[1] == 'b' || (fmt[1] == 'R' && fmt[2] == 'b'))) 986 sprint_symbol_build_id(sym, value); 987 else if (*fmt != 's') 988 sprint_symbol(sym, value); 989 else 990 sprint_symbol_no_offset(sym, value); 991 992 return string_nocheck(buf, end, sym, spec); 993 #else 994 return special_hex_number(buf, end, value, sizeof(void *)); 995 #endif 996 } 997 998 static const struct printf_spec default_str_spec = { 999 .field_width = -1, 1000 .precision = -1, 1001 }; 1002 1003 static const struct printf_spec default_flag_spec = { 1004 .base = 16, 1005 .precision = -1, 1006 .flags = SPECIAL | SMALL, 1007 }; 1008 1009 static const struct printf_spec default_dec_spec = { 1010 .base = 10, 1011 .precision = -1, 1012 }; 1013 1014 static const struct printf_spec default_dec02_spec = { 1015 .base = 10, 1016 .field_width = 2, 1017 .precision = -1, 1018 .flags = ZEROPAD, 1019 }; 1020 1021 static const struct printf_spec default_dec04_spec = { 1022 .base = 10, 1023 .field_width = 4, 1024 .precision = -1, 1025 .flags = ZEROPAD, 1026 }; 1027 1028 static noinline_for_stack 1029 char *hex_range(char *buf, char *end, u64 start_val, u64 end_val, 1030 struct printf_spec spec) 1031 { 1032 buf = number(buf, end, start_val, spec); 1033 if (start_val == end_val) 1034 return buf; 1035 1036 if (buf < end) 1037 *buf = '-'; 1038 ++buf; 1039 return number(buf, end, end_val, spec); 1040 } 1041 1042 static noinline_for_stack 1043 char *resource_string(char *buf, char *end, struct resource *res, 1044 struct printf_spec spec, const char *fmt) 1045 { 1046 #ifndef IO_RSRC_PRINTK_SIZE 1047 #define IO_RSRC_PRINTK_SIZE 6 1048 #endif 1049 1050 #ifndef MEM_RSRC_PRINTK_SIZE 1051 #define MEM_RSRC_PRINTK_SIZE 10 1052 #endif 1053 static const struct printf_spec io_spec = { 1054 .base = 16, 1055 .field_width = IO_RSRC_PRINTK_SIZE, 1056 .precision = -1, 1057 .flags = SPECIAL | SMALL | ZEROPAD, 1058 }; 1059 static const struct printf_spec mem_spec = { 1060 .base = 16, 1061 .field_width = MEM_RSRC_PRINTK_SIZE, 1062 .precision = -1, 1063 .flags = SPECIAL | SMALL | ZEROPAD, 1064 }; 1065 static const struct printf_spec bus_spec = { 1066 .base = 16, 1067 .field_width = 2, 1068 .precision = -1, 1069 .flags = SMALL | ZEROPAD, 1070 }; 1071 static const struct printf_spec str_spec = { 1072 .field_width = -1, 1073 .precision = 10, 1074 .flags = LEFT, 1075 }; 1076 1077 /* 32-bit res (sizeof==4): 10 chars in dec, 10 in hex ("0x" + 8) 1078 * 64-bit res (sizeof==8): 20 chars in dec, 18 in hex ("0x" + 16) */ 1079 #define RSRC_BUF_SIZE ((2 * sizeof(resource_size_t)) + 4) 1080 #define FLAG_BUF_SIZE (2 * sizeof(res->flags)) 1081 #define DECODED_BUF_SIZE sizeof("[mem - 64bit pref window disabled]") 1082 #define RAW_BUF_SIZE sizeof("[mem - flags 0x]") 1083 char sym[MAX(2*RSRC_BUF_SIZE + DECODED_BUF_SIZE, 1084 2*RSRC_BUF_SIZE + FLAG_BUF_SIZE + RAW_BUF_SIZE)]; 1085 1086 char *p = sym, *pend = sym + sizeof(sym); 1087 int decode = (fmt[0] == 'R') ? 1 : 0; 1088 const struct printf_spec *specp; 1089 1090 if (check_pointer(&buf, end, res, spec)) 1091 return buf; 1092 1093 *p++ = '['; 1094 if (res->flags & IORESOURCE_IO) { 1095 p = string_nocheck(p, pend, "io ", str_spec); 1096 specp = &io_spec; 1097 } else if (res->flags & IORESOURCE_MEM) { 1098 p = string_nocheck(p, pend, "mem ", str_spec); 1099 specp = &mem_spec; 1100 } else if (res->flags & IORESOURCE_IRQ) { 1101 p = string_nocheck(p, pend, "irq ", str_spec); 1102 specp = &default_dec_spec; 1103 } else if (res->flags & IORESOURCE_DMA) { 1104 p = string_nocheck(p, pend, "dma ", str_spec); 1105 specp = &default_dec_spec; 1106 } else if (res->flags & IORESOURCE_BUS) { 1107 p = string_nocheck(p, pend, "bus ", str_spec); 1108 specp = &bus_spec; 1109 } else { 1110 p = string_nocheck(p, pend, "??? ", str_spec); 1111 specp = &mem_spec; 1112 decode = 0; 1113 } 1114 if (decode && res->flags & IORESOURCE_UNSET) { 1115 p = string_nocheck(p, pend, "size ", str_spec); 1116 p = number(p, pend, resource_size(res), *specp); 1117 } else { 1118 p = hex_range(p, pend, res->start, res->end, *specp); 1119 } 1120 if (decode) { 1121 if (res->flags & IORESOURCE_MEM_64) 1122 p = string_nocheck(p, pend, " 64bit", str_spec); 1123 if (res->flags & IORESOURCE_PREFETCH) 1124 p = string_nocheck(p, pend, " pref", str_spec); 1125 if (res->flags & IORESOURCE_WINDOW) 1126 p = string_nocheck(p, pend, " window", str_spec); 1127 if (res->flags & IORESOURCE_DISABLED) 1128 p = string_nocheck(p, pend, " disabled", str_spec); 1129 } else { 1130 p = string_nocheck(p, pend, " flags ", str_spec); 1131 p = number(p, pend, res->flags, default_flag_spec); 1132 } 1133 *p++ = ']'; 1134 *p = '\0'; 1135 1136 return string_nocheck(buf, end, sym, spec); 1137 } 1138 1139 static noinline_for_stack 1140 char *range_string(char *buf, char *end, const struct range *range, 1141 struct printf_spec spec, const char *fmt) 1142 { 1143 char sym[sizeof("[range 0x0123456789abcdef-0x0123456789abcdef]")]; 1144 char *p = sym, *pend = sym + sizeof(sym); 1145 1146 struct printf_spec range_spec = { 1147 .field_width = 2 + 2 * sizeof(range->start), /* 0x + 2 * 8 */ 1148 .flags = SPECIAL | SMALL | ZEROPAD, 1149 .base = 16, 1150 .precision = -1, 1151 }; 1152 1153 if (check_pointer(&buf, end, range, spec)) 1154 return buf; 1155 1156 p = string_nocheck(p, pend, "[range ", default_str_spec); 1157 p = hex_range(p, pend, range->start, range->end, range_spec); 1158 *p++ = ']'; 1159 *p = '\0'; 1160 1161 return string_nocheck(buf, end, sym, spec); 1162 } 1163 1164 static noinline_for_stack 1165 char *hex_string(char *buf, char *end, u8 *addr, struct printf_spec spec, 1166 const char *fmt) 1167 { 1168 int i, len = 1; /* if we pass '%ph[CDN]', field width remains 1169 negative value, fallback to the default */ 1170 char separator; 1171 1172 if (spec.field_width == 0) 1173 /* nothing to print */ 1174 return buf; 1175 1176 if (check_pointer(&buf, end, addr, spec)) 1177 return buf; 1178 1179 switch (fmt[1]) { 1180 case 'C': 1181 separator = ':'; 1182 break; 1183 case 'D': 1184 separator = '-'; 1185 break; 1186 case 'N': 1187 separator = 0; 1188 break; 1189 default: 1190 separator = ' '; 1191 break; 1192 } 1193 1194 if (spec.field_width > 0) 1195 len = min_t(int, spec.field_width, 64); 1196 1197 for (i = 0; i < len; ++i) { 1198 if (buf < end) 1199 *buf = hex_asc_hi(addr[i]); 1200 ++buf; 1201 if (buf < end) 1202 *buf = hex_asc_lo(addr[i]); 1203 ++buf; 1204 1205 if (separator && i != len - 1) { 1206 if (buf < end) 1207 *buf = separator; 1208 ++buf; 1209 } 1210 } 1211 1212 return buf; 1213 } 1214 1215 static noinline_for_stack 1216 char *bitmap_string(char *buf, char *end, const unsigned long *bitmap, 1217 struct printf_spec spec, const char *fmt) 1218 { 1219 const int CHUNKSZ = 32; 1220 int nr_bits = max_t(int, spec.field_width, 0); 1221 int i, chunksz; 1222 bool first = true; 1223 1224 if (check_pointer(&buf, end, bitmap, spec)) 1225 return buf; 1226 1227 /* reused to print numbers */ 1228 spec = (struct printf_spec){ .flags = SMALL | ZEROPAD, .base = 16 }; 1229 1230 chunksz = nr_bits & (CHUNKSZ - 1); 1231 if (chunksz == 0) 1232 chunksz = CHUNKSZ; 1233 1234 i = ALIGN(nr_bits, CHUNKSZ) - CHUNKSZ; 1235 for (; i >= 0; i -= CHUNKSZ) { 1236 u32 chunkmask, val; 1237 int word, bit; 1238 1239 chunkmask = ((1ULL << chunksz) - 1); 1240 word = i / BITS_PER_LONG; 1241 bit = i % BITS_PER_LONG; 1242 val = (bitmap[word] >> bit) & chunkmask; 1243 1244 if (!first) { 1245 if (buf < end) 1246 *buf = ','; 1247 buf++; 1248 } 1249 first = false; 1250 1251 spec.field_width = DIV_ROUND_UP(chunksz, 4); 1252 buf = number(buf, end, val, spec); 1253 1254 chunksz = CHUNKSZ; 1255 } 1256 return buf; 1257 } 1258 1259 static noinline_for_stack 1260 char *bitmap_list_string(char *buf, char *end, const unsigned long *bitmap, 1261 struct printf_spec spec, const char *fmt) 1262 { 1263 int nr_bits = max_t(int, spec.field_width, 0); 1264 bool first = true; 1265 int rbot, rtop; 1266 1267 if (check_pointer(&buf, end, bitmap, spec)) 1268 return buf; 1269 1270 for_each_set_bitrange(rbot, rtop, bitmap, nr_bits) { 1271 if (!first) { 1272 if (buf < end) 1273 *buf = ','; 1274 buf++; 1275 } 1276 first = false; 1277 1278 buf = number(buf, end, rbot, default_dec_spec); 1279 if (rtop == rbot + 1) 1280 continue; 1281 1282 if (buf < end) 1283 *buf = '-'; 1284 buf = number(++buf, end, rtop - 1, default_dec_spec); 1285 } 1286 return buf; 1287 } 1288 1289 static noinline_for_stack 1290 char *mac_address_string(char *buf, char *end, u8 *addr, 1291 struct printf_spec spec, const char *fmt) 1292 { 1293 char mac_addr[sizeof("xx:xx:xx:xx:xx:xx")]; 1294 char *p = mac_addr; 1295 int i; 1296 char separator; 1297 bool reversed = false; 1298 1299 if (check_pointer(&buf, end, addr, spec)) 1300 return buf; 1301 1302 switch (fmt[1]) { 1303 case 'F': 1304 separator = '-'; 1305 break; 1306 1307 case 'R': 1308 reversed = true; 1309 fallthrough; 1310 1311 default: 1312 separator = ':'; 1313 break; 1314 } 1315 1316 for (i = 0; i < 6; i++) { 1317 if (reversed) 1318 p = hex_byte_pack(p, addr[5 - i]); 1319 else 1320 p = hex_byte_pack(p, addr[i]); 1321 1322 if (fmt[0] == 'M' && i != 5) 1323 *p++ = separator; 1324 } 1325 *p = '\0'; 1326 1327 return string_nocheck(buf, end, mac_addr, spec); 1328 } 1329 1330 static noinline_for_stack 1331 char *ip4_string(char *p, const u8 *addr, const char *fmt) 1332 { 1333 int i; 1334 bool leading_zeros = (fmt[0] == 'i'); 1335 int index; 1336 int step; 1337 1338 switch (fmt[2]) { 1339 case 'h': 1340 #ifdef __BIG_ENDIAN 1341 index = 0; 1342 step = 1; 1343 #else 1344 index = 3; 1345 step = -1; 1346 #endif 1347 break; 1348 case 'l': 1349 index = 3; 1350 step = -1; 1351 break; 1352 case 'n': 1353 case 'b': 1354 default: 1355 index = 0; 1356 step = 1; 1357 break; 1358 } 1359 for (i = 0; i < 4; i++) { 1360 char temp[4] __aligned(2); /* hold each IP quad in reverse order */ 1361 int digits = put_dec_trunc8(temp, addr[index]) - temp; 1362 if (leading_zeros) { 1363 if (digits < 3) 1364 *p++ = '0'; 1365 if (digits < 2) 1366 *p++ = '0'; 1367 } 1368 /* reverse the digits in the quad */ 1369 while (digits--) 1370 *p++ = temp[digits]; 1371 if (i < 3) 1372 *p++ = '.'; 1373 index += step; 1374 } 1375 *p = '\0'; 1376 1377 return p; 1378 } 1379 1380 static noinline_for_stack 1381 char *ip6_compressed_string(char *p, const char *addr) 1382 { 1383 int i, j, range; 1384 unsigned char zerolength[8]; 1385 int longest = 1; 1386 int colonpos = -1; 1387 u16 word; 1388 u8 hi, lo; 1389 bool needcolon = false; 1390 bool useIPv4; 1391 struct in6_addr in6; 1392 1393 memcpy(&in6, addr, sizeof(struct in6_addr)); 1394 1395 useIPv4 = ipv6_addr_v4mapped(&in6) || ipv6_addr_is_isatap(&in6); 1396 1397 memset(zerolength, 0, sizeof(zerolength)); 1398 1399 if (useIPv4) 1400 range = 6; 1401 else 1402 range = 8; 1403 1404 /* find position of longest 0 run */ 1405 for (i = 0; i < range; i++) { 1406 for (j = i; j < range; j++) { 1407 if (in6.s6_addr16[j] != 0) 1408 break; 1409 zerolength[i]++; 1410 } 1411 } 1412 for (i = 0; i < range; i++) { 1413 if (zerolength[i] > longest) { 1414 longest = zerolength[i]; 1415 colonpos = i; 1416 } 1417 } 1418 if (longest == 1) /* don't compress a single 0 */ 1419 colonpos = -1; 1420 1421 /* emit address */ 1422 for (i = 0; i < range; i++) { 1423 if (i == colonpos) { 1424 if (needcolon || i == 0) 1425 *p++ = ':'; 1426 *p++ = ':'; 1427 needcolon = false; 1428 i += longest - 1; 1429 continue; 1430 } 1431 if (needcolon) { 1432 *p++ = ':'; 1433 needcolon = false; 1434 } 1435 /* hex u16 without leading 0s */ 1436 word = ntohs(in6.s6_addr16[i]); 1437 hi = word >> 8; 1438 lo = word & 0xff; 1439 if (hi) { 1440 if (hi > 0x0f) 1441 p = hex_byte_pack(p, hi); 1442 else 1443 *p++ = hex_asc_lo(hi); 1444 p = hex_byte_pack(p, lo); 1445 } 1446 else if (lo > 0x0f) 1447 p = hex_byte_pack(p, lo); 1448 else 1449 *p++ = hex_asc_lo(lo); 1450 needcolon = true; 1451 } 1452 1453 if (useIPv4) { 1454 if (needcolon) 1455 *p++ = ':'; 1456 p = ip4_string(p, &in6.s6_addr[12], "I4"); 1457 } 1458 *p = '\0'; 1459 1460 return p; 1461 } 1462 1463 static noinline_for_stack 1464 char *ip6_string(char *p, const char *addr, const char *fmt) 1465 { 1466 int i; 1467 1468 for (i = 0; i < 8; i++) { 1469 p = hex_byte_pack(p, *addr++); 1470 p = hex_byte_pack(p, *addr++); 1471 if (fmt[0] == 'I' && i != 7) 1472 *p++ = ':'; 1473 } 1474 *p = '\0'; 1475 1476 return p; 1477 } 1478 1479 static noinline_for_stack 1480 char *ip6_addr_string(char *buf, char *end, const u8 *addr, 1481 struct printf_spec spec, const char *fmt) 1482 { 1483 char ip6_addr[sizeof("xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:255.255.255.255")]; 1484 1485 if (fmt[0] == 'I' && fmt[2] == 'c') 1486 ip6_compressed_string(ip6_addr, addr); 1487 else 1488 ip6_string(ip6_addr, addr, fmt); 1489 1490 return string_nocheck(buf, end, ip6_addr, spec); 1491 } 1492 1493 static noinline_for_stack 1494 char *ip4_addr_string(char *buf, char *end, const u8 *addr, 1495 struct printf_spec spec, const char *fmt) 1496 { 1497 char ip4_addr[sizeof("255.255.255.255")]; 1498 1499 ip4_string(ip4_addr, addr, fmt); 1500 1501 return string_nocheck(buf, end, ip4_addr, spec); 1502 } 1503 1504 static noinline_for_stack 1505 char *ip6_addr_string_sa(char *buf, char *end, const struct sockaddr_in6 *sa, 1506 struct printf_spec spec, const char *fmt) 1507 { 1508 bool have_p = false, have_s = false, have_f = false, have_c = false; 1509 char ip6_addr[sizeof("[xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:255.255.255.255]") + 1510 sizeof(":12345") + sizeof("/123456789") + 1511 sizeof("%1234567890")]; 1512 char *p = ip6_addr, *pend = ip6_addr + sizeof(ip6_addr); 1513 const u8 *addr = (const u8 *) &sa->sin6_addr; 1514 char fmt6[2] = { fmt[0], '6' }; 1515 u8 off = 0; 1516 1517 fmt++; 1518 while (isalpha(*++fmt)) { 1519 switch (*fmt) { 1520 case 'p': 1521 have_p = true; 1522 break; 1523 case 'f': 1524 have_f = true; 1525 break; 1526 case 's': 1527 have_s = true; 1528 break; 1529 case 'c': 1530 have_c = true; 1531 break; 1532 } 1533 } 1534 1535 if (have_p || have_s || have_f) { 1536 *p = '['; 1537 off = 1; 1538 } 1539 1540 if (fmt6[0] == 'I' && have_c) 1541 p = ip6_compressed_string(ip6_addr + off, addr); 1542 else 1543 p = ip6_string(ip6_addr + off, addr, fmt6); 1544 1545 if (have_p || have_s || have_f) 1546 *p++ = ']'; 1547 1548 if (have_p) { 1549 *p++ = ':'; 1550 p = number(p, pend, ntohs(sa->sin6_port), spec); 1551 } 1552 if (have_f) { 1553 *p++ = '/'; 1554 p = number(p, pend, ntohl(sa->sin6_flowinfo & 1555 IPV6_FLOWINFO_MASK), spec); 1556 } 1557 if (have_s) { 1558 *p++ = '%'; 1559 p = number(p, pend, sa->sin6_scope_id, spec); 1560 } 1561 *p = '\0'; 1562 1563 return string_nocheck(buf, end, ip6_addr, spec); 1564 } 1565 1566 static noinline_for_stack 1567 char *ip4_addr_string_sa(char *buf, char *end, const struct sockaddr_in *sa, 1568 struct printf_spec spec, const char *fmt) 1569 { 1570 bool have_p = false; 1571 char *p, ip4_addr[sizeof("255.255.255.255") + sizeof(":12345")]; 1572 char *pend = ip4_addr + sizeof(ip4_addr); 1573 const u8 *addr = (const u8 *) &sa->sin_addr.s_addr; 1574 char fmt4[3] = { fmt[0], '4', 0 }; 1575 1576 fmt++; 1577 while (isalpha(*++fmt)) { 1578 switch (*fmt) { 1579 case 'p': 1580 have_p = true; 1581 break; 1582 case 'h': 1583 case 'l': 1584 case 'n': 1585 case 'b': 1586 fmt4[2] = *fmt; 1587 break; 1588 } 1589 } 1590 1591 p = ip4_string(ip4_addr, addr, fmt4); 1592 if (have_p) { 1593 *p++ = ':'; 1594 p = number(p, pend, ntohs(sa->sin_port), spec); 1595 } 1596 *p = '\0'; 1597 1598 return string_nocheck(buf, end, ip4_addr, spec); 1599 } 1600 1601 static noinline_for_stack 1602 char *ip_addr_string(char *buf, char *end, const void *ptr, 1603 struct printf_spec spec, const char *fmt) 1604 { 1605 char *err_fmt_msg; 1606 1607 if (check_pointer(&buf, end, ptr, spec)) 1608 return buf; 1609 1610 switch (fmt[1]) { 1611 case '6': 1612 return ip6_addr_string(buf, end, ptr, spec, fmt); 1613 case '4': 1614 return ip4_addr_string(buf, end, ptr, spec, fmt); 1615 case 'S': { 1616 const union { 1617 struct sockaddr raw; 1618 struct sockaddr_in v4; 1619 struct sockaddr_in6 v6; 1620 } *sa = ptr; 1621 1622 switch (sa->raw.sa_family) { 1623 case AF_INET: 1624 return ip4_addr_string_sa(buf, end, &sa->v4, spec, fmt); 1625 case AF_INET6: 1626 return ip6_addr_string_sa(buf, end, &sa->v6, spec, fmt); 1627 default: 1628 return error_string(buf, end, "(einval)", spec); 1629 }} 1630 } 1631 1632 err_fmt_msg = fmt[0] == 'i' ? "(%pi?)" : "(%pI?)"; 1633 return error_string(buf, end, err_fmt_msg, spec); 1634 } 1635 1636 static noinline_for_stack 1637 char *escaped_string(char *buf, char *end, u8 *addr, struct printf_spec spec, 1638 const char *fmt) 1639 { 1640 bool found = true; 1641 int count = 1; 1642 unsigned int flags = 0; 1643 int len; 1644 1645 if (spec.field_width == 0) 1646 return buf; /* nothing to print */ 1647 1648 if (check_pointer(&buf, end, addr, spec)) 1649 return buf; 1650 1651 do { 1652 switch (fmt[count++]) { 1653 case 'a': 1654 flags |= ESCAPE_ANY; 1655 break; 1656 case 'c': 1657 flags |= ESCAPE_SPECIAL; 1658 break; 1659 case 'h': 1660 flags |= ESCAPE_HEX; 1661 break; 1662 case 'n': 1663 flags |= ESCAPE_NULL; 1664 break; 1665 case 'o': 1666 flags |= ESCAPE_OCTAL; 1667 break; 1668 case 'p': 1669 flags |= ESCAPE_NP; 1670 break; 1671 case 's': 1672 flags |= ESCAPE_SPACE; 1673 break; 1674 default: 1675 found = false; 1676 break; 1677 } 1678 } while (found); 1679 1680 if (!flags) 1681 flags = ESCAPE_ANY_NP; 1682 1683 len = spec.field_width < 0 ? 1 : spec.field_width; 1684 1685 /* 1686 * string_escape_mem() writes as many characters as it can to 1687 * the given buffer, and returns the total size of the output 1688 * had the buffer been big enough. 1689 */ 1690 buf += string_escape_mem(addr, len, buf, buf < end ? end - buf : 0, flags, NULL); 1691 1692 return buf; 1693 } 1694 1695 static char *va_format(char *buf, char *end, struct va_format *va_fmt, 1696 struct printf_spec spec, const char *fmt) 1697 { 1698 va_list va; 1699 1700 if (check_pointer(&buf, end, va_fmt, spec)) 1701 return buf; 1702 1703 va_copy(va, *va_fmt->va); 1704 buf += vsnprintf(buf, end > buf ? end - buf : 0, va_fmt->fmt, va); 1705 va_end(va); 1706 1707 return buf; 1708 } 1709 1710 static noinline_for_stack 1711 char *uuid_string(char *buf, char *end, const u8 *addr, 1712 struct printf_spec spec, const char *fmt) 1713 { 1714 char uuid[UUID_STRING_LEN + 1]; 1715 char *p = uuid; 1716 int i; 1717 const u8 *index = uuid_index; 1718 bool uc = false; 1719 1720 if (check_pointer(&buf, end, addr, spec)) 1721 return buf; 1722 1723 switch (*(++fmt)) { 1724 case 'L': 1725 uc = true; 1726 fallthrough; 1727 case 'l': 1728 index = guid_index; 1729 break; 1730 case 'B': 1731 uc = true; 1732 break; 1733 } 1734 1735 for (i = 0; i < 16; i++) { 1736 if (uc) 1737 p = hex_byte_pack_upper(p, addr[index[i]]); 1738 else 1739 p = hex_byte_pack(p, addr[index[i]]); 1740 switch (i) { 1741 case 3: 1742 case 5: 1743 case 7: 1744 case 9: 1745 *p++ = '-'; 1746 break; 1747 } 1748 } 1749 1750 *p = 0; 1751 1752 return string_nocheck(buf, end, uuid, spec); 1753 } 1754 1755 static noinline_for_stack 1756 char *netdev_bits(char *buf, char *end, const void *addr, 1757 struct printf_spec spec, const char *fmt) 1758 { 1759 unsigned long long num; 1760 int size; 1761 1762 if (check_pointer(&buf, end, addr, spec)) 1763 return buf; 1764 1765 switch (fmt[1]) { 1766 case 'F': 1767 num = *(const netdev_features_t *)addr; 1768 size = sizeof(netdev_features_t); 1769 break; 1770 default: 1771 return error_string(buf, end, "(%pN?)", spec); 1772 } 1773 1774 return special_hex_number(buf, end, num, size); 1775 } 1776 1777 static noinline_for_stack 1778 char *fourcc_string(char *buf, char *end, const u32 *fourcc, 1779 struct printf_spec spec, const char *fmt) 1780 { 1781 char output[sizeof("0123 little-endian (0x01234567)")]; 1782 char *p = output; 1783 unsigned int i; 1784 u32 orig, val; 1785 1786 if (fmt[1] != 'c' || fmt[2] != 'c') 1787 return error_string(buf, end, "(%p4?)", spec); 1788 1789 if (check_pointer(&buf, end, fourcc, spec)) 1790 return buf; 1791 1792 orig = get_unaligned(fourcc); 1793 val = orig & ~BIT(31); 1794 1795 for (i = 0; i < sizeof(u32); i++) { 1796 unsigned char c = val >> (i * 8); 1797 1798 /* Print non-control ASCII characters as-is, dot otherwise */ 1799 *p++ = isascii(c) && isprint(c) ? c : '.'; 1800 } 1801 1802 *p++ = ' '; 1803 strcpy(p, orig & BIT(31) ? "big-endian" : "little-endian"); 1804 p += strlen(p); 1805 1806 *p++ = ' '; 1807 *p++ = '('; 1808 p = special_hex_number(p, output + sizeof(output) - 2, orig, sizeof(u32)); 1809 *p++ = ')'; 1810 *p = '\0'; 1811 1812 return string(buf, end, output, spec); 1813 } 1814 1815 static noinline_for_stack 1816 char *address_val(char *buf, char *end, const void *addr, 1817 struct printf_spec spec, const char *fmt) 1818 { 1819 unsigned long long num; 1820 int size; 1821 1822 if (check_pointer(&buf, end, addr, spec)) 1823 return buf; 1824 1825 switch (fmt[1]) { 1826 case 'd': 1827 num = *(const dma_addr_t *)addr; 1828 size = sizeof(dma_addr_t); 1829 break; 1830 case 'p': 1831 default: 1832 num = *(const phys_addr_t *)addr; 1833 size = sizeof(phys_addr_t); 1834 break; 1835 } 1836 1837 return special_hex_number(buf, end, num, size); 1838 } 1839 1840 static noinline_for_stack 1841 char *date_str(char *buf, char *end, const struct rtc_time *tm, bool r) 1842 { 1843 int year = tm->tm_year + (r ? 0 : 1900); 1844 int mon = tm->tm_mon + (r ? 0 : 1); 1845 1846 buf = number(buf, end, year, default_dec04_spec); 1847 if (buf < end) 1848 *buf = '-'; 1849 buf++; 1850 1851 buf = number(buf, end, mon, default_dec02_spec); 1852 if (buf < end) 1853 *buf = '-'; 1854 buf++; 1855 1856 return number(buf, end, tm->tm_mday, default_dec02_spec); 1857 } 1858 1859 static noinline_for_stack 1860 char *time_str(char *buf, char *end, const struct rtc_time *tm, bool r) 1861 { 1862 buf = number(buf, end, tm->tm_hour, default_dec02_spec); 1863 if (buf < end) 1864 *buf = ':'; 1865 buf++; 1866 1867 buf = number(buf, end, tm->tm_min, default_dec02_spec); 1868 if (buf < end) 1869 *buf = ':'; 1870 buf++; 1871 1872 return number(buf, end, tm->tm_sec, default_dec02_spec); 1873 } 1874 1875 static noinline_for_stack 1876 char *rtc_str(char *buf, char *end, const struct rtc_time *tm, 1877 struct printf_spec spec, const char *fmt) 1878 { 1879 bool have_t = true, have_d = true; 1880 bool raw = false, iso8601_separator = true; 1881 bool found = true; 1882 int count = 2; 1883 1884 if (check_pointer(&buf, end, tm, spec)) 1885 return buf; 1886 1887 switch (fmt[count]) { 1888 case 'd': 1889 have_t = false; 1890 count++; 1891 break; 1892 case 't': 1893 have_d = false; 1894 count++; 1895 break; 1896 } 1897 1898 do { 1899 switch (fmt[count++]) { 1900 case 'r': 1901 raw = true; 1902 break; 1903 case 's': 1904 iso8601_separator = false; 1905 break; 1906 default: 1907 found = false; 1908 break; 1909 } 1910 } while (found); 1911 1912 if (have_d) 1913 buf = date_str(buf, end, tm, raw); 1914 if (have_d && have_t) { 1915 if (buf < end) 1916 *buf = iso8601_separator ? 'T' : ' '; 1917 buf++; 1918 } 1919 if (have_t) 1920 buf = time_str(buf, end, tm, raw); 1921 1922 return buf; 1923 } 1924 1925 static noinline_for_stack 1926 char *time64_str(char *buf, char *end, const time64_t time, 1927 struct printf_spec spec, const char *fmt) 1928 { 1929 struct rtc_time rtc_time; 1930 struct tm tm; 1931 1932 time64_to_tm(time, 0, &tm); 1933 1934 rtc_time.tm_sec = tm.tm_sec; 1935 rtc_time.tm_min = tm.tm_min; 1936 rtc_time.tm_hour = tm.tm_hour; 1937 rtc_time.tm_mday = tm.tm_mday; 1938 rtc_time.tm_mon = tm.tm_mon; 1939 rtc_time.tm_year = tm.tm_year; 1940 rtc_time.tm_wday = tm.tm_wday; 1941 rtc_time.tm_yday = tm.tm_yday; 1942 1943 rtc_time.tm_isdst = 0; 1944 1945 return rtc_str(buf, end, &rtc_time, spec, fmt); 1946 } 1947 1948 static noinline_for_stack 1949 char *time_and_date(char *buf, char *end, void *ptr, struct printf_spec spec, 1950 const char *fmt) 1951 { 1952 switch (fmt[1]) { 1953 case 'R': 1954 return rtc_str(buf, end, (const struct rtc_time *)ptr, spec, fmt); 1955 case 'T': 1956 return time64_str(buf, end, *(const time64_t *)ptr, spec, fmt); 1957 default: 1958 return error_string(buf, end, "(%pt?)", spec); 1959 } 1960 } 1961 1962 static noinline_for_stack 1963 char *clock(char *buf, char *end, struct clk *clk, struct printf_spec spec, 1964 const char *fmt) 1965 { 1966 if (!IS_ENABLED(CONFIG_HAVE_CLK)) 1967 return error_string(buf, end, "(%pC?)", spec); 1968 1969 if (check_pointer(&buf, end, clk, spec)) 1970 return buf; 1971 1972 switch (fmt[1]) { 1973 case 'n': 1974 default: 1975 #ifdef CONFIG_COMMON_CLK 1976 return string(buf, end, __clk_get_name(clk), spec); 1977 #else 1978 return ptr_to_id(buf, end, clk, spec); 1979 #endif 1980 } 1981 } 1982 1983 static 1984 char *format_flags(char *buf, char *end, unsigned long flags, 1985 const struct trace_print_flags *names) 1986 { 1987 unsigned long mask; 1988 1989 for ( ; flags && names->name; names++) { 1990 mask = names->mask; 1991 if ((flags & mask) != mask) 1992 continue; 1993 1994 buf = string(buf, end, names->name, default_str_spec); 1995 1996 flags &= ~mask; 1997 if (flags) { 1998 if (buf < end) 1999 *buf = '|'; 2000 buf++; 2001 } 2002 } 2003 2004 if (flags) 2005 buf = number(buf, end, flags, default_flag_spec); 2006 2007 return buf; 2008 } 2009 2010 struct page_flags_fields { 2011 int width; 2012 int shift; 2013 int mask; 2014 const struct printf_spec *spec; 2015 const char *name; 2016 }; 2017 2018 static const struct page_flags_fields pff[] = { 2019 {SECTIONS_WIDTH, SECTIONS_PGSHIFT, SECTIONS_MASK, 2020 &default_dec_spec, "section"}, 2021 {NODES_WIDTH, NODES_PGSHIFT, NODES_MASK, 2022 &default_dec_spec, "node"}, 2023 {ZONES_WIDTH, ZONES_PGSHIFT, ZONES_MASK, 2024 &default_dec_spec, "zone"}, 2025 {LAST_CPUPID_WIDTH, LAST_CPUPID_PGSHIFT, LAST_CPUPID_MASK, 2026 &default_flag_spec, "lastcpupid"}, 2027 {KASAN_TAG_WIDTH, KASAN_TAG_PGSHIFT, KASAN_TAG_MASK, 2028 &default_flag_spec, "kasantag"}, 2029 }; 2030 2031 static 2032 char *format_page_flags(char *buf, char *end, unsigned long flags) 2033 { 2034 unsigned long main_flags = flags & PAGEFLAGS_MASK; 2035 bool append = false; 2036 int i; 2037 2038 buf = number(buf, end, flags, default_flag_spec); 2039 if (buf < end) 2040 *buf = '('; 2041 buf++; 2042 2043 /* Page flags from the main area. */ 2044 if (main_flags) { 2045 buf = format_flags(buf, end, main_flags, pageflag_names); 2046 append = true; 2047 } 2048 2049 /* Page flags from the fields area */ 2050 for (i = 0; i < ARRAY_SIZE(pff); i++) { 2051 /* Skip undefined fields. */ 2052 if (!pff[i].width) 2053 continue; 2054 2055 /* Format: Flag Name + '=' (equals sign) + Number + '|' (separator) */ 2056 if (append) { 2057 if (buf < end) 2058 *buf = '|'; 2059 buf++; 2060 } 2061 2062 buf = string(buf, end, pff[i].name, default_str_spec); 2063 if (buf < end) 2064 *buf = '='; 2065 buf++; 2066 buf = number(buf, end, (flags >> pff[i].shift) & pff[i].mask, 2067 *pff[i].spec); 2068 2069 append = true; 2070 } 2071 if (buf < end) 2072 *buf = ')'; 2073 buf++; 2074 2075 return buf; 2076 } 2077 2078 static noinline_for_stack 2079 char *flags_string(char *buf, char *end, void *flags_ptr, 2080 struct printf_spec spec, const char *fmt) 2081 { 2082 unsigned long flags; 2083 const struct trace_print_flags *names; 2084 2085 if (check_pointer(&buf, end, flags_ptr, spec)) 2086 return buf; 2087 2088 switch (fmt[1]) { 2089 case 'p': 2090 return format_page_flags(buf, end, *(unsigned long *)flags_ptr); 2091 case 'v': 2092 flags = *(unsigned long *)flags_ptr; 2093 names = vmaflag_names; 2094 break; 2095 case 'g': 2096 flags = (__force unsigned long)(*(gfp_t *)flags_ptr); 2097 names = gfpflag_names; 2098 break; 2099 default: 2100 return error_string(buf, end, "(%pG?)", spec); 2101 } 2102 2103 return format_flags(buf, end, flags, names); 2104 } 2105 2106 static noinline_for_stack 2107 char *fwnode_full_name_string(struct fwnode_handle *fwnode, char *buf, 2108 char *end) 2109 { 2110 int depth; 2111 2112 /* Loop starting from the root node to the current node. */ 2113 for (depth = fwnode_count_parents(fwnode); depth >= 0; depth--) { 2114 /* 2115 * Only get a reference for other nodes (i.e. parent nodes). 2116 * fwnode refcount may be 0 here. 2117 */ 2118 struct fwnode_handle *__fwnode = depth ? 2119 fwnode_get_nth_parent(fwnode, depth) : fwnode; 2120 2121 buf = string(buf, end, fwnode_get_name_prefix(__fwnode), 2122 default_str_spec); 2123 buf = string(buf, end, fwnode_get_name(__fwnode), 2124 default_str_spec); 2125 2126 if (depth) 2127 fwnode_handle_put(__fwnode); 2128 } 2129 2130 return buf; 2131 } 2132 2133 static noinline_for_stack 2134 char *device_node_string(char *buf, char *end, struct device_node *dn, 2135 struct printf_spec spec, const char *fmt) 2136 { 2137 char tbuf[sizeof("xxxx") + 1]; 2138 const char *p; 2139 int ret; 2140 char *buf_start = buf; 2141 struct property *prop; 2142 bool has_mult, pass; 2143 2144 struct printf_spec str_spec = spec; 2145 str_spec.field_width = -1; 2146 2147 if (fmt[0] != 'F') 2148 return error_string(buf, end, "(%pO?)", spec); 2149 2150 if (!IS_ENABLED(CONFIG_OF)) 2151 return error_string(buf, end, "(%pOF?)", spec); 2152 2153 if (check_pointer(&buf, end, dn, spec)) 2154 return buf; 2155 2156 /* simple case without anything any more format specifiers */ 2157 fmt++; 2158 if (fmt[0] == '\0' || strcspn(fmt,"fnpPFcC") > 0) 2159 fmt = "f"; 2160 2161 for (pass = false; strspn(fmt,"fnpPFcC"); fmt++, pass = true) { 2162 int precision; 2163 if (pass) { 2164 if (buf < end) 2165 *buf = ':'; 2166 buf++; 2167 } 2168 2169 switch (*fmt) { 2170 case 'f': /* full_name */ 2171 buf = fwnode_full_name_string(of_fwnode_handle(dn), buf, 2172 end); 2173 break; 2174 case 'n': /* name */ 2175 p = fwnode_get_name(of_fwnode_handle(dn)); 2176 precision = str_spec.precision; 2177 str_spec.precision = strchrnul(p, '@') - p; 2178 buf = string(buf, end, p, str_spec); 2179 str_spec.precision = precision; 2180 break; 2181 case 'p': /* phandle */ 2182 buf = number(buf, end, (unsigned int)dn->phandle, default_dec_spec); 2183 break; 2184 case 'P': /* path-spec */ 2185 p = fwnode_get_name(of_fwnode_handle(dn)); 2186 if (!p[1]) 2187 p = "/"; 2188 buf = string(buf, end, p, str_spec); 2189 break; 2190 case 'F': /* flags */ 2191 tbuf[0] = of_node_check_flag(dn, OF_DYNAMIC) ? 'D' : '-'; 2192 tbuf[1] = of_node_check_flag(dn, OF_DETACHED) ? 'd' : '-'; 2193 tbuf[2] = of_node_check_flag(dn, OF_POPULATED) ? 'P' : '-'; 2194 tbuf[3] = of_node_check_flag(dn, OF_POPULATED_BUS) ? 'B' : '-'; 2195 tbuf[4] = 0; 2196 buf = string_nocheck(buf, end, tbuf, str_spec); 2197 break; 2198 case 'c': /* major compatible string */ 2199 ret = of_property_read_string(dn, "compatible", &p); 2200 if (!ret) 2201 buf = string(buf, end, p, str_spec); 2202 break; 2203 case 'C': /* full compatible string */ 2204 has_mult = false; 2205 of_property_for_each_string(dn, "compatible", prop, p) { 2206 if (has_mult) 2207 buf = string_nocheck(buf, end, ",", str_spec); 2208 buf = string_nocheck(buf, end, "\"", str_spec); 2209 buf = string(buf, end, p, str_spec); 2210 buf = string_nocheck(buf, end, "\"", str_spec); 2211 2212 has_mult = true; 2213 } 2214 break; 2215 default: 2216 break; 2217 } 2218 } 2219 2220 return widen_string(buf, buf - buf_start, end, spec); 2221 } 2222 2223 static noinline_for_stack 2224 char *fwnode_string(char *buf, char *end, struct fwnode_handle *fwnode, 2225 struct printf_spec spec, const char *fmt) 2226 { 2227 struct printf_spec str_spec = spec; 2228 char *buf_start = buf; 2229 2230 str_spec.field_width = -1; 2231 2232 if (*fmt != 'w') 2233 return error_string(buf, end, "(%pf?)", spec); 2234 2235 if (check_pointer(&buf, end, fwnode, spec)) 2236 return buf; 2237 2238 fmt++; 2239 2240 switch (*fmt) { 2241 case 'P': /* name */ 2242 buf = string(buf, end, fwnode_get_name(fwnode), str_spec); 2243 break; 2244 case 'f': /* full_name */ 2245 default: 2246 buf = fwnode_full_name_string(fwnode, buf, end); 2247 break; 2248 } 2249 2250 return widen_string(buf, buf - buf_start, end, spec); 2251 } 2252 2253 static noinline_for_stack 2254 char *resource_or_range(const char *fmt, char *buf, char *end, void *ptr, 2255 struct printf_spec spec) 2256 { 2257 if (*fmt == 'r' && fmt[1] == 'a') 2258 return range_string(buf, end, ptr, spec, fmt); 2259 return resource_string(buf, end, ptr, spec, fmt); 2260 } 2261 2262 int __init no_hash_pointers_enable(char *str) 2263 { 2264 if (no_hash_pointers) 2265 return 0; 2266 2267 no_hash_pointers = true; 2268 2269 pr_warn("**********************************************************\n"); 2270 pr_warn("** NOTICE NOTICE NOTICE NOTICE NOTICE NOTICE NOTICE **\n"); 2271 pr_warn("** **\n"); 2272 pr_warn("** This system shows unhashed kernel memory addresses **\n"); 2273 pr_warn("** via the console, logs, and other interfaces. This **\n"); 2274 pr_warn("** might reduce the security of your system. **\n"); 2275 pr_warn("** **\n"); 2276 pr_warn("** If you see this message and you are not debugging **\n"); 2277 pr_warn("** the kernel, report this immediately to your system **\n"); 2278 pr_warn("** administrator! **\n"); 2279 pr_warn("** **\n"); 2280 pr_warn("** NOTICE NOTICE NOTICE NOTICE NOTICE NOTICE NOTICE **\n"); 2281 pr_warn("**********************************************************\n"); 2282 2283 return 0; 2284 } 2285 early_param("no_hash_pointers", no_hash_pointers_enable); 2286 2287 /* 2288 * Show a '%p' thing. A kernel extension is that the '%p' is followed 2289 * by an extra set of alphanumeric characters that are extended format 2290 * specifiers. 2291 * 2292 * Please update scripts/checkpatch.pl when adding/removing conversion 2293 * characters. (Search for "check for vsprintf extension"). 2294 * 2295 * Right now we handle: 2296 * 2297 * - 'S' For symbolic direct pointers (or function descriptors) with offset 2298 * - 's' For symbolic direct pointers (or function descriptors) without offset 2299 * - '[Ss]R' as above with __builtin_extract_return_addr() translation 2300 * - 'S[R]b' as above with module build ID (for use in backtraces) 2301 * - '[Ff]' %pf and %pF were obsoleted and later removed in favor of 2302 * %ps and %pS. Be careful when re-using these specifiers. 2303 * - 'B' For backtraced symbolic direct pointers with offset 2304 * - 'Bb' as above with module build ID (for use in backtraces) 2305 * - 'R' For decoded struct resource, e.g., [mem 0x0-0x1f 64bit pref] 2306 * - 'r' For raw struct resource, e.g., [mem 0x0-0x1f flags 0x201] 2307 * - 'ra' For struct ranges, e.g., [range 0x0000000000000000 - 0x00000000000000ff] 2308 * - 'b[l]' For a bitmap, the number of bits is determined by the field 2309 * width which must be explicitly specified either as part of the 2310 * format string '%32b[l]' or through '%*b[l]', [l] selects 2311 * range-list format instead of hex format 2312 * - 'M' For a 6-byte MAC address, it prints the address in the 2313 * usual colon-separated hex notation 2314 * - 'm' For a 6-byte MAC address, it prints the hex address without colons 2315 * - 'MF' For a 6-byte MAC FDDI address, it prints the address 2316 * with a dash-separated hex notation 2317 * - '[mM]R' For a 6-byte MAC address, Reverse order (Bluetooth) 2318 * - 'I' [46] for IPv4/IPv6 addresses printed in the usual way 2319 * IPv4 uses dot-separated decimal without leading 0's (1.2.3.4) 2320 * IPv6 uses colon separated network-order 16 bit hex with leading 0's 2321 * [S][pfs] 2322 * Generic IPv4/IPv6 address (struct sockaddr *) that falls back to 2323 * [4] or [6] and is able to print port [p], flowinfo [f], scope [s] 2324 * - 'i' [46] for 'raw' IPv4/IPv6 addresses 2325 * IPv6 omits the colons (01020304...0f) 2326 * IPv4 uses dot-separated decimal with leading 0's (010.123.045.006) 2327 * [S][pfs] 2328 * Generic IPv4/IPv6 address (struct sockaddr *) that falls back to 2329 * [4] or [6] and is able to print port [p], flowinfo [f], scope [s] 2330 * - '[Ii][4S][hnbl]' IPv4 addresses in host, network, big or little endian order 2331 * - 'I[6S]c' for IPv6 addresses printed as specified by 2332 * https://tools.ietf.org/html/rfc5952 2333 * - 'E[achnops]' For an escaped buffer, where rules are defined by combination 2334 * of the following flags (see string_escape_mem() for the 2335 * details): 2336 * a - ESCAPE_ANY 2337 * c - ESCAPE_SPECIAL 2338 * h - ESCAPE_HEX 2339 * n - ESCAPE_NULL 2340 * o - ESCAPE_OCTAL 2341 * p - ESCAPE_NP 2342 * s - ESCAPE_SPACE 2343 * By default ESCAPE_ANY_NP is used. 2344 * - 'U' For a 16 byte UUID/GUID, it prints the UUID/GUID in the form 2345 * "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" 2346 * Options for %pU are: 2347 * b big endian lower case hex (default) 2348 * B big endian UPPER case hex 2349 * l little endian lower case hex 2350 * L little endian UPPER case hex 2351 * big endian output byte order is: 2352 * [0][1][2][3]-[4][5]-[6][7]-[8][9]-[10][11][12][13][14][15] 2353 * little endian output byte order is: 2354 * [3][2][1][0]-[5][4]-[7][6]-[8][9]-[10][11][12][13][14][15] 2355 * - 'V' For a struct va_format which contains a format string * and va_list *, 2356 * call vsnprintf(->format, *->va_list). 2357 * Implements a "recursive vsnprintf". 2358 * Do not use this feature without some mechanism to verify the 2359 * correctness of the format string and va_list arguments. 2360 * - 'K' For a kernel pointer that should be hidden from unprivileged users. 2361 * Use only for procfs, sysfs and similar files, not printk(); please 2362 * read the documentation (path below) first. 2363 * - 'NF' For a netdev_features_t 2364 * - '4cc' V4L2 or DRM FourCC code, with endianness and raw numerical value. 2365 * - 'h[CDN]' For a variable-length buffer, it prints it as a hex string with 2366 * a certain separator (' ' by default): 2367 * C colon 2368 * D dash 2369 * N no separator 2370 * The maximum supported length is 64 bytes of the input. Consider 2371 * to use print_hex_dump() for the larger input. 2372 * - 'a[pd]' For address types [p] phys_addr_t, [d] dma_addr_t and derivatives 2373 * (default assumed to be phys_addr_t, passed by reference) 2374 * - 'd[234]' For a dentry name (optionally 2-4 last components) 2375 * - 'D[234]' Same as 'd' but for a struct file 2376 * - 'g' For block_device name (gendisk + partition number) 2377 * - 't[RT][dt][r][s]' For time and date as represented by: 2378 * R struct rtc_time 2379 * T time64_t 2380 * - 'C' For a clock, it prints the name (Common Clock Framework) or address 2381 * (legacy clock framework) of the clock 2382 * - 'Cn' For a clock, it prints the name (Common Clock Framework) or address 2383 * (legacy clock framework) of the clock 2384 * - 'G' For flags to be printed as a collection of symbolic strings that would 2385 * construct the specific value. Supported flags given by option: 2386 * p page flags (see struct page) given as pointer to unsigned long 2387 * g gfp flags (GFP_* and __GFP_*) given as pointer to gfp_t 2388 * v vma flags (VM_*) given as pointer to unsigned long 2389 * - 'OF[fnpPcCF]' For a device tree object 2390 * Without any optional arguments prints the full_name 2391 * f device node full_name 2392 * n device node name 2393 * p device node phandle 2394 * P device node path spec (name + @unit) 2395 * F device node flags 2396 * c major compatible string 2397 * C full compatible string 2398 * - 'fw[fP]' For a firmware node (struct fwnode_handle) pointer 2399 * Without an option prints the full name of the node 2400 * f full name 2401 * P node name, including a possible unit address 2402 * - 'x' For printing the address unmodified. Equivalent to "%lx". 2403 * Please read the documentation (path below) before using! 2404 * - '[ku]s' For a BPF/tracing related format specifier, e.g. used out of 2405 * bpf_trace_printk() where [ku] prefix specifies either kernel (k) 2406 * or user (u) memory to probe, and: 2407 * s a string, equivalent to "%s" on direct vsnprintf() use 2408 * 2409 * ** When making changes please also update: 2410 * Documentation/core-api/printk-formats.rst 2411 * 2412 * Note: The default behaviour (unadorned %p) is to hash the address, 2413 * rendering it useful as a unique identifier. 2414 * 2415 * There is also a '%pA' format specifier, but it is only intended to be used 2416 * from Rust code to format core::fmt::Arguments. Do *not* use it from C. 2417 * See rust/kernel/print.rs for details. 2418 */ 2419 static noinline_for_stack 2420 char *pointer(const char *fmt, char *buf, char *end, void *ptr, 2421 struct printf_spec spec) 2422 { 2423 switch (*fmt) { 2424 case 'S': 2425 case 's': 2426 ptr = dereference_symbol_descriptor(ptr); 2427 fallthrough; 2428 case 'B': 2429 return symbol_string(buf, end, ptr, spec, fmt); 2430 case 'R': 2431 case 'r': 2432 return resource_or_range(fmt, buf, end, ptr, spec); 2433 case 'h': 2434 return hex_string(buf, end, ptr, spec, fmt); 2435 case 'b': 2436 switch (fmt[1]) { 2437 case 'l': 2438 return bitmap_list_string(buf, end, ptr, spec, fmt); 2439 default: 2440 return bitmap_string(buf, end, ptr, spec, fmt); 2441 } 2442 case 'M': /* Colon separated: 00:01:02:03:04:05 */ 2443 case 'm': /* Contiguous: 000102030405 */ 2444 /* [mM]F (FDDI) */ 2445 /* [mM]R (Reverse order; Bluetooth) */ 2446 return mac_address_string(buf, end, ptr, spec, fmt); 2447 case 'I': /* Formatted IP supported 2448 * 4: 1.2.3.4 2449 * 6: 0001:0203:...:0708 2450 * 6c: 1::708 or 1::1.2.3.4 2451 */ 2452 case 'i': /* Contiguous: 2453 * 4: 001.002.003.004 2454 * 6: 000102...0f 2455 */ 2456 return ip_addr_string(buf, end, ptr, spec, fmt); 2457 case 'E': 2458 return escaped_string(buf, end, ptr, spec, fmt); 2459 case 'U': 2460 return uuid_string(buf, end, ptr, spec, fmt); 2461 case 'V': 2462 return va_format(buf, end, ptr, spec, fmt); 2463 case 'K': 2464 return restricted_pointer(buf, end, ptr, spec); 2465 case 'N': 2466 return netdev_bits(buf, end, ptr, spec, fmt); 2467 case '4': 2468 return fourcc_string(buf, end, ptr, spec, fmt); 2469 case 'a': 2470 return address_val(buf, end, ptr, spec, fmt); 2471 case 'd': 2472 return dentry_name(buf, end, ptr, spec, fmt); 2473 case 't': 2474 return time_and_date(buf, end, ptr, spec, fmt); 2475 case 'C': 2476 return clock(buf, end, ptr, spec, fmt); 2477 case 'D': 2478 return file_dentry_name(buf, end, ptr, spec, fmt); 2479 #ifdef CONFIG_BLOCK 2480 case 'g': 2481 return bdev_name(buf, end, ptr, spec, fmt); 2482 #endif 2483 2484 case 'G': 2485 return flags_string(buf, end, ptr, spec, fmt); 2486 case 'O': 2487 return device_node_string(buf, end, ptr, spec, fmt + 1); 2488 case 'f': 2489 return fwnode_string(buf, end, ptr, spec, fmt + 1); 2490 case 'A': 2491 if (!IS_ENABLED(CONFIG_RUST)) { 2492 WARN_ONCE(1, "Please remove %%pA from non-Rust code\n"); 2493 return error_string(buf, end, "(%pA?)", spec); 2494 } 2495 return rust_fmt_argument(buf, end, ptr); 2496 case 'x': 2497 return pointer_string(buf, end, ptr, spec); 2498 case 'e': 2499 /* %pe with a non-ERR_PTR gets treated as plain %p */ 2500 if (!IS_ERR(ptr)) 2501 return default_pointer(buf, end, ptr, spec); 2502 return err_ptr(buf, end, ptr, spec); 2503 case 'u': 2504 case 'k': 2505 switch (fmt[1]) { 2506 case 's': 2507 return string(buf, end, ptr, spec); 2508 default: 2509 return error_string(buf, end, "(einval)", spec); 2510 } 2511 default: 2512 return default_pointer(buf, end, ptr, spec); 2513 } 2514 } 2515 2516 struct fmt { 2517 const char *str; 2518 unsigned char state; // enum format_state 2519 unsigned char size; // size of numbers 2520 }; 2521 2522 #define SPEC_CHAR(x, flag) [(x)-32] = flag 2523 static unsigned char spec_flag(unsigned char c) 2524 { 2525 static const unsigned char spec_flag_array[] = { 2526 SPEC_CHAR(' ', SPACE), 2527 SPEC_CHAR('#', SPECIAL), 2528 SPEC_CHAR('+', PLUS), 2529 SPEC_CHAR('-', LEFT), 2530 SPEC_CHAR('0', ZEROPAD), 2531 }; 2532 c -= 32; 2533 return (c < sizeof(spec_flag_array)) ? spec_flag_array[c] : 0; 2534 } 2535 2536 /* 2537 * Helper function to decode printf style format. 2538 * Each call decode a token from the format and return the 2539 * number of characters read (or likely the delta where it wants 2540 * to go on the next call). 2541 * The decoded token is returned through the parameters 2542 * 2543 * 'h', 'l', or 'L' for integer fields 2544 * 'z' support added 23/7/1999 S.H. 2545 * 'z' changed to 'Z' --davidm 1/25/99 2546 * 'Z' changed to 'z' --adobriyan 2017-01-25 2547 * 't' added for ptrdiff_t 2548 * 2549 * @fmt: the format string 2550 * @type of the token returned 2551 * @flags: various flags such as +, -, # tokens.. 2552 * @field_width: overwritten width 2553 * @base: base of the number (octal, hex, ...) 2554 * @precision: precision of a number 2555 * @qualifier: qualifier of a number (long, size_t, ...) 2556 */ 2557 static noinline_for_stack 2558 struct fmt format_decode(struct fmt fmt, struct printf_spec *spec) 2559 { 2560 const char *start = fmt.str; 2561 char flag; 2562 2563 /* we finished early by reading the field width */ 2564 if (unlikely(fmt.state == FORMAT_STATE_WIDTH)) { 2565 if (spec->field_width < 0) { 2566 spec->field_width = -spec->field_width; 2567 spec->flags |= LEFT; 2568 } 2569 fmt.state = FORMAT_STATE_NONE; 2570 goto precision; 2571 } 2572 2573 /* we finished early by reading the precision */ 2574 if (unlikely(fmt.state == FORMAT_STATE_PRECISION)) { 2575 if (spec->precision < 0) 2576 spec->precision = 0; 2577 2578 fmt.state = FORMAT_STATE_NONE; 2579 goto qualifier; 2580 } 2581 2582 /* By default */ 2583 fmt.state = FORMAT_STATE_NONE; 2584 2585 for (; *fmt.str ; fmt.str++) { 2586 if (*fmt.str == '%') 2587 break; 2588 } 2589 2590 /* Return the current non-format string */ 2591 if (fmt.str != start || !*fmt.str) 2592 return fmt; 2593 2594 /* Process flags. This also skips the first '%' */ 2595 spec->flags = 0; 2596 do { 2597 /* this also skips first '%' */ 2598 flag = spec_flag(*++fmt.str); 2599 spec->flags |= flag; 2600 } while (flag); 2601 2602 /* get field width */ 2603 spec->field_width = -1; 2604 2605 if (isdigit(*fmt.str)) 2606 spec->field_width = skip_atoi(&fmt.str); 2607 else if (unlikely(*fmt.str == '*')) { 2608 /* it's the next argument */ 2609 fmt.state = FORMAT_STATE_WIDTH; 2610 fmt.str++; 2611 return fmt; 2612 } 2613 2614 precision: 2615 /* get the precision */ 2616 spec->precision = -1; 2617 if (unlikely(*fmt.str == '.')) { 2618 fmt.str++; 2619 if (isdigit(*fmt.str)) { 2620 spec->precision = skip_atoi(&fmt.str); 2621 if (spec->precision < 0) 2622 spec->precision = 0; 2623 } else if (*fmt.str == '*') { 2624 /* it's the next argument */ 2625 fmt.state = FORMAT_STATE_PRECISION; 2626 fmt.str++; 2627 return fmt; 2628 } 2629 } 2630 2631 qualifier: 2632 /* Set up default numeric format */ 2633 spec->base = 10; 2634 fmt.state = FORMAT_STATE_NUM; 2635 fmt.size = sizeof(int); 2636 static const struct format_state { 2637 unsigned char state; 2638 unsigned char size; 2639 unsigned char flags_or_double_size; 2640 unsigned char base; 2641 } lookup_state[256] = { 2642 // Length 2643 ['l'] = { 0, sizeof(long), sizeof(long long) }, 2644 ['L'] = { 0, sizeof(long long) }, 2645 ['h'] = { 0, sizeof(short), sizeof(char) }, 2646 ['H'] = { 0, sizeof(char) }, // Questionable historical 2647 ['z'] = { 0, sizeof(size_t) }, 2648 ['t'] = { 0, sizeof(ptrdiff_t) }, 2649 2650 // Non-numeric formats 2651 ['c'] = { FORMAT_STATE_CHAR }, 2652 ['s'] = { FORMAT_STATE_STR }, 2653 ['p'] = { FORMAT_STATE_PTR }, 2654 ['%'] = { FORMAT_STATE_PERCENT_CHAR }, 2655 2656 // Numerics 2657 ['o'] = { FORMAT_STATE_NUM, 0, 0, 8 }, 2658 ['x'] = { FORMAT_STATE_NUM, 0, SMALL, 16 }, 2659 ['X'] = { FORMAT_STATE_NUM, 0, 0, 16 }, 2660 ['d'] = { FORMAT_STATE_NUM, 0, SIGN, 10 }, 2661 ['i'] = { FORMAT_STATE_NUM, 0, SIGN, 10 }, 2662 ['u'] = { FORMAT_STATE_NUM, 0, 0, 10, }, 2663 2664 /* 2665 * Since %n poses a greater security risk than 2666 * utility, treat it as any other invalid or 2667 * unsupported format specifier. 2668 */ 2669 }; 2670 2671 const struct format_state *p = lookup_state + (u8)*fmt.str; 2672 if (p->size) { 2673 fmt.size = p->size; 2674 if (p->flags_or_double_size && fmt.str[0] == fmt.str[1]) { 2675 fmt.size = p->flags_or_double_size; 2676 fmt.str++; 2677 } 2678 fmt.str++; 2679 p = lookup_state + *fmt.str; 2680 } 2681 if (p->state) { 2682 if (p->base) 2683 spec->base = p->base; 2684 spec->flags |= p->flags_or_double_size; 2685 fmt.state = p->state; 2686 fmt.str++; 2687 return fmt; 2688 } 2689 2690 WARN_ONCE(1, "Please remove unsupported %%%c in format string\n", *fmt.str); 2691 fmt.state = FORMAT_STATE_INVALID; 2692 return fmt; 2693 } 2694 2695 static void 2696 set_field_width(struct printf_spec *spec, int width) 2697 { 2698 spec->field_width = width; 2699 if (WARN_ONCE(spec->field_width != width, "field width %d too large", width)) { 2700 spec->field_width = clamp(width, -FIELD_WIDTH_MAX, FIELD_WIDTH_MAX); 2701 } 2702 } 2703 2704 static void 2705 set_precision(struct printf_spec *spec, int prec) 2706 { 2707 spec->precision = prec; 2708 if (WARN_ONCE(spec->precision != prec, "precision %d too large", prec)) { 2709 spec->precision = clamp(prec, 0, PRECISION_MAX); 2710 } 2711 } 2712 2713 /* 2714 * Turn a 1/2/4-byte value into a 64-bit one for printing: truncate 2715 * as necessary and deal with signedness. 2716 * 2717 * 'size' is the size of the value in bytes. 2718 */ 2719 static unsigned long long convert_num_spec(unsigned int val, int size, struct printf_spec spec) 2720 { 2721 unsigned int shift = 32 - size*8; 2722 2723 val <<= shift; 2724 if (!(spec.flags & SIGN)) 2725 return val >> shift; 2726 return (int)val >> shift; 2727 } 2728 2729 /** 2730 * vsnprintf - Format a string and place it in a buffer 2731 * @buf: The buffer to place the result into 2732 * @size: The size of the buffer, including the trailing null space 2733 * @fmt_str: The format string to use 2734 * @args: Arguments for the format string 2735 * 2736 * This function generally follows C99 vsnprintf, but has some 2737 * extensions and a few limitations: 2738 * 2739 * - ``%n`` is unsupported 2740 * - ``%p*`` is handled by pointer() 2741 * 2742 * See pointer() or Documentation/core-api/printk-formats.rst for more 2743 * extensive description. 2744 * 2745 * **Please update the documentation in both places when making changes** 2746 * 2747 * The return value is the number of characters which would 2748 * be generated for the given input, excluding the trailing 2749 * '\0', as per ISO C99. If you want to have the exact 2750 * number of characters written into @buf as return value 2751 * (not including the trailing '\0'), use vscnprintf(). If the 2752 * return is greater than or equal to @size, the resulting 2753 * string is truncated. 2754 * 2755 * If you're not already dealing with a va_list consider using snprintf(). 2756 */ 2757 int vsnprintf(char *buf, size_t size, const char *fmt_str, va_list args) 2758 { 2759 char *str, *end; 2760 struct printf_spec spec = {0}; 2761 struct fmt fmt = { 2762 .str = fmt_str, 2763 .state = FORMAT_STATE_NONE, 2764 }; 2765 2766 /* Reject out-of-range values early. Large positive sizes are 2767 used for unknown buffer sizes. */ 2768 if (WARN_ON_ONCE(size > INT_MAX)) 2769 return 0; 2770 2771 str = buf; 2772 end = buf + size; 2773 2774 /* Make sure end is always >= buf */ 2775 if (end < buf) { 2776 end = ((void *)-1); 2777 size = end - buf; 2778 } 2779 2780 while (*fmt.str) { 2781 const char *old_fmt = fmt.str; 2782 2783 fmt = format_decode(fmt, &spec); 2784 2785 switch (fmt.state) { 2786 case FORMAT_STATE_NONE: { 2787 int read = fmt.str - old_fmt; 2788 if (str < end) { 2789 int copy = read; 2790 if (copy > end - str) 2791 copy = end - str; 2792 memcpy(str, old_fmt, copy); 2793 } 2794 str += read; 2795 continue; 2796 } 2797 2798 case FORMAT_STATE_NUM: { 2799 unsigned long long num; 2800 if (fmt.size <= sizeof(int)) 2801 num = convert_num_spec(va_arg(args, int), fmt.size, spec); 2802 else 2803 num = va_arg(args, long long); 2804 str = number(str, end, num, spec); 2805 continue; 2806 } 2807 2808 case FORMAT_STATE_WIDTH: 2809 set_field_width(&spec, va_arg(args, int)); 2810 continue; 2811 2812 case FORMAT_STATE_PRECISION: 2813 set_precision(&spec, va_arg(args, int)); 2814 continue; 2815 2816 case FORMAT_STATE_CHAR: { 2817 char c; 2818 2819 if (!(spec.flags & LEFT)) { 2820 while (--spec.field_width > 0) { 2821 if (str < end) 2822 *str = ' '; 2823 ++str; 2824 2825 } 2826 } 2827 c = (unsigned char) va_arg(args, int); 2828 if (str < end) 2829 *str = c; 2830 ++str; 2831 while (--spec.field_width > 0) { 2832 if (str < end) 2833 *str = ' '; 2834 ++str; 2835 } 2836 continue; 2837 } 2838 2839 case FORMAT_STATE_STR: 2840 str = string(str, end, va_arg(args, char *), spec); 2841 continue; 2842 2843 case FORMAT_STATE_PTR: 2844 str = pointer(fmt.str, str, end, va_arg(args, void *), 2845 spec); 2846 while (isalnum(*fmt.str)) 2847 fmt.str++; 2848 continue; 2849 2850 case FORMAT_STATE_PERCENT_CHAR: 2851 if (str < end) 2852 *str = '%'; 2853 ++str; 2854 continue; 2855 2856 default: 2857 /* 2858 * Presumably the arguments passed gcc's type 2859 * checking, but there is no safe or sane way 2860 * for us to continue parsing the format and 2861 * fetching from the va_list; the remaining 2862 * specifiers and arguments would be out of 2863 * sync. 2864 */ 2865 goto out; 2866 } 2867 } 2868 2869 out: 2870 if (size > 0) { 2871 if (str < end) 2872 *str = '\0'; 2873 else 2874 end[-1] = '\0'; 2875 } 2876 2877 /* the trailing null byte doesn't count towards the total */ 2878 return str-buf; 2879 2880 } 2881 EXPORT_SYMBOL(vsnprintf); 2882 2883 /** 2884 * vscnprintf - Format a string and place it in a buffer 2885 * @buf: The buffer to place the result into 2886 * @size: The size of the buffer, including the trailing null space 2887 * @fmt: The format string to use 2888 * @args: Arguments for the format string 2889 * 2890 * The return value is the number of characters which have been written into 2891 * the @buf not including the trailing '\0'. If @size is == 0 the function 2892 * returns 0. 2893 * 2894 * If you're not already dealing with a va_list consider using scnprintf(). 2895 * 2896 * See the vsnprintf() documentation for format string extensions over C99. 2897 */ 2898 int vscnprintf(char *buf, size_t size, const char *fmt, va_list args) 2899 { 2900 int i; 2901 2902 if (unlikely(!size)) 2903 return 0; 2904 2905 i = vsnprintf(buf, size, fmt, args); 2906 2907 if (likely(i < size)) 2908 return i; 2909 2910 return size - 1; 2911 } 2912 EXPORT_SYMBOL(vscnprintf); 2913 2914 /** 2915 * snprintf - Format a string and place it in a buffer 2916 * @buf: The buffer to place the result into 2917 * @size: The size of the buffer, including the trailing null space 2918 * @fmt: The format string to use 2919 * @...: Arguments for the format string 2920 * 2921 * The return value is the number of characters which would be 2922 * generated for the given input, excluding the trailing null, 2923 * as per ISO C99. If the return is greater than or equal to 2924 * @size, the resulting string is truncated. 2925 * 2926 * See the vsnprintf() documentation for format string extensions over C99. 2927 */ 2928 int snprintf(char *buf, size_t size, const char *fmt, ...) 2929 { 2930 va_list args; 2931 int i; 2932 2933 va_start(args, fmt); 2934 i = vsnprintf(buf, size, fmt, args); 2935 va_end(args); 2936 2937 return i; 2938 } 2939 EXPORT_SYMBOL(snprintf); 2940 2941 /** 2942 * scnprintf - Format a string and place it in a buffer 2943 * @buf: The buffer to place the result into 2944 * @size: The size of the buffer, including the trailing null space 2945 * @fmt: The format string to use 2946 * @...: Arguments for the format string 2947 * 2948 * The return value is the number of characters written into @buf not including 2949 * the trailing '\0'. If @size is == 0 the function returns 0. 2950 */ 2951 2952 int scnprintf(char *buf, size_t size, const char *fmt, ...) 2953 { 2954 va_list args; 2955 int i; 2956 2957 va_start(args, fmt); 2958 i = vscnprintf(buf, size, fmt, args); 2959 va_end(args); 2960 2961 return i; 2962 } 2963 EXPORT_SYMBOL(scnprintf); 2964 2965 /** 2966 * vsprintf - Format a string and place it in a buffer 2967 * @buf: The buffer to place the result into 2968 * @fmt: The format string to use 2969 * @args: Arguments for the format string 2970 * 2971 * The function returns the number of characters written 2972 * into @buf. Use vsnprintf() or vscnprintf() in order to avoid 2973 * buffer overflows. 2974 * 2975 * If you're not already dealing with a va_list consider using sprintf(). 2976 * 2977 * See the vsnprintf() documentation for format string extensions over C99. 2978 */ 2979 int vsprintf(char *buf, const char *fmt, va_list args) 2980 { 2981 return vsnprintf(buf, INT_MAX, fmt, args); 2982 } 2983 EXPORT_SYMBOL(vsprintf); 2984 2985 /** 2986 * sprintf - Format a string and place it in a buffer 2987 * @buf: The buffer to place the result into 2988 * @fmt: The format string to use 2989 * @...: Arguments for the format string 2990 * 2991 * The function returns the number of characters written 2992 * into @buf. Use snprintf() or scnprintf() in order to avoid 2993 * buffer overflows. 2994 * 2995 * See the vsnprintf() documentation for format string extensions over C99. 2996 */ 2997 int sprintf(char *buf, const char *fmt, ...) 2998 { 2999 va_list args; 3000 int i; 3001 3002 va_start(args, fmt); 3003 i = vsnprintf(buf, INT_MAX, fmt, args); 3004 va_end(args); 3005 3006 return i; 3007 } 3008 EXPORT_SYMBOL(sprintf); 3009 3010 #ifdef CONFIG_BINARY_PRINTF 3011 /* 3012 * bprintf service: 3013 * vbin_printf() - VA arguments to binary data 3014 * bstr_printf() - Binary data to text string 3015 */ 3016 3017 /** 3018 * vbin_printf - Parse a format string and place args' binary value in a buffer 3019 * @bin_buf: The buffer to place args' binary value 3020 * @size: The size of the buffer(by words(32bits), not characters) 3021 * @fmt_str: The format string to use 3022 * @args: Arguments for the format string 3023 * 3024 * The format follows C99 vsnprintf, except %n is ignored, and its argument 3025 * is skipped. 3026 * 3027 * The return value is the number of words(32bits) which would be generated for 3028 * the given input. 3029 * 3030 * NOTE: 3031 * If the return value is greater than @size, the resulting bin_buf is NOT 3032 * valid for bstr_printf(). 3033 */ 3034 int vbin_printf(u32 *bin_buf, size_t size, const char *fmt_str, va_list args) 3035 { 3036 struct fmt fmt = { 3037 .str = fmt_str, 3038 .state = FORMAT_STATE_NONE, 3039 }; 3040 struct printf_spec spec = {0}; 3041 char *str, *end; 3042 int width; 3043 3044 str = (char *)bin_buf; 3045 end = (char *)(bin_buf + size); 3046 3047 #define save_arg(type) \ 3048 ({ \ 3049 unsigned long long value; \ 3050 if (sizeof(type) == 8) { \ 3051 unsigned long long val8; \ 3052 str = PTR_ALIGN(str, sizeof(u32)); \ 3053 val8 = va_arg(args, unsigned long long); \ 3054 if (str + sizeof(type) <= end) { \ 3055 *(u32 *)str = *(u32 *)&val8; \ 3056 *(u32 *)(str + 4) = *((u32 *)&val8 + 1); \ 3057 } \ 3058 value = val8; \ 3059 } else { \ 3060 unsigned int val4; \ 3061 str = PTR_ALIGN(str, sizeof(type)); \ 3062 val4 = va_arg(args, int); \ 3063 if (str + sizeof(type) <= end) \ 3064 *(typeof(type) *)str = (type)(long)val4; \ 3065 value = (unsigned long long)val4; \ 3066 } \ 3067 str += sizeof(type); \ 3068 value; \ 3069 }) 3070 3071 while (*fmt.str) { 3072 fmt = format_decode(fmt, &spec); 3073 3074 switch (fmt.state) { 3075 case FORMAT_STATE_NONE: 3076 case FORMAT_STATE_PERCENT_CHAR: 3077 break; 3078 case FORMAT_STATE_INVALID: 3079 goto out; 3080 3081 case FORMAT_STATE_WIDTH: 3082 case FORMAT_STATE_PRECISION: 3083 width = (int)save_arg(int); 3084 /* Pointers may require the width */ 3085 if (*fmt.str == 'p') 3086 set_field_width(&spec, width); 3087 break; 3088 3089 case FORMAT_STATE_CHAR: 3090 save_arg(char); 3091 break; 3092 3093 case FORMAT_STATE_STR: { 3094 const char *save_str = va_arg(args, char *); 3095 const char *err_msg; 3096 size_t len; 3097 3098 err_msg = check_pointer_msg(save_str); 3099 if (err_msg) 3100 save_str = err_msg; 3101 3102 len = strlen(save_str) + 1; 3103 if (str + len < end) 3104 memcpy(str, save_str, len); 3105 str += len; 3106 break; 3107 } 3108 3109 case FORMAT_STATE_PTR: 3110 /* Dereferenced pointers must be done now */ 3111 switch (*fmt.str) { 3112 /* Dereference of functions is still OK */ 3113 case 'S': 3114 case 's': 3115 case 'x': 3116 case 'K': 3117 case 'e': 3118 save_arg(void *); 3119 break; 3120 default: 3121 if (!isalnum(*fmt.str)) { 3122 save_arg(void *); 3123 break; 3124 } 3125 str = pointer(fmt.str, str, end, va_arg(args, void *), 3126 spec); 3127 if (str + 1 < end) 3128 *str++ = '\0'; 3129 else 3130 end[-1] = '\0'; /* Must be nul terminated */ 3131 } 3132 /* skip all alphanumeric pointer suffixes */ 3133 while (isalnum(*fmt.str)) 3134 fmt.str++; 3135 break; 3136 3137 case FORMAT_STATE_NUM: 3138 if (fmt.size > sizeof(int)) { 3139 save_arg(long long); 3140 } else { 3141 save_arg(int); 3142 } 3143 } 3144 } 3145 3146 out: 3147 return (u32 *)(PTR_ALIGN(str, sizeof(u32))) - bin_buf; 3148 #undef save_arg 3149 } 3150 EXPORT_SYMBOL_GPL(vbin_printf); 3151 3152 /** 3153 * bstr_printf - Format a string from binary arguments and place it in a buffer 3154 * @buf: The buffer to place the result into 3155 * @size: The size of the buffer, including the trailing null space 3156 * @fmt_str: The format string to use 3157 * @bin_buf: Binary arguments for the format string 3158 * 3159 * This function like C99 vsnprintf, but the difference is that vsnprintf gets 3160 * arguments from stack, and bstr_printf gets arguments from @bin_buf which is 3161 * a binary buffer that generated by vbin_printf. 3162 * 3163 * The format follows C99 vsnprintf, but has some extensions: 3164 * see vsnprintf comment for details. 3165 * 3166 * The return value is the number of characters which would 3167 * be generated for the given input, excluding the trailing 3168 * '\0', as per ISO C99. If you want to have the exact 3169 * number of characters written into @buf as return value 3170 * (not including the trailing '\0'), use vscnprintf(). If the 3171 * return is greater than or equal to @size, the resulting 3172 * string is truncated. 3173 */ 3174 int bstr_printf(char *buf, size_t size, const char *fmt_str, const u32 *bin_buf) 3175 { 3176 struct fmt fmt = { 3177 .str = fmt_str, 3178 .state = FORMAT_STATE_NONE, 3179 }; 3180 struct printf_spec spec = {0}; 3181 char *str, *end; 3182 const char *args = (const char *)bin_buf; 3183 3184 if (WARN_ON_ONCE(size > INT_MAX)) 3185 return 0; 3186 3187 str = buf; 3188 end = buf + size; 3189 3190 #define get_arg(type) \ 3191 ({ \ 3192 typeof(type) value; \ 3193 if (sizeof(type) == 8) { \ 3194 args = PTR_ALIGN(args, sizeof(u32)); \ 3195 *(u32 *)&value = *(u32 *)args; \ 3196 *((u32 *)&value + 1) = *(u32 *)(args + 4); \ 3197 } else { \ 3198 args = PTR_ALIGN(args, sizeof(type)); \ 3199 value = *(typeof(type) *)args; \ 3200 } \ 3201 args += sizeof(type); \ 3202 value; \ 3203 }) 3204 3205 /* Make sure end is always >= buf */ 3206 if (end < buf) { 3207 end = ((void *)-1); 3208 size = end - buf; 3209 } 3210 3211 while (*fmt.str) { 3212 const char *old_fmt = fmt.str; 3213 unsigned long long num; 3214 3215 fmt = format_decode(fmt, &spec); 3216 switch (fmt.state) { 3217 case FORMAT_STATE_NONE: { 3218 int read = fmt.str - old_fmt; 3219 if (str < end) { 3220 int copy = read; 3221 if (copy > end - str) 3222 copy = end - str; 3223 memcpy(str, old_fmt, copy); 3224 } 3225 str += read; 3226 continue; 3227 } 3228 3229 case FORMAT_STATE_WIDTH: 3230 set_field_width(&spec, get_arg(int)); 3231 continue; 3232 3233 case FORMAT_STATE_PRECISION: 3234 set_precision(&spec, get_arg(int)); 3235 continue; 3236 3237 case FORMAT_STATE_CHAR: { 3238 char c; 3239 3240 if (!(spec.flags & LEFT)) { 3241 while (--spec.field_width > 0) { 3242 if (str < end) 3243 *str = ' '; 3244 ++str; 3245 } 3246 } 3247 c = (unsigned char) get_arg(char); 3248 if (str < end) 3249 *str = c; 3250 ++str; 3251 while (--spec.field_width > 0) { 3252 if (str < end) 3253 *str = ' '; 3254 ++str; 3255 } 3256 continue; 3257 } 3258 3259 case FORMAT_STATE_STR: { 3260 const char *str_arg = args; 3261 args += strlen(str_arg) + 1; 3262 str = string(str, end, (char *)str_arg, spec); 3263 continue; 3264 } 3265 3266 case FORMAT_STATE_PTR: { 3267 bool process = false; 3268 int copy, len; 3269 /* Non function dereferences were already done */ 3270 switch (*fmt.str) { 3271 case 'S': 3272 case 's': 3273 case 'x': 3274 case 'K': 3275 case 'e': 3276 process = true; 3277 break; 3278 default: 3279 if (!isalnum(*fmt.str)) { 3280 process = true; 3281 break; 3282 } 3283 /* Pointer dereference was already processed */ 3284 if (str < end) { 3285 len = copy = strlen(args); 3286 if (copy > end - str) 3287 copy = end - str; 3288 memcpy(str, args, copy); 3289 str += len; 3290 args += len + 1; 3291 } 3292 } 3293 if (process) 3294 str = pointer(fmt.str, str, end, get_arg(void *), spec); 3295 3296 while (isalnum(*fmt.str)) 3297 fmt.str++; 3298 continue; 3299 } 3300 3301 case FORMAT_STATE_PERCENT_CHAR: 3302 if (str < end) 3303 *str = '%'; 3304 ++str; 3305 continue; 3306 3307 case FORMAT_STATE_INVALID: 3308 goto out; 3309 3310 case FORMAT_STATE_NUM: 3311 if (fmt.size > sizeof(int)) { 3312 num = get_arg(long long); 3313 } else { 3314 num = convert_num_spec(get_arg(int), fmt.size, spec); 3315 } 3316 str = number(str, end, num, spec); 3317 continue; 3318 } 3319 } /* while(*fmt.str) */ 3320 3321 out: 3322 if (size > 0) { 3323 if (str < end) 3324 *str = '\0'; 3325 else 3326 end[-1] = '\0'; 3327 } 3328 3329 #undef get_arg 3330 3331 /* the trailing null byte doesn't count towards the total */ 3332 return str - buf; 3333 } 3334 EXPORT_SYMBOL_GPL(bstr_printf); 3335 3336 #endif /* CONFIG_BINARY_PRINTF */ 3337 3338 /** 3339 * vsscanf - Unformat a buffer into a list of arguments 3340 * @buf: input buffer 3341 * @fmt: format of buffer 3342 * @args: arguments 3343 */ 3344 int vsscanf(const char *buf, const char *fmt, va_list args) 3345 { 3346 const char *str = buf; 3347 char *next; 3348 char digit; 3349 int num = 0; 3350 u8 qualifier; 3351 unsigned int base; 3352 union { 3353 long long s; 3354 unsigned long long u; 3355 } val; 3356 s16 field_width; 3357 bool is_sign; 3358 3359 while (*fmt) { 3360 /* skip any white space in format */ 3361 /* white space in format matches any amount of 3362 * white space, including none, in the input. 3363 */ 3364 if (isspace(*fmt)) { 3365 fmt = skip_spaces(++fmt); 3366 str = skip_spaces(str); 3367 } 3368 3369 /* anything that is not a conversion must match exactly */ 3370 if (*fmt != '%' && *fmt) { 3371 if (*fmt++ != *str++) 3372 break; 3373 continue; 3374 } 3375 3376 if (!*fmt) 3377 break; 3378 ++fmt; 3379 3380 /* skip this conversion. 3381 * advance both strings to next white space 3382 */ 3383 if (*fmt == '*') { 3384 if (!*str) 3385 break; 3386 while (!isspace(*fmt) && *fmt != '%' && *fmt) { 3387 /* '%*[' not yet supported, invalid format */ 3388 if (*fmt == '[') 3389 return num; 3390 fmt++; 3391 } 3392 while (!isspace(*str) && *str) 3393 str++; 3394 continue; 3395 } 3396 3397 /* get field width */ 3398 field_width = -1; 3399 if (isdigit(*fmt)) { 3400 field_width = skip_atoi(&fmt); 3401 if (field_width <= 0) 3402 break; 3403 } 3404 3405 /* get conversion qualifier */ 3406 qualifier = -1; 3407 if (*fmt == 'h' || _tolower(*fmt) == 'l' || 3408 *fmt == 'z') { 3409 qualifier = *fmt++; 3410 if (unlikely(qualifier == *fmt)) { 3411 if (qualifier == 'h') { 3412 qualifier = 'H'; 3413 fmt++; 3414 } else if (qualifier == 'l') { 3415 qualifier = 'L'; 3416 fmt++; 3417 } 3418 } 3419 } 3420 3421 if (!*fmt) 3422 break; 3423 3424 if (*fmt == 'n') { 3425 /* return number of characters read so far */ 3426 *va_arg(args, int *) = str - buf; 3427 ++fmt; 3428 continue; 3429 } 3430 3431 if (!*str) 3432 break; 3433 3434 base = 10; 3435 is_sign = false; 3436 3437 switch (*fmt++) { 3438 case 'c': 3439 { 3440 char *s = (char *)va_arg(args, char*); 3441 if (field_width == -1) 3442 field_width = 1; 3443 do { 3444 *s++ = *str++; 3445 } while (--field_width > 0 && *str); 3446 num++; 3447 } 3448 continue; 3449 case 's': 3450 { 3451 char *s = (char *)va_arg(args, char *); 3452 if (field_width == -1) 3453 field_width = SHRT_MAX; 3454 /* first, skip leading white space in buffer */ 3455 str = skip_spaces(str); 3456 3457 /* now copy until next white space */ 3458 while (*str && !isspace(*str) && field_width--) 3459 *s++ = *str++; 3460 *s = '\0'; 3461 num++; 3462 } 3463 continue; 3464 /* 3465 * Warning: This implementation of the '[' conversion specifier 3466 * deviates from its glibc counterpart in the following ways: 3467 * (1) It does NOT support ranges i.e. '-' is NOT a special 3468 * character 3469 * (2) It cannot match the closing bracket ']' itself 3470 * (3) A field width is required 3471 * (4) '%*[' (discard matching input) is currently not supported 3472 * 3473 * Example usage: 3474 * ret = sscanf("00:0a:95","%2[^:]:%2[^:]:%2[^:]", 3475 * buf1, buf2, buf3); 3476 * if (ret < 3) 3477 * // etc.. 3478 */ 3479 case '[': 3480 { 3481 char *s = (char *)va_arg(args, char *); 3482 DECLARE_BITMAP(set, 256) = {0}; 3483 unsigned int len = 0; 3484 bool negate = (*fmt == '^'); 3485 3486 /* field width is required */ 3487 if (field_width == -1) 3488 return num; 3489 3490 if (negate) 3491 ++fmt; 3492 3493 for ( ; *fmt && *fmt != ']'; ++fmt, ++len) 3494 __set_bit((u8)*fmt, set); 3495 3496 /* no ']' or no character set found */ 3497 if (!*fmt || !len) 3498 return num; 3499 ++fmt; 3500 3501 if (negate) { 3502 bitmap_complement(set, set, 256); 3503 /* exclude null '\0' byte */ 3504 __clear_bit(0, set); 3505 } 3506 3507 /* match must be non-empty */ 3508 if (!test_bit((u8)*str, set)) 3509 return num; 3510 3511 while (test_bit((u8)*str, set) && field_width--) 3512 *s++ = *str++; 3513 *s = '\0'; 3514 ++num; 3515 } 3516 continue; 3517 case 'o': 3518 base = 8; 3519 break; 3520 case 'x': 3521 case 'X': 3522 base = 16; 3523 break; 3524 case 'i': 3525 base = 0; 3526 fallthrough; 3527 case 'd': 3528 is_sign = true; 3529 fallthrough; 3530 case 'u': 3531 break; 3532 case '%': 3533 /* looking for '%' in str */ 3534 if (*str++ != '%') 3535 return num; 3536 continue; 3537 default: 3538 /* invalid format; stop here */ 3539 return num; 3540 } 3541 3542 /* have some sort of integer conversion. 3543 * first, skip white space in buffer. 3544 */ 3545 str = skip_spaces(str); 3546 3547 digit = *str; 3548 if (is_sign && digit == '-') { 3549 if (field_width == 1) 3550 break; 3551 3552 digit = *(str + 1); 3553 } 3554 3555 if (!digit 3556 || (base == 16 && !isxdigit(digit)) 3557 || (base == 10 && !isdigit(digit)) 3558 || (base == 8 && !isodigit(digit)) 3559 || (base == 0 && !isdigit(digit))) 3560 break; 3561 3562 if (is_sign) 3563 val.s = simple_strntoll(str, &next, base, 3564 field_width >= 0 ? field_width : INT_MAX); 3565 else 3566 val.u = simple_strntoull(str, &next, base, 3567 field_width >= 0 ? field_width : INT_MAX); 3568 3569 switch (qualifier) { 3570 case 'H': /* that's 'hh' in format */ 3571 if (is_sign) 3572 *va_arg(args, signed char *) = val.s; 3573 else 3574 *va_arg(args, unsigned char *) = val.u; 3575 break; 3576 case 'h': 3577 if (is_sign) 3578 *va_arg(args, short *) = val.s; 3579 else 3580 *va_arg(args, unsigned short *) = val.u; 3581 break; 3582 case 'l': 3583 if (is_sign) 3584 *va_arg(args, long *) = val.s; 3585 else 3586 *va_arg(args, unsigned long *) = val.u; 3587 break; 3588 case 'L': 3589 if (is_sign) 3590 *va_arg(args, long long *) = val.s; 3591 else 3592 *va_arg(args, unsigned long long *) = val.u; 3593 break; 3594 case 'z': 3595 *va_arg(args, size_t *) = val.u; 3596 break; 3597 default: 3598 if (is_sign) 3599 *va_arg(args, int *) = val.s; 3600 else 3601 *va_arg(args, unsigned int *) = val.u; 3602 break; 3603 } 3604 num++; 3605 3606 if (!next) 3607 break; 3608 str = next; 3609 } 3610 3611 return num; 3612 } 3613 EXPORT_SYMBOL(vsscanf); 3614 3615 /** 3616 * sscanf - Unformat a buffer into a list of arguments 3617 * @buf: input buffer 3618 * @fmt: formatting of buffer 3619 * @...: resulting arguments 3620 */ 3621 int sscanf(const char *buf, const char *fmt, ...) 3622 { 3623 va_list args; 3624 int i; 3625 3626 va_start(args, fmt); 3627 i = vsscanf(buf, fmt, args); 3628 va_end(args); 3629 3630 return i; 3631 } 3632 EXPORT_SYMBOL(sscanf); 3633