소스 검색

fix invalid read in aligned_alloc

in case of mmap-obtained chunks, end points past the end of the
mapping and reading it may fault. since the value is not needed until
after the conditional, move the access to prevent invalid reads.
Rich Felker 12 년 전
부모
커밋
2ad9cf52eb
1개의 변경된 파일3개의 추가작업 그리고 2개의 파일을 삭제
  1. 3 2
      src/malloc/aligned_alloc.c

+ 3 - 2
src/malloc/aligned_alloc.c

@@ -31,8 +31,6 @@ void *aligned_alloc(size_t align, size_t len)
 		return NULL;
 
 	header = ((size_t *)mem)[-1];
-	end = mem + (header & -8);
-	footer = ((size_t *)end)[-2];
 	new = (void *)((uintptr_t)mem + align-1 & -align);
 
 	if (!(header & 7)) {
@@ -41,6 +39,9 @@ void *aligned_alloc(size_t align, size_t len)
 		return new;
 	}
 
+	end = mem + (header & -8);
+	footer = ((size_t *)end)[-2];
+
 	((size_t *)mem)[-1] = header&7 | new-mem;
 	((size_t *)new)[-2] = footer&7 | new-mem;
 	((size_t *)new)[-1] = header&7 | end-new;