blob: 1769e09d07b51bf9f9eb1503a6899a6e0ffdd4d9 [file] [log] [blame]
Andres Amaya Garciaf2d17922017-10-24 22:47:14 +01001# test_zeroize.gdb
2#
Andres Amaya Garcia757cd722018-03-08 21:25:25 +00003# Copyright (c) 2018, Arm Limited, All Rights Reserved
Bence Szépkútif744bd72020-06-05 13:02:18 +02004# 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úti51b41d52020-05-26 01:54:15 +020011#
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útif744bd72020-06-05 13:02:18 +020024# **********
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#
Bence Szépkúti51b41d52020-05-26 01:54:15 +020045# This file is part of Mbed TLS (https://tls.mbed.org)
Andres Amaya Garciaf2d17922017-10-24 22:47:14 +010046#
47# Purpose
48#
Andres Amaya Garciaeecea0e2018-04-17 10:14:53 -050049# Run a test using the debugger to check that the mbedtls_platform_zeroize()
50# function in platform_util.h is not being optimized out by the compiler. To do
51# so, the script loads the test program at programs/test/zeroize.c and sets a
52# breakpoint at the last return statement in main(). When the breakpoint is
53# hit, the debugger manually checks the contents to be zeroized and checks that
54# it is actually cleared.
Andres Amaya Garciaf2d17922017-10-24 22:47:14 +010055#
Andres Amaya Garciaeecea0e2018-04-17 10:14:53 -050056# The mbedtls_platform_zeroize() test is debugger driven because there does not
57# seem to be a mechanism to reliably check whether the zeroize calls are being
Andres Amaya Garcia42defd12018-03-08 21:21:40 +000058# eliminated by compiler optimizations from within the compiled program. The
59# problem is that a compiler would typically remove what it considers to be
Antonin Décimod5f47592019-01-23 15:24:37 +010060# "unnecessary" assignments as part of redundant code elimination. To identify
Andres Amaya Garcia42defd12018-03-08 21:21:40 +000061# such code, the compilar will create some form dependency graph between
62# reads and writes to variables (among other situations). It will then use this
63# data structure to remove redundant code that does not have an impact on the
Andres Amaya Garciaeecea0e2018-04-17 10:14:53 -050064# program's observable behavior. In the case of mbedtls_platform_zeroize(), an
Andres Amaya Garcia42defd12018-03-08 21:21:40 +000065# intelligent compiler could determine that this function clears a block of
66# memory that is not accessed later in the program, so removing the call to
Andres Amaya Garciaeecea0e2018-04-17 10:14:53 -050067# mbedtls_platform_zeroize() does not have an observable behavior. However,
Andres Amaya Garcia708c5cb2018-04-24 08:33:31 -050068# inserting a test after a call to mbedtls_platform_zeroize() to check whether
69# the block of memory was correctly zeroed would force the compiler to not
70# eliminate the mbedtls_platform_zeroize() call. If this does not occur, then
71# the compiler potentially has a bug.
Andres Amaya Garcia42defd12018-03-08 21:21:40 +000072#
Andres Amaya Garciaf2d17922017-10-24 22:47:14 +010073# Note: This test requires that the test program is compiled with -g3.
74
Andres Amaya Garciaddebc492017-10-24 22:16:34 +010075set confirm off
Gilles Peskine427df372018-09-27 11:50:24 +020076
Andres Amaya Garciaddebc492017-10-24 22:16:34 +010077file ./programs/test/zeroize
Bence Szépkúticd6fd062020-06-09 12:52:04 +020078
79search GDB_BREAK_HERE
80break $_
Andres Amaya Garciaddebc492017-10-24 22:16:34 +010081
82set args ./programs/test/zeroize.c
83run
84
85set $i = 0
86set $len = sizeof(buf)
87set $buf = buf
88
Andres Amaya Garciaddebc492017-10-24 22:16:34 +010089while $i < $len
90 if $buf[$i++] != 0
91 echo The buffer at was not zeroized\n
92 quit 1
93 end
94end
95
96echo The buffer was correctly zeroized\n
Andres Amaya Garcia806f4032017-11-01 10:03:36 +000097
98continue
99
100if $_exitcode != 0
101 echo The program did not terminate correctly\n
102 quit 1
103end
104
Andres Amaya Garciaddebc492017-10-24 22:16:34 +0100105quit 0