cherry-pick.FILE5_34-65-ge64f6d71.fix-use-after-free-https-runtimeverification-com.patch 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. Subject: Fix use-after-free (https://runtimeverification.com/)
  2. Origin: FILE5_34-65-ge64f6d71 <https://github.com/file/file/commit/FILE5_34-65-ge64f6d71>
  3. Upstream-Author: Christos Zoulas <christos@zoulas.com>
  4. Date: Sat Sep 1 15:52:02 2018 +0000
  5. Fix use-after-free (https://runtimeverification.com/). The free code was
  6. never changed when the mlist was changed from a NULL-terminated list to
  7. a circular one.
  8. --- a/src/apprentice.c
  9. +++ b/src/apprentice.c
  10. @@ -586,6 +586,14 @@
  11. }
  12. private void
  13. +mlist_free_one(struct mlist *ml)
  14. +{
  15. + if (ml->map)
  16. + apprentice_unmap(CAST(struct magic_map *, ml->map));
  17. + free(ml);
  18. +}
  19. +
  20. +private void
  21. mlist_free(struct mlist *mlist)
  22. {
  23. struct mlist *ml, *next;
  24. @@ -593,14 +601,11 @@
  25. if (mlist == NULL)
  26. return;
  27. - ml = mlist->next;
  28. - for (ml = mlist->next; (next = ml->next) != NULL; ml = next) {
  29. - if (ml->map)
  30. - apprentice_unmap(CAST(struct magic_map *, ml->map));
  31. - free(ml);
  32. - if (ml == mlist)
  33. - break;
  34. + for (ml = mlist->next; ml != mlist; ml = next) {
  35. + next = ml->next;
  36. + mlist_free_one(ml);
  37. }
  38. + mlist_free_one(mlist);
  39. }
  40. #ifndef COMPILE_ONLY