Skip to content

Commit a65e735

Browse files
mc-nvyinggeh
andauthored
fix: Verify MemoryShm::byte_size within CPU shm boundary (#406) (#411)
Co-authored-by: Yingge He <157551214+yinggeh@users.noreply.github.com>
1 parent bb82100 commit a65e735

File tree

2 files changed

+33
-3
lines changed

2 files changed

+33
-3
lines changed

src/pb_memory.cc

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2022-2023, NVIDIA CORPORATION & AFFILIATES. All rights reserved.
1+
// Copyright 2022-2025, NVIDIA CORPORATION & AFFILIATES. All rights reserved.
22
//
33
// Redistribution and use in source and binary forms, with or without
44
// modification, are permitted provided that the following conditions
@@ -26,6 +26,8 @@
2626

2727
#include "pb_memory.h"
2828

29+
#include <sstream>
30+
2931
namespace triton { namespace backend { namespace python {
3032

3133
std::unique_ptr<PbMemory>
@@ -225,7 +227,6 @@ PbMemory::LoadFromSharedMemory(
225227
{
226228
MemoryShm* memory_shm_ptr = reinterpret_cast<MemoryShm*>(data_shm);
227229
char* memory_data_shm = data_shm + sizeof(MemoryShm);
228-
229230
char* data_ptr = nullptr;
230231
bool opened_cuda_ipc_handle = false;
231232
if (memory_shm_ptr->memory_type == TRITONSERVER_MEMORY_GPU &&
@@ -260,6 +261,19 @@ PbMemory::LoadFromSharedMemory(
260261
} else {
261262
data_ptr = memory_data_shm;
262263
}
264+
265+
// This check only validates CPU shared memory access.
266+
if (memory_shm_ptr->memory_type != TRITONSERVER_MEMORY_GPU &&
267+
(data_ptr + memory_shm_ptr->byte_size >
268+
(char*)shm_pool->GetBaseAddress() + shm_pool->GetCurrentCapacity())) {
269+
std::ostringstream oss;
270+
oss << "0x" << std::hex
271+
<< (reinterpret_cast<uintptr_t>(data_ptr) + memory_shm_ptr->byte_size);
272+
throw PythonBackendException(
273+
std::string("Attempted to access out of bounds memory address ") +
274+
oss.str());
275+
}
276+
263277
return std::unique_ptr<PbMemory>(new PbMemory(
264278
data_shm, data_ptr, handle,
265279
opened_cuda_ipc_handle /* opened_cuda_ipc_handle */));
@@ -309,6 +323,19 @@ PbMemory::LoadFromSharedMemory(
309323
} else {
310324
data_ptr = memory_data_shm;
311325
}
326+
327+
// This check only validates CPU shared memory access.
328+
if (memory_shm_ptr->memory_type != TRITONSERVER_MEMORY_GPU &&
329+
(data_ptr + memory_shm_ptr->byte_size >
330+
(char*)shm_pool->GetBaseAddress() + shm_pool->GetCurrentCapacity())) {
331+
std::ostringstream oss;
332+
oss << "0x" << std::hex
333+
<< (reinterpret_cast<uintptr_t>(data_ptr) + memory_shm_ptr->byte_size);
334+
throw PythonBackendException(
335+
std::string("Attempted to access out of bounds memory address ") +
336+
oss.str());
337+
}
338+
312339
return std::unique_ptr<PbMemory>(new PbMemory(
313340
memory_shm, data_ptr,
314341
opened_cuda_ipc_handle /* opened_cuda_ipc_handle */));

src/shm_manager.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2021-2023, NVIDIA CORPORATION & AFFILIATES. All rights reserved.
1+
// Copyright 2021-2025, NVIDIA CORPORATION & AFFILIATES. All rights reserved.
22
//
33
// Redistribution and use in source and binary forms, with or without
44
// modification, are permitted provided that the following conditions
@@ -188,6 +188,9 @@ class SharedMemoryManager {
188188
return cuda_memory_pool_manager_;
189189
}
190190

191+
uint64_t GetCurrentCapacity() { return current_capacity_; }
192+
void* GetBaseAddress() { return managed_buffer_->get_address(); }
193+
191194
~SharedMemoryManager() noexcept(false);
192195

193196
private:

0 commit comments

Comments
 (0)