commit a48420af0e6a32a59f5fbcc7c78fa642ca2d89a832de5aad1f6234ddf212b326 from: Stefan Sperling date: Sun May 10 12:41:46 2026 UTC avoid checking out tree entry names "." and "..", and names containing "/" This would allow malicious repositories to perform arbitrary file overwrites with user permissions via crafted tree entry names. The regression test added here shows that such repositories can be trivially created with Git. On operating systems which lack unveil(2) any user-writable file could be created or changed. On OpenBSD, unveil(2) restricts arbitrary write access to the work tree itself and the /tmp directory. Problem reported me by Runxi Yu, along with a reproduction script I could use to write the regression test. Thanks! commit - a3e261487a807988b92001ccb748c29465526cb3f5be0bf2c2b6de6123a1a27b commit + a48420af0e6a32a59f5fbcc7c78fa642ca2d89a832de5aad1f6234ddf212b326 blob - c5b5dbc5f19754d24d01826a72bf2dd0402397e4082ec845347612d844de792f blob + ef94d066eff2225f9f5b0901492bb49950a7c5e59b94cd348d4ed8ff28576594 --- lib/worktree.c +++ lib/worktree.c @@ -1942,11 +1942,19 @@ update_blob(struct got_worktree *worktree, const struct got_error *err = NULL; struct got_blob_object *blob = NULL; char *ondisk_path = NULL; + const char *te_name; unsigned char status = GOT_STATUS_NO_CHANGE; struct stat sb; int fd1 = -1, fd2 = -1; int update_timestamps = 0; + te_name = got_tree_entry_get_name(te); + if (strchr(te_name, '/') != NULL || + strcmp(te_name, ".") == 0 || strcmp(te_name, "..") == 0) { + return (*progress_cb)(progress_arg, + GOT_STATUS_UNVERSIONED, path); + } + err = got_fileindex_entry_relpath_allowed(path, strlen(path)); if (err) { if (err->code != GOT_ERR_BAD_PATH) blob - 7e2eef68ba9eebe707b274be59378b4d4205dcbcb0aef4bad77d2db8fdc006cb blob + ad1025f67ad845290a0f53e6c175c42d6c8ccc20e00d0f3829c2ad2ff670299e --- regress/cmdline/checkout.sh +++ regress/cmdline/checkout.sh @@ -1124,6 +1124,67 @@ test_checkout_tree_with_dot_got() { test_done "$testroot" "$ret" } +test_checkout_bad_tree_entry() { + local testroot=`test_init checkout_bad_tree_entry` + + ( + cd $testroot/repo + + entry() { + mode="$1" + name="$2" + sha="$3" + printf '%s %s\0' "$mode" "$name" + printf '%s' "$sha" | xxd -r -p + } + + blob="$(echo 'oh no' | git hash-object -w --stdin)" + tree="$(entry 100644 ../pwned "$blob" | git hash-object -w -t tree --stdin --literally)" + + commit="$(echo cursed | git commit-tree "$tree")" + git update-ref refs/heads/main "$commit" + git symbolic-ref HEAD refs/heads/main + ) + + local commit_id=`git_show_head $testroot/repo` + + got checkout $testroot/repo $testroot/wt > $testroot/stdout + ret=$? + if [ $ret -ne 0 ]; then + test_done "$testroot" "$ret" + return 1 + fi + + echo "? $testroot/wt/../pwned" > $testroot/stdout.expected + echo "Checked out refs/heads/main: $commit_id" \ + >> $testroot/stdout.expected + echo "Now shut up and hack" >> $testroot/stdout.expected + cmp -s $testroot/stdout.expected $testroot/stdout + ret=$? + if [ $ret -ne 0 ]; then + diff -u $testroot/stdout.expected $testroot/stdout + test_done "$testroot" "$ret" + return 1 + fi + + (cd $testroot/wt && ls -a > $testroot/stdout) + + cat > $testroot/stdout.expected <