Fix out-of-bounds access and key handling in Caffe/Caffe2 reader#6211
Conversation
- Uses sized std::string construction for LMDB keys to avoid reading past non-null-terminated key data - Adds bounds checks for label indices in Caffe2 multi-label sparse and weighted-sparse parsing paths - Validate image byte_data size against declared dimensions in Caffe2Parser - Add negative Python tests exercising the new error paths Signed-off-by: Janusz Lisiecki <[email protected]>
Greptile SummaryThis PR fixes critical security vulnerabilities in the Caffe/Caffe2 reader by preventing out-of-bounds memory accesses in three distinct code paths. Key Changes:
Testing: Confidence Score: 5/5
Important Files Changed
Flowchart%%{init: {'theme': 'neutral'}}%%
flowchart TD
A[LMDB Key Read] --> B{Key Data Valid?}
B -->|Before Fix| C[to_string with reinterpret_cast]
B -->|After Fix| D[Sized string constructor]
C --> E[Potential OOB Read]
D --> F[Safe Key String]
G[Caffe2 Label Parse] --> H{Label Type?}
H -->|MULTI_LABEL_SPARSE| I[Extract Label Index]
H -->|MULTI_LABEL_WEIGHTED_SPARSE| J[Extract Index & Weight]
I --> K{idx >= 0 && idx < num_labels?}
J --> K
K -->|No| L[DALI_ENFORCE Error]
K -->|Yes| M[Store Label Value]
N[Caffe2 Image Parse] --> O[Read Dims: H, W, C]
O --> P{byte_data.size == H*W*C?}
P -->|No| Q[DALI_ENFORCE Error]
P -->|Yes| R[memcpy Image Data]
style E fill:#ff6b6b
style L fill:#ff6b6b
style Q fill:#ff6b6b
style F fill:#51cf66
style M fill:#51cf66
style R fill:#51cf66
Last reviewed commit: 9a2fa78 |
|
!build |
|
CI MESSAGE: [44018446]: BUILD STARTED |
|
CI MESSAGE: [44018446]: BUILD PASSED |
|
!build |
|
CI MESSAGE: [44137002]: BUILD STARTED |
Signed-off-by: Janusz Lisiecki <[email protected]>
5405a37 to
4cbfa68
Compare
|
!build |
|
CI MESSAGE: [44137308]: BUILD STARTED |
|
CI MESSAGE: [44137308]: BUILD PASSED |
| for (int i = 0; i < label_data_size; ++i) { | ||
| label_tensor_data[static_cast<int>(proto_get_data<T>(label_indices, i))] | ||
| = static_cast<T>(1); | ||
| auto idx = static_cast<int>(proto_get_data<T>(label_indices, i)); |
There was a problem hiding this comment.
| auto idx = static_cast<int>(proto_get_data<T>(label_indices, i)); | |
| int64_t idx = proto_get_data<T>(label_indices, i); |
Much more readable - and also supports larger range.
| const int W = image_proto.dims(1); | ||
|
|
||
| image.Resize({H, W, C}, DALI_UINT8); | ||
| DALI_ENFORCE(image_proto.byte_data().size() == static_cast<size_t>(H * W * C), |
There was a problem hiding this comment.
| DALI_ENFORCE(image_proto.byte_data().size() == static_cast<size_t>(H * W * C), | |
| DALI_ENFORCE(image_proto.byte_data().size() == static_cast<size_t>(H) * W * C, |
Signed-off-by: Janusz Lisiecki <[email protected]>
|
!build |
|
CI MESSAGE: [44382257]: BUILD STARTED |
|
CI MESSAGE: [44382257]: BUILD FAILED |
|
CI MESSAGE: [44382257]: BUILD PASSED |
past non-null-terminated key data
and weighted-sparse parsing paths
Caffe2Parser
Category:
Bug fix (non-breaking change which fixes an issue)
Description:
past non-null-terminated key data
and weighted-sparse parsing paths
Caffe2Parser
Additional information:
Affected modules and functionalities:
Key points relevant for the review:
Tests:
Checklist
Documentation
DALI team only
Requirements
REQ IDs: N/A
JIRA TASK: N/A