safe.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /*
  2. Copyright (c) 2011, 2012, Simon Howard
  3. Permission to use, copy, modify, and/or distribute this software
  4. for any purpose with or without fee is hereby granted, provided
  5. that the above copyright notice and this permission notice appear
  6. in all copies.
  7. THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
  8. WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
  9. WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
  10. AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR
  11. CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
  12. LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
  13. NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
  14. CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  15. */
  16. #include <stdio.h>
  17. #include <stdarg.h>
  18. #include <stdlib.h>
  19. #include "lib/lha_arch.h"
  20. // Routines for safe terminal output.
  21. //
  22. // Data in LHA files (eg. filenames) may contain malicious string
  23. // data. If printed carelessly, this can include terminal emulator
  24. // commands that cause very unpleasant things to occur. For more
  25. // information, see:
  26. //
  27. // http://marc.info/?l=bugtraq&m=104612710031920&w=2
  28. //
  29. // Quote:
  30. // > Many of the features supported by popular terminal emulator
  31. // > software can be abused when un-trusted data is displayed on the
  32. // > screen. The impact of this abuse can range from annoying screen
  33. // > garbage to a complete system compromise.
  34. // TODO: This may not be ideal behavior for handling files with
  35. // names that contain Unicode characters.
  36. static void safe_output(FILE *stream, unsigned char *str)
  37. {
  38. unsigned char *p;
  39. for (p = str; *p != '\0'; ++p) {
  40. // Accept only plain ASCII characters.
  41. // Control characters (0x00-0x1f) are rejected,
  42. // as is 0x7f and all characters in the upper range.
  43. if (*p < 0x20 || *p >= 0x7f) {
  44. *p = '?';
  45. }
  46. }
  47. fprintf(stream, "%s", str);
  48. }
  49. // Version of printf() that strips out any potentially malicious
  50. // characters from the outputted string.
  51. // Note: all escape characters are considered potentially malicious,
  52. // including newline characters.
  53. int safe_fprintf(FILE *stream, char *format, ...)
  54. {
  55. va_list args;
  56. int result;
  57. unsigned char *str;
  58. va_start(args, format);
  59. result = lha_arch_vasprintf((char **) &str, format, args);
  60. if (str == NULL) {
  61. exit(-1);
  62. }
  63. safe_output(stream, str);
  64. free(str);
  65. va_end(args);
  66. return result;
  67. }
  68. int safe_printf(char *format, ...)
  69. {
  70. va_list args;
  71. int result;
  72. unsigned char *str;
  73. va_start(args, format);
  74. result = lha_arch_vasprintf((char **) &str, format, args);
  75. if (str == NULL) {
  76. exit(-1);
  77. }
  78. safe_output(stdout, str);
  79. free(str);
  80. va_end(args);
  81. return result;
  82. }