blob: d35f918b9bf5d1c18ea880a88f342368076d0b82 [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úti4e9f7122020-06-05 13:02:18 +020012# 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úti09b4f192020-05-26 01:54:15 +020019#
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útib7246ad2020-05-26 00:33:31 +020031#
Bence Szépkúti4e9f7122020-06-05 13:02:18 +020032# **********
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útib7246ad2020-05-26 00:33:31 +020053# This file is part of Mbed TLS (https://tls.mbed.org)
Manuel Pégourié-Gonnardfd60a5c2014-11-12 22:54:24 +010054
55use warnings;
56use strict;
57
58use utf8;
59use open qw(:std utf8);
60
61# exclude functions that are ok:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020062# - 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é-Gonnard10c44d72014-11-20 17:30:37 +010064my $known_ok = qr/mpi_write_hlp|x509_crt_verify_child/;
Manuel Pégourié-Gonnardfd60a5c2014-11-12 22:54:24 +010065
66my $cur_name;
67my $inside;
68my @funcs;
69
70die "Usage: $0 file.c [...]\n" unless @ARGV;
71
72while (<>)
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
87print "$_\n" for @funcs;
88exit @funcs;