CVE-2014-1943.patch 4.2 KB

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