Skip to content

Commit

Permalink
Prevent access after free of trace messages
Browse files Browse the repository at this point in the history
Add a GetNextSeqNum() function and call it to set the sequence
number in the trace message before calling TraceWrite().

Change-Id: I2d71e63421f5732648d40da69f1b5e65e520950d
Partial-Bug: #1602899
  • Loading branch information
Megh Bhatt committed Jul 28, 2016
1 parent 4a6fe9b commit d178741
Showing 1 changed file with 9 additions and 7 deletions.
16 changes: 9 additions & 7 deletions src/base/trace.h
Expand Up @@ -24,8 +24,8 @@ class TraceBuffer {
trace_buf_(trace_buf_size_),
write_index_(0),
read_index_(0),
wrap_(false),
seqno_(0) {
wrap_(false) {
seqno_ = 0;
trace_enable_ = trace_enable;
}

Expand Down Expand Up @@ -54,7 +54,7 @@ class TraceBuffer {
return trace_buf_size_;
}

uint32_t TraceWrite(TraceEntryT *trace_entry) {
void TraceWrite(TraceEntryT *trace_entry) {
tbb::mutex::scoped_lock lock(mutex_);

// Add the trace
Expand Down Expand Up @@ -90,13 +90,15 @@ class TraceBuffer {
read_context_map_.erase(it);
}
}
}

uint32_t GetNextSeqNum() {
uint32_t nseqno(seqno_.fetch_and_increment());
// Reset seqno_ if it reaches max value
if (++seqno_ > kMaxSeqno) {
if (nseqno > kMaxSeqno) {
seqno_ = kMinSeqno;
}

return seqno_;
return nseqno;
}

void TraceRead(const std::string& context, const int count,
Expand Down Expand Up @@ -166,7 +168,7 @@ class TraceBuffer {
// trace message in the trace buffer
bool wrap_; // indicates if the trace buffer is wrapped
ReadContextMap read_context_map_; // stores the read context
uint32_t seqno_;
tbb::atomic<uint32_t> seqno_;
tbb::mutex mutex_;

// Reserve 0 and max(uint32_t)
Expand Down

0 comments on commit d178741

Please sign in to comment.