-
Use After Free보안개론 2024. 6. 3. 17:35
CWE 416
이미 해제된 메모리 청크를 가리키는 포인터를 다시 참조함
https://github.com/Lagooneng/UseAfterFree
예시 코드
프로세스에서 의도되지 않은 동작을 유발할 수 있음
#include <stdio.h> #include <stdlib.h> struct unicorn_counter {int num;}; int main() { struct unicorn_counter *counter; int *run_calc = (int *)malloc(sizeod(int)); *run_calc = 0; free(run_calc)l counter = (struct unicorn_counter *)malloc(sizeof(struct unicorn_counter)); counter->num = 42; if( *run_calc ) execl("/bin/sh", 0); }
프리 된 청크는 빈에 들어가고,
다음에 할당 요청이 오면 빈에서 꺼내서 씀
run_calc를 free하고 counter에 할당받으면 같은 청크가 할당됨
* 구조체 크기가 4바이트라 같은 빈에서 꺼내옴
이후 counter의 값을 바꾸고,
run_calc의 값을 참조하면 counter에서 바꾼 값이 들어가 있게 됨
#include <stdio.h> #include <stdlib.h> typedef void (*fp)(); void func1() { printf("func1"); } void func2() { printf("func2"); } void main() { fp *pointer1 = malloc(sizeof(fp)); *pointer1 = func1; (*pointer)(); free(pointer); fp *pointer2 = malloc(sizeof(fp)); *pointer2 = func2; (*pointer2)(); (*pointer1)(); // pointer2 is executed }
함수 포인터의 경우도 마찬가지
결과
메모리 변조 -> 무결성
도스, 크래시 -> 가용성
임의 코드 실행 -> CIA 등 ..
청크가 통합 발생 전 악성 데이터 입력 시 특정한 경우 임의 코드 실행 가능
더블 프리 취약점의 완화, 방어 기법
아키텍처, 설계 단계
- 자동으로 메모리를 관리해주는 언어 선택
구현 단계
- 각 할당이 한 번만 해제되도록 보장
- 정적 분석 도구로 더블 프리 인스턴스가 있는지 확인한다. (포티파이, 스패로우)
유즈 애프터 프리의 완화, 방어 기법
아키텍처, 설계 단계
- 자동으로 메모리를 관리해주는 언어 선택
구현 단계
- 포인터가 해제되면 곧바로 변수에 NULL로 초기화해서 더 이상 유효한 메모리를 가리키지 않도록
'보안개론' 카테고리의 다른 글
Array Operations (0) 2024.06.04 Integer Overflow, Underflow / Signedness bugs / Widthness bugs (0) 2024.06.04 bin, chunk, double free (0) 2024.06.03 ptr[-1], ptr[-2]가 가지는 의미 (0) 2024.05.19 프로그램 버그 예시 (0) 2024.05.19