iemejia commented on code in PR #3842: URL: https://github.com/apache/avro/pull/3842#discussion_r3563698418
########## lang/c/tests/test_avro_4275.c: ########## @@ -0,0 +1,377 @@ +/* + * 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. + */ + +/** + * Regression test for INT64_MIN negation overflow in read_array_value() + * and read_map_value() (CWE-190). + * + * The Avro binary format encodes block counts as zigzag-encoded varints. + * A negative block count means the absolute value is the actual count, + * preceded by a byte-size field. When block_count == INT64_MIN, the + * negation overflows (undefined behavior in C). This test verifies that + * the decoder rejects such malformed input gracefully. + */ + +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <stdint.h> +#include <avro.h> + Review Comment: Good point. Updated to assert `rc == EINVAL` specifically, so the test will fail if the guard regresses and the error comes from elsewhere. ########## lang/c/tests/test_avro_4275.c: ########## @@ -0,0 +1,377 @@ +/* + * 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. + */ + +/** + * Regression test for INT64_MIN negation overflow in read_array_value() + * and read_map_value() (CWE-190). + * + * The Avro binary format encodes block counts as zigzag-encoded varints. + * A negative block count means the absolute value is the actual count, + * preceded by a byte-size field. When block_count == INT64_MIN, the + * negation overflows (undefined behavior in C). This test verifies that + * the decoder rejects such malformed input gracefully. + */ + +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <stdint.h> +#include <avro.h> + +/* + * Zigzag encoding of INT64_MIN is 0xFFFFFFFFFFFFFFFF, which encodes as + * the 10-byte varint: FF FF FF FF FF FF FF FF FF 01 + */ +static const char int64min_block_count[] = { + '\xFF', '\xFF', '\xFF', '\xFF', '\xFF', + '\xFF', '\xFF', '\xFF', '\xFF', '\x01' +}; + +/* + * A valid array with negative block count: [-3] means 3 items follow, + * preceded by the block byte-size. + * + * Layout: + * 05 = varint 5 = zigzag(-3) => block_count = -3 + * 06 = varint 6 = zigzag(3) => block_size = 3 bytes + * 14 28 3C = zigzag ints: 10, 20, 30 + * 00 = terminator (block_count = 0) + */ +static const char valid_neg_block_array[] = { + '\x05', /* block_count = -3 */ + '\x06', /* block_size = 3 */ + '\x14', '\x28', '\x3C', /* ints: 10, 20, 30 */ + '\x00' /* terminator */ +}; + +/* + * A valid map with negative block count: [-2] means 2 entries follow. + * + * Layout: + * 03 = varint 3 = zigzag(-2) => block_count = -2 + * 0C = varint 12 = zigzag(6) => block_size = 6 bytes + * 02 61 14 = key "a" (len=1, 'a'), value int 10 + * 02 62 28 = key "b" (len=1, 'b'), value int 20 + * 00 = terminator + */ +static const char valid_neg_block_map[] = { + '\x03', /* block_count = -2 */ + '\x0C', /* block_size = 6 */ + '\x02', '\x61', '\x14', /* key "a", value 10 */ + '\x02', '\x62', '\x28', /* key "b", value 20 */ + '\x00' /* terminator */ +}; + +static int test_array_int64min(void) +{ + int rc; + avro_schema_t schema = NULL; + avro_value_iface_t *iface = NULL; + avro_value_t value; + avro_reader_t reader = NULL; + + rc = avro_schema_from_json_literal( + "{\"type\":\"array\",\"items\":\"int\"}", &schema); + if (rc != 0) { + fprintf(stderr, "Failed to parse array schema: %s\n", + avro_strerror()); + return 1; + } + + iface = avro_generic_class_from_schema(schema); + if (iface == NULL) { + fprintf(stderr, "Failed to create array iface\n"); + avro_schema_decref(schema); + return 1; + } + + rc = avro_generic_value_new(iface, &value); + if (rc != 0) { + fprintf(stderr, "Failed to create array value: %s\n", + avro_strerror()); + avro_value_iface_decref(iface); + avro_schema_decref(schema); + return 1; + } + + reader = avro_reader_memory(int64min_block_count, + sizeof(int64min_block_count)); + if (reader == NULL) { + fprintf(stderr, "Failed to create reader\n"); + avro_value_decref(&value); + avro_value_iface_decref(iface); + avro_schema_decref(schema); + return 1; + } + + /* This MUST fail gracefully (return error) rather than looping + * unboundedly or triggering undefined behavior. */ + rc = avro_value_read(reader, &value); + if (rc == 0) { + fprintf(stderr, + "FAIL: INT64_MIN array block count was not rejected\n"); + avro_reader_free(reader); + avro_value_decref(&value); + avro_value_iface_decref(iface); + avro_schema_decref(schema); + return 1; + } Review Comment: Fixed — now checks for `EINVAL` explicitly and prints the actual `rc` on mismatch. ########## lang/c/tests/test_avro_4275.c: ########## @@ -0,0 +1,377 @@ +/* + * 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. + */ + +/** + * Regression test for INT64_MIN negation overflow in read_array_value() + * and read_map_value() (CWE-190). + * + * The Avro binary format encodes block counts as zigzag-encoded varints. + * A negative block count means the absolute value is the actual count, + * preceded by a byte-size field. When block_count == INT64_MIN, the + * negation overflows (undefined behavior in C). This test verifies that + * the decoder rejects such malformed input gracefully. + */ + +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <stdint.h> +#include <avro.h> + +/* + * Zigzag encoding of INT64_MIN is 0xFFFFFFFFFFFFFFFF, which encodes as + * the 10-byte varint: FF FF FF FF FF FF FF FF FF 01 + */ +static const char int64min_block_count[] = { + '\xFF', '\xFF', '\xFF', '\xFF', '\xFF', + '\xFF', '\xFF', '\xFF', '\xFF', '\x01' +}; + +/* + * A valid array with negative block count: [-3] means 3 items follow, + * preceded by the block byte-size. + * + * Layout: + * 05 = varint 5 = zigzag(-3) => block_count = -3 + * 06 = varint 6 = zigzag(3) => block_size = 3 bytes + * 14 28 3C = zigzag ints: 10, 20, 30 + * 00 = terminator (block_count = 0) + */ +static const char valid_neg_block_array[] = { + '\x05', /* block_count = -3 */ + '\x06', /* block_size = 3 */ + '\x14', '\x28', '\x3C', /* ints: 10, 20, 30 */ + '\x00' /* terminator */ +}; + +/* + * A valid map with negative block count: [-2] means 2 entries follow. + * + * Layout: + * 03 = varint 3 = zigzag(-2) => block_count = -2 + * 0C = varint 12 = zigzag(6) => block_size = 6 bytes + * 02 61 14 = key "a" (len=1, 'a'), value int 10 + * 02 62 28 = key "b" (len=1, 'b'), value int 20 + * 00 = terminator + */ +static const char valid_neg_block_map[] = { + '\x03', /* block_count = -2 */ + '\x0C', /* block_size = 6 */ + '\x02', '\x61', '\x14', /* key "a", value 10 */ + '\x02', '\x62', '\x28', /* key "b", value 20 */ + '\x00' /* terminator */ +}; + +static int test_array_int64min(void) +{ + int rc; + avro_schema_t schema = NULL; + avro_value_iface_t *iface = NULL; + avro_value_t value; + avro_reader_t reader = NULL; + + rc = avro_schema_from_json_literal( + "{\"type\":\"array\",\"items\":\"int\"}", &schema); + if (rc != 0) { + fprintf(stderr, "Failed to parse array schema: %s\n", + avro_strerror()); + return 1; + } + + iface = avro_generic_class_from_schema(schema); + if (iface == NULL) { + fprintf(stderr, "Failed to create array iface\n"); + avro_schema_decref(schema); + return 1; + } + + rc = avro_generic_value_new(iface, &value); + if (rc != 0) { + fprintf(stderr, "Failed to create array value: %s\n", + avro_strerror()); + avro_value_iface_decref(iface); + avro_schema_decref(schema); + return 1; + } + + reader = avro_reader_memory(int64min_block_count, + sizeof(int64min_block_count)); + if (reader == NULL) { + fprintf(stderr, "Failed to create reader\n"); + avro_value_decref(&value); + avro_value_iface_decref(iface); + avro_schema_decref(schema); + return 1; + } + + /* This MUST fail gracefully (return error) rather than looping + * unboundedly or triggering undefined behavior. */ + rc = avro_value_read(reader, &value); + if (rc == 0) { + fprintf(stderr, + "FAIL: INT64_MIN array block count was not rejected\n"); + avro_reader_free(reader); + avro_value_decref(&value); + avro_value_iface_decref(iface); + avro_schema_decref(schema); + return 1; + } + + printf("PASS: INT64_MIN array block count rejected: %s\n", + avro_strerror()); + + avro_reader_free(reader); + avro_value_decref(&value); + avro_value_iface_decref(iface); + avro_schema_decref(schema); + return 0; +} + +static int test_map_int64min(void) +{ + int rc; + avro_schema_t schema = NULL; + avro_value_iface_t *iface = NULL; + avro_value_t value; + avro_reader_t reader = NULL; + + rc = avro_schema_from_json_literal( + "{\"type\":\"map\",\"values\":\"int\"}", &schema); + if (rc != 0) { + fprintf(stderr, "Failed to parse map schema: %s\n", + avro_strerror()); + return 1; + } + + iface = avro_generic_class_from_schema(schema); + if (iface == NULL) { + fprintf(stderr, "Failed to create map iface\n"); + avro_schema_decref(schema); + return 1; + } + + rc = avro_generic_value_new(iface, &value); + if (rc != 0) { + fprintf(stderr, "Failed to create map value: %s\n", + avro_strerror()); + avro_value_iface_decref(iface); + avro_schema_decref(schema); + return 1; + } + + reader = avro_reader_memory(int64min_block_count, + sizeof(int64min_block_count)); + if (reader == NULL) { + fprintf(stderr, "Failed to create reader\n"); + avro_value_decref(&value); + avro_value_iface_decref(iface); + avro_schema_decref(schema); + return 1; + } + + /* This MUST fail gracefully. */ + rc = avro_value_read(reader, &value); + if (rc == 0) { + fprintf(stderr, + "FAIL: INT64_MIN map block count was not rejected\n"); + avro_reader_free(reader); + avro_value_decref(&value); + avro_value_iface_decref(iface); + avro_schema_decref(schema); + return 1; + } Review Comment: Same fix applied here. Both INT64_MIN tests now assert `EINVAL`. -- 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]
