Skip to content

Commit

Permalink
- Add a limit to the number of ELF notes processed (Suggested by Alex…
Browse files Browse the repository at this point in the history
…ander

  Cherepanov)
- Restructure ELF note printing so that we don't print the same message
  multiple times on repeated notes of the same kind.
  • Loading branch information
zoulasc committed Dec 16, 2014
1 parent 0ece9b2 commit ce90e05
Show file tree
Hide file tree
Showing 11 changed files with 292 additions and 198 deletions.
5 changes: 5 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
2014-12-16 18:10 Christos Zoulas <christos@zoulas.com>

* restructure elf note printing to avoid repeated messages
* add note limit, suggested by Alexander Cherepanov

2014-12-16 16:53 Christos Zoulas <christos@zoulas.com>

* Bail out on partial pread()'s (Alexander Cherepanov)
Expand Down
5 changes: 3 additions & 2 deletions doc/file.man
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
.\" $File: file.man,v 1.109 2014/11/28 02:35:05 christos Exp $
.Dd November 27, 2014
.\" $File: file.man,v 1.110 2014/11/28 02:46:39 christos Exp $
.Dd December 16, 2014
.Dt FILE __CSECTION__
.Os
.Sh NAME
Expand Down Expand Up @@ -310,6 +310,7 @@ Set various parameter limits.
.It Sy "Name" Ta Sy "Default" Ta Sy "Explanation"
.It Li indir Ta 15 Ta recursion limit for indirect magic
.It Li name Ta 30 Ta use count limit for name/use magic
.It Li elf_notes Ta 256 Ta max ELF notes processed
.It Li elf_phnum Ta 128 Ta max ELF program sections processed
.It Li elf_shnum Ta 32768 Ta max ELF sections processed
.El
Expand Down
13 changes: 9 additions & 4 deletions doc/libmagic.man
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
.\" $File: libmagic.man,v 1.32 2014/11/28 02:35:05 christos Exp $
.\" $File: libmagic.man,v 1.33 2014/11/28 02:46:39 christos Exp $
.\"
.\" Copyright (c) Christos Zoulas 2003.
.\" All Rights Reserved.
Expand All @@ -25,7 +25,7 @@
.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
.\" SUCH DAMAGE.
.\"
.Dd November 27, 2014
.Dd December 16, 2014
.Dt LIBMAGIC 3
.Os
.Sh NAME
Expand Down Expand Up @@ -284,6 +284,7 @@ library.
.It Sy "Parameter" Ta Sy "Type" Ta Sy "Default"
.It Li MAGIC_PARAM_INDIR_MAX Ta size_t Ta 15
.It Li MAGIC_PARAM_NAME_MAX Ta size_t Ta 30
.It Li MAGIC_PARAM_ELF_NOTES_MAX Ta size_t Ta 256
.It Li MAGIC_PARAM_ELF_PHNUM_MAX Ta size_t Ta 128
.It Li MAGIC_PARAM_ELF_SHNUM_MAX Ta size_t Ta 32768
.El
Expand All @@ -303,12 +304,16 @@ The
parameter controls the maximum number of calls for name/use.
.Pp
The
.Dv MAGIC_PARAM_NOTES_MAX
parameter controls how many ELF notes will be processed.
.Pp
The
.Dv MAGIC_PARAM_PHNUM_MAX
parameter controls how many elf program sections will be processed.
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.
parameter controls how many ELF sections will be processed.
.Pp
The
.Fn magic_version
Expand Down
3 changes: 2 additions & 1 deletion src/apprentice.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
#include "file.h"

#ifndef lint
FILE_RCSID("@(#)$File: apprentice.c,v 1.226 2014/11/28 02:35:05 christos Exp $")
FILE_RCSID("@(#)$File: apprentice.c,v 1.227 2014/11/28 02:46:39 christos Exp $")
#endif /* lint */

#include "magic.h"
Expand Down Expand Up @@ -528,6 +528,7 @@ file_ms_alloc(int flags)
ms->name_max = FILE_NAME_MAX;
ms->elf_shnum_max = FILE_ELF_SHNUM_MAX;
ms->elf_phnum_max = FILE_ELF_PHNUM_MAX;
ms->elf_notes_max = FILE_ELF_NOTES_MAX;
return ms;
free:
free(ms);
Expand Down
16 changes: 10 additions & 6 deletions src/elfclass.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,17 +32,18 @@
swap = (u.c[sizeof(int32_t) - 1] + 1) != elfhdr.e_ident[EI_DATA];

