TEMP-0000000-C482B4.59e6383.patch 1.2 KB

12345678910111213141516171819202122232425262728293031323334
  1. Subject: PR/398: Correctly truncate pascal strings (fixes out of bounds read of 1, 2, or 4 bytes)
  2. ID: TEMP-0000000-C482B4
  3. Upstream-Author: Christos Zoulas <christos@zoulas.com>
  4. Date: Tue Nov 11 17:48:23 2014 +0000
  5. Origin: FILE5_20-21-g59e6383
  6. Last-Update: 2015-01-05
  7. PR/398: Correctly truncate pascal strings (fixes out of bounds read of 1, 2,
  8. or 4 bytes).
  9. diff --git a/src/softmagic.c b/src/softmagic.c
  10. index 82603bf..72eae4b 100644
  11. --- a/src/softmagic.c
  12. +++ b/src/softmagic.c
  13. @@ -803,14 +803,17 @@ mconvert(struct magic_set *ms, struct magic *m)
  14. size_t sz = file_pstring_length_size(m);
  15. char *ptr1 = p->s, *ptr2 = ptr1 + sz;
  16. size_t len = file_pstring_get_length(m, ptr1);
  17. - if (len >= sizeof(p->s)) {
  18. + sz = sizeof(p->s) - sz; /* maximum length of string */
  19. + if (len >= sz) {
  20. /*
  21. * The size of the pascal string length (sz)
  22. * is 1, 2, or 4. We need at least 1 byte for NUL
  23. * termination, but we've already truncated the
  24. * string by p->s, so we need to deduct sz.
  25. + * Because we can use one of the bytes of the length
  26. + * after we shifted as NUL termination.
  27. */
  28. - len = sizeof(p->s) - sz;
  29. + len = sz;
  30. }
  31. while (len--)
  32. *ptr1++ = *ptr2++;