Skip to content

Commit 32c6dca

Browse files
committed
Merge branch 'jk/path-name-safety-2.4' into maint-2.4
Bugfix patches were backported from the 'master' front to plug heap corruption holes, to catch integer overflow in the computation of pathname lengths, and to get rid of the name_path API. Both of these would have resulted in writing over an under-allocated buffer when formulating pathnames while tree traversal. * jk/path-name-safety-2.4: list-objects: pass full pathname to callbacks list-objects: drop name_path entirely list-objects: convert name_path to a strbuf show_object_with_name: simplify by using path_name() http-push: stop using name_path tree-diff: catch integer overflow in combine_diff_path allocation add helpers for detecting size_t overflow
2 parents a2558fb + 2824e18 commit 32c6dca

13 files changed

+84
-146
lines changed

builtin/pack-objects.c

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2284,21 +2284,11 @@ static void show_commit(struct commit *commit, void *data)
22842284
index_commit_for_bitmap(commit);
22852285
}
22862286

2287-
static void show_object(struct object *obj,
2288-
const struct name_path *path, const char *last,
2289-
void *data)
2287+
static void show_object(struct object *obj, const char *name, void *data)
22902288
{
2291-
char *name = path_name(path, last);
2292-
22932289
add_preferred_base_object(name);
22942290
add_object_entry(obj->sha1, obj->type, name, 0);
22952291
obj->flags |= OBJECT_ADDED;
2296-
2297-
/*
2298-
* We will have generated the hash from the name,
2299-
* but not saved a pointer to it - we can free it
2300-
*/
2301-
free((char *)name);
23022292
}
23032293

23042294
static void show_edge(struct commit *commit)
@@ -2480,8 +2470,7 @@ static int get_object_list_from_bitmap(struct rev_info *revs)
24802470
}
24812471

24822472
static void record_recent_object(struct object *obj,
2483-
const struct name_path *path,
2484-
const char *last,
2473+
const char *name,
24852474
void *data)
24862475
{
24872476
sha1_array_append(&recent_objects, obj->sha1);

builtin/rev-list.c

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -177,9 +177,7 @@ static void finish_commit(struct commit *commit, void *data)
177177
free_commit_buffer(commit);
178178
}
179179

180-
static void finish_object(struct object *obj,
181-
const struct name_path *path, const char *name,
182-
void *cb_data)
180+
static void finish_object(struct object *obj, const char *name, void *cb_data)
183181
{
184182
struct rev_list_info *info = cb_data;
185183
if (obj->type == OBJ_BLOB && !has_sha1_file(obj->sha1))
@@ -188,15 +186,13 @@ static void finish_object(struct object *obj,
188186
parse_object(obj->sha1);
189187
}
190188

191-
static void show_object(struct object *obj,
192-
const struct name_path *path, const char *component,
193-
void *cb_data)
189+
static void show_object(struct object *obj, const char *name, void *cb_data)
194190
{
195191
struct rev_list_info *info = cb_data;
196-
finish_object(obj, path, component, cb_data);
192+
finish_object(obj, name, cb_data);
197193
if (info->flags & REV_LIST_QUIET)
198194
return;
199-
show_object_with_name(stdout, obj, path, component);
195+
show_object_with_name(stdout, obj, name);
200196
}
201197

202198
static void show_edge(struct commit *commit)

diff.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -215,8 +215,8 @@ struct combine_diff_path {
215215
} parent[FLEX_ARRAY];
216216
};
217217
#define combine_diff_path_size(n, l) \
218-
(sizeof(struct combine_diff_path) + \
219-
sizeof(struct combine_diff_parent) * (n) + (l) + 1)
218+
st_add4(sizeof(struct combine_diff_path), (l), 1, \
219+
st_mult(sizeof(struct combine_diff_parent), (n)))
220220

221221
extern void show_combined_diff(struct combine_diff_path *elem, int num_parent,
222222
int dense, struct rev_info *);

git-compat-util.h

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,14 @@
9696
#define unsigned_add_overflows(a, b) \
9797
((b) > maximum_unsigned_value_of_type(a) - (a))
9898

