fbrosson | 3a74571 | 2018-04-04 22:26:56 +0000 | [diff] [blame] | 1 | #!/usr/bin/env perl |
Manuel Pégourié-Gonnard | fd60a5c | 2014-11-12 22:54:24 +0100 | [diff] [blame] | 2 | |
| 3 | # Find functions making recursive calls to themselves. |
| 4 | # (Multiple recursion where a() calls b() which calls a() not covered.) |
| 5 | # |
| 6 | # When the recursion depth might depend on data controlled by the attacker in |
| 7 | # an unbounded way, those functions should use interation instead. |
| 8 | # |
| 9 | # Typical usage: scripts/recursion.pl library/*.c |
Bence Szépkúti | b7246ad | 2020-05-26 00:33:31 +0200 | [diff] [blame] | 10 | # |
| 11 | # Copyright (C) 2014-2015, Arm Limited, All Rights Reserved |
Bence Szépkúti | 4e9f712 | 2020-06-05 13:02:18 +0200 | [diff] [blame] | 12 | # SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later |
| 13 | # |
| 14 | # This file is provided under the Apache License 2.0, or the |
| 15 | # GNU General Public License v2.0 or later. |
| 16 | # |
| 17 | # ********** |
| 18 | # Apache License 2.0: |
Bence Szépkúti | 09b4f19 | 2020-05-26 01:54:15 +0200 | [diff] [blame] | 19 | # |
| 20 | # Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 21 | # not use this file except in compliance with the License. |
| 22 | # You may obtain a copy of the License at |
| 23 | # |
| 24 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 25 | # |
| 26 | # Unless required by applicable law or agreed to in writing, software |
| 27 | # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 28 | # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 29 | # See the License for the specific language governing permissions and |
| 30 | # limitations under the License. |
Bence Szépkúti | b7246ad | 2020-05-26 00:33:31 +0200 | [diff] [blame] | 31 | # |
Bence Szépkúti | 4e9f712 | 2020-06-05 13:02:18 +0200 | [diff] [blame] | 32 | # ********** |
| 33 | # |
| 34 | # ********** |
| 35 | # GNU General Public License v2.0 or later: |
| 36 | # |
| 37 | # This program is free software; you can redistribute it and/or modify |
| 38 | # it under the terms of the GNU General Public License as published by |
| 39 | # the Free Software Foundation; either version 2 of the License, or |
| 40 | # (at your option) any later version. |
| 41 | # |
| 42 | # This program is distributed in the hope that it will be useful, |
| 43 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 44 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 45 | # GNU General Public License for more details. |
| 46 | # |
| 47 | # You should have received a copy of the GNU General Public License along |
| 48 | # with this program; if not, write to the Free Software Foundation, Inc., |
| 49 | # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 50 | # |
| 51 | # ********** |
| 52 | # |
Bence Szépkúti | b7246ad | 2020-05-26 00:33:31 +0200 | [diff] [blame] | 53 | # This file is part of Mbed TLS (https://tls.mbed.org) |
Manuel Pégourié-Gonnard | fd60a5c | 2014-11-12 22:54:24 +0100 | [diff] [blame] | 54 | |
| 55 | use warnings; |
| 56 | use strict; |
| 57 | |
| 58 | use utf8; |
| 59 | use open qw(:std utf8); |
| 60 | |
| 61 | # exclude functions that are ok: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 62 | # - mpi_write_hlp: bounded by size of mbedtls_mpi, a compile-time constant |
| 63 | # - x509_crt_verify_child: bounded by MBEDTLS_X509_MAX_INTERMEDIATE_CA |
Manuel Pégourié-Gonnard | 10c44d7 | 2014-11-20 17:30:37 +0100 | [diff] [blame] | 64 | my $known_ok = qr/mpi_write_hlp|x509_crt_verify_child/; |
Manuel Pégourié-Gonnard | fd60a5c | 2014-11-12 22:54:24 +0100 | [diff] [blame] | 65 | |
| 66 | my $cur_name; |
| 67 | my $inside; |
| 68 | my @funcs; |
| 69 | |
| 70 | die "Usage: $0 file.c [...]\n" unless @ARGV; |
| 71 | |
| 72 | while (<>) |
| 73 | { |
| 74 | if( /^[^\/#{}\s]/ && ! /\[.*]/ ) { |
| 75 | chomp( $cur_name = $_ ) unless $inside; |
| 76 | } elsif( /^{/ && $cur_name ) { |
| 77 | $inside = 1; |
| 78 | $cur_name =~ s/.* ([^ ]*)\(.*/$1/; |
| 79 | } elsif( /^}/ && $inside ) { |
| 80 | undef $inside; |
| 81 | undef $cur_name; |
| 82 | } elsif( $inside && /\b\Q$cur_name\E\([^)]/ ) { |
| 83 | push @funcs, $cur_name unless /$known_ok/; |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | print "$_\n" for @funcs; |
| 88 | exit @funcs; |