summaryrefslogtreecommitdiffstats
path: root/pkgs/os-specific/linux/opensnitch-ebpf/default.nix
blob: 42f214f9ea137d8578efcc6ec06320ca8a7dc6ea (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
{
  lib,
  kernel,
  stdenv,
  clang-tools,
  llvmPackages,
  elfutils,
  flex,
  bison,
  bc,
  opensnitch,
  nixosTests,
  fetchpatch2,
}:

stdenv.mkDerivation rec {
  pname = "opensnitch_ebpf";
  version = "${opensnitch.version}-${kernel.version}";

  inherit (opensnitch) src;

  patches = [
    (fetchpatch2 {
      # fixes build failures on kernel >= 6.19 (#490127)
      # remove when added to a release
      name = "fix-kernel-6.19-build.patch";
      stripLen = 1;
      url = "https://github.com/evilsocket/opensnitch/commit/614537c92ec82f54f76a45fb406ad2fb6e6fa618.patch?full_index=1";
      hash = "sha256-FCJfDhgmnm1GXPDaxr+YpVWTRrwBvjVzvGdZSFB6SqQ=";
    })
  ];

  sourceRoot = "${src.name}/ebpf_prog";

  nativeBuildInputs = with llvmPackages; [
    bc
    bison
    clang
    clang-tools
    elfutils
    flex
    libllvm
  ];

  env.NIX_CFLAGS_COMPILE =
    # We set -fno-stack-protector here to work around a clang regression.
    # This is fine - bpf programs do not use stack protectors
    # https://aur.archlinux.org/cgit/aur.git/tree/PKGBUILD?h=opensnitch-ebpf-module&id=984b952a784eb701f691dd9f2d45dfeb8d15053b
    "-fno-stack-protector"
    # clang doesn't set this, so we have to
    # only relevant after 7.2 due to changes in https://github.com/torvalds/linux/commit/aef4dfa790b22d8052cfb78044eadbe03c876c39
    + lib.optionalString (lib.versionAtLeast kernel.version "7.2") " -DCC_USING_FENTRY";

  env.KERNEL_VER = kernel.modDirVersion;
  env.KERNEL_DIR = "${kernel.dev}/lib/modules/${kernel.modDirVersion}/source";
  env.KERNEL_HEADERS = "${kernel.dev}/lib/modules/${kernel.modDirVersion}/build";

  extraConfig = ''
    CONFIG_UPROBE_EVENTS=y
  '';

  installPhase = ''
    runHook preInstall

    for file in opensnitch*.o; do
      install -Dm644 "$file" "$out/etc/opensnitchd/$file"
    done

    runHook postInstall
  '';

  postFixup = ''
    # reduces closure size significantly (fixes https://github.com/NixOS/nixpkgs/issues/391351)
    for file in $out/etc/opensnitchd/*.o; do
      llvm-strip --strip-debug $file
    done
  '';

  passthru.tests = {
    inherit (nixosTests) opensnitch;
  };

  meta = {
    description = "eBPF process monitor module for OpenSnitch";
    homepage = "https://github.com/evilsocket/opensnitch";
    license = lib.licenses.gpl3Only;
    maintainers = with lib.maintainers; [
      onny
      grimmauld
    ];
    platforms = lib.platforms.linux;
  };
}