]> git.proxmox.com Git - mirror_edk2.git/blob - CryptoPkg/Library/BaseCryptLib/SysCall/CrtWrapper.c
CryptoPkg/Crt: turn strchr() into a function (CVE-2019-14553)
[mirror_edk2.git] / CryptoPkg / Library / BaseCryptLib / SysCall / CrtWrapper.c
1 /** @file
2 C Run-Time Libraries (CRT) Wrapper Implementation for OpenSSL-based
3 Cryptographic Library.
4
5 Copyright (c) 2009 - 2017, Intel Corporation. All rights reserved.<BR>
6 SPDX-License-Identifier: BSD-2-Clause-Patent
7
8 **/
9
10 #include <CrtLibSupport.h>
11
12 int errno = 0;
13
14 FILE *stderr = NULL;
15 FILE *stdin = NULL;
16 FILE *stdout = NULL;
17
18 typedef
19 int
20 (*SORT_COMPARE)(
21 IN VOID *Buffer1,
22 IN VOID *Buffer2
23 );
24
25 //
26 // Duplicated from EDKII BaseSortLib for qsort() wrapper
27 //
28 STATIC
29 VOID
30 QuickSortWorker (
31 IN OUT VOID *BufferToSort,
32 IN CONST UINTN Count,
33 IN CONST UINTN ElementSize,
34 IN SORT_COMPARE CompareFunction,
35 IN VOID *Buffer
36 )
37 {
38 VOID *Pivot;
39 UINTN LoopCount;
40 UINTN NextSwapLocation;
41
42 ASSERT(BufferToSort != NULL);
43 ASSERT(CompareFunction != NULL);
44 ASSERT(Buffer != NULL);
45
46 if (Count < 2 || ElementSize < 1) {
47 return;
48 }
49
50 NextSwapLocation = 0;
51
52 //
53 // Pick a pivot (we choose last element)
54 //
55 Pivot = ((UINT8 *)BufferToSort + ((Count - 1) * ElementSize));
56
57 //
58 // Now get the pivot such that all on "left" are below it
59 // and everything "right" are above it
60 //
61 for (LoopCount = 0; LoopCount < Count - 1; LoopCount++)
62 {
63 //
64 // If the element is less than the pivot
65 //
66 if (CompareFunction ((VOID *)((UINT8 *)BufferToSort + ((LoopCount) * ElementSize)), Pivot) <= 0) {
67 //
68 // Swap
69 //
70 CopyMem (Buffer, (UINT8 *)BufferToSort + (NextSwapLocation * ElementSize), ElementSize);
71 CopyMem ((UINT8 *)BufferToSort + (NextSwapLocation * ElementSize), (UINT8 *)BufferToSort + ((LoopCount) * ElementSize), ElementSize);
72 CopyMem ((UINT8 *)BufferToSort + ((LoopCount) * ElementSize), Buffer, ElementSize);
73
74 //
75 // Increment NextSwapLocation
76 //
77 NextSwapLocation++;
78 }
79 }
80 //
81 // Swap pivot to its final position (NextSwapLocation)
82 //
83 CopyMem (Buffer, Pivot, ElementSize);
84 CopyMem (Pivot, (UINT8 *)BufferToSort + (NextSwapLocation * ElementSize), ElementSize);
85 CopyMem ((UINT8 *)BufferToSort + (NextSwapLocation * ElementSize), Buffer, ElementSize);
86
87 //
88 // Now recurse on 2 partial lists. Neither of these will have the 'pivot' element.
89 // IE list is sorted left half, pivot element, sorted right half...
90 //
91 QuickSortWorker (
92 BufferToSort,
93 NextSwapLocation,
94 ElementSize,
95 CompareFunction,
96 Buffer
97 );
98
99 QuickSortWorker (
100 (UINT8 *)BufferToSort + (NextSwapLocation + 1) * ElementSize,
101 Count - NextSwapLocation - 1,
102 ElementSize,
103 CompareFunction,
104 Buffer
105 );
106
107 return;
108 }
109
110 //---------------------------------------------------------
111 // Standard C Run-time Library Interface Wrapper
112 //---------------------------------------------------------
113
114 //
115 // -- String Manipulation Routines --
116 //
117
118 char *strchr(const char *str, int ch)
119 {
120 return ScanMem8 (str, AsciiStrSize (str), (UINT8)ch);
121 }
122
123 /* Scan a string for the last occurrence of a character */
124 char *strrchr (const char *str, int c)
125 {
126 char * save;
127
128 for (save = NULL; ; ++str) {
129 if (*str == c) {
130 save = (char *)str;
131 }
132 if (*str == 0) {
133 return (save);
134 }
135 }
136 }
137
138 /* Compare first n bytes of string s1 with string s2, ignoring case */
139 int strncasecmp (const char *s1, const char *s2, size_t n)
140 {
141 int Val;
142
143 ASSERT(s1 != NULL);
144 ASSERT(s2 != NULL);
145
146 if (n != 0) {
147 do {
148 Val = tolower(*s1) - tolower(*s2);
149 if (Val != 0) {
150 return Val;
151 }
152 ++s1;
153 ++s2;
154 if (*s1 == '\0') {
155 break;
156 }
157 } while (--n != 0);
158 }
159 return 0;
160 }
161
162 /* Read formatted data from a string */
163 int sscanf (const char *buffer, const char *format, ...)
164 {
165 //
166 // Null sscanf() function implementation to satisfy the linker, since
167 // no direct functionality logic dependency in present UEFI cases.
168 //
169 return 0;
170 }
171
172 /* Maps errnum to an error-message string */
173 char * strerror (int errnum)
174 {
175 return NULL;
176 }
177
178 /* Computes the length of the maximum initial segment of the string pointed to by s1
179 which consists entirely of characters from the string pointed to by s2. */
180 size_t strspn (const char *s1 , const char *s2)
181 {
182 UINT8 Map[32];
183 UINT32 Index;
184 size_t Count;
185
186 for (Index = 0; Index < 32; Index++) {
187 Map[Index] = 0;
188 }
189
190 while (*s2) {
191 Map[*s2 >> 3] |= (1 << (*s2 & 7));
192 s2++;
193 }
194
195 if (*s1) {
196 Count = 0;
197 while (Map[*s1 >> 3] & (1 << (*s1 & 7))) {
198 Count++;
199 s1++;
200 }
201
202 return Count;
203 }
204
205 return 0;
206 }
207
208 /* Computes the length of the maximum initial segment of the string pointed to by s1
209 which consists entirely of characters not from the string pointed to by s2. */
210 size_t strcspn (const char *s1, const char *s2)
211 {
212 UINT8 Map[32];
213 UINT32 Index;
214 size_t Count;
215
216 for (Index = 0; Index < 32; Index++) {
217 Map[Index] = 0;
218 }
219
220 while (*s2) {
221 Map[*s2 >> 3] |= (1 << (*s2 & 7));
222 s2++;
223 }
224
225 Map[0] |= 1;
226
227 Count = 0;
228 while (!(Map[*s1 >> 3] & (1 << (*s1 & 7)))) {
229 Count ++;
230 s1++;
231 }
232
233 return Count;
234 }
235
236 //
237 // -- Character Classification Routines --
238 //
239
240 /* Determines if a particular character is a decimal-digit character */
241 int isdigit (int c)
242 {
243 //
244 // <digit> ::= [0-9]
245 //
246 return (('0' <= (c)) && ((c) <= '9'));
247 }
248
249 /* Determine if an integer represents character that is a hex digit */
250 int isxdigit (int c)
251 {
252 //
253 // <hexdigit> ::= [0-9] | [a-f] | [A-F]
254 //
255 return ((('0' <= (c)) && ((c) <= '9')) ||
256 (('a' <= (c)) && ((c) <= 'f')) ||
257 (('A' <= (c)) && ((c) <= 'F')));
258 }
259
260 /* Determines if a particular character represents a space character */
261 int isspace (int c)
262 {
263 //
264 // <space> ::= [ ]
265 //
266 return ((c) == ' ');
267 }
268
269 /* Determine if a particular character is an alphanumeric character */
270 int isalnum (int c)
271 {
272 //
273 // <alnum> ::= [0-9] | [a-z] | [A-Z]
274 //
275 return ((('0' <= (c)) && ((c) <= '9')) ||
276 (('a' <= (c)) && ((c) <= 'z')) ||
277 (('A' <= (c)) && ((c) <= 'Z')));
278 }
279
280 /* Determines if a particular character is in upper case */
281 int isupper (int c)
282 {
283 //
284 // <uppercase letter> := [A-Z]
285 //
286 return (('A' <= (c)) && ((c) <= 'Z'));
287 }
288
289 //
290 // -- Data Conversion Routines --
291 //
292
293 /* Convert strings to a long-integer value */
294 long strtol (const char *nptr, char **endptr, int base)
295 {
296 //
297 // Null strtol() function implementation to satisfy the linker, since there is
298 // no direct functionality logic dependency in present UEFI cases.
299 //
300 return 0;
301 }
302
303 /* Convert strings to an unsigned long-integer value */
304 unsigned long strtoul (const char *nptr, char **endptr, int base)
305 {
306 //
307 // Null strtoul() function implementation to satisfy the linker, since there is
308 // no direct functionality logic dependency in present UEFI cases.
309 //
310 return 0;
311 }
312
313 /* Convert character to lowercase */
314 int tolower (int c)
315 {
316 if (('A' <= (c)) && ((c) <= 'Z')) {
317 return (c - ('A' - 'a'));
318 }
319 return (c);
320 }
321
322 //
323 // -- Searching and Sorting Routines --
324 //
325
326 /* Performs a quick sort */
327 void qsort (void *base, size_t num, size_t width, int (*compare)(const void *, const void *))
328 {
329 VOID *Buffer;
330
331 ASSERT (base != NULL);
332 ASSERT (compare != NULL);
333
334 //
335 // Use CRT-style malloc to cover BS and RT memory allocation.
336 //
337 Buffer = malloc (width);
338 ASSERT (Buffer != NULL);
339
340 //
341 // Re-use PerformQuickSort() function Implementation in EDKII BaseSortLib.
342 //
343 QuickSortWorker (base, (UINTN)num, (UINTN)width, (SORT_COMPARE)compare, Buffer);
344
345 free (Buffer);
346 return;
347 }
348
349 //
350 // -- Process and Environment Control Routines --
351 //
352
353 /* Get a value from the current environment */
354 char *getenv (const char *varname)
355 {
356 //
357 // Null getenv() function implementation to satisfy the linker, since there is
358 // no direct functionality logic dependency in present UEFI cases.
359 //
360 return NULL;
361 }
362
363 /* Get a value from the current environment */
364 char *secure_getenv (const char *varname)
365 {
366 //
367 // Null secure_getenv() function implementation to satisfy the linker, since
368 // there is no direct functionality logic dependency in present UEFI cases.
369 //
370 // From the secure_getenv() manual: 'just like getenv() except that it
371 // returns NULL in cases where "secure execution" is required'.
372 //
373 return NULL;
374 }
375
376 //
377 // -- Stream I/O Routines --
378 //
379
380 /* Write data to a stream */
381 size_t fwrite (const void *buffer, size_t size, size_t count, FILE *stream)
382 {
383 return 0;
384 }
385
386 //
387 // -- Dummy OpenSSL Support Routines --
388 //
389
390 int BIO_printf (void *bio, const char *format, ...)
391 {
392 return 0;
393 }
394
395 int BIO_snprintf(char *buf, size_t n, const char *format, ...)
396 {
397 return 0;
398 }
399
400 #ifdef __GNUC__
401
402 typedef
403 VOID
404 (EFIAPI *NoReturnFuncPtr)(
405 VOID
406 ) __attribute__((__noreturn__));
407
408 STATIC
409 VOID
410 EFIAPI
411 NopFunction (
412 VOID
413 )
414 {
415 }
416
417 void abort (void)
418 {
419 NoReturnFuncPtr NoReturnFunc;
420
421 NoReturnFunc = (NoReturnFuncPtr) NopFunction;
422
423 NoReturnFunc ();
424 }
425
426 #else
427
428 void abort (void)
429 {
430 // Do nothing
431 }
432
433 #endif
434
435 int fclose (FILE *f)
436 {
437 return 0;
438 }
439
440 FILE *fopen (const char *c, const char *m)
441 {
442 return NULL;
443 }
444
445 size_t fread (void *b, size_t c, size_t i, FILE *f)
446 {
447 return 0;
448 }
449
450 uid_t getuid (void)
451 {
452 return 0;
453 }
454
455 uid_t geteuid (void)
456 {
457 return 0;
458 }
459
460 gid_t getgid (void)
461 {
462 return 0;
463 }
464
465 gid_t getegid (void)
466 {
467 return 0;
468 }
469
470 int printf (char const *fmt, ...)
471 {
472 return 0;
473 }