Skip to content

Commit 388b96d

Browse files
peffgitster
authored andcommitted
upload-pack: use oidset for deepen_not list
We record the oid of every deepen-not line the client sends to us. For a well-behaved client, the resulting array should be bounded by the number of unique refs we have. But because there's no de-duplication, a malicious client can cause the array to grow unbounded by just sending the same "refs/heads/foo" over and over (assuming such a ref exists). Since the deepen-not list is just being fed to a "rev-list --not" traversal, the order of items doesn't matter. So we can replace the oid_array with an oidset which notices and skips duplicates. That bounds the memory in malicious cases to be linear in the number of unique refs. And even in non-malicious cases, there may be a slight improvement in memory usage if multiple refs point to the same oid (though in practice this list is probably pretty tiny anyway, as it comes from the user specifying "--shallow-exclude" on the client fetch). Note that in the trace2 output we'll now output the number of de-duplicated objects, rather than the total number of "deepen-not" lines we received. This is arguably a more useful value for tracing / debugging anyway. Reported-by: Benjamin Flesch <[email protected]> Signed-off-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 720ba25 commit 388b96d

File tree

1 file changed

+11
-10
lines changed

1 file changed

+11
-10
lines changed

upload-pack.c

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ struct upload_pack_data {
6565
struct strvec hidden_refs;
6666

6767
struct object_array shallows;
68-
struct oid_array deepen_not;
68+
struct oidset deepen_not;
6969
struct object_array extra_edge_obj;
7070
int depth;
7171
timestamp_t deepen_since;
@@ -125,7 +125,7 @@ static void upload_pack_data_init(struct upload_pack_data *data)
125125
struct object_array want_obj = OBJECT_ARRAY_INIT;
126126
struct object_array have_obj = OBJECT_ARRAY_INIT;
127127
struct object_array shallows = OBJECT_ARRAY_INIT;
128-
struct oid_array deepen_not = OID_ARRAY_INIT;
128+
struct oidset deepen_not = OID_ARRAY_INIT;
129129
struct string_list uri_protocols = STRING_LIST_INIT_DUP;
130130
struct object_array extra_edge_obj = OBJECT_ARRAY_INIT;
131131
struct string_list allowed_filters = STRING_LIST_INIT_DUP;
@@ -158,7 +158,7 @@ static void upload_pack_data_clear(struct upload_pack_data *data)
158158
object_array_clear(&data->want_obj);
159159
object_array_clear(&data->have_obj);
160160
object_array_clear(&data->shallows);
161-
oid_array_clear(&data->deepen_not);
161+
oidset_clear(&data->deepen_not);
162162
object_array_clear(&data->extra_edge_obj);
163163
list_objects_filter_release(&data->filter_options);
164164
string_list_clear(&data->allowed_filters, 0);
@@ -923,12 +923,13 @@ static int send_shallow_list(struct upload_pack_data *data)
923923
strvec_push(&av, "rev-list");
924924
if (data->deepen_since)
925925
strvec_pushf(&av, "--max-age=%"PRItime, data->deepen_since);
926-
if (data->deepen_not.nr) {
926+
if (oidset_size(&data->deepen_not)) {
927+
const struct object_id *oid;
928+
struct oidset_iter iter;
927929
strvec_push(&av, "--not");
928-
for (i = 0; i < data->deepen_not.nr; i++) {
929-
struct object_id *oid = data->deepen_not.oid + i;
930+
oidset_iter_init(&data->deepen_not, &iter);
931+
while ((oid = oidset_iter_next(&iter)))
930932
strvec_push(&av, oid_to_hex(oid));
931-
}
932933
strvec_push(&av, "--not");
933934
}
934935
for (i = 0; i < data->want_obj.nr; i++) {
@@ -1004,15 +1005,15 @@ static int process_deepen_since(const char *line, timestamp_t *deepen_since, int
10041005
return 0;
10051006
}
10061007

1007-
static int process_deepen_not(const char *line, struct oid_array *deepen_not, int *deepen_rev_list)
1008+
static int process_deepen_not(const char *line, struct oidset *deepen_not, int *deepen_rev_list)
10081009
{
10091010
const char *arg;
10101011
if (skip_prefix(line, "deepen-not ", &arg)) {
10111012
char *ref = NULL;
10121013
struct object_id oid;
10131014
if (expand_ref(the_repository, arg, strlen(arg), &oid, &ref) != 1)
10141015
die("git upload-pack: ambiguous deepen-not: %s", line);
1015-
oid_array_append(deepen_not, &oid);
1016+
oidset_insert(deepen_not, &oid);
10161017
free(ref);
10171018
*deepen_rev_list = 1;
10181019
return 1;
@@ -1554,7 +1555,7 @@ static void trace2_fetch_info(struct upload_pack_data *data)
15541555
jw_object_intmax(&jw, "depth", data->depth);
15551556
jw_object_intmax(&jw, "shallows", data->shallows.nr);
15561557
jw_object_bool(&jw, "deepen-since", data->deepen_since);
1557-
jw_object_intmax(&jw, "deepen-not", data->deepen_not.nr);
1558+
jw_object_intmax(&jw, "deepen-not", oidset_size(&data->deepen_not));
15581559
jw_object_bool(&jw, "deepen-relative", data->deepen_relative);
15591560
if (data->filter_options.choice)
15601561
jw_object_string(&jw, "filter", list_object_filter_config_name(data->filter_options.choice));

0 commit comments

Comments
 (0)