recursion.pl 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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
  12. #
  13. # Licensed under the Apache License, Version 2.0 (the "License"); you may
  14. # not use this file except in compliance with the License.
  15. # You may obtain a copy of the License at
  16. #
  17. # http://www.apache.org/licenses/LICENSE-2.0
  18. #
  19. # Unless required by applicable law or agreed to in writing, software
  20. # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  21. # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  22. # See the License for the specific language governing permissions and
  23. # limitations under the License.
  24. use warnings;
  25. use strict;
  26. use utf8;
  27. use open qw(:std utf8);
  28. # exclude functions that are ok:
  29. # - mpi_write_hlp: bounded by size of mbedtls_mpi, a compile-time constant
  30. # - x509_crt_verify_child: bounded by MBEDTLS_X509_MAX_INTERMEDIATE_CA
  31. my $known_ok = qr/mpi_write_hlp|x509_crt_verify_child/;
  32. my $cur_name;
  33. my $inside;
  34. my @funcs;
  35. die "Usage: $0 file.c [...]\n" unless @ARGV;
  36. while (<>)
  37. {
  38. if( /^[^\/#{}\s]/ && ! /\[.*]/ ) {
  39. chomp( $cur_name = $_ ) unless $inside;
  40. } elsif( /^{/ && $cur_name ) {
  41. $inside = 1;
  42. $cur_name =~ s/.* ([^ ]*)\(.*/$1/;
  43. } elsif( /^}/ && $inside ) {
  44. undef $inside;
  45. undef $cur_name;
  46. } elsif( $inside && /\b\Q$cur_name\E\([^)]/ ) {
  47. push @funcs, $cur_name unless /$known_ok/;
  48. }
  49. }
  50. print "$_\n" for @funcs;
  51. exit @funcs;