blob: c80666ec80f7aee2b7b6fa2d1066acfa09739e94 [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
7# an unbounded way, those functions should use interation instead.
8#
9# Typical usage: scripts/recursion.pl library/*.c
Bence Szépkúti700ee442020-05-26 00:33:31 +020010#
11# Copyright (C) 2014-2015, Arm Limited, All Rights Reserved
12#
13# This file is part of Mbed TLS (https://tls.mbed.org)
Manuel Pégourié-Gonnardfd60a5c2014-11-12 22:54:24 +010014
15use warnings;
16use strict;
17
18use utf8;
19use open qw(:std utf8);
20
21# exclude functions that are ok:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020022# - mpi_write_hlp: bounded by size of mbedtls_mpi, a compile-time constant
23# - x509_crt_verify_child: bounded by MBEDTLS_X509_MAX_INTERMEDIATE_CA
Manuel Pégourié-Gonnard10c44d72014-11-20 17:30:37 +010024my $known_ok = qr/mpi_write_hlp|x509_crt_verify_child/;
Manuel Pégourié-Gonnardfd60a5c2014-11-12 22:54:24 +010025
26my $cur_name;
27my $inside;
28my @funcs;
29
30die "Usage: $0 file.c [...]\n" unless @ARGV;
31
32while (<>)
33{
34 if( /^[^\/#{}\s]/ && ! /\[.*]/ ) {
35 chomp( $cur_name = $_ ) unless $inside;
36 } elsif( /^{/ && $cur_name ) {
37 $inside = 1;
38 $cur_name =~ s/.* ([^ ]*)\(.*/$1/;
39 } elsif( /^}/ && $inside ) {
40 undef $inside;
41 undef $cur_name;
42 } elsif( $inside && /\b\Q$cur_name\E\([^)]/ ) {
43 push @funcs, $cur_name unless /$known_ok/;
44 }
45}
46
47print "$_\n" for @funcs;
48exit @funcs;