From ede59c8feb4b80e1b94e4abdaa0711051e2912ab Mon Sep 17 00:00:00 2001 From: Anatol Belski Date: Sun, 4 Jan 2015 14:20:21 +0100 Subject: Fixed bug #68735 fileinfo out-of-bounds memory access --- ext/fileinfo/libmagic/softmagic.c | 7 +++++-- ext/fileinfo/tests/bug68735.jpg | Bin 0 -> 24 bytes ext/fileinfo/tests/bug68735.phpt | 16 ++++++++++++++++ 3 files changed, 21 insertions(+), 2 deletions(-) create mode 100644 ext/fileinfo/tests/bug68735.jpg create mode 100644 ext/fileinfo/tests/bug68735.phpt (limited to 'ext/fileinfo') diff --git a/ext/fileinfo/libmagic/softmagic.c b/ext/fileinfo/libmagic/softmagic.c index 7e0c8560e3..e7b7855eef 100644 --- a/ext/fileinfo/libmagic/softmagic.c +++ b/ext/fileinfo/libmagic/softmagic.c @@ -884,14 +884,17 @@ mconvert(struct magic_set *ms, struct magic *m, int flip) size_t sz = file_pstring_length_size(m); char *ptr1 = p->s, *ptr2 = ptr1 + sz; size_t len = file_pstring_get_length(m, ptr1); - if (len >= sizeof(p->s)) { + sz = sizeof(p->s) - sz; /* maximum length of string */ + if (len >= sz) { /* * The size of the pascal string length (sz) * is 1, 2, or 4. We need at least 1 byte for NUL * termination, but we've already truncated the * string by p->s, so we need to deduct sz. + * Because we can use one of the bytes of the length + * after we shifted as NUL termination. */ - len = sizeof(p->s) - sz; + len = sz; } while (len--) *ptr1++ = *ptr2++; diff --git a/ext/fileinfo/tests/bug68735.jpg b/ext/fileinfo/tests/bug68735.jpg new file mode 100644 index 0000000000..633bdb93ed Binary files /dev/null and b/ext/fileinfo/tests/bug68735.jpg differ diff --git a/ext/fileinfo/tests/bug68735.phpt b/ext/fileinfo/tests/bug68735.phpt new file mode 100644 index 0000000000..f86109cb8f --- /dev/null +++ b/ext/fileinfo/tests/bug68735.phpt @@ -0,0 +1,16 @@ +--TEST-- +Bug #68735 fileinfo out-of-bounds memory access +--SKIPIF-- + +--FILE-- +file($test_file)); + +?> +===DONE=== +--EXPECTF-- +string(%d) "JPEG image data, JFIF standard 1.01, comment: "%S"" +===DONE=== -- cgit v1.2.1