type = elf_getu16(swap, elfhdr.e_type);
notecount = ms->elf_notes_max;
switch (type) {
#ifdef ELFCORE
case ET_CORE:
phnum = elf_getu16(swap, elfhdr.e_phnum);
if (phnum > ms->elf_phnum_max)
return toomany(ms, "program", phnum);
return toomany(ms, "program headers", phnum);
flags |= FLAGS_IS_CORE;
if (dophn_core(ms, clazz, swap, fd,
(off_t)elf_getu(swap, elfhdr.e_phoff), phnum,
(size_t)elf_getu16(swap, elfhdr.e_phentsize),
fsize, &flags) == -1)
fsize, &flags, &notecount) == -1)
return -1;
break;
#endif
Expand All @@ -57,22 +58,25 @@
if (dophn_exec(ms, clazz, swap, fd,
(off_t)elf_getu(swap, elfhdr.e_phoff), phnum,
(size_t)elf_getu16(swap, elfhdr.e_phentsize),
fsize, &flags, shnum) == -1)
fsize, shnum, &flags, &notecount) == -1)
return -1;
/*FALLTHROUGH*/
case ET_REL:
shnum = elf_getu16(swap, elfhdr.e_shnum);
if (shnum > ms->elf_shnum_max)
return toomany(ms, "section", shnum);
return toomany(ms, "section headers", shnum);
if (doshn(ms, clazz, swap, fd,
(off_t)elf_getu(swap, elfhdr.e_shoff), shnum,
(size_t)elf_getu16(swap, elfhdr.e_shentsize),
fsize, &flags, elf_getu16(swap, elfhdr.e_machine),
(int)elf_getu16(swap, elfhdr.e_shstrndx)) == -1)
fsize, elf_getu16(swap, elfhdr.e_machine),
(int)elf_getu16(swap, elfhdr.e_shstrndx),
&flags, &notecount) == -1)
return -1;
break;

default:
break;
}
if (notecount == 0)
return toomany(ms, "notes", ms->elf_notes_max);
return 1;
3 changes: 2 additions & 1 deletion src/file.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
#include "file.h"

#ifndef lint
FILE_RCSID("@(#)$File: file.c,v 1.158 2014/11/28 02:35:05 christos Exp $")
FILE_RCSID("@(#)$File: file.c,v 1.159 2014/11/28 02:46:39 christos Exp $")
#endif /* lint */

#include "magic.h"
Expand Down Expand Up @@ -125,6 +125,7 @@ private struct {
{ "name", MAGIC_PARAM_NAME_MAX, 0 },
{ "elf_phnum", MAGIC_PARAM_ELF_PHNUM_MAX, 0 },
{ "elf_shnum", MAGIC_PARAM_ELF_SHNUM_MAX, 0 },
{ "elf_notes", MAGIC_PARAM_ELF_NOTES_MAX, 0 },
};

private char *progname; /* used throughout */
Expand Down
4 changes: 3 additions & 1 deletion src/file.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
*/
/*
* file.h - definitions for file(1) program
* @(#)$File: file.h,v 1.161 2014/12/04 15:56:46 christos Exp $
* @(#)$File: file.h,v 1.162 2014/12/11 12:34:24 christos Exp $
*/

#ifndef __file_h__
Expand Down Expand Up @@ -407,10 +407,12 @@ struct magic_set {
uint16_t name_max;
uint16_t elf_shnum_max;
uint16_t elf_phnum_max;
uint16_t elf_notes_max;
#define FILE_INDIR_MAX 15
#define FILE_NAME_MAX 30
#define FILE_ELF_SHNUM_MAX 32768
#define FILE_ELF_PHNUM_MAX 128
#define FILE_ELF_NOTES_MAX 256
};

/* Type for Unicode characters */
Expand Down
1 change: 1 addition & 0 deletions src/file_opts.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ OPT('p', "preserve-date", 0, " preserve access times on files\n")
OPT('P', "parameter", 0, " set file engine parameter limits\n"
" indir 15 recursion limit for indirection\n"
" name 30 use limit for name/use magic\n"
" elf_notes 256 max ELF notes processed\n"
" elf_phnum 128 max ELF prog sections processed\n"
" elf_shnum 32768 max ELF sections processed\n")
OPT('r', "raw", 0, " don't translate unprintable chars to \\ooo\n")
Expand Down
8 changes: 7 additions & 1 deletion src/magic.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
#include "file.h"

#ifndef lint
FILE_RCSID("@(#)$File: magic.c,v 1.89 2014/11/28 02:46:39 christos Exp $")
FILE_RCSID("@(#)$File: magic.c,v 1.90 2014/12/04 15:56:46 christos Exp $")
#endif /* lint */

#include "magic.h"
Expand Down Expand Up @@ -554,6 +554,9 @@ magic_setparam(struct magic_set *ms, int param, const void *val)
case MAGIC_PARAM_ELF_SHNUM_MAX:
ms->elf_shnum_max = *(const size_t *)val;
return 0;
case MAGIC_PARAM_ELF_NOTES_MAX:
ms->elf_notes_max = *(const size_t *)val;
return 0;
default:
errno = EINVAL;
return -1;
Expand All @@ -576,6 +579,9 @@ magic_getparam(struct magic_set *ms, int param, void *val)
case MAGIC_PARAM_ELF_SHNUM_MAX:
*(size_t *)val = ms->elf_shnum_max;
return 0;
case MAGIC_PARAM_ELF_NOTES_MAX:
*(size_t *)val = ms->elf_notes_max;
return 0;
default:
errno = EINVAL;
return -1;
Expand Down
1 change: 1 addition & 0 deletions src/magic.h.in
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ int magic_errno(magic_t);
#define MAGIC_PARAM_NAME_MAX 1
#define MAGIC_PARAM_ELF_PHNUM_MAX 2
#define MAGIC_PARAM_ELF_SHNUM_MAX 3
#define MAGIC_PARAM_ELF_NOTES_MAX 4

int magic_setparam(magic_t, int, const void *);
int magic_getparam(magic_t, int, void *);
Expand Down
Loading

0 comments on commit ce90e05

Please sign in to comment.