Skip to content

Commit 2b3d38a

Browse files
committed
Merge branch 'defense-in-depth'
This topic branch adds a couple of measures designed to make it much harder to exploit any bugs in Git's recursive clone machinery that might be found in the future. Signed-off-by: Johannes Schindelin <[email protected]>
2 parents 86cb6a3 + a33fea0 commit 2b3d38a

21 files changed

+538
-30
lines changed

Documentation/fsck-msgids.txt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,18 @@
157157
`nullSha1`::
158158
(WARN) Tree contains entries pointing to a null sha1.
159159

160+
`symlinkPointsToGitDir`::
161+
(WARN) Symbolic link points inside a gitdir.
162+
163+
`symlinkTargetBlob`::
164+
(ERROR) A non-blob found instead of a symbolic link's target.
165+
166+
`symlinkTargetLength`::
167+
(WARN) Symbolic link target longer than maximum path length.
168+
169+
`symlinkTargetMissing`::
170+
(ERROR) Unable to read symbolic link target's blob.
171+
160172
`treeNotSorted`::
161173
(ERROR) A tree is not properly sorted.
162174

builtin/clone.c

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -908,6 +908,8 @@ int cmd_clone(int argc, const char **argv, const char *prefix)
908908
int err = 0, complete_refs_before_fetch = 1;
909909
int submodule_progress;
910910
int filter_submodules = 0;
911+
const char *template_dir;
912+
char *template_dir_dup = NULL;
911913

912914
struct transport_ls_refs_options transport_ls_refs_options =
913915
TRANSPORT_LS_REFS_OPTIONS_INIT;
@@ -927,6 +929,13 @@ int cmd_clone(int argc, const char **argv, const char *prefix)
927929
usage_msg_opt(_("You must specify a repository to clone."),
928930
builtin_clone_usage, builtin_clone_options);
929931

932+
xsetenv("GIT_CLONE_PROTECTION_ACTIVE", "true", 0 /* allow user override */);
933+
template_dir = get_template_dir(option_template);
934+
if (*template_dir && !is_absolute_path(template_dir))
935+
template_dir = template_dir_dup =
936+
absolute_pathdup(template_dir);
937+
xsetenv("GIT_CLONE_TEMPLATE_DIR", template_dir, 1);
938+
930939
if (option_depth || option_since || option_not.nr)
931940
deepen = 1;
932941
if (option_single_branch == -1)
@@ -1074,7 +1083,7 @@ int cmd_clone(int argc, const char **argv, const char *prefix)
10741083
}
10751084
}
10761085

