Manuel Pégourié-Gonnard | 6240def | 2020-07-10 09:35:54 +0200 | [diff] [blame] | 1 | /** |
| 2 | * \file constant_flow.h |
| 3 | * |
| 4 | * \brief This file contains tools to ensure tested code has constant flow. |
| 5 | */ |
| 6 | |
| 7 | /* |
Dan Handley | 5011814 | 2020-08-20 11:20:12 +0100 | [diff] [blame] | 8 | * Copyright The Mbed TLS Contributors |
Manuel Pégourié-Gonnard | 6240def | 2020-07-10 09:35:54 +0200 | [diff] [blame] | 9 | * SPDX-License-Identifier: Apache-2.0 |
| 10 | * |
| 11 | * Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 12 | * not use this file except in compliance with the License. |
| 13 | * You may obtain a copy of the License at |
| 14 | * |
| 15 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 16 | * |
| 17 | * Unless required by applicable law or agreed to in writing, software |
| 18 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 19 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 20 | * See the License for the specific language governing permissions and |
| 21 | * limitations under the License. |
Manuel Pégourié-Gonnard | 6240def | 2020-07-10 09:35:54 +0200 | [diff] [blame] | 22 | */ |
| 23 | |
| 24 | #ifndef TEST_CONSTANT_FLOW_H |
| 25 | #define TEST_CONSTANT_FLOW_H |
| 26 | |
Bence Szépkúti | c662b36 | 2021-05-27 11:25:03 +0200 | [diff] [blame] | 27 | #include "mbedtls/build_info.h" |
Manuel Pégourié-Gonnard | 6240def | 2020-07-10 09:35:54 +0200 | [diff] [blame] | 28 | |
Manuel Pégourié-Gonnard | 73afa37 | 2020-08-19 10:27:38 +0200 | [diff] [blame] | 29 | /* |
| 30 | * This file defines the two macros |
| 31 | * |
| 32 | * #define TEST_CF_SECRET(ptr, size) |
| 33 | * #define TEST_CF_PUBLIC(ptr, size) |
| 34 | * |
| 35 | * that can be used in tests to mark a memory area as secret (no branch or |
| 36 | * memory access should depend on it) or public (default, only needs to be |
| 37 | * marked explicitly when it was derived from secret data). |
| 38 | * |
| 39 | * Arguments: |
| 40 | * - ptr: a pointer to the memory area to be marked |
| 41 | * - size: the size in bytes of the memory area |
| 42 | * |
| 43 | * Implementation: |
| 44 | * The basic idea is that of ctgrind <https://github.com/agl/ctgrind>: we can |
| 45 | * re-use tools that were designed for checking use of uninitialized memory. |
| 46 | * This file contains two implementations: one based on MemorySanitizer, the |
| 47 | * other on valgrind's memcheck. If none of them is enabled, dummy macros that |
| 48 | * do nothing are defined for convenience. |
Gilles Peskine | 0c67160 | 2022-11-29 16:01:41 +0100 | [diff] [blame^] | 49 | * |
| 50 | * \note #TEST_CF_SECRET must be called directly from within a .function file, |
| 51 | * not indirectly via a macro defined under tests/include or a function |
| 52 | * under tests/src. This is because we only run Valgrind for constant |
| 53 | * flow on test suites that have greppable annotations inside them (see |
| 54 | * `skip_suites_without_constant_flow` in `tests/scripts/all.sh`). |
Manuel Pégourié-Gonnard | 73afa37 | 2020-08-19 10:27:38 +0200 | [diff] [blame] | 55 | */ |
| 56 | |
Manuel Pégourié-Gonnard | 6240def | 2020-07-10 09:35:54 +0200 | [diff] [blame] | 57 | #if defined(MBEDTLS_TEST_CONSTANT_FLOW_MEMSAN) |
| 58 | #include <sanitizer/msan_interface.h> |
| 59 | |
| 60 | /* Use macros to avoid messing up with origin tracking */ |
| 61 | #define TEST_CF_SECRET __msan_allocated_memory |
| 62 | // void __msan_allocated_memory(const volatile void* data, size_t size); |
| 63 | #define TEST_CF_PUBLIC __msan_unpoison |
| 64 | // void __msan_unpoison(const volatile void *a, size_t size); |
| 65 | |
Manuel Pégourié-Gonnard | 73afa37 | 2020-08-19 10:27:38 +0200 | [diff] [blame] | 66 | #elif defined(MBEDTLS_TEST_CONSTANT_FLOW_VALGRIND) |
| 67 | #include <valgrind/memcheck.h> |
| 68 | |
| 69 | #define TEST_CF_SECRET VALGRIND_MAKE_MEM_UNDEFINED |
| 70 | // VALGRIND_MAKE_MEM_UNDEFINED(_qzz_addr, _qzz_len) |
| 71 | #define TEST_CF_PUBLIC VALGRIND_MAKE_MEM_DEFINED |
| 72 | // VALGRIND_MAKE_MEM_DEFINED(_qzz_addr, _qzz_len) |
| 73 | |
| 74 | #else /* MBEDTLS_TEST_CONSTANT_FLOW_MEMSAN || |
| 75 | MBEDTLS_TEST_CONSTANT_FLOW_VALGRIND */ |
Manuel Pégourié-Gonnard | 6240def | 2020-07-10 09:35:54 +0200 | [diff] [blame] | 76 | |
| 77 | #define TEST_CF_SECRET(ptr, size) |
| 78 | #define TEST_CF_PUBLIC(ptr, size) |
| 79 | |
Manuel Pégourié-Gonnard | 73afa37 | 2020-08-19 10:27:38 +0200 | [diff] [blame] | 80 | #endif /* MBEDTLS_TEST_CONSTANT_FLOW_MEMSAN || |
| 81 | MBEDTLS_TEST_CONSTANT_FLOW_VALGRIND */ |
Manuel Pégourié-Gonnard | 6240def | 2020-07-10 09:35:54 +0200 | [diff] [blame] | 82 | |
| 83 | #endif /* TEST_CONSTANT_FLOW_H */ |