systemdにはIPAddressAllow, IPAddressDenyというディレクティブがあり、利用するとeBPFを利用した簡易なファイアウォールが設定されます。
- iptables/nftablesとは無関係に動き、他とまとめて操作・確認するような方法はありません。
- systemctl statusなどで関係する設定を一覧するような機能はなく、各サービスのsystemdのunit fileを読むのが一番はやくて便利です。
- 設定ではなく動いているものがみたいときはbpftoolで対応するmapを見ることはできるが、実装内部の話になりお世辞にも読みやすいとは言えない。
個人的な感想としては、特定IPアドレスだけとしか通信しないことが確定しているサービスのパッケージ作成時くらいしか出番はなさそう。
systemdでの設定
[Service] IPAddressDeny=any IPAddressAllow=192.168.1.0/24 IPAddressAllow=10.0.0.0/8
chronyd.service に設定して再起動したログ例
1月 09 16:27:42 turtle audit[1]: SERVICE_STOP pid=1 uid=0 auid=4294967295 ses=4294967295 subj=system_u:system_r:init_t:s0 msg='unit=chronyd comm="systemd" exe="/usr/lib/systemd/systemd" hostname=? addr=? terminal=? res=success' 1月 09 16:27:42 turtle systemd[1]: chronyd.service: Consumed 4min 15.225s CPU time. 1月 09 16:27:42 turtle audit: BPF prog-id=485 op=LOAD <<=== BPF の操作はaudit logに記録 1月 09 16:27:42 turtle audit: BPF prog-id=486 op=LOAD 1月 09 16:27:42 turtle audit: BPF prog-id=487 op=LOAD 1月 09 16:27:42 turtle audit: BPF prog-id=391 op=UNLOAD <<== 不要になったBPFはunloadする 1月 09 16:27:42 turtle audit: BPF prog-id=390 op=UNLOAD 1月 09 16:27:42 turtle audit: BPF prog-id=389 op=UNLOAD 1月 09 16:27:42 turtle systemd[1]: Starting chronyd.service - NTP client/server... 1月 09 16:27:42 turtle chronyd[3890870]: chronyd version 4.6.1 starting (+CMDMON +NTP +REFCLOCK +RTC +PRIVDROP +SCFILTER +SIGND +ASYNCDNS +NTS +SECHASH +IPV6 +DEBUG) 1月 09 16:27:42 turtle chronyd[3890870]: Using right/UTC timezone to obtain leap second data 1月 09 16:27:42 turtle chronyd[3890870]: Frequency -20.746 +/- 0.733 ppm read from /var/lib/chrony/drift 1月 09 16:27:42 turtle chronyd[3890870]: Loaded seccomp filter (level 2) 1月 09 16:27:42 turtle chronyd[3890870]: Added source 132.163.97.4 <<=== 設定から解決しているのでsourceの設定はおこなうが…… 1月 09 16:27:42 turtle systemd[1]: Started chronyd.service - NTP client/server. 1月 09 16:27:42 turtle audit[1]: SERVICE_START pid=1 uid=0 auid=4294967295 ses=4294967295 subj=system_u:system_r:init_t:s0 msg='unit=chronyd comm="systemd" exe="/usr/lib/systemd/systemd" hostname=? addr=? terminal=? res=success' ... 1月 09 16:27:59 turtle chronyd[3890870]: Can't synchronise: no selectable sources <<== 通信できなくて同期しない
unitとbpfプログラムの対応関係
bpftool cgroup tree でcgroup←→bpf prog がわかるので、どのunitとの対応するかがわかる
# bpftool cgroup tree
CgroupPath
ID AttachType AttachFlags Name
...
/sys/fs/cgroup/system.slice/chronyd.service
487 cgroup_inet_ingress multi sd_fw_ingress
486 cgroup_inet_egress multi sd_fw_egress
485 cgroup_device multi sd_devices
...
bpftool での詳細出力
bpftool prog, bpftool map で詳細が出力できるがこれで調査をおこなうにはbpfまわりの事前知識が必要:
# bpftool prog
...
485: cgroup_device name sd_devices tag ccbbf91f3c6979c7 gpl
loaded_at 2025-01-09T16:27:42+0900 uid 0
xlated 560B jited 364B memlock 4096B
pids systemd(1)
486: cgroup_skb name sd_fw_egress tag df35b3b526089f21 gpl
loaded_at 2025-01-09T16:27:42+0900 uid 0
xlated 184B jited 143B memlock 4096B map_ids 45
pids systemd(1)
487: cgroup_skb name sd_fw_ingress tag 208d1bf35e7113d2 gpl
loaded_at 2025-01-09T16:27:42+0900 uid 0
xlated 184B jited 143B memlock 4096B map_ids 45
pids systemd(1)
# bpftool prog dump xlated id 487
0: (bf) r6 = r1
1: (69) r7 = *(u16 *)(r6 +180)
2: (b4) w8 = 0
3: (55) if r7 != 0x8 goto pc+14
4: (bf) r1 = r6
5: (b4) w2 = 12
6: (bf) r3 = r10
7: (07) r3 += -4
8: (b4) w4 = 4
9: (85) call bpf_skb_load_bytes#12499728
10: (18) r1 = map[id:45]
12: (bf) r2 = r10
13: (07) r2 += -8
14: (62) *(u32 *)(r2 +0) = 32
15: (85) call trie_lookup_elem#338112
16: (15) if r0 == 0x0 goto pc+1
17: (44) w8 |= 1
18: (44) w8 |= 2
19: (b7) r0 = 1
20: (55) if r8 != 0x2 goto pc+1
21: (b7) r0 = 0
22: (95) exit
# bpftool map dump id 45
key: 08 00 00 00 0a 00 00 00 value: 01 00 00 00 00 00 00 00
key: 18 00 00 00 c0 a8 01 00 value: 01 00 00 00 00 00 00 00
Found 2 elements
プログラムのソース
プログラムの内容は systemd/src/core/bpf-firewall.c で定義 以下ブロックをipv4/v6, allow/denyの組み合わせで最大4回繰り返す
/* Compare IPv4 with one word instruction (32-bit) */
struct bpf_insn insn[] = {
/* If skb->protocol != ETH_P_IP, skip this whole block. The offset will be set later. */
BPF_JMP_IMM(BPF_JNE, BPF_REG_7, htobe16(protocol), 0),
/*
* Call into BPF_FUNC_skb_load_bytes to load the dst/src IP address
*
* R1: Pointer to the skb
* R2: Data offset
* R3: Destination buffer on the stack (r10 - 4)
* R4: Number of bytes to read (4)
*/
BPF_MOV64_REG(BPF_REG_1, BPF_REG_6),
BPF_MOV32_IMM(BPF_REG_2, addr_offset),
BPF_MOV64_REG(BPF_REG_3, BPF_REG_10),
BPF_ALU64_IMM(BPF_ADD, BPF_REG_3, -addr_size),
BPF_MOV32_IMM(BPF_REG_4, addr_size),
BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0, BPF_FUNC_skb_load_bytes),
/*
* Call into BPF_FUNC_map_lookup_elem to see if the address matches any entry in the
* LPM trie map. For this to work, the prefixlen field of 'struct bpf_lpm_trie_key'
* has to be set to the maximum possible value.
*
* On success, the looked up value is stored in R0. For this application, the actual
* value doesn't matter, however; we just set the bit in @verdict in R8 if we found any
* matching value.
*/
BPF_LD_MAP_FD(BPF_REG_1, map_fd),
BPF_MOV64_REG(BPF_REG_2, BPF_REG_10),
BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -addr_size - sizeof(uint32_t)),
BPF_ST_MEM(BPF_W, BPF_REG_2, 0, addr_size * 8),
BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0, BPF_FUNC_map_lookup_elem),
BPF_JMP_IMM(BPF_JEQ, BPF_REG_0, 0, 1),
BPF_ALU32_IMM(BPF_OR, BPF_REG_8, verdict),
};
/* Jump label fixup */
insn[0].off = ELEMENTSOF(insn) - 1;