Skip to content

Commit d5f88cb

Browse files
author
chenhuanguang
committed
fix(bridge): fix buffer overflow bug
Change-Id: I048600b66afcdd90237bd7f7ba4cf0b05f8f53e3
1 parent 446c9c6 commit d5f88cb

File tree

2 files changed

+27
-12
lines changed

2 files changed

+27
-12
lines changed

modules/bridge/common/bridge_header.cc

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,14 +53,19 @@ bool BridgeHeader::Diserialize(const char *buf, size_t buf_size) {
5353
i -= static_cast<int>(sizeof(HType) + sizeof(bsize) + size + 3);
5454
continue;
5555
}
56-
size_t value_size = 0;
56+
5757
for (int j = 0; j < Header_Tail; j++) {
5858
if (type == header_item[j]->GetType()) {
59-
cursor = header_item[j]->DiserializeItem(cursor, &value_size);
59+
size_t value_size = 0;
60+
cursor = header_item[j]->DiserializeItem(cursor, static_cast<size_t>(i),
61+
&value_size);
62+
i -= static_cast<int>(value_size);
63+
if (cursor == nullptr) {
64+
return false;
65+
}
6066
break;
6167
}
6268
}
63-
i -= static_cast<int>(value_size);
6469
}
6570
return true;
6671
}

modules/bridge/common/bridge_header_item.h

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ class HeaderItemBase {
4444

4545
public:
4646
virtual char *SerializeItem(char *buf, size_t buf_size) = 0;
47-
virtual const char *DiserializeItem(const char *buf,
47+
virtual const char *DiserializeItem(const char *buf, const size_t buf_size,
4848
size_t *diserialized_size) = 0;
4949
virtual HType GetType() const = 0;
5050
};
@@ -56,18 +56,23 @@ template <enum HType t, typename T>
5656
char *SerializeItemImp(const HeaderItem<t, T> &item, char *buf,
5757
size_t buf_size) {
5858
if (!buf || buf_size == 0 ||
59-
buf_size < size_t(sizeof(t) + item.ValueSize() + 3)) {
59+
buf_size < size_t(sizeof(t) + sizeof(bsize) + item.ValueSize() + 3)) {
6060
return nullptr;
6161
}
6262
char *res = buf;
63-
size_t item_size = item.ValueSize();
63+
64+
// item.ValueSize() get the size of T type data,
65+
// the maximum of which is proto_name
66+
// when transfer data, bsize can save sizeof(proto_name).
67+
// The type needs to be kept consistent during serialize and diserialize.
68+
bsize item_size = static_cast<bsize>(item.ValueSize());
6469

6570
HType type = t;
6671
memcpy(res, &type, sizeof(HType));
6772
res[sizeof(HType)] = ':';
6873
res = res + sizeof(HType) + 1;
6974

70-
memcpy(res, &item_size, sizeof(size_t));
75+
memcpy(res, &item_size, sizeof(bsize));
7176
res[sizeof(bsize)] = ':';
7277
res = res + sizeof(bsize) + 1;
7378

@@ -79,8 +84,10 @@ char *SerializeItemImp(const HeaderItem<t, T> &item, char *buf,
7984

8085
template <enum HType t, typename T>
8186
const char *DiserializeItemImp(HeaderItem<t, T> *item, const char *buf,
87+
const size_t buf_size,
8288
size_t *diserialized_size) {
83-
if (!buf || !diserialized_size) {
89+
if (!buf || !diserialized_size ||
90+
buf_size < size_t(sizeof(HType) + sizeof(bsize) + 2)) {
8491
return nullptr;
8592
}
8693
const char *res = buf;
@@ -100,6 +107,9 @@ const char *DiserializeItemImp(HeaderItem<t, T> *item, const char *buf,
100107
res += sizeof(bsize) + 1;
101108
*diserialized_size += sizeof(bsize) + 1;
102109

110+
if (buf_size < size_t(sizeof(HType) + sizeof(bsize) + size + 3)) {
111+
return nullptr;
112+
}
103113
item->SetValue(res);
104114
res += size + 1;
105115
*diserialized_size += size + 1;
@@ -129,9 +139,9 @@ struct HeaderItem : public HeaderItemBase {
129139
return SerializeItemImp(*this, buf, buf_size);
130140
}
131141

132-
const char *DiserializeItem(const char *buf,
142+
const char *DiserializeItem(const char *buf, size_t buf_size,
133143
size_t *diserialized_size) override {
134-
return DiserializeItemImp(this, buf, diserialized_size);
144+
return DiserializeItemImp(this, buf, buf_size, diserialized_size);
135145
}
136146
};
137147

@@ -157,9 +167,9 @@ struct HeaderItem<t, std::string> : public HeaderItemBase {
157167
return SerializeItemImp(*this, buf, buf_size);
158168
}
159169

160-
const char *DiserializeItem(const char *buf,
170+
const char *DiserializeItem(const char *buf, size_t buf_size,
161171
size_t *diserialized_size) override {
162-
return DiserializeItemImp(this, buf, diserialized_size);
172+
return DiserializeItemImp(this, buf, buf_size, diserialized_size);
163173
}
164174
};
165175

0 commit comments

Comments
 (0)