Skip to content

Commit 26da017

Browse files
authored
TLS Transfer Serialization Improvements (#2616)
### Issues: Addresses CryptoAlg-3221, CryptoAlg-3220, CryptoAlg-3219, CryptoAlg-3218, CryptoAlg-3217, CryptoAlg-3216, CryptoAlg-3215, CryptoAlg-3214, CryptoAlg-3212, CryptoAlg-3211 ### Description of changes: This pull request addresses and improves the experimental TLS transfer serialization feature. Primarily focusing on performance of restored connections for the SSLBuffer by improving the serialization format, and hardens the checking of the data structures on deserialization. By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license and the ISC license.
1 parent ff7a411 commit 26da017

File tree

128 files changed

+1233
-187
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

128 files changed

+1233
-187
lines changed

fuzz/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,5 +38,6 @@ fuzzer(read_pem)
3838
fuzzer(server ssl)
3939
fuzzer(session ssl)
4040
fuzzer(spki)
41+
fuzzer(ssl_buffer ssl)
4142
fuzzer(ssl_ctx_api ssl)
4243
fuzzer(ssl_serialization ssl)

fuzz/ssl_buffer.cc

Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
1+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
// SPDX-License-Identifier: Apache-2.0 OR ISC
3+
4+
#include <openssl/bytestring.h>
5+
#include <openssl/err.h>
6+
#include <openssl/mem.h>
7+
#include <openssl/span.h>
8+
#include "../ssl/internal.h"
9+
10+
extern "C" int LLVMFuzzerTestOneInput(const uint8_t *buf, size_t len) {
11+
CBS cbs;
12+
CBS_init(&cbs, buf, len);
13+
14+
bssl::SSLBuffer buffer;
15+
16+
if (!buffer.DoDeserialization(cbs)) {
17+
return 0;
18+
}
19+
20+
// See if we can serialize it back
21+
bssl::ScopedCBB cbb;
22+
CBB_init(cbb.get(), len);
23+
if (!buffer.DoSerialization(*cbb.get())) {
24+
return 1;
25+
}
26+
cbb.Reset();
27+
28+
// If the restore buffer is not empty lets use it
29+
if (!buffer.empty()) {
30+
// Get a view to the written but not discarded data and verify
31+
// we can safely read it all.
32+
auto span = buffer.span();
33+
{
34+
volatile const uint8_t *vp = span.data();
35+
for (size_t i = 0; i < span.size(); i++) {
36+
uint8_t v = vp[i];
37+
(void)v;
38+
}
39+
}
40+
// Now "consume" the content we read which moves offset_ forward and reduces size_ and cap_.
41+
buffer.Consume(span.size());
42+
43+
// Serialize the read span we were using. This is valid and allowed use case
44+
// as the data is still kept until we call discard.
45+
bssl::ScopedCBB spanCBB;
46+
CBB_init(spanCBB.get(), span.size());
47+
if (!buffer.SerializeBufferView(*spanCBB.get(), span)) {
48+
return 1;
49+
}
50+
51+
// Serialize the current buffer state, we should be able to restore it
52+
// and restore the span and use it safely.
53+
CBB_init(cbb.get(), len);
54+
if (!buffer.DoSerialization(*cbb.get())) {
55+
return 1;
56+
}
57+
58+
// Now let's try to fill the rest of the buffer's remaining space
59+
{
60+
// Fill the remaining capacity with 1's
61+
auto remaining = buffer.remaining();
62+
memset(remaining.data(), 1, remaining.size());
63+
buffer.DidWrite(remaining.size());
64+
65+
// Since we told the buffer we wrote remaining.size(), then the
66+
// buffer.span() should now point to that content.
67+
auto remSpan = buffer.span();
68+
if (remSpan.size() != remaining.size()) {
69+
return 1;
70+
}
71+
72+
// Validate we read all 1's
73+
for (size_t i = 0; i < remSpan.size(); i++) {
74+
uint8_t v = remSpan.data()[i];
75+
if (v != 1) {
76+
return 1;
77+
}
78+
}
79+
80+
// Inform that we have now consumed the data
81+
buffer.Consume(remSpan.size());
82+
remaining = buffer.remaining();
83+
84+
// There should be no space left...
85+
if (remaining.size() != 0) {
86+
return 1;
87+
}
88+
89+
// This should cause the buffer to be free'd
90+
buffer.DiscardConsumed();
91+
if (buffer.buf_ptr() != nullptr) {
92+
return 1;
93+
}
94+
}
95+
96+
// Reset to the serialized version before the above writes
97+
CBS_init(&cbs, CBB_data(cbb.get()), CBB_len(cbb.get()));
98+
if (!buffer.DoDeserialization(cbs)) {
99+
return 1;
100+
}
101+
102+
// Restore the span to the earlier data we consumed
103+
CBS_init(&cbs, CBB_data(spanCBB.get()), CBB_len(spanCBB.get()));
104+
if (!buffer.DeserializeBufferView(cbs, span)) {
105+
return 1;
106+
}
107+
108+
// We should still be able to safely read the data the span referred to.
109+
{
110+
volatile const uint8_t *vp = span.data();
111+
for (size_t i = 0; i < span.size(); i++) {
112+
uint8_t v = vp[i];
113+
(void)v;
114+
}
115+
}
116+
}
117+
118+
// let's try to fill the rest of the buffer's remaining space.
119+
// We did this earlier if the buffer was not empty as well.
120+
{
121+
// Fill the remaining capacity with 1's
122+
auto remaining = buffer.remaining();
123+
memset(remaining.data(), 1, remaining.size());
124+
buffer.DidWrite(remaining.size());
125+
126+
// Since we told the buffer we wrote remaining.size(), then the
127+
// buffer.span() should now point to that content.
128+
auto span = buffer.span();
129+
if (span.size() != remaining.size()) {
130+
return 1;
131+
}
132+
133+
// Validate we read all 1's
134+
for (size_t i = 0; i < span.size(); i++) {
135+
uint8_t v = span.data()[i];
136+
if (v != 1) {
137+
return 1;
138+
}
139+
}
140+
141+
// Inform that we have now consumed the data
142+
buffer.Consume(span.size());
143+
remaining = buffer.remaining();
144+
145+
// There should be no space left...
146+
if (remaining.size() != 0) {
147+
return 1;
148+
}
149+
150+
// This should cause the buffer to be free'd
151+
buffer.DiscardConsumed();
152+
if (buffer.buf_ptr() != nullptr) {
153+
return 1;
154+
}
155+
}
156+
157+
return 0;
158+
}
2 Bytes
Binary file not shown.
9 Bytes
Binary file not shown.
24 Bytes
Binary file not shown.
22 Bytes
Binary file not shown.
8 Bytes
Binary file not shown.
24 Bytes
Binary file not shown.
23 Bytes
Binary file not shown.
25 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)