escape.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /* Copyright (c) 2003, Roger Dingledine
  2. * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
  3. * Copyright (c) 2007-2018, The Tor Project, Inc. */
  4. /* See LICENSE for licensing information */
  5. #include "lib/log/escape.h"
  6. #include "lib/log/util_bug.h"
  7. #include "lib/string/compat_ctype.h"
  8. #include "lib/string/printf.h"
  9. #include "lib/malloc/util_malloc.h"
  10. /** Allocate and return a new string representing the contents of <b>s</b>,
  11. * surrounded by quotes and using standard C escapes.
  12. *
  13. * Generally, we use this for logging values that come in over the network to
  14. * keep them from tricking users, and for sending certain values to the
  15. * controller.
  16. *
  17. * We trust values from the resolver, OS, configuration file, and command line
  18. * to not be maliciously ill-formed. We validate incoming routerdescs and
  19. * SOCKS requests and addresses from BEGIN cells as they're parsed;
  20. * afterwards, we trust them as non-malicious.
  21. */
  22. char *
  23. esc_for_log(const char *s)
  24. {
  25. const char *cp;
  26. char *result, *outp;
  27. size_t len = 3;
  28. if (!s) {
  29. return tor_strdup("(null)");
  30. }
  31. for (cp = s; *cp; ++cp) {
  32. switch (*cp) {
  33. case '\\':
  34. case '\"':
  35. case '\'':
  36. case '\r':
  37. case '\n':
  38. case '\t':
  39. len += 2;
  40. break;
  41. default:
  42. if (TOR_ISPRINT(*cp) && ((uint8_t)*cp)<127)
  43. ++len;
  44. else
  45. len += 4;
  46. break;
  47. }
  48. }
  49. tor_assert(len <= SSIZE_MAX);
  50. result = outp = tor_malloc(len);
  51. *outp++ = '\"';
  52. for (cp = s; *cp; ++cp) {
  53. /* This assertion should always succeed, since we will write at least
  54. * one char here, and two chars for closing quote and nul later */
  55. tor_assert((outp-result) < (ssize_t)len-2);
  56. switch (*cp) {
  57. case '\\':
  58. case '\"':
  59. case '\'':
  60. *outp++ = '\\';
  61. *outp++ = *cp;
  62. break;
  63. case '\n':
  64. *outp++ = '\\';
  65. *outp++ = 'n';
  66. break;
  67. case '\t':
  68. *outp++ = '\\';
  69. *outp++ = 't';
  70. break;
  71. case '\r':
  72. *outp++ = '\\';
  73. *outp++ = 'r';
  74. break;
  75. default:
  76. if (TOR_ISPRINT(*cp) && ((uint8_t)*cp)<127) {
  77. *outp++ = *cp;
  78. } else {
  79. tor_assert((outp-result) < (ssize_t)len-4);
  80. tor_snprintf(outp, 5, "\\%03o", (int)(uint8_t) *cp);
  81. outp += 4;
  82. }
  83. break;
  84. }
  85. }
  86. tor_assert((outp-result) <= (ssize_t)len-2);
  87. *outp++ = '\"';
  88. *outp++ = 0;
  89. return result;
  90. }
  91. /** Similar to esc_for_log. Allocate and return a new string representing
  92. * the first n characters in <b>chars</b>, surround by quotes and using
  93. * standard C escapes. If a NUL character is encountered in <b>chars</b>,
  94. * the resulting string will be terminated there.
  95. */
  96. char *
  97. esc_for_log_len(const char *chars, size_t n)
  98. {
  99. char *string = tor_strndup(chars, n);
  100. char *string_escaped = esc_for_log(string);
  101. tor_free(string);
  102. return string_escaped;
  103. }
  104. /** Allocate and return a new string representing the contents of <b>s</b>,
  105. * surrounded by quotes and using standard C escapes.
  106. *
  107. * THIS FUNCTION IS NOT REENTRANT. Don't call it from outside the main
  108. * thread. Also, each call invalidates the last-returned value, so don't
  109. * try log_warn(LD_GENERAL, "%s %s", escaped(a), escaped(b));
  110. */
  111. const char *
  112. escaped(const char *s)
  113. {
  114. static char *escaped_val_ = NULL;
  115. tor_free(escaped_val_);
  116. if (s)
  117. escaped_val_ = esc_for_log(s);
  118. else
  119. escaped_val_ = NULL;
  120. return escaped_val_;
  121. }