iemejia commented on code in PR #3858:
URL: https://github.com/apache/avro/pull/3858#discussion_r3565199445
##########
lang/c/src/value-read.c:
##########
@@ -38,6 +39,79 @@ static int
read_value(avro_reader_t reader, avro_value_t *dest);
+/*
+ * Minimum number of bytes a single value of the given schema can occupy on the
+ * wire. Used to reject an array/map block count that could not be backed by
the
+ * bytes remaining. A type that can encode to zero bytes (null) returns 0,
which
+ * disables the collection check for it (so an array of nulls is not falsely
+ * rejected). A recursion guard breaks self-referencing schemas.
+ */
+static int64_t
+min_bytes_per_element(avro_schema_t schema, int depth)
+{
+ if (schema == NULL) {
+ return 0;
+ }
+ if (depth > 64) {
+ /* A cyclic or pathologically deep schema. Return 1 (not 0) so
the
+ * collection check stays enabled rather than being silently
+ * bypassed; a valid recursive value always encodes to >= 1
byte. */
+ return 1;
+ }
+ switch (avro_typeof(schema)) {
+ case AVRO_NULL:
+ return 0;
+ case AVRO_FLOAT:
+ return 4;
+ case AVRO_DOUBLE:
+ return 8;
+ case AVRO_FIXED:
+ return avro_schema_fixed_size(schema);
+ case AVRO_RECORD: {
+ size_t n = avro_schema_record_size(schema);
+ int64_t total = 0;
+ size_t i;
+ for (i = 0; i < n; i++) {
+ avro_schema_t field =
+ avro_schema_record_field_get_by_index(schema, i);
+ total += min_bytes_per_element(field, depth + 1);
+ }
Review Comment:
Fixed in b549013: the record field-minima sum now saturates to INT64_MAX
instead of overflowing, so a wrapped negative total can no longer disable the
collection check.
##########
lang/c/src/value-read.c:
##########
@@ -83,6 +164,12 @@ read_map_value(avro_reader_t reader, avro_value_t *dest)
size_t index = 0; /* index within the entire array */
int64_t block_count;
int64_t block_size;
+ int64_t min_bytes;
+ avro_schema_t map_schema = avro_value_get_schema(dest);
+
+ /* Map keys are strings (>= 1 byte length prefix) plus the value. */
+ min_bytes = 1 + min_bytes_per_element(
+ map_schema ? avro_schema_map_values(map_schema) : NULL, 0);
Review Comment:
Fixed in b549013: the map computation saturates the key's +1 (only adds when
below INT64_MAX), so a maxed-out value minimum cannot wrap.
##########
lang/c/tests/test_avro_4293.c:
##########
@@ -0,0 +1,229 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to you under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * https://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
+ * implied. See the License for the specific language governing
+ * permissions and limitations under the License.
+ */
+
+/*
+ * A bytes/string value is encoded as a length prefix followed by that many
+ * bytes of data. A malicious or truncated input can declare a huge length with
+ * little or no actual data, which previously caused a correspondingly huge
+ * allocation before the shortfall was noticed. When the reader knows how many
+ * bytes remain (a memory-backed reader), the declared length must be rejected
+ * before allocating for it.
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <stdint.h>
+#include <errno.h>
+#include <avro.h>
+
+/* Decodes buf against the given schema and returns the avro_value_read rc.
+ * A non-zero rc means the read was rejected. */
+static int try_decode(avro_value_iface_t *iface, const char *buf, size_t
buf_size)
+{
+ int rc;
+ avro_value_t decoded;
+ avro_reader_t reader;
+
+ rc = avro_generic_value_new(iface, &decoded);
+ if (rc != 0) {
+ fprintf(stderr, "avro_generic_value_new failed: %s\n",
avro_strerror());
+ return -1;
+ }
+
+ reader = avro_reader_memory(buf, buf_size);
+ if (reader == NULL) {
+ fprintf(stderr, "avro_reader_memory failed\n");
+ avro_value_decref(&decoded);
+ return -1;
+ }
+
+ rc = avro_value_read(reader, &decoded);
+
+ avro_reader_free(reader);
+ avro_value_decref(&decoded);
+ return rc;
+}
+
+static int check_rejects_oversized(const char *schema_literal, const char
*label)
+{
+ avro_schema_t schema = NULL;
+ avro_value_iface_t *iface = NULL;
+ int rc;
+ int ret = EXIT_FAILURE;
+
+ /*
+ * A length prefix declaring 127 bytes (zig-zag long 254 -> varint
+ * 0xFE 0x01), followed by no data at all. The reader has 0 bytes
+ * remaining after the prefix, so a declared length of 127 must be
+ * rejected before allocating.
+ */
+ const char oversized[] = { (char) 0xFE, (char) 0x01 };
+
+ if (avro_schema_from_json_length(schema_literal, strlen(schema_literal),
+ &schema) != 0) {
+ fprintf(stderr, "%s: failed to parse schema: %s\n", label,
avro_strerror());
+ return EXIT_FAILURE;
+ }
+
+ iface = avro_generic_class_from_schema(schema);
+ if (iface == NULL) {
+ fprintf(stderr, "%s: failed to create iface\n", label);
+ avro_schema_decref(schema);
+ return EXIT_FAILURE;
+ }
+
+ rc = try_decode(iface, oversized, sizeof(oversized));
+ if (rc == 0) {
+ fprintf(stderr, "%s: FAIL - oversized length was accepted\n",
label);
+ } else if (rc != EINVAL) {
+ /* The availability checks reject with EINVAL before allocating.
+ * A different error (e.g. ENOSPC from failing to read after a
+ * large allocation) would indicate the pre-fix behavior. */
+ fprintf(stderr, "%s: FAIL - rejected with rc=%d (expected
EINVAL): %s\n",
+ label, rc, avro_strerror());
+ } else {
+ fprintf(stderr, "%s: oversized length rejected as expected:
%s\n",
+ label, avro_strerror());
+ ret = EXIT_SUCCESS;
+ }
+
+ avro_value_iface_decref(iface);
+ avro_schema_decref(schema);
+ return ret;
+}
+
+static int check_accepts_valid(void)
+{
+ avro_schema_t schema = NULL;
+ avro_value_iface_t *iface = NULL;
+ int rc;
+ int ret = EXIT_FAILURE;
+ const char *text = NULL;
+ size_t text_size = 0;
+ avro_value_t decoded;
+ avro_reader_t reader;
+
+ /* A well-formed 3-byte string "abc": length 3 (zig-zag 6 -> 0x06),
+ * then the bytes 'a','b','c'. This must still decode. */
+ const char valid[] = { 0x06, 'a', 'b', 'c' };
+
+ if (avro_schema_from_json_literal("\"string\"", &schema) != 0) {
+ fprintf(stderr, "valid: failed to parse schema: %s\n",
avro_strerror());
+ return EXIT_FAILURE;
+ }
+ iface = avro_generic_class_from_schema(schema);
+ if (iface == NULL) {
+ fprintf(stderr, "valid: failed to create iface\n");
+ avro_schema_decref(schema);
+ return EXIT_FAILURE;
+ }
+
+ if (avro_generic_value_new(iface, &decoded) != 0) {
+ avro_value_iface_decref(iface);
+ avro_schema_decref(schema);
+ return EXIT_FAILURE;
+ }
+ reader = avro_reader_memory(valid, sizeof(valid));
+ rc = avro_value_read(reader, &decoded);
Review Comment:
Fixed in b549013: check_accepts_valid() now checks avro_reader_memory() for
NULL before use.
##########
lang/c/tests/test_avro_4293.c:
##########
@@ -0,0 +1,229 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to you under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * https://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
+ * implied. See the License for the specific language governing
+ * permissions and limitations under the License.
+ */
+
+/*
+ * A bytes/string value is encoded as a length prefix followed by that many
+ * bytes of data. A malicious or truncated input can declare a huge length with
+ * little or no actual data, which previously caused a correspondingly huge
+ * allocation before the shortfall was noticed. When the reader knows how many
+ * bytes remain (a memory-backed reader), the declared length must be rejected
+ * before allocating for it.
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <stdint.h>
+#include <errno.h>
+#include <avro.h>
+
+/* Decodes buf against the given schema and returns the avro_value_read rc.
+ * A non-zero rc means the read was rejected. */
+static int try_decode(avro_value_iface_t *iface, const char *buf, size_t
buf_size)
+{
+ int rc;
+ avro_value_t decoded;
+ avro_reader_t reader;
+
+ rc = avro_generic_value_new(iface, &decoded);
+ if (rc != 0) {
+ fprintf(stderr, "avro_generic_value_new failed: %s\n",
avro_strerror());
+ return -1;
+ }
+
+ reader = avro_reader_memory(buf, buf_size);
+ if (reader == NULL) {
+ fprintf(stderr, "avro_reader_memory failed\n");
+ avro_value_decref(&decoded);
+ return -1;
+ }
+
+ rc = avro_value_read(reader, &decoded);
+
+ avro_reader_free(reader);
+ avro_value_decref(&decoded);
+ return rc;
+}
+
+static int check_rejects_oversized(const char *schema_literal, const char
*label)
+{
+ avro_schema_t schema = NULL;
+ avro_value_iface_t *iface = NULL;
+ int rc;
+ int ret = EXIT_FAILURE;
+
+ /*
+ * A length prefix declaring 127 bytes (zig-zag long 254 -> varint
+ * 0xFE 0x01), followed by no data at all. The reader has 0 bytes
+ * remaining after the prefix, so a declared length of 127 must be
+ * rejected before allocating.
+ */
+ const char oversized[] = { (char) 0xFE, (char) 0x01 };
+
+ if (avro_schema_from_json_length(schema_literal, strlen(schema_literal),
+ &schema) != 0) {
+ fprintf(stderr, "%s: failed to parse schema: %s\n", label,
avro_strerror());
+ return EXIT_FAILURE;
+ }
+
+ iface = avro_generic_class_from_schema(schema);
+ if (iface == NULL) {
+ fprintf(stderr, "%s: failed to create iface\n", label);
+ avro_schema_decref(schema);
+ return EXIT_FAILURE;
+ }
+
+ rc = try_decode(iface, oversized, sizeof(oversized));
+ if (rc == 0) {
+ fprintf(stderr, "%s: FAIL - oversized length was accepted\n",
label);
+ } else if (rc != EINVAL) {
+ /* The availability checks reject with EINVAL before allocating.
+ * A different error (e.g. ENOSPC from failing to read after a
+ * large allocation) would indicate the pre-fix behavior. */
+ fprintf(stderr, "%s: FAIL - rejected with rc=%d (expected
EINVAL): %s\n",
+ label, rc, avro_strerror());
+ } else {
+ fprintf(stderr, "%s: oversized length rejected as expected:
%s\n",
+ label, avro_strerror());
+ ret = EXIT_SUCCESS;
+ }
+
+ avro_value_iface_decref(iface);
+ avro_schema_decref(schema);
+ return ret;
+}
+
+static int check_accepts_valid(void)
+{
+ avro_schema_t schema = NULL;
+ avro_value_iface_t *iface = NULL;
+ int rc;
+ int ret = EXIT_FAILURE;
+ const char *text = NULL;
+ size_t text_size = 0;
+ avro_value_t decoded;
+ avro_reader_t reader;
+
+ /* A well-formed 3-byte string "abc": length 3 (zig-zag 6 -> 0x06),
+ * then the bytes 'a','b','c'. This must still decode. */
+ const char valid[] = { 0x06, 'a', 'b', 'c' };
+
+ if (avro_schema_from_json_literal("\"string\"", &schema) != 0) {
+ fprintf(stderr, "valid: failed to parse schema: %s\n",
avro_strerror());
+ return EXIT_FAILURE;
+ }
+ iface = avro_generic_class_from_schema(schema);
+ if (iface == NULL) {
+ fprintf(stderr, "valid: failed to create iface\n");
+ avro_schema_decref(schema);
+ return EXIT_FAILURE;
+ }
+
+ if (avro_generic_value_new(iface, &decoded) != 0) {
+ avro_value_iface_decref(iface);
+ avro_schema_decref(schema);
+ return EXIT_FAILURE;
+ }
+ reader = avro_reader_memory(valid, sizeof(valid));
+ rc = avro_value_read(reader, &decoded);
+ if (rc == 0 && avro_value_get_string(&decoded, &text, &text_size) == 0
&&
+ text_size == 4 && strcmp(text, "abc") == 0) {
+ fprintf(stderr, "valid: well-formed string decoded as
expected\n");
+ ret = EXIT_SUCCESS;
+ } else {
+ fprintf(stderr, "valid: FAIL - well-formed string did not
decode (rc=%d)\n", rc);
+ }
+
+ avro_reader_free(reader);
+ avro_value_decref(&decoded);
+ avro_value_iface_decref(iface);
+ avro_schema_decref(schema);
+ return ret;
+}
+
+/* An array of nulls: null elements occupy zero bytes, so a large declared
+ * count is legitimate and must not be rejected. */
+static int check_accepts_null_array(void)
+{
+ avro_schema_t schema = NULL;
+ avro_value_iface_t *iface = NULL;
+ avro_value_t decoded;
+ avro_reader_t reader;
+ int rc;
+ int ret = EXIT_FAILURE;
+ size_t count = 0;
+
+ /* count 127 (zig-zag 254 -> 0xFE 0x01), then the end-of-array marker
0. */
+ const char null_array[] = { (char) 0xFE, (char) 0x01, 0x00 };
+
+ if
(avro_schema_from_json_length("{\"type\":\"array\",\"items\":\"null\"}",
+
strlen("{\"type\":\"array\",\"items\":\"null\"}"),
+ &schema) != 0) {
+ fprintf(stderr, "null-array: failed to parse schema: %s\n",
avro_strerror());
+ return EXIT_FAILURE;
+ }
+ iface = avro_generic_class_from_schema(schema);
+ if (iface == NULL) {
+ avro_schema_decref(schema);
+ return EXIT_FAILURE;
+ }
+ if (avro_generic_value_new(iface, &decoded) != 0) {
+ avro_value_iface_decref(iface);
+ avro_schema_decref(schema);
+ return EXIT_FAILURE;
+ }
+ reader = avro_reader_memory(null_array, sizeof(null_array));
+ rc = avro_value_read(reader, &decoded);
Review Comment:
Fixed in b549013: check_accepts_null_array() now checks avro_reader_memory()
for NULL before use.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]