Andres Amaya Garcia | f2d1792 | 2017-10-24 22:47:14 +0100 | [diff] [blame] | 1 | # test_zeroize.gdb |
| 2 | # |
Andres Amaya Garcia | 757cd72 | 2018-03-08 21:25:25 +0000 | [diff] [blame] | 3 | # Copyright (c) 2018, Arm Limited, All Rights Reserved |
Bence Szépkúti | c7da1fe | 2020-05-26 01:54:15 +0200 | [diff] [blame] | 4 | # SPDX-License-Identifier: Apache-2.0 |
| 5 | # |
| 6 | # Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 7 | # not use this file except in compliance with the License. |
| 8 | # You may obtain a copy of the License at |
| 9 | # |
| 10 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | # |
| 12 | # Unless required by applicable law or agreed to in writing, software |
| 13 | # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 14 | # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | # See the License for the specific language governing permissions and |
| 16 | # limitations under the License. |
| 17 | # |
| 18 | # This file is part of Mbed TLS (https://tls.mbed.org) |
Andres Amaya Garcia | f2d1792 | 2017-10-24 22:47:14 +0100 | [diff] [blame] | 19 | # |
| 20 | # Purpose |
| 21 | # |
Andres Amaya Garcia | eecea0e | 2018-04-17 10:14:53 -0500 | [diff] [blame] | 22 | # Run a test using the debugger to check that the mbedtls_platform_zeroize() |
| 23 | # function in platform_util.h is not being optimized out by the compiler. To do |
| 24 | # so, the script loads the test program at programs/test/zeroize.c and sets a |
| 25 | # breakpoint at the last return statement in main(). When the breakpoint is |
| 26 | # hit, the debugger manually checks the contents to be zeroized and checks that |
| 27 | # it is actually cleared. |
Andres Amaya Garcia | f2d1792 | 2017-10-24 22:47:14 +0100 | [diff] [blame] | 28 | # |
Andres Amaya Garcia | eecea0e | 2018-04-17 10:14:53 -0500 | [diff] [blame] | 29 | # The mbedtls_platform_zeroize() test is debugger driven because there does not |
| 30 | # seem to be a mechanism to reliably check whether the zeroize calls are being |
Andres Amaya Garcia | 42defd1 | 2018-03-08 21:21:40 +0000 | [diff] [blame] | 31 | # eliminated by compiler optimizations from within the compiled program. The |
| 32 | # problem is that a compiler would typically remove what it considers to be |
Antonin Décimo | 36e89b5 | 2019-01-23 15:24:37 +0100 | [diff] [blame] | 33 | # "unnecessary" assignments as part of redundant code elimination. To identify |
Andres Amaya Garcia | 42defd1 | 2018-03-08 21:21:40 +0000 | [diff] [blame] | 34 | # such code, the compilar will create some form dependency graph between |
| 35 | # reads and writes to variables (among other situations). It will then use this |
| 36 | # data structure to remove redundant code that does not have an impact on the |
Andres Amaya Garcia | eecea0e | 2018-04-17 10:14:53 -0500 | [diff] [blame] | 37 | # program's observable behavior. In the case of mbedtls_platform_zeroize(), an |
Andres Amaya Garcia | 42defd1 | 2018-03-08 21:21:40 +0000 | [diff] [blame] | 38 | # intelligent compiler could determine that this function clears a block of |
| 39 | # memory that is not accessed later in the program, so removing the call to |
Andres Amaya Garcia | eecea0e | 2018-04-17 10:14:53 -0500 | [diff] [blame] | 40 | # mbedtls_platform_zeroize() does not have an observable behavior. However, |
Andres Amaya Garcia | 708c5cb | 2018-04-24 08:33:31 -0500 | [diff] [blame] | 41 | # inserting a test after a call to mbedtls_platform_zeroize() to check whether |
| 42 | # the block of memory was correctly zeroed would force the compiler to not |
| 43 | # eliminate the mbedtls_platform_zeroize() call. If this does not occur, then |
| 44 | # the compiler potentially has a bug. |
Andres Amaya Garcia | 42defd1 | 2018-03-08 21:21:40 +0000 | [diff] [blame] | 45 | # |
Andres Amaya Garcia | f2d1792 | 2017-10-24 22:47:14 +0100 | [diff] [blame] | 46 | # Note: This test requires that the test program is compiled with -g3. |
| 47 | |
Andres Amaya Garcia | ddebc49 | 2017-10-24 22:16:34 +0100 | [diff] [blame] | 48 | set confirm off |
Gilles Peskine | 427df37 | 2018-09-27 11:50:24 +0200 | [diff] [blame] | 49 | |
Andres Amaya Garcia | ddebc49 | 2017-10-24 22:16:34 +0100 | [diff] [blame] | 50 | file ./programs/test/zeroize |
Bence Szépkúti | 5620d71 | 2020-06-09 12:52:04 +0200 | [diff] [blame] | 51 | |
| 52 | search GDB_BREAK_HERE |
| 53 | break $_ |
Andres Amaya Garcia | ddebc49 | 2017-10-24 22:16:34 +0100 | [diff] [blame] | 54 | |
| 55 | set args ./programs/test/zeroize.c |
| 56 | run |
| 57 | |
| 58 | set $i = 0 |
| 59 | set $len = sizeof(buf) |
| 60 | set $buf = buf |
| 61 | |
Andres Amaya Garcia | ddebc49 | 2017-10-24 22:16:34 +0100 | [diff] [blame] | 62 | while $i < $len |
| 63 | if $buf[$i++] != 0 |
| 64 | echo The buffer at was not zeroized\n |
| 65 | quit 1 |
| 66 | end |
| 67 | end |
| 68 | |
| 69 | echo The buffer was correctly zeroized\n |
Andres Amaya Garcia | 806f403 | 2017-11-01 10:03:36 +0000 | [diff] [blame] | 70 | |
| 71 | continue |
| 72 | |
| 73 | if $_exitcode != 0 |
| 74 | echo The program did not terminate correctly\n |
| 75 | quit 1 |
| 76 | end |
| 77 | |
Andres Amaya Garcia | ddebc49 | 2017-10-24 22:16:34 +0100 | [diff] [blame] | 78 | quit 0 |