blob: 3cdeff7f432b92255f0077d1e3817976e0d99b0c [file] [log] [blame]
fbrosson533407a2018-04-04 21:44:29 +00001#!/usr/bin/env perl
Manuel Pégourié-Gonnardfd60a5c2014-11-12 22:54:24 +01002
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 Case8b0ecbc2021-12-20 21:14:10 -08007# an unbounded way, those functions should use iteration instead.
Manuel Pégourié-Gonnardfd60a5c2014-11-12 22:54:24 +01008#
9# Typical usage: scripts/recursion.pl library/*.c
Bence Szépkúti700ee442020-05-26 00:33:31 +020010#
Bence Szépkúti1e148272020-08-07 13:07:28 +020011# Copyright The Mbed TLS Contributors
Dave Rodgman16799db2023-11-02 19:47:20 +000012# SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
Manuel Pégourié-Gonnardfd60a5c2014-11-12 22:54:24 +010013
14use warnings;
15use strict;
16
17use utf8;
18use open qw(:std utf8);
19
20# exclude functions that are ok:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020021# - mpi_write_hlp: bounded by size of mbedtls_mpi, a compile-time constant
22# - x509_crt_verify_child: bounded by MBEDTLS_X509_MAX_INTERMEDIATE_CA
Manuel Pégourié-Gonnard10c44d72014-11-20 17:30:37 +010023my $known_ok = qr/mpi_write_hlp|x509_crt_verify_child/;
Manuel Pégourié-Gonnardfd60a5c2014-11-12 22:54:24 +010024
25my $cur_name;
26my $inside;
27my @funcs;
28
29die "Usage: $0 file.c [...]\n" unless @ARGV;
30
31while (<>)
32{
33 if( /^[^\/#{}\s]/ && ! /\[.*]/ ) {
34 chomp( $cur_name = $_ ) unless $inside;
35 } elsif( /^{/ && $cur_name ) {
36 $inside = 1;
37 $cur_name =~ s/.* ([^ ]*)\(.*/$1/;
38 } elsif( /^}/ && $inside ) {
39 undef $inside;
40 undef $cur_name;
41 } elsif( $inside && /\b\Q$cur_name\E\([^)]/ ) {
42 push @funcs, $cur_name unless /$known_ok/;
43 }
44}
45
46print "$_\n" for @funcs;
47exit @funcs;