xref: /linux/lib/vsprintf.c (revision 5293b5f97ea17814da1b88a807290c392823162b)
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
simple_strntoull(const char * startp,char ** endp,unsigned int base,size_t max_chars)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
simple_strtoull(const char * cp,char ** endp,unsigned int base)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  */
simple_strtoul(const char * cp,char ** endp,unsigned int base)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  */
simple_strtol(const char * cp,char ** endp,unsigned int base)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
simple_strntoll(const char * cp,char ** endp,unsigned int base,size_t max_chars)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  */
simple_strtoll(const char * cp,char ** endp,unsigned int base)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 
skip_atoi(const char ** s)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
put_dec_trunc8(char * buf,unsigned r)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
put_dec_full8(char * buf,unsigned r)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
put_dec(char * buf,unsigned long long n)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
put_dec_full4(char * buf,unsigned r)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
put_dec_helper4(char * buf,unsigned x)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
put_dec(char * buf,unsigned long long n)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  */
num_to_str(char * buf,int size,unsigned long long num,unsigned int width)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
number(char * buf,char * end,unsigned long long num,struct printf_spec spec)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
special_hex_number(char * buf,char * end,unsigned long long num,int size)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 
move_right(char * buf,char * end,unsigned len,unsigned spaces)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
widen_string(char * buf,int n,char * end,struct printf_spec spec)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. */
string_nocheck(char * buf,char * end,const char * s,struct printf_spec spec)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 
err_ptr(char * buf,char * end,void * ptr,struct printf_spec spec)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. */
error_string(char * buf,char * end,const char * s,struct printf_spec spec)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  */
check_pointer_msg(const void * ptr)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 
check_pointer(char ** buf,char * end,const void * ptr,struct printf_spec spec)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
string(char * buf,char * end,const char * s,struct printf_spec spec)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 
pointer_string(char * buf,char * end,const void * ptr,struct printf_spec spec)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 
debug_boot_weak_hash_enable(char * str)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 
fill_ptr_key(struct notifier_block * nb,unsigned long action,void * data)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 
vsprintf_init_hashval(void)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 }
subsys_initcall(vsprintf_init_hashval)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 
ptr_to_hashval(const void * ptr,unsigned long * hashval_out)788 int ptr_to_hashval(const void *ptr, unsigned long *hashval_out)
789 {
790 	return __ptr_to_hashval(ptr, hashval_out);
791 }
792 
ptr_to_id(char * buf,char * end,const void * ptr,struct printf_spec spec)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 
default_pointer(char * buf,char * end,const void * ptr,struct printf_spec spec)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
restricted_pointer(char * buf,char * end,const void * ptr,struct printf_spec spec)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
dentry_name(char * buf,char * end,const struct dentry * d,struct printf_spec spec,const char * fmt)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
file_dentry_name(char * buf,char * end,const struct file * f,struct printf_spec spec,const char * fmt)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
bdev_name(char * buf,char * end,struct block_device * bdev,struct printf_spec spec,const char * fmt)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
symbol_string(char * buf,char * end,void * ptr,struct printf_spec spec,const char * fmt)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
hex_range(char * buf,char * end,u64 start_val,u64 end_val,struct printf_spec spec)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
resource_string(char * buf,char * end,struct resource * res,struct printf_spec spec,const char * fmt)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
range_string(char * buf,char * end,const struct range * range,struct printf_spec spec,const char * fmt)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
hex_string(char * buf,char * end,u8 * addr,struct printf_spec spec,const char * fmt)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
bitmap_string(char * buf,char * end,const unsigned long * bitmap,struct printf_spec spec,const char * fmt)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
bitmap_list_string(char * buf,char * end,const unsigned long * bitmap,struct printf_spec spec,const char * fmt)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
mac_address_string(char * buf,char * end,u8 * addr,struct printf_spec spec,const char * fmt)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
ip4_string(char * p,const u8 * addr,const char * fmt)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
ip6_compressed_string(char * p,const char * addr)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
ip6_string(char * p,const char * addr,const char * fmt)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
ip6_addr_string(char * buf,char * end,const u8 * addr,struct printf_spec spec,const char * fmt)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
ip4_addr_string(char * buf,char * end,const u8 * addr,struct printf_spec spec,const char * fmt)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
ip6_addr_string_sa(char * buf,char * end,const struct sockaddr_in6 * sa,struct printf_spec spec,const char * fmt)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
ip4_addr_string_sa(char * buf,char * end,const struct sockaddr_in * sa,struct printf_spec spec,const char * fmt)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
ip_addr_string(char * buf,char * end,const void * ptr,struct printf_spec spec,const char * fmt)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
escaped_string(char * buf,char * end,u8 * addr,struct printf_spec spec,const char * fmt)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 
va_format(char * buf,char * end,struct va_format * va_fmt,struct printf_spec spec,const char * fmt)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
uuid_string(char * buf,char * end,const u8 * addr,struct printf_spec spec,const char * fmt)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
netdev_bits(char * buf,char * end,const void * addr,struct printf_spec spec,const char * fmt)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
fourcc_string(char * buf,char * end,const u32 * fourcc,struct printf_spec spec,const char * fmt)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
address_val(char * buf,char * end,const void * addr,struct printf_spec spec,const char * fmt)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
date_str(char * buf,char * end,const struct rtc_time * tm,bool r)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
time_str(char * buf,char * end,const struct rtc_time * tm,bool r)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
rtc_str(char * buf,char * end,const struct rtc_time * tm,struct printf_spec spec,const char * fmt)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
time64_str(char * buf,char * end,const time64_t time,struct printf_spec spec,const char * fmt)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
time_and_date(char * buf,char * end,void * ptr,struct printf_spec spec,const char * fmt)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
clock(char * buf,char * end,struct clk * clk,struct printf_spec spec,const char * fmt)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
format_flags(char * buf,char * end,unsigned long flags,const struct trace_print_flags * names)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
format_page_flags(char * buf,char * end,unsigned long flags)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
flags_string(char * buf,char * end,void * flags_ptr,struct printf_spec spec,const char * fmt)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
fwnode_full_name_string(struct fwnode_handle * fwnode,char * buf,char * end)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
device_node_string(char * buf,char * end,struct device_node * dn,struct printf_spec spec,const char * fmt)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
fwnode_string(char * buf,char * end,struct fwnode_handle * fwnode,struct printf_spec spec,const char * fmt)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
resource_or_range(const char * fmt,char * buf,char * end,void * ptr,struct printf_spec spec)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 
no_hash_pointers_enable(char * str)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 /* Used for Rust formatting ('%pA'). */
2288 char *rust_fmt_argument(char *buf, char *end, void *ptr);
2289 
2290 /*
2291  * Show a '%p' thing.  A kernel extension is that the '%p' is followed
2292  * by an extra set of alphanumeric characters that are extended format
2293  * specifiers.
2294  *
2295  * Please update scripts/checkpatch.pl when adding/removing conversion
2296  * characters.  (Search for "check for vsprintf extension").
2297  *
2298  * Right now we handle:
2299  *
2300  * - 'S' For symbolic direct pointers (or function descriptors) with offset
2301  * - 's' For symbolic direct pointers (or function descriptors) without offset
2302  * - '[Ss]R' as above with __builtin_extract_return_addr() translation
2303  * - 'S[R]b' as above with module build ID (for use in backtraces)
2304  * - '[Ff]' %pf and %pF were obsoleted and later removed in favor of
2305  *	    %ps and %pS. Be careful when re-using these specifiers.
2306  * - 'B' For backtraced symbolic direct pointers with offset
2307  * - 'Bb' as above with module build ID (for use in backtraces)
2308  * - 'R' For decoded struct resource, e.g., [mem 0x0-0x1f 64bit pref]
2309  * - 'r' For raw struct resource, e.g., [mem 0x0-0x1f flags 0x201]
2310  * - 'ra' For struct ranges, e.g., [range 0x0000000000000000 - 0x00000000000000ff]
2311  * - 'b[l]' For a bitmap, the number of bits is determined by the field
2312  *       width which must be explicitly specified either as part of the
2313  *       format string '%32b[l]' or through '%*b[l]', [l] selects
2314  *       range-list format instead of hex format
2315  * - 'M' For a 6-byte MAC address, it prints the address in the
2316  *       usual colon-separated hex notation
2317  * - 'm' For a 6-byte MAC address, it prints the hex address without colons
2318  * - 'MF' For a 6-byte MAC FDDI address, it prints the address
2319  *       with a dash-separated hex notation
2320  * - '[mM]R' For a 6-byte MAC address, Reverse order (Bluetooth)
2321  * - 'I' [46] for IPv4/IPv6 addresses printed in the usual way
2322  *       IPv4 uses dot-separated decimal without leading 0's (1.2.3.4)
2323  *       IPv6 uses colon separated network-order 16 bit hex with leading 0's
2324  *       [S][pfs]
2325  *       Generic IPv4/IPv6 address (struct sockaddr *) that falls back to
2326  *       [4] or [6] and is able to print port [p], flowinfo [f], scope [s]
2327  * - 'i' [46] for 'raw' IPv4/IPv6 addresses
2328  *       IPv6 omits the colons (01020304...0f)
2329  *       IPv4 uses dot-separated decimal with leading 0's (010.123.045.006)
2330  *       [S][pfs]
2331  *       Generic IPv4/IPv6 address (struct sockaddr *) that falls back to
2332  *       [4] or [6] and is able to print port [p], flowinfo [f], scope [s]
2333  * - '[Ii][4S][hnbl]' IPv4 addresses in host, network, big or little endian order
2334  * - 'I[6S]c' for IPv6 addresses printed as specified by
2335  *       https://tools.ietf.org/html/rfc5952
2336  * - 'E[achnops]' For an escaped buffer, where rules are defined by combination
2337  *                of the following flags (see string_escape_mem() for the
2338  *                details):
2339  *                  a - ESCAPE_ANY
2340  *                  c - ESCAPE_SPECIAL
2341  *                  h - ESCAPE_HEX
2342  *                  n - ESCAPE_NULL
2343  *                  o - ESCAPE_OCTAL
2344  *                  p - ESCAPE_NP
2345  *                  s - ESCAPE_SPACE
2346  *                By default ESCAPE_ANY_NP is used.
2347  * - 'U' For a 16 byte UUID/GUID, it prints the UUID/GUID in the form
2348  *       "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
2349  *       Options for %pU are:
2350  *         b big endian lower case hex (default)
2351  *         B big endian UPPER case hex
2352  *         l little endian lower case hex
2353  *         L little endian UPPER case hex
2354  *           big endian output byte order is:
2355  *             [0][1][2][3]-[4][5]-[6][7]-[8][9]-[10][11][12][13][14][15]
2356  *           little endian output byte order is:
2357  *             [3][2][1][0]-[5][4]-[7][6]-[8][9]-[10][11][12][13][14][15]
2358  * - 'V' For a struct va_format which contains a format string * and va_list *,
2359  *       call vsnprintf(->format, *->va_list).
2360  *       Implements a "recursive vsnprintf".
2361  *       Do not use this feature without some mechanism to verify the
2362  *       correctness of the format string and va_list arguments.
2363  * - 'K' For a kernel pointer that should be hidden from unprivileged users.
2364  *       Use only for procfs, sysfs and similar files, not printk(); please
2365  *       read the documentation (path below) first.
2366  * - 'NF' For a netdev_features_t
2367  * - '4cc' V4L2 or DRM FourCC code, with endianness and raw numerical value.
2368  * - 'h[CDN]' For a variable-length buffer, it prints it as a hex string with
2369  *            a certain separator (' ' by default):
2370  *              C colon
2371  *              D dash
2372  *              N no separator
2373  *            The maximum supported length is 64 bytes of the input. Consider
2374  *            to use print_hex_dump() for the larger input.
2375  * - 'a[pd]' For address types [p] phys_addr_t, [d] dma_addr_t and derivatives
2376  *           (default assumed to be phys_addr_t, passed by reference)
2377  * - 'd[234]' For a dentry name (optionally 2-4 last components)
2378  * - 'D[234]' Same as 'd' but for a struct file
2379  * - 'g' For block_device name (gendisk + partition number)
2380  * - 't[RT][dt][r][s]' For time and date as represented by:
2381  *      R    struct rtc_time
2382  *      T    time64_t
2383  * - 'C' For a clock, it prints the name (Common Clock Framework) or address
2384  *       (legacy clock framework) of the clock
2385  * - 'Cn' For a clock, it prints the name (Common Clock Framework) or address
2386  *        (legacy clock framework) of the clock
2387  * - 'G' For flags to be printed as a collection of symbolic strings that would
2388  *       construct the specific value. Supported flags given by option:
2389  *       p page flags (see struct page) given as pointer to unsigned long
2390  *       g gfp flags (GFP_* and __GFP_*) given as pointer to gfp_t
2391  *       v vma flags (VM_*) given as pointer to unsigned long
2392  * - 'OF[fnpPcCF]'  For a device tree object
2393  *                  Without any optional arguments prints the full_name
2394  *                  f device node full_name
2395  *                  n device node name
2396  *                  p device node phandle
2397  *                  P device node path spec (name + @unit)
2398  *                  F device node flags
2399  *                  c major compatible string
2400  *                  C full compatible string
2401  * - 'fw[fP]'	For a firmware node (struct fwnode_handle) pointer
2402  *		Without an option prints the full name of the node
2403  *		f full name
2404  *		P node name, including a possible unit address
2405  * - 'x' For printing the address unmodified. Equivalent to "%lx".
2406  *       Please read the documentation (path below) before using!
2407  * - '[ku]s' For a BPF/tracing related format specifier, e.g. used out of
2408  *           bpf_trace_printk() where [ku] prefix specifies either kernel (k)
2409  *           or user (u) memory to probe, and:
2410  *              s a string, equivalent to "%s" on direct vsnprintf() use
2411  *
2412  * ** When making changes please also update:
2413  *	Documentation/core-api/printk-formats.rst
2414  *
2415  * Note: The default behaviour (unadorned %p) is to hash the address,
2416  * rendering it useful as a unique identifier.
2417  *
2418  * There is also a '%pA' format specifier, but it is only intended to be used
2419  * from Rust code to format core::fmt::Arguments. Do *not* use it from C.
2420  * See rust/kernel/print.rs for details.
2421  */
2422 static noinline_for_stack
pointer(const char * fmt,char * buf,char * end,void * ptr,struct printf_spec spec)2423 char *pointer(const char *fmt, char *buf, char *end, void *ptr,
2424 	      struct printf_spec spec)
2425 {
2426 	switch (*fmt) {
2427 	case 'S':
2428 	case 's':
2429 		ptr = dereference_symbol_descriptor(ptr);
2430 		fallthrough;
2431 	case 'B':
2432 		return symbol_string(buf, end, ptr, spec, fmt);
2433 	case 'R':
2434 	case 'r':
2435 		return resource_or_range(fmt, buf, end, ptr, spec);
2436 	case 'h':
2437 		return hex_string(buf, end, ptr, spec, fmt);
2438 	case 'b':
2439 		switch (fmt[1]) {
2440 		case 'l':
2441 			return bitmap_list_string(buf, end, ptr, spec, fmt);
2442 		default:
2443 			return bitmap_string(buf, end, ptr, spec, fmt);
2444 		}
2445 	case 'M':			/* Colon separated: 00:01:02:03:04:05 */
2446 	case 'm':			/* Contiguous: 000102030405 */
2447 					/* [mM]F (FDDI) */
2448 					/* [mM]R (Reverse order; Bluetooth) */
2449 		return mac_address_string(buf, end, ptr, spec, fmt);
2450 	case 'I':			/* Formatted IP supported
2451 					 * 4:	1.2.3.4
2452 					 * 6:	0001:0203:...:0708
2453 					 * 6c:	1::708 or 1::1.2.3.4
2454 					 */
2455 	case 'i':			/* Contiguous:
2456 					 * 4:	001.002.003.004
2457 					 * 6:   000102...0f
2458 					 */
2459 		return ip_addr_string(buf, end, ptr, spec, fmt);
2460 	case 'E':
2461 		return escaped_string(buf, end, ptr, spec, fmt);
2462 	case 'U':
2463 		return uuid_string(buf, end, ptr, spec, fmt);
2464 	case 'V':
2465 		return va_format(buf, end, ptr, spec, fmt);
2466 	case 'K':
2467 		return restricted_pointer(buf, end, ptr, spec);
2468 	case 'N':
2469 		return netdev_bits(buf, end, ptr, spec, fmt);
2470 	case '4':
2471 		return fourcc_string(buf, end, ptr, spec, fmt);
2472 	case 'a':
2473 		return address_val(buf, end, ptr, spec, fmt);
2474 	case 'd':
2475 		return dentry_name(buf, end, ptr, spec, fmt);
2476 	case 't':
2477 		return time_and_date(buf, end, ptr, spec, fmt);
2478 	case 'C':
2479 		return clock(buf, end, ptr, spec, fmt);
2480 	case 'D':
2481 		return file_dentry_name(buf, end, ptr, spec, fmt);
2482 #ifdef CONFIG_BLOCK
2483 	case 'g':
2484 		return bdev_name(buf, end, ptr, spec, fmt);
2485 #endif
2486 
2487 	case 'G':
2488 		return flags_string(buf, end, ptr, spec, fmt);
2489 	case 'O':
2490 		return device_node_string(buf, end, ptr, spec, fmt + 1);
2491 	case 'f':
2492 		return fwnode_string(buf, end, ptr, spec, fmt + 1);
2493 	case 'A':
2494 		if (!IS_ENABLED(CONFIG_RUST)) {
2495 			WARN_ONCE(1, "Please remove %%pA from non-Rust code\n");
2496 			return error_string(buf, end, "(%pA?)", spec);
2497 		}
2498 		return rust_fmt_argument(buf, end, ptr);
2499 	case 'x':
2500 		return pointer_string(buf, end, ptr, spec);
2501 	case 'e':
2502 		/* %pe with a non-ERR_PTR gets treated as plain %p */
2503 		if (!IS_ERR(ptr))
2504 			return default_pointer(buf, end, ptr, spec);
2505 		return err_ptr(buf, end, ptr, spec);
2506 	case 'u':
2507 	case 'k':
2508 		switch (fmt[1]) {
2509 		case 's':
2510 			return string(buf, end, ptr, spec);
2511 		default:
2512 			return error_string(buf, end, "(einval)", spec);
2513 		}
2514 	default:
2515 		return default_pointer(buf, end, ptr, spec);
2516 	}
2517 }
2518 
2519 struct fmt {
2520 	const char *str;
2521 	unsigned char state;	// enum format_state
2522 	unsigned char size;	// size of numbers
2523 };
2524 
2525 #define SPEC_CHAR(x, flag) [(x)-32] = flag
spec_flag(unsigned char c)2526 static unsigned char spec_flag(unsigned char c)
2527 {
2528 	static const unsigned char spec_flag_array[] = {
2529 		SPEC_CHAR(' ', SPACE),
2530 		SPEC_CHAR('#', SPECIAL),
2531 		SPEC_CHAR('+', PLUS),
2532 		SPEC_CHAR('-', LEFT),
2533 		SPEC_CHAR('0', ZEROPAD),
2534 	};
2535 	c -= 32;
2536 	return (c < sizeof(spec_flag_array)) ? spec_flag_array[c] : 0;
2537 }
2538 
2539 /*
2540  * Helper function to decode printf style format.
2541  * Each call decode a token from the format and return the
2542  * number of characters read (or likely the delta where it wants
2543  * to go on the next call).
2544  * The decoded token is returned through the parameters
2545  *
2546  * 'h', 'l', or 'L' for integer fields
2547  * 'z' support added 23/7/1999 S.H.
2548  * 'z' changed to 'Z' --davidm 1/25/99
2549  * 'Z' changed to 'z' --adobriyan 2017-01-25
2550  * 't' added for ptrdiff_t
2551  *
2552  * @fmt: the format string
2553  * @type of the token returned
2554  * @flags: various flags such as +, -, # tokens..
2555  * @field_width: overwritten width
2556  * @base: base of the number (octal, hex, ...)
2557  * @precision: precision of a number
2558  * @qualifier: qualifier of a number (long, size_t, ...)
2559  */
2560 static noinline_for_stack
format_decode(struct fmt fmt,struct printf_spec * spec)2561 struct fmt format_decode(struct fmt fmt, struct printf_spec *spec)
2562 {
2563 	const char *start = fmt.str;
2564 	char flag;
2565 
2566 	/* we finished early by reading the field width */
2567 	if (unlikely(fmt.state == FORMAT_STATE_WIDTH)) {
2568 		if (spec->field_width < 0) {
2569 			spec->field_width = -spec->field_width;
2570 			spec->flags |= LEFT;
2571 		}
2572 		fmt.state = FORMAT_STATE_NONE;
2573 		goto precision;
2574 	}
2575 
2576 	/* we finished early by reading the precision */
2577 	if (unlikely(fmt.state == FORMAT_STATE_PRECISION)) {
2578 		if (spec->precision < 0)
2579 			spec->precision = 0;
2580 
2581 		fmt.state = FORMAT_STATE_NONE;
2582 		goto qualifier;
2583 	}
2584 
2585 	/* By default */
2586 	fmt.state = FORMAT_STATE_NONE;
2587 
2588 	for (; *fmt.str ; fmt.str++) {
2589 		if (*fmt.str == '%')
2590 			break;
2591 	}
2592 
2593 	/* Return the current non-format string */
2594 	if (fmt.str != start || !*fmt.str)
2595 		return fmt;
2596 
2597 	/* Process flags. This also skips the first '%' */
2598 	spec->flags = 0;
2599 	do {
2600 		/* this also skips first '%' */
2601 		flag = spec_flag(*++fmt.str);
2602 		spec->flags |= flag;
2603 	} while (flag);
2604 
2605 	/* get field width */
2606 	spec->field_width = -1;
2607 
2608 	if (isdigit(*fmt.str))
2609 		spec->field_width = skip_atoi(&fmt.str);
2610 	else if (unlikely(*fmt.str == '*')) {
2611 		/* it's the next argument */
2612 		fmt.state = FORMAT_STATE_WIDTH;
2613 		fmt.str++;
2614 		return fmt;
2615 	}
2616 
2617 precision:
2618 	/* get the precision */
2619 	spec->precision = -1;
2620 	if (unlikely(*fmt.str == '.')) {
2621 		fmt.str++;
2622 		if (isdigit(*fmt.str)) {
2623 			spec->precision = skip_atoi(&fmt.str);
2624 			if (spec->precision < 0)
2625 				spec->precision = 0;
2626 		} else if (*fmt.str == '*') {
2627 			/* it's the next argument */
2628 			fmt.state = FORMAT_STATE_PRECISION;
2629 			fmt.str++;
2630 			return fmt;
2631 		}
2632 	}
2633 
2634 qualifier:
2635 	/* Set up default numeric format */
2636 	spec->base = 10;
2637 	fmt.state = FORMAT_STATE_NUM;
2638 	fmt.size = sizeof(int);
2639 	static const struct format_state {
2640 		unsigned char state;
2641 		unsigned char size;
2642 		unsigned char flags_or_double_size;
2643 		unsigned char base;
2644 	} lookup_state[256] = {
2645 		// Length
2646 		['l'] = { 0, sizeof(long), sizeof(long long) },
2647 		['L'] = { 0, sizeof(long long) },
2648 		['h'] = { 0, sizeof(short), sizeof(char) },
2649 		['H'] = { 0, sizeof(char) },	// Questionable historical
2650 		['z'] = { 0, sizeof(size_t) },
2651 		['t'] = { 0, sizeof(ptrdiff_t) },
2652 
2653 		// Non-numeric formats
2654 		['c'] = { FORMAT_STATE_CHAR },
2655 		['s'] = { FORMAT_STATE_STR },
2656 		['p'] = { FORMAT_STATE_PTR },
2657 		['%'] = { FORMAT_STATE_PERCENT_CHAR },
2658 
2659 		// Numerics
2660 		['o'] = { FORMAT_STATE_NUM, 0, 0, 8 },
2661 		['x'] = { FORMAT_STATE_NUM, 0, SMALL, 16 },
2662 		['X'] = { FORMAT_STATE_NUM, 0, 0, 16 },
2663 		['d'] = { FORMAT_STATE_NUM, 0, SIGN, 10 },
2664 		['i'] = { FORMAT_STATE_NUM, 0, SIGN, 10 },
2665 		['u'] = { FORMAT_STATE_NUM, 0, 0, 10, },
2666 
2667 		/*
2668 		 * Since %n poses a greater security risk than
2669 		 * utility, treat it as any other invalid or
2670 		 * unsupported format specifier.
2671 		 */
2672 	};
2673 
2674 	const struct format_state *p = lookup_state + (u8)*fmt.str;
2675 	if (p->size) {
2676 		fmt.size = p->size;
2677 		if (p->flags_or_double_size && fmt.str[0] == fmt.str[1]) {
2678 			fmt.size = p->flags_or_double_size;
2679 			fmt.str++;
2680 		}
2681 		fmt.str++;
2682 		p = lookup_state + *fmt.str;
2683 	}
2684 	if (p->state) {
2685 		if (p->base)
2686 			spec->base = p->base;
2687 		spec->flags |= p->flags_or_double_size;
2688 		fmt.state = p->state;
2689 		fmt.str++;
2690 		return fmt;
2691 	}
2692 
2693 	WARN_ONCE(1, "Please remove unsupported %%%c in format string\n", *fmt.str);
2694 	fmt.state = FORMAT_STATE_INVALID;
2695 	return fmt;
2696 }
2697 
2698 static void
set_field_width(struct printf_spec * spec,int width)2699 set_field_width(struct printf_spec *spec, int width)
2700 {
2701 	spec->field_width = width;
2702 	if (WARN_ONCE(spec->field_width != width, "field width %d too large", width)) {
2703 		spec->field_width = clamp(width, -FIELD_WIDTH_MAX, FIELD_WIDTH_MAX);
2704 	}
2705 }
2706 
2707 static void
set_precision(struct printf_spec * spec,int prec)2708 set_precision(struct printf_spec *spec, int prec)
2709 {
2710 	spec->precision = prec;
2711 	if (WARN_ONCE(spec->precision != prec, "precision %d too large", prec)) {
2712 		spec->precision = clamp(prec, 0, PRECISION_MAX);
2713 	}
2714 }
2715 
2716 /*
2717  * Turn a 1/2/4-byte value into a 64-bit one for printing: truncate
2718  * as necessary and deal with signedness.
2719  *
2720  * 'size' is the size of the value in bytes.
2721  */
convert_num_spec(unsigned int val,int size,struct printf_spec spec)2722 static unsigned long long convert_num_spec(unsigned int val, int size, struct printf_spec spec)
2723 {
2724 	unsigned int shift = 32 - size*8;
2725 
2726 	val <<= shift;
2727 	if (!(spec.flags & SIGN))
2728 		return val >> shift;
2729 	return (int)val >> shift;
2730 }
2731 
2732 /**
2733  * vsnprintf - Format a string and place it in a buffer
2734  * @buf: The buffer to place the result into
2735  * @size: The size of the buffer, including the trailing null space
2736  * @fmt_str: The format string to use
2737  * @args: Arguments for the format string
2738  *
2739  * This function generally follows C99 vsnprintf, but has some
2740  * extensions and a few limitations:
2741  *
2742  *  - ``%n`` is unsupported
2743  *  - ``%p*`` is handled by pointer()
2744  *
2745  * See pointer() or Documentation/core-api/printk-formats.rst for more
2746  * extensive description.
2747  *
2748  * **Please update the documentation in both places when making changes**
2749  *
2750  * The return value is the number of characters which would
2751  * be generated for the given input, excluding the trailing
2752  * '\0', as per ISO C99. If you want to have the exact
2753  * number of characters written into @buf as return value
2754  * (not including the trailing '\0'), use vscnprintf(). If the
2755  * return is greater than or equal to @size, the resulting
2756  * string is truncated.
2757  *
2758  * If you're not already dealing with a va_list consider using snprintf().
2759  */
vsnprintf(char * buf,size_t size,const char * fmt_str,va_list args)2760 int vsnprintf(char *buf, size_t size, const char *fmt_str, va_list args)
2761 {
2762 	char *str, *end;
2763 	struct printf_spec spec = {0};
2764 	struct fmt fmt = {
2765 		.str = fmt_str,
2766 		.state = FORMAT_STATE_NONE,
2767 	};
2768 
2769 	/* Reject out-of-range values early.  Large positive sizes are
2770 	   used for unknown buffer sizes. */
2771 	if (WARN_ON_ONCE(size > INT_MAX))
2772 		return 0;
2773 
2774 	str = buf;
2775 	end = buf + size;
2776 
2777 	/* Make sure end is always >= buf */
2778 	if (end < buf) {
2779 		end = ((void *)-1);
2780 		size = end - buf;
2781 	}
2782 
2783 	while (*fmt.str) {
2784 		const char *old_fmt = fmt.str;
2785 
2786 		fmt = format_decode(fmt, &spec);
2787 
2788 		switch (fmt.state) {
2789 		case FORMAT_STATE_NONE: {
2790 			int read = fmt.str - old_fmt;
2791 			if (str < end) {
2792 				int copy = read;
2793 				if (copy > end - str)
2794 					copy = end - str;
2795 				memcpy(str, old_fmt, copy);
2796 			}
2797 			str += read;
2798 			continue;
2799 		}
2800 
2801 		case FORMAT_STATE_NUM: {
2802 			unsigned long long num;
2803 			if (fmt.size <= sizeof(int))
2804 				num = convert_num_spec(va_arg(args, int), fmt.size, spec);
2805 			else
2806 				num = va_arg(args, long long);
2807 			str = number(str, end, num, spec);
2808 			continue;
2809 		}
2810 
2811 		case FORMAT_STATE_WIDTH:
2812 			set_field_width(&spec, va_arg(args, int));
2813 			continue;
2814 
2815 		case FORMAT_STATE_PRECISION:
2816 			set_precision(&spec, va_arg(args, int));
2817 			continue;
2818 
2819 		case FORMAT_STATE_CHAR: {
2820 			char c;
2821 
2822 			if (!(spec.flags & LEFT)) {
2823 				while (--spec.field_width > 0) {
2824 					if (str < end)
2825 						*str = ' ';
2826 					++str;
2827 
2828 				}
2829 			}
2830 			c = (unsigned char) va_arg(args, int);
2831 			if (str < end)
2832 				*str = c;
2833 			++str;
2834 			while (--spec.field_width > 0) {
2835 				if (str < end)
2836 					*str = ' ';
2837 				++str;
2838 			}
2839 			continue;
2840 		}
2841 
2842 		case FORMAT_STATE_STR:
2843 			str = string(str, end, va_arg(args, char *), spec);
2844 			continue;
2845 
2846 		case FORMAT_STATE_PTR:
2847 			str = pointer(fmt.str, str, end, va_arg(args, void *),
2848 				      spec);
2849 			while (isalnum(*fmt.str))
2850 				fmt.str++;
2851 			continue;
2852 
2853 		case FORMAT_STATE_PERCENT_CHAR:
2854 			if (str < end)
2855 				*str = '%';
2856 			++str;
2857 			continue;
2858 
2859 		default:
2860 			/*
2861 			 * Presumably the arguments passed gcc's type
2862 			 * checking, but there is no safe or sane way
2863 			 * for us to continue parsing the format and
2864 			 * fetching from the va_list; the remaining
2865 			 * specifiers and arguments would be out of
2866 			 * sync.
2867 			 */
2868 			goto out;
2869 		}
2870 	}
2871 
2872 out:
2873 	if (size > 0) {
2874 		if (str < end)
2875 			*str = '\0';
2876 		else
2877 			end[-1] = '\0';
2878 	}
2879 
2880 	/* the trailing null byte doesn't count towards the total */
2881 	return str-buf;
2882 
2883 }
2884 EXPORT_SYMBOL(vsnprintf);
2885 
2886 /**
2887  * vscnprintf - Format a string and place it in a buffer
2888  * @buf: The buffer to place the result into
2889  * @size: The size of the buffer, including the trailing null space
2890  * @fmt: The format string to use
2891  * @args: Arguments for the format string
2892  *
2893  * The return value is the number of characters which have been written into
2894  * the @buf not including the trailing '\0'. If @size is == 0 the function
2895  * returns 0.
2896  *
2897  * If you're not already dealing with a va_list consider using scnprintf().
2898  *
2899  * See the vsnprintf() documentation for format string extensions over C99.
2900  */
vscnprintf(char * buf,size_t size,const char * fmt,va_list args)2901 int vscnprintf(char *buf, size_t size, const char *fmt, va_list args)
2902 {
2903 	int i;
2904 
2905 	if (unlikely(!size))
2906 		return 0;
2907 
2908 	i = vsnprintf(buf, size, fmt, args);
2909 
2910 	if (likely(i < size))
2911 		return i;
2912 
2913 	return size - 1;
2914 }
2915 EXPORT_SYMBOL(vscnprintf);
2916 
2917 /**
2918  * snprintf - Format a string and place it in a buffer
2919  * @buf: The buffer to place the result into
2920  * @size: The size of the buffer, including the trailing null space
2921  * @fmt: The format string to use
2922  * @...: Arguments for the format string
2923  *
2924  * The return value is the number of characters which would be
2925  * generated for the given input, excluding the trailing null,
2926  * as per ISO C99.  If the return is greater than or equal to
2927  * @size, the resulting string is truncated.
2928  *
2929  * See the vsnprintf() documentation for format string extensions over C99.
2930  */
snprintf(char * buf,size_t size,const char * fmt,...)2931 int snprintf(char *buf, size_t size, const char *fmt, ...)
2932 {
2933 	va_list args;
2934 	int i;
2935 
2936 	va_start(args, fmt);
2937 	i = vsnprintf(buf, size, fmt, args);
2938 	va_end(args);
2939 
2940 	return i;
2941 }
2942 EXPORT_SYMBOL(snprintf);
2943 
2944 /**
2945  * scnprintf - Format a string and place it in a buffer
2946  * @buf: The buffer to place the result into
2947  * @size: The size of the buffer, including the trailing null space
2948  * @fmt: The format string to use
2949  * @...: Arguments for the format string
2950  *
2951  * The return value is the number of characters written into @buf not including
2952  * the trailing '\0'. If @size is == 0 the function returns 0.
2953  */
2954 
scnprintf(char * buf,size_t size,const char * fmt,...)2955 int scnprintf(char *buf, size_t size, const char *fmt, ...)
2956 {
2957 	va_list args;
2958 	int i;
2959 
2960 	va_start(args, fmt);
2961 	i = vscnprintf(buf, size, fmt, args);
2962 	va_end(args);
2963 
2964 	return i;
2965 }
2966 EXPORT_SYMBOL(scnprintf);
2967 
2968 /**
2969  * vsprintf - Format a string and place it in a buffer
2970  * @buf: The buffer to place the result into
2971  * @fmt: The format string to use
2972  * @args: Arguments for the format string
2973  *
2974  * The function returns the number of characters written
2975  * into @buf. Use vsnprintf() or vscnprintf() in order to avoid
2976  * buffer overflows.
2977  *
2978  * If you're not already dealing with a va_list consider using sprintf().
2979  *
2980  * See the vsnprintf() documentation for format string extensions over C99.
2981  */
vsprintf(char * buf,const char * fmt,va_list args)2982 int vsprintf(char *buf, const char *fmt, va_list args)
2983 {
2984 	return vsnprintf(buf, INT_MAX, fmt, args);
2985 }
2986 EXPORT_SYMBOL(vsprintf);
2987 
2988 /**
2989  * sprintf - Format a string and place it in a buffer
2990  * @buf: The buffer to place the result into
2991  * @fmt: The format string to use
2992  * @...: Arguments for the format string
2993  *
2994  * The function returns the number of characters written
2995  * into @buf. Use snprintf() or scnprintf() in order to avoid
2996  * buffer overflows.
2997  *
2998  * See the vsnprintf() documentation for format string extensions over C99.
2999  */
sprintf(char * buf,const char * fmt,...)3000 int sprintf(char *buf, const char *fmt, ...)
3001 {
3002 	va_list args;
3003 	int i;
3004 
3005 	va_start(args, fmt);
3006 	i = vsnprintf(buf, INT_MAX, fmt, args);
3007 	va_end(args);
3008 
3009 	return i;
3010 }
3011 EXPORT_SYMBOL(sprintf);
3012 
3013 #ifdef CONFIG_BINARY_PRINTF
3014 /*
3015  * bprintf service:
3016  * vbin_printf() - VA arguments to binary data
3017  * bstr_printf() - Binary data to text string
3018  */
3019 
3020 /**
3021  * vbin_printf - Parse a format string and place args' binary value in a buffer
3022  * @bin_buf: The buffer to place args' binary value
3023  * @size: The size of the buffer(by words(32bits), not characters)
3024  * @fmt_str: The format string to use
3025  * @args: Arguments for the format string
3026  *
3027  * The format follows C99 vsnprintf, except %n is ignored, and its argument
3028  * is skipped.
3029  *
3030  * The return value is the number of words(32bits) which would be generated for
3031  * the given input.
3032  *
3033  * NOTE:
3034  * If the return value is greater than @size, the resulting bin_buf is NOT
3035  * valid for bstr_printf().
3036  */
vbin_printf(u32 * bin_buf,size_t size,const char * fmt_str,va_list args)3037 int vbin_printf(u32 *bin_buf, size_t size, const char *fmt_str, va_list args)
3038 {
3039 	struct fmt fmt = {
3040 		.str = fmt_str,
3041 		.state = FORMAT_STATE_NONE,
3042 	};
3043 	struct printf_spec spec = {0};
3044 	char *str, *end;
3045 	int width;
3046 
3047 	str = (char *)bin_buf;
3048 	end = (char *)(bin_buf + size);
3049 
3050 #define save_arg(type)							\
3051 ({									\
3052 	unsigned long long value;					\
3053 	if (sizeof(type) == 8) {					\
3054 		unsigned long long val8;				\
3055 		str = PTR_ALIGN(str, sizeof(u32));			\
3056 		val8 = va_arg(args, unsigned long long);		\
3057 		if (str + sizeof(type) <= end) {			\
3058 			*(u32 *)str = *(u32 *)&val8;			\
3059 			*(u32 *)(str + 4) = *((u32 *)&val8 + 1);	\
3060 		}							\
3061 		value = val8;						\
3062 	} else {							\
3063 		unsigned int val4;					\
3064 		str = PTR_ALIGN(str, sizeof(type));			\
3065 		val4 = va_arg(args, int);				\
3066 		if (str + sizeof(type) <= end)				\
3067 			*(typeof(type) *)str = (type)(long)val4;	\
3068 		value = (unsigned long long)val4;			\
3069 	}								\
3070 	str += sizeof(type);						\
3071 	value;								\
3072 })
3073 
3074 	while (*fmt.str) {
3075 		fmt = format_decode(fmt, &spec);
3076 
3077 		switch (fmt.state) {
3078 		case FORMAT_STATE_NONE:
3079 		case FORMAT_STATE_PERCENT_CHAR:
3080 			break;
3081 		case FORMAT_STATE_INVALID:
3082 			goto out;
3083 
3084 		case FORMAT_STATE_WIDTH:
3085 		case FORMAT_STATE_PRECISION:
3086 			width = (int)save_arg(int);
3087 			/* Pointers may require the width */
3088 			if (*fmt.str == 'p')
3089 				set_field_width(&spec, width);
3090 			break;
3091 
3092 		case FORMAT_STATE_CHAR:
3093 			save_arg(char);
3094 			break;
3095 
3096 		case FORMAT_STATE_STR: {
3097 			const char *save_str = va_arg(args, char *);
3098 			const char *err_msg;
3099 			size_t len;
3100 
3101 			err_msg = check_pointer_msg(save_str);
3102 			if (err_msg)
3103 				save_str = err_msg;
3104 
3105 			len = strlen(save_str) + 1;
3106 			if (str + len < end)
3107 				memcpy(str, save_str, len);
3108 			str += len;
3109 			break;
3110 		}
3111 
3112 		case FORMAT_STATE_PTR:
3113 			/* Dereferenced pointers must be done now */
3114 			switch (*fmt.str) {
3115 			/* Dereference of functions is still OK */
3116 			case 'S':
3117 			case 's':
3118 			case 'x':
3119 			case 'K':
3120 			case 'e':
3121 				save_arg(void *);
3122 				break;
3123 			default:
3124 				if (!isalnum(*fmt.str)) {
3125 					save_arg(void *);
3126 					break;
3127 				}
3128 				str = pointer(fmt.str, str, end, va_arg(args, void *),
3129 					      spec);
3130 				if (str + 1 < end)
3131 					*str++ = '\0';
3132 				else
3133 					end[-1] = '\0'; /* Must be nul terminated */
3134 			}
3135 			/* skip all alphanumeric pointer suffixes */
3136 			while (isalnum(*fmt.str))
3137 				fmt.str++;
3138 			break;
3139 
3140 		case FORMAT_STATE_NUM:
3141 			if (fmt.size > sizeof(int)) {
3142 				save_arg(long long);
3143 			} else {
3144 				save_arg(int);
3145 			}
3146 		}
3147 	}
3148 
3149 out:
3150 	return (u32 *)(PTR_ALIGN(str, sizeof(u32))) - bin_buf;
3151 #undef save_arg
3152 }
3153 EXPORT_SYMBOL_GPL(vbin_printf);
3154 
3155 /**
3156  * bstr_printf - Format a string from binary arguments and place it in a buffer
3157  * @buf: The buffer to place the result into
3158  * @size: The size of the buffer, including the trailing null space
3159  * @fmt_str: The format string to use
3160  * @bin_buf: Binary arguments for the format string
3161  *
3162  * This function like C99 vsnprintf, but the difference is that vsnprintf gets
3163  * arguments from stack, and bstr_printf gets arguments from @bin_buf which is
3164  * a binary buffer that generated by vbin_printf.
3165  *
3166  * The format follows C99 vsnprintf, but has some extensions:
3167  *  see vsnprintf comment for details.
3168  *
3169  * The return value is the number of characters which would
3170  * be generated for the given input, excluding the trailing
3171  * '\0', as per ISO C99. If you want to have the exact
3172  * number of characters written into @buf as return value
3173  * (not including the trailing '\0'), use vscnprintf(). If the
3174  * return is greater than or equal to @size, the resulting
3175  * string is truncated.
3176  */
bstr_printf(char * buf,size_t size,const char * fmt_str,const u32 * bin_buf)3177 int bstr_printf(char *buf, size_t size, const char *fmt_str, const u32 *bin_buf)
3178 {
3179 	struct fmt fmt = {
3180 		.str = fmt_str,
3181 		.state = FORMAT_STATE_NONE,
3182 	};
3183 	struct printf_spec spec = {0};
3184 	char *str, *end;
3185 	const char *args = (const char *)bin_buf;
3186 
3187 	if (WARN_ON_ONCE(size > INT_MAX))
3188 		return 0;
3189 
3190 	str = buf;
3191 	end = buf + size;
3192 
3193 #define get_arg(type)							\
3194 ({									\
3195 	typeof(type) value;						\
3196 	if (sizeof(type) == 8) {					\
3197 		args = PTR_ALIGN(args, sizeof(u32));			\
3198 		*(u32 *)&value = *(u32 *)args;				\
3199 		*((u32 *)&value + 1) = *(u32 *)(args + 4);		\
3200 	} else {							\
3201 		args = PTR_ALIGN(args, sizeof(type));			\
3202 		value = *(typeof(type) *)args;				\
3203 	}								\
3204 	args += sizeof(type);						\
3205 	value;								\
3206 })
3207 
3208 	/* Make sure end is always >= buf */
3209 	if (end < buf) {
3210 		end = ((void *)-1);
3211 		size = end - buf;
3212 	}
3213 
3214 	while (*fmt.str) {
3215 		const char *old_fmt = fmt.str;
3216 		unsigned long long num;
3217 
3218 		fmt = format_decode(fmt, &spec);
3219 		switch (fmt.state) {
3220 		case FORMAT_STATE_NONE: {
3221 			int read = fmt.str - old_fmt;
3222 			if (str < end) {
3223 				int copy = read;
3224 				if (copy > end - str)
3225 					copy = end - str;
3226 				memcpy(str, old_fmt, copy);
3227 			}
3228 			str += read;
3229 			continue;
3230 		}
3231 
3232 		case FORMAT_STATE_WIDTH:
3233 			set_field_width(&spec, get_arg(int));
3234 			continue;
3235 
3236 		case FORMAT_STATE_PRECISION:
3237 			set_precision(&spec, get_arg(int));
3238 			continue;
3239 
3240 		case FORMAT_STATE_CHAR: {
3241 			char c;
3242 
3243 			if (!(spec.flags & LEFT)) {
3244 				while (--spec.field_width > 0) {
3245 					if (str < end)
3246 						*str = ' ';
3247 					++str;
3248 				}
3249 			}
3250 			c = (unsigned char) get_arg(char);
3251 			if (str < end)
3252 				*str = c;
3253 			++str;
3254 			while (--spec.field_width > 0) {
3255 				if (str < end)
3256 					*str = ' ';
3257 				++str;
3258 			}
3259 			continue;
3260 		}
3261 
3262 		case FORMAT_STATE_STR: {
3263 			const char *str_arg = args;
3264 			args += strlen(str_arg) + 1;
3265 			str = string(str, end, (char *)str_arg, spec);
3266 			continue;
3267 		}
3268 
3269 		case FORMAT_STATE_PTR: {
3270 			bool process = false;
3271 			int copy, len;
3272 			/* Non function dereferences were already done */
3273 			switch (*fmt.str) {
3274 			case 'S':
3275 			case 's':
3276 			case 'x':
3277 			case 'K':
3278 			case 'e':
3279 				process = true;
3280 				break;
3281 			default:
3282 				if (!isalnum(*fmt.str)) {
3283 					process = true;
3284 					break;
3285 				}
3286 				/* Pointer dereference was already processed */
3287 				if (str < end) {
3288 					len = copy = strlen(args);
3289 					if (copy > end - str)
3290 						copy = end - str;
3291 					memcpy(str, args, copy);
3292 					str += len;
3293 					args += len + 1;
3294 				}
3295 			}
3296 			if (process)
3297 				str = pointer(fmt.str, str, end, get_arg(void *), spec);
3298 
3299 			while (isalnum(*fmt.str))
3300 				fmt.str++;
3301 			continue;
3302 		}
3303 
3304 		case FORMAT_STATE_PERCENT_CHAR:
3305 			if (str < end)
3306 				*str = '%';
3307 			++str;
3308 			continue;
3309 
3310 		case FORMAT_STATE_INVALID:
3311 			goto out;
3312 
3313 		case FORMAT_STATE_NUM:
3314 			if (fmt.size > sizeof(int)) {
3315 				num = get_arg(long long);
3316 			} else {
3317 				num = convert_num_spec(get_arg(int), fmt.size, spec);
3318 			}
3319 			str = number(str, end, num, spec);
3320 			continue;
3321 		}
3322 	} /* while(*fmt.str) */
3323 
3324 out:
3325 	if (size > 0) {
3326 		if (str < end)
3327 			*str = '\0';
3328 		else
3329 			end[-1] = '\0';
3330 	}
3331 
3332 #undef get_arg
3333 
3334 	/* the trailing null byte doesn't count towards the total */
3335 	return str - buf;
3336 }
3337 EXPORT_SYMBOL_GPL(bstr_printf);
3338 
3339 #endif /* CONFIG_BINARY_PRINTF */
3340 
3341 /**
3342  * vsscanf - Unformat a buffer into a list of arguments
3343  * @buf:	input buffer
3344  * @fmt:	format of buffer
3345  * @args:	arguments
3346  */
vsscanf(const char * buf,const char * fmt,va_list args)3347 int vsscanf(const char *buf, const char *fmt, va_list args)
3348 {
3349 	const char *str = buf;
3350 	char *next;
3351 	char digit;
3352 	int num = 0;
3353 	u8 qualifier;
3354 	unsigned int base;
3355 	union {
3356 		long long s;
3357 		unsigned long long u;
3358 	} val;
3359 	s16 field_width;
3360 	bool is_sign;
3361 
3362 	while (*fmt) {
3363 		/* skip any white space in format */
3364 		/* white space in format matches any amount of
3365 		 * white space, including none, in the input.
3366 		 */
3367 		if (isspace(*fmt)) {
3368 			fmt = skip_spaces(++fmt);
3369 			str = skip_spaces(str);
3370 		}
3371 
3372 		/* anything that is not a conversion must match exactly */
3373 		if (*fmt != '%' && *fmt) {
3374 			if (*fmt++ != *str++)
3375 				break;
3376 			continue;
3377 		}
3378 
3379 		if (!*fmt)
3380 			break;
3381 		++fmt;
3382 
3383 		/* skip this conversion.
3384 		 * advance both strings to next white space
3385 		 */
3386 		if (*fmt == '*') {
3387 			if (!*str)
3388 				break;
3389 			while (!isspace(*fmt) && *fmt != '%' && *fmt) {
3390 				/* '%*[' not yet supported, invalid format */
3391 				if (*fmt == '[')
3392 					return num;
3393 				fmt++;
3394 			}
3395 			while (!isspace(*str) && *str)
3396 				str++;
3397 			continue;
3398 		}
3399 
3400 		/* get field width */
3401 		field_width = -1;
3402 		if (isdigit(*fmt)) {
3403 			field_width = skip_atoi(&fmt);
3404 			if (field_width <= 0)
3405 				break;
3406 		}
3407 
3408 		/* get conversion qualifier */
3409 		qualifier = -1;
3410 		if (*fmt == 'h' || _tolower(*fmt) == 'l' ||
3411 		    *fmt == 'z') {
3412 			qualifier = *fmt++;
3413 			if (unlikely(qualifier == *fmt)) {
3414 				if (qualifier == 'h') {
3415 					qualifier = 'H';
3416 					fmt++;
3417 				} else if (qualifier == 'l') {
3418 					qualifier = 'L';
3419 					fmt++;
3420 				}
3421 			}
3422 		}
3423 
3424 		if (!*fmt)
3425 			break;
3426 
3427 		if (*fmt == 'n') {
3428 			/* return number of characters read so far */
3429 			*va_arg(args, int *) = str - buf;
3430 			++fmt;
3431 			continue;
3432 		}
3433 
3434 		if (!*str)
3435 			break;
3436 
3437 		base = 10;
3438 		is_sign = false;
3439 
3440 		switch (*fmt++) {
3441 		case 'c':
3442 		{
3443 			char *s = (char *)va_arg(args, char*);
3444 			if (field_width == -1)
3445 				field_width = 1;
3446 			do {
3447 				*s++ = *str++;
3448 			} while (--field_width > 0 && *str);
3449 			num++;
3450 		}
3451 		continue;
3452 		case 's':
3453 		{
3454 			char *s = (char *)va_arg(args, char *);
3455 			if (field_width == -1)
3456 				field_width = SHRT_MAX;
3457 			/* first, skip leading white space in buffer */
3458 			str = skip_spaces(str);
3459 
3460 			/* now copy until next white space */
3461 			while (*str && !isspace(*str) && field_width--)
3462 				*s++ = *str++;
3463 			*s = '\0';
3464 			num++;
3465 		}
3466 		continue;
3467 		/*
3468 		 * Warning: This implementation of the '[' conversion specifier
3469 		 * deviates from its glibc counterpart in the following ways:
3470 		 * (1) It does NOT support ranges i.e. '-' is NOT a special
3471 		 *     character
3472 		 * (2) It cannot match the closing bracket ']' itself
3473 		 * (3) A field width is required
3474 		 * (4) '%*[' (discard matching input) is currently not supported
3475 		 *
3476 		 * Example usage:
3477 		 * ret = sscanf("00:0a:95","%2[^:]:%2[^:]:%2[^:]",
3478 		 *		buf1, buf2, buf3);
3479 		 * if (ret < 3)
3480 		 *    // etc..
3481 		 */
3482 		case '[':
3483 		{
3484 			char *s = (char *)va_arg(args, char *);
3485 			DECLARE_BITMAP(set, 256) = {0};
3486 			unsigned int len = 0;
3487 			bool negate = (*fmt == '^');
3488 
3489 			/* field width is required */
3490 			if (field_width == -1)
3491 				return num;
3492 
3493 			if (negate)
3494 				++fmt;
3495 
3496 			for ( ; *fmt && *fmt != ']'; ++fmt, ++len)
3497 				__set_bit((u8)*fmt, set);
3498 
3499 			/* no ']' or no character set found */
3500 			if (!*fmt || !len)
3501 				return num;
3502 			++fmt;
3503 
3504 			if (negate) {
3505 				bitmap_complement(set, set, 256);
3506 				/* exclude null '\0' byte */
3507 				__clear_bit(0, set);
3508 			}
3509 
3510 			/* match must be non-empty */
3511 			if (!test_bit((u8)*str, set))
3512 				return num;
3513 
3514 			while (test_bit((u8)*str, set) && field_width--)
3515 				*s++ = *str++;
3516 			*s = '\0';
3517 			++num;
3518 		}
3519 		continue;
3520 		case 'o':
3521 			base = 8;
3522 			break;
3523 		case 'x':
3524 		case 'X':
3525 			base = 16;
3526 			break;
3527 		case 'i':
3528 			base = 0;
3529 			fallthrough;
3530 		case 'd':
3531 			is_sign = true;
3532 			fallthrough;
3533 		case 'u':
3534 			break;
3535 		case '%':
3536 			/* looking for '%' in str */
3537 			if (*str++ != '%')
3538 				return num;
3539 			continue;
3540 		default:
3541 			/* invalid format; stop here */
3542 			return num;
3543 		}
3544 
3545 		/* have some sort of integer conversion.
3546 		 * first, skip white space in buffer.
3547 		 */
3548 		str = skip_spaces(str);
3549 
3550 		digit = *str;
3551 		if (is_sign && digit == '-') {
3552 			if (field_width == 1)
3553 				break;
3554 
3555 			digit = *(str + 1);
3556 		}
3557 
3558 		if (!digit
3559 		    || (base == 16 && !isxdigit(digit))
3560 		    || (base == 10 && !isdigit(digit))
3561 		    || (base == 8 && !isodigit(digit))
3562 		    || (base == 0 && !isdigit(digit)))
3563 			break;
3564 
3565 		if (is_sign)
3566 			val.s = simple_strntoll(str, &next, base,
3567 						field_width >= 0 ? field_width : INT_MAX);
3568 		else
3569 			val.u = simple_strntoull(str, &next, base,
3570 						 field_width >= 0 ? field_width : INT_MAX);
3571 
3572 		switch (qualifier) {
3573 		case 'H':	/* that's 'hh' in format */
3574 			if (is_sign)
3575 				*va_arg(args, signed char *) = val.s;
3576 			else
3577 				*va_arg(args, unsigned char *) = val.u;
3578 			break;
3579 		case 'h':
3580 			if (is_sign)
3581 				*va_arg(args, short *) = val.s;
3582 			else
3583 				*va_arg(args, unsigned short *) = val.u;
3584 			break;
3585 		case 'l':
3586 			if (is_sign)
3587 				*va_arg(args, long *) = val.s;
3588 			else
3589 				*va_arg(args, unsigned long *) = val.u;
3590 			break;
3591 		case 'L':
3592 			if (is_sign)
3593 				*va_arg(args, long long *) = val.s;
3594 			else
3595 				*va_arg(args, unsigned long long *) = val.u;
3596 			break;
3597 		case 'z':
3598 			*va_arg(args, size_t *) = val.u;
3599 			break;
3600 		default:
3601 			if (is_sign)
3602 				*va_arg(args, int *) = val.s;
3603 			else
3604 				*va_arg(args, unsigned int *) = val.u;
3605 			break;
3606 		}
3607 		num++;
3608 
3609 		if (!next)
3610 			break;
3611 		str = next;
3612 	}
3613 
3614 	return num;
3615 }
3616 EXPORT_SYMBOL(vsscanf);
3617 
3618 /**
3619  * sscanf - Unformat a buffer into a list of arguments
3620  * @buf:	input buffer
3621  * @fmt:	formatting of buffer
3622  * @...:	resulting arguments
3623  */
sscanf(const char * buf,const char * fmt,...)3624 int sscanf(const char *buf, const char *fmt, ...)
3625 {
3626 	va_list args;
3627 	int i;
3628 
3629 	va_start(args, fmt);
3630 	i = vsscanf(buf, fmt, args);
3631 	va_end(args);
3632 
3633 	return i;
3634 }
3635 EXPORT_SYMBOL(sscanf);
3636