build.rs 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. // -*- coding: utf-8 -*-
  2. //
  3. // Copyright (C) 2024 Michael Büsch <m@bues.ch>
  4. //
  5. // Licensed under the Apache License version 2.0
  6. // or the MIT license, at your option.
  7. // SPDX-License-Identifier: Apache-2.0 OR MIT
  8. #![forbid(unsafe_code)]
  9. use build_target::{target_arch, Arch};
  10. use letmein_seccomp::{Action, Allow, Filter};
  11. use std::{env, fs::OpenOptions, io::Write, path::Path};
  12. const SECCOMP_ALLOW_LIST: [Allow; 10] = [
  13. Allow::Mmap,
  14. Allow::Mprotect,
  15. Allow::Read,
  16. Allow::Write,
  17. Allow::Recv,
  18. Allow::Send,
  19. Allow::TcpAccept,
  20. Allow::UnixConnect,
  21. Allow::Signal,
  22. Allow::Futex,
  23. ];
  24. fn seccomp_compile_action(arch: &Arch, action: Action) {
  25. let filter =
  26. if let Ok(filter) = Filter::compile_for_arch(&SECCOMP_ALLOW_LIST, action, arch.as_str()) {
  27. filter.serialize()
  28. } else {
  29. vec![]
  30. };
  31. let suffix = match action {
  32. Action::Kill => "kill",
  33. Action::Log => "log",
  34. };
  35. let filter_file = format!("seccomp_filter_{suffix}.bpf");
  36. let out_dir = env::var("OUT_DIR").expect("OUT_DIR is not set");
  37. OpenOptions::new()
  38. .create(true)
  39. .truncate(true)
  40. .write(true)
  41. .open(Path::new(&out_dir).join(filter_file))
  42. .expect("Failed to open filter.bpf")
  43. .write_all(&filter)
  44. .expect("Failed to write filter.bpf");
  45. }
  46. fn seccomp_compile(arch: &Arch) {
  47. seccomp_compile_action(arch, Action::Kill);
  48. seccomp_compile_action(arch, Action::Log);
  49. }
  50. fn main() {
  51. let arch = target_arch().expect("Failed to get build target architecture");
  52. seccomp_compile(&arch);
  53. }
  54. // vim: ts=4 sw=4 expandtab