Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions signal/micro/kernels/energy.cc
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,15 @@ TfLiteStatus EnergyPrepare(TfLiteContext* context, TfLiteNode* node) {
TF_LITE_ENSURE_TYPES_EQ(context, input->type, kTfLiteInt16);
TF_LITE_ENSURE_TYPES_EQ(context, output->type, kTfLiteUInt32);

// Validate start_index/end_index against the input tensor size to prevent
// out-of-bounds access in SpectrumToEnergy (energy.cc uses Complex<int16_t>,
// so the element count in complex pairs is dims[0] / 2).
auto* params = reinterpret_cast<TFLMSignalEnergyParams*>(node->user_data);
const int input_elems = input->dims->data[0] / 2; // complex pairs
TF_LITE_ENSURE(context, params->start_index >= 0);
TF_LITE_ENSURE(context, params->end_index > params->start_index);
TF_LITE_ENSURE(context, params->end_index <= input_elems);

micro_context->DeallocateTempTfLiteTensor(input);
micro_context->DeallocateTempTfLiteTensor(output);
return kTfLiteOk;
Expand Down
11 changes: 11 additions & 0 deletions signal/micro/kernels/filter_bank_spectral_subtraction.cc
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,17 @@ TfLiteStatus FilterBankSpectralSubtractionPrepare(TfLiteContext* context,

auto* params =
reinterpret_cast<TFLMSignalSpectralSubtractionParams*>(node->user_data);

// Validate that num_channels (from flexbuffer) matches all tensor sizes
// to prevent out-of-bounds access in FilterbankSpectralSubtraction.
const int input_elems = ElementCount(*input->dims);
const int output_elems = ElementCount(*output->dims);
const int noise_elems = ElementCount(*noise_estimate->dims);
TF_LITE_ENSURE(context, params->config.num_channels > 0);
TF_LITE_ENSURE_EQ(context, params->config.num_channels, input_elems);
TF_LITE_ENSURE_EQ(context, params->config.num_channels, output_elems);
TF_LITE_ENSURE_EQ(context, params->config.num_channels, noise_elems);

TfLiteTypeSizeOf(output->type, &params->noise_estimate_size);
params->noise_estimate_size *= ElementCount(*noise_estimate->dims);

Expand Down
5 changes: 5 additions & 0 deletions signal/micro/kernels/overlap_add.cc
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,11 @@ TfLiteStatus OverlapAddPrepare(TfLiteContext* context, TfLiteNode* node) {

params->frame_size = input_shape.Dims(input_shape.DimensionsCount() - 1);
params->n_frames = input_shape.Dims(input_shape.DimensionsCount() - 2);
// Validate dimension values and op param to prevent OOB/divide-by-zero.
TF_LITE_ENSURE(context, params->frame_size > 0);
TF_LITE_ENSURE(context, params->n_frames > 0);
TF_LITE_ENSURE(context, params->frame_step > 0);
TF_LITE_ENSURE(context, params->frame_step <= params->frame_size);
params->outer_dims =
input_shape.FlatSize() / (params->frame_size * params->n_frames);
params->state_buffers = static_cast<T**>(context->AllocatePersistentBuffer(
Expand Down
9 changes: 9 additions & 0 deletions signal/micro/kernels/pcan.cc
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,15 @@ TfLiteStatus PcanPrepare(TfLiteContext* context, TfLiteNode* node) {
TF_LITE_ENSURE_TYPES_EQ(context, gain_lut->type, kTfLiteInt16);
TF_LITE_ENSURE_TYPES_EQ(context, output->type, kTfLiteUInt32);

// Validate that input/noise_estimate/output dimensions match to prevent
// out-of-bounds access in ApplyPcanAutoGainControlFixed.
const int input_elems = ElementCount(*input->dims);
const int noise_elems = ElementCount(*noise_estimate->dims);
const int output_elems = ElementCount(*output->dims);
TF_LITE_ENSURE(context, input_elems > 0);
TF_LITE_ENSURE_EQ(context, input_elems, noise_elems);
TF_LITE_ENSURE_EQ(context, input_elems, output_elems);

micro_context->DeallocateTempTfLiteTensor(input);
micro_context->DeallocateTempTfLiteTensor(output);
micro_context->DeallocateTempTfLiteTensor(noise_estimate);
Expand Down
5 changes: 5 additions & 0 deletions signal/micro/kernels/rfft.cc
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,11 @@ TfLiteStatus RfftPrepare(TfLiteContext* context, TfLiteNode* node) {
RuntimeShape output_shape = GetTensorShape(output);
params->input_length = input_shape.Dims(input_shape.DimensionsCount() - 1);
params->input_size = input_shape.FlatSize();
// Validate that the input fits within the FFT scratch buffer to prevent
// heap-buffer-overflow in the Eval zero-padding memcpy/memset.
TF_LITE_ENSURE(context, params->fft_length > 0);
TF_LITE_ENSURE(context, params->input_length > 0);
TF_LITE_ENSURE(context, params->input_length <= params->fft_length);
// Divide by 2 because output is complex.
params->output_length =
output_shape.Dims(output_shape.DimensionsCount() - 1) / 2;
Expand Down
9 changes: 9 additions & 0 deletions signal/micro/kernels/window.cc
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,15 @@ TfLiteStatus WindowPrepare(TfLiteContext* context, TfLiteNode* node) {
RuntimeShape input_shape = GetTensorShape(input);
params->input_size = input_shape.FlatSize();

// Validate that input size is a multiple of the window (weights) size
// and that all buffers match, preventing OOB in ApplyWindow.
const int weight_size = weights->dims->data[0];
TF_LITE_ENSURE(context, weight_size > 0);
TF_LITE_ENSURE(context, params->input_size > 0);
TF_LITE_ENSURE_EQ(context, params->input_size % weight_size, 0);
TF_LITE_ENSURE_EQ(context, params->input_size,
GetTensorShape(output).FlatSize());

micro_context->DeallocateTempTfLiteTensor(input);
micro_context->DeallocateTempTfLiteTensor(weights);
micro_context->DeallocateTempTfLiteTensor(output);
Expand Down
Loading