123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474 |
- Subject: Kill -R and replace with -P param=value. Allow setting of 4 parameters: indir, name, shnum, phnum
- Upstream-Author: Christos Zoulas <christos@zoulas.com>
- Date: Thu Nov 27 23:42:58 2014 +0000
- Origin: FILE5_20-36-g6ce24f3
- Last-Update: 2015-01-05
- Kill -R and replace with -P param=value. Allow setting of 4 parameters:
- indir, name, shnum, phnum.
- (prequisite for TEMP-0000000-B67840)
- diff --git a/doc/file.man b/doc/file.man
- index f6c91e7..e4ce958 100644
- --- a/doc/file.man
- +++ b/doc/file.man
- @@ -16,7 +16,7 @@
- .Op Fl F Ar separator
- .Op Fl f Ar namefile
- .Op Fl m Ar magicfiles
- -.Op Fl R Ar maxrecursion
- +.Op Fl P Ar name=value
- .Ar
- .Ek
- .Nm
- @@ -291,16 +291,20 @@ or
- attempt to preserve the access time of files analyzed, to pretend that
- .Nm
- never read them.
- +.It Fl P , Fl Fl parameter Ar name=value
- +Set various parameter limits.
- +.Bl -column "indir" "Default" "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" -offset indent
- +.It Sy "Name" Ta Sy "Default" Ta Sy "Explanation"
- +.It Li indir Ta 15 Ta recursion limit for indirect magic
- +.It Li name Ta 40 Ta recursion limit for name/use magic
- +.It Li phnum Ta 128 Ta max ELF program sections processed
- +.It Li shnum Ta 32768 Ta max ELF sections processed
- +.El
- .It Fl r , Fl Fl raw
- Don't translate unprintable characters to \eooo.
- Normally
- .Nm
- translates unprintable characters to their octal representation.
- -.It Fl R , Fl Fl recursion Ar maxlevel
- -Set the maximum recursion level for indirect type magic or name/use entry
- -invocations.
- -The default is
- -.Dv 15 .
- .It Fl s , Fl Fl special-files
- Normally,
- .Nm
- diff --git a/doc/libmagic.man b/doc/libmagic.man
- index 0a2ed9a..ba06c9f 100644
- --- a/doc/libmagic.man
- +++ b/doc/libmagic.man
- @@ -258,14 +258,31 @@ and
- .Fn magic_setparam
- allow getting and setting various limits related to the the magic
- library.
- -.Bl -column "MAGIC_PARAM_MAX_RECURSION" "size_t" "Default" -offset indent
- -.It Sy "Parameter" Ta Sy "Type" Ta Sy "Default
- -.It Li MAGIC_PARAM_MAX_RECURSION Ta size_t Ta 15
- +.Bl -column "MAGIC_PARAM_INDIR_RECURSION" "size_t" "Default" -offset indent
- +.It Sy "Parameter" Ta Sy "Type" Ta Sy "Default"
- +.It Li MAGIC_PARAM_INDIR_RECURSION Ta size_t Ta 15
- +.It Li MAGIC_PARAM_NAME_RECURSION Ta size_t Ta 40
- +.It Li MAGIC_PARAM_PHNUM_MAX Ta size_t Ta 128
- +.It Li MAGIC_PARAM_SHNUM_MAX Ta size_t Ta 32768
- .El
- +.Pp
- +The
- +.Dv MAGIC_PARAM_INDIR_RECURSION
- +parameter controls how many levels of recursion will be followed for
- +indirect magic entries.
- +.Pp
- The
- -.Dv MAGIC_PARAM_MAX_RECURSION
- +.Dv MAGIC_PARAM_NAME_RECURSION
- parameter controls how many levels of recursion will be followed for
- -indirect magic entries or for name/use calls.
- +for name/use calls.
- +.Pp
- +The
- +.Dv MAGIC_PARAM_PHNUM_MAX
- +parameter controls how many elf program sections will be processed.
- +.Pp
- +The
- +.Dv MAGIC_PARAM_SHNUM_MAX
- +parameter controls how many elf sections will be processed.
- .Sh RETURN VALUES
- The function
- .Fn magic_open
- diff --git a/src/ascmagic.c b/src/ascmagic.c
- index 1c37e8e..9f4b012 100644
- --- a/src/ascmagic.c
- +++ b/src/ascmagic.c
- @@ -147,7 +147,7 @@ file_ascmagic_with_encoding(struct magic_set *ms, const unsigned char *buf,
- == NULL)
- goto done;
- if ((rv = file_softmagic(ms, utf8_buf,
- - (size_t)(utf8_end - utf8_buf), 0, TEXTTEST, text)) == 0)
- + (size_t)(utf8_end - utf8_buf), 0, 0, TEXTTEST, text)) == 0)
- rv = -1;
- }
-
- diff --git a/src/elfclass.h b/src/elfclass.h
- index 0826ce3..bda53b3 100644
- --- a/src/elfclass.h
- +++ b/src/elfclass.h
- @@ -36,7 +36,7 @@
- #ifdef ELFCORE
- case ET_CORE:
- phnum = elf_getu16(swap, elfhdr.e_phnum);
- - if (phnum > MAX_PHNUM)
- + if (phnum > ms->phnum_max)
- return toomany(ms, "program", phnum);
- flags |= FLAGS_IS_CORE;
- if (dophn_core(ms, clazz, swap, fd,
- @@ -49,10 +49,10 @@
- case ET_EXEC:
- case ET_DYN:
- phnum = elf_getu16(swap, elfhdr.e_phnum);
- - if (phnum > MAX_PHNUM)
- + if (phnum > ms->phnum_max)
- return toomany(ms, "program", phnum);
- shnum = elf_getu16(swap, elfhdr.e_shnum);
- - if (shnum > MAX_SHNUM)
- + if (shnum > ms->shnum_max)
- return toomany(ms, "section", shnum);
- if (dophn_exec(ms, clazz, swap, fd,
- (off_t)elf_getu(swap, elfhdr.e_phoff), phnum,
- @@ -62,7 +62,7 @@
- /*FALLTHROUGH*/
- case ET_REL:
- shnum = elf_getu16(swap, elfhdr.e_shnum);
- - if (shnum > MAX_SHNUM)
- + if (shnum > ms->shnum_max)
- return toomany(ms, "section", shnum);
- if (doshn(ms, clazz, swap, fd,
- (off_t)elf_getu(swap, elfhdr.e_shoff), shnum,
- diff --git a/src/file.c b/src/file.c
- index 2f3cf9d..4bb3102 100644
- --- a/src/file.c
- +++ b/src/file.c
- @@ -101,7 +101,7 @@ private const struct option long_options[] = {
- #undef OPT_LONGONLY
- {0, 0, NULL, 0}
- };
- -#define OPTSTRING "bcCde:f:F:hiklLm:nNprR:svz0"
- +#define OPTSTRING "bcCde:f:F:hiklLm:nNprP:rsvz0"
-
- private const struct {
- const char *name;
- @@ -119,6 +119,17 @@ private const struct {
- { "tokens", MAGIC_NO_CHECK_TOKENS }, /* OBSOLETE: ignored for backwards compatibility */
- };
-
- +private struct {
- + const char *name;
- + int tag;
- + size_t value;
- +} pm[] = {
- + { "indir", MAGIC_PARAM_INDIR_RECURSION, 0 },
- + { "name", MAGIC_PARAM_NAME_RECURSION, 0 },
- + { "phnum", MAGIC_PARAM_PHNUM_MAX, 0 },
- + { "shnum", MAGIC_PARAM_SHNUM_MAX, 0 },
- +};
- +
- private char *progname; /* used throughout */
-
- private void usage(void);
- @@ -128,6 +139,8 @@ int main(int, char *[]);
- private int unwrap(struct magic_set *, const char *);
- private int process(struct magic_set *ms, const char *, int);
- private struct magic_set *load(const char *, int);
- +private void setparam(const char *);
- +private void applyparam(magic_t);
-
-
- /*
- @@ -140,7 +153,6 @@ main(int argc, char *argv[])
- size_t i;
- int action = 0, didsomefiles = 0, errflg = 0;
- int flags = 0, e = 0;
- - size_t max_recursion = 0;
- struct magic_set *magic = NULL;
- int longindex;
- const char *magicfile = NULL; /* where the magic is */
- @@ -241,11 +253,12 @@ main(int argc, char *argv[])
- flags |= MAGIC_PRESERVE_ATIME;
- break;
- #endif
- + case 'P':
- + setparam(optarg);
- + break;
- case 'r':
- flags |= MAGIC_RAW;
- break;
- - case 'R':
- - max_recursion = atoi(optarg);
- break;
- case 's':
- flags |= MAGIC_DEVICES;
- @@ -319,16 +332,7 @@ main(int argc, char *argv[])
- if (magic == NULL)
- if ((magic = load(magicfile, flags)) == NULL)
- return 1;
- - if (max_recursion) {
- - if (magic_setparam(magic, MAGIC_PARAM_MAX_RECURSION,
- - &max_recursion) == -1) {
- - (void)fprintf(stderr,
- - "%s: Can't set recurision %s\n", progname,
- - strerror(errno));
- - return 1;
- - }
- - }
- - break;
- + applyparam(magic);
- }
-
- if (optind == argc) {
- @@ -358,6 +362,41 @@ main(int argc, char *argv[])
- return e;
- }
-
- +private void
- +applyparam(magic_t magic)
- +{
- + size_t i;
- +
- + for (i = 0; i < __arraycount(pm); i++) {
- + if (pm[i].value == 0)
- + continue;
- + if (magic_setparam(magic, pm[i].tag, &pm[i].value) == -1) {
- + (void)fprintf(stderr, "%s: Can't set %s %s\n", progname,
- + pm[i].name, strerror(errno));
- + exit(1);
- + }
- + }
- +}
- +
- +private void
- +setparam(const char *p)
- +{
- + size_t i;
- + char *s;
- +
- + if ((s = strchr(p, '=')) == NULL)
- + goto badparm;
- +
- + for (i = 0; i < __arraycount(pm); i++) {
- + if (strncmp(p, pm[i].name, s - p) != 0)
- + continue;
- + pm[i].value = atoi(s + 1);
- + return;
- + }
- +badparm:
- + (void)fprintf(stderr, "%s: Unknown param %s\n", progname, p);
- + exit(1);
- +}
-
- private struct magic_set *
- /*ARGSUSED*/
- diff --git a/src/file.h b/src/file.h
- index 71a3aeb..237bc4d 100644
- --- a/src/file.h
- +++ b/src/file.h
- @@ -380,8 +380,14 @@ struct magic_set {
- /* FIXME: Make the string dynamically allocated so that e.g.
- strings matched in files can be longer than MAXstring */
- union VALUETYPE ms_value; /* either number or string */
- - size_t max_recursion;
- -#define FILE_MAX_RECURSION 15
- + uint16_t indir_recursion;
- + uint16_t name_recursion;
- + uint16_t shnum_max;
- + uint16_t phnum_max;
- +#define FILE_INDIR_RECURSION 15
- +#define FILE_NAME_RECURSION 40
- +#define FILE_ELF_SHNUM 32768
- +#define FILE_ELF_PHNUM 128
- };
-
- /* Type for Unicode characters */
- @@ -416,7 +422,7 @@ protected int file_encoding(struct magic_set *, const unsigned char *, size_t,
- unichar **, size_t *, const char **, const char **, const char **);
- protected int file_is_tar(struct magic_set *, const unsigned char *, size_t);
- protected int file_softmagic(struct magic_set *, const unsigned char *, size_t,
- - size_t, int, int);
- + uint16_t, uint16_t, int, int);
- protected struct mlist *file_apprentice(struct magic_set *, const char *, int);
- protected uint64_t file_signextend(struct magic_set *, struct magic *,
- uint64_t);
- diff --git a/src/funcs.c b/src/funcs.c
- index 04bab02..88e77ef 100644
- --- a/src/funcs.c
- +++ b/src/funcs.c
- @@ -228,7 +228,7 @@ file_buffer(struct magic_set *ms, int fd, const char *inname __attribute__ ((unu
-
- /* try soft magic tests */
- if ((ms->flags & MAGIC_NO_CHECK_SOFT) == 0)
- - if ((m = file_softmagic(ms, ubuf, nb, 0, BINTEST,
- + if ((m = file_softmagic(ms, ubuf, nb, 0, 0, BINTEST,
- looks_text)) != 0) {
- if ((ms->flags & MAGIC_DEBUG) != 0)
- (void)fprintf(stderr, "softmagic %d\n", m);
- diff --git a/src/magic.c b/src/magic.c
- index 2a916c7..aa812df 100644
- --- a/src/magic.c
- +++ b/src/magic.c
- @@ -233,7 +233,10 @@ magic_open(int flags)
- ms->mlist = NULL;
- ms->file = "unknown";
- ms->line = 0;
- - ms->max_recursion = FILE_MAX_RECURSION;
- + ms->indir_recursion = FILE_INDIR_RECURSION;
- + ms->name_recursion = FILE_NAME_RECURSION;
- + ms->shnum_max = FILE_ELF_SHNUM;
- + ms->phnum_max = FILE_ELF_PHNUM;
- return ms;
- free:
- free(ms);
- @@ -517,8 +520,17 @@ public int
- magic_setparam(struct magic_set *ms, int param, const void *val)
- {
- switch (param) {
- - case MAGIC_PARAM_MAX_RECURSION:
- - ms->max_recursion = *(const size_t *)val;
- + case MAGIC_PARAM_INDIR_RECURSION:
- + ms->indir_recursion = *(const size_t *)val;
- + return 0;
- + case MAGIC_PARAM_NAME_RECURSION:
- + ms->name_recursion = *(const size_t *)val;
- + return 0;
- + case MAGIC_PARAM_PHNUM_MAX:
- + ms->phnum_max = *(const size_t *)val;
- + return 0;
- + case MAGIC_PARAM_SHNUM_MAX:
- + ms->shnum_max = *(const size_t *)val;
- return 0;
- default:
- errno = EINVAL;
- @@ -530,8 +542,17 @@ public int
- magic_getparam(struct magic_set *ms, int param, void *val)
- {
- switch (param) {
- - case MAGIC_PARAM_MAX_RECURSION:
- - *(size_t *)val = ms->max_recursion;
- + case MAGIC_PARAM_INDIR_RECURSION:
- + *(size_t *)val = ms->indir_recursion;
- + return 0;
- + case MAGIC_PARAM_NAME_RECURSION:
- + *(size_t *)val = ms->name_recursion;
- + return 0;
- + case MAGIC_PARAM_PHNUM_MAX:
- + *(size_t *)val = ms->phnum_max;
- + return 0;
- + case MAGIC_PARAM_SHNUM_MAX:
- + *(size_t *)val = ms->shnum_max;
- return 0;
- default:
- errno = EINVAL;
- diff --git a/src/magic.h b/src/magic.h
- index 3d9ee8a..20c05c1 100644
- --- a/src/magic.h
- +++ b/src/magic.h
- @@ -99,7 +99,11 @@ int magic_check(magic_t, const char *);
- int magic_list(magic_t, const char *);
- int magic_errno(magic_t);
-
- -#define MAGIC_PARAM_MAX_RECURSION 0
- +#define MAGIC_PARAM_INDIR_RECURSION 0
- +#define MAGIC_PARAM_NAME_RECURSION 1
- +#define MAGIC_PARAM_PHNUM_MAX 2
- +#define MAGIC_PARAM_SHNUM_MAX 3
- +
- int magic_setparam(magic_t, int, const void *);
- int magic_getparam(magic_t, int, void *);
-
- diff --git a/src/softmagic.c b/src/softmagic.c
- index 188e66b..d507e11 100644
- --- a/src/softmagic.c
- +++ b/src/softmagic.c
- @@ -43,9 +43,9 @@ FILE_RCSID("@(#)$File: softmagic.c,v 1.147 2011/11/05 15:44:22 rrt Exp $")
-
-
- private int match(struct magic_set *, struct magic *, uint32_t,
- - const unsigned char *, size_t, int, int, size_t);
- + const unsigned char *, size_t, int, int, uint16_t, uint16_t);
- private int mget(struct magic_set *, const unsigned char *,
- - struct magic *, size_t, unsigned int, int, size_t);
- + struct magic *, size_t, unsigned int, int, uint16_t, uint16_t);
- private int magiccheck(struct magic_set *, struct magic *);
- private int32_t mprint(struct magic_set *, struct magic *);
- private int32_t moffset(struct magic_set *, struct magic *);
- @@ -69,13 +69,13 @@ private void cvt_64(union VALUETYPE *, const struct magic *);
- /*ARGSUSED1*/ /* nbytes passed for regularity, maybe need later */
- protected int
- file_softmagic(struct magic_set *ms, const unsigned char *buf, size_t nbytes,
- - size_t level, int mode, int text)
- + uint16_t indir_level, uint16_t name_level, int mode, int text)
- {
- struct mlist *ml;
- int rv;
- for (ml = ms->mlist->next; ml != ms->mlist; ml = ml->next)
- if ((rv = match(ms, ml->magic, ml->nmagic, buf, nbytes, mode,
- - text, level)) != 0)
- + text, indir_level, name_level)) != 0)
- return rv;
-
- return 0;
- @@ -111,7 +111,7 @@ file_softmagic(struct magic_set *ms, const unsigned char *buf, size_t nbytes,
- private int
- match(struct magic_set *ms, struct magic *magic, uint32_t nmagic,
- const unsigned char *s, size_t nbytes, int mode, int text,
- - size_t recursion_level)
- + uint16_t indir_level, uint16_t name_level)
- {
- uint32_t magindex = 0;
- unsigned int cont_level = 0;
- @@ -143,7 +143,7 @@ match(struct magic_set *ms, struct magic *magic, uint32_t nmagic,
- ms->line = m->lineno;
-
- /* if main entry matches, print it... */
- - switch (mget(ms, s, m, nbytes, cont_level, text, recursion_level + 1)) {
- + switch (mget(ms, s, m, nbytes, cont_level, text, indir_level, name_level)) {
- case -1:
- return -1;
- case 0:
- @@ -226,7 +226,7 @@ match(struct magic_set *ms, struct magic *magic, uint32_t nmagic,
- continue;
- }
- #endif
- - switch (mget(ms, s, m, nbytes, cont_level, text, recursion_level + 1)) {
- + switch (mget(ms, s, m, nbytes, cont_level, text, indir_level, name_level)) {
- case -1:
- return -1;
- case 0:
- @@ -1042,7 +1042,7 @@ mcopy(struct magic_set *ms, union VALUETYPE *p, int type, int indir,
- private int
- mget(struct magic_set *ms, const unsigned char *s,
- struct magic *m, size_t nbytes, unsigned int cont_level, int text,
- - size_t recursion_level)
- + uint16_t indir_level, uint16_t name_level)
- {
- uint32_t offset = ms->offset;
- file_pushbuf_t *pb;
- @@ -1050,9 +1050,15 @@ mget(struct magic_set *ms, const unsigned char *s,
- char *rbuf;
- union VALUETYPE *p = &ms->ms_value;
-
- - if (recursion_level >= ms->max_recursion) {
- - file_error(ms, 0, "recursion nesting (%zu) exceeded",
- - recursion_level);
- + if (indir_level >= ms->indir_recursion) {
- + file_error(ms, 0, "indirect recursion nesting (%hu) exceeded",
- + indir_level);
- + return -1;
- + }
- +
- + if (name_level >= ms->name_recursion) {
- + file_error(ms, 0, "name recursion nesting (%hu) exceeded",
- + name_level);
- return -1;
- }
-
- @@ -1619,7 +1625,7 @@ mget(struct magic_set *ms, const unsigned char *s,
- return -1;
-
- rv = file_softmagic(ms, s + offset, nbytes - offset,
- - recursion_level, BINTEST, text);
- + indir_level + 1, name_level, BINTEST, text);
-
- if ((ms->flags & MAGIC_DEBUG) != 0)
- fprintf(stderr, "indirect @offs=%u[%d]\n", offset, rv);
|