| 1 | /* |
| 2 | * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"). |
| 5 | * You may not use this file except in compliance with the License. |
| 6 | * A copy of the License is located at |
| 7 | * |
| 8 | * http://aws.amazon.com/apache2.0 |
| 9 | * |
| 10 | * or in the "license" file accompanying this file. This file is distributed |
| 11 | * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either |
| 12 | * express or implied. See the License for the specific language governing |
| 13 | * permissions and limitations under the License. |
| 14 | */ |
| 15 | |
| 16 | #define _GNU_SOURCE |
| 17 | |
| 18 | #include <stdio.h> |
| 19 | #include <dlfcn.h> |
| 20 | |
| 21 | #include "api/s2n.h" |
| 22 | #include "error/s2n_errno.h" |
| 23 | |
| 24 | #include "stuffer/s2n_stuffer.h" |
| 25 | |
| 26 | #include "utils/s2n_safety.h" |
| 27 | #include "utils/s2n_random.h" |
| 28 | |
| 29 | int s2n_stuffer_send_to_fd(struct s2n_stuffer *stuffer, const int wfd, const uint32_t len, uint32_t *bytes_sent) |
| 30 | { |
| 31 | /* Override the original s2n_stuffer_send_to_fd to check if the write file descriptor is -1, and if so, skip |
| 32 | * writing anything. This is to speed up fuzz tests that write unnecessary data that is never actually read. |
| 33 | */ |
| 34 | if(wfd == -1){ |
| 35 | stuffer->read_cursor += len; |
| 36 | return len; |
| 37 | } |
| 38 | |
| 39 | /* Otherwise, call the original s2n_stuffer_send_to_fd() */ |
| 40 | typedef int (*orig_s2n_stuffer_send_to_fd_func_type)(struct s2n_stuffer *stuffer, const int wfd, const uint32_t len, uint32_t *bytes_sent); |
| 41 | orig_s2n_stuffer_send_to_fd_func_type orig_s2n_stuffer_send_to_fd; |
| 42 | orig_s2n_stuffer_send_to_fd = (orig_s2n_stuffer_send_to_fd_func_type) dlsym(RTLD_NEXT, "s2n_stuffer_send_to_fd"); |
| 43 | POSIX_GUARD_PTR(orig_s2n_stuffer_send_to_fd); |
| 44 | POSIX_GUARD(orig_s2n_stuffer_send_to_fd(stuffer, wfd, len, bytes_sent)); |
| 45 | return S2N_SUCCESS; |
| 46 | } |
| 47 | |
| 48 | S2N_RESULT s2n_get_public_random_data(struct s2n_blob *blob){ |
| 49 | |
| 50 | /* If fuzzing, only generate "fake" random numbers in order to ensure that fuzz tests are deterministic and repeatable. |
| 51 | * This function should generate non-zero values since this function may be called repeatedly at startup until a |
| 52 | * non-zero value is generated. |
| 53 | */ |
| 54 | for(int i=0; i < blob->size; i++){ |
| 55 | blob->data[i] = 4; /* Fake RNG. Chosen by fair dice roll. https://xkcd.com/221/ */ |
| 56 | } |
| 57 | return S2N_RESULT_OK; |
| 58 | } |