OLD | NEW |
---|---|
(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 // TODO(katrielc) Also test the authenticated path, which is what | |
17 // WebRTC uses. This is nontrivial because you need to bypass the MAC | |
18 // check. Two options: add a UNSAFE_FUZZER_MODE flag to libsrtp (or | |
19 // the chromium fork of it), or compute the HMAC of whatever gibberish | |
20 // the fuzzer produces and write it into the packet manually. | |
21 | |
22 namespace LibSrtpFuzzer { | |
23 enum CryptoPolicy { | |
24 NONE, | |
25 LIKE_WEBRTC, | |
26 LIKE_WEBRTC_WITHOUT_AUTH, | |
27 AES_GCM, | |
28 NUMBER_OF_POLICIES, | |
29 }; | |
30 } | |
31 | |
32 struct Environment { | |
33 srtp_policy_t GetCryptoPolicy(LibSrtpFuzzer::CryptoPolicy crypto_policy, | |
34 const unsigned char* replacement_key) { | |
35 switch (crypto_policy) { | |
36 case LibSrtpFuzzer::NUMBER_OF_POLICIES: | |
37 case LibSrtpFuzzer::NONE: | |
38 crypto_policy_set_null_cipher_null_auth(&policy.rtp); | |
39 crypto_policy_set_null_cipher_null_auth(&policy.rtcp); | |
40 break; | |
41 case LibSrtpFuzzer::LIKE_WEBRTC: | |
42 crypto_policy_set_aes_cm_128_hmac_sha1_80(&policy.rtp); | |
43 crypto_policy_set_aes_cm_128_hmac_sha1_80(&policy.rtcp); | |
44 case LibSrtpFuzzer::LIKE_WEBRTC_WITHOUT_AUTH: | |
45 crypto_policy_set_aes_cm_128_null_auth(&policy.rtp); | |
46 crypto_policy_set_aes_cm_128_null_auth(&policy.rtcp); | |
47 break; | |
48 case LibSrtpFuzzer::AES_GCM: | |
49 // There was a security bug in the GCM mode in libsrtp 1.5.2. | |
50 crypto_policy_set_aes_gcm_128_8_auth(&policy.rtp); | |
51 crypto_policy_set_aes_gcm_128_8_auth(&policy.rtcp); | |
52 break; | |
53 } | |
54 | |
55 memcpy(key, replacement_key, SRTP_MASTER_KEY_LEN); | |
56 return policy; | |
57 }; | |
58 | |
59 Environment() { | |
60 srtp_init(); | |
61 | |
62 memset(&policy, 0, sizeof(policy)); | |
63 policy.allow_repeat_tx = 1; | |
64 policy.ekt = nullptr; | |
65 policy.key = key; | |
66 policy.next = nullptr; | |
67 policy.ssrc.type = ssrc_any_inbound; | |
68 policy.ssrc.value = 0xdeadbeef; | |
69 policy.window_size = 1024; | |
70 } | |
71 | |
72 private: | |
73 srtp_policy_t policy; | |
74 unsigned char key[SRTP_MASTER_KEY_LEN] = {0}; | |
75 | |
76 static void crypto_policy_set_null_cipher_null_auth(crypto_policy_t* p) { | |
77 p->cipher_type = NULL_CIPHER; | |
78 p->cipher_key_len = 0; | |
79 p->auth_type = NULL_AUTH; | |
80 p->auth_key_len = 0; | |
81 p->auth_tag_len = 0; | |
82 p->sec_serv = sec_serv_none; | |
83 }; | |
84 }; | |
85 | |
86 size_t ReadLength(const uint8_t* data, size_t size) { | |
87 // Read one byte of input and check that that many bytes remain. | |
88 if (size == 0) | |
89 return 0; | |
90 size_t n = static_cast<size_t>(data[0]); | |
91 | |
92 if (n > size - 1) | |
93 return 0; | |
mmoroz
2016/07/06 12:13:14
May be let's return |size - 1| here and use all th
katrielc
2016/07/06 12:23:36
Done.
| |
94 else | |
95 return n; | |
96 } | |
97 | |
98 Environment* env = new Environment(); | |
99 | |
100 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) { | |
101 // Read one byte and use it to choose a crypto policy. | |
102 if (size <= 1) | |
103 return 0; | |
104 LibSrtpFuzzer::CryptoPolicy policy = static_cast<LibSrtpFuzzer::CryptoPolicy>( | |
105 data[0] % LibSrtpFuzzer::NUMBER_OF_POLICIES); | |
106 data += 1; | |
107 size -= 1; | |
108 | |
109 // Read some more bytes to use as a key. | |
110 if (size <= SRTP_MASTER_KEY_LEN) | |
111 return 0; | |
112 srtp_policy_t srtp_policy = env->GetCryptoPolicy(policy, data); | |
113 data += SRTP_MASTER_KEY_LEN; | |
114 size -= SRTP_MASTER_KEY_LEN; | |
115 | |
116 srtp_t session; | |
117 err_status_t error = srtp_create(&session, &srtp_policy); | |
118 assert(error == err_status_ok); | |
119 | |
mmoroz
2016/07/06 12:13:14
Comment from previous patchset (lines 152-153) mak
katrielc
2016/07/06 12:23:36
Done.
| |
120 size_t packet_size; | |
121 while ((packet_size = ReadLength(data, size)) > 0) { | |
122 // One byte was used by ReadLength. | |
123 data++; | |
124 size--; | |
125 | |
126 size_t header_size = std::min(sizeof(srtp_hdr_t), packet_size); | |
127 size_t body_size = packet_size - header_size; | |
128 | |
129 // We deliberately do not initialise this struct. MSAN will catch | |
130 // any reads beyond the provided data. | |
mmoroz
2016/07/06 12:13:14
Actually MSan catches not simple reads, but usage
katrielc
2016/07/06 12:23:36
Acknowledged.
| |
131 rtp_msg_t message; | |
132 memcpy(&message.header, data, header_size); | |
133 memcpy(&message.body, data + header_size, body_size); | |
134 | |
135 int out_len = static_cast<int>(packet_size); | |
136 srtp_unprotect(session, &message, &out_len); | |
137 | |
138 // |packet_size| bytes were used above. | |
139 data += packet_size; | |
140 size -= packet_size; | |
141 } | |
142 | |
143 srtp_dealloc(session); | |
144 return 0; | |
145 } | |
OLD | NEW |