recursion.pl 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. #!/usr/bin/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. use warnings;
  10. use strict;
  11. use utf8;
  12. use open qw(:std utf8);
  13. # exclude functions that are ok:
  14. # - mpi_write_hlp: bounded by size of mbedtls_mpi, a compile-time constant
  15. # - x509_crt_verify_child: bounded by MBEDTLS_X509_MAX_INTERMEDIATE_CA
  16. my $known_ok = qr/mpi_write_hlp|x509_crt_verify_child/;
  17. my $cur_name;
  18. my $inside;
  19. my @funcs;
  20. die "Usage: $0 file.c [...]\n" unless @ARGV;
  21. while (<>)
  22. {
  23. if( /^[^\/#{}\s]/ && ! /\[.*]/ ) {
  24. chomp( $cur_name = $_ ) unless $inside;
  25. } elsif( /^{/ && $cur_name ) {
  26. $inside = 1;
  27. $cur_name =~ s/.* ([^ ]*)\(.*/$1/;
  28. } elsif( /^}/ && $inside ) {
  29. undef $inside;
  30. undef $cur_name;
  31. } elsif( $inside && /\b\Q$cur_name\E\([^)]/ ) {
  32. push @funcs, $cur_name unless /$known_ok/;
  33. }
  34. }
  35. print "$_\n" for @funcs;
  36. exit @funcs;