Skip to content

Commit d1bb66a

Browse files
pks-tdscho
authored andcommitted
builtin/clone: abort when hardlinked source and target file differ
When performing local clones with hardlinks we refuse to copy source files which are symlinks as a mitigation for CVE-2022-39253. This check can be raced by an adversary though by changing the file to a symlink after we have checked it. Fix the issue by checking whether the hardlinked destination file matches the source file and abort in case it doesn't. This addresses CVE-2024-32021. Reported-by: Apple Product Security <[email protected]> Suggested-by: Linus Torvalds <[email protected]> Signed-off-by: Patrick Steinhardt <[email protected]> Signed-off-by: Johannes Schindelin <[email protected]>
1 parent 150e6b0 commit d1bb66a

File tree

1 file changed

+20
-1
lines changed

1 file changed

+20
-1
lines changed

builtin/clone.c

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -357,8 +357,27 @@ static void copy_or_link_directory(struct strbuf *src, struct strbuf *dest,
357357
if (unlink(dest->buf) && errno != ENOENT)
358358
die_errno(_("failed to unlink '%s'"), dest->buf);
359359
if (!option_no_hardlinks) {
360-
if (!link(src->buf, dest->buf))
360+
if (!link(src->buf, dest->buf)) {
361+
struct stat st;
362+
363+
/*
364+
* Sanity-check whether the created hardlink
365+
* actually links to the expected file now. This
366+
* catches time-of-check-time-of-use bugs in
367+
* case the source file was meanwhile swapped.
368+
*/
369+
if (lstat(dest->buf, &st))
370+
die(_("hardlink cannot be checked at '%s'"), dest->buf);
371+
if (st.st_mode != iter->st.st_mode ||
372+
st.st_ino != iter->st.st_ino ||
373+
st.st_dev != iter->st.st_dev ||
374+
st.st_size != iter->st.st_size ||
375+
st.st_uid != iter->st.st_uid ||
376+
st.st_gid != iter->st.st_gid)
377+
die(_("hardlink different from source at '%s'"), dest->buf);
378+
361379
continue;
380+
}
362381
if (option_local > 0)
363382
die_errno(_("failed to create link '%s'"), dest->buf);
364383
option_no_hardlinks = 1;

0 commit comments

Comments
 (0)