flawopen.com/Teardowns/cve-2021-3156-sudo-baron-samedit

● CVE-2021-3156 · CVSS 9.8 · 심각
보안 연구 · FlawOpen

CVE-2021-3156: Sudo Baron Samedit 힙 오버플로 권한 상승 취약점

Baron Samedit (CVE-2021-3156) 기술 분석: Sudo의 인자 이스케이프 해제 과정에서 발생한 힙 오버플로우로 인해 10년 만에 드러난 로컬 루트 권한 상승 결함.

💡 알기 쉬운 설명 (ELI5)

서류 검토원에게 '백슬래시가 나오면 다음 글자를 그대로 복사하라'는 엄격한 규칙이 있는 상황을 상상해 보십시오. 서류 끝에 백슬래시만 남겨둔 신청서를 제출하자, 검토원은 종이 여백을 넘어 책상 위 다음 기밀 서류에 결재 도장을 덮어써 버렸습니다.

핵심 개념 및 용어

setuid 바이너리
호출자가 아닌 소유자(root)의 권한으로 실행되는 실행 파일.
힙 버퍼 오버플로
할당된 힙 메모리 범위를 초과하여 인접한 제어 블록을 손상시키는 취약점.
로컬 권한 상승
일반 권한의 계정에서 시스템 최고 관리자(root) 권한을 즉시 획득하는 행위.

근본 원인 분석 (Root Cause)

근본 원인은 오픈 소스 시스템의 검증되지 않은 경계 매개변수로 인해 상태 비동기화 및 보안 제어 우회가 발생한 데 있습니다.

단계별 공격 실행 흐름

Step 1

Trailing Backslash Injection

Unprivileged user executes 'sudoedit -s \', bypassing normal argument escaping flags.

Step 2

Unterminated Buffer Traversal

The unescape pointer jumps past the null byte delimiter into uninitialized heap memory.

Step 3

Heap Corruption of Service Structures

The heap overflow overwrites the sudo_nss service structure in glibc.

Step 4

Root Privilege Escalation

Sudo loads an attacker-controlled shared library (/tmp/libnss_x.so.2) as root, granting instant root shell access.

소스 코드 비교: 취약한 구현 vs 보안 패치

✕ 취약한 구현
/* VULNERABLE: Unescape loop skips past string termination null byte */
for (to = user_args, from = NewArgv[0]; *from != '\0'; from++) {
    if (from[0] == '\\' && !isspace((unsigned char)from[1]))
        from++; /* BUG: If from[1] == '\0', loop skips past null terminator! */
    *to++ = *from;
}
*to = '\0'; /* Corrupts adjacent heap chunks with root auth structures */
✓ 보안 강화 패치
/* FIXED: Explicitly check that from[1] is neither space nor the null byte */
for (to = user_args, from = NewArgv[0]; *from != '\0'; from++) {
    if (from[0] == '\\' && from[1] != '\0' && !isspace((unsigned char)from[1]))
        from++;
    *to++ = *from;
}
*to = '\0';

엔지니어링 및 시스템 보안 강화 체크리스트

References