Skip to content

Commit 9a25ad0

Browse files
committed
Add BPF hardening
1 parent a6c617b commit 9a25ad0

File tree

3 files changed

+77
-0
lines changed

3 files changed

+77
-0
lines changed

settings/kernel/default.nix

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ let
4141
./sysrq.nix
4242
./tcp-timestamps.nix
4343
./restrict-line-disciplines.nix
44+
./harden-bpf.nix
45+
./restrict-bpf.nix
4446
]
4547
{
4648
inherit

settings/kernel/harden-bpf.nix

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# This file is part of nix-mineral (https://github.com/cynicsketch/nix-mineral/).
2+
# Copyright (c) 2025 cynicsketch
3+
#
4+
# This program is free software: you can redistribute it and/or modify
5+
# it under the terms of the GNU General Public License as published by
6+
# the Free Software Foundation, either version 3 of the License, or
7+
# (at your option) any later version.
8+
#
9+
# This program is distributed in the hope that it will be useful,
10+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
# GNU General Public License for more details.
13+
#
14+
# You should have received a copy of the GNU General Public License
15+
# along with this program. If not, see <http://www.gnu.org/licenses/>.
16+
17+
{
18+
l,
19+
cfg,
20+
...
21+
}:
22+
23+
{
24+
options = {
25+
harden-bpf = l.mkBoolOption ''
26+
Harden eBPF against JIT spraying attacks, to reduce the risk of abuse
27+
because eBPF allows executing potentially dangerous code in the kernel.
28+
29+
See https://en.wikipedia.org/wiki/EBPF#Security for more info.
30+
'' true;
31+
};
32+
33+
config = l.mkIf cfg {
34+
boot.kernel.sysctl = {
35+
"net.core.bpf_jit_harden" = l.mkDefault "2";
36+
};
37+
};
38+
}

settings/kernel/restrict-bpf.nix

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# This file is part of nix-mineral (https://github.com/cynicsketch/nix-mineral/).
2+
# Copyright (c) 2025 cynicsketch
3+
#
4+
# This program is free software: you can redistribute it and/or modify
5+
# it under the terms of the GNU General Public License as published by
6+
# the Free Software Foundation, either version 3 of the License, or
7+
# (at your option) any later version.
8+
#
9+
# This program is distributed in the hope that it will be useful,
10+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
# GNU General Public License for more details.
13+
#
14+
# You should have received a copy of the GNU General Public License
15+
# along with this program. If not, see <http://www.gnu.org/licenses/>.
16+
17+
{
18+
l,
19+
cfg,
20+
...
21+
}:
22+
23+
{
24+
options = {
25+
restrict-bpf = l.mkBoolOption ''
26+
Restrict eBPF to CAP_BPF in order to prevent abuse by unprivileged users.
27+
28+
See https://en.wikipedia.org/wiki/EBPF#Security for more info.
29+
'' true;
30+
};
31+
32+
config = l.mkIf cfg {
33+
boot.kernel.sysctl = {
34+
"kernel.unprivileged_bpf_disabled" = l.mkDefault "1";
35+
};
36+
};
37+
}

0 commit comments

Comments
 (0)