Skip to content
This repository was archived by the owner on Sep 5, 2024. It is now read-only.

Commit 1f32d8e

Browse files
rdoeffingerguoyejun
authored andcommitted
dnn_backend_native: Add overflow check for length calculation.
We should not silently allocate an incorrect sized buffer. Fixes trac issue #8718. Signed-off-by: Reimar Döffinger <[email protected]> Reviewed-by: Michael Niedermayer <[email protected]> Reviewed-by: Guo, Yejun <[email protected]>
1 parent 7cbb6ee commit 1f32d8e

8 files changed

+23
-1
lines changed

libavfilter/dnn/dnn_backend_native.c

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,8 @@ static DNNReturnType set_input_output_native(void *model, DNNData *input, const
7979

8080
av_freep(&oprd->data);
8181
oprd->length = calculate_operand_data_length(oprd);
82+
if (oprd->length <= 0)
83+
return DNN_ERROR;
8284
oprd->data = av_malloc(oprd->length);
8385
if (!oprd->data)
8486
return DNN_ERROR;
@@ -295,7 +297,13 @@ int32_t calculate_operand_dims_count(const DnnOperand *oprd)
295297
int32_t calculate_operand_data_length(const DnnOperand* oprd)
296298
{
297299
// currently, we just support DNN_FLOAT
298-
return oprd->dims[0] * oprd->dims[1] * oprd->dims[2] * oprd->dims[3] * sizeof(float);
300+
uint64_t len = sizeof(float);
301+
for (int i = 0; i < 4; i++) {
302+
len *= oprd->dims[i];
303+
if (len > INT32_MAX)
304+
return 0;
305+
}
306+
return len;
299307
}
300308

301309
void ff_dnn_free_model_native(DNNModel **model)

libavfilter/dnn/dnn_backend_native.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,8 @@ DNNReturnType ff_dnn_execute_model_native(const DNNModel *model, DNNData *output
120120

121121
void ff_dnn_free_model_native(DNNModel **model);
122122

123+
// NOTE: User must check for error (return value <= 0) to handle
124+
// case like integer overflow.
123125
int32_t calculate_operand_data_length(const DnnOperand *oprd);
124126
int32_t calculate_operand_dims_count(const DnnOperand *oprd);
125127
#endif

libavfilter/dnn/dnn_backend_native_layer_conv2d.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,8 @@ int dnn_execute_layer_conv2d(DnnOperand *operands, const int32_t *input_operand_
113113
output_operand->dims[3] = conv_params->output_num;
114114
output_operand->data_type = operands[input_operand_index].data_type;
115115
output_operand->length = calculate_operand_data_length(output_operand);
116+
if (output_operand->length <= 0)
117+
return -1;
116118
output_operand->data = av_realloc(output_operand->data, output_operand->length);
117119
if (!output_operand->data)
118120
return -1;

libavfilter/dnn/dnn_backend_native_layer_depth2space.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,8 @@ int dnn_execute_layer_depth2space(DnnOperand *operands, const int32_t *input_ope
7575
output_operand->dims[3] = new_channels;
7676
output_operand->data_type = operands[input_operand_index].data_type;
7777
output_operand->length = calculate_operand_data_length(output_operand);
78+
if (output_operand->length <= 0)
79+
return -1;
7880
output_operand->data = av_realloc(output_operand->data, output_operand->length);
7981
if (!output_operand->data)
8082
return -1;

libavfilter/dnn/dnn_backend_native_layer_mathbinary.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,8 @@ int dnn_execute_layer_math_binary(DnnOperand *operands, const int32_t *input_ope
9191

9292
output->data_type = input->data_type;
9393
output->length = calculate_operand_data_length(output);
94+
if (output->length <= 0)
95+
return DNN_ERROR;
9496
output->data = av_realloc(output->data, output->length);
9597
if (!output->data)
9698
return DNN_ERROR;

libavfilter/dnn/dnn_backend_native_layer_mathunary.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,8 @@ int dnn_execute_layer_math_unary(DnnOperand *operands, const int32_t *input_oper
6565

6666
output->data_type = input->data_type;
6767
output->length = calculate_operand_data_length(output);
68+
if (output->length <= 0)
69+
return DNN_ERROR;
6870
output->data = av_realloc(output->data, output->length);
6971
if (!output->data)
7072
return DNN_ERROR;

libavfilter/dnn/dnn_backend_native_layer_maximum.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,8 @@ int dnn_execute_layer_maximum(DnnOperand *operands, const int32_t *input_operand
6464

6565
output->data_type = input->data_type;
6666
output->length = calculate_operand_data_length(output);
67+
if (output->length <= 0)
68+
return DNN_ERROR;
6769
output->data = av_realloc(output->data, output->length);
6870
if (!output->data)
6971
return DNN_ERROR;

libavfilter/dnn/dnn_backend_native_layer_pad.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,8 @@ int dnn_execute_layer_pad(DnnOperand *operands, const int32_t *input_operand_ind
111111
output_operand->dims[3] = new_channel;
112112
output_operand->data_type = operands[input_operand_index].data_type;
113113
output_operand->length = calculate_operand_data_length(output_operand);
114+
if (output_operand->length <= 0)
115+
return -1;
114116
output_operand->data = av_realloc(output_operand->data, output_operand->length);
115117
if (!output_operand->data)
116118
return -1;

0 commit comments

Comments
 (0)