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 <benjaminflesch@icloud.com> Signed-off-by: Jeff King <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jeff King committed Feb 28, 2024 at 17:37 UTC 388b96df319c62e9c8d984921b8967db37481d8a
1 file changed +11 -10
upload-pack.c
+11 -10
@@ -65,7 +65,7 @@ struct upload_pack_data {
65 struct strvec hidden_refs;
66
67 struct object_array shallows;
68 - struct oid_array deepen_not;
68 + struct oidset deepen_not;
69 struct object_array extra_edge_obj;
70 int depth;
71 timestamp_t deepen_since;
@@ -125,7 +125,7 @@ static void upload_pack_data_init(struct upload_pack_data *data)
125 struct object_array want_obj = OBJECT_ARRAY_INIT;
126 struct object_array have_obj = OBJECT_ARRAY_INIT;
127 struct object_array shallows = OBJECT_ARRAY_INIT;
128 - struct oid_array deepen_not = OID_ARRAY_INIT;
128 + struct oidset deepen_not = OID_ARRAY_INIT;
129 struct string_list uri_protocols = STRING_LIST_INIT_DUP;
130 struct object_array extra_edge_obj = OBJECT_ARRAY_INIT;
131 struct string_list allowed_filters = STRING_LIST_INIT_DUP;
@@ -158,7 +158,7 @@ static void upload_pack_data_clear(struct upload_pack_data *data)
158 object_array_clear(&data->want_obj);
159 object_array_clear(&data->have_obj);
160 object_array_clear(&data->shallows);
161 - oid_array_clear(&data->deepen_not);
161 + oidset_clear(&data->deepen_not);
162 object_array_clear(&data->extra_edge_obj);
163 list_objects_filter_release(&data->filter_options);
164 string_list_clear(&data->allowed_filters, 0);
@@ -923,12 +923,13 @@ static int send_shallow_list(struct upload_pack_data *data)
923 strvec_push(&av, "rev-list");
924 if (data->deepen_since)
925 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;
929 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)))
932 strvec_push(&av, oid_to_hex(oid));
931 - }
933 strvec_push(&av, "--not");
934 }
935 for (i = 0; i < data->want_obj.nr; i++) {
@@ -1004,7 +1005,7 @@ static int process_deepen_since(const char *line, timestamp_t *deepen_since, int
1005 return 0;
1006 }
1007
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)
1009 {
1010 const char *arg;
1011 if (skip_prefix(line, "deepen-not ", &arg)) {
@@ -1012,7 +1013,7 @@ static int process_deepen_not(const char *line, struct oid_array *deepen_not, in
1013 struct object_id oid;
1014 if (expand_ref(the_repository, arg, strlen(arg), &oid, &ref) != 1)
1015 die("git upload-pack: ambiguous deepen-not: %s", line);
1015 - oid_array_append(deepen_not, &oid);
1016 + oidset_insert(deepen_not, &oid);
1017 free(ref);
1018 *deepen_rev_list = 1;
1019 return 1;
@@ -1554,7 +1555,7 @@ static void trace2_fetch_info(struct upload_pack_data *data)
1555 jw_object_intmax(&jw, "depth", data->depth);
1556 jw_object_intmax(&jw, "shallows", data->shallows.nr);
1557 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));
1559 jw_object_bool(&jw, "deepen-relative", data->deepen_relative);
1560 if (data->filter_options.choice)
1561 jw_object_string(&jw, "filter", list_object_filter_config_name(data->filter_options.choice));