Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhance bound check for shm offset #6914

Merged
merged 7 commits into from
Mar 8, 2024
Merged

Enhance bound check for shm offset #6914

merged 7 commits into from
Mar 8, 2024

Conversation

kthui
Copy link
Contributor

@kthui kthui commented Feb 23, 2024

Enhance shared memory bound checking during inference to make sure the offset supplied by the user cannot go beyond the shared memory region when the region was registered.

@kthui kthui marked this pull request as ready for review February 26, 2024 22:54
@kthui kthui requested a review from rmccorm4 March 5, 2024 20:26
Copy link
Contributor

@GuanLuo GuanLuo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't you also check the byte size of the read / write as well? Couldn't the same issue happen if one specifies offset = (max_offset - 1) and instructs Triton to write > 1 bytes?

@kthui
Copy link
Contributor Author

kthui commented Mar 6, 2024

Shouldn't you also check the byte size of the read / write as well? Couldn't the same issue happen if one specifies offset = (max_offset - 1) and instructs Triton to write > 1 bytes?

This scope is exactly the next ticket after this one. We can address it in a different PR, unless you think both fixes must be committed at the same time.

@nv-kmcgill53
Copy link
Contributor

nv-kmcgill53 commented Mar 6, 2024

This is increasing the scope by a little bit but could you provide an improvement to the shared memory manager where we check that the offset provided by the user is a multiple of the host's page size? You could probably test for this if mmap returns an error.

const size_t page_size = sysconf(_SC_PAGE_SIZE);
if ( (offset % page_size ) != 0) {
      return TRITONSERVER_ErrorNew(
        TRITONSERVER_ERROR_INTERNAL, std::string(
                                         "Bad argument: offset, " + std::to_string(offest) + 
                                         ", is not a multiple of the host's page size (" + 
                                         std::to_string(page_size) + "): " +
                                         std::string(std::strerror(errno)))
                                         .c_str());
}

@kthui
Copy link
Contributor Author

kthui commented Mar 7, 2024

@nv-kmcgill53 if I understand correctly, you are talking about an improved bound check when registering a shm region, not when inferencing with a shm region? The next next ticket after this one is about improving checks when registering a shm region. Maybe we can add this check on a separate PR, unless you think the shm registration bound check improvement and this one must be committed together?

@@ -346,12 +346,26 @@ SharedMemoryManager::GetMemoryInfo(
std::string("Unable to find shared memory region: '" + name + "'")
.c_str());
}

// populate 'shm_mapped_addr'
size_t max_offset = 0;
Copy link
Contributor

@nnshah1 nnshah1 Mar 7, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Small suggestion - what if we refactored to set the offsets, check them, and then only set the shm variable? I think one thing was a little odd was seeing the address set and then the offsets checked.

By setting the variables you can also keep a single place to set the address - just a thought.

size_t max_offset = 0;
 
if (it->second->byte_size_ > 0) {
    max_offset = it->second->byte_size_ - 1;
  }
  if (it->second->kind_ == TRITONSERVER_MEMORY_CPU) {
     offset += it->second->offset;
     max_offset += it -> second-> offset;
  } 
 
  if (offset > max_offset) {
    *shm_mapped_addr = nullptr;
    return TRITONSERVER_ErrorNew(
        TRITONSERVER_ERROR_INVALID_ARG,
        std::string("Invalid offset for shared memory region: '" + name + "'")
            .c_str());
  }
   *shm_mapped_addr = (void*)((uint8_t*)it->second->mapped_addr_ + offset);

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@nv-kmcgill53
Copy link
Contributor

@nv-kmcgill53 if I understand correctly, you are talking about an improved bound check when registering a shm region, not when inferencing with a shm region? The next next ticket after this one is about improving checks when registering a shm region. Maybe we can add this check on a separate PR, unless you think the shm registration bound check improvement and this one must be committed together?

Yes, if there is other work slated for improvement on the registering of shared memory then, yes, that change should go in there.

@kthui
Copy link
Contributor Author

kthui commented Mar 7, 2024

@nv-kmcgill53 if I understand correctly, you are talking about an improved bound check when registering a shm region, not when inferencing with a shm region? The next next ticket after this one is about improving checks when registering a shm region. Maybe we can add this check on a separate PR, unless you think the shm registration bound check improvement and this one must be committed together?

Yes, if there is other work slated for improvement on the registering of shared memory then, yes, that change should go in there.

Sure. Linked this conversation in that ticket.

@kthui kthui requested review from nnshah1, rmccorm4 and GuanLuo March 7, 2024 18:03
Copy link
Contributor

@rmccorm4 rmccorm4 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM assuming the byte size check will be in a follow up

@kthui kthui merged commit 60071e1 into main Mar 8, 2024
3 checks passed
@kthui kthui deleted the jacky-shm-offset branch March 8, 2024 17:46
jbkyang-nvi added a commit that referenced this pull request Apr 12, 2024
…nds (#7083)

Validate that memory offset and byte size requested is not out of bounds 
of registered memory. 
Previously in #6914 we checked out of bounds offset for shared memory
requests. This PR also adds more testing to verify the block of memory 
is in fact in bounds.
Client change: triton-inference-server/client#565
jbkyang-nvi added a commit that referenced this pull request Apr 12, 2024
…nds (#7083)

Validate that memory offset and byte size requested is not out of bounds 
of registered memory. 
Previously in #6914 we checked out of bounds offset for shared memory
requests. This PR also adds more testing to verify the block of memory 
is in fact in bounds.
Client change: triton-inference-server/client#565
mc-nv pushed a commit that referenced this pull request Apr 12, 2024
…nds (#7083) (#7111)

Validate that memory offset and byte size requested is not out of bounds 
of registered memory. 
Previously in #6914 we checked out of bounds offset for shared memory
requests. This PR also adds more testing to verify the block of memory 
is in fact in bounds.
Client change: triton-inference-server/client#565
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

5 participants