| fbrosson | 533407a | 2018-04-04 21:44:29 +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 | 
| Shaun Case | 0e7791f | 2021-12-20 21:14:10 -0800 | [diff] [blame] | 7 | # an unbounded way, those functions should use iteration instead. | 
| Manuel Pégourié-Gonnard | fd60a5c | 2014-11-12 22:54:24 +0100 | [diff] [blame] | 8 | # | 
|  | 9 | # Typical usage: scripts/recursion.pl library/*.c | 
| Bence Szépkúti | 700ee44 | 2020-05-26 00:33:31 +0200 | [diff] [blame] | 10 | # | 
| Bence Szépkúti | 1e14827 | 2020-08-07 13:07:28 +0200 | [diff] [blame] | 11 | # Copyright The Mbed TLS Contributors | 
| Bence Szépkúti | c7da1fe | 2020-05-26 01:54:15 +0200 | [diff] [blame] | 12 | # SPDX-License-Identifier: Apache-2.0 | 
|  | 13 | # | 
|  | 14 | # Licensed under the Apache License, Version 2.0 (the "License"); you may | 
|  | 15 | # not use this file except in compliance with the License. | 
|  | 16 | # You may obtain a copy of the License at | 
|  | 17 | # | 
|  | 18 | # http://www.apache.org/licenses/LICENSE-2.0 | 
|  | 19 | # | 
|  | 20 | # Unless required by applicable law or agreed to in writing, software | 
|  | 21 | # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | 
|  | 22 | # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 
|  | 23 | # See the License for the specific language governing permissions and | 
|  | 24 | # limitations under the License. | 
| Manuel Pégourié-Gonnard | fd60a5c | 2014-11-12 22:54:24 +0100 | [diff] [blame] | 25 |  | 
|  | 26 | use warnings; | 
|  | 27 | use strict; | 
|  | 28 |  | 
|  | 29 | use utf8; | 
|  | 30 | use open qw(:std utf8); | 
|  | 31 |  | 
|  | 32 | # exclude functions that are ok: | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 33 | # - mpi_write_hlp: bounded by size of mbedtls_mpi, a compile-time constant | 
|  | 34 | # - 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] | 35 | 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] | 36 |  | 
|  | 37 | my $cur_name; | 
|  | 38 | my $inside; | 
|  | 39 | my @funcs; | 
|  | 40 |  | 
|  | 41 | die "Usage: $0 file.c [...]\n" unless @ARGV; | 
|  | 42 |  | 
|  | 43 | while (<>) | 
|  | 44 | { | 
|  | 45 | if( /^[^\/#{}\s]/ && ! /\[.*]/ ) { | 
|  | 46 | chomp( $cur_name = $_ ) unless $inside; | 
|  | 47 | } elsif( /^{/ && $cur_name ) { | 
|  | 48 | $inside = 1; | 
|  | 49 | $cur_name =~ s/.* ([^ ]*)\(.*/$1/; | 
|  | 50 | } elsif( /^}/ && $inside ) { | 
|  | 51 | undef $inside; | 
|  | 52 | undef $cur_name; | 
|  | 53 | } elsif( $inside && /\b\Q$cur_name\E\([^)]/ ) { | 
|  | 54 | push @funcs, $cur_name unless /$known_ok/; | 
|  | 55 | } | 
|  | 56 | } | 
|  | 57 |  | 
|  | 58 | print "$_\n" for @funcs; | 
|  | 59 | exit @funcs; |