bneradt commented on code in PR #14147:
URL: https://github.com/apache/trafficserver/pull/14147#discussion_r4683167509
##########
src/records/RecMessage.cc:
##########
@@ -170,50 +170,109 @@ RecMessageUnmarshalFirst(RecMessage *msg, RecMessageItr
*itr, RecRecord **record
// RecMessageUnmarshalNext
//-------------------------------------------------------------------------
+namespace
+{
+// A message read back from records.snap is only as sound as the file, so
every offset in it must land inside
+// the buffer, which is o_end bytes long.
+bool
+fits_in_message(const RecMessage *msg, intptr_t offset, intptr_t len)
+{
+ return offset >= msg->o_start && offset <= msg->o_end && len <= msg->o_end -
offset;
+}
+
+// The string offsets are relative to the record and become pointers only once
the string is known to end
+// inside the message.
+template <typename Char>
+bool
+resolve_string(RecMessage *msg, const RecRecord *r, Char *&str)
+{
+ if (str == nullptr) {
+ return true;
+ }
+
+ // Bound rel before adding it, so a wild offset from the file cannot
overflow the sum.
+ intptr_t const rel = reinterpret_cast<intptr_t>(str);
+ if (rel < 0 || rel > msg->o_end) {
+ return false;
+ }
+ intptr_t const offset = (reinterpret_cast<const char *>(r) -
reinterpret_cast<const char *>(msg)) + rel;
+ if (!fits_in_message(msg, offset, 1)) {
+ return false;
+ }
+
+ char *p = reinterpret_cast<char *>(msg) + offset;
+ if (memchr(p, '\0', msg->o_end - offset) == nullptr) {
+ return false;
+ }
+ str = p;
+ return true;
+}
+} // namespace
+
int
RecMessageUnmarshalNext(RecMessage *msg, RecMessageItr *itr, RecRecord
**record)
{
RecMessageEleHdr *eh;
RecRecord *r;
+ intptr_t offset;
+ intptr_t const ele_size = sizeof(RecMessageEleHdr) + sizeof(RecRecord);
+
+ auto corrupt = [](const char *why) {
+ Warning("Persistent statistics file records.snap is corrupted (%s).
Ignoring the rest of the file\n", why);
+ return REC_ERR_FAIL;
+ };
if (itr == nullptr) {
if (msg->entries == 0) {
return REC_ERR_FAIL;
- } else {
- eh = reinterpret_cast<RecMessageEleHdr *>(reinterpret_cast<char *>(msg)
+ msg->o_start);
}
+ offset = msg->o_start;
} else {
if (itr->next >= msg->entries) {
return REC_ERR_FAIL;
}
- itr->ele_hdr = reinterpret_cast<RecMessageEleHdr *>(reinterpret_cast<char
*>(msg) + itr->ele_hdr->o_next);
+ // The next element must start after this element's header and record, or
it would overlap a record whose
+ // string offsets have already been turned into pointers.
+ intptr_t const current = reinterpret_cast<char *>(itr->ele_hdr) -
reinterpret_cast<char *>(msg);
+ offset = itr->ele_hdr->o_next;
+ if (offset < current + ele_size) {
+ return corrupt("element overlaps the previous one");
+ }
+ }
+
+ if (!fits_in_message(msg, offset, ele_size)) {
+ return corrupt("element outside the snapshot");
+ }
+ eh = reinterpret_cast<RecMessageEleHdr *>(reinterpret_cast<char *>(msg) +
offset);
Review Comment:
**[P2] Check element alignment before the typed reads.** A next-element
offset can pass the size and overlap checks while being misaligned for
`RecMessageEleHdr` or `RecRecord`. The casts are then followed by misaligned
member access. Validate the writer's element alignment before reading either
structure.
##########
src/records/RecMessage.cc:
##########
@@ -170,50 +170,109 @@ RecMessageUnmarshalFirst(RecMessage *msg, RecMessageItr
*itr, RecRecord **record
// RecMessageUnmarshalNext
//-------------------------------------------------------------------------
+namespace
+{
+// A message read back from records.snap is only as sound as the file, so
every offset in it must land inside
+// the buffer, which is o_end bytes long.
+bool
+fits_in_message(const RecMessage *msg, intptr_t offset, intptr_t len)
+{
+ return offset >= msg->o_start && offset <= msg->o_end && len <= msg->o_end -
offset;
+}
+
+// The string offsets are relative to the record and become pointers only once
the string is known to end
+// inside the message.
+template <typename Char>
+bool
+resolve_string(RecMessage *msg, const RecRecord *r, Char *&str)
+{
+ if (str == nullptr) {
+ return true;
+ }
+
+ // Bound rel before adding it, so a wild offset from the file cannot
overflow the sum.
+ intptr_t const rel = reinterpret_cast<intptr_t>(str);
+ if (rel < 0 || rel > msg->o_end) {
+ return false;
+ }
+ intptr_t const offset = (reinterpret_cast<const char *>(r) -
reinterpret_cast<const char *>(msg)) + rel;
+ if (!fits_in_message(msg, offset, 1)) {
+ return false;
+ }
+
+ char *p = reinterpret_cast<char *>(msg) + offset;
+ if (memchr(p, '\0', msg->o_end - offset) == nullptr) {
+ return false;
+ }
+ str = p;
+ return true;
+}
+} // namespace
+
int
RecMessageUnmarshalNext(RecMessage *msg, RecMessageItr *itr, RecRecord
**record)
{
RecMessageEleHdr *eh;
RecRecord *r;
+ intptr_t offset;
+ intptr_t const ele_size = sizeof(RecMessageEleHdr) + sizeof(RecRecord);
+
+ auto corrupt = [](const char *why) {
+ Warning("Persistent statistics file records.snap is corrupted (%s).
Ignoring the rest of the file\n", why);
+ return REC_ERR_FAIL;
+ };
if (itr == nullptr) {
if (msg->entries == 0) {
return REC_ERR_FAIL;
- } else {
- eh = reinterpret_cast<RecMessageEleHdr *>(reinterpret_cast<char *>(msg)
+ msg->o_start);
}
+ offset = msg->o_start;
} else {
if (itr->next >= msg->entries) {
return REC_ERR_FAIL;
}
- itr->ele_hdr = reinterpret_cast<RecMessageEleHdr *>(reinterpret_cast<char
*>(msg) + itr->ele_hdr->o_next);
+ // The next element must start after this element's header and record, or
it would overlap a record whose
+ // string offsets have already been turned into pointers.
+ intptr_t const current = reinterpret_cast<char *>(itr->ele_hdr) -
reinterpret_cast<char *>(msg);
+ offset = itr->ele_hdr->o_next;
+ if (offset < current + ele_size) {
+ return corrupt("element overlaps the previous one");
+ }
+ }
+
+ if (!fits_in_message(msg, offset, ele_size)) {
+ return corrupt("element outside the snapshot");
+ }
+ eh = reinterpret_cast<RecMessageEleHdr *>(reinterpret_cast<char *>(msg) +
offset);
+ if (itr != nullptr) {
+ itr->ele_hdr = eh;
itr->next += 1;
- eh = itr->ele_hdr;
}
ink_assert(eh->magic == REC_MESSAGE_ELE_MAGIC);
// If the file is corrupt, ignore the rest of the file.
if (eh->magic != REC_MESSAGE_ELE_MAGIC) {
- Warning("Persistent statistics file records.stat is corrupted. Ignoring
the rest of the file\n");
- return REC_ERR_FAIL;
+ return corrupt("bad element magic");
Review Comment:
**[P2] Let the corruption handler see bad element magic.** The `ink_assert`
immediately above this branch aborts an assertion-enabled build before the
warning and `REC_ERR_FAIL` can run. The magic comes from disk, so remove the
assertion and use the checked failure path in both build types.
##########
src/records/RecMessage.cc:
##########
@@ -170,50 +170,109 @@ RecMessageUnmarshalFirst(RecMessage *msg, RecMessageItr
*itr, RecRecord **record
// RecMessageUnmarshalNext
//-------------------------------------------------------------------------
+namespace
+{
+// A message read back from records.snap is only as sound as the file, so
every offset in it must land inside
+// the buffer, which is o_end bytes long.
+bool
+fits_in_message(const RecMessage *msg, intptr_t offset, intptr_t len)
+{
+ return offset >= msg->o_start && offset <= msg->o_end && len <= msg->o_end -
offset;
+}
+
+// The string offsets are relative to the record and become pointers only once
the string is known to end
+// inside the message.
+template <typename Char>
+bool
+resolve_string(RecMessage *msg, const RecRecord *r, Char *&str)
+{
+ if (str == nullptr) {
+ return true;
+ }
+
+ // Bound rel before adding it, so a wild offset from the file cannot
overflow the sum.
+ intptr_t const rel = reinterpret_cast<intptr_t>(str);
+ if (rel < 0 || rel > msg->o_end) {
+ return false;
+ }
+ intptr_t const offset = (reinterpret_cast<const char *>(r) -
reinterpret_cast<const char *>(msg)) + rel;
+ if (!fits_in_message(msg, offset, 1)) {
+ return false;
+ }
+
+ char *p = reinterpret_cast<char *>(msg) + offset;
+ if (memchr(p, '\0', msg->o_end - offset) == nullptr) {
+ return false;
+ }
+ str = p;
+ return true;
+}
+} // namespace
+
int
RecMessageUnmarshalNext(RecMessage *msg, RecMessageItr *itr, RecRecord
**record)
{
RecMessageEleHdr *eh;
RecRecord *r;
+ intptr_t offset;
+ intptr_t const ele_size = sizeof(RecMessageEleHdr) + sizeof(RecRecord);
+
+ auto corrupt = [](const char *why) {
+ Warning("Persistent statistics file records.snap is corrupted (%s).
Ignoring the rest of the file\n", why);
+ return REC_ERR_FAIL;
+ };
if (itr == nullptr) {
if (msg->entries == 0) {
return REC_ERR_FAIL;
- } else {
- eh = reinterpret_cast<RecMessageEleHdr *>(reinterpret_cast<char *>(msg)
+ msg->o_start);
}
+ offset = msg->o_start;
} else {
if (itr->next >= msg->entries) {
return REC_ERR_FAIL;
}
- itr->ele_hdr = reinterpret_cast<RecMessageEleHdr *>(reinterpret_cast<char
*>(msg) + itr->ele_hdr->o_next);
+ // The next element must start after this element's header and record, or
it would overlap a record whose
+ // string offsets have already been turned into pointers.
+ intptr_t const current = reinterpret_cast<char *>(itr->ele_hdr) -
reinterpret_cast<char *>(msg);
+ offset = itr->ele_hdr->o_next;
+ if (offset < current + ele_size) {
+ return corrupt("element overlaps the previous one");
+ }
+ }
+
+ if (!fits_in_message(msg, offset, ele_size)) {
+ return corrupt("element outside the snapshot");
+ }
+ eh = reinterpret_cast<RecMessageEleHdr *>(reinterpret_cast<char *>(msg) +
offset);
+ if (itr != nullptr) {
+ itr->ele_hdr = eh;
itr->next += 1;
- eh = itr->ele_hdr;
}
ink_assert(eh->magic == REC_MESSAGE_ELE_MAGIC);
// If the file is corrupt, ignore the rest of the file.
if (eh->magic != REC_MESSAGE_ELE_MAGIC) {
- Warning("Persistent statistics file records.stat is corrupted. Ignoring
the rest of the file\n");
- return REC_ERR_FAIL;
+ return corrupt("bad element magic");
}
r = reinterpret_cast<RecRecord *>(reinterpret_cast<char *>(eh) +
sizeof(RecMessageEleHdr));
- if (r->name) {
- r->name = reinterpret_cast<char *>(r) + (intptr_t)(r->name);
+ if (!resolve_string(msg, r, r->name)) {
+ return corrupt("bad record name");
+ }
+ // RecSetRecord() reads RECD_NULL data as a string pointer, which the bytes
from the file are not, and RecDataSet()
+ // asserts on any type past RECD_COUNTER.
+ if (r->data_type <= RECD_NULL || r->data_type >= RECD_MAX) {
+ return corrupt("bad data type");
}
if (r->data_type == RECD_STRING) {
- if (r->data.rec_string) {
- r->data.rec_string = reinterpret_cast<char *>(r) +
(intptr_t)(r->data.rec_string);
- }
- if (r->data_default.rec_string) {
- r->data_default.rec_string = reinterpret_cast<char *>(r) +
(intptr_t)(r->data_default.rec_string);
+ if (!resolve_string(msg, r, r->data.rec_string) || !resolve_string(msg, r,
r->data_default.rec_string)) {
+ return corrupt("bad string value");
}
}
- if (REC_TYPE_IS_CONFIG(r->rec_type) && (r->config_meta.check_expr)) {
- r->config_meta.check_expr = reinterpret_cast<char *>(r) +
(intptr_t)(r->config_meta.check_expr);
+ if (REC_TYPE_IS_CONFIG(r->rec_type) && !resolve_string(msg, r,
r->config_meta.check_expr)) {
Review Comment:
**[P2] Restrict snapshot records to persisted stat types.**
`RecSyncStatsFile()` writes only stat records. The reader still accepts config
and unknown record types, which can reach `RecSetRecord()` through the
unregistered-record path. Invalid types can assert there, and a restored
config-shaped record can make later metric registration hit its type assertion.
Validate the record kind against the types the snapshot writer emits before
returning the record.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]