TEMP-0000000-B67840.5.6ce24f3.patch 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474
  1. Subject: Kill -R and replace with -P param=value. Allow setting of 4 parameters: indir, name, shnum, phnum
  2. Upstream-Author: Christos Zoulas <christos@zoulas.com>
  3. Date: Thu Nov 27 23:42:58 2014 +0000
  4. Origin: FILE5_20-36-g6ce24f3
  5. Last-Update: 2015-01-05
  6. Kill -R and replace with -P param=value. Allow setting of 4 parameters:
  7. indir, name, shnum, phnum.
  8. (prequisite for TEMP-0000000-B67840)
  9. diff --git a/doc/file.man b/doc/file.man
  10. index f6c91e7..e4ce958 100644
  11. --- a/doc/file.man
  12. +++ b/doc/file.man
  13. @@ -16,7 +16,7 @@
  14. .Op Fl F Ar separator
  15. .Op Fl f Ar namefile
  16. .Op Fl m Ar magicfiles
  17. -.Op Fl R Ar maxrecursion
  18. +.Op Fl P Ar name=value
  19. .Ar
  20. .Ek
  21. .Nm
  22. @@ -291,16 +291,20 @@ or
  23. attempt to preserve the access time of files analyzed, to pretend that
  24. .Nm
  25. never read them.
  26. +.It Fl P , Fl Fl parameter Ar name=value
  27. +Set various parameter limits.
  28. +.Bl -column "indir" "Default" "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" -offset indent
  29. +.It Sy "Name" Ta Sy "Default" Ta Sy "Explanation"
  30. +.It Li indir Ta 15 Ta recursion limit for indirect magic
  31. +.It Li name Ta 40 Ta recursion limit for name/use magic
  32. +.It Li phnum Ta 128 Ta max ELF program sections processed
  33. +.It Li shnum Ta 32768 Ta max ELF sections processed
  34. +.El
  35. .It Fl r , Fl Fl raw
  36. Don't translate unprintable characters to \eooo.
  37. Normally
  38. .Nm
  39. translates unprintable characters to their octal representation.
  40. -.It Fl R , Fl Fl recursion Ar maxlevel
  41. -Set the maximum recursion level for indirect type magic or name/use entry
  42. -invocations.
  43. -The default is
  44. -.Dv 15 .
  45. .It Fl s , Fl Fl special-files
  46. Normally,
  47. .Nm
  48. diff --git a/doc/libmagic.man b/doc/libmagic.man
  49. index 0a2ed9a..ba06c9f 100644
  50. --- a/doc/libmagic.man
  51. +++ b/doc/libmagic.man
  52. @@ -258,14 +258,31 @@ and
  53. .Fn magic_setparam
  54. allow getting and setting various limits related to the the magic
  55. library.
  56. -.Bl -column "MAGIC_PARAM_MAX_RECURSION" "size_t" "Default" -offset indent
  57. -.It Sy "Parameter" Ta Sy "Type" Ta Sy "Default
  58. -.It Li MAGIC_PARAM_MAX_RECURSION Ta size_t Ta 15
  59. +.Bl -column "MAGIC_PARAM_INDIR_RECURSION" "size_t" "Default" -offset indent
  60. +.It Sy "Parameter" Ta Sy "Type" Ta Sy "Default"
  61. +.It Li MAGIC_PARAM_INDIR_RECURSION Ta size_t Ta 15
  62. +.It Li MAGIC_PARAM_NAME_RECURSION Ta size_t Ta 40
  63. +.It Li MAGIC_PARAM_PHNUM_MAX Ta size_t Ta 128
  64. +.It Li MAGIC_PARAM_SHNUM_MAX Ta size_t Ta 32768
  65. .El
  66. +.Pp
  67. +The
  68. +.Dv MAGIC_PARAM_INDIR_RECURSION
  69. +parameter controls how many levels of recursion will be followed for
  70. +indirect magic entries.
  71. +.Pp
  72. The
  73. -.Dv MAGIC_PARAM_MAX_RECURSION
  74. +.Dv MAGIC_PARAM_NAME_RECURSION
  75. parameter controls how many levels of recursion will be followed for
  76. -indirect magic entries or for name/use calls.
  77. +for name/use calls.
  78. +.Pp
  79. +The
  80. +.Dv MAGIC_PARAM_PHNUM_MAX
  81. +parameter controls how many elf program sections will be processed.
  82. +.Pp
  83. +The
  84. +.Dv MAGIC_PARAM_SHNUM_MAX
  85. +parameter controls how many elf sections will be processed.
  86. .Sh RETURN VALUES
  87. The function
  88. .Fn magic_open
  89. diff --git a/src/ascmagic.c b/src/ascmagic.c
  90. index 1c37e8e..9f4b012 100644
  91. --- a/src/ascmagic.c
  92. +++ b/src/ascmagic.c
  93. @@ -147,7 +147,7 @@ file_ascmagic_with_encoding(struct magic_set *ms, const unsigned char *buf,
  94. == NULL)
  95. goto done;
  96. if ((rv = file_softmagic(ms, utf8_buf,
  97. - (size_t)(utf8_end - utf8_buf), 0, TEXTTEST, text)) == 0)
  98. + (size_t)(utf8_end - utf8_buf), 0, 0, TEXTTEST, text)) == 0)
  99. rv = -1;
  100. }
  101. diff --git a/src/elfclass.h b/src/elfclass.h
  102. index 0826ce3..bda53b3 100644
  103. --- a/src/elfclass.h
  104. +++ b/src/elfclass.h
  105. @@ -36,7 +36,7 @@
  106. #ifdef ELFCORE
  107. case ET_CORE:
  108. phnum = elf_getu16(swap, elfhdr.e_phnum);
  109. - if (phnum > MAX_PHNUM)
  110. + if (phnum > ms->phnum_max)
  111. return toomany(ms, "program", phnum);
  112. flags |= FLAGS_IS_CORE;
  113. if (dophn_core(ms, clazz, swap, fd,
  114. @@ -49,10 +49,10 @@
  115. case ET_EXEC:
  116. case ET_DYN:
  117. phnum = elf_getu16(swap, elfhdr.e_phnum);
  118. - if (phnum > MAX_PHNUM)
  119. + if (phnum > ms->phnum_max)
  120. return toomany(ms, "program", phnum);
  121. shnum = elf_getu16(swap, elfhdr.e_shnum);
  122. - if (shnum > MAX_SHNUM)
  123. + if (shnum > ms->shnum_max)
  124. return toomany(ms, "section", shnum);
  125. if (dophn_exec(ms, clazz, swap, fd,
  126. (off_t)elf_getu(swap, elfhdr.e_phoff), phnum,
  127. @@ -62,7 +62,7 @@
  128. /*FALLTHROUGH*/
  129. case ET_REL:
  130. shnum = elf_getu16(swap, elfhdr.e_shnum);
  131. - if (shnum > MAX_SHNUM)
  132. + if (shnum > ms->shnum_max)
  133. return toomany(ms, "section", shnum);
  134. if (doshn(ms, clazz, swap, fd,
  135. (off_t)elf_getu(swap, elfhdr.e_shoff), shnum,
  136. diff --git a/src/file.c b/src/file.c
  137. index 2f3cf9d..4bb3102 100644
  138. --- a/src/file.c
  139. +++ b/src/file.c
  140. @@ -101,7 +101,7 @@ private const struct option long_options[] = {
  141. #undef OPT_LONGONLY
  142. {0, 0, NULL, 0}
  143. };
  144. -#define OPTSTRING "bcCde:f:F:hiklLm:nNprR:svz0"
  145. +#define OPTSTRING "bcCde:f:F:hiklLm:nNprP:rsvz0"
  146. private const struct {
  147. const char *name;
  148. @@ -119,6 +119,17 @@ private const struct {
  149. { "tokens", MAGIC_NO_CHECK_TOKENS }, /* OBSOLETE: ignored for backwards compatibility */
  150. };
  151. +private struct {
  152. + const char *name;
  153. + int tag;
  154. + size_t value;
  155. +} pm[] = {
  156. + { "indir", MAGIC_PARAM_INDIR_RECURSION, 0 },
  157. + { "name", MAGIC_PARAM_NAME_RECURSION, 0 },
  158. + { "phnum", MAGIC_PARAM_PHNUM_MAX, 0 },
  159. + { "shnum", MAGIC_PARAM_SHNUM_MAX, 0 },
  160. +};
  161. +
  162. private char *progname; /* used throughout */
  163. private void usage(void);
  164. @@ -128,6 +139,8 @@ int main(int, char *[]);
  165. private int unwrap(struct magic_set *, const char *);
  166. private int process(struct magic_set *ms, const char *, int);
  167. private struct magic_set *load(const char *, int);
  168. +private void setparam(const char *);
  169. +private void applyparam(magic_t);
  170. /*
  171. @@ -140,7 +153,6 @@ main(int argc, char *argv[])
  172. size_t i;
  173. int action = 0, didsomefiles = 0, errflg = 0;
  174. int flags = 0, e = 0;
  175. - size_t max_recursion = 0;
  176. struct magic_set *magic = NULL;
  177. int longindex;
  178. const char *magicfile = NULL; /* where the magic is */
  179. @@ -241,11 +253,12 @@ main(int argc, char *argv[])
  180. flags |= MAGIC_PRESERVE_ATIME;
  181. break;
  182. #endif
  183. + case 'P':
  184. + setparam(optarg);
  185. + break;
  186. case 'r':
  187. flags |= MAGIC_RAW;
  188. break;
  189. - case 'R':
  190. - max_recursion = atoi(optarg);
  191. break;
  192. case 's':
  193. flags |= MAGIC_DEVICES;
  194. @@ -319,16 +332,7 @@ main(int argc, char *argv[])
  195. if (magic == NULL)
  196. if ((magic = load(magicfile, flags)) == NULL)
  197. return 1;
  198. - if (max_recursion) {
  199. - if (magic_setparam(magic, MAGIC_PARAM_MAX_RECURSION,
  200. - &max_recursion) == -1) {
  201. - (void)fprintf(stderr,
  202. - "%s: Can't set recurision %s\n", progname,
  203. - strerror(errno));
  204. - return 1;
  205. - }
  206. - }
  207. - break;
  208. + applyparam(magic);
  209. }
  210. if (optind == argc) {
  211. @@ -358,6 +362,41 @@ main(int argc, char *argv[])
  212. return e;
  213. }
  214. +private void
  215. +applyparam(magic_t magic)
  216. +{
  217. + size_t i;
  218. +
  219. + for (i = 0; i < __arraycount(pm); i++) {
  220. + if (pm[i].value == 0)
  221. + continue;
  222. + if (magic_setparam(magic, pm[i].tag, &pm[i].value) == -1) {
  223. + (void)fprintf(stderr, "%s: Can't set %s %s\n", progname,
  224. + pm[i].name, strerror(errno));
  225. + exit(1);
  226. + }
  227. + }
  228. +}
  229. +
  230. +private void
  231. +setparam(const char *p)
  232. +{
  233. + size_t i;
  234. + char *s;
  235. +
  236. + if ((s = strchr(p, '=')) == NULL)
  237. + goto badparm;
  238. +
  239. + for (i = 0; i < __arraycount(pm); i++) {
  240. + if (strncmp(p, pm[i].name, s - p) != 0)
  241. + continue;
  242. + pm[i].value = atoi(s + 1);
  243. + return;
  244. + }
  245. +badparm:
  246. + (void)fprintf(stderr, "%s: Unknown param %s\n", progname, p);
  247. + exit(1);
  248. +}
  249. private struct magic_set *
  250. /*ARGSUSED*/
  251. diff --git a/src/file.h b/src/file.h
  252. index 71a3aeb..237bc4d 100644
  253. --- a/src/file.h
  254. +++ b/src/file.h
  255. @@ -380,8 +380,14 @@ struct magic_set {
  256. /* FIXME: Make the string dynamically allocated so that e.g.
  257. strings matched in files can be longer than MAXstring */
  258. union VALUETYPE ms_value; /* either number or string */
  259. - size_t max_recursion;
  260. -#define FILE_MAX_RECURSION 15
  261. + uint16_t indir_recursion;
  262. + uint16_t name_recursion;
  263. + uint16_t shnum_max;
  264. + uint16_t phnum_max;
  265. +#define FILE_INDIR_RECURSION 15
  266. +#define FILE_NAME_RECURSION 40
  267. +#define FILE_ELF_SHNUM 32768
  268. +#define FILE_ELF_PHNUM 128
  269. };
  270. /* Type for Unicode characters */
  271. @@ -416,7 +422,7 @@ protected int file_encoding(struct magic_set *, const unsigned char *, size_t,
  272. unichar **, size_t *, const char **, const char **, const char **);
  273. protected int file_is_tar(struct magic_set *, const unsigned char *, size_t);
  274. protected int file_softmagic(struct magic_set *, const unsigned char *, size_t,
  275. - size_t, int, int);
  276. + uint16_t, uint16_t, int, int);
  277. protected struct mlist *file_apprentice(struct magic_set *, const char *, int);
  278. protected uint64_t file_signextend(struct magic_set *, struct magic *,
  279. uint64_t);
  280. diff --git a/src/funcs.c b/src/funcs.c
  281. index 04bab02..88e77ef 100644
  282. --- a/src/funcs.c
  283. +++ b/src/funcs.c
  284. @@ -228,7 +228,7 @@ file_buffer(struct magic_set *ms, int fd, const char *inname __attribute__ ((unu
  285. /* try soft magic tests */
  286. if ((ms->flags & MAGIC_NO_CHECK_SOFT) == 0)
  287. - if ((m = file_softmagic(ms, ubuf, nb, 0, BINTEST,
  288. + if ((m = file_softmagic(ms, ubuf, nb, 0, 0, BINTEST,
  289. looks_text)) != 0) {
  290. if ((ms->flags & MAGIC_DEBUG) != 0)
  291. (void)fprintf(stderr, "softmagic %d\n", m);
  292. diff --git a/src/magic.c b/src/magic.c
  293. index 2a916c7..aa812df 100644
  294. --- a/src/magic.c
  295. +++ b/src/magic.c
  296. @@ -233,7 +233,10 @@ magic_open(int flags)
  297. ms->mlist = NULL;
  298. ms->file = "unknown";
  299. ms->line = 0;
  300. - ms->max_recursion = FILE_MAX_RECURSION;
  301. + ms->indir_recursion = FILE_INDIR_RECURSION;
  302. + ms->name_recursion = FILE_NAME_RECURSION;
  303. + ms->shnum_max = FILE_ELF_SHNUM;
  304. + ms->phnum_max = FILE_ELF_PHNUM;
  305. return ms;
  306. free:
  307. free(ms);
  308. @@ -517,8 +520,17 @@ public int
  309. magic_setparam(struct magic_set *ms, int param, const void *val)
  310. {
  311. switch (param) {
  312. - case MAGIC_PARAM_MAX_RECURSION:
  313. - ms->max_recursion = *(const size_t *)val;
  314. + case MAGIC_PARAM_INDIR_RECURSION:
  315. + ms->indir_recursion = *(const size_t *)val;
  316. + return 0;
  317. + case MAGIC_PARAM_NAME_RECURSION:
  318. + ms->name_recursion = *(const size_t *)val;
  319. + return 0;
  320. + case MAGIC_PARAM_PHNUM_MAX:
  321. + ms->phnum_max = *(const size_t *)val;
  322. + return 0;
  323. + case MAGIC_PARAM_SHNUM_MAX:
  324. + ms->shnum_max = *(const size_t *)val;
  325. return 0;
  326. default:
  327. errno = EINVAL;
  328. @@ -530,8 +542,17 @@ public int
  329. magic_getparam(struct magic_set *ms, int param, void *val)
  330. {
  331. switch (param) {
  332. - case MAGIC_PARAM_MAX_RECURSION:
  333. - *(size_t *)val = ms->max_recursion;
  334. + case MAGIC_PARAM_INDIR_RECURSION:
  335. + *(size_t *)val = ms->indir_recursion;
  336. + return 0;
  337. + case MAGIC_PARAM_NAME_RECURSION:
  338. + *(size_t *)val = ms->name_recursion;
  339. + return 0;
  340. + case MAGIC_PARAM_PHNUM_MAX:
  341. + *(size_t *)val = ms->phnum_max;
  342. + return 0;
  343. + case MAGIC_PARAM_SHNUM_MAX:
  344. + *(size_t *)val = ms->shnum_max;
  345. return 0;
  346. default:
  347. errno = EINVAL;
  348. diff --git a/src/magic.h b/src/magic.h
  349. index 3d9ee8a..20c05c1 100644
  350. --- a/src/magic.h
  351. +++ b/src/magic.h
  352. @@ -99,7 +99,11 @@ int magic_check(magic_t, const char *);
  353. int magic_list(magic_t, const char *);
  354. int magic_errno(magic_t);
  355. -#define MAGIC_PARAM_MAX_RECURSION 0
  356. +#define MAGIC_PARAM_INDIR_RECURSION 0
  357. +#define MAGIC_PARAM_NAME_RECURSION 1
  358. +#define MAGIC_PARAM_PHNUM_MAX 2
  359. +#define MAGIC_PARAM_SHNUM_MAX 3
  360. +
  361. int magic_setparam(magic_t, int, const void *);
  362. int magic_getparam(magic_t, int, void *);
  363. diff --git a/src/softmagic.c b/src/softmagic.c
  364. index 188e66b..d507e11 100644
  365. --- a/src/softmagic.c
  366. +++ b/src/softmagic.c
  367. @@ -43,9 +43,9 @@ FILE_RCSID("@(#)$File: softmagic.c,v 1.147 2011/11/05 15:44:22 rrt Exp $")
  368. private int match(struct magic_set *, struct magic *, uint32_t,
  369. - const unsigned char *, size_t, int, int, size_t);
  370. + const unsigned char *, size_t, int, int, uint16_t, uint16_t);
  371. private int mget(struct magic_set *, const unsigned char *,
  372. - struct magic *, size_t, unsigned int, int, size_t);
  373. + struct magic *, size_t, unsigned int, int, uint16_t, uint16_t);
  374. private int magiccheck(struct magic_set *, struct magic *);
  375. private int32_t mprint(struct magic_set *, struct magic *);
  376. private int32_t moffset(struct magic_set *, struct magic *);
  377. @@ -69,13 +69,13 @@ private void cvt_64(union VALUETYPE *, const struct magic *);
  378. /*ARGSUSED1*/ /* nbytes passed for regularity, maybe need later */
  379. protected int
  380. file_softmagic(struct magic_set *ms, const unsigned char *buf, size_t nbytes,
  381. - size_t level, int mode, int text)
  382. + uint16_t indir_level, uint16_t name_level, int mode, int text)
  383. {
  384. struct mlist *ml;
  385. int rv;
  386. for (ml = ms->mlist->next; ml != ms->mlist; ml = ml->next)
  387. if ((rv = match(ms, ml->magic, ml->nmagic, buf, nbytes, mode,
  388. - text, level)) != 0)
  389. + text, indir_level, name_level)) != 0)
  390. return rv;
  391. return 0;
  392. @@ -111,7 +111,7 @@ file_softmagic(struct magic_set *ms, const unsigned char *buf, size_t nbytes,
  393. private int
  394. match(struct magic_set *ms, struct magic *magic, uint32_t nmagic,
  395. const unsigned char *s, size_t nbytes, int mode, int text,
  396. - size_t recursion_level)
  397. + uint16_t indir_level, uint16_t name_level)
  398. {
  399. uint32_t magindex = 0;
  400. unsigned int cont_level = 0;
  401. @@ -143,7 +143,7 @@ match(struct magic_set *ms, struct magic *magic, uint32_t nmagic,
  402. ms->line = m->lineno;
  403. /* if main entry matches, print it... */
  404. - switch (mget(ms, s, m, nbytes, cont_level, text, recursion_level + 1)) {
  405. + switch (mget(ms, s, m, nbytes, cont_level, text, indir_level, name_level)) {
  406. case -1:
  407. return -1;
  408. case 0:
  409. @@ -226,7 +226,7 @@ match(struct magic_set *ms, struct magic *magic, uint32_t nmagic,
  410. continue;
  411. }
  412. #endif
  413. - switch (mget(ms, s, m, nbytes, cont_level, text, recursion_level + 1)) {
  414. + switch (mget(ms, s, m, nbytes, cont_level, text, indir_level, name_level)) {
  415. case -1:
  416. return -1;
  417. case 0:
  418. @@ -1042,7 +1042,7 @@ mcopy(struct magic_set *ms, union VALUETYPE *p, int type, int indir,
  419. private int
  420. mget(struct magic_set *ms, const unsigned char *s,
  421. struct magic *m, size_t nbytes, unsigned int cont_level, int text,
  422. - size_t recursion_level)
  423. + uint16_t indir_level, uint16_t name_level)
  424. {
  425. uint32_t offset = ms->offset;
  426. file_pushbuf_t *pb;
  427. @@ -1050,9 +1050,15 @@ mget(struct magic_set *ms, const unsigned char *s,
  428. char *rbuf;
  429. union VALUETYPE *p = &ms->ms_value;
  430. - if (recursion_level >= ms->max_recursion) {
  431. - file_error(ms, 0, "recursion nesting (%zu) exceeded",
  432. - recursion_level);
  433. + if (indir_level >= ms->indir_recursion) {
  434. + file_error(ms, 0, "indirect recursion nesting (%hu) exceeded",
  435. + indir_level);
  436. + return -1;
  437. + }
  438. +
  439. + if (name_level >= ms->name_recursion) {
  440. + file_error(ms, 0, "name recursion nesting (%hu) exceeded",
  441. + name_level);
  442. return -1;
  443. }
  444. @@ -1619,7 +1625,7 @@ mget(struct magic_set *ms, const unsigned char *s,
  445. return -1;
  446. rv = file_softmagic(ms, s + offset, nbytes - offset,
  447. - recursion_level, BINTEST, text);
  448. + indir_level + 1, name_level, BINTEST, text);
  449. if ((ms->flags & MAGIC_DEBUG) != 0)
  450. fprintf(stderr, "indirect @offs=%u[%d]\n", offset, rv);