|
|
e14024 |
From a33759fd275b32ed0bbe89796fe2953b3cb0b41f Mon Sep 17 00:00:00 2001
|
|
|
e14024 |
From: Remi Collet <remi@php.net>
|
|
|
e14024 |
Date: Tue, 4 Mar 2014 20:32:52 +0100
|
|
|
e14024 |
Subject: [PATCH] Fixed Bug #66820 out-of-bounds memory access in fileinfo
|
|
|
e14024 |
|
|
|
e14024 |
Upstream fix:
|
|
|
e14024 |
https://github.com/glensc/file/commit/447558595a3650db2886cd2f416ad0beba965801
|
|
|
e14024 |
|
|
|
e14024 |
Notice, test changed, with upstream agreement:
|
|
|
e14024 |
-define OFFSET_OOB(n, o, i) ((n) < (o) || (i) >= ((n) - (o)))
|
|
|
e14024 |
+define OFFSET_OOB(n, o, i) ((n) < (o) || (i) > ((n) - (o)))
|
|
|
e14024 |
---
|
|
|
e14024 |
ext/fileinfo/libmagic/softmagic.c | 34 ++++++++++++++++++----------------
|
|
|
e14024 |
1 file changed, 18 insertions(+), 16 deletions(-)
|
|
|
e14024 |
|
|
|
e14024 |
diff --git a/ext/fileinfo/libmagic/softmagic.c b/ext/fileinfo/libmagic/softmagic.c
|
|
|
e14024 |
index 82a470a..21fea6b 100644
|
|
|
e14024 |
--- a/ext/fileinfo/libmagic/softmagic.c
|
|
|
e14024 |
+++ b/ext/fileinfo/libmagic/softmagic.c
|
|
|
e14024 |
@@ -67,6 +67,8 @@ private void cvt_16(union VALUETYPE *, const struct magic *);
|
|
|
e14024 |
private void cvt_32(union VALUETYPE *, const struct magic *);
|
|
|
e14024 |
private void cvt_64(union VALUETYPE *, const struct magic *);
|
|
|
e14024 |
|
|
|
e14024 |
+#define OFFSET_OOB(n, o, i) ((n) < (o) || (i) > ((n) - (o)))
|
|
|
e14024 |
+
|
|
|
e14024 |
/*
|
|
|
e14024 |
* softmagic - lookup one file in parsed, in-memory copy of database
|
|
|
e14024 |
* Passed the name and FILE * of one file to be typed.
|
|
|
e14024 |
@@ -1171,7 +1173,7 @@ mget(struct magic_set *ms, const unsigned char *s, struct magic *m,
|
|
|
e14024 |
}
|
|
|
e14024 |
switch (cvt_flip(m->in_type, flip)) {
|
|
|
e14024 |
case FILE_BYTE:
|
|
|
e14024 |
- if (nbytes < (offset + 1))
|
|
|
e14024 |
+ if (OFFSET_OOB(nbytes, offset, 1))
|
|
|
e14024 |
return 0;
|
|
|
e14024 |
if (off) {
|
|
|
e14024 |
switch (m->in_op & FILE_OPS_MASK) {
|
|
|
e14024 |
@@ -1206,7 +1208,7 @@ mget(struct magic_set *ms, const unsigned char *s, struct magic *m,
|
|
|
e14024 |
offset = ~offset;
|
|
|
e14024 |
break;
|
|
|
e14024 |
case FILE_BESHORT:
|
|
|
e14024 |
- if (nbytes < (offset + 2))
|
|
|
e14024 |
+ if (OFFSET_OOB(nbytes, offset, 2))
|
|
|
e14024 |
return 0;
|
|
|
e14024 |
if (off) {
|
|
|
e14024 |
switch (m->in_op & FILE_OPS_MASK) {
|
|
|
e14024 |
@@ -1258,7 +1260,7 @@ mget(struct magic_set *ms, const unsigned char *s, struct magic *m,
|
|
|
e14024 |
offset = ~offset;
|
|
|
e14024 |
break;
|
|
|
e14024 |
case FILE_LESHORT:
|
|
|
e14024 |
- if (nbytes < (offset + 2))
|
|
|
e14024 |
+ if (OFFSET_OOB(nbytes, offset, 2))
|
|
|
e14024 |
return 0;
|
|
|
e14024 |
if (off) {
|
|
|
e14024 |
switch (m->in_op & FILE_OPS_MASK) {
|
|
|
e14024 |
@@ -1310,7 +1312,7 @@ mget(struct magic_set *ms, const unsigned char *s, struct magic *m,
|
|
|
e14024 |
offset = ~offset;
|
|
|
e14024 |
break;
|
|
|
e14024 |
case FILE_SHORT:
|
|
|
e14024 |
- if (nbytes < (offset + 2))
|
|
|
e14024 |
+ if (OFFSET_OOB(nbytes, offset, 2))
|
|
|
e14024 |
return 0;
|
|
|
e14024 |
if (off) {
|
|
|
e14024 |
switch (m->in_op & FILE_OPS_MASK) {
|
|
|
e14024 |
@@ -1347,7 +1349,7 @@ mget(struct magic_set *ms, const unsigned char *s, struct magic *m,
|
|
|
e14024 |
break;
|
|
|
e14024 |
case FILE_BELONG:
|
|
|
e14024 |
case FILE_BEID3:
|
|
|
e14024 |
- if (nbytes < (offset + 4))
|
|
|
e14024 |
+ if (OFFSET_OOB(nbytes, offset, 4))
|
|
|
e14024 |
return 0;
|
|
|
e14024 |
if (off) {
|
|
|
e14024 |
switch (m->in_op & FILE_OPS_MASK) {
|
|
|
e14024 |
@@ -1418,7 +1420,7 @@ mget(struct magic_set *ms, const unsigned char *s, struct magic *m,
|
|
|
e14024 |
break;
|
|
|
e14024 |
case FILE_LELONG:
|
|
|
e14024 |
case FILE_LEID3:
|
|
|
e14024 |
- if (nbytes < (offset + 4))
|
|
|
e14024 |
+ if (OFFSET_OOB(nbytes, offset, 4))
|
|
|
e14024 |
return 0;
|
|
|
e14024 |
if (off) {
|
|
|
e14024 |
switch (m->in_op & FILE_OPS_MASK) {
|
|
|
e14024 |
@@ -1488,7 +1490,7 @@ mget(struct magic_set *ms, const unsigned char *s, struct magic *m,
|
|
|
e14024 |
offset = ~offset;
|
|
|
e14024 |
break;
|
|
|
e14024 |
case FILE_MELONG:
|
|
|
e14024 |
- if (nbytes < (offset + 4))
|
|
|
e14024 |
+ if (OFFSET_OOB(nbytes, offset, 4))
|
|
|
e14024 |
return 0;
|
|
|
e14024 |
if (off) {
|
|
|
e14024 |
switch (m->in_op & FILE_OPS_MASK) {
|
|
|
e14024 |
@@ -1558,7 +1560,7 @@ mget(struct magic_set *ms, const unsigned char *s, struct magic *m,
|
|
|
e14024 |
offset = ~offset;
|
|
|
e14024 |
break;
|
|
|
e14024 |
case FILE_LONG:
|
|
|
e14024 |
- if (nbytes < (offset + 4))
|
|
|
e14024 |
+ if (OFFSET_OOB(nbytes, offset, 4))
|
|
|
e14024 |
return 0;
|
|
|
e14024 |
if (off) {
|
|
|
e14024 |
switch (m->in_op & FILE_OPS_MASK) {
|
|
|
e14024 |
@@ -1630,14 +1632,14 @@ mget(struct magic_set *ms, const unsigned char *s, struct magic *m,
|
|
|
e14024 |
/* Verify we have enough data to match magic type */
|
|
|
e14024 |
switch (m->type) {
|
|
|
e14024 |
case FILE_BYTE:
|
|
|
e14024 |
- if (nbytes < (offset + 1)) /* should alway be true */
|
|
|
e14024 |
+ if (OFFSET_OOB(nbytes, offset, 1))
|
|
|
e14024 |
return 0;
|
|
|
e14024 |
break;
|
|
|
e14024 |
|
|
|
e14024 |
case FILE_SHORT:
|
|
|
e14024 |
case FILE_BESHORT:
|
|
|
e14024 |
case FILE_LESHORT:
|
|
|
e14024 |
- if (nbytes < (offset + 2))
|
|
|
e14024 |
+ if (OFFSET_OOB(nbytes, offset, 2))
|
|
|
e14024 |
return 0;
|
|
|
e14024 |
break;
|
|
|
e14024 |
|
|
|
e14024 |
@@ -1656,33 +1658,33 @@ mget(struct magic_set *ms, const unsigned char *s, struct magic *m,
|
|
|
e14024 |
case FILE_FLOAT:
|
|
|
e14024 |
case FILE_BEFLOAT:
|
|
|
e14024 |
case FILE_LEFLOAT:
|
|
|
e14024 |
- if (nbytes < (offset + 4))
|
|
|
e14024 |
+ if (OFFSET_OOB(nbytes, offset, 4))
|
|
|
e14024 |
return 0;
|
|
|
e14024 |
break;
|
|
|
e14024 |
|
|
|
e14024 |
case FILE_DOUBLE:
|
|
|
e14024 |
case FILE_BEDOUBLE:
|
|
|
e14024 |
case FILE_LEDOUBLE:
|
|
|
e14024 |
- if (nbytes < (offset + 8))
|
|
|
e14024 |
+ if (OFFSET_OOB(nbytes, offset, 8))
|
|
|
e14024 |
return 0;
|
|
|
e14024 |
break;
|
|
|
e14024 |
|
|
|
e14024 |
case FILE_STRING:
|
|
|
e14024 |
case FILE_PSTRING:
|
|
|
e14024 |
case FILE_SEARCH:
|
|
|
e14024 |
- if (nbytes < (offset + m->vallen))
|
|
|
e14024 |
+ if (OFFSET_OOB(nbytes, offset, m->vallen))
|
|
|
e14024 |
return 0;
|
|
|
e14024 |
break;
|
|
|
e14024 |
|
|
|
e14024 |
case FILE_REGEX:
|
|
|
e14024 |
- if (nbytes < offset)
|
|
|
e14024 |
+ if (OFFSET_OOB(nbytes, offset, 0))
|
|
|
e14024 |
return 0;
|
|
|
e14024 |
break;
|
|
|
e14024 |
|
|
|
e14024 |
case FILE_INDIRECT:
|
|
|
e14024 |
if (offset == 0)
|
|
|
e14024 |
return 0;
|
|
|
e14024 |
- if (nbytes < offset)
|
|
|
e14024 |
+ if (OFFSET_OOB(nbytes, offset, 0))
|
|
|
e14024 |
return 0;
|
|
|
e14024 |
sbuf = ms->o.buf;
|
|
|
e14024 |
soffset = ms->offset;
|
|
|
e14024 |
@@ -1716,7 +1718,7 @@ mget(struct magic_set *ms, const unsigned char *s, struct magic *m,
|
|
|
e14024 |
return rv;
|
|
|
e14024 |
|
|
|
e14024 |
case FILE_USE:
|
|
|
e14024 |
- if (nbytes < offset)
|
|
|
e14024 |
+ if (OFFSET_OOB(nbytes, offset, 0))
|
|
|
e14024 |
return 0;
|
|
|
e14024 |
sbuf = m->value.s;
|
|
|
e14024 |
if (*sbuf == '^') {
|
|
|
e14024 |
--
|
|
|
e14024 |
1.8.4.3
|
|
|
e14024 |
|