Thomas Gleixner | 457c899 | 2019-05-19 13:08:55 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
James Bottomley | 3c9f368 | 2008-08-31 10:13:54 -0500 | [diff] [blame] | 2 | /* |
| 3 | * Helpers for formatting and printing strings |
| 4 | * |
| 5 | * Copyright 31 August 2008 James Bottomley |
Andy Shevchenko | 16c7fa0 | 2013-04-30 15:27:30 -0700 | [diff] [blame] | 6 | * Copyright (C) 2013, Intel Corporation |
James Bottomley | 3c9f368 | 2008-08-31 10:13:54 -0500 | [diff] [blame] | 7 | */ |
James Bottomley | b9f28d8 | 2015-03-05 18:47:01 -0800 | [diff] [blame] | 8 | #include <linux/bug.h> |
James Bottomley | 3c9f368 | 2008-08-31 10:13:54 -0500 | [diff] [blame] | 9 | #include <linux/kernel.h> |
| 10 | #include <linux/math64.h> |
Paul Gortmaker | 8bc3bcc | 2011-11-16 21:29:17 -0500 | [diff] [blame] | 11 | #include <linux/export.h> |
Andy Shevchenko | 16c7fa0 | 2013-04-30 15:27:30 -0700 | [diff] [blame] | 12 | #include <linux/ctype.h> |
Andy Shevchenko | acdb89b | 2021-11-05 14:42:25 +0200 | [diff] [blame] | 13 | #include <linux/device.h> |
Andy Shevchenko | c825038 | 2014-10-13 15:55:16 -0700 | [diff] [blame] | 14 | #include <linux/errno.h> |
Kees Cook | 2198531 | 2016-04-20 15:46:25 -0700 | [diff] [blame] | 15 | #include <linux/fs.h> |
| 16 | #include <linux/limits.h> |
Kees Cook | 0d04432 | 2016-04-20 15:46:24 -0700 | [diff] [blame] | 17 | #include <linux/mm.h> |
Kees Cook | b53f27e | 2016-04-20 15:46:23 -0700 | [diff] [blame] | 18 | #include <linux/slab.h> |
Andy Shevchenko | c825038 | 2014-10-13 15:55:16 -0700 | [diff] [blame] | 19 | #include <linux/string.h> |
James Bottomley | 3c9f368 | 2008-08-31 10:13:54 -0500 | [diff] [blame] | 20 | #include <linux/string_helpers.h> |
| 21 | |
| 22 | /** |
| 23 | * string_get_size - get the size in the specified units |
James Bottomley | b9f28d8 | 2015-03-05 18:47:01 -0800 | [diff] [blame] | 24 | * @size: The size to be converted in blocks |
| 25 | * @blk_size: Size of the block (use 1 for size in bytes) |
James Bottomley | 3c9f368 | 2008-08-31 10:13:54 -0500 | [diff] [blame] | 26 | * @units: units to use (powers of 1000 or 1024) |
| 27 | * @buf: buffer to format to |
| 28 | * @len: length of buffer |
| 29 | * |
| 30 | * This function returns a string formatted to 3 significant figures |
Rasmus Villemoes | d1214c6 | 2015-02-12 15:01:50 -0800 | [diff] [blame] | 31 | * giving the size in the required units. @buf should have room for |
| 32 | * at least 9 bytes and will always be zero terminated. |
James Bottomley | 3c9f368 | 2008-08-31 10:13:54 -0500 | [diff] [blame] | 33 | * |
| 34 | */ |
James Bottomley | b9f28d8 | 2015-03-05 18:47:01 -0800 | [diff] [blame] | 35 | void string_get_size(u64 size, u64 blk_size, const enum string_size_units units, |
Rasmus Villemoes | d1214c6 | 2015-02-12 15:01:50 -0800 | [diff] [blame] | 36 | char *buf, int len) |
James Bottomley | 3c9f368 | 2008-08-31 10:13:54 -0500 | [diff] [blame] | 37 | { |
Mathias Krause | 142cda5 | 2014-08-06 16:09:31 -0700 | [diff] [blame] | 38 | static const char *const units_10[] = { |
James Bottomley | b9f28d8 | 2015-03-05 18:47:01 -0800 | [diff] [blame] | 39 | "B", "kB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB" |
Mathias Krause | 142cda5 | 2014-08-06 16:09:31 -0700 | [diff] [blame] | 40 | }; |
| 41 | static const char *const units_2[] = { |
James Bottomley | b9f28d8 | 2015-03-05 18:47:01 -0800 | [diff] [blame] | 42 | "B", "KiB", "MiB", "GiB", "TiB", "PiB", "EiB", "ZiB", "YiB" |
Mathias Krause | 142cda5 | 2014-08-06 16:09:31 -0700 | [diff] [blame] | 43 | }; |
| 44 | static const char *const *const units_str[] = { |
| 45 | [STRING_UNITS_10] = units_10, |
James Bottomley | 3c9f368 | 2008-08-31 10:13:54 -0500 | [diff] [blame] | 46 | [STRING_UNITS_2] = units_2, |
| 47 | }; |
Andrew Morton | 68aecfb | 2012-05-29 15:07:32 -0700 | [diff] [blame] | 48 | static const unsigned int divisor[] = { |
James Bottomley | 3c9f368 | 2008-08-31 10:13:54 -0500 | [diff] [blame] | 49 | [STRING_UNITS_10] = 1000, |
| 50 | [STRING_UNITS_2] = 1024, |
| 51 | }; |
James Bottomley | 564b026 | 2016-01-20 14:58:29 -0800 | [diff] [blame] | 52 | static const unsigned int rounding[] = { 500, 50, 5 }; |
| 53 | int i = 0, j; |
| 54 | u32 remainder = 0, sf_cap; |
James Bottomley | 3c9f368 | 2008-08-31 10:13:54 -0500 | [diff] [blame] | 55 | char tmp[8]; |
James Bottomley | b9f28d8 | 2015-03-05 18:47:01 -0800 | [diff] [blame] | 56 | const char *unit; |
James Bottomley | 3c9f368 | 2008-08-31 10:13:54 -0500 | [diff] [blame] | 57 | |
| 58 | tmp[0] = '\0'; |
James Bottomley | 564b026 | 2016-01-20 14:58:29 -0800 | [diff] [blame] | 59 | |
| 60 | if (blk_size == 0) |
| 61 | size = 0; |
| 62 | if (size == 0) |
James Bottomley | b9f28d8 | 2015-03-05 18:47:01 -0800 | [diff] [blame] | 63 | goto out; |
James Bottomley | 3c9f368 | 2008-08-31 10:13:54 -0500 | [diff] [blame] | 64 | |
James Bottomley | 564b026 | 2016-01-20 14:58:29 -0800 | [diff] [blame] | 65 | /* This is Napier's algorithm. Reduce the original block size to |
| 66 | * |
| 67 | * coefficient * divisor[units]^i |
| 68 | * |
| 69 | * we do the reduction so both coefficients are just under 32 bits so |
| 70 | * that multiplying them together won't overflow 64 bits and we keep |
| 71 | * as much precision as possible in the numbers. |
| 72 | * |
| 73 | * Note: it's safe to throw away the remainders here because all the |
| 74 | * precision is in the coefficients. |
Vitaly Kuznetsov | 62bef58 | 2015-09-17 16:01:51 -0700 | [diff] [blame] | 75 | */ |
James Bottomley | 564b026 | 2016-01-20 14:58:29 -0800 | [diff] [blame] | 76 | while (blk_size >> 32) { |
| 77 | do_div(blk_size, divisor[units]); |
James Bottomley | b9f28d8 | 2015-03-05 18:47:01 -0800 | [diff] [blame] | 78 | i++; |
James Bottomley | b9f28d8 | 2015-03-05 18:47:01 -0800 | [diff] [blame] | 79 | } |
| 80 | |
James Bottomley | 564b026 | 2016-01-20 14:58:29 -0800 | [diff] [blame] | 81 | while (size >> 32) { |
| 82 | do_div(size, divisor[units]); |
| 83 | i++; |
| 84 | } |
James Bottomley | b9f28d8 | 2015-03-05 18:47:01 -0800 | [diff] [blame] | 85 | |
James Bottomley | 564b026 | 2016-01-20 14:58:29 -0800 | [diff] [blame] | 86 | /* now perform the actual multiplication keeping i as the sum of the |
| 87 | * two logarithms */ |
| 88 | size *= blk_size; |
| 89 | |
| 90 | /* and logarithmically reduce it until it's just under the divisor */ |
James Bottomley | b9f28d8 | 2015-03-05 18:47:01 -0800 | [diff] [blame] | 91 | while (size >= divisor[units]) { |
| 92 | remainder = do_div(size, divisor[units]); |
| 93 | i++; |
| 94 | } |
| 95 | |
James Bottomley | 564b026 | 2016-01-20 14:58:29 -0800 | [diff] [blame] | 96 | /* work out in j how many digits of precision we need from the |
| 97 | * remainder */ |
James Bottomley | b9f28d8 | 2015-03-05 18:47:01 -0800 | [diff] [blame] | 98 | sf_cap = size; |
| 99 | for (j = 0; sf_cap*10 < 1000; j++) |
| 100 | sf_cap *= 10; |
| 101 | |
James Bottomley | 564b026 | 2016-01-20 14:58:29 -0800 | [diff] [blame] | 102 | if (units == STRING_UNITS_2) { |
| 103 | /* express the remainder as a decimal. It's currently the |
| 104 | * numerator of a fraction whose denominator is |
| 105 | * divisor[units], which is 1 << 10 for STRING_UNITS_2 */ |
James Bottomley | b9f28d8 | 2015-03-05 18:47:01 -0800 | [diff] [blame] | 106 | remainder *= 1000; |
James Bottomley | 564b026 | 2016-01-20 14:58:29 -0800 | [diff] [blame] | 107 | remainder >>= 10; |
| 108 | } |
| 109 | |
| 110 | /* add a 5 to the digit below what will be printed to ensure |
| 111 | * an arithmetical round up and carry it through to size */ |
| 112 | remainder += rounding[j]; |
| 113 | if (remainder >= 1000) { |
| 114 | remainder -= 1000; |
| 115 | size += 1; |
| 116 | } |
| 117 | |
| 118 | if (j) { |
James Bottomley | b9f28d8 | 2015-03-05 18:47:01 -0800 | [diff] [blame] | 119 | snprintf(tmp, sizeof(tmp), ".%03u", remainder); |
| 120 | tmp[j+1] = '\0'; |
| 121 | } |
| 122 | |
| 123 | out: |
| 124 | if (i >= ARRAY_SIZE(units_2)) |
| 125 | unit = "UNK"; |
| 126 | else |
| 127 | unit = units_str[units][i]; |
| 128 | |
Rasmus Villemoes | 84b9fbe | 2015-02-12 15:01:48 -0800 | [diff] [blame] | 129 | snprintf(buf, len, "%u%s %s", (u32)size, |
James Bottomley | b9f28d8 | 2015-03-05 18:47:01 -0800 | [diff] [blame] | 130 | tmp, unit); |
James Bottomley | 3c9f368 | 2008-08-31 10:13:54 -0500 | [diff] [blame] | 131 | } |
| 132 | EXPORT_SYMBOL(string_get_size); |
Andy Shevchenko | 16c7fa0 | 2013-04-30 15:27:30 -0700 | [diff] [blame] | 133 | |
| 134 | static bool unescape_space(char **src, char **dst) |
| 135 | { |
| 136 | char *p = *dst, *q = *src; |
| 137 | |
| 138 | switch (*q) { |
| 139 | case 'n': |
| 140 | *p = '\n'; |
| 141 | break; |
| 142 | case 'r': |
| 143 | *p = '\r'; |
| 144 | break; |
| 145 | case 't': |
| 146 | *p = '\t'; |
| 147 | break; |
| 148 | case 'v': |
| 149 | *p = '\v'; |
| 150 | break; |
| 151 | case 'f': |
| 152 | *p = '\f'; |
| 153 | break; |
| 154 | default: |
| 155 | return false; |
| 156 | } |
| 157 | *dst += 1; |
| 158 | *src += 1; |
| 159 | return true; |
| 160 | } |
| 161 | |
| 162 | static bool unescape_octal(char **src, char **dst) |
| 163 | { |
| 164 | char *p = *dst, *q = *src; |
| 165 | u8 num; |
| 166 | |
| 167 | if (isodigit(*q) == 0) |
| 168 | return false; |
| 169 | |
| 170 | num = (*q++) & 7; |
| 171 | while (num < 32 && isodigit(*q) && (q - *src < 3)) { |
| 172 | num <<= 3; |
| 173 | num += (*q++) & 7; |
| 174 | } |
| 175 | *p = num; |
| 176 | *dst += 1; |
| 177 | *src = q; |
| 178 | return true; |
| 179 | } |
| 180 | |
| 181 | static bool unescape_hex(char **src, char **dst) |
| 182 | { |
| 183 | char *p = *dst, *q = *src; |
| 184 | int digit; |
| 185 | u8 num; |
| 186 | |
| 187 | if (*q++ != 'x') |
| 188 | return false; |
| 189 | |
| 190 | num = digit = hex_to_bin(*q++); |
| 191 | if (digit < 0) |
| 192 | return false; |
| 193 | |
| 194 | digit = hex_to_bin(*q); |
| 195 | if (digit >= 0) { |
| 196 | q++; |
| 197 | num = (num << 4) | digit; |
| 198 | } |
| 199 | *p = num; |
| 200 | *dst += 1; |
| 201 | *src = q; |
| 202 | return true; |
| 203 | } |
| 204 | |
| 205 | static bool unescape_special(char **src, char **dst) |
| 206 | { |
| 207 | char *p = *dst, *q = *src; |
| 208 | |
| 209 | switch (*q) { |
| 210 | case '\"': |
| 211 | *p = '\"'; |
| 212 | break; |
| 213 | case '\\': |
| 214 | *p = '\\'; |
| 215 | break; |
| 216 | case 'a': |
| 217 | *p = '\a'; |
| 218 | break; |
| 219 | case 'e': |
| 220 | *p = '\e'; |
| 221 | break; |
| 222 | default: |
| 223 | return false; |
| 224 | } |
| 225 | *dst += 1; |
| 226 | *src += 1; |
| 227 | return true; |
| 228 | } |
| 229 | |
Andy Shevchenko | d295634 | 2014-10-13 15:55:11 -0700 | [diff] [blame] | 230 | /** |
| 231 | * string_unescape - unquote characters in the given string |
| 232 | * @src: source buffer (escaped) |
| 233 | * @dst: destination buffer (unescaped) |
| 234 | * @size: size of the destination buffer (0 to unlimit) |
Jonathan Corbet | b4658cd | 2019-07-16 16:27:36 -0700 | [diff] [blame] | 235 | * @flags: combination of the flags. |
Andy Shevchenko | d295634 | 2014-10-13 15:55:11 -0700 | [diff] [blame] | 236 | * |
| 237 | * Description: |
| 238 | * The function unquotes characters in the given string. |
| 239 | * |
| 240 | * Because the size of the output will be the same as or less than the size of |
| 241 | * the input, the transformation may be performed in place. |
| 242 | * |
| 243 | * Caller must provide valid source and destination pointers. Be aware that |
| 244 | * destination buffer will always be NULL-terminated. Source string must be |
Jonathan Corbet | b4658cd | 2019-07-16 16:27:36 -0700 | [diff] [blame] | 245 | * NULL-terminated as well. The supported flags are:: |
| 246 | * |
| 247 | * UNESCAPE_SPACE: |
| 248 | * '\f' - form feed |
| 249 | * '\n' - new line |
| 250 | * '\r' - carriage return |
| 251 | * '\t' - horizontal tab |
| 252 | * '\v' - vertical tab |
| 253 | * UNESCAPE_OCTAL: |
| 254 | * '\NNN' - byte with octal value NNN (1 to 3 digits) |
| 255 | * UNESCAPE_HEX: |
| 256 | * '\xHH' - byte with hexadecimal value HH (1 to 2 digits) |
| 257 | * UNESCAPE_SPECIAL: |
| 258 | * '\"' - double quote |
| 259 | * '\\' - backslash |
| 260 | * '\a' - alert (BEL) |
| 261 | * '\e' - escape |
| 262 | * UNESCAPE_ANY: |
| 263 | * all previous together |
Andy Shevchenko | d295634 | 2014-10-13 15:55:11 -0700 | [diff] [blame] | 264 | * |
| 265 | * Return: |
| 266 | * The amount of the characters processed to the destination buffer excluding |
| 267 | * trailing '\0' is returned. |
| 268 | */ |
Andy Shevchenko | 16c7fa0 | 2013-04-30 15:27:30 -0700 | [diff] [blame] | 269 | int string_unescape(char *src, char *dst, size_t size, unsigned int flags) |
| 270 | { |
| 271 | char *out = dst; |
| 272 | |
| 273 | while (*src && --size) { |
| 274 | if (src[0] == '\\' && src[1] != '\0' && size > 1) { |
| 275 | src++; |
| 276 | size--; |
| 277 | |
| 278 | if (flags & UNESCAPE_SPACE && |
| 279 | unescape_space(&src, &out)) |
| 280 | continue; |
| 281 | |
| 282 | if (flags & UNESCAPE_OCTAL && |
| 283 | unescape_octal(&src, &out)) |
| 284 | continue; |
| 285 | |
| 286 | if (flags & UNESCAPE_HEX && |
| 287 | unescape_hex(&src, &out)) |
| 288 | continue; |
| 289 | |
| 290 | if (flags & UNESCAPE_SPECIAL && |
| 291 | unescape_special(&src, &out)) |
| 292 | continue; |
| 293 | |
| 294 | *out++ = '\\'; |
| 295 | } |
| 296 | *out++ = *src++; |
| 297 | } |
| 298 | *out = '\0'; |
| 299 | |
| 300 | return out - dst; |
| 301 | } |
| 302 | EXPORT_SYMBOL(string_unescape); |
Andy Shevchenko | c825038 | 2014-10-13 15:55:16 -0700 | [diff] [blame] | 303 | |
Rasmus Villemoes | 3aeddc7 | 2015-04-15 16:17:25 -0700 | [diff] [blame] | 304 | static bool escape_passthrough(unsigned char c, char **dst, char *end) |
Andy Shevchenko | c825038 | 2014-10-13 15:55:16 -0700 | [diff] [blame] | 305 | { |
| 306 | char *out = *dst; |
| 307 | |
Rasmus Villemoes | 3aeddc7 | 2015-04-15 16:17:25 -0700 | [diff] [blame] | 308 | if (out < end) |
| 309 | *out = c; |
| 310 | *dst = out + 1; |
| 311 | return true; |
Andy Shevchenko | c825038 | 2014-10-13 15:55:16 -0700 | [diff] [blame] | 312 | } |
| 313 | |
Rasmus Villemoes | 3aeddc7 | 2015-04-15 16:17:25 -0700 | [diff] [blame] | 314 | static bool escape_space(unsigned char c, char **dst, char *end) |
Andy Shevchenko | c825038 | 2014-10-13 15:55:16 -0700 | [diff] [blame] | 315 | { |
| 316 | char *out = *dst; |
| 317 | unsigned char to; |
| 318 | |
Andy Shevchenko | c825038 | 2014-10-13 15:55:16 -0700 | [diff] [blame] | 319 | switch (c) { |
| 320 | case '\n': |
| 321 | to = 'n'; |
| 322 | break; |
| 323 | case '\r': |
| 324 | to = 'r'; |
| 325 | break; |
| 326 | case '\t': |
| 327 | to = 't'; |
| 328 | break; |
| 329 | case '\v': |
| 330 | to = 'v'; |
| 331 | break; |
| 332 | case '\f': |
| 333 | to = 'f'; |
| 334 | break; |
| 335 | default: |
Rasmus Villemoes | 3aeddc7 | 2015-04-15 16:17:25 -0700 | [diff] [blame] | 336 | return false; |
Andy Shevchenko | c825038 | 2014-10-13 15:55:16 -0700 | [diff] [blame] | 337 | } |
| 338 | |
Rasmus Villemoes | 3aeddc7 | 2015-04-15 16:17:25 -0700 | [diff] [blame] | 339 | if (out < end) |
| 340 | *out = '\\'; |
| 341 | ++out; |
| 342 | if (out < end) |
| 343 | *out = to; |
| 344 | ++out; |
Andy Shevchenko | c825038 | 2014-10-13 15:55:16 -0700 | [diff] [blame] | 345 | |
| 346 | *dst = out; |
Rasmus Villemoes | 3aeddc7 | 2015-04-15 16:17:25 -0700 | [diff] [blame] | 347 | return true; |
Andy Shevchenko | c825038 | 2014-10-13 15:55:16 -0700 | [diff] [blame] | 348 | } |
| 349 | |
Rasmus Villemoes | 3aeddc7 | 2015-04-15 16:17:25 -0700 | [diff] [blame] | 350 | static bool escape_special(unsigned char c, char **dst, char *end) |
Andy Shevchenko | c825038 | 2014-10-13 15:55:16 -0700 | [diff] [blame] | 351 | { |
| 352 | char *out = *dst; |
| 353 | unsigned char to; |
| 354 | |
Andy Shevchenko | c825038 | 2014-10-13 15:55:16 -0700 | [diff] [blame] | 355 | switch (c) { |
| 356 | case '\\': |
| 357 | to = '\\'; |
| 358 | break; |
| 359 | case '\a': |
| 360 | to = 'a'; |
| 361 | break; |
| 362 | case '\e': |
| 363 | to = 'e'; |
| 364 | break; |
Chris Down | 91027d0 | 2021-06-15 17:52:45 +0100 | [diff] [blame] | 365 | case '"': |
| 366 | to = '"'; |
| 367 | break; |
Andy Shevchenko | c825038 | 2014-10-13 15:55:16 -0700 | [diff] [blame] | 368 | default: |
Rasmus Villemoes | 3aeddc7 | 2015-04-15 16:17:25 -0700 | [diff] [blame] | 369 | return false; |
Andy Shevchenko | c825038 | 2014-10-13 15:55:16 -0700 | [diff] [blame] | 370 | } |
| 371 | |
Rasmus Villemoes | 3aeddc7 | 2015-04-15 16:17:25 -0700 | [diff] [blame] | 372 | if (out < end) |
| 373 | *out = '\\'; |
| 374 | ++out; |
| 375 | if (out < end) |
| 376 | *out = to; |
| 377 | ++out; |
Andy Shevchenko | c825038 | 2014-10-13 15:55:16 -0700 | [diff] [blame] | 378 | |
| 379 | *dst = out; |
Rasmus Villemoes | 3aeddc7 | 2015-04-15 16:17:25 -0700 | [diff] [blame] | 380 | return true; |
Andy Shevchenko | c825038 | 2014-10-13 15:55:16 -0700 | [diff] [blame] | 381 | } |
| 382 | |
Rasmus Villemoes | 3aeddc7 | 2015-04-15 16:17:25 -0700 | [diff] [blame] | 383 | static bool escape_null(unsigned char c, char **dst, char *end) |
Andy Shevchenko | c825038 | 2014-10-13 15:55:16 -0700 | [diff] [blame] | 384 | { |
| 385 | char *out = *dst; |
| 386 | |
Andy Shevchenko | c825038 | 2014-10-13 15:55:16 -0700 | [diff] [blame] | 387 | if (c) |
Rasmus Villemoes | 3aeddc7 | 2015-04-15 16:17:25 -0700 | [diff] [blame] | 388 | return false; |
Andy Shevchenko | c825038 | 2014-10-13 15:55:16 -0700 | [diff] [blame] | 389 | |
Rasmus Villemoes | 3aeddc7 | 2015-04-15 16:17:25 -0700 | [diff] [blame] | 390 | if (out < end) |
| 391 | *out = '\\'; |
| 392 | ++out; |
| 393 | if (out < end) |
| 394 | *out = '0'; |
| 395 | ++out; |
Andy Shevchenko | c825038 | 2014-10-13 15:55:16 -0700 | [diff] [blame] | 396 | |
| 397 | *dst = out; |
Rasmus Villemoes | 3aeddc7 | 2015-04-15 16:17:25 -0700 | [diff] [blame] | 398 | return true; |
Andy Shevchenko | c825038 | 2014-10-13 15:55:16 -0700 | [diff] [blame] | 399 | } |
| 400 | |
Rasmus Villemoes | 3aeddc7 | 2015-04-15 16:17:25 -0700 | [diff] [blame] | 401 | static bool escape_octal(unsigned char c, char **dst, char *end) |
Andy Shevchenko | c825038 | 2014-10-13 15:55:16 -0700 | [diff] [blame] | 402 | { |
| 403 | char *out = *dst; |
| 404 | |
Rasmus Villemoes | 3aeddc7 | 2015-04-15 16:17:25 -0700 | [diff] [blame] | 405 | if (out < end) |
| 406 | *out = '\\'; |
| 407 | ++out; |
| 408 | if (out < end) |
| 409 | *out = ((c >> 6) & 0x07) + '0'; |
| 410 | ++out; |
| 411 | if (out < end) |
| 412 | *out = ((c >> 3) & 0x07) + '0'; |
| 413 | ++out; |
| 414 | if (out < end) |
| 415 | *out = ((c >> 0) & 0x07) + '0'; |
| 416 | ++out; |
Andy Shevchenko | c825038 | 2014-10-13 15:55:16 -0700 | [diff] [blame] | 417 | |
| 418 | *dst = out; |
Rasmus Villemoes | 3aeddc7 | 2015-04-15 16:17:25 -0700 | [diff] [blame] | 419 | return true; |
Andy Shevchenko | c825038 | 2014-10-13 15:55:16 -0700 | [diff] [blame] | 420 | } |
| 421 | |
Rasmus Villemoes | 3aeddc7 | 2015-04-15 16:17:25 -0700 | [diff] [blame] | 422 | static bool escape_hex(unsigned char c, char **dst, char *end) |
Andy Shevchenko | c825038 | 2014-10-13 15:55:16 -0700 | [diff] [blame] | 423 | { |
| 424 | char *out = *dst; |
| 425 | |
Rasmus Villemoes | 3aeddc7 | 2015-04-15 16:17:25 -0700 | [diff] [blame] | 426 | if (out < end) |
| 427 | *out = '\\'; |
| 428 | ++out; |
| 429 | if (out < end) |
| 430 | *out = 'x'; |
| 431 | ++out; |
| 432 | if (out < end) |
| 433 | *out = hex_asc_hi(c); |
| 434 | ++out; |
| 435 | if (out < end) |
| 436 | *out = hex_asc_lo(c); |
| 437 | ++out; |
Andy Shevchenko | c825038 | 2014-10-13 15:55:16 -0700 | [diff] [blame] | 438 | |
| 439 | *dst = out; |
Rasmus Villemoes | 3aeddc7 | 2015-04-15 16:17:25 -0700 | [diff] [blame] | 440 | return true; |
Andy Shevchenko | c825038 | 2014-10-13 15:55:16 -0700 | [diff] [blame] | 441 | } |
| 442 | |
| 443 | /** |
| 444 | * string_escape_mem - quote characters in the given memory buffer |
| 445 | * @src: source buffer (unescaped) |
| 446 | * @isz: source buffer size |
| 447 | * @dst: destination buffer (escaped) |
| 448 | * @osz: destination buffer size |
Jonathan Corbet | b4658cd | 2019-07-16 16:27:36 -0700 | [diff] [blame] | 449 | * @flags: combination of the flags |
| 450 | * @only: NULL-terminated string containing characters used to limit |
| 451 | * the selected escape class. If characters are included in @only |
| 452 | * that would not normally be escaped by the classes selected |
| 453 | * in @flags, they will be copied to @dst unescaped. |
| 454 | * |
| 455 | * Description: |
| 456 | * The process of escaping byte buffer includes several parts. They are applied |
| 457 | * in the following sequence. |
| 458 | * |
Andy Shevchenko | 62519b8 | 2021-06-30 18:55:08 -0700 | [diff] [blame] | 459 | * 1. The character is not matched to the one from @only string and thus |
Jonathan Corbet | b4658cd | 2019-07-16 16:27:36 -0700 | [diff] [blame] | 460 | * must go as-is to the output. |
Andy Shevchenko | 0362c27 | 2021-06-30 18:55:17 -0700 | [diff] [blame] | 461 | * 2. The character is matched to the printable and ASCII classes, if asked, |
Andy Shevchenko | a080978 | 2021-06-30 18:55:14 -0700 | [diff] [blame] | 462 | * and in case of match it passes through to the output. |
Andy Shevchenko | 0362c27 | 2021-06-30 18:55:17 -0700 | [diff] [blame] | 463 | * 3. The character is matched to the printable or ASCII class, if asked, |
| 464 | * and in case of match it passes through to the output. |
| 465 | * 4. The character is checked if it falls into the class given by @flags. |
Jonathan Corbet | b4658cd | 2019-07-16 16:27:36 -0700 | [diff] [blame] | 466 | * %ESCAPE_OCTAL and %ESCAPE_HEX are going last since they cover any |
| 467 | * character. Note that they actually can't go together, otherwise |
| 468 | * %ESCAPE_HEX will be ignored. |
| 469 | * |
| 470 | * Caller must provide valid source and destination pointers. Be aware that |
| 471 | * destination buffer will not be NULL-terminated, thus caller have to append |
Andy Shevchenko | a080978 | 2021-06-30 18:55:14 -0700 | [diff] [blame] | 472 | * it if needs. The supported flags are:: |
Jonathan Corbet | b4658cd | 2019-07-16 16:27:36 -0700 | [diff] [blame] | 473 | * |
Kees Cook | d89a3f7 | 2015-09-09 15:37:14 -0700 | [diff] [blame] | 474 | * %ESCAPE_SPACE: (special white space, not space itself) |
Andy Shevchenko | c825038 | 2014-10-13 15:55:16 -0700 | [diff] [blame] | 475 | * '\f' - form feed |
| 476 | * '\n' - new line |
| 477 | * '\r' - carriage return |
| 478 | * '\t' - horizontal tab |
| 479 | * '\v' - vertical tab |
| 480 | * %ESCAPE_SPECIAL: |
Chris Down | 91027d0 | 2021-06-15 17:52:45 +0100 | [diff] [blame] | 481 | * '\"' - double quote |
Andy Shevchenko | c825038 | 2014-10-13 15:55:16 -0700 | [diff] [blame] | 482 | * '\\' - backslash |
| 483 | * '\a' - alert (BEL) |
| 484 | * '\e' - escape |
| 485 | * %ESCAPE_NULL: |
| 486 | * '\0' - null |
| 487 | * %ESCAPE_OCTAL: |
| 488 | * '\NNN' - byte with octal value NNN (3 digits) |
| 489 | * %ESCAPE_ANY: |
| 490 | * all previous together |
| 491 | * %ESCAPE_NP: |
Andy Shevchenko | a080978 | 2021-06-30 18:55:14 -0700 | [diff] [blame] | 492 | * escape only non-printable characters, checked by isprint() |
Andy Shevchenko | c825038 | 2014-10-13 15:55:16 -0700 | [diff] [blame] | 493 | * %ESCAPE_ANY_NP: |
| 494 | * all previous together |
| 495 | * %ESCAPE_HEX: |
| 496 | * '\xHH' - byte with hexadecimal value HH (2 digits) |
Andy Shevchenko | a080978 | 2021-06-30 18:55:14 -0700 | [diff] [blame] | 497 | * %ESCAPE_NA: |
| 498 | * escape only non-ascii characters, checked by isascii() |
Andy Shevchenko | 0362c27 | 2021-06-30 18:55:17 -0700 | [diff] [blame] | 499 | * %ESCAPE_NAP: |
| 500 | * escape only non-printable or non-ascii characters |
Andy Shevchenko | aec0d09 | 2021-06-30 18:55:20 -0700 | [diff] [blame] | 501 | * %ESCAPE_APPEND: |
| 502 | * append characters from @only to be escaped by the given classes |
| 503 | * |
| 504 | * %ESCAPE_APPEND would help to pass additional characters to the escaped, when |
| 505 | * one of %ESCAPE_NP, %ESCAPE_NA, or %ESCAPE_NAP is provided. |
Andy Shevchenko | a080978 | 2021-06-30 18:55:14 -0700 | [diff] [blame] | 506 | * |
Andy Shevchenko | 0362c27 | 2021-06-30 18:55:17 -0700 | [diff] [blame] | 507 | * One notable caveat, the %ESCAPE_NAP, %ESCAPE_NP and %ESCAPE_NA have the |
| 508 | * higher priority than the rest of the flags (%ESCAPE_NAP is the highest). |
Andy Shevchenko | a080978 | 2021-06-30 18:55:14 -0700 | [diff] [blame] | 509 | * It doesn't make much sense to use either of them without %ESCAPE_OCTAL |
| 510 | * or %ESCAPE_HEX, because they cover most of the other character classes. |
Andy Shevchenko | 0362c27 | 2021-06-30 18:55:17 -0700 | [diff] [blame] | 511 | * %ESCAPE_NAP can utilize %ESCAPE_SPACE or %ESCAPE_SPECIAL in addition to |
| 512 | * the above. |
Andy Shevchenko | c825038 | 2014-10-13 15:55:16 -0700 | [diff] [blame] | 513 | * |
| 514 | * Return: |
Rasmus Villemoes | 41416f2 | 2015-04-15 16:17:28 -0700 | [diff] [blame] | 515 | * The total size of the escaped output that would be generated for |
| 516 | * the given input and flags. To check whether the output was |
| 517 | * truncated, compare the return value to osz. There is room left in |
| 518 | * dst for a '\0' terminator if and only if ret < osz. |
Andy Shevchenko | c825038 | 2014-10-13 15:55:16 -0700 | [diff] [blame] | 519 | */ |
Rasmus Villemoes | 41416f2 | 2015-04-15 16:17:28 -0700 | [diff] [blame] | 520 | int string_escape_mem(const char *src, size_t isz, char *dst, size_t osz, |
Kees Cook | b40bdb7 | 2015-09-09 15:37:16 -0700 | [diff] [blame] | 521 | unsigned int flags, const char *only) |
Andy Shevchenko | c825038 | 2014-10-13 15:55:16 -0700 | [diff] [blame] | 522 | { |
Rasmus Villemoes | 41416f2 | 2015-04-15 16:17:28 -0700 | [diff] [blame] | 523 | char *p = dst; |
Rasmus Villemoes | 3aeddc7 | 2015-04-15 16:17:25 -0700 | [diff] [blame] | 524 | char *end = p + osz; |
Kees Cook | b40bdb7 | 2015-09-09 15:37:16 -0700 | [diff] [blame] | 525 | bool is_dict = only && *only; |
Andy Shevchenko | aec0d09 | 2021-06-30 18:55:20 -0700 | [diff] [blame] | 526 | bool is_append = flags & ESCAPE_APPEND; |
Andy Shevchenko | c825038 | 2014-10-13 15:55:16 -0700 | [diff] [blame] | 527 | |
| 528 | while (isz--) { |
| 529 | unsigned char c = *src++; |
Andy Shevchenko | aec0d09 | 2021-06-30 18:55:20 -0700 | [diff] [blame] | 530 | bool in_dict = is_dict && strchr(only, c); |
Andy Shevchenko | c825038 | 2014-10-13 15:55:16 -0700 | [diff] [blame] | 531 | |
| 532 | /* |
| 533 | * Apply rules in the following sequence: |
Kees Cook | b40bdb7 | 2015-09-09 15:37:16 -0700 | [diff] [blame] | 534 | * - the @only string is supplied and does not contain a |
Andy Shevchenko | c825038 | 2014-10-13 15:55:16 -0700 | [diff] [blame] | 535 | * character under question |
Andy Shevchenko | 0362c27 | 2021-06-30 18:55:17 -0700 | [diff] [blame] | 536 | * - the character is printable and ASCII, when @flags has |
| 537 | * %ESCAPE_NAP bit set |
Andy Shevchenko | 62519b8 | 2021-06-30 18:55:08 -0700 | [diff] [blame] | 538 | * - the character is printable, when @flags has |
| 539 | * %ESCAPE_NP bit set |
Andy Shevchenko | a080978 | 2021-06-30 18:55:14 -0700 | [diff] [blame] | 540 | * - the character is ASCII, when @flags has |
| 541 | * %ESCAPE_NA bit set |
Andy Shevchenko | c825038 | 2014-10-13 15:55:16 -0700 | [diff] [blame] | 542 | * - the character doesn't fall into a class of symbols |
| 543 | * defined by given @flags |
| 544 | * In these cases we just pass through a character to the |
| 545 | * output buffer. |
Andy Shevchenko | aec0d09 | 2021-06-30 18:55:20 -0700 | [diff] [blame] | 546 | * |
| 547 | * When %ESCAPE_APPEND is passed, the characters from @only |
| 548 | * have been excluded from the %ESCAPE_NAP, %ESCAPE_NP, and |
| 549 | * %ESCAPE_NA cases. |
Andy Shevchenko | c825038 | 2014-10-13 15:55:16 -0700 | [diff] [blame] | 550 | */ |
Andy Shevchenko | aec0d09 | 2021-06-30 18:55:20 -0700 | [diff] [blame] | 551 | if (!(is_append || in_dict) && is_dict && |
Andy Shevchenko | 7e5969a | 2021-06-30 18:55:11 -0700 | [diff] [blame] | 552 | escape_passthrough(c, &p, end)) |
| 553 | continue; |
Andy Shevchenko | 62519b8 | 2021-06-30 18:55:08 -0700 | [diff] [blame] | 554 | |
Andy Shevchenko | aec0d09 | 2021-06-30 18:55:20 -0700 | [diff] [blame] | 555 | if (!(is_append && in_dict) && isascii(c) && isprint(c) && |
Andy Shevchenko | 0362c27 | 2021-06-30 18:55:17 -0700 | [diff] [blame] | 556 | flags & ESCAPE_NAP && escape_passthrough(c, &p, end)) |
| 557 | continue; |
| 558 | |
Andy Shevchenko | aec0d09 | 2021-06-30 18:55:20 -0700 | [diff] [blame] | 559 | if (!(is_append && in_dict) && isprint(c) && |
Andy Shevchenko | 7e5969a | 2021-06-30 18:55:11 -0700 | [diff] [blame] | 560 | flags & ESCAPE_NP && escape_passthrough(c, &p, end)) |
| 561 | continue; |
Andy Shevchenko | c825038 | 2014-10-13 15:55:16 -0700 | [diff] [blame] | 562 | |
Andy Shevchenko | aec0d09 | 2021-06-30 18:55:20 -0700 | [diff] [blame] | 563 | if (!(is_append && in_dict) && isascii(c) && |
Andy Shevchenko | a080978 | 2021-06-30 18:55:14 -0700 | [diff] [blame] | 564 | flags & ESCAPE_NA && escape_passthrough(c, &p, end)) |
| 565 | continue; |
| 566 | |
Andy Shevchenko | 7e5969a | 2021-06-30 18:55:11 -0700 | [diff] [blame] | 567 | if (flags & ESCAPE_SPACE && escape_space(c, &p, end)) |
| 568 | continue; |
Andy Shevchenko | c825038 | 2014-10-13 15:55:16 -0700 | [diff] [blame] | 569 | |
Andy Shevchenko | 7e5969a | 2021-06-30 18:55:11 -0700 | [diff] [blame] | 570 | if (flags & ESCAPE_SPECIAL && escape_special(c, &p, end)) |
| 571 | continue; |
Andy Shevchenko | c825038 | 2014-10-13 15:55:16 -0700 | [diff] [blame] | 572 | |
Andy Shevchenko | 7e5969a | 2021-06-30 18:55:11 -0700 | [diff] [blame] | 573 | if (flags & ESCAPE_NULL && escape_null(c, &p, end)) |
| 574 | continue; |
Rasmus Villemoes | 3aeddc7 | 2015-04-15 16:17:25 -0700 | [diff] [blame] | 575 | |
Andy Shevchenko | 7e5969a | 2021-06-30 18:55:11 -0700 | [diff] [blame] | 576 | /* ESCAPE_OCTAL and ESCAPE_HEX always go last */ |
| 577 | if (flags & ESCAPE_OCTAL && escape_octal(c, &p, end)) |
| 578 | continue; |
| 579 | |
| 580 | if (flags & ESCAPE_HEX && escape_hex(c, &p, end)) |
| 581 | continue; |
Andy Shevchenko | c825038 | 2014-10-13 15:55:16 -0700 | [diff] [blame] | 582 | |
Rasmus Villemoes | 3aeddc7 | 2015-04-15 16:17:25 -0700 | [diff] [blame] | 583 | escape_passthrough(c, &p, end); |
Andy Shevchenko | c825038 | 2014-10-13 15:55:16 -0700 | [diff] [blame] | 584 | } |
| 585 | |
Rasmus Villemoes | 41416f2 | 2015-04-15 16:17:28 -0700 | [diff] [blame] | 586 | return p - dst; |
Andy Shevchenko | c825038 | 2014-10-13 15:55:16 -0700 | [diff] [blame] | 587 | } |
| 588 | EXPORT_SYMBOL(string_escape_mem); |
Kees Cook | b53f27e | 2016-04-20 15:46:23 -0700 | [diff] [blame] | 589 | |
| 590 | /* |
| 591 | * Return an allocated string that has been escaped of special characters |
| 592 | * and double quotes, making it safe to log in quotes. |
| 593 | */ |
| 594 | char *kstrdup_quotable(const char *src, gfp_t gfp) |
| 595 | { |
| 596 | size_t slen, dlen; |
| 597 | char *dst; |
| 598 | const int flags = ESCAPE_HEX; |
| 599 | const char esc[] = "\f\n\r\t\v\a\e\\\""; |
| 600 | |
| 601 | if (!src) |
| 602 | return NULL; |
| 603 | slen = strlen(src); |
| 604 | |
| 605 | dlen = string_escape_mem(src, slen, NULL, 0, flags, esc); |
| 606 | dst = kmalloc(dlen + 1, gfp); |
| 607 | if (!dst) |
| 608 | return NULL; |
| 609 | |
| 610 | WARN_ON(string_escape_mem(src, slen, dst, dlen, flags, esc) != dlen); |
| 611 | dst[dlen] = '\0'; |
| 612 | |
| 613 | return dst; |
| 614 | } |
| 615 | EXPORT_SYMBOL_GPL(kstrdup_quotable); |
Kees Cook | 0d04432 | 2016-04-20 15:46:24 -0700 | [diff] [blame] | 616 | |
| 617 | /* |
| 618 | * Returns allocated NULL-terminated string containing process |
| 619 | * command line, with inter-argument NULLs replaced with spaces, |
| 620 | * and other special characters escaped. |
| 621 | */ |
| 622 | char *kstrdup_quotable_cmdline(struct task_struct *task, gfp_t gfp) |
| 623 | { |
| 624 | char *buffer, *quoted; |
| 625 | int i, res; |
| 626 | |
Michal Hocko | 0ee931c | 2017-09-13 16:28:29 -0700 | [diff] [blame] | 627 | buffer = kmalloc(PAGE_SIZE, GFP_KERNEL); |
Kees Cook | 0d04432 | 2016-04-20 15:46:24 -0700 | [diff] [blame] | 628 | if (!buffer) |
| 629 | return NULL; |
| 630 | |
| 631 | res = get_cmdline(task, buffer, PAGE_SIZE - 1); |
| 632 | buffer[res] = '\0'; |
| 633 | |
| 634 | /* Collapse trailing NULLs, leave res pointing to last non-NULL. */ |
| 635 | while (--res >= 0 && buffer[res] == '\0') |
| 636 | ; |
| 637 | |
| 638 | /* Replace inter-argument NULLs. */ |
| 639 | for (i = 0; i <= res; i++) |
| 640 | if (buffer[i] == '\0') |
| 641 | buffer[i] = ' '; |
| 642 | |
| 643 | /* Make sure result is printable. */ |
| 644 | quoted = kstrdup_quotable(buffer, gfp); |
| 645 | kfree(buffer); |
| 646 | return quoted; |
| 647 | } |
| 648 | EXPORT_SYMBOL_GPL(kstrdup_quotable_cmdline); |
Kees Cook | 2198531 | 2016-04-20 15:46:25 -0700 | [diff] [blame] | 649 | |
| 650 | /* |
| 651 | * Returns allocated NULL-terminated string containing pathname, |
| 652 | * with special characters escaped, able to be safely logged. If |
| 653 | * there is an error, the leading character will be "<". |
| 654 | */ |
| 655 | char *kstrdup_quotable_file(struct file *file, gfp_t gfp) |
| 656 | { |
| 657 | char *temp, *pathname; |
| 658 | |
| 659 | if (!file) |
| 660 | return kstrdup("<unknown>", gfp); |
| 661 | |
| 662 | /* We add 11 spaces for ' (deleted)' to be appended */ |
Michal Hocko | 0ee931c | 2017-09-13 16:28:29 -0700 | [diff] [blame] | 663 | temp = kmalloc(PATH_MAX + 11, GFP_KERNEL); |
Kees Cook | 2198531 | 2016-04-20 15:46:25 -0700 | [diff] [blame] | 664 | if (!temp) |
| 665 | return kstrdup("<no_memory>", gfp); |
| 666 | |
| 667 | pathname = file_path(file, temp, PATH_MAX + 11); |
| 668 | if (IS_ERR(pathname)) |
| 669 | pathname = kstrdup("<too_long>", gfp); |
| 670 | else |
| 671 | pathname = kstrdup_quotable(pathname, gfp); |
| 672 | |
| 673 | kfree(temp); |
| 674 | return pathname; |
| 675 | } |
| 676 | EXPORT_SYMBOL_GPL(kstrdup_quotable_file); |
Bartosz Golaszewski | 0fd1601 | 2020-09-29 12:09:55 +0200 | [diff] [blame] | 677 | |
| 678 | /** |
Andy Shevchenko | 418e0a3 | 2021-11-05 14:42:24 +0200 | [diff] [blame] | 679 | * kasprintf_strarray - allocate and fill array of sequential strings |
| 680 | * @gfp: flags for the slab allocator |
| 681 | * @prefix: prefix to be used |
| 682 | * @n: amount of lines to be allocated and filled |
| 683 | * |
| 684 | * Allocates and fills @n strings using pattern "%s-%zu", where prefix |
| 685 | * is provided by caller. The caller is responsible to free them with |
| 686 | * kfree_strarray() after use. |
| 687 | * |
| 688 | * Returns array of strings or NULL when memory can't be allocated. |
| 689 | */ |
| 690 | char **kasprintf_strarray(gfp_t gfp, const char *prefix, size_t n) |
| 691 | { |
| 692 | char **names; |
| 693 | size_t i; |
| 694 | |
| 695 | names = kcalloc(n + 1, sizeof(char *), gfp); |
| 696 | if (!names) |
| 697 | return NULL; |
| 698 | |
| 699 | for (i = 0; i < n; i++) { |
| 700 | names[i] = kasprintf(gfp, "%s-%zu", prefix, i); |
| 701 | if (!names[i]) { |
| 702 | kfree_strarray(names, i); |
| 703 | return NULL; |
| 704 | } |
| 705 | } |
| 706 | |
| 707 | return names; |
| 708 | } |
| 709 | EXPORT_SYMBOL_GPL(kasprintf_strarray); |
| 710 | |
| 711 | /** |
Bartosz Golaszewski | 0fd1601 | 2020-09-29 12:09:55 +0200 | [diff] [blame] | 712 | * kfree_strarray - free a number of dynamically allocated strings contained |
| 713 | * in an array and the array itself |
| 714 | * |
| 715 | * @array: Dynamically allocated array of strings to free. |
| 716 | * @n: Number of strings (starting from the beginning of the array) to free. |
| 717 | * |
| 718 | * Passing a non-NULL @array and @n == 0 as well as NULL @array are valid |
| 719 | * use-cases. If @array is NULL, the function does nothing. |
| 720 | */ |
| 721 | void kfree_strarray(char **array, size_t n) |
| 722 | { |
| 723 | unsigned int i; |
| 724 | |
| 725 | if (!array) |
| 726 | return; |
| 727 | |
| 728 | for (i = 0; i < n; i++) |
| 729 | kfree(array[i]); |
| 730 | kfree(array); |
| 731 | } |
| 732 | EXPORT_SYMBOL_GPL(kfree_strarray); |
Kees Cook | cfecea6 | 2021-06-18 10:57:38 -0700 | [diff] [blame] | 733 | |
Andy Shevchenko | acdb89b | 2021-11-05 14:42:25 +0200 | [diff] [blame] | 734 | struct strarray { |
| 735 | char **array; |
| 736 | size_t n; |
| 737 | }; |
| 738 | |
| 739 | static void devm_kfree_strarray(struct device *dev, void *res) |
| 740 | { |
| 741 | struct strarray *array = res; |
| 742 | |
| 743 | kfree_strarray(array->array, array->n); |
| 744 | } |
| 745 | |
| 746 | char **devm_kasprintf_strarray(struct device *dev, const char *prefix, size_t n) |
| 747 | { |
| 748 | struct strarray *ptr; |
| 749 | |
| 750 | ptr = devres_alloc(devm_kfree_strarray, sizeof(*ptr), GFP_KERNEL); |
| 751 | if (!ptr) |
| 752 | return ERR_PTR(-ENOMEM); |
| 753 | |
| 754 | ptr->array = kasprintf_strarray(GFP_KERNEL, prefix, n); |
| 755 | if (!ptr->array) { |
| 756 | devres_free(ptr); |
| 757 | return ERR_PTR(-ENOMEM); |
| 758 | } |
| 759 | |
| 760 | return ptr->array; |
| 761 | } |
| 762 | EXPORT_SYMBOL_GPL(devm_kasprintf_strarray); |
| 763 | |
Kees Cook | cfecea6 | 2021-06-18 10:57:38 -0700 | [diff] [blame] | 764 | /** |
| 765 | * strscpy_pad() - Copy a C-string into a sized buffer |
| 766 | * @dest: Where to copy the string to |
| 767 | * @src: Where to copy the string from |
| 768 | * @count: Size of destination buffer |
| 769 | * |
| 770 | * Copy the string, or as much of it as fits, into the dest buffer. The |
| 771 | * behavior is undefined if the string buffers overlap. The destination |
| 772 | * buffer is always %NUL terminated, unless it's zero-sized. |
| 773 | * |
| 774 | * If the source string is shorter than the destination buffer, zeros |
| 775 | * the tail of the destination buffer. |
| 776 | * |
| 777 | * For full explanation of why you may want to consider using the |
| 778 | * 'strscpy' functions please see the function docstring for strscpy(). |
| 779 | * |
| 780 | * Returns: |
| 781 | * * The number of characters copied (not including the trailing %NUL) |
| 782 | * * -E2BIG if count is 0 or @src was truncated. |
| 783 | */ |
| 784 | ssize_t strscpy_pad(char *dest, const char *src, size_t count) |
| 785 | { |
| 786 | ssize_t written; |
| 787 | |
| 788 | written = strscpy(dest, src, count); |
| 789 | if (written < 0 || written == count - 1) |
| 790 | return written; |
| 791 | |
| 792 | memset(dest + written + 1, 0, count - written - 1); |
| 793 | |
| 794 | return written; |
| 795 | } |
| 796 | EXPORT_SYMBOL(strscpy_pad); |
| 797 | |
| 798 | /** |
| 799 | * skip_spaces - Removes leading whitespace from @str. |
| 800 | * @str: The string to be stripped. |
| 801 | * |
| 802 | * Returns a pointer to the first non-whitespace character in @str. |
| 803 | */ |
| 804 | char *skip_spaces(const char *str) |
| 805 | { |
| 806 | while (isspace(*str)) |
| 807 | ++str; |
| 808 | return (char *)str; |
| 809 | } |
| 810 | EXPORT_SYMBOL(skip_spaces); |
| 811 | |
| 812 | /** |
| 813 | * strim - Removes leading and trailing whitespace from @s. |
| 814 | * @s: The string to be stripped. |
| 815 | * |
| 816 | * Note that the first trailing whitespace is replaced with a %NUL-terminator |
| 817 | * in the given string @s. Returns a pointer to the first non-whitespace |
| 818 | * character in @s. |
| 819 | */ |
| 820 | char *strim(char *s) |
| 821 | { |
| 822 | size_t size; |
| 823 | char *end; |
| 824 | |
| 825 | size = strlen(s); |
| 826 | if (!size) |
| 827 | return s; |
| 828 | |
| 829 | end = s + size - 1; |
| 830 | while (end >= s && isspace(*end)) |
| 831 | end--; |
| 832 | *(end + 1) = '\0'; |
| 833 | |
| 834 | return skip_spaces(s); |
| 835 | } |
| 836 | EXPORT_SYMBOL(strim); |
| 837 | |
| 838 | /** |
| 839 | * sysfs_streq - return true if strings are equal, modulo trailing newline |
| 840 | * @s1: one string |
| 841 | * @s2: another string |
| 842 | * |
| 843 | * This routine returns true iff two strings are equal, treating both |
| 844 | * NUL and newline-then-NUL as equivalent string terminations. It's |
| 845 | * geared for use with sysfs input strings, which generally terminate |
| 846 | * with newlines but are compared against values without newlines. |
| 847 | */ |
| 848 | bool sysfs_streq(const char *s1, const char *s2) |
| 849 | { |
| 850 | while (*s1 && *s1 == *s2) { |
| 851 | s1++; |
| 852 | s2++; |
| 853 | } |
| 854 | |
| 855 | if (*s1 == *s2) |
| 856 | return true; |
| 857 | if (!*s1 && *s2 == '\n' && !s2[1]) |
| 858 | return true; |
| 859 | if (*s1 == '\n' && !s1[1] && !*s2) |
| 860 | return true; |
| 861 | return false; |
| 862 | } |
| 863 | EXPORT_SYMBOL(sysfs_streq); |
| 864 | |
| 865 | /** |
| 866 | * match_string - matches given string in an array |
| 867 | * @array: array of strings |
| 868 | * @n: number of strings in the array or -1 for NULL terminated arrays |
| 869 | * @string: string to match with |
| 870 | * |
| 871 | * This routine will look for a string in an array of strings up to the |
| 872 | * n-th element in the array or until the first NULL element. |
| 873 | * |
| 874 | * Historically the value of -1 for @n, was used to search in arrays that |
| 875 | * are NULL terminated. However, the function does not make a distinction |
| 876 | * when finishing the search: either @n elements have been compared OR |
| 877 | * the first NULL element was found. |
| 878 | * |
| 879 | * Return: |
| 880 | * index of a @string in the @array if matches, or %-EINVAL otherwise. |
| 881 | */ |
| 882 | int match_string(const char * const *array, size_t n, const char *string) |
| 883 | { |
| 884 | int index; |
| 885 | const char *item; |
| 886 | |
| 887 | for (index = 0; index < n; index++) { |
| 888 | item = array[index]; |
| 889 | if (!item) |
| 890 | break; |
| 891 | if (!strcmp(item, string)) |
| 892 | return index; |
| 893 | } |
| 894 | |
| 895 | return -EINVAL; |
| 896 | } |
| 897 | EXPORT_SYMBOL(match_string); |
| 898 | |
| 899 | /** |
| 900 | * __sysfs_match_string - matches given string in an array |
| 901 | * @array: array of strings |
| 902 | * @n: number of strings in the array or -1 for NULL terminated arrays |
| 903 | * @str: string to match with |
| 904 | * |
| 905 | * Returns index of @str in the @array or -EINVAL, just like match_string(). |
| 906 | * Uses sysfs_streq instead of strcmp for matching. |
| 907 | * |
| 908 | * This routine will look for a string in an array of strings up to the |
| 909 | * n-th element in the array or until the first NULL element. |
| 910 | * |
| 911 | * Historically the value of -1 for @n, was used to search in arrays that |
| 912 | * are NULL terminated. However, the function does not make a distinction |
| 913 | * when finishing the search: either @n elements have been compared OR |
| 914 | * the first NULL element was found. |
| 915 | */ |
| 916 | int __sysfs_match_string(const char * const *array, size_t n, const char *str) |
| 917 | { |
| 918 | const char *item; |
| 919 | int index; |
| 920 | |
| 921 | for (index = 0; index < n; index++) { |
| 922 | item = array[index]; |
| 923 | if (!item) |
| 924 | break; |
| 925 | if (sysfs_streq(item, str)) |
| 926 | return index; |
| 927 | } |
| 928 | |
| 929 | return -EINVAL; |
| 930 | } |
| 931 | EXPORT_SYMBOL(__sysfs_match_string); |
| 932 | |
| 933 | /** |
| 934 | * strreplace - Replace all occurrences of character in string. |
| 935 | * @s: The string to operate on. |
| 936 | * @old: The character being replaced. |
| 937 | * @new: The character @old is replaced with. |
| 938 | * |
| 939 | * Returns pointer to the nul byte at the end of @s. |
| 940 | */ |
| 941 | char *strreplace(char *s, char old, char new) |
| 942 | { |
| 943 | for (; *s; ++s) |
| 944 | if (*s == old) |
| 945 | *s = new; |
| 946 | return s; |
| 947 | } |
| 948 | EXPORT_SYMBOL(strreplace); |
| 949 | |
Guenter Roeck | 5c4e0a2 | 2021-11-02 07:24:20 -0700 | [diff] [blame] | 950 | /** |
| 951 | * memcpy_and_pad - Copy one buffer to another with padding |
| 952 | * @dest: Where to copy to |
| 953 | * @dest_len: The destination buffer size |
| 954 | * @src: Where to copy from |
| 955 | * @count: The number of bytes to copy |
| 956 | * @pad: Character to use for padding if space is left in destination. |
| 957 | */ |
| 958 | void memcpy_and_pad(void *dest, size_t dest_len, const void *src, size_t count, |
| 959 | int pad) |
| 960 | { |
| 961 | if (dest_len > count) { |
| 962 | memcpy(dest, src, count); |
| 963 | memset(dest + count, pad, dest_len - count); |
| 964 | } else { |
| 965 | memcpy(dest, src, dest_len); |
| 966 | } |
| 967 | } |
| 968 | EXPORT_SYMBOL(memcpy_and_pad); |
| 969 | |
Kees Cook | c430f60 | 2021-04-14 15:45:39 -0700 | [diff] [blame] | 970 | #ifdef CONFIG_FORTIFY_SOURCE |
Kees Cook | cfecea6 | 2021-06-18 10:57:38 -0700 | [diff] [blame] | 971 | void fortify_panic(const char *name) |
| 972 | { |
| 973 | pr_emerg("detected buffer overflow in %s\n", name); |
| 974 | BUG(); |
| 975 | } |
| 976 | EXPORT_SYMBOL(fortify_panic); |
Kees Cook | c430f60 | 2021-04-14 15:45:39 -0700 | [diff] [blame] | 977 | #endif /* CONFIG_FORTIFY_SOURCE */ |