recursion.pl 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. #!/usr/bin/env perl
  2. # Find functions making recursive calls to themselves.
  3. # (Multiple recursion where a() calls b() which calls a() not covered.)
  4. #
  5. # When the recursion depth might depend on data controlled by the attacker in
  6. # an unbounded way, those functions should use interation instead.
  7. #
  8. # Typical usage: scripts/recursion.pl library/*.c
  9. #
  10. # Copyright The Mbed TLS Contributors
  11. # SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
  12. #
  13. # This file is provided under the Apache License 2.0, or the
  14. # GNU General Public License v2.0 or later.
  15. #
  16. # **********
  17. # Apache License 2.0:
  18. #
  19. # Licensed under the Apache License, Version 2.0 (the "License"); you may
  20. # not use this file except in compliance with the License.
  21. # You may obtain a copy of the License at
  22. #
  23. # http://www.apache.org/licenses/LICENSE-2.0
  24. #
  25. # Unless required by applicable law or agreed to in writing, software
  26. # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  27. # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  28. # See the License for the specific language governing permissions and
  29. # limitations under the License.
  30. #
  31. # **********
  32. #
  33. # **********
  34. # GNU General Public License v2.0 or later:
  35. #
  36. # This program is free software; you can redistribute it and/or modify
  37. # it under the terms of the GNU General Public License as published by
  38. # the Free Software Foundation; either version 2 of the License, or
  39. # (at your option) any later version.
  40. #
  41. # This program is distributed in the hope that it will be useful,
  42. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  43. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  44. # GNU General Public License for more details.
  45. #
  46. # You should have received a copy of the GNU General Public License along
  47. # with this program; if not, write to the Free Software Foundation, Inc.,
  48. # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  49. #
  50. # **********
  51. use warnings;
  52. use strict;
  53. use utf8;
  54. use open qw(:std utf8);
  55. # exclude functions that are ok:
  56. # - mpi_write_hlp: bounded by size of mbedtls_mpi, a compile-time constant
  57. # - x509_crt_verify_child: bounded by MBEDTLS_X509_MAX_INTERMEDIATE_CA
  58. my $known_ok = qr/mpi_write_hlp|x509_crt_verify_child/;
  59. my $cur_name;
  60. my $inside;
  61. my @funcs;
  62. die "Usage: $0 file.c [...]\n" unless @ARGV;
  63. while (<>)
  64. {
  65. if( /^[^\/#{}\s]/ && ! /\[.*]/ ) {
  66. chomp( $cur_name = $_ ) unless $inside;
  67. } elsif( /^{/ && $cur_name ) {
  68. $inside = 1;
  69. $cur_name =~ s/.* ([^ ]*)\(.*/$1/;
  70. } elsif( /^}/ && $inside ) {
  71. undef $inside;
  72. undef $cur_name;
  73. } elsif( $inside && /\b\Q$cur_name\E\([^)]/ ) {
  74. push @funcs, $cur_name unless /$known_ok/;
  75. }
  76. }
  77. print "$_\n" for @funcs;
  78. exit @funcs;