mkseccomp.pl 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. #!/usr/bin/perl
  2. #
  3. # A simple helper script to help people build seccomp profiles for
  4. # Docker/LXC. The goal is mostly to reduce the attack surface to the
  5. # kernel, by restricting access to rarely used, recently added or not used
  6. # syscalls.
  7. #
  8. # This script processes one or more files which contain the list of system
  9. # calls to be allowed. See mkseccomp.sample for more information how you
  10. # can configure the list of syscalls. When run, this script produces output
  11. # which, when stored in a file, can be passed to docker as follows:
  12. #
  13. # docker run --lxc-conf="lxc.seccomp=$file" <rest of arguments>
  14. #
  15. # The included sample file shows how to cut about a quarter of all syscalls,
  16. # which affecting most applications.
  17. #
  18. # For specific situations it is possible to reduce the list further. By
  19. # reducing the list to just those syscalls required by a certain application
  20. # you can make it difficult for unknown/unexpected code to run.
  21. #
  22. # Run this script as follows:
  23. #
  24. # ./mkseccomp.pl < mkseccomp.sample >syscalls.list
  25. # or
  26. # ./mkseccomp.pl mkseccomp.sample >syscalls.list
  27. #
  28. # Multiple files can be specified, in which case the lists of syscalls are
  29. # combined.
  30. #
  31. # By Martijn van Oosterhout <kleptog@svana.org> Nov 2013
  32. # How it works:
  33. #
  34. # This program basically spawns two processes to form a chain like:
  35. #
  36. # <process data section to prefix __NR_> | cpp | <add header and filter unknown syscalls>
  37. use strict;
  38. use warnings;
  39. if( -t ) {
  40. print STDERR "Helper script to make seccomp filters for Docker/LXC.\n";
  41. print STDERR "Usage: mkseccomp.pl < [files...]\n";
  42. exit 1;
  43. }
  44. my $pid = open(my $in, "-|") // die "Couldn't fork1 ($!)\n";
  45. if($pid == 0) { # Child
  46. $pid = open(my $out, "|-") // die "Couldn't fork2 ($!)\n";
  47. if($pid == 0) { # Child, which execs cpp
  48. exec "cpp" or die "Couldn't exec cpp ($!)\n";
  49. exit 1;
  50. }
  51. # Process the DATA section and output to cpp
  52. print $out "#include <sys/syscall.h>\n";
  53. while(<>) {
  54. if(/^\w/) {
  55. print $out "__NR_$_";
  56. }
  57. }
  58. close $out;
  59. exit 0;
  60. }
  61. # Print header and then process output from cpp.
  62. print "1\n";
  63. print "whitelist\n";
  64. while(<$in>) {
  65. print if( /^[0-9]/ );
  66. }