Andres Amaya Garcia | f2d1792 | 2017-10-24 22:47:14 +0100 | [diff] [blame] | 1 | # test_zeroize.gdb |
| 2 | # |
Bence Szépkúti | a2947ac | 2020-08-19 16:37:36 +0200 | [diff] [blame] | 3 | # Copyright The Mbed TLS Contributors |
Bence Szépkúti | f744bd7 | 2020-06-05 13:02:18 +0200 | [diff] [blame] | 4 | # SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later |
| 5 | # |
| 6 | # This file is provided under the Apache License 2.0, or the |
| 7 | # GNU General Public License v2.0 or later. |
| 8 | # |
| 9 | # ********** |
| 10 | # Apache License 2.0: |
Bence Szépkúti | 51b41d5 | 2020-05-26 01:54:15 +0200 | [diff] [blame] | 11 | # |
| 12 | # Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 13 | # not use this file except in compliance with the License. |
| 14 | # You may obtain a copy of the License at |
| 15 | # |
| 16 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 17 | # |
| 18 | # Unless required by applicable law or agreed to in writing, software |
| 19 | # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 20 | # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 21 | # See the License for the specific language governing permissions and |
| 22 | # limitations under the License. |
| 23 | # |
Bence Szépkúti | f744bd7 | 2020-06-05 13:02:18 +0200 | [diff] [blame] | 24 | # ********** |
| 25 | # |
| 26 | # ********** |
| 27 | # GNU General Public License v2.0 or later: |
| 28 | # |
| 29 | # This program is free software; you can redistribute it and/or modify |
| 30 | # it under the terms of the GNU General Public License as published by |
| 31 | # the Free Software Foundation; either version 2 of the License, or |
| 32 | # (at your option) any later version. |
| 33 | # |
| 34 | # This program is distributed in the hope that it will be useful, |
| 35 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 36 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 37 | # GNU General Public License for more details. |
| 38 | # |
| 39 | # You should have received a copy of the GNU General Public License along |
| 40 | # with this program; if not, write to the Free Software Foundation, Inc., |
| 41 | # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 42 | # |
| 43 | # ********** |
| 44 | # |
Andres Amaya Garcia | f2d1792 | 2017-10-24 22:47:14 +0100 | [diff] [blame] | 45 | # Purpose |
| 46 | # |
Andres Amaya Garcia | eecea0e | 2018-04-17 10:14:53 -0500 | [diff] [blame] | 47 | # Run a test using the debugger to check that the mbedtls_platform_zeroize() |
| 48 | # function in platform_util.h is not being optimized out by the compiler. To do |
| 49 | # so, the script loads the test program at programs/test/zeroize.c and sets a |
| 50 | # breakpoint at the last return statement in main(). When the breakpoint is |
| 51 | # hit, the debugger manually checks the contents to be zeroized and checks that |
| 52 | # it is actually cleared. |
Andres Amaya Garcia | f2d1792 | 2017-10-24 22:47:14 +0100 | [diff] [blame] | 53 | # |
Andres Amaya Garcia | eecea0e | 2018-04-17 10:14:53 -0500 | [diff] [blame] | 54 | # The mbedtls_platform_zeroize() test is debugger driven because there does not |
| 55 | # 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] | 56 | # eliminated by compiler optimizations from within the compiled program. The |
| 57 | # problem is that a compiler would typically remove what it considers to be |
Antonin Décimo | d5f4759 | 2019-01-23 15:24:37 +0100 | [diff] [blame] | 58 | # "unnecessary" assignments as part of redundant code elimination. To identify |
Andres Amaya Garcia | 42defd1 | 2018-03-08 21:21:40 +0000 | [diff] [blame] | 59 | # such code, the compilar will create some form dependency graph between |
| 60 | # reads and writes to variables (among other situations). It will then use this |
| 61 | # 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] | 62 | # 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] | 63 | # intelligent compiler could determine that this function clears a block of |
| 64 | # 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] | 65 | # mbedtls_platform_zeroize() does not have an observable behavior. However, |
Andres Amaya Garcia | 708c5cb | 2018-04-24 08:33:31 -0500 | [diff] [blame] | 66 | # inserting a test after a call to mbedtls_platform_zeroize() to check whether |
| 67 | # the block of memory was correctly zeroed would force the compiler to not |
| 68 | # eliminate the mbedtls_platform_zeroize() call. If this does not occur, then |
| 69 | # the compiler potentially has a bug. |
Andres Amaya Garcia | 42defd1 | 2018-03-08 21:21:40 +0000 | [diff] [blame] | 70 | # |
Andres Amaya Garcia | f2d1792 | 2017-10-24 22:47:14 +0100 | [diff] [blame] | 71 | # Note: This test requires that the test program is compiled with -g3. |
| 72 | |
Andres Amaya Garcia | ddebc49 | 2017-10-24 22:16:34 +0100 | [diff] [blame] | 73 | set confirm off |
Gilles Peskine | 427df37 | 2018-09-27 11:50:24 +0200 | [diff] [blame] | 74 | |
Andres Amaya Garcia | ddebc49 | 2017-10-24 22:16:34 +0100 | [diff] [blame] | 75 | file ./programs/test/zeroize |
Bence Szépkúti | cd6fd06 | 2020-06-09 12:52:04 +0200 | [diff] [blame] | 76 | |
| 77 | search GDB_BREAK_HERE |
| 78 | break $_ |
Andres Amaya Garcia | ddebc49 | 2017-10-24 22:16:34 +0100 | [diff] [blame] | 79 | |
| 80 | set args ./programs/test/zeroize.c |
| 81 | run |
| 82 | |
| 83 | set $i = 0 |
| 84 | set $len = sizeof(buf) |
| 85 | set $buf = buf |
| 86 | |
Andres Amaya Garcia | ddebc49 | 2017-10-24 22:16:34 +0100 | [diff] [blame] | 87 | while $i < $len |
| 88 | if $buf[$i++] != 0 |
| 89 | echo The buffer at was not zeroized\n |
| 90 | quit 1 |
| 91 | end |
| 92 | end |
| 93 | |
| 94 | echo The buffer was correctly zeroized\n |
Andres Amaya Garcia | 806f403 | 2017-11-01 10:03:36 +0000 | [diff] [blame] | 95 | |
| 96 | continue |
| 97 | |
| 98 | if $_exitcode != 0 |
| 99 | echo The program did not terminate correctly\n |
| 100 | quit 1 |
| 101 | end |
| 102 | |
Andres Amaya Garcia | ddebc49 | 2017-10-24 22:16:34 +0100 | [diff] [blame] | 103 | quit 0 |