1 commit 20c59ad54afc7427ea680f84c8ee5a576ba54b08
2 Author: Christos Zoulas <christos@zoulas.com>
3 Date: Mon Apr 18 15:10:34 2016 +0000
5 Downgrade DER comparison and offset lookup failures to be handled as match
8 diff --git a/src/softmagic.c b/src/softmagic.c
9 index 14a8bc5..5b5f0f9 100644
12 @@ -186,11 +186,11 @@ match(struct magic_set *ms, struct magic *magic, uint32_t nmagic,
13 ((text && (m->str_flags & FLT) == STRING_BINTEST) ||
14 (!text && (m->str_flags & FLT) == STRING_TEXTTEST))) ||
15 (m->flag & mode) != mode) {
18 - while (magindex + 1 < nmagic &&
19 - magic[magindex + 1].cont_level != 0 &&
22 + while (magindex < nmagic - 1 &&
23 + magic[magindex + 1].cont_level != 0)
25 continue; /* Skip to next top-level test*/
28 @@ -227,10 +227,7 @@ match(struct magic_set *ms, struct magic *magic, uint32_t nmagic,
29 * main entry didn't match,
30 * flush its continuations
32 - while (magindex < nmagic - 1 &&
33 - magic[magindex + 1].cont_level != 0)
39 if ((e = handle_annotation(ms, m)) != 0) {
40 @@ -255,8 +252,14 @@ match(struct magic_set *ms, struct magic *magic, uint32_t nmagic,
41 if (print && mprint(ms, m) == -1)
44 - if (moffset(ms, m, nbytes, &ms->c.li[cont_level].off) == -1)
45 + switch (moffset(ms, m, nbytes, &ms->c.li[cont_level].off)) {
54 /* and any continuations that match */
55 if (file_check_mem(ms, ++cont_level) == -1)
56 @@ -362,9 +365,16 @@ match(struct magic_set *ms, struct magic *magic, uint32_t nmagic,
57 if (print && mprint(ms, m) == -1)
60 - if (moffset(ms, m, nbytes,
61 - &ms->c.li[cont_level].off) == -1)
62 + switch (moffset(ms, m, nbytes,
63 + &ms->c.li[cont_level].off)) {
75 @@ -813,9 +823,13 @@ moffset(struct magic_set *ms, struct magic *m, size_t nbytes, int32_t *op)
78 o = der_offs(ms, m, nbytes);
80 - file_error(ms, 0, "EOF computing DER offset");
82 + if (o == -1 || (size_t)o > nbytes) {
83 + if ((ms->flags & MAGIC_DEBUG) != 0) {
84 + (void)fprintf(stderr,
85 + "Bad DER offset %d nbytes=%zu",
92 @@ -825,12 +839,13 @@ moffset(struct magic_set *ms, struct magic *m, size_t nbytes, int32_t *op)
96 - if ((size_t)o >= nbytes) {
97 - file_error(ms, 0, "Offset out of range");
98 + if ((size_t)o > nbytes) {
99 + file_error(ms, 0, "Offset out of range %zu > %zu",
100 + (size_t)o, nbytes);
109 @@ -2107,8 +2122,13 @@ magiccheck(struct magic_set *ms, struct magic *m)
112 matched = der_cmp(ms, m);
114 - file_error(ms, 0, "EOF comparing DER entries");
115 + if (matched == -1) {
116 + if ((ms->flags & MAGIC_DEBUG) != 0) {
117 + (void) fprintf(stderr,
118 + "EOF comparing DER entries");
124 file_magerror(ms, "invalid type %d in magiccheck()", m->type);