Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(18)

Side by Side Diff: testing/libfuzzer/fuzzers/libsrtp_fuzzer.cc

Issue 2123553002: Add a fuzzer for srtp_unprotect in libsrtp. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Use a global message and ASAN poison it. Created 4 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « testing/libfuzzer/fuzzers/BUILD.gn ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright 2016 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include <assert.h>
6 #include <stddef.h>
7 #include <stdint.h>
8
9 #include <algorithm>
10 #include <vector>
11
12 #include "third_party/libsrtp/srtp/include/rtp.h"
13 #include "third_party/libsrtp/srtp/include/rtp_priv.h"
14 #include "third_party/libsrtp/srtp/include/srtp.h"
15
16 #ifdef ADDRESS_SANITIZER
17 #include <sanitizer/asan_interface.h>
18 #endif
19
20 // TODO(katrielc) Also test the authenticated path, which is what
21 // WebRTC uses. This is nontrivial because you need to bypass the MAC
22 // check. Two options: add a UNSAFE_FUZZER_MODE flag to libsrtp (or
23 // the chromium fork of it), or compute the HMAC of whatever gibberish
24 // the fuzzer produces and write it into the packet manually.
25
26 namespace LibSrtpFuzzer {
27 enum CryptoPolicy {
28 NONE,
29 LIKE_WEBRTC,
30 LIKE_WEBRTC_WITHOUT_AUTH,
31 AES_GCM,
32 NUMBER_OF_POLICIES,
33 };
34 }
35
36 void poison(void const volatile* addr, size_t size) {
37 #ifdef ADDRESS_SANITIZER
38 ASAN_POISON_MEMORY_REGION(addr, size);
39 #endif
40 };
41
42 void unpoison(void const volatile* addr, size_t size) {
43 #ifdef ADDRESS_SANITIZER
44 ASAN_UNPOISON_MEMORY_REGION(addr, size);
45 #endif
46 };
47
48 void crypto_policy_set_null_cipher_null_auth(crypto_policy_t* p) {
49 p->cipher_type = NULL_CIPHER;
50 p->cipher_key_len = 0;
51 p->auth_type = NULL_AUTH;
52 p->auth_key_len = 0;
53 p->auth_tag_len = 0;
54 p->sec_serv = sec_serv_none;
55 };
56
57 // The environment holds some structs and buffers so that we don't
58 // need to reallocate them on every fuzzer iteration.
59 struct Environment {
60 srtp_t session;
61 srtp_policy_t policy;
62 unsigned char key[SRTP_MASTER_KEY_LEN] = {0};
63 rtp_msg_t message = {};
64
65 srtp_policy_t GetCryptoPolicy(LibSrtpFuzzer::CryptoPolicy crypto_policy,
66 const unsigned char* replacement_key) {
67 // Update some values in the global srtp_policy_t and return it.
68 switch (crypto_policy) {
69 case LibSrtpFuzzer::NUMBER_OF_POLICIES:
70 case LibSrtpFuzzer::NONE:
71 crypto_policy_set_null_cipher_null_auth(&policy.rtp);
72 crypto_policy_set_null_cipher_null_auth(&policy.rtcp);
73 break;
74 case LibSrtpFuzzer::LIKE_WEBRTC:
75 crypto_policy_set_aes_cm_128_hmac_sha1_80(&policy.rtp);
76 crypto_policy_set_aes_cm_128_hmac_sha1_80(&policy.rtcp);
77 case LibSrtpFuzzer::LIKE_WEBRTC_WITHOUT_AUTH:
78 crypto_policy_set_aes_cm_128_null_auth(&policy.rtp);
79 crypto_policy_set_aes_cm_128_null_auth(&policy.rtcp);
80 break;
81 case LibSrtpFuzzer::AES_GCM:
82 // There was a security bug in the GCM mode in libsrtp 1.5.2.
83 crypto_policy_set_aes_gcm_128_8_auth(&policy.rtp);
84 crypto_policy_set_aes_gcm_128_8_auth(&policy.rtcp);
85 break;
86 }
87
88 memcpy(key, replacement_key, SRTP_MASTER_KEY_LEN);
89 return policy;
90 };
91
92 Environment() {
93 srtp_init();
94
95 // We keep one global rtp_msg_t and copy messages into it. TO
96 // ensure that OOB reads are detected, if building with ASAN we
97 // explicitly poison the entire packet memory and then unpoison
98 // just the region we expect.
99 poison(&message.header, sizeof(srtp_hdr_t));
100 poison(&message.body, RTP_MAX_BUF_LEN);
101
102 memset(&policy, 0, sizeof(policy));
103 policy.ssrc.type = ssrc_any_inbound;
104 policy.ssrc.value = 0xdeadbeef;
105 policy.window_size = 1024;
106 policy.allow_repeat_tx = 1;
107 policy.ekt = nullptr;
108 policy.next = nullptr;
109
110 // Set some defaults in case we don't want to override them later.
111 policy.key = key;
112 crypto_policy_set_null_cipher_null_auth(&policy.rtp);
113 crypto_policy_set_null_cipher_null_auth(&policy.rtcp);
114 }
115 };
116
117 size_t ReadLength(const uint8_t* data, size_t size) {
118 // Read one byte of input and check that that many bytes remain.
119 if (size == 0)
120 return 0;
121 size_t n = static_cast<size_t>(data[0]);
122
123 if (n > size - 1)
124 return 0;
125 else
126 return n;
127 }
128
129 Environment* env = new Environment();
130
131 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
132 // Read one byte and use it to choose a crypto policy.
133 if (size <= 1)
134 return 0;
135 LibSrtpFuzzer::CryptoPolicy policy = static_cast<LibSrtpFuzzer::CryptoPolicy>(
136 data[0] % LibSrtpFuzzer::NUMBER_OF_POLICIES);
137 data += 1;
138 size -= 1;
139
140 // Read some more bytes to use as a key.
141 if (size <= SRTP_MASTER_KEY_LEN)
142 return 0;
143 srtp_policy_t srtp_policy = env->GetCryptoPolicy(policy, data);
144 data += SRTP_MASTER_KEY_LEN;
145 size -= SRTP_MASTER_KEY_LEN;
146
147 // Create a session with the above data.
148 srtp_t session;
149 err_status_t error = srtp_create(&session, &srtp_policy);
150 assert(error == err_status_ok);
151
152 // Read one byte as a packet length N, then feed the next N bytes
153 // into srtp_unprotect. Keep going until we run out of data.
154 size_t packet_size;
155 while ((packet_size = ReadLength(data, size)) > 0) {
156 // One byte was used by ReadLength.
157 data++;
158 size--;
159
160 size_t header_size = std::min(sizeof(srtp_hdr_t), packet_size);
161 size_t body_size = packet_size - std::min(packet_size, header_size);
mmoroz 2016/07/06 09:44:22 I think we can use packet_size - header_size. It w
katrielc 2016/07/06 10:36:03 Haha, I stared at this code for five minutes and s
162
163 unpoison(&env->message.header, header_size);
mmoroz 2016/07/06 09:44:22 Manual poisoning is not the best approach, but I a
katrielc 2016/07/06 10:36:03 Done.
164 memcpy(&env->message.header, data, header_size);
165 if (body_size > 0) {
166 unpoison(&env->message.body, body_size);
167 memcpy(&env->message.body, data + header_size, body_size);
168 };
169
170 int out_len = static_cast<int>(packet_size);
171 std::vector<unsigned char> packet(data, data + packet_size);
mmoroz 2016/07/06 09:44:22 For what do we need the same data in both env-> me
katrielc 2016/07/06 10:36:03 Oops. Fixed.
172 srtp_unprotect(session, reinterpret_cast<rtp_msg_t*>(packet.data()),
173 &out_len);
174
175 poison(&env->message.header, sizeof(srtp_hdr_t));
176 poison(&env->message.body, RTP_MAX_BUF_LEN);
177
178 // |packet_size| bytes were used above.
179 data += packet_size;
180 size -= packet_size;
181 }
182
183 srtp_dealloc(session);
184 return 0;
185 }
OLDNEW
« no previous file with comments | « testing/libfuzzer/fuzzers/BUILD.gn ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698