CVE-2014-1943.patch 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. Upstream-Author: Christos Zoulas <christos@zoulas.com>
  2. Description:
  3. prevent infinite recursion.
  4. count indirect recursion as recursion.
  5. Upstream commit IDs:
  6. 3c081560c23f20b2985c285338b52c7aae9fdb0f
  7. cc9e74dfeca5265ad725acc926ef0b8d2a18ee70
  8. Backport for 5.11: Christoph Biedl <debian.axhn@manchmal.in-ulm.de>
  9. --- a/src/softmagic.c
  10. +++ b/src/softmagic.c
  11. @@ -43,9 +43,9 @@
  12. private int match(struct magic_set *, struct magic *, uint32_t,
  13. - const unsigned char *, size_t, int, int);
  14. + const unsigned char *, size_t, int, int, int);
  15. private int mget(struct magic_set *, const unsigned char *,
  16. - struct magic *, size_t, unsigned int, int);
  17. + struct magic *, size_t, unsigned int, int, int);
  18. private int magiccheck(struct magic_set *, struct magic *);
  19. private int32_t mprint(struct magic_set *, struct magic *);
  20. private int32_t moffset(struct magic_set *, struct magic *);
  21. @@ -67,13 +67,13 @@
  22. /*ARGSUSED1*/ /* nbytes passed for regularity, maybe need later */
  23. protected int
  24. file_softmagic(struct magic_set *ms, const unsigned char *buf, size_t nbytes,
  25. - int mode, int text)
  26. + size_t level, int mode, int text)
  27. {
  28. struct mlist *ml;
  29. int rv;
  30. for (ml = ms->mlist->next; ml != ms->mlist; ml = ml->next)
  31. if ((rv = match(ms, ml->magic, ml->nmagic, buf, nbytes, mode,
  32. - text)) != 0)
  33. + text, level)) != 0)
  34. return rv;
  35. return 0;
  36. @@ -108,7 +108,8 @@
  37. */
  38. private int
  39. match(struct magic_set *ms, struct magic *magic, uint32_t nmagic,
  40. - const unsigned char *s, size_t nbytes, int mode, int text)
  41. + const unsigned char *s, size_t nbytes, int mode, int text,
  42. + int recursion_level)
  43. {
  44. uint32_t magindex = 0;
  45. unsigned int cont_level = 0;
  46. @@ -140,7 +141,7 @@
  47. ms->line = m->lineno;
  48. /* if main entry matches, print it... */
  49. - switch (mget(ms, s, m, nbytes, cont_level, text)) {
  50. + switch (mget(ms, s, m, nbytes, cont_level, text, recursion_level + 1)) {
  51. case -1:
  52. return -1;
  53. case 0:
  54. @@ -223,7 +224,7 @@
  55. continue;
  56. }
  57. #endif
  58. - switch (mget(ms, s, m, nbytes, cont_level, text)) {
  59. + switch (mget(ms, s, m, nbytes, cont_level, text, recursion_level + 1)) {
  60. case -1:
  61. return -1;
  62. case 0:
  63. @@ -1018,12 +1019,18 @@
  64. private int
  65. mget(struct magic_set *ms, const unsigned char *s,
  66. - struct magic *m, size_t nbytes, unsigned int cont_level, int text)
  67. + struct magic *m, size_t nbytes, unsigned int cont_level, int text,
  68. + int recursion_level)
  69. {
  70. uint32_t offset = ms->offset;
  71. uint32_t count = m->str_range;
  72. union VALUETYPE *p = &ms->ms_value;
  73. + if (recursion_level >= 20) {
  74. + file_error(ms, 0, "recursion nesting exceeded");
  75. + return -1;
  76. + }
  77. +
  78. if (mcopy(ms, p, m->type, m->flag & INDIR, s, offset, nbytes, count) == -1)
  79. return -1;
  80. @@ -1577,13 +1584,15 @@
  81. break;
  82. case FILE_INDIRECT:
  83. + if (offset == 0)
  84. + return 0;
  85. if ((ms->flags & (MAGIC_MIME|MAGIC_APPLE)) == 0 &&
  86. file_printf(ms, "%s", m->desc) == -1)
  87. return -1;
  88. if (nbytes < offset)
  89. return 0;
  90. return file_softmagic(ms, s + offset, nbytes - offset,
  91. - BINTEST, text);
  92. + recursion_level, BINTEST, text);
  93. case FILE_DEFAULT: /* nothing to check */
  94. default:
  95. --- a/src/ascmagic.c
  96. +++ b/src/ascmagic.c
  97. @@ -147,7 +147,7 @@
  98. == NULL)
  99. goto done;
  100. if ((rv = file_softmagic(ms, utf8_buf,
  101. - (size_t)(utf8_end - utf8_buf), TEXTTEST, text)) == 0)
  102. + (size_t)(utf8_end - utf8_buf), 0, TEXTTEST, text)) == 0)
  103. rv = -1;
  104. }
  105. --- a/src/file.h
  106. +++ b/src/file.h
  107. @@ -414,7 +414,7 @@
  108. unichar **, size_t *, const char **, const char **, const char **);
  109. protected int file_is_tar(struct magic_set *, const unsigned char *, size_t);
  110. protected int file_softmagic(struct magic_set *, const unsigned char *, size_t,
  111. - int, int);
  112. + size_t, int, int);
  113. protected struct mlist *file_apprentice(struct magic_set *, const char *, int);
  114. protected uint64_t file_signextend(struct magic_set *, struct magic *,
  115. uint64_t);
  116. --- a/src/funcs.c
  117. +++ b/src/funcs.c
  118. @@ -228,7 +228,7 @@
  119. /* try soft magic tests */
  120. if ((ms->flags & MAGIC_NO_CHECK_SOFT) == 0)
  121. - if ((m = file_softmagic(ms, ubuf, nb, BINTEST,
  122. + if ((m = file_softmagic(ms, ubuf, nb, 0, BINTEST,
  123. looks_text)) != 0) {
  124. if ((ms->flags & MAGIC_DEBUG) != 0)
  125. (void)fprintf(stderr, "softmagic %d\n", m);