blob: 693703132106da5b9c00a1eda57605f26a141bad [file] [log] [blame]
fbrosson3a745712018-04-04 22:26:56 +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útib7246ad2020-05-26 00:33:31 +020010#
11# Copyright (C) 2014-2015, Arm Limited, All Rights Reserved
Bence Szépkúti09b4f192020-05-26 01:54:15 +020012# 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.
Bence Szépkútib7246ad2020-05-26 00:33:31 +020025#
26# This file is part of Mbed TLS (https://tls.mbed.org)
Manuel Pégourié-Gonnardfd60a5c2014-11-12 22:54:24 +010027
28use warnings;
29use strict;
30
31use utf8;
32use open qw(:std utf8);
33
34# exclude functions that are ok:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020035# - mpi_write_hlp: bounded by size of mbedtls_mpi, a compile-time constant
36# - x509_crt_verify_child: bounded by MBEDTLS_X509_MAX_INTERMEDIATE_CA
Manuel Pégourié-Gonnard10c44d72014-11-20 17:30:37 +010037my $known_ok = qr/mpi_write_hlp|x509_crt_verify_child/;
Manuel Pégourié-Gonnardfd60a5c2014-11-12 22:54:24 +010038
39my $cur_name;
40my $inside;
41my @funcs;
42
43die "Usage: $0 file.c [...]\n" unless @ARGV;
44
45while (<>)
46{
47 if( /^[^\/#{}\s]/ && ! /\[.*]/ ) {
48 chomp( $cur_name = $_ ) unless $inside;
49 } elsif( /^{/ && $cur_name ) {
50 $inside = 1;
51 $cur_name =~ s/.* ([^ ]*)\(.*/$1/;
52 } elsif( /^}/ && $inside ) {
53 undef $inside;
54 undef $cur_name;
55 } elsif( $inside && /\b\Q$cur_name\E\([^)]/ ) {
56 push @funcs, $cur_name unless /$known_ok/;
57 }
58}
59
60print "$_\n" for @funcs;
61exit @funcs;