Loading...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 | /* * Copyright (c) 2015-2016 Apple Inc. All rights reserved. * * @APPLE_LICENSE_HEADER_START@ * * This file contains Original Code and/or Modifications of Original Code * as defined in and that are subject to the Apple Public Source License * Version 2.0 (the 'License'). You may not use this file except in * compliance with the License. Please obtain a copy of the License at * http://www.opensource.apple.com/apsl/ and read it before using this * file. * * The Original Code and all software distributed under the License are * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. * Please see the License for the specific language governing rights and * limitations under the License. * * @APPLE_LICENSE_HEADER_END@ */ #ifndef log_encode_h #define log_encode_h #include "log_encode_types.h" #include <sys/param.h> #ifdef KERNEL #define isdigit(ch) (((ch) >= '0') && ((ch) <= '9')) extern boolean_t doprnt_hide_pointers; #endif static bool _encode_data(os_log_buffer_value_t content, const void *arg, uint16_t arg_len, os_log_buffer_context_t context) { struct os_log_arginfo_s arginfo; void *databuf; if (content->flags & OS_LOG_CONTENT_FLAG_PRIVATE) { databuf = context->privdata + context->privdata_off; arginfo.length = MIN(arg_len, (context->privdata_sz - context->privdata_off)); arginfo.offset = context->privdata_off; } else { databuf = context->pubdata + context->pubdata_off; arginfo.length = MIN(arg_len, (context->pubdata_sz - context->pubdata_off)); arginfo.offset = context->pubdata_off; } if (context->arg_content_sz > 0) { arginfo.length = MIN(context->arg_content_sz, arginfo.length); } memcpy(content->value, &arginfo, sizeof(arginfo)); content->size = sizeof(arginfo); if (arginfo.length) { if (content->type == OS_LOG_BUFFER_VALUE_TYPE_STRING #ifndef KERNEL || content->type == OS_LOG_BUFFER_VALUE_TYPE_OBJECT #endif ) { strlcpy(databuf, arg, arginfo.length); } else { memcpy(databuf, arg, arginfo.length); } } if (content->flags & OS_LOG_CONTENT_FLAG_PRIVATE) { context->privdata_off += arginfo.length; } else { context->pubdata_off += arginfo.length; } context->content_off += sizeof(*content) + content->size; context->arg_content_sz = 0; return true; } #ifndef KERNEL static void _os_log_parse_annotated(char *annotated, const char **visibility, const char **library, const char **type) { char *values[3] = { NULL }; int cnt = 0; int idx = 0; for (; cnt < 3;) { char *token = strsep(&annotated, ", {}"); if (token == NULL) { break; } if (*token == '\0') { continue; } values[cnt++] = token; } if ((cnt > 0) && (!strcmp(values[0], "public") || !strcmp(values[0], "private"))) { if (visibility != NULL) { (*visibility) = values[0]; } idx++; } if (idx < cnt && (library != NULL) && (type != NULL)) { char *decoder = values[idx]; for (cnt = 0; cnt < 3; ) { char *token = strsep(&decoder, ": {}"); if (token == NULL) { break; } if (*token == '\0') { continue; } values[cnt++] = token; } if (cnt == 2) { (*library) = values[0]; (*type) = values[1]; } if (cnt == 1) { (*library) = "builtin"; (*type) = values[0]; } } } #endif /* !KERNEL */ OS_ALWAYS_INLINE static inline bool _os_log_encode_arg(void *arg, uint16_t arg_len, os_log_value_type_t ctype, bool is_private, os_log_buffer_context_t context) { os_log_buffer_value_t content = (os_log_buffer_value_t) &context->buffer->content[context->content_off]; size_t content_sz = sizeof(*content) + arg_len; char tempString[OS_LOG_BUFFER_MAX_SIZE] = {}; #ifndef KERNEL bool obj_private = true; #endif #ifdef KERNEL /* scrub kernel pointers */ if (doprnt_hide_pointers && ctype == OS_LOG_BUFFER_VALUE_TYPE_SCALAR && arg_len >= sizeof(void *)) { unsigned long long value = 0; memcpy(&value, arg, arg_len); if (value >= VM_MIN_KERNEL_AND_KEXT_ADDRESS && value <= VM_MAX_KERNEL_ADDRESS) { is_private = true; bzero(arg, arg_len); } } #endif content->type = ctype; content->flags = (is_private ? OS_LOG_CONTENT_FLAG_PRIVATE : 0); #ifndef KERNEL if (context->annotated != NULL) { const char *visibility = NULL; _os_log_parse_annotated(context->annotated, &visibility, NULL, NULL); if (visibility) { if (!strcasecmp(visibility, "private")) { content->flags |= OS_LOG_CONTENT_FLAG_PRIVATE; } else if (!strcasecmp(visibility, "public")) { content->flags &= ~OS_LOG_CONTENT_FLAG_PRIVATE; } } context->annotated = NULL; } #endif /* !KERNEL */ switch (ctype) { case OS_LOG_BUFFER_VALUE_TYPE_COUNT: case OS_LOG_BUFFER_VALUE_TYPE_SCALAR: if (is_private) { _encode_data(content, tempString, strlen(tempString) + 1, context); } else { if ((context->content_off + content_sz) > context->content_sz) { return false; } memcpy(content->value, arg, arg_len); content->size = arg_len; context->content_off += content_sz; } break; case OS_LOG_BUFFER_VALUE_TYPE_STRING: context->buffer->flags |= OS_LOG_BUFFER_HAS_NON_SCALAR; if (_os_log_string_is_public(arg)) { content->flags &= ~OS_LOG_CONTENT_FLAG_PRIVATE; } _encode_data(content, arg, arg_len, context); break; #ifndef KERNEL case OS_LOG_BUFFER_VALUE_TYPE_POINTER: context->buffer->flags |= OS_LOG_BUFFER_HAS_NON_SCALAR; _encode_data(content, arg, arg_len, context); break; case OS_LOG_BUFFER_VALUE_TYPE_OBJECT: context->buffer->flags |= OS_LOG_BUFFER_HAS_NON_SCALAR; if (!_NSCF2data(arg, tempString, sizeof(tempString), &obj_private)) { tempString[0] = '\0'; } if (!obj_private) { content->flags &= ~OS_LOG_CONTENT_FLAG_PRIVATE; } _encode_data(content, tempString, strlen(tempString) + 1, context); break; #endif /* !KERNEL */ } if (content->flags & OS_LOG_CONTENT_FLAG_PRIVATE) { context->buffer->flags |= OS_LOG_BUFFER_HAS_PRIVATE; } context->arg_idx++; return true; } static bool _os_log_encode(const char *format, va_list args, int saved_errno, os_log_buffer_context_t context) { const char *percent = strchr(format, '%'); #ifndef KERNEL char annotated[256]; #endif while (percent != NULL) { ++percent; if (percent[0] != '%') { struct os_log_format_value_s value; int type = OST_INT; #ifndef KERNEL bool long_double = false; #endif int prec = 0; char ch; for (bool done = false; !done; percent++) { switch (ch = percent[0]) { /* type of types or other */ case 'l': // longer type++; break; case 'h': // shorter type--; break; case 'z': type = OST_SIZE; break; case 'j': type = OST_INTMAX; break; case 't': type = OST_PTRDIFF; break; case '.': // precision if ((percent[1]) == '*') { prec = va_arg(args, int); _os_log_encode_arg(&prec, sizeof(prec), OS_LOG_BUFFER_VALUE_TYPE_COUNT, false, context); percent++; continue; } else { // we have to read the precision and do the right thing const char *fmt = percent + 1; prec = 0; while (isdigit(ch = *fmt++)) { prec = 10 * prec + (ch - '0'); } if (prec > 1024) { prec = 1024; } _os_log_encode_arg(&prec, sizeof(prec), OS_LOG_BUFFER_VALUE_TYPE_COUNT, false, context); } break; case '-': // left-align case '+': // force sign case ' ': // prefix non-negative with space case '#': // alternate case '\'': // group by thousands break; /* fixed types */ case 'd': // integer case 'i': // integer case 'o': // octal case 'u': // unsigned case 'x': // hex case 'X': // upper-hex switch (type) { case OST_CHAR: value.type.ch = va_arg(args, int); _os_log_encode_arg(&value.type.ch, sizeof(value.type.ch), OS_LOG_BUFFER_VALUE_TYPE_SCALAR, false, context); break; case OST_SHORT: value.type.s = va_arg(args, int); _os_log_encode_arg(&value.type.s, sizeof(value.type.s), OS_LOG_BUFFER_VALUE_TYPE_SCALAR, false, context); break; case OST_INT: value.type.i = va_arg(args, int); _os_log_encode_arg(&value.type.i, sizeof(value.type.i), OS_LOG_BUFFER_VALUE_TYPE_SCALAR, false, context); break; case OST_LONG: value.type.l = va_arg(args, long); _os_log_encode_arg(&value.type.l, sizeof(value.type.l), OS_LOG_BUFFER_VALUE_TYPE_SCALAR, false, context); break; case OST_LONGLONG: value.type.ll = va_arg(args, long long); _os_log_encode_arg(&value.type.ll, sizeof(value.type.ll), OS_LOG_BUFFER_VALUE_TYPE_SCALAR, false, context); break; case OST_SIZE: value.type.z = va_arg(args, size_t); _os_log_encode_arg(&value.type.z, sizeof(value.type.z), OS_LOG_BUFFER_VALUE_TYPE_SCALAR, false, context); break; case OST_INTMAX: value.type.im = va_arg(args, intmax_t); _os_log_encode_arg(&value.type.im, sizeof(value.type.im), OS_LOG_BUFFER_VALUE_TYPE_SCALAR, false, context); break; case OST_PTRDIFF: value.type.pd = va_arg(args, ptrdiff_t); _os_log_encode_arg(&value.type.pd, sizeof(value.type.pd), OS_LOG_BUFFER_VALUE_TYPE_SCALAR, false, context); break; default: return false; } done = true; break; #ifndef KERNEL case '{': // we do not support this for shimmed code if (context->shimmed) { return false; } for (const char *curr2 = percent + 1; (ch = (*curr2)) != NUL; curr2++) { if (ch == '}') { strlcpy(annotated, percent, MIN(curr2 - (percent + 1), sizeof(annotated))); context->annotated = annotated; percent = curr2; break; } } break; #endif /* !KERNEL */ case 'p': // pointer value.type.p = va_arg(args, void *); _os_log_encode_arg(&value.type.p, sizeof(value.type.p), OS_LOG_BUFFER_VALUE_TYPE_SCALAR, false, context); done = true; break; #ifndef KERNEL case 'P': // pointer data if (context->shimmed) { // we do not support this for shimmed code return false; } context->buffer->flags |= OS_LOG_BUFFER_HAS_NON_SCALAR; value.type.p = va_arg(args, void *); // capture the string pointer to generate a symptom if (context->log && context->log->generate_symptoms && context->arg_idx == 1 && value.type.pch && prec) { context->symptom_ptr = value.type.p; context->symptom_ptr_len = prec; } _os_log_encode_arg(value.type.p, prec, OS_LOG_BUFFER_VALUE_TYPE_POINTER, false, context); prec = 0; done = true; break; #endif /* !KERNEL */ #ifndef KERNEL case 'L': // long double long_double = true; break; case 'a': case 'A': case 'e': case 'E': // floating types case 'f': case 'F': case 'g': case 'G': if (long_double) { value.type.ld = va_arg(args, long double); _os_log_encode_arg(&value.type.ld, sizeof(value.type.ld), OS_LOG_BUFFER_VALUE_TYPE_SCALAR, false, context); } else { value.type.d = va_arg(args, double); _os_log_encode_arg(&value.type.d, sizeof(value.type.d), OS_LOG_BUFFER_VALUE_TYPE_SCALAR, false, context); } done = true; break; #endif /* !KERNEL */ case 'c': // char value.type.ch = va_arg(args, int); _os_log_encode_arg(&value.type.ch, sizeof(value.type.ch), OS_LOG_BUFFER_VALUE_TYPE_SCALAR, false, context); done = true; break; #ifndef KERNEL case 'C': // wide-char value.type.wch = va_arg(args, wint_t); _os_log_encode_arg(&value.type.wch, sizeof(value.type.wch), OS_LOG_BUFFER_VALUE_TYPE_SCALAR, false, context); done = true; break; #endif /* !KERNEL */ case 's': // string value.type.pch = va_arg(args, char *); if (!prec && value.type.pch != NULL) { prec = (int) strlen(value.type.pch) + 1; } #ifndef KERNEL // capture the string pointer to generate a symptom if (context->log && context->log->generate_symptoms && context->arg_idx == 0 && value.type.pch) { context->symptom_str = value.type.pch; } #endif context->buffer->flags |= OS_LOG_BUFFER_HAS_NON_SCALAR; _os_log_encode_arg(value.type.pch, prec, OS_LOG_BUFFER_VALUE_TYPE_STRING, false, context); prec = 0; done = true; break; #ifndef KERNEL case 'S': // wide-string value.type.pwch = va_arg(args, wchar_t *); if (!prec && value.type.pwch != NULL) { prec = (int) wcslen(value.type.pwch) + 1; } context->buffer->flags |= OS_LOG_BUFFER_HAS_NON_SCALAR; _os_log_encode_arg(value.type.pwch, prec, OS_LOG_BUFFER_VALUE_TYPE_STRING, false, context); prec = 0; done = true; break; #endif /* !KERNEL */ #ifndef KERNEL case '@': // CFTypeRef aka NSObject * context->buffer->flags |= OS_LOG_BUFFER_HAS_NON_SCALAR; _os_log_encode_arg(va_arg(args, void *), 0, OS_LOG_BUFFER_VALUE_TYPE_OBJECT, false, context); done = true; break; #endif /* !KERNEL */ case 'm': value.type.i = saved_errno; _os_log_encode_arg(&value.type.i, sizeof(value.type.i), OS_LOG_BUFFER_VALUE_TYPE_SCALAR, false, context); done = true; break; default: if (isdigit(ch)) { // [0-9] continue; } return false; } if (done) { percent = strchr(percent, '%'); // Find next format break; } } } else { percent = strchr(percent+1, '%'); // Find next format after %% } } context->buffer->arg_cnt = context->arg_idx; context->content_sz = context->content_off; context->pubdata_sz = context->pubdata_off; context->privdata_sz = context->privdata_off; context->arg_idx = context->content_off = context->pubdata_off = context->privdata_off = 0; return true; } #endif /* log_encode_h */ |