blob: b64c0b542eb7977bae964b5b5d4b9019e6001a16 [file] [log] [blame]
Laurence Lundbladeb69cad72018-09-13 11:09:01 -07001/*==============================================================================
2Copyright (c) 2016-2018, The Linux Foundation. All rights reserved.
3
4Redistribution and use in source and binary forms, with or without
5modification, are permitted provided that the following conditions are
6met:
7 * Redistributions of source code must retain the above copyright
8 notice, this list of conditions and the following disclaimer.
9 * Redistributions in binary form must reproduce the above
10 copyright notice, this list of conditions and the following
11 disclaimer in the documentation and/or other materials provided
12 with the distribution.
13 * Neither the name of The Linux Foundation nor the names of its
14 contributors may be used to endorse or promote products derived
15 from this software without specific prior written permission.
16
17THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
18WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
20ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
21BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
24BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
25WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
26OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
27IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28==============================================================================*/
29
Laurence Lundblade624405d2018-09-18 20:10:47 -070030/*==============================================================================
31 Modifications beyond the version released on CAF are under the MIT license:
32
33 Copyright 2018 Laurence Lundblade
34
35 Permission is hereby granted, free of charge, to any person obtaining
36 a copy of this software and associated documentation files (the
37 "Software"), to deal in the Software without restriction, including
38 without limitation the rights to use, copy, modify, merge, publish,
39 distribute, sublicense, and/or sell copies of the Software, and to
40 permit persons to whom the Software is furnished to do so, subject to
41 the following conditions:
42
43 The above copyright notice and this permission notice shall be included
44 in all copies or substantial portions of the Software.
45
46 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
47 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
48 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
49 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
50 BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
51 ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
52 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
53 SOFTWARE.
54 ==============================================================================*/
55
Laurence Lundbladeb69cad72018-09-13 11:09:01 -070056/*===================================================================================
57 FILE: UsefulBuf.c
58
59 DESCRIPTION: General purpose input and output buffers
60
61 EDIT HISTORY FOR FILE:
62
63 This section contains comments describing changes made to the module.
64 Notice that changes are listed in reverse chronological order.
65
66 when who what, where, why
67 -------- ---- ---------------------------------------------------
68 09/07/17 llundbla Fix critical bug in UsefulBuf_Find() -- a read off
69 the end of memory when the bytes to find is longer
70 than the bytes to search.
71 06/27/17 llundbla Fix UsefulBuf_Compare() bug. Only affected comparison
72 for < or > for unequal length buffers. Added
73 UsefulBuf_Set() function.
74 05/30/17 llundbla Functions for NULL UsefulBufs and const / unconst
75 11/13/16 llundbla Initial Version.
76
77 =====================================================================================*/
78
Laurence Lundbladeb69cad72018-09-13 11:09:01 -070079#include "UsefulBuf.h"
Laurence Lundbladeb69cad72018-09-13 11:09:01 -070080
81#define USEFUL_OUT_BUF_MAGIC (0x0B0F) // used to catch use of uninitialized or corrupted UOBs
82
83/*
84 Public function -- see UsefulBuf.h
85 */
Laurence Lundblade2296db52018-09-14 18:08:39 -070086UsefulBufC UsefulBuf_Copy(UsefulBuf Dest, const UsefulBufC Src)
Laurence Lundbladeb69cad72018-09-13 11:09:01 -070087{
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +053088 if(Src.len > Dest.len) {
Laurence Lundblade2296db52018-09-14 18:08:39 -070089 return NULLUsefulBufC;
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +053090 }
Laurence Lundbladeb69cad72018-09-13 11:09:01 -070091
Laurence Lundblade2296db52018-09-14 18:08:39 -070092 memcpy(Dest.ptr, Src.ptr, Src.len);
93
94 return((UsefulBufC){Dest.ptr, Src.len});
Laurence Lundbladeb69cad72018-09-13 11:09:01 -070095}
96
Laurence Lundblade041ffa52018-10-07 11:43:51 +070097
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +053098/*
99 Public function -- see UsefulBuf.h
100 */
Laurence Lundblade041ffa52018-10-07 11:43:51 +0700101UsefulBufC UsefulBuf_CopyOffset(UsefulBuf Dest, size_t uOffset, const UsefulBufC Src)
102{
103 if(Src.len > Dest.len - uOffset) {
104 return NULLUsefulBufC;
105 }
106
107 memcpy(Dest.ptr + uOffset, Src.ptr, Src.len);
108
109 return((UsefulBufC){Dest.ptr, Src.len});
110}
111
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530112
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700113/*
114 Public function -- see UsefulBuf.h
115 */
116int UsefulBuf_Compare(const UsefulBufC UB1, const UsefulBufC UB2)
117{
118 // use the comparisons rather than subtracting lengths to
119 // return an int instead of a size_t
120 if(UB1.len < UB2.len) {
121 return -1;
122 } else if (UB1.len > UB2.len) {
123 return 1;
124 } // else UB1.len == UB2.len
125
126 return memcmp(UB1.ptr, UB2.ptr, UB1.len);
127}
128
129
Laurence Lundbladeda3f0822018-09-18 19:49:02 -0700130
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700131/*
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530132 Public function -- see UsefulBuf.h
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700133 */
134size_t UsefulBuf_FindBytes(UsefulBufC BytesToSearch, UsefulBufC BytesToFind)
135{
136 if(BytesToSearch.len < BytesToFind.len) {
137 return SIZE_MAX;
138 }
139
140 for(size_t uPos = 0; uPos <= BytesToSearch.len - BytesToFind.len; uPos++) {
141 if(!UsefulBuf_Compare((UsefulBufC){((uint8_t *)BytesToSearch.ptr) + uPos, BytesToFind.len}, BytesToFind)) {
142 return uPos;
143 }
144 }
145
146 return SIZE_MAX;
147}
148
149
150/*
151 Public function -- see UsefulBuf.h
152
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530153 Code Reviewers: THIS FUNCTION DOES POINTER MATH
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700154 */
Laurence Lundblade2296db52018-09-14 18:08:39 -0700155void UsefulOutBuf_Init(UsefulOutBuf *me, UsefulBuf Storage)
156{
157 me->magic = USEFUL_OUT_BUF_MAGIC;
158 UsefulOutBuf_Reset(me);
159 me->UB = Storage;
160
Laurence Lundblade2296db52018-09-14 18:08:39 -0700161#if 0
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530162 // This check is off by default.
163
164 // The following check fails on ThreadX
165
Laurence Lundblade2296db52018-09-14 18:08:39 -0700166 // Sanity check on the pointer and size to be sure we are not
167 // passed a buffer that goes off the end of the address space.
168 // Given this test, we know that all unsigned lengths less than
169 // me->size are valid and won't wrap in any pointer additions
170 // based off of pStorage in the rest of this code.
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530171 const uintptr_t ptrM = UINTPTR_MAX - Storage.len;
172 if(Storage.ptr && (uintptr_t)Storage.ptr > ptrM) // Check #0
Laurence Lundblade2296db52018-09-14 18:08:39 -0700173 me->err = 1;
174#endif
175}
176
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700177
178
179/*
180 Public function -- see UsefulBuf.h
181
182 The core of UsefulOutBuf -- put some bytes in the buffer without writing off the end of it.
183
184 Code Reviewers: THIS FUNCTION DOES POINTER MATH
185
186 This function inserts the source buffer, NewData, into the destination buffer, me->UB.ptr.
187
188 Destination is represented as:
189 me->UB.ptr -- start of the buffer
Laurence Lundblade9e7f4742018-09-20 18:50:31 -0700190 me->UB.len -- size of the buffer UB.ptr
191 me->data_len -- length of value data in UB
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700192
193 Source is data:
194 NewData.ptr -- start of source buffer
195 NewData.len -- length of source buffer
196
197 Insertion point:
198 uInsertionPos.
199
200 Steps:
201
202 0. Corruption checks on UsefulOutBuf
203
204 1. Figure out if the new data will fit or not
205
206 2. Is insertion position in the range of valid data?
207
208 3. If insertion point is not at the end, slide data to the right of the insertion point to the right
209
210 4. Put the new data in at the insertion position.
211
212 */
213void UsefulOutBuf_InsertUsefulBuf(UsefulOutBuf *me, UsefulBufC NewData, size_t uInsertionPos)
214{
215 if(me->err) {
216 // Already in error state.
217 return;
218 }
219
220 /* 0. Sanity check the UsefulOutBuf structure */
221 // A "counter measure". If magic number is not the right number it
222 // probably means me was not initialized or it was corrupted. Attackers
223 // can defeat this, but it is a hurdle and does good with very
224 // little code.
225 if(me->magic != USEFUL_OUT_BUF_MAGIC) {
226 me->err = 1;
227 return; // Magic number is wrong due to uninitalization or corrption
228 }
229
230 // Make sure valid data is less than buffer size. This would only occur
231 // if there was corruption of me, but it is also part of the checks to
232 // be sure there is no pointer arithmatic under/overflow.
Laurence Lundblade2296db52018-09-14 18:08:39 -0700233 if(me->data_len > me->UB.len) { // Check #1
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700234 me->err = 1;
235 return; // Offset of valid data is off the end of the UsefulOutBuf due to uninitialization or corruption
236 }
237
238 /* 1. Will it fit? */
Laurence Lundblade9e7f4742018-09-20 18:50:31 -0700239 // WillItFit() is the same as: NewData.len <= (me->size - me->data_len)
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700240 // Check #1 makes sure subtraction in RoomLeft will not wrap around
241 if(! UsefulOutBuf_WillItFit(me, NewData.len)) { // Check #2
242 // The new data will not fit into the the buffer.
243 me->err = 1;
244 return;
245 }
246
247 /* 2. Check the Insertion Position */
248 // This, with Check #1, also confirms that uInsertionPos <= me->size
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530249 if(uInsertionPos > me->data_len) { // Check #3
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700250 // Off the end of the valid data in the buffer.
251 me->err = 1;
252 return;
253 }
254
255 /* 3. Slide existing data to the right */
256 uint8_t *pSourceOfMove = ((uint8_t *)me->UB.ptr) + uInsertionPos; // PtrMath #1
Laurence Lundblade9e7f4742018-09-20 18:50:31 -0700257 size_t uNumBytesToMove = me->data_len - uInsertionPos; // PtrMath #2
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700258 uint8_t *pDestinationOfMove = pSourceOfMove + NewData.len; // PtrMath #3
Laurence Lundblade2296db52018-09-14 18:08:39 -0700259 size_t uRoomInDestination = me->UB.len - (uInsertionPos + NewData.len); // PtrMath #4
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700260
261 if(uNumBytesToMove && me->UB.ptr) {
Laurence Lundblade74f68412018-09-13 12:18:49 -0700262 memmove(pDestinationOfMove, pSourceOfMove, uNumBytesToMove);
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700263 }
264
265 /* 4. Put the new data in */
266 uint8_t *pInsertionPoint = ((uint8_t *)me->UB.ptr) + uInsertionPos; // PtrMath #5
Laurence Lundblade2296db52018-09-14 18:08:39 -0700267 uRoomInDestination = me->UB.len - uInsertionPos; // PtrMath #6
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700268 if(me->UB.ptr) {
Laurence Lundblade74f68412018-09-13 12:18:49 -0700269 memmove(pInsertionPoint, NewData.ptr, NewData.len);
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700270 }
Laurence Lundblade2296db52018-09-14 18:08:39 -0700271 me->data_len += NewData.len ;
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700272}
273
274
275/*
276 Rationale that describes why the above pointer math is safe
277
278 PtrMath #1 will never wrap around over because
279 Check #0 in UsefulOutBuf_Init makes sure me-UB.ptr + me->size doesn't wrap
Laurence Lundblade9e7f4742018-09-20 18:50:31 -0700280 Check #1 makes sure me->data_len is less than me->UB.len
281 Check #3 makes sure uInsertionPos is less than me->data_len
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700282
283 PtrMath #2 will never wrap around under because
Laurence Lundblade9e7f4742018-09-20 18:50:31 -0700284 Check #3 makes sure uInsertionPos is less than me->data_len
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700285
286 PtrMath #3 will never wrap around over because todo
287 PtrMath #1 is checked resulting in pStartOfDataToMove being between me->UB.ptr and a maximum valid ptr
288
289 PtrMath #4 will never wrap under because
Laurence Lundblade9e7f4742018-09-20 18:50:31 -0700290 Check #3 makes sure uInsertionPos is less than me->data_len
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700291 Check #3 allows Check #2 to be refactored as NewData.Len > (me->size - uInsertionPos)
292 This algebraically rearranges to me->size > uInsertionPos + NewData.len
293
294 PtrMath #5 is exactly the same as PtrMath #1
295
296 PtrMath #6 will never wrap under because
Laurence Lundblade9e7f4742018-09-20 18:50:31 -0700297 Check #1 makes sure me->data_len is less than me->size
298 Check #3 makes sure uInsertionPos is less than me->data_len
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700299 */
300
301
302/*
Laurence Lundblade2296db52018-09-14 18:08:39 -0700303 Public function -- see UsefulBuf.h
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700304 */
Laurence Lundblade2296db52018-09-14 18:08:39 -0700305UsefulBufC UsefulOutBuf_OutUBuf(UsefulOutBuf *me)
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700306{
307 if(me->err) {
Laurence Lundblade2296db52018-09-14 18:08:39 -0700308 return NULLUsefulBufC;
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700309 }
310
311 if(me->magic != USEFUL_OUT_BUF_MAGIC) {
312 me->err = 1;
Laurence Lundblade2296db52018-09-14 18:08:39 -0700313 return NULLUsefulBufC;
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700314 }
Laurence Lundblade2296db52018-09-14 18:08:39 -0700315
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530316 return(UsefulBufC){me->UB.ptr,me->data_len};
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700317}
318
319
320/*
321 Public function -- see UsefulBuf.h
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700322
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530323 Copy out the data accumulated in to the output buffer.
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700324 */
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530325UsefulBufC UsefulOutBuf_CopyOut(UsefulOutBuf *me, UsefulBuf pDest)
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700326{
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530327 UsefulBufC Tmp = UsefulOutBuf_OutUBuf(me);
328 if(UsefulBuf_IsNULLC(Tmp)) {
329 return NULLUsefulBufC;
330 }
331 return UsefulBuf_Copy(pDest, Tmp);
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700332}
333
334
335
336
337/*
338 Public function -- see UsefulBuf.h
339
340 The core of UsefulInputBuf -- consume some bytes without going off the end of the buffer.
341
342 Code Reviewers: THIS FUNCTION DOES POINTER MATH
343 */
344const void * UsefulInputBuf_GetBytes(UsefulInputBuf *me, size_t uAmount)
345{
346 // Already in error state. Do nothing.
347 if(me->err) {
348 return NULL;
349 }
350
351 if(!UsefulInputBuf_BytesAvailable(me, uAmount)) {
352 // The number of bytes asked for at current position are more than available
353 me->err = 1;
354 return NULL;
355 }
356
357 // This is going to succeed
358 const void * const result = ((uint8_t *)me->UB.ptr) + me->cursor;
359 me->cursor += uAmount; // this will not overflow because of check using UsefulInputBuf_BytesAvailable()
360 return result;
361}
362