99+
/*
100+
* Returns true if the multiplication of "a" and "b" will
101+
* overflow. The types of "a" and "b" must match and must be unsigned.
102+
* Note that this macro evaluates "a" twice!
103+
*/
104+
#define unsigned_mult_overflows(a, b) \
105+
((a) && (b) > maximum_unsigned_value_of_type(a) / (a))
106+
99107
#ifdef __GNUC__
100108
#define TYPEOF(x) (__typeof__(x))
101109
#else
@@ -698,6 +706,32 @@ extern void release_pack_memory(size_t);
698706
typedef void (*try_to_free_t)(size_t);
699707
extern try_to_free_t set_try_to_free_routine(try_to_free_t);
700708

709+
static inline size_t st_add(size_t a, size_t b)
710+
{
711+
if (unsigned_add_overflows(a, b))
712+
die("size_t overflow: %"PRIuMAX" + %"PRIuMAX,
713+
(uintmax_t)a, (uintmax_t)b);
714+
return a + b;
715+
}
716+
#define st_add3(a,b,c) st_add((a),st_add((b),(c)))
717+
#define st_add4(a,b,c,d) st_add((a),st_add3((b),(c),(d)))
718+
719+
static inline size_t st_mult(size_t a, size_t b)
720+
{
721+
if (unsigned_mult_overflows(a, b))
722+
die("size_t overflow: %"PRIuMAX" * %"PRIuMAX,
723+
(uintmax_t)a, (uintmax_t)b);
724+
return a * b;
725+
}
726+
727+
static inline size_t st_sub(size_t a, size_t b)
728+
{
729+
if (a < b)
730+
die("size_t underflow: %"PRIuMAX" - %"PRIuMAX,
731+
(uintmax_t)a, (uintmax_t)b);
732+
return a - b;
733+
}
734+
701735
#ifdef HAVE_ALLOCA_H
702736
# include <alloca.h>
703737
# define xalloca(size) (alloca(size))

http-push.c

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1276,9 +1276,7 @@ static struct object_list **add_one_object(struct object *obj, struct object_lis
12761276
}
12771277

