1 #include "git-compat-util.h"
5 #include "object-store-ll.h"
7 #include "repository.h"
12 #include "tree-walk.h"
22 #include "submodule-config.h"
24 #include "credential.h"
28 #define MSG_ID(id, msg_type) { STR(id), NULL, NULL, FSCK_##msg_type },
30 const char *id_string
;
31 const char *downcased
;
32 const char *camelcased
;
33 enum fsck_msg_type msg_type
;
34 } msg_id_info
[FSCK_MSG_MAX
+ 1] = {
35 FOREACH_FSCK_MSG_ID(MSG_ID
)
36 { NULL
, NULL
, NULL
, -1 }
41 static void prepare_msg_ids(void)
45 if (msg_id_info
[0].downcased
)
48 /* convert id_string to lower case, without underscores. */
49 for (i
= 0; i
< FSCK_MSG_MAX
; i
++) {
50 const char *p
= msg_id_info
[i
].id_string
;
52 char *q
= xmalloc(len
);
54 msg_id_info
[i
].downcased
= q
;
59 *(q
)++ = tolower(*(p
)++);
62 p
= msg_id_info
[i
].id_string
;
64 msg_id_info
[i
].camelcased
= q
;
78 static int parse_msg_id(const char *text
)
84 for (i
= 0; i
< FSCK_MSG_MAX
; i
++)
85 if (!strcmp(text
, msg_id_info
[i
].downcased
))
91 void list_config_fsck_msg_ids(struct string_list
*list
, const char *prefix
)
97 for (i
= 0; i
< FSCK_MSG_MAX
; i
++)
98 list_config_item(list
, prefix
, msg_id_info
[i
].camelcased
);
101 static enum fsck_msg_type
fsck_msg_type(enum fsck_msg_id msg_id
,
102 struct fsck_options
*options
)
104 assert(msg_id
>= 0 && msg_id
< FSCK_MSG_MAX
);
106 if (!options
->msg_type
) {
107 enum fsck_msg_type msg_type
= msg_id_info
[msg_id
].msg_type
;
109 if (options
->strict
&& msg_type
== FSCK_WARN
)
110 msg_type
= FSCK_ERROR
;
114 return options
->msg_type
[msg_id
];
117 static enum fsck_msg_type
parse_msg_type(const char *str
)
119 if (!strcmp(str
, "error"))
121 else if (!strcmp(str
, "warn"))
123 else if (!strcmp(str
, "ignore"))
126 die("Unknown fsck message type: '%s'", str
);
129 int is_valid_msg_type(const char *msg_id
, const char *msg_type
)
131 if (parse_msg_id(msg_id
) < 0)
133 parse_msg_type(msg_type
);
137 void fsck_set_msg_type_from_ids(struct fsck_options
*options
,
138 enum fsck_msg_id msg_id
,
139 enum fsck_msg_type msg_type
)
141 if (!options
->msg_type
) {
143 enum fsck_msg_type
*severity
;
144 ALLOC_ARRAY(severity
, FSCK_MSG_MAX
);
145 for (i
= 0; i
< FSCK_MSG_MAX
; i
++)
146 severity
[i
] = fsck_msg_type(i
, options
);
147 options
->msg_type
= severity
;
150 options
->msg_type
[msg_id
] = msg_type
;
153 void fsck_set_msg_type(struct fsck_options
*options
,
154 const char *msg_id_str
, const char *msg_type_str
)
156 int msg_id
= parse_msg_id(msg_id_str
);
157 enum fsck_msg_type msg_type
= parse_msg_type(msg_type_str
);
160 die("Unhandled message id: %s", msg_id_str
);
162 if (msg_type
!= FSCK_ERROR
&& msg_id_info
[msg_id
].msg_type
== FSCK_FATAL
)
163 die("Cannot demote %s to %s", msg_id_str
, msg_type_str
);
165 fsck_set_msg_type_from_ids(options
, msg_id
, msg_type
);
168 void fsck_set_msg_types(struct fsck_options
*options
, const char *values
)
170 char *buf
= xstrdup(values
), *to_free
= buf
;
174 int len
= strcspn(buf
, " ,|"), equal
;
184 equal
< len
&& buf
[equal
] != '=' && buf
[equal
] != ':';
186 buf
[equal
] = tolower(buf
[equal
]);
189 if (!strcmp(buf
, "skiplist")) {
191 die("skiplist requires a path");
192 oidset_parse_file(&options
->skiplist
, buf
+ equal
+ 1);
198 die("Missing '=': '%s'", buf
);
200 fsck_set_msg_type(options
, buf
, buf
+ equal
+ 1);
206 static int object_on_skiplist(struct fsck_options
*opts
,
207 const struct object_id
*oid
)
209 return opts
&& oid
&& oidset_contains(&opts
->skiplist
, oid
);
212 __attribute__((format (printf
, 5, 6)))
213 static int report(struct fsck_options
*options
,
214 const struct object_id
*oid
, enum object_type object_type
,
215 enum fsck_msg_id msg_id
, const char *fmt
, ...)
218 struct strbuf sb
= STRBUF_INIT
;
219 enum fsck_msg_type msg_type
= fsck_msg_type(msg_id
, options
);
222 if (msg_type
== FSCK_IGNORE
)
225 if (object_on_skiplist(options
, oid
))
228 if (msg_type
== FSCK_FATAL
)
229 msg_type
= FSCK_ERROR
;
230 else if (msg_type
== FSCK_INFO
)
231 msg_type
= FSCK_WARN
;
234 strbuf_addf(&sb
, "%s: ", msg_id_info
[msg_id
].camelcased
);
237 strbuf_vaddf(&sb
, fmt
, ap
);
238 result
= options
->error_func(options
, oid
, object_type
,
239 msg_type
, msg_id
, sb
.buf
);
246 void fsck_enable_object_names(struct fsck_options
*options
)
248 if (!options
->object_names
)
249 options
->object_names
= kh_init_oid_map();
252 const char *fsck_get_object_name(struct fsck_options
*options
,
253 const struct object_id
*oid
)
256 if (!options
->object_names
)
258 pos
= kh_get_oid_map(options
->object_names
, *oid
);
259 if (pos
>= kh_end(options
->object_names
))
261 return kh_value(options
->object_names
, pos
);
264 void fsck_put_object_name(struct fsck_options
*options
,
265 const struct object_id
*oid
,
266 const char *fmt
, ...)
269 struct strbuf buf
= STRBUF_INIT
;
273 if (!options
->object_names
)
276 pos
= kh_put_oid_map(options
->object_names
, *oid
, &hashret
);
280 strbuf_vaddf(&buf
, fmt
, ap
);
281 kh_value(options
->object_names
, pos
) = strbuf_detach(&buf
, NULL
);
285 const char *fsck_describe_object(struct fsck_options
*options
,
286 const struct object_id
*oid
)
288 static struct strbuf bufs
[] = {
289 STRBUF_INIT
, STRBUF_INIT
, STRBUF_INIT
, STRBUF_INIT
293 const char *name
= fsck_get_object_name(options
, oid
);
296 b
= (b
+ 1) % ARRAY_SIZE(bufs
);
298 strbuf_addstr(buf
, oid_to_hex(oid
));
300 strbuf_addf(buf
, " (%s)", name
);
305 static int fsck_walk_tree(struct tree
*tree
, void *data
, struct fsck_options
*options
)
307 struct tree_desc desc
;
308 struct name_entry entry
;
312 if (parse_tree(tree
))
315 name
= fsck_get_object_name(options
, &tree
->object
.oid
);
316 if (init_tree_desc_gently(&desc
, tree
->buffer
, tree
->size
, 0))
318 while (tree_entry_gently(&desc
, &entry
)) {
322 if (S_ISGITLINK(entry
.mode
))
325 if (S_ISDIR(entry
.mode
)) {
326 obj
= (struct object
*)lookup_tree(the_repository
, &entry
.oid
);
328 fsck_put_object_name(options
, &entry
.oid
, "%s%s/",
330 result
= options
->walk(obj
, OBJ_TREE
, data
, options
);
332 else if (S_ISREG(entry
.mode
) || S_ISLNK(entry
.mode
)) {
333 obj
= (struct object
*)lookup_blob(the_repository
, &entry
.oid
);
335 fsck_put_object_name(options
, &entry
.oid
, "%s%s",
337 result
= options
->walk(obj
, OBJ_BLOB
, data
, options
);
340 result
= error("in tree %s: entry %s has bad mode %.6o",
341 fsck_describe_object(options
, &tree
->object
.oid
),
342 entry
.path
, entry
.mode
);
352 static int fsck_walk_commit(struct commit
*commit
, void *data
, struct fsck_options
*options
)
354 int counter
= 0, generation
= 0, name_prefix_len
= 0;
355 struct commit_list
*parents
;
360 if (repo_parse_commit(the_repository
, commit
))
363 name
= fsck_get_object_name(options
, &commit
->object
.oid
);
365 fsck_put_object_name(options
, get_commit_tree_oid(commit
),
368 result
= options
->walk((struct object
*) repo_get_commit_tree(the_repository
, commit
),
369 OBJ_TREE
, data
, options
);
374 parents
= commit
->parents
;
375 if (name
&& parents
) {
376 int len
= strlen(name
), power
;
378 if (len
&& name
[len
- 1] == '^') {
380 name_prefix_len
= len
- 1;
382 else { /* parse ~<generation> suffix */
383 for (generation
= 0, power
= 1;
384 len
&& isdigit(name
[len
- 1]);
386 generation
+= power
* (name
[--len
] - '0');
387 if (power
> 1 && len
&& name
[len
- 1] == '~')
388 name_prefix_len
= len
- 1;
390 /* Maybe a non-first parent, e.g. HEAD^2 */
392 name_prefix_len
= len
;
399 struct object_id
*oid
= &parents
->item
->object
.oid
;
402 fsck_put_object_name(options
, oid
, "%s^%d",
404 else if (generation
> 0)
405 fsck_put_object_name(options
, oid
, "%.*s~%d",
406 name_prefix_len
, name
,
409 fsck_put_object_name(options
, oid
, "%s^", name
);
411 result
= options
->walk((struct object
*)parents
->item
, OBJ_COMMIT
, data
, options
);
416 parents
= parents
->next
;
421 static int fsck_walk_tag(struct tag
*tag
, void *data
, struct fsck_options
*options
)
423 const char *name
= fsck_get_object_name(options
, &tag
->object
.oid
);
428 fsck_put_object_name(options
, &tag
->tagged
->oid
, "%s", name
);
429 return options
->walk(tag
->tagged
, OBJ_ANY
, data
, options
);
432 int fsck_walk(struct object
*obj
, void *data
, struct fsck_options
*options
)
437 if (obj
->type
== OBJ_NONE
)
438 parse_object(the_repository
, &obj
->oid
);
444 return fsck_walk_tree((struct tree
*)obj
, data
, options
);
446 return fsck_walk_commit((struct commit
*)obj
, data
, options
);
448 return fsck_walk_tag((struct tag
*)obj
, data
, options
);
450 error("Unknown object type for %s",
451 fsck_describe_object(options
, &obj
->oid
));
461 static void name_stack_push(struct name_stack
*stack
, const char *name
)
463 ALLOC_GROW(stack
->names
, stack
->nr
+ 1, stack
->alloc
);
464 stack
->names
[stack
->nr
++] = name
;
467 static const char *name_stack_pop(struct name_stack
*stack
)
469 return stack
->nr
? stack
->names
[--stack
->nr
] : NULL
;
472 static void name_stack_clear(struct name_stack
*stack
)
474 FREE_AND_NULL(stack
->names
);
475 stack
->nr
= stack
->alloc
= 0;
479 * The entries in a tree are ordered in the _path_ order,
480 * which means that a directory entry is ordered by adding
481 * a slash to the end of it.
483 * So a directory called "a" is ordered _after_ a file
484 * called "a.c", because "a/" sorts after "a.c".
486 #define TREE_UNORDERED (-1)
487 #define TREE_HAS_DUPS (-2)
489 static int is_less_than_slash(unsigned char c
)
491 return '\0' < c
&& c
< '/';
494 static int verify_ordered(unsigned mode1
, const char *name1
,
495 unsigned mode2
, const char *name2
,
496 struct name_stack
*candidates
)
498 int len1
= strlen(name1
);
499 int len2
= strlen(name2
);
500 int len
= len1
< len2
? len1
: len2
;
501 unsigned char c1
, c2
;
504 cmp
= memcmp(name1
, name2
, len
);
508 return TREE_UNORDERED
;
511 * Ok, the first <len> characters are the same.
512 * Now we need to order the next one, but turn
513 * a '\0' into a '/' for a directory entry.
519 * git-write-tree used to write out a nonsense tree that has
520 * entries with the same name, one blob and one tree. Make
521 * sure we do not have duplicate entries.
523 return TREE_HAS_DUPS
;
524 if (!c1
&& S_ISDIR(mode1
))
526 if (!c2
&& S_ISDIR(mode2
))
530 * There can be non-consecutive duplicates due to the implicitly
539 * Record non-directory candidates (like "foo" and "foo.bar" in
540 * the example) on a stack and check directory candidates (like
541 * foo/" and "foo.bar/") against that stack.
543 if (!c1
&& is_less_than_slash(c2
)) {
544 name_stack_push(candidates
, name1
);
545 } else if (c2
== '/' && is_less_than_slash(c1
)) {
548 const char *f_name
= name_stack_pop(candidates
);
552 if (!skip_prefix(name2
, f_name
, &p
))
555 return TREE_HAS_DUPS
;
556 if (is_less_than_slash(*p
)) {
557 name_stack_push(candidates
, f_name
);
563 return c1
< c2
? 0 : TREE_UNORDERED
;
566 static int fsck_tree(const struct object_id
*tree_oid
,
567 const char *buffer
, unsigned long size
,
568 struct fsck_options
*options
)
571 int has_null_sha1
= 0;
572 int has_full_path
= 0;
573 int has_empty_name
= 0;
577 int has_zero_pad
= 0;
578 int has_bad_modes
= 0;
579 int has_dup_entries
= 0;
580 int not_properly_sorted
= 0;
581 struct tree_desc desc
;
584 struct name_stack df_dup_candidates
= { NULL
};
586 if (init_tree_desc_gently(&desc
, buffer
, size
, TREE_DESC_RAW_MODES
)) {
587 retval
+= report(options
, tree_oid
, OBJ_TREE
,
589 "cannot be parsed as a tree");
598 const char *name
, *backslash
;
599 const struct object_id
*entry_oid
;
601 entry_oid
= tree_entry_extract(&desc
, &name
, &mode
);
603 has_null_sha1
|= is_null_oid(entry_oid
);
604 has_full_path
|= !!strchr(name
, '/');
605 has_empty_name
|= !*name
;
606 has_dot
|= !strcmp(name
, ".");
607 has_dotdot
|= !strcmp(name
, "..");
608 has_dotgit
|= is_hfs_dotgit(name
) || is_ntfs_dotgit(name
);
609 has_zero_pad
|= *(char *)desc
.buffer
== '0';
611 if (is_hfs_dotgitmodules(name
) || is_ntfs_dotgitmodules(name
)) {
613 oidset_insert(&options
->gitmodules_found
,
616 retval
+= report(options
,
618 FSCK_MSG_GITMODULES_SYMLINK
,
619 ".gitmodules is a symbolic link");
622 if (is_hfs_dotgitattributes(name
) || is_ntfs_dotgitattributes(name
)) {
624 oidset_insert(&options
->gitattributes_found
,
627 retval
+= report(options
, tree_oid
, OBJ_TREE
,
628 FSCK_MSG_GITATTRIBUTES_SYMLINK
,
629 ".gitattributes is a symlink");
633 if (is_hfs_dotgitignore(name
) ||
634 is_ntfs_dotgitignore(name
))
635 retval
+= report(options
, tree_oid
, OBJ_TREE
,
636 FSCK_MSG_GITIGNORE_SYMLINK
,
637 ".gitignore is a symlink");
638 if (is_hfs_dotmailmap(name
) ||
639 is_ntfs_dotmailmap(name
))
640 retval
+= report(options
, tree_oid
, OBJ_TREE
,
641 FSCK_MSG_MAILMAP_SYMLINK
,
642 ".mailmap is a symlink");
645 if ((backslash
= strchr(name
, '\\'))) {
648 has_dotgit
|= is_ntfs_dotgit(backslash
);
649 if (is_ntfs_dotgitmodules(backslash
)) {
651 oidset_insert(&options
->gitmodules_found
,
654 retval
+= report(options
, tree_oid
, OBJ_TREE
,
655 FSCK_MSG_GITMODULES_SYMLINK
,
656 ".gitmodules is a symbolic link");
658 backslash
= strchr(backslash
, '\\');
662 if (update_tree_entry_gently(&desc
)) {
663 retval
+= report(options
, tree_oid
, OBJ_TREE
,
665 "cannot be parsed as a tree");
680 * This is nonstandard, but we had a few of these
681 * early on when we honored the full set of mode
685 if (!options
->strict
)
693 switch (verify_ordered(o_mode
, o_name
, mode
, name
,
694 &df_dup_candidates
)) {
696 not_properly_sorted
= 1;
710 name_stack_clear(&df_dup_candidates
);
713 retval
+= report(options
, tree_oid
, OBJ_TREE
,
715 "contains entries pointing to null sha1");
717 retval
+= report(options
, tree_oid
, OBJ_TREE
,
718 FSCK_MSG_FULL_PATHNAME
,
719 "contains full pathnames");
721 retval
+= report(options
, tree_oid
, OBJ_TREE
,
723 "contains empty pathname");
725 retval
+= report(options
, tree_oid
, OBJ_TREE
,
729 retval
+= report(options
, tree_oid
, OBJ_TREE
,
733 retval
+= report(options
, tree_oid
, OBJ_TREE
,
737 retval
+= report(options
, tree_oid
, OBJ_TREE
,
738 FSCK_MSG_ZERO_PADDED_FILEMODE
,
739 "contains zero-padded file modes");
741 retval
+= report(options
, tree_oid
, OBJ_TREE
,
742 FSCK_MSG_BAD_FILEMODE
,
743 "contains bad file modes");
745 retval
+= report(options
, tree_oid
, OBJ_TREE
,
746 FSCK_MSG_DUPLICATE_ENTRIES
,
747 "contains duplicate file entries");
748 if (not_properly_sorted
)
749 retval
+= report(options
, tree_oid
, OBJ_TREE
,
750 FSCK_MSG_TREE_NOT_SORTED
,
751 "not properly sorted");
756 * Confirm that the headers of a commit or tag object end in a reasonable way,
757 * either with the usual "\n\n" separator, or at least with a trailing newline
758 * on the final header line.
760 * This property is important for the memory safety of our callers. It allows
761 * them to scan the buffer linewise without constantly checking the remaining
764 * - they check that there are bytes left in the buffer at the start of any
765 * line (i.e., that the last newline they saw was not the final one we
768 * - any intra-line scanning they do will stop at a newline, which will worst
769 * case hit the newline we found here as the end-of-header. This makes it
770 * OK for them to use helpers like parse_oid_hex(), or even skip_prefix().
772 static int verify_headers(const void *data
, unsigned long size
,
773 const struct object_id
*oid
, enum object_type type
,
774 struct fsck_options
*options
)
776 const char *buffer
= (const char *)data
;
779 for (i
= 0; i
< size
; i
++) {
782 return report(options
, oid
, type
,
783 FSCK_MSG_NUL_IN_HEADER
,
784 "unterminated header: NUL at offset %ld", i
);
786 if (i
+ 1 < size
&& buffer
[i
+ 1] == '\n')
792 * We did not find double-LF that separates the header
793 * and the body. Not having a body is not a crime but
794 * we do want to see the terminating LF for the last header
797 if (size
&& buffer
[size
- 1] == '\n')
800 return report(options
, oid
, type
,
801 FSCK_MSG_UNTERMINATED_HEADER
, "unterminated header");
804 static int fsck_ident(const char **ident
,
805 const struct object_id
*oid
, enum object_type type
,
806 struct fsck_options
*options
)
808 const char *p
= *ident
;
811 *ident
= strchrnul(*ident
, '\n');
816 return report(options
, oid
, type
, FSCK_MSG_MISSING_NAME_BEFORE_EMAIL
, "invalid author/committer line - missing space before email");
817 p
+= strcspn(p
, "<>\n");
819 return report(options
, oid
, type
, FSCK_MSG_BAD_NAME
, "invalid author/committer line - bad name");
821 return report(options
, oid
, type
, FSCK_MSG_MISSING_EMAIL
, "invalid author/committer line - missing email");
823 return report(options
, oid
, type
, FSCK_MSG_MISSING_SPACE_BEFORE_EMAIL
, "invalid author/committer line - missing space before email");
825 p
+= strcspn(p
, "<>\n");
827 return report(options
, oid
, type
, FSCK_MSG_BAD_EMAIL
, "invalid author/committer line - bad email");
830 return report(options
, oid
, type
, FSCK_MSG_MISSING_SPACE_BEFORE_DATE
, "invalid author/committer line - missing space before date");
833 * Our timestamp parser is based on the C strto*() functions, which
834 * will happily eat whitespace, including the newline that is supposed
835 * to prevent us walking past the end of the buffer. So do our own
836 * scan, skipping linear whitespace but not newlines, and then
837 * confirming we found a digit. We _could_ be even more strict here,
838 * as we really expect only a single space, but since we have
839 * traditionally allowed extra whitespace, we'll continue to do so.
841 while (*p
== ' ' || *p
== '\t')
844 return report(options
, oid
, type
, FSCK_MSG_BAD_DATE
,
845 "invalid author/committer line - bad date");
846 if (*p
== '0' && p
[1] != ' ')
847 return report(options
, oid
, type
, FSCK_MSG_ZERO_PADDED_DATE
, "invalid author/committer line - zero-padded date");
848 if (date_overflows(parse_timestamp(p
, &end
, 10)))
849 return report(options
, oid
, type
, FSCK_MSG_BAD_DATE_OVERFLOW
, "invalid author/committer line - date causes integer overflow");
850 if ((end
== p
|| *end
!= ' '))
851 return report(options
, oid
, type
, FSCK_MSG_BAD_DATE
, "invalid author/committer line - bad date");
853 if ((*p
!= '+' && *p
!= '-') ||
859 return report(options
, oid
, type
, FSCK_MSG_BAD_TIMEZONE
, "invalid author/committer line - bad time zone");
864 static int fsck_commit(const struct object_id
*oid
,
865 const char *buffer
, unsigned long size
,
866 struct fsck_options
*options
)
868 struct object_id tree_oid
, parent_oid
;
869 unsigned author_count
;
871 const char *buffer_begin
= buffer
;
872 const char *buffer_end
= buffer
+ size
;
876 * We _must_ stop parsing immediately if this reports failure, as the
877 * memory safety of the rest of the function depends on it. See the
878 * comment above the definition of verify_headers() for more details.
880 if (verify_headers(buffer
, size
, oid
, OBJ_COMMIT
, options
))
883 if (buffer
>= buffer_end
|| !skip_prefix(buffer
, "tree ", &buffer
))
884 return report(options
, oid
, OBJ_COMMIT
, FSCK_MSG_MISSING_TREE
, "invalid format - expected 'tree' line");
885 if (parse_oid_hex(buffer
, &tree_oid
, &p
) || *p
!= '\n') {
886 err
= report(options
, oid
, OBJ_COMMIT
, FSCK_MSG_BAD_TREE_SHA1
, "invalid 'tree' line format - bad sha1");
891 while (buffer
< buffer_end
&& skip_prefix(buffer
, "parent ", &buffer
)) {
892 if (parse_oid_hex(buffer
, &parent_oid
, &p
) || *p
!= '\n') {
893 err
= report(options
, oid
, OBJ_COMMIT
, FSCK_MSG_BAD_PARENT_SHA1
, "invalid 'parent' line format - bad sha1");
900 while (buffer
< buffer_end
&& skip_prefix(buffer
, "author ", &buffer
)) {
902 err
= fsck_ident(&buffer
, oid
, OBJ_COMMIT
, options
);
906 if (author_count
< 1)
907 err
= report(options
, oid
, OBJ_COMMIT
, FSCK_MSG_MISSING_AUTHOR
, "invalid format - expected 'author' line");
908 else if (author_count
> 1)
909 err
= report(options
, oid
, OBJ_COMMIT
, FSCK_MSG_MULTIPLE_AUTHORS
, "invalid format - multiple 'author' lines");
912 if (buffer
>= buffer_end
|| !skip_prefix(buffer
, "committer ", &buffer
))
913 return report(options
, oid
, OBJ_COMMIT
, FSCK_MSG_MISSING_COMMITTER
, "invalid format - expected 'committer' line");
914 err
= fsck_ident(&buffer
, oid
, OBJ_COMMIT
, options
);
917 if (memchr(buffer_begin
, '\0', size
)) {
918 err
= report(options
, oid
, OBJ_COMMIT
, FSCK_MSG_NUL_IN_COMMIT
,
919 "NUL byte in the commit object body");
926 static int fsck_tag(const struct object_id
*oid
, const char *buffer
,
927 unsigned long size
, struct fsck_options
*options
)
929 struct object_id tagged_oid
;
931 return fsck_tag_standalone(oid
, buffer
, size
, options
, &tagged_oid
,
935 int fsck_tag_standalone(const struct object_id
*oid
, const char *buffer
,
936 unsigned long size
, struct fsck_options
*options
,
937 struct object_id
*tagged_oid
,
942 struct strbuf sb
= STRBUF_INIT
;
943 const char *buffer_end
= buffer
+ size
;
947 * We _must_ stop parsing immediately if this reports failure, as the
948 * memory safety of the rest of the function depends on it. See the
949 * comment above the definition of verify_headers() for more details.
951 ret
= verify_headers(buffer
, size
, oid
, OBJ_TAG
, options
);
955 if (buffer
>= buffer_end
|| !skip_prefix(buffer
, "object ", &buffer
)) {
956 ret
= report(options
, oid
, OBJ_TAG
, FSCK_MSG_MISSING_OBJECT
, "invalid format - expected 'object' line");
959 if (parse_oid_hex(buffer
, tagged_oid
, &p
) || *p
!= '\n') {
960 ret
= report(options
, oid
, OBJ_TAG
, FSCK_MSG_BAD_OBJECT_SHA1
, "invalid 'object' line format - bad sha1");
966 if (buffer
>= buffer_end
|| !skip_prefix(buffer
, "type ", &buffer
)) {
967 ret
= report(options
, oid
, OBJ_TAG
, FSCK_MSG_MISSING_TYPE_ENTRY
, "invalid format - expected 'type' line");
970 eol
= memchr(buffer
, '\n', buffer_end
- buffer
);
972 ret
= report(options
, oid
, OBJ_TAG
, FSCK_MSG_MISSING_TYPE
, "invalid format - unexpected end after 'type' line");
975 *tagged_type
= type_from_string_gently(buffer
, eol
- buffer
, 1);
976 if (*tagged_type
< 0)
977 ret
= report(options
, oid
, OBJ_TAG
, FSCK_MSG_BAD_TYPE
, "invalid 'type' value");
982 if (buffer
>= buffer_end
|| !skip_prefix(buffer
, "tag ", &buffer
)) {
983 ret
= report(options
, oid
, OBJ_TAG
, FSCK_MSG_MISSING_TAG_ENTRY
, "invalid format - expected 'tag' line");
986 eol
= memchr(buffer
, '\n', buffer_end
- buffer
);
988 ret
= report(options
, oid
, OBJ_TAG
, FSCK_MSG_MISSING_TAG
, "invalid format - unexpected end after 'type' line");
991 strbuf_addf(&sb
, "refs/tags/%.*s", (int)(eol
- buffer
), buffer
);
992 if (check_refname_format(sb
.buf
, 0)) {
993 ret
= report(options
, oid
, OBJ_TAG
,
994 FSCK_MSG_BAD_TAG_NAME
,
995 "invalid 'tag' name: %.*s",
996 (int)(eol
- buffer
), buffer
);
1002 if (buffer
>= buffer_end
|| !skip_prefix(buffer
, "tagger ", &buffer
)) {
1003 /* early tags do not contain 'tagger' lines; warn only */
1004 ret
= report(options
, oid
, OBJ_TAG
, FSCK_MSG_MISSING_TAGGER_ENTRY
, "invalid format - expected 'tagger' line");
1009 ret
= fsck_ident(&buffer
, oid
, OBJ_TAG
, options
);
1011 if (buffer
< buffer_end
&& !starts_with(buffer
, "\n")) {
1013 * The verify_headers() check will allow
1014 * e.g. "[...]tagger <tagger>\nsome
1015 * garbage\n\nmessage" to pass, thinking "some
1016 * garbage" could be a custom header. E.g. "mktag"
1017 * doesn't want any unknown headers.
1019 ret
= report(options
, oid
, OBJ_TAG
, FSCK_MSG_EXTRA_HEADER_ENTRY
, "invalid format - extra header(s) after 'tagger'");
1025 strbuf_release(&sb
);
1029 static int starts_with_dot_slash(const char *const path
)
1031 return path_match_flags(path
, PATH_MATCH_STARTS_WITH_DOT_SLASH
|
1032 PATH_MATCH_XPLATFORM
);
1035 static int starts_with_dot_dot_slash(const char *const path
)
1037 return path_match_flags(path
, PATH_MATCH_STARTS_WITH_DOT_DOT_SLASH
|
1038 PATH_MATCH_XPLATFORM
);
1041 static int submodule_url_is_relative(const char *url
)
1043 return starts_with_dot_slash(url
) || starts_with_dot_dot_slash(url
);
1047 * Count directory components that a relative submodule URL should chop
1048 * from the remote_url it is to be resolved against.
1050 * In other words, this counts "../" components at the start of a
1053 * Returns the number of directory components to chop and writes a
1054 * pointer to the next character of url after all leading "./" and
1055 * "../" components to out.
1057 static int count_leading_dotdots(const char *url
, const char **out
)
1061 if (starts_with_dot_dot_slash(url
)) {
1063 url
+= strlen("../");
1066 if (starts_with_dot_slash(url
)) {
1067 url
+= strlen("./");
1075 * Check whether a transport is implemented by git-remote-curl.
1077 * If it is, returns 1 and writes the URL that would be passed to
1078 * git-remote-curl to the "out" parameter.
1080 * Otherwise, returns 0 and leaves "out" untouched.
1083 * http::https://example.com/repo.git -> 1, https://example.com/repo.git
1084 * https://example.com/repo.git -> 1, https://example.com/repo.git
1085 * git://example.com/repo.git -> 0
1087 * This is for use in checking for previously exploitable bugs that
1088 * required a submodule URL to be passed to git-remote-curl.
1090 static int url_to_curl_url(const char *url
, const char **out
)
1093 * We don't need to check for case-aliases, "http.exe", and so
1094 * on because in the default configuration, is_transport_allowed
1095 * prevents URLs with those schemes from being cloned
1098 if (skip_prefix(url
, "http::", out
) ||
1099 skip_prefix(url
, "https::", out
) ||
1100 skip_prefix(url
, "ftp::", out
) ||
1101 skip_prefix(url
, "ftps::", out
))
1103 if (starts_with(url
, "http://") ||
1104 starts_with(url
, "https://") ||
1105 starts_with(url
, "ftp://") ||
1106 starts_with(url
, "ftps://")) {
1113 static int check_submodule_url(const char *url
)
1115 const char *curl_url
;
1117 if (looks_like_command_line_option(url
))
1120 if (submodule_url_is_relative(url
) || starts_with(url
, "git://")) {
1126 * This could be appended to an http URL and url-decoded;
1127 * check for malicious characters.
1129 decoded
= url_decode(url
);
1130 has_nl
= !!strchr(decoded
, '\n');
1137 * URLs which escape their root via "../" can overwrite
1138 * the host field and previous components, resolving to
1139 * URLs like https::example.com/submodule.git and
1140 * https:///example.com/submodule.git that were
1141 * susceptible to CVE-2020-11008.
1143 if (count_leading_dotdots(url
, &next
) > 0 &&
1144 (*next
== ':' || *next
== '/'))
1148 else if (url_to_curl_url(url
, &curl_url
)) {
1149 struct credential c
= CREDENTIAL_INIT
;
1151 if (credential_from_url_gently(&c
, curl_url
, 1) ||
1154 credential_clear(&c
);
1161 struct fsck_gitmodules_data
{
1162 const struct object_id
*oid
;
1163 struct fsck_options
*options
;
1167 static int fsck_gitmodules_fn(const char *var
, const char *value
,
1168 const struct config_context
*ctx UNUSED
,
1171 struct fsck_gitmodules_data
*data
= vdata
;
1172 const char *subsection
, *key
;
1173 size_t subsection_len
;
1176 if (parse_config_key(var
, "submodule", &subsection
, &subsection_len
, &key
) < 0 ||
1180 name
= xmemdupz(subsection
, subsection_len
);
1181 if (check_submodule_name(name
) < 0)
1182 data
->ret
|= report(data
->options
,
1183 data
->oid
, OBJ_BLOB
,
1184 FSCK_MSG_GITMODULES_NAME
,
1185 "disallowed submodule name: %s",
1187 if (!strcmp(key
, "url") && value
&&
1188 check_submodule_url(value
) < 0)
1189 data
->ret
|= report(data
->options
,
1190 data
->oid
, OBJ_BLOB
,
1191 FSCK_MSG_GITMODULES_URL
,
1192 "disallowed submodule url: %s",
1194 if (!strcmp(key
, "path") && value
&&
1195 looks_like_command_line_option(value
))
1196 data
->ret
|= report(data
->options
,
1197 data
->oid
, OBJ_BLOB
,
1198 FSCK_MSG_GITMODULES_PATH
,
1199 "disallowed submodule path: %s",
1201 if (!strcmp(key
, "update") && value
&&
1202 parse_submodule_update_type(value
) == SM_UPDATE_COMMAND
)
1203 data
->ret
|= report(data
->options
, data
->oid
, OBJ_BLOB
,
1204 FSCK_MSG_GITMODULES_UPDATE
,
1205 "disallowed submodule update setting: %s",
1212 static int fsck_blob(const struct object_id
*oid
, const char *buf
,
1213 unsigned long size
, struct fsck_options
*options
)
1217 if (object_on_skiplist(options
, oid
))
1220 if (oidset_contains(&options
->gitmodules_found
, oid
)) {
1221 struct config_options config_opts
= { 0 };
1222 struct fsck_gitmodules_data data
;
1224 oidset_insert(&options
->gitmodules_done
, oid
);
1228 * A missing buffer here is a sign that the caller found the
1229 * blob too gigantic to load into memory. Let's just consider
1232 return report(options
, oid
, OBJ_BLOB
,
1233 FSCK_MSG_GITMODULES_LARGE
,
1234 ".gitmodules too large to parse");
1238 data
.options
= options
;
1240 config_opts
.error_action
= CONFIG_ERROR_SILENT
;
1241 if (git_config_from_mem(fsck_gitmodules_fn
, CONFIG_ORIGIN_BLOB
,
1242 ".gitmodules", buf
, size
, &data
,
1243 CONFIG_SCOPE_UNKNOWN
, &config_opts
))
1244 data
.ret
|= report(options
, oid
, OBJ_BLOB
,
1245 FSCK_MSG_GITMODULES_PARSE
,
1246 "could not parse gitmodules blob");
1250 if (oidset_contains(&options
->gitattributes_found
, oid
)) {
1253 oidset_insert(&options
->gitattributes_done
, oid
);
1255 if (!buf
|| size
> ATTR_MAX_FILE_SIZE
) {
1257 * A missing buffer here is a sign that the caller found the
1258 * blob too gigantic to load into memory. Let's just consider
1261 return report(options
, oid
, OBJ_BLOB
,
1262 FSCK_MSG_GITATTRIBUTES_LARGE
,
1263 ".gitattributes too large to parse");
1266 for (ptr
= buf
; *ptr
; ) {
1267 const char *eol
= strchrnul(ptr
, '\n');
1268 if (eol
- ptr
>= ATTR_MAX_LINE_LENGTH
) {
1269 ret
|= report(options
, oid
, OBJ_BLOB
,
1270 FSCK_MSG_GITATTRIBUTES_LINE_LENGTH
,
1271 ".gitattributes has too long lines to parse");
1275 ptr
= *eol
? eol
+ 1 : eol
;
1282 int fsck_object(struct object
*obj
, void *data
, unsigned long size
,
1283 struct fsck_options
*options
)
1286 return report(options
, NULL
, OBJ_NONE
, FSCK_MSG_BAD_OBJECT_SHA1
, "no valid object to fsck");
1288 return fsck_buffer(&obj
->oid
, obj
->type
, data
, size
, options
);
1291 int fsck_buffer(const struct object_id
*oid
, enum object_type type
,
1292 void *data
, unsigned long size
,
1293 struct fsck_options
*options
)
1295 if (type
== OBJ_BLOB
)
1296 return fsck_blob(oid
, data
, size
, options
);
1297 if (type
== OBJ_TREE
)
1298 return fsck_tree(oid
, data
, size
, options
);
1299 if (type
== OBJ_COMMIT
)
1300 return fsck_commit(oid
, data
, size
, options
);
1301 if (type
== OBJ_TAG
)
1302 return fsck_tag(oid
, data
, size
, options
);
1304 return report(options
, oid
, type
,
1305 FSCK_MSG_UNKNOWN_TYPE
,
1306 "unknown type '%d' (internal fsck error)",
1310 int fsck_error_function(struct fsck_options
*o
,
1311 const struct object_id
*oid
,
1312 enum object_type object_type UNUSED
,
1313 enum fsck_msg_type msg_type
,
1314 enum fsck_msg_id msg_id UNUSED
,
1315 const char *message
)
1317 if (msg_type
== FSCK_WARN
) {
1318 warning("object %s: %s", fsck_describe_object(o
, oid
), message
);
1321 error("object %s: %s", fsck_describe_object(o
, oid
), message
);
1325 static int fsck_blobs(struct oidset
*blobs_found
, struct oidset
*blobs_done
,
1326 enum fsck_msg_id msg_missing
, enum fsck_msg_id msg_type
,
1327 struct fsck_options
*options
, const char *blob_type
)
1330 struct oidset_iter iter
;
1331 const struct object_id
*oid
;
1333 oidset_iter_init(blobs_found
, &iter
);
1334 while ((oid
= oidset_iter_next(&iter
))) {
1335 enum object_type type
;
1339 if (oidset_contains(blobs_done
, oid
))
1342 buf
= repo_read_object_file(the_repository
, oid
, &type
, &size
);
1344 if (is_promisor_object(oid
))
1346 ret
|= report(options
,
1347 oid
, OBJ_BLOB
, msg_missing
,
1348 "unable to read %s blob", blob_type
);
1352 if (type
== OBJ_BLOB
)
1353 ret
|= fsck_blob(oid
, buf
, size
, options
);
1355 ret
|= report(options
, oid
, type
, msg_type
,
1356 "non-blob found at %s", blob_type
);
1360 oidset_clear(blobs_found
);
1361 oidset_clear(blobs_done
);
1366 int fsck_finish(struct fsck_options
*options
)
1370 ret
|= fsck_blobs(&options
->gitmodules_found
, &options
->gitmodules_done
,
1371 FSCK_MSG_GITMODULES_MISSING
, FSCK_MSG_GITMODULES_BLOB
,
1372 options
, ".gitmodules");
1373 ret
|= fsck_blobs(&options
->gitattributes_found
, &options
->gitattributes_done
,
1374 FSCK_MSG_GITATTRIBUTES_MISSING
, FSCK_MSG_GITATTRIBUTES_BLOB
,
1375 options
, ".gitattributes");
1380 int git_fsck_config(const char *var
, const char *value
,
1381 const struct config_context
*ctx
, void *cb
)
1383 struct fsck_options
*options
= cb
;
1384 if (strcmp(var
, "fsck.skiplist") == 0) {
1386 struct strbuf sb
= STRBUF_INIT
;
1388 if (git_config_pathname(&path
, var
, value
))
1390 strbuf_addf(&sb
, "skiplist=%s", path
);
1392 fsck_set_msg_types(options
, sb
.buf
);
1393 strbuf_release(&sb
);
1397 if (skip_prefix(var
, "fsck.", &var
)) {
1398 fsck_set_msg_type(options
, var
, value
);
1402 return git_default_config(var
, value
, ctx
, cb
);
1406 * Custom error callbacks that are used in more than one place.
1409 int fsck_error_cb_print_missing_gitmodules(struct fsck_options
*o
,
1410 const struct object_id
*oid
,
1411 enum object_type object_type
,
1412 enum fsck_msg_type msg_type
,
1413 enum fsck_msg_id msg_id
,
1414 const char *message
)
1416 if (msg_id
== FSCK_MSG_GITMODULES_MISSING
) {
1417 puts(oid_to_hex(oid
));
1420 return fsck_error_function(o
, oid
, object_type
, msg_type
, msg_id
, message
);