blob: ff464e617c71f50130152772ad6f52f105c7ec71 [file] [log] [blame]
Manuel Pégourié-Gonnard6240def2020-07-10 09:35:54 +02001/**
2 * \file constant_flow.h
3 *
4 * \brief This file contains tools to ensure tested code has constant flow.
5 */
6
7/*
Dan Handley50118142020-08-20 11:20:12 +01008 * Copyright The Mbed TLS Contributors
Manuel Pégourié-Gonnard6240def2020-07-10 09:35:54 +02009 * 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é-Gonnard6240def2020-07-10 09:35:54 +020022 */
23
24#ifndef TEST_CONSTANT_FLOW_H
25#define TEST_CONSTANT_FLOW_H
26
Bence Szépkútic662b362021-05-27 11:25:03 +020027#include "mbedtls/build_info.h"
Manuel Pégourié-Gonnard6240def2020-07-10 09:35:54 +020028
Manuel Pégourié-Gonnard73afa372020-08-19 10:27:38 +020029/*
30 * This file defines the two macros
31 *
32 * #define TEST_CF_SECRET(ptr, size)
33 * #define TEST_CF_PUBLIC(ptr, size)
34 *
Dave Rodgman2d28c462023-07-28 18:22:56 +010035 * and
36 *
37 * #define TEST_CF_SAVE_SECRET(variable)
38 * #define TEST_CF_RESTORE_SECRET(variable)
39 *
Manuel Pégourié-Gonnard73afa372020-08-19 10:27:38 +020040 * that can be used in tests to mark a memory area as secret (no branch or
41 * memory access should depend on it) or public (default, only needs to be
42 * marked explicitly when it was derived from secret data).
43 *
Dave Rodgman2d28c462023-07-28 18:22:56 +010044 * The SAVE/RESTORE forms mark a variable as public, and subsequently restore its
45 * previous secret/not-secret state. This is used where library code is generating
46 * false positives and needs to temporarily disable Memsan checks for a particular
47 * variable, and then restore it's original state afterwards so it doesn't interfere
48 * with other checks.
49 *
Manuel Pégourié-Gonnard73afa372020-08-19 10:27:38 +020050 * Arguments:
51 * - ptr: a pointer to the memory area to be marked
52 * - size: the size in bytes of the memory area
53 *
Dave Rodgman2d28c462023-07-28 18:22:56 +010054 * - variable: a variable name
55 *
Manuel Pégourié-Gonnard73afa372020-08-19 10:27:38 +020056 * Implementation:
57 * The basic idea is that of ctgrind <https://github.com/agl/ctgrind>: we can
58 * re-use tools that were designed for checking use of uninitialized memory.
59 * This file contains two implementations: one based on MemorySanitizer, the
60 * other on valgrind's memcheck. If none of them is enabled, dummy macros that
61 * do nothing are defined for convenience.
Gilles Peskine0c671602022-11-29 16:01:41 +010062 *
63 * \note #TEST_CF_SECRET must be called directly from within a .function file,
64 * not indirectly via a macro defined under tests/include or a function
65 * under tests/src. This is because we only run Valgrind for constant
66 * flow on test suites that have greppable annotations inside them (see
67 * `skip_suites_without_constant_flow` in `tests/scripts/all.sh`).
Manuel Pégourié-Gonnard73afa372020-08-19 10:27:38 +020068 */
69
Manuel Pégourié-Gonnard6240def2020-07-10 09:35:54 +020070#if defined(MBEDTLS_TEST_CONSTANT_FLOW_MEMSAN)
71#include <sanitizer/msan_interface.h>
72
73/* Use macros to avoid messing up with origin tracking */
74#define TEST_CF_SECRET __msan_allocated_memory
75// void __msan_allocated_memory(const volatile void* data, size_t size);
76#define TEST_CF_PUBLIC __msan_unpoison
77// void __msan_unpoison(const volatile void *a, size_t size);
78
Dave Rodgman2b174ab2023-07-28 18:29:41 +010079#define TEST_CF_SAVE_SECRET(_x) \
80 int _test_cf_is_public_ ## _x = __msan_test_shadow(&(_x), sizeof(_x)) == -1; \
81 TEST_CF_PUBLIC(&(_x), sizeof(_x));
82#define TEST_CF_RESTORE_SECRET(_x) \
83 if (!_test_cf_is_public_ ## _x) TEST_CF_SECRET(&(_x), sizeof(_x));
Dave Rodgman2d28c462023-07-28 18:22:56 +010084
Manuel Pégourié-Gonnard73afa372020-08-19 10:27:38 +020085#elif defined(MBEDTLS_TEST_CONSTANT_FLOW_VALGRIND)
86#include <valgrind/memcheck.h>
87
88#define TEST_CF_SECRET VALGRIND_MAKE_MEM_UNDEFINED
89// VALGRIND_MAKE_MEM_UNDEFINED(_qzz_addr, _qzz_len)
90#define TEST_CF_PUBLIC VALGRIND_MAKE_MEM_DEFINED
91// VALGRIND_MAKE_MEM_DEFINED(_qzz_addr, _qzz_len)
92
Dave Rodgman2d28c462023-07-28 18:22:56 +010093#define TEST_CF_SAVE_SECRET(_x)
94#define TEST_CF_RESTORE_SECRET(_x)
95
Manuel Pégourié-Gonnard73afa372020-08-19 10:27:38 +020096#else /* MBEDTLS_TEST_CONSTANT_FLOW_MEMSAN ||
97 MBEDTLS_TEST_CONSTANT_FLOW_VALGRIND */
Manuel Pégourié-Gonnard6240def2020-07-10 09:35:54 +020098
99#define TEST_CF_SECRET(ptr, size)
100#define TEST_CF_PUBLIC(ptr, size)
101
Dave Rodgman2d28c462023-07-28 18:22:56 +0100102#define TEST_CF_SAVE_SECRET(_x)
103#define TEST_CF_RESTORE_SECRET(_x)
104
Manuel Pégourié-Gonnard73afa372020-08-19 10:27:38 +0200105#endif /* MBEDTLS_TEST_CONSTANT_FLOW_MEMSAN ||
106 MBEDTLS_TEST_CONSTANT_FLOW_VALGRIND */
Manuel Pégourié-Gonnard6240def2020-07-10 09:35:54 +0200107
108#endif /* TEST_CONSTANT_FLOW_H */