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 | # This file is part of Mbed TLS (https://tls.mbed.org) |
Andres Amaya Garcia | f2d1792 | 2017-10-24 22:47:14 +0100 | [diff] [blame] | 4 | # |
Andres Amaya Garcia | 757cd72 | 2018-03-08 21:25:25 +0000 | [diff] [blame] | 5 | # Copyright (c) 2018, Arm Limited, All Rights Reserved |
Andres Amaya Garcia | f2d1792 | 2017-10-24 22:47:14 +0100 | [diff] [blame] | 6 | # |
| 7 | # Purpose |
| 8 | # |
| 9 | # Run a test using the debugger to check that the mbedtls_zeroize() function in |
| 10 | # utils.h is not being optimized out by the compiler. To do so, the script |
| 11 | # loads the test program at programs/test/zeroize.c and sets a breakpoint at |
| 12 | # the last return statement in the main(). When the breakpoint is hit, the |
| 13 | # debugger manually checks the contents to be zeroized and checks that it is |
| 14 | # actually cleared. |
| 15 | # |
Andres Amaya Garcia | 42defd1 | 2018-03-08 21:21:40 +0000 | [diff] [blame] | 16 | # The mbedtls_zeroize() test is debugger driven because there does not seem to |
| 17 | # be a mechanism to reliably check whether the zeroize calls are being |
| 18 | # eliminated by compiler optimizations from within the compiled program. The |
| 19 | # problem is that a compiler would typically remove what it considers to be |
| 20 | # "unecessary" assignments as part of redundant code elimination. To identify |
| 21 | # such code, the compilar will create some form dependency graph between |
| 22 | # reads and writes to variables (among other situations). It will then use this |
| 23 | # data structure to remove redundant code that does not have an impact on the |
| 24 | # program's observable behavior. In the case of mbedtls_zeroize(), an |
| 25 | # intelligent compiler could determine that this function clears a block of |
| 26 | # memory that is not accessed later in the program, so removing the call to |
| 27 | # mbedtls_zeroize() does not have an observable behavior. However, inserting a |
| 28 | # test after a call to mbedtls_zeroize() to check whether the block of |
| 29 | # memory was correctly zeroed would force the compiler to not eliminate the |
| 30 | # mbedtls_zeroize() call. If this does not occur, then the compiler potentially |
| 31 | # has a bug. |
| 32 | # |
Andres Amaya Garcia | f2d1792 | 2017-10-24 22:47:14 +0100 | [diff] [blame] | 33 | # Note: This test requires that the test program is compiled with -g3. |
Andres Amaya Garcia | 42defd1 | 2018-03-08 21:21:40 +0000 | [diff] [blame] | 34 | # |
| 35 | # WARNING: There does not seem to be a mechanism in GDB scripts to set a |
| 36 | # breakpoint at the end of a function (probably because there are a lot of |
| 37 | # complications as function can have multiple exit points, etc). Therefore, it |
| 38 | # was necessary to hard-code the line number of the breakpoint in the zeroize.c |
| 39 | # test app. The assumption is that zeroize.c is a simple test app that does not |
| 40 | # change often (as opposed to the actual library code), so the breakpoint line |
| 41 | # number does not need to be updated often. |
Andres Amaya Garcia | f2d1792 | 2017-10-24 22:47:14 +0100 | [diff] [blame] | 42 | |
Andres Amaya Garcia | ddebc49 | 2017-10-24 22:16:34 +0100 | [diff] [blame] | 43 | set confirm off |
| 44 | file ./programs/test/zeroize |
Andres Amaya Garcia | 42defd1 | 2018-03-08 21:21:40 +0000 | [diff] [blame] | 45 | break zeroize.c:99 |
Andres Amaya Garcia | ddebc49 | 2017-10-24 22:16:34 +0100 | [diff] [blame] | 46 | |
| 47 | set args ./programs/test/zeroize.c |
| 48 | run |
| 49 | |
| 50 | set $i = 0 |
| 51 | set $len = sizeof(buf) |
| 52 | set $buf = buf |
| 53 | |
Andres Amaya Garcia | ddebc49 | 2017-10-24 22:16:34 +0100 | [diff] [blame] | 54 | while $i < $len |
| 55 | if $buf[$i++] != 0 |
| 56 | echo The buffer at was not zeroized\n |
| 57 | quit 1 |
| 58 | end |
| 59 | end |
| 60 | |
| 61 | echo The buffer was correctly zeroized\n |
Andres Amaya Garcia | 806f403 | 2017-11-01 10:03:36 +0000 | [diff] [blame] | 62 | |
| 63 | continue |
| 64 | |
| 65 | if $_exitcode != 0 |
| 66 | echo The program did not terminate correctly\n |
| 67 | quit 1 |
| 68 | end |
| 69 | |
Andres Amaya Garcia | ddebc49 | 2017-10-24 22:16:34 +0100 | [diff] [blame] | 70 | quit 0 |