Skip to content

Commit

Permalink
Kill -R and replace with -P param=value. Allow setting of 4 parameters:
Browse files Browse the repository at this point in the history
    indir, name, shnum, phnum.
  • Loading branch information
zoulasc committed Nov 27, 2014
1 parent 5063ca3 commit 6ce24f3
Show file tree
Hide file tree
Showing 12 changed files with 169 additions and 67 deletions.
5 changes: 5 additions & 0 deletions ChangeLog
@@ -1,3 +1,8 @@
2014-11-27 18:40 Christos Zoulas <christos@zoulas.com>

* Allow setting more parameters from the command line.
* Split name/use and indirect magic recursion limits.

2014-11-27 11:12 Christos Zoulas <christos@zoulas.com>

* Adjust ELF parameters and the default recursion
Expand Down
18 changes: 11 additions & 7 deletions doc/file.man
@@ -1,4 +1,4 @@
.\" $File: file.man,v 1.106 2014/03/07 23:11:51 christos Exp $
.\" $File: file.man,v 1.107 2014/11/27 16:15:06 christos Exp $
.Dd November 27, 2014
.Dt FILE __CSECTION__
.Os
Expand All @@ -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
Expand Down Expand Up @@ -304,16 +304,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
Expand Down
29 changes: 23 additions & 6 deletions doc/libmagic.man
@@ -1,4 +1,4 @@
.\" $File: libmagic.man,v 1.29 2014/08/04 06:19:44 christos Exp $
.\" $File: libmagic.man,v 1.30 2014/11/27 16:15:06 christos Exp $
.\"
.\" Copyright (c) Christos Zoulas 2003.
.\" All Rights Reserved.
Expand Down Expand Up @@ -280,14 +280,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.
.Pp
The
.Fn magic_version
Expand Down
7 changes: 5 additions & 2 deletions src/apprentice.c
Expand Up @@ -32,7 +32,7 @@
#include "file.h"

#ifndef lint
FILE_RCSID("@(#)$File: apprentice.c,v 1.223 2014/11/12 15:28:34 christos Exp $")
FILE_RCSID("@(#)$File: apprentice.c,v 1.224 2014/11/27 15:40:36 christos Exp $")
#endif /* lint */

#include "magic.h"
Expand Down Expand Up @@ -524,7 +524,10 @@ file_ms_alloc(int flags)
ms->mlist[i] = 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);
Expand Down
4 changes: 2 additions & 2 deletions src/ascmagic.c
Expand Up @@ -35,7 +35,7 @@
#include "file.h"

#ifndef lint
FILE_RCSID("@(#)$File: ascmagic.c,v 1.87 2013/09/17 15:51:22 christos Exp $")
FILE_RCSID("@(#)$File: ascmagic.c,v 1.88 2014/02/12 23:20:53 christos Exp $")
#endif /* lint */

#include "magic.h"
Expand Down Expand Up @@ -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;
}

Expand Down
8 changes: 4 additions & 4 deletions src/elfclass.h
Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand Down
69 changes: 54 additions & 15 deletions src/file.c
Expand Up @@ -32,7 +32,7 @@
#include "file.h"

#ifndef lint
FILE_RCSID("@(#)$File: file.c,v 1.155 2014/10/11 15:03:16 christos Exp $")
FILE_RCSID("@(#)$File: file.c,v 1.156 2014/11/27 15:40:36 christos Exp $")
#endif /* lint */

#include "magic.h"
Expand Down Expand Up @@ -98,7 +98,7 @@ private const struct option long_options[] = {
#undef OPT_LONGONLY
{0, 0, NULL, 0}
};
#define OPTSTRING "bcCde:Ef:F:hiklLm:nNprR:svz0"
#define OPTSTRING "bcCde:Ef:F:hiklLm:nNpP:rsvz0"

private const struct {
const char *name;
Expand All @@ -116,6 +116,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);
Expand All @@ -125,6 +136,8 @@ private void help(void);
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);


/*
Expand All @@ -137,7 +150,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 */
Expand Down Expand Up @@ -243,11 +255,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;
Expand Down Expand Up @@ -326,16 +339,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) {
Expand Down Expand Up @@ -365,6 +369,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*/
Expand Down
14 changes: 10 additions & 4 deletions src/file.h
Expand Up @@ -27,7 +27,7 @@
*/
/*
* file.h - definitions for file(1) program
* @(#)$File: file.h,v 1.156 2014/11/23 13:54:27 christos Exp $
* @(#)$File: file.h,v 1.157 2014/11/27 15:40:36 christos Exp $
*/

#ifndef __file_h__
Expand Down Expand Up @@ -401,8 +401,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 */
Expand Down Expand Up @@ -442,7 +448,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 int file_apprentice(struct magic_set *, const char *, int);
protected int buffer_apprentice(struct magic_set *, struct magic **,
size_t *, size_t);
Expand Down
4 changes: 2 additions & 2 deletions src/funcs.c
Expand Up @@ -27,7 +27,7 @@
#include "file.h"

#ifndef lint
FILE_RCSID("@(#)$File: funcs.c,v 1.73 2014/09/10 18:41:51 christos Exp $")
FILE_RCSID("@(#)$File: funcs.c,v 1.74 2014/11/23 13:54:27 christos Exp $")
#endif /* lint */

#include "magic.h"
Expand Down Expand Up @@ -227,7 +227,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);
Expand Down
28 changes: 23 additions & 5 deletions src/magic.c
Expand Up @@ -33,7 +33,7 @@
#include "file.h"

#ifndef lint
FILE_RCSID("@(#)$File: magic.c,v 1.85 2014/08/04 06:19:44 christos Exp $")
FILE_RCSID("@(#)$File: magic.c,v 1.86 2014/11/27 15:40:36 christos Exp $")
#endif /* lint */

#include "magic.h"
Expand Down Expand Up @@ -541,8 +541,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;
Expand All @@ -554,8 +563,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;
Expand Down

0 comments on commit 6ce24f3

Please sign in to comment.