1077-
init_db(git_dir, real_git_dir, option_template, GIT_HASH_UNKNOWN, NULL,
1086+
init_db(git_dir, real_git_dir, template_dir, GIT_HASH_UNKNOWN, NULL,
10781087
INIT_DB_QUIET);
10791088

10801089
if (real_git_dir) {
@@ -1392,6 +1401,7 @@ int cmd_clone(int argc, const char **argv, const char *prefix)
13921401
free(unborn_head);
13931402
free(dir);
13941403
free(path);
1404+
free(template_dir_dup);
13951405
UNLEAK(repo);
13961406
junk_mode = JUNK_LEAVE_ALL;
13971407

builtin/init-db.c

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,6 @@
1111
#include "parse-options.h"
1212
#include "worktree.h"
1313

14-
#ifndef DEFAULT_GIT_TEMPLATE_DIR
15-
#define DEFAULT_GIT_TEMPLATE_DIR "/usr/share/git-core/templates"
16-
#endif
17-
1814
#ifdef NO_TRUSTABLE_FILEMODE
1915
#define TEST_FILEMODE 0
2016
#else
@@ -93,8 +89,9 @@ static void copy_templates_1(struct strbuf *path, struct strbuf *template_path,
9389
}
9490
}
9591

96-
static void copy_templates(const char *template_dir, const char *init_template_dir)
92+
static void copy_templates(const char *option_template)
9793
{
94+
const char *template_dir = get_template_dir(option_template);
9895
struct strbuf path = STRBUF_INIT;
9996
struct strbuf template_path = STRBUF_INIT;
10097
size_t template_len;
@@ -103,16 +100,8 @@ static void copy_templates(const char *template_dir, const char *init_template_d
103100
DIR *dir;
104101
char *to_free = NULL;
105102

106-
if (!template_dir)
107-
template_dir = getenv(TEMPLATE_DIR_ENVIRONMENT);
108-
if (!template_dir)
109-
template_dir = init_template_dir;
110-
if (!template_dir)
111-
template_dir = to_free = system_path(DEFAULT_GIT_TEMPLATE_DIR);
112-
if (!template_dir[0]) {
113-
free(to_free);
103+
if (!template_dir || !*template_dir)
114104
return;
115-
}
116105

117106
strbuf_addstr(&template_path, template_dir);
118107
strbuf_complete(&template_path, '/');
@@ -200,7 +189,6 @@ static int create_default_files(const char *template_path,
200189
int reinit;
201190
int filemode;
202191
struct strbuf err = STRBUF_INIT;
203-
const char *init_template_dir = NULL;
204192
const char *work_tree = get_git_work_tree();
205193

206194
/*
@@ -212,9 +200,7 @@ static int create_default_files(const char *template_path,
212200
* values (since we've just potentially changed what's available on
213201
* disk).
214202
*/
215-
git_config_get_pathname("init.templatedir", &init_template_dir);
216-
copy_templates(template_path, init_template_dir);
217-
free((char *)init_template_dir);
203+
copy_templates(template_path);
218204
git_config_clear();
219205
reset_shared_repository();
220206
git_config(git_default_config, NULL);

cache.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -656,6 +656,7 @@ int path_inside_repo(const char *prefix, const char *path);
656656
#define INIT_DB_QUIET 0x0001
657657
#define INIT_DB_EXIST_OK 0x0002
658658

659+
const char *get_template_dir(const char *option_template);
659660
int init_db(const char *git_dir, const char *real_git_dir,
660661
const char *template_dir, int hash_algo,
661662
const char *initial_branch, unsigned int flags);
@@ -1784,6 +1785,20 @@ int copy_fd(int ifd, int ofd);
17841785
int copy_file(const char *dst, const char *src, int mode);
17851786
int copy_file_with_time(const char *dst, const char *src, int mode);
17861787

1788+
/*
1789+
* Compare the file mode and contents of two given files.
1790+
*
1791+
* If both files are actually symbolic links, the function returns 1 if the link
1792+
* targets are identical or 0 if they are not.
1793+
*
1794+
* If any of the two files cannot be accessed or in case of read failures, this
1795+
* function returns 0.
1796+
*
1797+
* If the file modes and contents are identical, the function returns 1,
1798+
* otherwise it returns 0.
1799+
*/
1800+
int do_files_match(const char *path1, const char *path2);
1801+
17871802
void write_or_die(int fd, const void *buf, size_t count);
17881803
void fsync_or_die(int fd, const char *);
17891804
int fsync_component(enum fsync_component component, int fd);

config.c

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1525,8 +1525,19 @@ static int git_default_core_config(const char *var, const char *value, void *cb)
15251525
if (!strcmp(var, "core.attributesfile"))
15261526
return git_config_pathname(&git_attributes_file, var, value);
15271527

1528-
if (!strcmp(var, "core.hookspath"))
1528+
if (!strcmp(var, "core.hookspath")) {
1529+
if (current_config_scope() == CONFIG_SCOPE_LOCAL &&
1530+
git_env_bool("GIT_CLONE_PROTECTION_ACTIVE", 0))
1531+
die(_("active `core.hooksPath` found in the local "
1532+
"repository config:\n\t%s\nFor security "
1533+
"reasons, this is disallowed by default.\nIf "
1534+
"this is intentional and the hook should "
1535+
"actually be run, please\nrun the command "
1536+
"again with "
1537+
"`GIT_CLONE_PROTECTION_ACTIVE=false`"),
1538+
value);
15291539
return git_config_pathname(&git_hooks_path, var, value);
1540+
}
15301541

15311542
if (!strcmp(var, "core.bare")) {
15321543
is_bare_repository_cfg = git_config_bool(var, value);

copy.c

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,3 +65,61 @@ int copy_file_with_time(const char *dst, const char *src, int mode)
6565
return copy_times(dst, src);
6666
return status;
6767
}
68+
69+
static int do_symlinks_match(const char *path1, const char *path2)
70+
{
71+
struct strbuf buf1 = STRBUF_INIT, buf2 = STRBUF_INIT;
72+
int ret = 0;
73+
74+
if (!strbuf_readlink(&buf1, path1, 0) &&
75+
!strbuf_readlink(&buf2, path2, 0))
76+
ret = !strcmp(buf1.buf, buf2.buf);
77+
78+
strbuf_release(&buf1);
79+
strbuf_release(&buf2);
80+
return ret;
81+
}
82+
83+
int do_files_match(const char *path1, const char *path2)
84+
{
85+
struct stat st1, st2;
86+
int fd1 = -1, fd2 = -1, ret = 1;
87+
char buf1[8192], buf2[8192];
88+
89+
if ((fd1 = open_nofollow(path1, O_RDONLY)) < 0 ||
90+
fstat(fd1, &st1) || !S_ISREG(st1.st_mode)) {
91+
if (fd1 < 0 && errno == ELOOP)
92+
/* maybe this is a symbolic link? */
93+
return do_symlinks_match(path1, path2);
94+
ret = 0;
95+
} else if ((fd2 = open_nofollow(path2, O_RDONLY)) < 0 ||
96+
fstat(fd2, &st2) || !S_ISREG(st2.st_mode)) {
97+
ret = 0;
98+
}
99+
100+
if (ret)
101+
/* to match, neither must be executable, or both */
102+
ret = !(st1.st_mode & 0111) == !(st2.st_mode & 0111);
103+
104+
if (ret)
105+
ret = st1.st_size == st2.st_size;
106+
107+
while (ret) {
108+
ssize_t len1 = read_in_full(fd1, buf1, sizeof(buf1));
109+
ssize_t len2 = read_in_full(fd2, buf2, sizeof(buf2));
110+
111+
if (len1 < 0 || len2 < 0 || len1 != len2)
112+
ret = 0; /* read error or different file size */
113+
else if (!len1) /* len2 is also 0; hit EOF on both */
114+
break; /* ret is still true */
115+
else
116+
ret = !memcmp(buf1, buf2, len1);
117+
}
118+
119+
if (fd1 >= 0)
120+
close(fd1);
121+
if (fd2 >= 0)
122+
close(fd2);
123+
124+
return ret;
125+
}

dir.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,18 @@ int fspathncmp(const char *a, const char *b, size_t count)
8888
return ignore_case ? strncasecmp(a, b, count) : strncmp(a, b, count);
8989
}
9090

91+
int paths_collide(const char *a, const char *b)
92+
{
93+
size_t len_a = strlen(a), len_b = strlen(b);
94+
95+
if (len_a == len_b)
96+
return fspatheq(a, b);
97+
98+
if (len_a < len_b)
99+
return is_dir_sep(b[len_a]) && !fspathncmp(a, b, len_a);
100+
return is_dir_sep(a[len_b]) && !fspathncmp(a, b, len_b);
101+
}
102+
91103
unsigned int fspathhash(const char *str)
92104
{
93105
return ignore_case ? strihash(str) : strhash(str);

dir.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -519,6 +519,13 @@ int fspatheq(const char *a, const char *b);
519519
int fspathncmp(const char *a, const char *b, size_t count);
520520
unsigned int fspathhash(const char *str);
521521

522+
/*
523+
* Reports whether paths collide. This may be because the paths differ only in
524+
* case on a case-sensitive filesystem, or that one path refers to a symlink
525+
* that collides with one of the parent directories of the other.
526+
*/
527+
int paths_collide(const char *a, const char *b);
528+
522529
/*
523530
* The prefix part of pattern must not contains wildcards.
524531
*/

entry.c

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -454,7 +454,7 @@ static void mark_colliding_entries(const struct checkout *state,
454454
continue;
455455

456456
if ((trust_ino && !match_stat_data(&dup->ce_stat_data, st)) ||
457-
(!trust_ino && !fspathcmp(ce->name, dup->name))) {
457+
paths_collide(ce->name, dup->name)) {
458458
dup->ce_flags |= CE_MATCHED;
459459
break;
460460
}
@@ -541,6 +541,20 @@ int checkout_entry_ca(struct cache_entry *ce, struct conv_attrs *ca,
541541
/* If it is a gitlink, leave it alone! */
542542
if (S_ISGITLINK(ce->ce_mode))
543543
return 0;
544+
/*
545+
* We must avoid replacing submodules' leading
546+
* directories with symbolic links, lest recursive
547+
* clones can write into arbitrary locations.
548+
*
549+
* Technically, this logic is not limited
550+
* to recursive clones, or for that matter to
551+
* submodules' paths colliding with symbolic links'
552+
* paths. Yet it strikes a balance in favor of
553+
* simplicity, and if paths are colliding, we might
554+
* just as well keep the directories during a clone.
555+
*/
556+
if (state->clone && S_ISLNK(ce->ce_mode))
557+
return 0;
544558
remove_subtree(&path);
545559
} else if (unlink(path.buf))
546560
return error_errno("unable to unlink old '%s'", path.buf);

fsck.c

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -636,6 +636,8 @@ static int fsck_tree(const struct object_id *tree_oid,
636636
retval += report(options, tree_oid, OBJ_TREE,
637637
FSCK_MSG_MAILMAP_SYMLINK,
638638
".mailmap is a symlink");
639+
oidset_insert(&options->symlink_targets_found,
640+
entry_oid);
639641
}
640642

641643
if ((backslash = strchr(name, '\\'))) {
@@ -1228,6 +1230,56 @@ static int fsck_blob(const struct object_id *oid, const char *buf,
12281230
}
12291231
}
12301232

1233+
if (oidset_contains(&options->symlink_targets_found, oid)) {
1234+
const char *ptr = buf;
1235+
const struct object_id *reported = NULL;
1236+
1237+
oidset_insert(&options->symlink_targets_done, oid);
1238+
1239+
if (!buf || size > PATH_MAX) {
1240+
/*
1241+
* A missing buffer here is a sign that the caller found the
1242+
* blob too gigantic to load into memory. Let's just consider
1243+
* that an error.
1244+
*/
1245+
return report(options, oid, OBJ_BLOB,
1246+
FSCK_MSG_SYMLINK_TARGET_LENGTH,
1247+
"symlink target too long");
1248+
}
1249+
1250+
while (!reported && ptr) {
1251+
const char *p = ptr;
1252+
char c, *slash = strchrnul(ptr, '/');
1253+
char *backslash = memchr(ptr, '\\', slash - ptr);
1254+
1255+
c = *slash;
1256+
*slash = '\0';
1257+
1258+
while (!reported && backslash) {
1259+
*backslash = '\0';
1260+
if (is_ntfs_dotgit(p))
1261+
ret |= report(options, reported = oid, OBJ_BLOB,
1262+
FSCK_MSG_SYMLINK_POINTS_TO_GIT_DIR,
1263+
"symlink target points to git dir");
1264+
*backslash = '\\';
1265+
p = backslash + 1;
1266+
backslash = memchr(p, '\\', slash - p);
1267+
}
1268+
if (!reported && is_ntfs_dotgit(p))
1269+
ret |= report(options, reported = oid, OBJ_BLOB,
1270+
FSCK_MSG_SYMLINK_POINTS_TO_GIT_DIR,
1271+
"symlink target points to git dir");
1272+
1273+
if (!reported && is_hfs_dotgit(ptr))
1274+
ret |= report(options, reported = oid, OBJ_BLOB,
1275+
FSCK_MSG_SYMLINK_POINTS_TO_GIT_DIR,
1276+
"symlink target points to git dir");
1277+
1278+
*slash = c;
1279+
ptr = c ? slash + 1 : NULL;
1280+
}
1281+
}
1282+
12311283
return ret;
12321284
}
12331285

@@ -1319,6 +1371,10 @@ int fsck_finish(struct fsck_options *options)
13191371
FSCK_MSG_GITATTRIBUTES_MISSING, FSCK_MSG_GITATTRIBUTES_BLOB,
13201372
options, ".gitattributes");
13211373

1374+
ret |= fsck_blobs(&options->symlink_targets_found, &options->symlink_targets_done,
1375+
FSCK_MSG_SYMLINK_TARGET_MISSING, FSCK_MSG_SYMLINK_TARGET_BLOB,
1376+
options, "<symlink-target>");
1377+
13221378
return ret;
13231379
}
13241380

0 commit comments

Comments
 (0)