Subject: PR/398: Correctly truncate pascal strings (fixes out of bounds read of 1, 2, or 4 bytes) ID: TEMP-0000000-C482B4 Upstream-Author: Christos Zoulas Date: Tue Nov 11 17:48:23 2014 +0000 Origin: FILE5_20-21-g59e6383 Last-Update: 2015-01-05 PR/398: Correctly truncate pascal strings (fixes out of bounds read of 1, 2, or 4 bytes). diff --git a/src/softmagic.c b/src/softmagic.c index 82603bf..72eae4b 100644 --- a/src/softmagic.c +++ b/src/softmagic.c @@ -803,14 +803,17 @@ mconvert(struct magic_set *ms, struct magic *m) 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++;