Skip to content

worktree: Fix out of bounds read that causes data loss and reject invalid empty input in worktree add#2187

Open
rimrul wants to merge 2 commits into
gitgitgadget:masterfrom
rimrul:worktree-fix-oob
Open

worktree: Fix out of bounds read that causes data loss and reject invalid empty input in worktree add#2187
rimrul wants to merge 2 commits into
gitgitgadget:masterfrom
rimrul:worktree-fix-oob

Conversation

@rimrul

@rimrul rimrul commented Jul 25, 2026

Copy link
Copy Markdown

Passing an empty string to git worktree add (typically via an unset variable, e.g. git worktree add "$UNSET_VAR" -b tb origin/main) can result in BUG: How come '' becomes empty after sanitization? but it can also have worse consequences: recursively deleting the current working directory, including .git. The inconsistent behaviour is caused by worktree_basename reading unrelated bytes from the memory before path and passing that back to add_worktree, which can circumvent the check for the BUG call.

CC: Marc Branchaud marcnarc@xiplink.com
CC: Nguyễn Thái Ngọc Duy pclouds@gmail.com
CC: Eric Sunshine sunshine@sunshineco.com

@gitgitgadget

gitgitgadget Bot commented Jul 25, 2026

Copy link
Copy Markdown

There is an issue in commit 6d24f9a:
worktree: don't read out of bounds

  • Lines in the body of the commit messages should be wrapped between 60 and 76 characters.
    Indented lines, and lines without whitespace, are exempt

@gitgitgadget

gitgitgadget Bot commented Jul 25, 2026

Copy link
Copy Markdown

There is an issue in commit 500ec11:
worktree: reject empty string

  • Lines in the body of the commit messages should be wrapped between 60 and 76 characters.
    Indented lines, and lines without whitespace, are exempt

rimrul added 2 commits July 25, 2026 12:01
`worktree_basename` tries to read from memory before the passed `path`
string, if `path` is empty (or only consists of directory separators).
That results in unexpected nonsense data being returned to the caller,
which can lead to issues, such as `git worktree add ""` recursively
deleting the current working directory, including `.git`.

Stop reading out of bounds in these cases to avoid that behaviour.

This leads to `git worktree add ""` consistently exiting with the
message `BUG: How come '' becomes empty after sanitization?`, which is
still undesirable, but at least it doesn't result in data loss anymore.

This fixes git-for-windows#6346

Signed-off-by: Matthias Aßhauer <mha1993@live.de>
`git worktree add ""` errors out with the message `BUG: How come ''
becomes empty after sanitization?`, but not due to a bug in the
sanitization code. An empty string should remain empty during
sanitization. Instead reject the argument as invalid user input,
if it's already empty before sanitization.

Signed-off-by: Matthias Aßhauer <mha1993@live.de>
@rimrul

rimrul commented Jul 25, 2026

Copy link
Copy Markdown
Author

/submit

@gitgitgadget

gitgitgadget Bot commented Jul 25, 2026

Copy link
Copy Markdown

Submitted as pull.2187.git.1784978348.gitgitgadget@gmail.com

To fetch this version into FETCH_HEAD:

git fetch https://github.com/gitgitgadget/git/ pr-2187/rimrul/worktree-fix-oob-v1

To fetch this version to local tag pr-2187/rimrul/worktree-fix-oob-v1:

git fetch --no-tags https://github.com/gitgitgadget/git/ tag pr-2187/rimrul/worktree-fix-oob-v1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant