Skip to content

Commit 133a97f

Browse files
addaleaxjasnell
authored andcommittedJun 17, 2020
quic: always copy stateless reset token
Take ownership of the token value, since the memory for it is allocated anyway and the buffer size is just 16, i.e. copyable very cheaply. This makes valgrind stop complaining about a use-after-free error when running `sequential/test-quic-preferred-address-ipv6`. PR-URL: #33917 Reviewed-By: James M Snell <[email protected]> Reviewed-By: Denys Otrishko <[email protected]> Reviewed-By: Matteo Collina <[email protected]>
1 parent e9145db commit 133a97f

File tree

2 files changed

+13
-10
lines changed

2 files changed

+13
-10
lines changed
 

‎src/quic/node_quic_util-inl.h

+10-5
Original file line numberDiff line numberDiff line change
@@ -287,22 +287,27 @@ bool PreferredAddress::ResolvePreferredAddress(
287287
StatelessResetToken::StatelessResetToken(
288288
uint8_t* token,
289289
const uint8_t* secret,
290-
const QuicCID& cid) : token_(token) {
290+
const QuicCID& cid) {
291291
GenerateResetToken(token, secret, cid);
292+
memcpy(buf_, token, sizeof(buf_));
292293
}
293294

294295
StatelessResetToken::StatelessResetToken(
295296
const uint8_t* secret,
296-
const QuicCID& cid)
297-
: token_(buf_) {
297+
const QuicCID& cid) {
298298
GenerateResetToken(buf_, secret, cid);
299299
}
300300

301+
StatelessResetToken::StatelessResetToken(
302+
const uint8_t* token) {
303+
memcpy(buf_, token, sizeof(buf_));
304+
}
305+
301306
std::string StatelessResetToken::ToString() const {
302307
std::vector<char> dest(NGTCP2_STATELESS_RESET_TOKENLEN * 2 + 1);
303308
dest[dest.size() - 1] = '\0';
304309
size_t written = StringBytes::hex_encode(
305-
reinterpret_cast<const char*>(token_),
310+
reinterpret_cast<const char*>(buf_),
306311
NGTCP2_STATELESS_RESET_TOKENLEN,
307312
dest.data(),
308313
dest.size());
@@ -313,7 +318,7 @@ size_t StatelessResetToken::Hash::operator()(
313318
const StatelessResetToken& token) const {
314319
size_t hash = 0;
315320
for (size_t n = 0; n < NGTCP2_STATELESS_RESET_TOKENLEN; n++)
316-
hash ^= std::hash<uint8_t>{}(token.token_[n]) + 0x9e3779b9 +
321+
hash ^= std::hash<uint8_t>{}(token.buf_[n]) + 0x9e3779b9 +
317322
(hash << 6) + (hash >> 2);
318323
return hash;
319324
}

‎src/quic/node_quic_util.h

+3-5
Original file line numberDiff line numberDiff line change
@@ -386,13 +386,12 @@ class StatelessResetToken : public MemoryRetainer {
386386
const uint8_t* secret,
387387
const QuicCID& cid);
388388

389-
explicit StatelessResetToken(
390-
const uint8_t* token)
391-
: token_(token) {}
389+
explicit inline StatelessResetToken(
390+
const uint8_t* token);
392391

393392
inline std::string ToString() const;
394393

395-
const uint8_t* data() const { return token_; }
394+
const uint8_t* data() const { return buf_; }
396395

397396
struct Hash {
398397
inline size_t operator()(const StatelessResetToken& token) const;
@@ -414,7 +413,6 @@ class StatelessResetToken : public MemoryRetainer {
414413

415414
private:
416415
uint8_t buf_[NGTCP2_STATELESS_RESET_TOKENLEN]{};
417-
const uint8_t* token_;
418416
};
419417

420418
template <typename T>

0 commit comments

Comments
 (0)