1 #include "git-compat-util.h"
4 #include "object-store.h"
5 #include "repository.h"
10 #include "tree-walk.h"
20 #include "submodule-config.h"
22 #include "credential.h"
26 #define MSG_ID(id, msg_type) { STR(id), NULL, NULL, FSCK_##msg_type },
28 const char *id_string
;
29 const char *downcased
;
30 const char *camelcased
;
31 enum fsck_msg_type msg_type
;
32 } msg_id_info
[FSCK_MSG_MAX
+ 1] = {
33 FOREACH_FSCK_MSG_ID(MSG_ID
)
34 { NULL
, NULL
, NULL
, -1 }
39 static void prepare_msg_ids(void)
43 if (msg_id_info
[0].downcased
)
46 /* convert id_string to lower case, without underscores. */
47 for (i
= 0; i
< FSCK_MSG_MAX
; i
++) {
48 const char *p
= msg_id_info
[i
].id_string
;
50 char *q
= xmalloc(len
);
52 msg_id_info
[i
].downcased
= q
;
57 *(q
)++ = tolower(*(p
)++);
60 p
= msg_id_info
[i
].id_string
;
62 msg_id_info
[i
].camelcased
= q
;
76 static int parse_msg_id(const char *text
)
82 for (i
= 0; i
< FSCK_MSG_MAX
; i
++)
83 if (!strcmp(text
, msg_id_info
[i
].downcased
))
89 void list_config_fsck_msg_ids(struct string_list
*list
, const char *prefix
)
95 for (i
= 0; i
< FSCK_MSG_MAX
; i
++)
96 list_config_item(list
, prefix
, msg_id_info
[i
].camelcased
);
99 static enum fsck_msg_type
fsck_msg_type(enum fsck_msg_id msg_id
,
100 struct fsck_options
*options
)
102 assert(msg_id
>= 0 && msg_id
< FSCK_MSG_MAX
);
104 if (!options
->msg_type
) {
105 enum fsck_msg_type msg_type
= msg_id_info
[msg_id
].msg_type
;
107 if (options
->strict
&& msg_type
== FSCK_WARN
)
108 msg_type
= FSCK_ERROR
;
112 return options
->msg_type
[msg_id
];
115 static enum fsck_msg_type
parse_msg_type(const char *str
)
117 if (!strcmp(str
, "error"))
119 else if (!strcmp(str
, "warn"))
121 else if (!strcmp(str
, "ignore"))
124 die("Unknown fsck message type: '%s'", str
);
127 int is_valid_msg_type(const char *msg_id
, const char *msg_type
)
129 if (parse_msg_id(msg_id
) < 0)
131 parse_msg_type(msg_type
);
135 void fsck_set_msg_type_from_ids(struct fsck_options
*options
,
136 enum fsck_msg_id msg_id
,
137 enum fsck_msg_type msg_type
)
139 if (!options
->msg_type
) {
141 enum fsck_msg_type
*severity
;
142 ALLOC_ARRAY(severity
, FSCK_MSG_MAX
);
143 for (i
= 0; i
< FSCK_MSG_MAX
; i
++)
144 severity
[i
] = fsck_msg_type(i
, options
);
145 options
->msg_type
= severity
;
148 options
->msg_type
[msg_id
] = msg_type
;
151 void fsck_set_msg_type(struct fsck_options
*options
,
152 const char *msg_id_str
, const char *msg_type_str
)
154 int msg_id
= parse_msg_id(msg_id_str
);
155 enum fsck_msg_type msg_type
= parse_msg_type(msg_type_str
);
158 die("Unhandled message id: %s", msg_id_str
);
160 if (msg_type
!= FSCK_ERROR
&& msg_id_info
[msg_id
].msg_type
== FSCK_FATAL
)
161 die("Cannot demote %s to %s", msg_id_str
, msg_type_str
);
163 fsck_set_msg_type_from_ids(options
, msg_id
, msg_type
);
166 void fsck_set_msg_types(struct fsck_options
*options
, const char *values
)
168 char *buf
= xstrdup(values
), *to_free
= buf
;
172 int len
= strcspn(buf
, " ,|"), equal
;
182 equal
< len
&& buf
[equal
] != '=' && buf
[equal
] != ':';
184 buf
[equal
] = tolower(buf
[equal
]);
187 if (!strcmp(buf
, "skiplist")) {
189 die("skiplist requires a path");
190 oidset_parse_file(&options
->skiplist
, buf
+ equal
+ 1);
196 die("Missing '=': '%s'", buf
);
198 fsck_set_msg_type(options
, buf
, buf
+ equal
+ 1);
204 static int object_on_skiplist(struct fsck_options
*opts
,
205 const struct object_id
*oid
)
207 return opts
&& oid
&& oidset_contains(&opts
->skiplist
, oid
);
210 __attribute__((format (printf
, 5, 6)))
211 static int report(struct fsck_options
*options
,
212 const struct object_id
*oid
, enum object_type object_type
,
213 enum fsck_msg_id msg_id
, const char *fmt
, ...)
216 struct strbuf sb
= STRBUF_INIT
;
217 enum fsck_msg_type msg_type
= fsck_msg_type(msg_id
, options
);
220 if (msg_type
== FSCK_IGNORE
)
223 if (object_on_skiplist(options
, oid
))
226 if (msg_type
== FSCK_FATAL
)
227 msg_type
= FSCK_ERROR
;
228 else if (msg_type
== FSCK_INFO
)
229 msg_type
= FSCK_WARN
;
232 strbuf_addf(&sb
, "%s: ", msg_id_info
[msg_id
].camelcased
);
235 strbuf_vaddf(&sb
, fmt
, ap
);
236 result
= options
->error_func(options
, oid
, object_type
,
237 msg_type
, msg_id
, sb
.buf
);
244 void fsck_enable_object_names(struct fsck_options
*options
)
246 if (!options
->object_names
)
247 options
->object_names
= kh_init_oid_map();
250 const char *fsck_get_object_name(struct fsck_options
*options
,
251 const struct object_id
*oid
)
254 if (!options
->object_names
)
256 pos
= kh_get_oid_map(options
->object_names
, *oid
);
257 if (pos
>= kh_end(options
->object_names
))
259 return kh_value(options
->object_names
, pos
);
262 void fsck_put_object_name(struct fsck_options
*options
,
263 const struct object_id
*oid
,
264 const char *fmt
, ...)
267 struct strbuf buf
= STRBUF_INIT
;
271 if (!options
->object_names
)
274 pos
= kh_put_oid_map(options
->object_names
, *oid
, &hashret
);
278 strbuf_vaddf(&buf
, fmt
, ap
);
279 kh_value(options
->object_names
, pos
) = strbuf_detach(&buf
, NULL
);
283 const char *fsck_describe_object(struct fsck_options
*options
,
284 const struct object_id
*oid
)
286 static struct strbuf bufs
[] = {
287 STRBUF_INIT
, STRBUF_INIT
, STRBUF_INIT
, STRBUF_INIT
291 const char *name
= fsck_get_object_name(options
, oid
);
294 b
= (b
+ 1) % ARRAY_SIZE(bufs
);
296 strbuf_addstr(buf
, oid_to_hex(oid
));
298 strbuf_addf(buf
, " (%s)", name
);
303 static int fsck_walk_tree(struct tree
*tree
, void *data
, struct fsck_options
*options
)
305 struct tree_desc desc
;
306 struct name_entry entry
;
310 if (parse_tree(tree
))
313 name
= fsck_get_object_name(options
, &tree
->object
.oid
);
314 if (init_tree_desc_gently(&desc
, tree
->buffer
, tree
->size
, 0))
316 while (tree_entry_gently(&desc
, &entry
)) {
320 if (S_ISGITLINK(entry
.mode
))
323 if (S_ISDIR(entry
.mode
)) {
324 obj
= (struct object
*)lookup_tree(the_repository
, &entry
.oid
);
326 fsck_put_object_name(options
, &entry
.oid
, "%s%s/",
328 result
= options
->walk(obj
, OBJ_TREE
, data
, options
);
330 else if (S_ISREG(entry
.mode
) || S_ISLNK(entry
.mode
)) {
331 obj
= (struct object
*)lookup_blob(the_repository
, &entry
.oid
);
333 fsck_put_object_name(options
, &entry
.oid
, "%s%s",
335 result
= options
->walk(obj
, OBJ_BLOB
, data
, options
);
338 result
= error("in tree %s: entry %s has bad mode %.6o",
339 fsck_describe_object(options
, &tree
->object
.oid
),
340 entry
.path
, entry
.mode
);
350 static int fsck_walk_commit(struct commit
*commit
, void *data
, struct fsck_options
*options
)
352 int counter
= 0, generation
= 0, name_prefix_len
= 0;
353 struct commit_list
*parents
;
358 if (parse_commit(commit
))
361 name
= fsck_get_object_name(options
, &commit
->object
.oid
);
363 fsck_put_object_name(options
, get_commit_tree_oid(commit
),
366 result
= options
->walk((struct object
*)get_commit_tree(commit
),
367 OBJ_TREE
, data
, options
);
372 parents
= commit
->parents
;
373 if (name
&& parents
) {
374 int len
= strlen(name
), power
;
376 if (len
&& name
[len
- 1] == '^') {
378 name_prefix_len
= len
- 1;
380 else { /* parse ~<generation> suffix */
381 for (generation
= 0, power
= 1;
382 len
&& isdigit(name
[len
- 1]);
384 generation
+= power
* (name
[--len
] - '0');
385 if (power
> 1 && len
&& name
[len
- 1] == '~')
386 name_prefix_len
= len
- 1;
388 /* Maybe a non-first parent, e.g. HEAD^2 */
390 name_prefix_len
= len
;
397 struct object_id
*oid
= &parents
->item
->object
.oid
;
400 fsck_put_object_name(options
, oid
, "%s^%d",
402 else if (generation
> 0)
403 fsck_put_object_name(options
, oid
, "%.*s~%d",
404 name_prefix_len
, name
,
407 fsck_put_object_name(options
, oid
, "%s^", name
);
409 result
= options
->walk((struct object
*)parents
->item
, OBJ_COMMIT
, data
, options
);
414 parents
= parents
->next
;
419 static int fsck_walk_tag(struct tag
*tag
, void *data
, struct fsck_options
*options
)
421 const char *name
= fsck_get_object_name(options
, &tag
->object
.oid
);
426 fsck_put_object_name(options
, &tag
->tagged
->oid
, "%s", name
);
427 return options
->walk(tag
->tagged
, OBJ_ANY
, data
, options
);
430 int fsck_walk(struct object
*obj
, void *data
, struct fsck_options
*options
)
435 if (obj
->type
== OBJ_NONE
)
436 parse_object(the_repository
, &obj
->oid
);
442 return fsck_walk_tree((struct tree
*)obj
, data
, options
);
444 return fsck_walk_commit((struct commit
*)obj
, data
, options
);
446 return fsck_walk_tag((struct tag
*)obj
, data
, options
);
448 error("Unknown object type for %s",
449 fsck_describe_object(options
, &obj
->oid
));
459 static void name_stack_push(struct name_stack
*stack
, const char *name
)
461 ALLOC_GROW(stack
->names
, stack
->nr
+ 1, stack
->alloc
);
462 stack
->names
[stack
->nr
++] = name
;
465 static const char *name_stack_pop(struct name_stack
*stack
)
467 return stack
->nr
? stack
->names
[--stack
->nr
] : NULL
;
470 static void name_stack_clear(struct name_stack
*stack
)
472 FREE_AND_NULL(stack
->names
);
473 stack
->nr
= stack
->alloc
= 0;
477 * The entries in a tree are ordered in the _path_ order,
478 * which means that a directory entry is ordered by adding
479 * a slash to the end of it.
481 * So a directory called "a" is ordered _after_ a file
482 * called "a.c", because "a/" sorts after "a.c".
484 #define TREE_UNORDERED (-1)
485 #define TREE_HAS_DUPS (-2)
487 static int is_less_than_slash(unsigned char c
)
489 return '\0' < c
&& c
< '/';
492 static int verify_ordered(unsigned mode1
, const char *name1
,
493 unsigned mode2
, const char *name2
,
494 struct name_stack
*candidates
)
496 int len1
= strlen(name1
);
497 int len2
= strlen(name2
);
498 int len
= len1
< len2
? len1
: len2
;
499 unsigned char c1
, c2
;
502 cmp
= memcmp(name1
, name2
, len
);
506 return TREE_UNORDERED
;
509 * Ok, the first <len> characters are the same.
510 * Now we need to order the next one, but turn
511 * a '\0' into a '/' for a directory entry.
517 * git-write-tree used to write out a nonsense tree that has
518 * entries with the same name, one blob and one tree. Make
519 * sure we do not have duplicate entries.
521 return TREE_HAS_DUPS
;
522 if (!c1
&& S_ISDIR(mode1
))
524 if (!c2
&& S_ISDIR(mode2
))
528 * There can be non-consecutive duplicates due to the implicitly
537 * Record non-directory candidates (like "foo" and "foo.bar" in
538 * the example) on a stack and check directory candidates (like
539 * foo/" and "foo.bar/") against that stack.
541 if (!c1
&& is_less_than_slash(c2
)) {
542 name_stack_push(candidates
, name1
);
543 } else if (c2
== '/' && is_less_than_slash(c1
)) {
546 const char *f_name
= name_stack_pop(candidates
);
550 if (!skip_prefix(name2
, f_name
, &p
))
553 return TREE_HAS_DUPS
;
554 if (is_less_than_slash(*p
)) {
555 name_stack_push(candidates
, f_name
);
561 return c1
< c2
? 0 : TREE_UNORDERED
;
564 static int fsck_tree(const struct object_id
*tree_oid
,
565 const char *buffer
, unsigned long size
,
566 struct fsck_options
*options
)
569 int has_null_sha1
= 0;
570 int has_full_path
= 0;
571 int has_empty_name
= 0;
575 int has_zero_pad
= 0;
576 int has_bad_modes
= 0;
577 int has_dup_entries
= 0;
578 int not_properly_sorted
= 0;
579 struct tree_desc desc
;
582 struct name_stack df_dup_candidates
= { NULL
};
584 if (init_tree_desc_gently(&desc
, buffer
, size
, TREE_DESC_RAW_MODES
)) {
585 retval
+= report(options
, tree_oid
, OBJ_TREE
,
587 "cannot be parsed as a tree");
596 const char *name
, *backslash
;
597 const struct object_id
*entry_oid
;
599 entry_oid
= tree_entry_extract(&desc
, &name
, &mode
);
601 has_null_sha1
|= is_null_oid(entry_oid
);
602 has_full_path
|= !!strchr(name
, '/');
603 has_empty_name
|= !*name
;
604 has_dot
|= !strcmp(name
, ".");
605 has_dotdot
|= !strcmp(name
, "..");
606 has_dotgit
|= is_hfs_dotgit(name
) || is_ntfs_dotgit(name
);
607 has_zero_pad
|= *(char *)desc
.buffer
== '0';
609 if (is_hfs_dotgitmodules(name
) || is_ntfs_dotgitmodules(name
)) {
611 oidset_insert(&options
->gitmodules_found
,
614 retval
+= report(options
,
616 FSCK_MSG_GITMODULES_SYMLINK
,
617 ".gitmodules is a symbolic link");
620 if (is_hfs_dotgitattributes(name
) || is_ntfs_dotgitattributes(name
)) {
622 oidset_insert(&options
->gitattributes_found
,
625 retval
+= report(options
, tree_oid
, OBJ_TREE
,
626 FSCK_MSG_GITATTRIBUTES_SYMLINK
,
627 ".gitattributes is a symlink");
631 if (is_hfs_dotgitignore(name
) ||
632 is_ntfs_dotgitignore(name
))
633 retval
+= report(options
, tree_oid
, OBJ_TREE
,
634 FSCK_MSG_GITIGNORE_SYMLINK
,
635 ".gitignore is a symlink");
636 if (is_hfs_dotmailmap(name
) ||
637 is_ntfs_dotmailmap(name
))
638 retval
+= report(options
, tree_oid
, OBJ_TREE
,
639 FSCK_MSG_MAILMAP_SYMLINK
,
640 ".mailmap is a symlink");
643 if ((backslash
= strchr(name
, '\\'))) {
646 has_dotgit
|= is_ntfs_dotgit(backslash
);
647 if (is_ntfs_dotgitmodules(backslash
)) {
649 oidset_insert(&options
->gitmodules_found
,
652 retval
+= report(options
, tree_oid
, OBJ_TREE
,
653 FSCK_MSG_GITMODULES_SYMLINK
,
654 ".gitmodules is a symbolic link");
656 backslash
= strchr(backslash
, '\\');
660 if (update_tree_entry_gently(&desc
)) {
661 retval
+= report(options
, tree_oid
, OBJ_TREE
,
663 "cannot be parsed as a tree");
678 * This is nonstandard, but we had a few of these
679 * early on when we honored the full set of mode
683 if (!options
->strict
)
691 switch (verify_ordered(o_mode
, o_name
, mode
, name
,
692 &df_dup_candidates
)) {
694 not_properly_sorted
= 1;
708 name_stack_clear(&df_dup_candidates
);
711 retval
+= report(options
, tree_oid
, OBJ_TREE
,
713 "contains entries pointing to null sha1");
715 retval
+= report(options
, tree_oid
, OBJ_TREE
,
716 FSCK_MSG_FULL_PATHNAME
,
717 "contains full pathnames");
719 retval
+= report(options
, tree_oid
, OBJ_TREE
,
721 "contains empty pathname");
723 retval
+= report(options
, tree_oid
, OBJ_TREE
,
727 retval
+= report(options
, tree_oid
, OBJ_TREE
,
731 retval
+= report(options
, tree_oid
, OBJ_TREE
,
735 retval
+= report(options
, tree_oid
, OBJ_TREE
,
736 FSCK_MSG_ZERO_PADDED_FILEMODE
,
737 "contains zero-padded file modes");
739 retval
+= report(options
, tree_oid
, OBJ_TREE
,
740 FSCK_MSG_BAD_FILEMODE
,
741 "contains bad file modes");
743 retval
+= report(options
, tree_oid
, OBJ_TREE
,
744 FSCK_MSG_DUPLICATE_ENTRIES
,
745 "contains duplicate file entries");
746 if (not_properly_sorted
)
747 retval
+= report(options
, tree_oid
, OBJ_TREE
,
748 FSCK_MSG_TREE_NOT_SORTED
,
749 "not properly sorted");
754 * Confirm that the headers of a commit or tag object end in a reasonable way,
755 * either with the usual "\n\n" separator, or at least with a trailing newline
756 * on the final header line.
758 * This property is important for the memory safety of our callers. It allows
759 * them to scan the buffer linewise without constantly checking the remaining
762 * - they check that there are bytes left in the buffer at the start of any
763 * line (i.e., that the last newline they saw was not the final one we
766 * - any intra-line scanning they do will stop at a newline, which will worst
767 * case hit the newline we found here as the end-of-header. This makes it
768 * OK for them to use helpers like parse_oid_hex(), or even skip_prefix().
770 static int verify_headers(const void *data
, unsigned long size
,
771 const struct object_id
*oid
, enum object_type type
,
772 struct fsck_options
*options
)
774 const char *buffer
= (const char *)data
;
777 for (i
= 0; i
< size
; i
++) {
780 return report(options
, oid
, type
,
781 FSCK_MSG_NUL_IN_HEADER
,
782 "unterminated header: NUL at offset %ld", i
);
784 if (i
+ 1 < size
&& buffer
[i
+ 1] == '\n')
790 * We did not find double-LF that separates the header
791 * and the body. Not having a body is not a crime but
792 * we do want to see the terminating LF for the last header
795 if (size
&& buffer
[size
- 1] == '\n')
798 return report(options
, oid
, type
,
799 FSCK_MSG_UNTERMINATED_HEADER
, "unterminated header");
802 static int fsck_ident(const char **ident
,
803 const struct object_id
*oid
, enum object_type type
,
804 struct fsck_options
*options
)
806 const char *p
= *ident
;
809 *ident
= strchrnul(*ident
, '\n');
814 return report(options
, oid
, type
, FSCK_MSG_MISSING_NAME_BEFORE_EMAIL
, "invalid author/committer line - missing space before email");
815 p
+= strcspn(p
, "<>\n");
817 return report(options
, oid
, type
, FSCK_MSG_BAD_NAME
, "invalid author/committer line - bad name");
819 return report(options
, oid
, type
, FSCK_MSG_MISSING_EMAIL
, "invalid author/committer line - missing email");
821 return report(options
, oid
, type
, FSCK_MSG_MISSING_SPACE_BEFORE_EMAIL
, "invalid author/committer line - missing space before email");
823 p
+= strcspn(p
, "<>\n");
825 return report(options
, oid
, type
, FSCK_MSG_BAD_EMAIL
, "invalid author/committer line - bad email");
828 return report(options
, oid
, type
, FSCK_MSG_MISSING_SPACE_BEFORE_DATE
, "invalid author/committer line - missing space before date");
831 * Our timestamp parser is based on the C strto*() functions, which
832 * will happily eat whitespace, including the newline that is supposed
833 * to prevent us walking past the end of the buffer. So do our own
834 * scan, skipping linear whitespace but not newlines, and then
835 * confirming we found a digit. We _could_ be even more strict here,
836 * as we really expect only a single space, but since we have
837 * traditionally allowed extra whitespace, we'll continue to do so.
839 while (*p
== ' ' || *p
== '\t')
842 return report(options
, oid
, type
, FSCK_MSG_BAD_DATE
,
843 "invalid author/committer line - bad date");
844 if (*p
== '0' && p
[1] != ' ')
845 return report(options
, oid
, type
, FSCK_MSG_ZERO_PADDED_DATE
, "invalid author/committer line - zero-padded date");
846 if (date_overflows(parse_timestamp(p
, &end
, 10)))
847 return report(options
, oid
, type
, FSCK_MSG_BAD_DATE_OVERFLOW
, "invalid author/committer line - date causes integer overflow");
848 if ((end
== p
|| *end
!= ' '))
849 return report(options
, oid
, type
, FSCK_MSG_BAD_DATE
, "invalid author/committer line - bad date");
851 if ((*p
!= '+' && *p
!= '-') ||
857 return report(options
, oid
, type
, FSCK_MSG_BAD_TIMEZONE
, "invalid author/committer line - bad time zone");
862 static int fsck_commit(const struct object_id
*oid
,
863 const char *buffer
, unsigned long size
,
864 struct fsck_options
*options
)
866 struct object_id tree_oid
, parent_oid
;
867 unsigned author_count
;
869 const char *buffer_begin
= buffer
;
870 const char *buffer_end
= buffer
+ size
;
874 * We _must_ stop parsing immediately if this reports failure, as the
875 * memory safety of the rest of the function depends on it. See the
876 * comment above the definition of verify_headers() for more details.
878 if (verify_headers(buffer
, size
, oid
, OBJ_COMMIT
, options
))
881 if (buffer
>= buffer_end
|| !skip_prefix(buffer
, "tree ", &buffer
))
882 return report(options
, oid
, OBJ_COMMIT
, FSCK_MSG_MISSING_TREE
, "invalid format - expected 'tree' line");
883 if (parse_oid_hex(buffer
, &tree_oid
, &p
) || *p
!= '\n') {
884 err
= report(options
, oid
, OBJ_COMMIT
, FSCK_MSG_BAD_TREE_SHA1
, "invalid 'tree' line format - bad sha1");
889 while (buffer
< buffer_end
&& skip_prefix(buffer
, "parent ", &buffer
)) {
890 if (parse_oid_hex(buffer
, &parent_oid
, &p
) || *p
!= '\n') {
891 err
= report(options
, oid
, OBJ_COMMIT
, FSCK_MSG_BAD_PARENT_SHA1
, "invalid 'parent' line format - bad sha1");
898 while (buffer
< buffer_end
&& skip_prefix(buffer
, "author ", &buffer
)) {
900 err
= fsck_ident(&buffer
, oid
, OBJ_COMMIT
, options
);
904 if (author_count
< 1)
905 err
= report(options
, oid
, OBJ_COMMIT
, FSCK_MSG_MISSING_AUTHOR
, "invalid format - expected 'author' line");
906 else if (author_count
> 1)
907 err
= report(options
, oid
, OBJ_COMMIT
, FSCK_MSG_MULTIPLE_AUTHORS
, "invalid format - multiple 'author' lines");
910 if (buffer
>= buffer_end
|| !skip_prefix(buffer
, "committer ", &buffer
))
911 return report(options
, oid
, OBJ_COMMIT
, FSCK_MSG_MISSING_COMMITTER
, "invalid format - expected 'committer' line");
912 err
= fsck_ident(&buffer
, oid
, OBJ_COMMIT
, options
);
915 if (memchr(buffer_begin
, '\0', size
)) {
916 err
= report(options
, oid
, OBJ_COMMIT
, FSCK_MSG_NUL_IN_COMMIT
,
917 "NUL byte in the commit object body");
924 static int fsck_tag(const struct object_id
*oid
, const char *buffer
,
925 unsigned long size
, struct fsck_options
*options
)
927 struct object_id tagged_oid
;
929 return fsck_tag_standalone(oid
, buffer
, size
, options
, &tagged_oid
,
933 int fsck_tag_standalone(const struct object_id
*oid
, const char *buffer
,
934 unsigned long size
, struct fsck_options
*options
,
935 struct object_id
*tagged_oid
,
940 struct strbuf sb
= STRBUF_INIT
;
941 const char *buffer_end
= buffer
+ size
;
945 * We _must_ stop parsing immediately if this reports failure, as the
946 * memory safety of the rest of the function depends on it. See the
947 * comment above the definition of verify_headers() for more details.
949 ret
= verify_headers(buffer
, size
, oid
, OBJ_TAG
, options
);
953 if (buffer
>= buffer_end
|| !skip_prefix(buffer
, "object ", &buffer
)) {
954 ret
= report(options
, oid
, OBJ_TAG
, FSCK_MSG_MISSING_OBJECT
, "invalid format - expected 'object' line");
957 if (parse_oid_hex(buffer
, tagged_oid
, &p
) || *p
!= '\n') {
958 ret
= report(options
, oid
, OBJ_TAG
, FSCK_MSG_BAD_OBJECT_SHA1
, "invalid 'object' line format - bad sha1");
964 if (buffer
>= buffer_end
|| !skip_prefix(buffer
, "type ", &buffer
)) {
965 ret
= report(options
, oid
, OBJ_TAG
, FSCK_MSG_MISSING_TYPE_ENTRY
, "invalid format - expected 'type' line");
968 eol
= memchr(buffer
, '\n', buffer_end
- buffer
);
970 ret
= report(options
, oid
, OBJ_TAG
, FSCK_MSG_MISSING_TYPE
, "invalid format - unexpected end after 'type' line");
973 *tagged_type
= type_from_string_gently(buffer
, eol
- buffer
, 1);
974 if (*tagged_type
< 0)
975 ret
= report(options
, oid
, OBJ_TAG
, FSCK_MSG_BAD_TYPE
, "invalid 'type' value");
980 if (buffer
>= buffer_end
|| !skip_prefix(buffer
, "tag ", &buffer
)) {
981 ret
= report(options
, oid
, OBJ_TAG
, FSCK_MSG_MISSING_TAG_ENTRY
, "invalid format - expected 'tag' line");
984 eol
= memchr(buffer
, '\n', buffer_end
- buffer
);
986 ret
= report(options
, oid
, OBJ_TAG
, FSCK_MSG_MISSING_TAG
, "invalid format - unexpected end after 'type' line");
989 strbuf_addf(&sb
, "refs/tags/%.*s", (int)(eol
- buffer
), buffer
);
990 if (check_refname_format(sb
.buf
, 0)) {
991 ret
= report(options
, oid
, OBJ_TAG
,
992 FSCK_MSG_BAD_TAG_NAME
,
993 "invalid 'tag' name: %.*s",
994 (int)(eol
- buffer
), buffer
);
1000 if (buffer
>= buffer_end
|| !skip_prefix(buffer
, "tagger ", &buffer
)) {
1001 /* early tags do not contain 'tagger' lines; warn only */
1002 ret
= report(options
, oid
, OBJ_TAG
, FSCK_MSG_MISSING_TAGGER_ENTRY
, "invalid format - expected 'tagger' line");
1007 ret
= fsck_ident(&buffer
, oid
, OBJ_TAG
, options
);
1009 if (buffer
< buffer_end
&& !starts_with(buffer
, "\n")) {
1011 * The verify_headers() check will allow
1012 * e.g. "[...]tagger <tagger>\nsome
1013 * garbage\n\nmessage" to pass, thinking "some
1014 * garbage" could be a custom header. E.g. "mktag"
1015 * doesn't want any unknown headers.
1017 ret
= report(options
, oid
, OBJ_TAG
, FSCK_MSG_EXTRA_HEADER_ENTRY
, "invalid format - extra header(s) after 'tagger'");
1023 strbuf_release(&sb
);
1027 static int starts_with_dot_slash(const char *const path
)
1029 return path_match_flags(path
, PATH_MATCH_STARTS_WITH_DOT_SLASH
|
1030 PATH_MATCH_XPLATFORM
);
1033 static int starts_with_dot_dot_slash(const char *const path
)
1035 return path_match_flags(path
, PATH_MATCH_STARTS_WITH_DOT_DOT_SLASH
|
1036 PATH_MATCH_XPLATFORM
);
1039 static int submodule_url_is_relative(const char *url
)
1041 return starts_with_dot_slash(url
) || starts_with_dot_dot_slash(url
);
1045 * Count directory components that a relative submodule URL should chop
1046 * from the remote_url it is to be resolved against.
1048 * In other words, this counts "../" components at the start of a
1051 * Returns the number of directory components to chop and writes a
1052 * pointer to the next character of url after all leading "./" and
1053 * "../" components to out.
1055 static int count_leading_dotdots(const char *url
, const char **out
)
1059 if (starts_with_dot_dot_slash(url
)) {
1061 url
+= strlen("../");
1064 if (starts_with_dot_slash(url
)) {
1065 url
+= strlen("./");
1073 * Check whether a transport is implemented by git-remote-curl.
1075 * If it is, returns 1 and writes the URL that would be passed to
1076 * git-remote-curl to the "out" parameter.
1078 * Otherwise, returns 0 and leaves "out" untouched.
1081 * http::https://example.com/repo.git -> 1, https://example.com/repo.git
1082 * https://example.com/repo.git -> 1, https://example.com/repo.git
1083 * git://example.com/repo.git -> 0
1085 * This is for use in checking for previously exploitable bugs that
1086 * required a submodule URL to be passed to git-remote-curl.
1088 static int url_to_curl_url(const char *url
, const char **out
)
1091 * We don't need to check for case-aliases, "http.exe", and so
1092 * on because in the default configuration, is_transport_allowed
1093 * prevents URLs with those schemes from being cloned
1096 if (skip_prefix(url
, "http::", out
) ||
1097 skip_prefix(url
, "https::", out
) ||
1098 skip_prefix(url
, "ftp::", out
) ||
1099 skip_prefix(url
, "ftps::", out
))
1101 if (starts_with(url
, "http://") ||
1102 starts_with(url
, "https://") ||
1103 starts_with(url
, "ftp://") ||
1104 starts_with(url
, "ftps://")) {
1111 static int check_submodule_url(const char *url
)
1113 const char *curl_url
;
1115 if (looks_like_command_line_option(url
))
1118 if (submodule_url_is_relative(url
) || starts_with(url
, "git://")) {
1124 * This could be appended to an http URL and url-decoded;
1125 * check for malicious characters.
1127 decoded
= url_decode(url
);
1128 has_nl
= !!strchr(decoded
, '\n');
1135 * URLs which escape their root via "../" can overwrite
1136 * the host field and previous components, resolving to
1137 * URLs like https::example.com/submodule.git and
1138 * https:///example.com/submodule.git that were
1139 * susceptible to CVE-2020-11008.
1141 if (count_leading_dotdots(url
, &next
) > 0 &&
1142 (*next
== ':' || *next
== '/'))
1146 else if (url_to_curl_url(url
, &curl_url
)) {
1147 struct credential c
= CREDENTIAL_INIT
;
1149 if (credential_from_url_gently(&c
, curl_url
, 1) ||
1152 credential_clear(&c
);
1159 struct fsck_gitmodules_data
{
1160 const struct object_id
*oid
;
1161 struct fsck_options
*options
;
1165 static int fsck_gitmodules_fn(const char *var
, const char *value
, void *vdata
)
1167 struct fsck_gitmodules_data
*data
= vdata
;
1168 const char *subsection
, *key
;
1169 size_t subsection_len
;
1172 if (parse_config_key(var
, "submodule", &subsection
, &subsection_len
, &key
) < 0 ||
1176 name
= xmemdupz(subsection
, subsection_len
);
1177 if (check_submodule_name(name
) < 0)
1178 data
->ret
|= report(data
->options
,
1179 data
->oid
, OBJ_BLOB
,
1180 FSCK_MSG_GITMODULES_NAME
,
1181 "disallowed submodule name: %s",
1183 if (!strcmp(key
, "url") && value
&&
1184 check_submodule_url(value
) < 0)
1185 data
->ret
|= report(data
->options
,
1186 data
->oid
, OBJ_BLOB
,
1187 FSCK_MSG_GITMODULES_URL
,
1188 "disallowed submodule url: %s",
1190 if (!strcmp(key
, "path") && value
&&
1191 looks_like_command_line_option(value
))
1192 data
->ret
|= report(data
->options
,
1193 data
->oid
, OBJ_BLOB
,
1194 FSCK_MSG_GITMODULES_PATH
,
1195 "disallowed submodule path: %s",
1197 if (!strcmp(key
, "update") && value
&&
1198 parse_submodule_update_type(value
) == SM_UPDATE_COMMAND
)
1199 data
->ret
|= report(data
->options
, data
->oid
, OBJ_BLOB
,
1200 FSCK_MSG_GITMODULES_UPDATE
,
1201 "disallowed submodule update setting: %s",
1208 static int fsck_blob(const struct object_id
*oid
, const char *buf
,
1209 unsigned long size
, struct fsck_options
*options
)
1213 if (object_on_skiplist(options
, oid
))
1216 if (oidset_contains(&options
->gitmodules_found
, oid
)) {
1217 struct config_options config_opts
= { 0 };
1218 struct fsck_gitmodules_data data
;
1220 oidset_insert(&options
->gitmodules_done
, oid
);
1224 * A missing buffer here is a sign that the caller found the
1225 * blob too gigantic to load into memory. Let's just consider
1228 return report(options
, oid
, OBJ_BLOB
,
1229 FSCK_MSG_GITMODULES_LARGE
,
1230 ".gitmodules too large to parse");
1234 data
.options
= options
;
1236 config_opts
.error_action
= CONFIG_ERROR_SILENT
;
1237 if (git_config_from_mem(fsck_gitmodules_fn
, CONFIG_ORIGIN_BLOB
,
1238 ".gitmodules", buf
, size
, &data
, &config_opts
))
1239 data
.ret
|= report(options
, oid
, OBJ_BLOB
,
1240 FSCK_MSG_GITMODULES_PARSE
,
1241 "could not parse gitmodules blob");
1245 if (oidset_contains(&options
->gitattributes_found
, oid
)) {
1248 oidset_insert(&options
->gitattributes_done
, oid
);
1250 if (!buf
|| size
> ATTR_MAX_FILE_SIZE
) {
1252 * A missing buffer here is a sign that the caller found the
1253 * blob too gigantic to load into memory. Let's just consider
1256 return report(options
, oid
, OBJ_BLOB
,
1257 FSCK_MSG_GITATTRIBUTES_LARGE
,
1258 ".gitattributes too large to parse");
1261 for (ptr
= buf
; *ptr
; ) {
1262 const char *eol
= strchrnul(ptr
, '\n');
1263 if (eol
- ptr
>= ATTR_MAX_LINE_LENGTH
) {
1264 ret
|= report(options
, oid
, OBJ_BLOB
,
1265 FSCK_MSG_GITATTRIBUTES_LINE_LENGTH
,
1266 ".gitattributes has too long lines to parse");
1270 ptr
= *eol
? eol
+ 1 : eol
;
1277 int fsck_object(struct object
*obj
, void *data
, unsigned long size
,
1278 struct fsck_options
*options
)
1281 return report(options
, NULL
, OBJ_NONE
, FSCK_MSG_BAD_OBJECT_SHA1
, "no valid object to fsck");
1283 return fsck_buffer(&obj
->oid
, obj
->type
, data
, size
, options
);
1286 int fsck_buffer(const struct object_id
*oid
, enum object_type type
,
1287 void *data
, unsigned long size
,
1288 struct fsck_options
*options
)
1290 if (type
== OBJ_BLOB
)
1291 return fsck_blob(oid
, data
, size
, options
);
1292 if (type
== OBJ_TREE
)
1293 return fsck_tree(oid
, data
, size
, options
);
1294 if (type
== OBJ_COMMIT
)
1295 return fsck_commit(oid
, data
, size
, options
);
1296 if (type
== OBJ_TAG
)
1297 return fsck_tag(oid
, data
, size
, options
);
1299 return report(options
, oid
, type
,
1300 FSCK_MSG_UNKNOWN_TYPE
,
1301 "unknown type '%d' (internal fsck error)",
1305 int fsck_error_function(struct fsck_options
*o
,
1306 const struct object_id
*oid
,
1307 enum object_type object_type
,
1308 enum fsck_msg_type msg_type
,
1309 enum fsck_msg_id msg_id
,
1310 const char *message
)
1312 if (msg_type
== FSCK_WARN
) {
1313 warning("object %s: %s", fsck_describe_object(o
, oid
), message
);
1316 error("object %s: %s", fsck_describe_object(o
, oid
), message
);
1320 static int fsck_blobs(struct oidset
*blobs_found
, struct oidset
*blobs_done
,
1321 enum fsck_msg_id msg_missing
, enum fsck_msg_id msg_type
,
1322 struct fsck_options
*options
, const char *blob_type
)
1325 struct oidset_iter iter
;
1326 const struct object_id
*oid
;
1328 oidset_iter_init(blobs_found
, &iter
);
1329 while ((oid
= oidset_iter_next(&iter
))) {
1330 enum object_type type
;
1334 if (oidset_contains(blobs_done
, oid
))
1337 buf
= read_object_file(oid
, &type
, &size
);
1339 if (is_promisor_object(oid
))
1341 ret
|= report(options
,
1342 oid
, OBJ_BLOB
, msg_missing
,
1343 "unable to read %s blob", blob_type
);
1347 if (type
== OBJ_BLOB
)
1348 ret
|= fsck_blob(oid
, buf
, size
, options
);
1350 ret
|= report(options
, oid
, type
, msg_type
,
1351 "non-blob found at %s", blob_type
);
1355 oidset_clear(blobs_found
);
1356 oidset_clear(blobs_done
);
1361 int fsck_finish(struct fsck_options
*options
)
1365 ret
|= fsck_blobs(&options
->gitmodules_found
, &options
->gitmodules_done
,
1366 FSCK_MSG_GITMODULES_MISSING
, FSCK_MSG_GITMODULES_BLOB
,
1367 options
, ".gitmodules");
1368 ret
|= fsck_blobs(&options
->gitattributes_found
, &options
->gitattributes_done
,
1369 FSCK_MSG_GITATTRIBUTES_MISSING
, FSCK_MSG_GITATTRIBUTES_BLOB
,
1370 options
, ".gitattributes");
1375 int git_fsck_config(const char *var
, const char *value
, void *cb
)
1377 struct fsck_options
*options
= cb
;
1378 if (strcmp(var
, "fsck.skiplist") == 0) {
1380 struct strbuf sb
= STRBUF_INIT
;
1382 if (git_config_pathname(&path
, var
, value
))
1384 strbuf_addf(&sb
, "skiplist=%s", path
);
1386 fsck_set_msg_types(options
, sb
.buf
);
1387 strbuf_release(&sb
);
1391 if (skip_prefix(var
, "fsck.", &var
)) {
1392 fsck_set_msg_type(options
, var
, value
);
1396 return git_default_config(var
, value
, cb
);
1400 * Custom error callbacks that are used in more than one place.
1403 int fsck_error_cb_print_missing_gitmodules(struct fsck_options
*o
,
1404 const struct object_id
*oid
,
1405 enum object_type object_type
,
1406 enum fsck_msg_type msg_type
,
1407 enum fsck_msg_id msg_id
,
1408 const char *message
)
1410 if (msg_id
== FSCK_MSG_GITMODULES_MISSING
) {
1411 puts(oid_to_hex(oid
));
1414 return fsck_error_function(o
, oid
, object_type
, msg_type
, msg_id
, message
);