Skip to content

Commit fea9f60

Browse files
committed
Sync with Git 2.37.5
2 parents 8706a59 + e43ac5f commit fea9f60

20 files changed

+562
-125
lines changed

Documentation/RelNotes/2.30.7.txt

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
Git v2.30.7 Release Notes
2+
=========================
3+
4+
This release addresses the security issues CVE-2022-41903 and
5+
CVE-2022-23521.
6+
7+
8+
Fixes since v2.30.6
9+
-------------------
10+
11+
* CVE-2022-41903:
12+
13+
git log has the ability to display commits using an arbitrary
14+
format with its --format specifiers. This functionality is also
15+
exposed to git archive via the export-subst gitattribute.
16+
17+
When processing the padding operators (e.g., %<(, %<|(, %>(,
18+
%>>(, or %><( ), an integer overflow can occur in
19+
pretty.c::format_and_pad_commit() where a size_t is improperly
20+
stored as an int, and then added as an offset to a subsequent
21+
memcpy() call.
22+
23+
This overflow can be triggered directly by a user running a
24+
command which invokes the commit formatting machinery (e.g., git
25+
log --format=...). It may also be triggered indirectly through
26+
git archive via the export-subst mechanism, which expands format
27+
specifiers inside of files within the repository during a git
28+
archive.
29+
30+
This integer overflow can result in arbitrary heap writes, which
31+
may result in remote code execution.
32+
33+
* CVE-2022-23521:
34+
35+
gitattributes are a mechanism to allow defining attributes for
36+
paths. These attributes can be defined by adding a `.gitattributes`
37+
file to the repository, which contains a set of file patterns and
38+
the attributes that should be set for paths matching this pattern.
39+
40+
When parsing gitattributes, multiple integer overflows can occur
41+
when there is a huge number of path patterns, a huge number of
42+
attributes for a single pattern, or when the declared attribute
43+
names are huge.
44+
45+
These overflows can be triggered via a crafted `.gitattributes` file
46+
that may be part of the commit history. Git silently splits lines
47+
longer than 2KB when parsing gitattributes from a file, but not when
48+
parsing them from the index. Consequentially, the failure mode
49+
depends on whether the file exists in the working tree, the index or
50+
both.
51+
52+
This integer overflow can result in arbitrary heap reads and writes,
53+
which may result in remote code execution.
54+
55+
Credit for finding CVE-2022-41903 goes to Joern Schneeweisz of GitLab.
56+
An initial fix was authored by Markus Vervier of X41 D-Sec. Credit for
57+
finding CVE-2022-23521 goes to Markus Vervier and Eric Sesterhenn of X41
58+
D-Sec. This work was sponsored by OSTIF.
59+
60+
The proposed fixes have been polished and extended to cover additional
61+
findings by Patrick Steinhardt of GitLab, with help from others on the
62+
Git security mailing list.
63+
64+
Patrick Steinhardt (21):
65+
attr: fix overflow when upserting attribute with overly long name
66+
attr: fix out-of-bounds read with huge attribute names
67+
attr: fix integer overflow when parsing huge attribute names
68+
attr: fix out-of-bounds write when parsing huge number of attributes
69+
attr: fix out-of-bounds read with unreasonable amount of patterns
70+
attr: fix integer overflow with more than INT_MAX macros
71+
attr: harden allocation against integer overflows
72+
attr: fix silently splitting up lines longer than 2048 bytes
73+
attr: ignore attribute lines exceeding 2048 bytes
74+
attr: ignore overly large gitattributes files
75+
pretty: fix out-of-bounds write caused by integer overflow
76+
pretty: fix out-of-bounds read when left-flushing with stealing
77+
pretty: fix out-of-bounds read when parsing invalid padding format
78+
pretty: fix adding linefeed when placeholder is not expanded
79+
pretty: fix integer overflow in wrapping format
80+
utf8: fix truncated string lengths in `utf8_strnwidth()`
81+
utf8: fix returning negative string width
82+
utf8: fix overflow when returning string width
83+
utf8: fix checking for glyph width in `strbuf_utf8_replace()`
84+
utf8: refactor `strbuf_utf8_replace` to not rely on preallocated buffer
85+
pretty: restrict input lengths for padding and wrapping formats
86+

Documentation/RelNotes/2.31.6.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
Git v2.31.6 Release Notes
2+
=========================
3+
4+
This release merges the security fix that appears in v2.30.7; see
5+
the release notes for that version for details.

Documentation/RelNotes/2.32.5.txt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
Git v2.32.5 Release Notes
2+
=========================
3+
4+
This release merges the security fix that appears in v2.30.7; see
5+
the release notes for that version for details.
6+
7+
In addition, included are additional code for "git fsck" to check
8+
for questionable .gitattributes files.

Documentation/RelNotes/2.33.6.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
Git v2.33.6 Release Notes
2+
=========================
3+
4+
This release merges the security fix that appears in v2.30.7; see
5+
the release notes for that version for details.

Documentation/RelNotes/2.34.6.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
Git v2.34.6 Release Notes
2+
=========================
3+
4+
This release merges the security fix that appears in v2.30.7; see
5+
the release notes for that version for details.

Documentation/RelNotes/2.35.6.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
Git v2.35.6 Release Notes
2+
=========================
3+
4+
This release merges the security fix that appears in v2.30.7; see
5+
the release notes for that version for details.

Documentation/RelNotes/2.36.4.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
Git v2.36.4 Release Notes
2+
=========================
3+
4+
This release merges the security fix that appears in v2.30.7; see
5+
the release notes for that version for details.

Documentation/RelNotes/2.37.5.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
Git v2.37.5 Release Notes
2+
=========================
3+
4+
This release merges the security fix that appears in v2.30.7; see
5+
the release notes for that version for details.

attr.c

Lines changed: 56 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ static const char git_attr__unknown[] = "(builtin)unknown";
2424
#define ATTR__UNKNOWN git_attr__unknown
2525

2626
struct git_attr {
27-
int attr_nr; /* unique attribute number */
27+
unsigned int attr_nr; /* unique attribute number */
2828
char name[FLEX_ARRAY]; /* attribute name */
2929
};
3030

@@ -206,7 +206,7 @@ static void report_invalid_attr(const char *name, size_t len,
206206
* dictionary. If no entry is found, create a new attribute and store it in
207207
* the dictionary.
208208
*/
209-
static const struct git_attr *git_attr_internal(const char *name, int namelen)
209+
static const struct git_attr *git_attr_internal(const char *name, size_t namelen)
210210
{
211211
struct git_attr *a;
212212

@@ -222,8 +222,8 @@ static const struct git_attr *git_attr_internal(const char *name, int namelen)
222222
a->attr_nr = hashmap_get_size(&g_attr_hashmap.map);
223223

224224
attr_hashmap_add(&g_attr_hashmap, a->name, namelen, a);
225-
assert(a->attr_nr ==
226-
(hashmap_get_size(&g_attr_hashmap.map) - 1));
225+
if (a->attr_nr != hashmap_get_size(&g_attr_hashmap.map) - 1)
226+
die(_("unable to add additional attribute"));
227227
}
228228

229229
hashmap_unlock(&g_attr_hashmap);
@@ -268,7 +268,7 @@ struct match_attr {
268268
const struct git_attr *attr;
269269
} u;
270270
char is_macro;
271-
unsigned num_attr;
271+
size_t num_attr;
272272
struct attr_state state[FLEX_ARRAY];
273273
};
274274

@@ -289,7 +289,7 @@ static const char *parse_attr(const char *src, int lineno, const char *cp,
289289
struct attr_state *e)
290290
{
291291
const char *ep, *equals;
292-
int len;
292+
size_t len;
293293

294294
ep = cp + strcspn(cp, blank);
295295
equals = strchr(cp, '=');
@@ -333,8 +333,7 @@ static const char *parse_attr(const char *src, int lineno, const char *cp,
333333
static struct match_attr *parse_attr_line(const char *line, const char *src,
334334
int lineno, unsigned flags)
335335
{
336-
int namelen;
337-
int num_attr, i;
336+
size_t namelen, num_attr, i;
338337
const char *cp, *name, *states;
339338
struct match_attr *res = NULL;
340339
int is_macro;
@@ -345,6 +344,11 @@ static struct match_attr *parse_attr_line(const char *line, const char *src,
345344
return NULL;
346345
name = cp;
347346

347+
if (strlen(line) >= ATTR_MAX_LINE_LENGTH) {
348+
warning(_("ignoring overly long attributes line %d"), lineno);
349+
return NULL;
350+
}
351+
348352
if (*cp == '"' && !unquote_c_style(&pattern, name, &states)) {
349353
name = pattern.buf;
350354
namelen = pattern.len;
@@ -381,10 +385,9 @@ static struct match_attr *parse_attr_line(const char *line, const char *src,
381385
goto fail_return;
382386
}
383387

384-
res = xcalloc(1,
385-
sizeof(*res) +
386-
sizeof(struct attr_state) * num_attr +
387-
(is_macro ? 0 : namelen + 1));
388+
res = xcalloc(1, st_add3(sizeof(*res),
389+
st_mult(sizeof(struct attr_state), num_attr),
390+
is_macro ? 0 : namelen + 1));
388391
if (is_macro) {
389392
res->u.attr = git_attr_internal(name, namelen);
390393
} else {
@@ -447,11 +450,12 @@ struct attr_stack {
447450

448451
static void attr_stack_free(struct attr_stack *e)
449452
{
450-
int i;
453+
unsigned i;
451454
free(e->origin);
452455
for (i = 0; i < e->num_matches; i++) {
453456
struct match_attr *a = e->attrs[i];
454-
int j;
457+
size_t j;
458+
455459
for (j = 0; j < a->num_attr; j++) {
456460
const char *setto = a->state[j].setto;
457461
if (setto == ATTR__TRUE ||
@@ -660,8 +664,8 @@ static void handle_attr_line(struct attr_stack *res,
660664
a = parse_attr_line(line, src, lineno, flags);
661665
if (!a)
662666
return;
663-
ALLOC_GROW(res->attrs, res->num_matches + 1, res->alloc);
664-
res->attrs[res->num_matches++] = a;
667+
ALLOC_GROW_BY(res->attrs, res->num_matches, 1, res->alloc);
668+
res->attrs[res->num_matches - 1] = a;
665669
}
666670

667671
static struct attr_stack *read_attr_from_array(const char **list)
@@ -701,11 +705,12 @@ void git_attr_set_direction(enum git_attr_direction new_direction)
701705

702706
static struct attr_stack *read_attr_from_file(const char *path, unsigned flags)
703707
{
708+
struct strbuf buf = STRBUF_INIT;
704709
int fd;
705710
FILE *fp;
706711
struct attr_stack *res;
707-
char buf[2048];
708712
int lineno = 0;
713+
struct stat st;
709714

710715
if (flags & READ_ATTR_NOFOLLOW)
711716
fd = open_nofollow(path, O_RDONLY);
@@ -717,15 +722,26 @@ static struct attr_stack *read_attr_from_file(const char *path, unsigned flags)
717722
return NULL;
718723
}
719724
fp = xfdopen(fd, "r");
725+
if (fstat(fd, &st)) {
726+
warning_errno(_("cannot fstat gitattributes file '%s'"), path);
727+
fclose(fp);
728+
return NULL;
729+
}
730+
if (st.st_size >= ATTR_MAX_FILE_SIZE) {
731+
warning(_("ignoring overly large gitattributes file '%s'"), path);
732+
fclose(fp);
733+
return NULL;
734+
}
720735

721736
CALLOC_ARRAY(res, 1);
722-
while (fgets(buf, sizeof(buf), fp)) {
723-
char *bufp = buf;
724-
if (!lineno)
725-
skip_utf8_bom(&bufp, strlen(bufp));
726-
handle_attr_line(res, bufp, path, ++lineno, flags);
737+
while (strbuf_getline(&buf, fp) != EOF) {
738+
if (!lineno && starts_with(buf.buf, utf8_bom))
739+
strbuf_remove(&buf, 0, strlen(utf8_bom));
740+
handle_attr_line(res, buf.buf, path, ++lineno, flags);
727741
}
742+
728743
fclose(fp);
744+
strbuf_release(&buf);
729745
return res;
730746
}
731747

@@ -736,6 +752,7 @@ static struct attr_stack *read_attr_from_index(struct index_state *istate,
736752
struct attr_stack *res;
737753
char *buf, *sp;
738754
int lineno = 0;
755+
size_t size;
739756

740757
if (!istate)
741758
return NULL;
@@ -754,9 +771,13 @@ static struct attr_stack *read_attr_from_index(struct index_state *istate,
754771
if (!path_in_cone_mode_sparse_checkout(path, istate))
755772
return NULL;
756773

757-
buf = read_blob_data_from_index(istate, path, NULL);
774+
buf = read_blob_data_from_index(istate, path, &size);
758775
if (!buf)
759776
return NULL;
777+
if (size >= ATTR_MAX_FILE_SIZE) {
778+
warning(_("ignoring overly large gitattributes blob '%s'"), path);
779+
return NULL;
780+
}
760781

761782
CALLOC_ARRAY(res, 1);
762783
for (sp = buf; *sp; ) {
@@ -999,12 +1020,12 @@ static int macroexpand_one(struct all_attrs_item *all_attrs, int nr, int rem);
9991020
static int fill_one(struct all_attrs_item *all_attrs,
10001021
const struct match_attr *a, int rem)
10011022
{
1002-
int i;
1023+
size_t i;
10031024

1004-
for (i = a->num_attr - 1; rem > 0 && i >= 0; i--) {
1005-
const struct git_attr *attr = a->state[i].attr;
1025+
for (i = a->num_attr; rem > 0 && i > 0; i--) {
1026+
const struct git_attr *attr = a->state[i - 1].attr;
10061027
const char **n = &(all_attrs[attr->attr_nr].value);
1007-
const char *v = a->state[i].setto;
1028+
const char *v = a->state[i - 1].setto;
10081029

10091030
if (*n == ATTR__UNKNOWN) {
10101031
*n = v;
@@ -1020,11 +1041,11 @@ static int fill(const char *path, int pathlen, int basename_offset,
10201041
struct all_attrs_item *all_attrs, int rem)
10211042
{
10221043
for (; rem > 0 && stack; stack = stack->prev) {
1023-
int i;
1044+
unsigned i;
10241045
const char *base = stack->origin ? stack->origin : "";
10251046

1026-
for (i = stack->num_matches - 1; 0 < rem && 0 <= i; i--) {
1027-
const struct match_attr *a = stack->attrs[i];
1047+
for (i = stack->num_matches; 0 < rem && 0 < i; i--) {
1048+
const struct match_attr *a = stack->attrs[i - 1];
10281049
if (a->is_macro)
10291050
continue;
10301051
if (path_matches(path, pathlen, basename_offset,
@@ -1055,11 +1076,11 @@ static void determine_macros(struct all_attrs_item *all_attrs,
10551076
const struct attr_stack *stack)
10561077
{
10571078
for (; stack; stack = stack->prev) {
1058-
int i;
1059-
for (i = stack->num_matches - 1; i >= 0; i--) {
1060-
const struct match_attr *ma = stack->attrs[i];
1079+
unsigned i;
1080+
for (i = stack->num_matches; i > 0; i--) {
1081+
const struct match_attr *ma = stack->attrs[i - 1];
10611082
if (ma->is_macro) {
1062-
int n = ma->u.attr->attr_nr;
1083+
unsigned int n = ma->u.attr->attr_nr;
10631084
if (!all_attrs[n].macro) {
10641085
all_attrs[n].macro = ma;
10651086
}
@@ -1111,7 +1132,7 @@ void git_check_attr(struct index_state *istate,
11111132
collect_some_attrs(istate, path, check);
11121133

11131134
for (i = 0; i < check->nr; i++) {
1114-
size_t n = check->items[i].attr->attr_nr;
1135+
unsigned int n = check->items[i].attr->attr_nr;
11151136
const char *value = check->all_attrs[n].value;
11161137
if (value == ATTR__UNKNOWN)
11171138
value = ATTR__UNSET;

attr.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,18 @@
107107
* - Free the `attr_check` struct by calling `attr_check_free()`.
108108
*/
109109

110+
/**
111+
* The maximum line length for a gitattributes file. If the line exceeds this
112+
* length we will ignore it.
113+
*/
114+
#define ATTR_MAX_LINE_LENGTH 2048
115+
116+
/**
117+
* The maximum size of the giattributes file. If the file exceeds this size we
118+
* will ignore it.
119+
*/
120+
#define ATTR_MAX_FILE_SIZE (100 * 1024 * 1024)
121+
110122
struct index_state;
111123

112124
/**

column.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ struct column_data {
2323
/* return length of 's' in letters, ANSI escapes stripped */
2424
static int item_length(const char *s)
2525
{
26-
return utf8_strnwidth(s, -1, 1);
26+
return utf8_strnwidth(s, strlen(s), 1);
2727
}
2828

2929
/*

0 commit comments

Comments
 (0)