blob: a7536c40642c455ad1e97251d121cf99b8c583bb [file] [log] [blame]
Andres Amaya Garciaf2d17922017-10-24 22:47:14 +01001# test_zeroize.gdb
2#
Bence Szépkútia2947ac2020-08-19 16:37:36 +02003# Copyright The Mbed TLS Contributors
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#
Andres Amaya Garciaf2d17922017-10-24 22:47:14 +010045# Purpose
46#
Andres Amaya Garciaeecea0e2018-04-17 10:14:53 -050047# 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 Garciaf2d17922017-10-24 22:47:14 +010053#
Andres Amaya Garciaeecea0e2018-04-17 10:14:53 -050054# 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 Garcia42defd12018-03-08 21:21:40 +000056# 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écimod5f47592019-01-23 15:24:37 +010058# "unnecessary" assignments as part of redundant code elimination. To identify
Andres Amaya Garcia42defd12018-03-08 21:21:40 +000059# 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 Garciaeecea0e2018-04-17 10:14:53 -050062# program's observable behavior. In the case of mbedtls_platform_zeroize(), an
Andres Amaya Garcia42defd12018-03-08 21:21:40 +000063# 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 Garciaeecea0e2018-04-17 10:14:53 -050065# mbedtls_platform_zeroize() does not have an observable behavior. However,
Andres Amaya Garcia708c5cb2018-04-24 08:33:31 -050066# 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 Garcia42defd12018-03-08 21:21:40 +000070#
Andres Amaya Garciaf2d17922017-10-24 22:47:14 +010071# Note: This test requires that the test program is compiled with -g3.
72
Andres Amaya Garciaddebc492017-10-24 22:16:34 +010073set confirm off
Gilles Peskine427df372018-09-27 11:50:24 +020074
Andres Amaya Garciaddebc492017-10-24 22:16:34 +010075file ./programs/test/zeroize
Bence Szépkúticd6fd062020-06-09 12:52:04 +020076
77search GDB_BREAK_HERE
78break $_
Andres Amaya Garciaddebc492017-10-24 22:16:34 +010079
80set args ./programs/test/zeroize.c
81run
82
83set $i = 0
84set $len = sizeof(buf)
85set $buf = buf
86
Andres Amaya Garciaddebc492017-10-24 22:16:34 +010087while $i < $len
88 if $buf[$i++] != 0
89 echo The buffer at was not zeroized\n
90 quit 1
91 end
92end
93
94echo The buffer was correctly zeroized\n
Andres Amaya Garcia806f4032017-11-01 10:03:36 +000095
96continue
97
98if $_exitcode != 0
99 echo The program did not terminate correctly\n
100 quit 1
101end
102
Andres Amaya Garciaddebc492017-10-24 22:16:34 +0100103quit 0