Skip to content

fix: prevent double-free in merge_sort (CVE-2021-31996)#7

Open
baima365-web wants to merge 1 commit into
AbrarNitk:masterfrom
baima365-web:forgecore-fix-double-free
Open

fix: prevent double-free in merge_sort (CVE-2021-31996)#7
baima365-web wants to merge 1 commit into
AbrarNitk:masterfrom
baima365-web:forgecore-fix-double-free

Conversation

@baima365-web

Copy link
Copy Markdown

Summary

merge_sort::merge() uses ptr::read() to extract elements from left/right vectors. This moves ownership out of the Vec, causing double-free when the Vec is dropped.

PoC

let mut list = vec![String::from("hello"), String::from("world")];
algorithmica::merge_sort::merge_sort(&mut list, 0, list.len()-1, &|a,b| a.lt(b));
// Double-free panic!

Fix

  1. Add Clone bound to merge() and merge_sort()
  2. Replace ptr::read() with ptr::read().clone() to copy instead of move
  3. Added regression test for String type (T: Drop)

References

…r::read()

The original merge() function used ptr::read() to extract elements
from the left/right vectors. This moves ownership out of the Vec,
causing double-free when the Vec is dropped.

Fix: Add Clone bound and use clone() to copy elements instead of
moving them. Added regression test for String type (T: Drop).

Fixes CVE-2021-31996 / GHSA-jh37-772x-4hpw / Issue AbrarNitk#1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant