TEMP-0000000-B67840.6.0056ec3.patch 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. Subject: Add a limit to the number of times a name/use entries can be used
  2. Upstream-Author: Christos Zoulas <christos@zoulas.com>
  3. Date: Fri Nov 28 02:35:05 2014 +0000
  4. Origin: FILE5_20-37-g0056ec3
  5. Last-Update: 2015-01-05
  6. (prequisite for TEMP-0000000-B67840)
  7. diff --git a/doc/file.man b/doc/file.man
  8. index e4ce958..8ea29ef 100644
  9. --- a/doc/file.man
  10. +++ b/doc/file.man
  11. @@ -293,10 +293,11 @@ attempt to preserve the access time of files analyzed, to pretend that
  12. never read them.
  13. .It Fl P , Fl Fl parameter Ar name=value
  14. Set various parameter limits.
  15. -.Bl -column "indir" "Default" "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" -offset indent
  16. +.Bl -column "namenum" "Default" "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" -offset indent
  17. .It Sy "Name" Ta Sy "Default" Ta Sy "Explanation"
  18. .It Li indir Ta 15 Ta recursion limit for indirect magic
  19. .It Li name Ta 40 Ta recursion limit for name/use magic
  20. +.It Li namenum Ta 30 Ta use count limit for name/use magic
  21. .It Li phnum Ta 128 Ta max ELF program sections processed
  22. .It Li shnum Ta 32768 Ta max ELF sections processed
  23. .El
  24. diff --git a/doc/libmagic.man b/doc/libmagic.man
  25. index ba06c9f..57ec7dc 100644
  26. --- a/doc/libmagic.man
  27. +++ b/doc/libmagic.man
  28. @@ -262,6 +262,7 @@ library.
  29. .It Sy "Parameter" Ta Sy "Type" Ta Sy "Default"
  30. .It Li MAGIC_PARAM_INDIR_RECURSION Ta size_t Ta 15
  31. .It Li MAGIC_PARAM_NAME_RECURSION Ta size_t Ta 40
  32. +.It Li MAGIC_PARAM_NAME_MAX Ta size_t Ta 30
  33. .It Li MAGIC_PARAM_PHNUM_MAX Ta size_t Ta 128
  34. .It Li MAGIC_PARAM_SHNUM_MAX Ta size_t Ta 32768
  35. .El
  36. @@ -277,6 +278,10 @@ parameter controls how many levels of recursion will be followed for
  37. for name/use calls.
  38. .Pp
  39. The
  40. +.Dv MAGIC_PARAM_NAME_MAX
  41. +parameter controls the maximum number of calls for name/use.
  42. +.Pp
  43. +The
  44. .Dv MAGIC_PARAM_PHNUM_MAX
  45. parameter controls how many elf program sections will be processed.
  46. .Pp
  47. diff --git a/src/ascmagic.c b/src/ascmagic.c
  48. index 9f4b012..3388682 100644
  49. --- a/src/ascmagic.c
  50. +++ b/src/ascmagic.c
  51. @@ -147,7 +147,8 @@ file_ascmagic_with_encoding(struct magic_set *ms, const unsigned char *buf,
  52. == NULL)
  53. goto done;
  54. if ((rv = file_softmagic(ms, utf8_buf,
  55. - (size_t)(utf8_end - utf8_buf), 0, 0, TEXTTEST, text)) == 0)
  56. + (size_t)(utf8_end - utf8_buf), 0, 0, NULL,
  57. + TEXTTEST, text)) == 0)
  58. rv = -1;
  59. }
  60. diff --git a/src/file.c b/src/file.c
  61. index 4bb3102..702a613 100644
  62. --- a/src/file.c
  63. +++ b/src/file.c
  64. @@ -126,6 +126,7 @@ private struct {
  65. } pm[] = {
  66. { "indir", MAGIC_PARAM_INDIR_RECURSION, 0 },
  67. { "name", MAGIC_PARAM_NAME_RECURSION, 0 },
  68. + { "namenum", MAGIC_PARAM_NAME_MAX, 0 },
  69. { "phnum", MAGIC_PARAM_PHNUM_MAX, 0 },
  70. { "shnum", MAGIC_PARAM_SHNUM_MAX, 0 },
  71. };
  72. diff --git a/src/file.h b/src/file.h
  73. index 237bc4d..6692b3a 100644
  74. --- a/src/file.h
  75. +++ b/src/file.h
  76. @@ -382,12 +382,14 @@ struct magic_set {
  77. union VALUETYPE ms_value; /* either number or string */
  78. uint16_t indir_recursion;
  79. uint16_t name_recursion;
  80. + uint16_t name_max;
  81. uint16_t shnum_max;
  82. uint16_t phnum_max;
  83. #define FILE_INDIR_RECURSION 15
  84. #define FILE_NAME_RECURSION 40
  85. -#define FILE_ELF_SHNUM 32768
  86. -#define FILE_ELF_PHNUM 128
  87. +#define FILE_NAME_MAX 30
  88. +#define FILE_ELF_SHNUM 32768
  89. +#define FILE_ELF_PHNUM 128
  90. };
  91. /* Type for Unicode characters */
  92. @@ -422,7 +424,7 @@ protected int file_encoding(struct magic_set *, const unsigned char *, size_t,
  93. unichar **, size_t *, const char **, const char **, const char **);
  94. protected int file_is_tar(struct magic_set *, const unsigned char *, size_t);
  95. protected int file_softmagic(struct magic_set *, const unsigned char *, size_t,
  96. - uint16_t, uint16_t, int, int);
  97. + uint16_t, uint16_t, uint16_t *, int, int);
  98. protected struct mlist *file_apprentice(struct magic_set *, const char *, int);
  99. protected uint64_t file_signextend(struct magic_set *, struct magic *,
  100. uint64_t);
  101. diff --git a/src/funcs.c b/src/funcs.c
  102. index 88e77ef..91b11fc 100644
  103. --- a/src/funcs.c
  104. +++ b/src/funcs.c
  105. @@ -228,7 +228,7 @@ file_buffer(struct magic_set *ms, int fd, const char *inname __attribute__ ((unu
  106. /* try soft magic tests */
  107. if ((ms->flags & MAGIC_NO_CHECK_SOFT) == 0)
  108. - if ((m = file_softmagic(ms, ubuf, nb, 0, 0, BINTEST,
  109. + if ((m = file_softmagic(ms, ubuf, nb, 0, 0, NULL, BINTEST,
  110. looks_text)) != 0) {
  111. if ((ms->flags & MAGIC_DEBUG) != 0)
  112. (void)fprintf(stderr, "softmagic %d\n", m);
  113. diff --git a/src/magic.c b/src/magic.c
  114. index aa812df..f2bdae5 100644
  115. --- a/src/magic.c
  116. +++ b/src/magic.c
  117. @@ -235,6 +235,7 @@ magic_open(int flags)
  118. ms->line = 0;
  119. ms->indir_recursion = FILE_INDIR_RECURSION;
  120. ms->name_recursion = FILE_NAME_RECURSION;
  121. + ms->name_max = FILE_NAME_MAX;
  122. ms->shnum_max = FILE_ELF_SHNUM;
  123. ms->phnum_max = FILE_ELF_PHNUM;
  124. return ms;
  125. @@ -526,6 +527,9 @@ magic_setparam(struct magic_set *ms, int param, const void *val)
  126. case MAGIC_PARAM_NAME_RECURSION:
  127. ms->name_recursion = *(const size_t *)val;
  128. return 0;
  129. + case MAGIC_PARAM_NAME_MAX:
  130. + ms->name_max = *(const size_t *)val;
  131. + return 0;
  132. case MAGIC_PARAM_PHNUM_MAX:
  133. ms->phnum_max = *(const size_t *)val;
  134. return 0;
  135. @@ -548,6 +552,9 @@ magic_getparam(struct magic_set *ms, int param, void *val)
  136. case MAGIC_PARAM_NAME_RECURSION:
  137. *(size_t *)val = ms->name_recursion;
  138. return 0;
  139. + case MAGIC_PARAM_NAME_MAX:
  140. + *(size_t *)val = ms->name_max;
  141. + return 0;
  142. case MAGIC_PARAM_PHNUM_MAX:
  143. *(size_t *)val = ms->phnum_max;
  144. return 0;
  145. diff --git a/src/magic.h b/src/magic.h
  146. index 20c05c1..c2667cc 100644
  147. --- a/src/magic.h
  148. +++ b/src/magic.h
  149. @@ -101,8 +101,9 @@ int magic_errno(magic_t);
  150. #define MAGIC_PARAM_INDIR_RECURSION 0
  151. #define MAGIC_PARAM_NAME_RECURSION 1
  152. -#define MAGIC_PARAM_PHNUM_MAX 2
  153. -#define MAGIC_PARAM_SHNUM_MAX 3
  154. +#define MAGIC_PARAM_NAME_MAX 2
  155. +#define MAGIC_PARAM_PHNUM_MAX 3
  156. +#define MAGIC_PARAM_SHNUM_MAX 4
  157. int magic_setparam(magic_t, int, const void *);
  158. int magic_getparam(magic_t, int, void *);
  159. diff --git a/src/softmagic.c b/src/softmagic.c
  160. index d507e11..6e94ae9 100644
  161. --- a/src/softmagic.c
  162. +++ b/src/softmagic.c
  163. @@ -43,9 +43,9 @@ FILE_RCSID("@(#)$File: softmagic.c,v 1.147 2011/11/05 15:44:22 rrt Exp $")
  164. private int match(struct magic_set *, struct magic *, uint32_t,
  165. - const unsigned char *, size_t, int, int, uint16_t, uint16_t);
  166. + const unsigned char *, size_t, int, int, uint16_t, uint16_t, uint16_t *);
  167. private int mget(struct magic_set *, const unsigned char *,
  168. - struct magic *, size_t, unsigned int, int, uint16_t, uint16_t);
  169. + struct magic *, size_t, unsigned int, int, uint16_t, uint16_t, uint16_t *);
  170. private int magiccheck(struct magic_set *, struct magic *);
  171. private int32_t mprint(struct magic_set *, struct magic *);
  172. private int32_t moffset(struct magic_set *, struct magic *);
  173. @@ -69,13 +69,20 @@ private void cvt_64(union VALUETYPE *, const struct magic *);
  174. /*ARGSUSED1*/ /* nbytes passed for regularity, maybe need later */
  175. protected int
  176. file_softmagic(struct magic_set *ms, const unsigned char *buf, size_t nbytes,
  177. - uint16_t indir_level, uint16_t name_level, int mode, int text)
  178. + uint16_t indir_level, uint16_t name_level, uint16_t *name_count,
  179. + int mode, int text)
  180. {
  181. struct mlist *ml;
  182. + uint16_t nc;
  183. + if (name_count == NULL) {
  184. + nc = 0;
  185. + name_count = &nc;
  186. + }
  187. +
  188. int rv;
  189. for (ml = ms->mlist->next; ml != ms->mlist; ml = ml->next)
  190. if ((rv = match(ms, ml->magic, ml->nmagic, buf, nbytes, mode,
  191. - text, indir_level, name_level)) != 0)
  192. + text, indir_level, name_level, name_count)) != 0)
  193. return rv;
  194. return 0;
  195. @@ -111,7 +118,7 @@ file_softmagic(struct magic_set *ms, const unsigned char *buf, size_t nbytes,
  196. private int
  197. match(struct magic_set *ms, struct magic *magic, uint32_t nmagic,
  198. const unsigned char *s, size_t nbytes, int mode, int text,
  199. - uint16_t indir_level, uint16_t name_level)
  200. + uint16_t indir_level, uint16_t name_level, uint16_t *name_count)
  201. {
  202. uint32_t magindex = 0;
  203. unsigned int cont_level = 0;
  204. @@ -143,7 +150,7 @@ match(struct magic_set *ms, struct magic *magic, uint32_t nmagic,
  205. ms->line = m->lineno;
  206. /* if main entry matches, print it... */
  207. - switch (mget(ms, s, m, nbytes, cont_level, text, indir_level, name_level)) {
  208. + switch (mget(ms, s, m, nbytes, cont_level, text, indir_level, name_level, name_count)) {
  209. case -1:
  210. return -1;
  211. case 0:
  212. @@ -226,7 +233,7 @@ match(struct magic_set *ms, struct magic *magic, uint32_t nmagic,
  213. continue;
  214. }
  215. #endif
  216. - switch (mget(ms, s, m, nbytes, cont_level, text, indir_level, name_level)) {
  217. + switch (mget(ms, s, m, nbytes, cont_level, text, indir_level, name_level, name_count)) {
  218. case -1:
  219. return -1;
  220. case 0:
  221. @@ -1042,7 +1049,7 @@ mcopy(struct magic_set *ms, union VALUETYPE *p, int type, int indir,
  222. private int
  223. mget(struct magic_set *ms, const unsigned char *s,
  224. struct magic *m, size_t nbytes, unsigned int cont_level, int text,
  225. - uint16_t indir_level, uint16_t name_level)
  226. + uint16_t indir_level, uint16_t name_level, uint16_t *name_count)
  227. {
  228. uint32_t offset = ms->offset;
  229. file_pushbuf_t *pb;
  230. @@ -1062,6 +1069,12 @@ mget(struct magic_set *ms, const unsigned char *s,
  231. return -1;
  232. }
  233. + if (*name_count >= ms->name_max) {
  234. + file_error(ms, 0, "name use count (%hu) exceeded",
  235. + *name_count);
  236. + return -1;
  237. + }
  238. +
  239. if (mcopy(ms, p, m->type, m->flag & INDIR, s, offset,
  240. (uint32_t)nbytes, m) == -1)
  241. return -1;
  242. @@ -1625,7 +1638,7 @@ mget(struct magic_set *ms, const unsigned char *s,
  243. return -1;
  244. rv = file_softmagic(ms, s + offset, nbytes - offset,
  245. - indir_level + 1, name_level, BINTEST, text);
  246. + indir_level + 1, name_level, name_count, BINTEST, text);
  247. if ((ms->flags & MAGIC_DEBUG) != 0)
  248. fprintf(stderr, "indirect @offs=%u[%d]\n", offset, rv);