12781278
static struct object_list **process_blob(struct blob *blob,
1279-
struct object_list **p,
1280-
struct name_path *path,
1281-
const char *name)
1279+
struct object_list **p)
12821280
{
12831281
struct object *obj = &blob->object;
12841282

@@ -1292,14 +1290,11 @@ static struct object_list **process_blob(struct blob *blob,
12921290
}
12931291

12941292
static struct object_list **process_tree(struct tree *tree,
1295-
struct object_list **p,
1296-
struct name_path *path,
1297-
const char *name)
1293+
struct object_list **p)
12981294
{
12991295
struct object *obj = &tree->object;
13001296
struct tree_desc desc;
13011297
struct name_entry entry;
1302-
struct name_path me;
13031298

13041299
obj->flags |= LOCAL;
13051300

@@ -1309,21 +1304,17 @@ static struct object_list **process_tree(struct tree *tree,
13091304
die("bad tree object %s", sha1_to_hex(obj->sha1));
13101305

13111306
obj->flags |= SEEN;
1312-
name = xstrdup(name);
13131307
p = add_one_object(obj, p);
1314-
me.up = path;
1315-
me.elem = name;
1316-
me.elem_len = strlen(name);
13171308

13181309
init_tree_desc(&desc, tree->buffer, tree->size);
13191310

13201311
while (tree_entry(&desc, &entry))
13211312
switch (object_type(entry.mode)) {
13221313
case OBJ_TREE:
1323-
p = process_tree(lookup_tree(entry.sha1), p, &me, name);
1314+
p = process_tree(lookup_tree(entry.sha1), p);
13241315
break;
13251316
case OBJ_BLOB:
1326-
p = process_blob(lookup_blob(entry.sha1), p, &me, name);
1317+
p = process_blob(lookup_blob(entry.sha1), p);
13271318
break;
13281319
default:
13291320
/* Subproject commit - not in this repository */
@@ -1342,7 +1333,7 @@ static int get_delta(struct rev_info *revs, struct remote_lock *lock)
13421333
int count = 0;
13431334

13441335
while ((commit = get_revision(revs)) != NULL) {
1345-
p = process_tree(commit->tree, p, NULL, "");
1336+
p = process_tree(commit->tree, p);
13461337
commit->object.flags |= LOCAL;
13471338
if (!(commit->object.flags & UNINTERESTING))
13481339
count += add_send_request(&commit->object, lock);
@@ -1361,11 +1352,11 @@ static int get_delta(struct rev_info *revs, struct remote_lock *lock)
13611352
continue;
13621353
}
13631354
if (obj->type == OBJ_TREE) {
1364-
p = process_tree((struct tree *)obj, p, NULL, name);
1355+
p = process_tree((struct tree *)obj, p);
13651356
continue;
13661357
}
13671358
if (obj->type == OBJ_BLOB) {
1368-
p = process_blob((struct blob *)obj, p, NULL, name);
1359+
p = process_blob((struct blob *)obj, p);
13691360
continue;
13701361
}
13711362
die("unknown pending object %s (%s)", sha1_to_hex(obj->sha1), name);

list-objects.c

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,12 @@
1111
static void process_blob(struct rev_info *revs,
1212
struct blob *blob,
1313
show_object_fn show,
14-
struct name_path *path,
14+
struct strbuf *path,
1515
const char *name,
1616
void *cb_data)
1717
{
1818
struct object *obj = &blob->object;
19+
size_t pathlen;
1920

2021
if (!revs->blob_objects)
2122
return;
@@ -24,7 +25,11 @@ static void process_blob(struct rev_info *revs,
2425
if (obj->flags & (UNINTERESTING | SEEN))
2526
return;
2627
obj->flags |= SEEN;
27-
show(obj, path, name, cb_data);
28+
29+
pathlen = path->len;
30+
strbuf_addstr(path, name);
31+
show(obj, path->buf, cb_data);
32+
strbuf_setlen(path, pathlen);
2833
}
2934

3035
/*
@@ -52,7 +57,7 @@ static void process_blob(struct rev_info *revs,
5257
static void process_gitlink(struct rev_info *revs,
5358
const unsigned char *sha1,
5459
show_object_fn show,
55-
struct name_path *path,
60+
struct strbuf *path,
5661
const char *name,
5762
void *cb_data)
5863
{
@@ -62,15 +67,13 @@ static void process_gitlink(struct rev_info *revs,
6267
static void process_tree(struct rev_info *revs,
6368
struct tree *tree,
6469
show_object_fn show,
65-
struct name_path *path,
6670
struct strbuf *base,
6771
const char *name,
6872
void *cb_data)
6973
{
7074
struct object *obj = &tree->object;
7175
struct tree_desc desc;
7276
struct name_entry entry;
73-
struct name_path me;
7477
enum interesting match = revs->diffopt.pathspec.nr == 0 ?
7578
all_entries_interesting: entry_not_interesting;
7679
int baselen = base->len;
@@ -86,17 +89,12 @@ static void process_tree(struct rev_info *revs,
8689
return;
8790
die("bad tree object %s", sha1_to_hex(obj->sha1));
8891
}
92+
8993
obj->flags |= SEEN;
90-
show(obj, path, name, cb_data);
91-
me.up = path;
92-
me.elem = name;
93-
me.elem_len = strlen(name);
94-
95-
if (!match) {
96-
strbuf_addstr(base, name);
97-
if (base->len)
98-
strbuf_addch(base, '/');
99-
}
94+
strbuf_addstr(base, name);
95+
show(obj, base->buf, cb_data);
96+
if (base->len)
97+
strbuf_addch(base, '/');
10098

10199
init_tree_desc(&desc, tree->buffer, tree->size);
102100

@@ -113,16 +111,16 @@ static void process_tree(struct rev_info *revs,
113111
if (S_ISDIR(entry.mode))
114112
process_tree(revs,
115113
lookup_tree(entry.sha1),
116-
show, &me, base, entry.path,
114+
show, base, entry.path,
117115
cb_data);
118116
else if (S_ISGITLINK(entry.mode))
119117
process_gitlink(revs, entry.sha1,
120-
show, &me, entry.path,
118+
show, base, entry.path,
121119
cb_data);
122120
else
123121
process_blob(revs,
124122
lookup_blob(entry.sha1),
125-
show, &me, entry.path,
123+
show, base, entry.path,
126124
cb_data);
127125
}
128126
strbuf_setlen(base, baselen);
@@ -213,19 +211,19 @@ void traverse_commit_list(struct rev_info *revs,
213211
continue;
214212
if (obj->type == OBJ_TAG) {
215213
obj->flags |= SEEN;
216-
show_object(obj, NULL, name, data);
214+
show_object(obj, name, data);
217215
continue;
218216
}
219217
if (!path)
220218
path = "";
221219
if (obj->type == OBJ_TREE) {
222220
process_tree(revs, (struct tree *)obj, show_object,
223-
NULL, &base, path, data);
221+
&base, path, data);
224222
continue;
225223
}
226224
if (obj->type == OBJ_BLOB) {
227225
process_blob(revs, (struct blob *)obj, show_object,
228-
NULL, path, data);
226+
&base, path, data);
229227
continue;
230228
}
231229
die("unknown pending object %s (%s)",

list-objects.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
#define LIST_OBJECTS_H
33

44
typedef void (*show_commit_fn)(struct commit *, void *);
5-
typedef void (*show_object_fn)(struct object *, const struct name_path *, const char *, void *);
5+
typedef void (*show_object_fn)(struct object *, const char *, void *);
66
void traverse_commit_list(struct rev_info *, show_commit_fn, show_object_fn, void *);
77

88
typedef void (*show_edge_fn)(struct commit *);

pack-bitmap-write.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -148,8 +148,7 @@ static uint32_t find_object_pos(const unsigned char *sha1)
148148
return entry->in_pack_pos;
149149
}
150150

151-
static void show_object(struct object *object, const struct name_path *path,
152-
const char *last, void *data)
151+
static void show_object(struct object *object, const char *name, void *data)
153152
{
154153
struct bitmap *base = data;
155154
bitmap_set(base, find_object_pos(object->sha1));

pack-bitmap.c

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -422,19 +422,15 @@ static int ext_index_add_object(struct object *object, const char *name)
422422
return bitmap_pos + bitmap_git.pack->num_objects;
423423
}
424424

425-
static void show_object(struct object *object, const struct name_path *path,
426-
const char *last, void *data)
425+
static void show_object(struct object *object, const char *name, void *data)
427426
{
428427
struct bitmap *base = data;
429428
int bitmap_pos;
430429

431430
bitmap_pos = bitmap_position(object->sha1);
432431

433-
if (bitmap_pos < 0) {
434-
char *name = path_name(path, last);
432+
if (bitmap_pos < 0)
435433
bitmap_pos = ext_index_add_object(object, name);
436-
free(name);
437-
}
438434

439435
bitmap_set(base, bitmap_pos);
440436
}
@@ -902,9 +898,8 @@ struct bitmap_test_data {
902898
size_t seen;
903899
};
904900

905-
static void test_show_object(struct object *object,
906-
const struct name_path *path,
907-
const char *last, void *data)
901+
static void test_show_object(struct object *object, const char *name,
902+
void *data)
908903
{
909904
struct bitmap_test_data *tdata = data;
910905
int bitmap_pos;

reachable.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,15 +36,14 @@ static int add_one_ref(const char *path, const unsigned char *sha1, int flag, vo
3636
* The traversal will have already marked us as SEEN, so we
3737
* only need to handle any progress reporting here.
3838
*/
39-
static void mark_object(struct object *obj, const struct name_path *path,
40-
const char *name, void *data)
39+
static void mark_object(struct object *obj, const char *name, void *data)
4140
{
4241
update_progress(data);
4342
}
4443

4544
static void mark_commit(struct commit *c, void *data)
4645
{
47-
mark_object(&c->object, NULL, NULL, data);
46+
mark_object(&c->object, NULL, data);
4847
}
4948

5049
struct recent_data {

0 commit comments

Comments
 (0)