Skip to content

LLM inference in C/C++

C++ ggml

llama : check LoRA tensor data is within file bounds (#27056)

Oğuzhan Akkaya10bf611e533d
diff --git a/src/llama-adapter.cpp b/src/llama-adapter.cpp
index 3e0fe66aff..e6678a66d2 100644
--- a/src/llama-adapter.cpp
+++ b/src/llama-adapter.cpp
@@ -396,8 +396,11 @@ static void llama_adapter_lora_init_impl(llama_model & model, const char * path_
llama_file gguf_file(path_lora, "rb");
std::vector<uint8_t> read_buf;
auto set_tensor = [&](ggml_tensor * orig, ggml_tensor * dev) {
- size_t offs = gguf_get_data_offset(ctx_gguf.get()) + gguf_get_tensor_offset(ctx_gguf.get(), gguf_find_tensor(ctx_gguf.get(), orig->name));
- size_t size = ggml_nbytes(orig);
+ const size_t offs = gguf_get_data_offset(ctx_gguf.get()) + gguf_get_tensor_offset(ctx_gguf.get(), gguf_find_tensor(ctx_gguf.get(), orig->name));
+ const size_t size = ggml_nbytes(orig);
+ if (offs + size < offs || offs + size > gguf_file.size()) {
+ throw std::runtime_error(format("LoRA tensor '%s' data is not within the file bounds, file is corrupted or incomplete", orig->name));
+ }
read_buf.resize(size);
gguf_file.seek(offs, SEEK_SET);
gguf_file.read_raw(read_buf.data(), size);