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年間潜んでいたエスケープ処理の不備と Root 権限昇格の手順。

💡 わかりやすい解説 (ELI5)

書類審査員には「バックスラッシュがあれば次の1文字をそのまま書き写す」というルールがあると想像してください。末尾がバックスラッシュで終わる書類を渡された審査員は、用紙の端を越えて机の上の書類にまでペンを走らせ、他の申請書の許可印を勝手に書き換えてしまいました。

主要な概念と専門用語

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