Skip to content

Commit 9dd2f28

Browse files
fix: address comments
1 parent 60e1272 commit 9dd2f28

File tree

2 files changed

+19
-16
lines changed

2 files changed

+19
-16
lines changed

bpf-prog/block-iptables/bpf/src/block_iptables.bpf.c

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include <bpf/bpf_helpers.h>
88
#include <bpf/bpf_core_read.h>
99
#include <bpf/bpf_tracing.h>
10+
#include <stdbool.h>
1011

1112
#define sk_family __sk_common.skc_family
1213
#define EPERM 1
@@ -34,11 +35,11 @@ struct {
3435
__type(key, u32);
3536
__type(value, u64);
3637
__uint(pinning, LIBBPF_PIN_BY_NAME);
37-
} event_counter SEC(".maps");
38+
} iptables_block_event_counter SEC(".maps");
3839

3940
// This function checks if the parent process of the current task is allowed to install iptables rules.
4041
// It checks the parent's command name against a predefined list of allowed prefixes.
41-
int is_allowed_parent ()
42+
bool is_allowed_parent ()
4243
{
4344
struct task_struct *task = (struct task_struct *)bpf_get_current_task();
4445
struct task_struct *parent_task = NULL;
@@ -80,7 +81,7 @@ int is_allowed_parent ()
8081
// check if the current task is in the host network namespace
8182
// This function compares the inode number of the current network namespace with the host's network namespace inode
8283
// The host's network namespace inode is initialized by userspace when the BPF program is loaded.
83-
int is_host_ns() {
84+
bool is_host_ns() {
8485
struct task_struct *task = (struct task_struct *)bpf_get_current_task();
8586
struct nsproxy *nsproxy;
8687
struct net *net_ns;
@@ -109,12 +110,12 @@ void increment_event_counter() {
109110
u32 key = 0;
110111
u64 *value;
111112

112-
value = bpf_map_lookup_elem(&event_counter, &key);
113+
value = bpf_map_lookup_elem(&iptables_block_event_counter, &key);
113114
if (value) {
114115
__sync_fetch_and_add(value, 1);
115116
} else {
116117
u64 initial_value = 1;
117-
bpf_map_update_elem(&event_counter, &key, &initial_value, BPF_ANY);
118+
bpf_map_update_elem(&iptables_block_event_counter, &key, &initial_value, BPF_ANY);
118119
}
119120
}
120121

@@ -144,18 +145,18 @@ int BPF_PROG(iptables_legacy_block, struct socket *sock, int level, int optname)
144145
// blocking hook for iptables-nftables rule installation
145146
SEC("lsm/netlink_send")
146147
int BPF_PROG(iptables_nftables_block, struct sock *sk, struct sk_buff *skb) {
147-
__u16 family = 0, proto = 0;
148-
if (sk != NULL) {
149-
bpf_probe_read_kernel(&family, sizeof(family), &sk->sk_family);
148+
if (sk == NULL || skb == NULL) {
149+
return 0;
150150
}
151+
__u16 family = 0, proto = 0;
152+
bpf_probe_read_kernel(&family, sizeof(family), &sk->sk_family);
151153

152154
// Check if the socket family is AF_NETLINK (just a sanity check)
153155
if (family != AF_NETLINK)
154156
return 0;
155157

156-
if (sk != NULL) {
157-
bpf_probe_read_kernel(&proto, sizeof(proto), &sk->sk_protocol);
158-
}
158+
bpf_probe_read_kernel(&proto, sizeof(proto), &sk->sk_protocol);
159+
159160

160161
// Check if the protocol is NETLINK_NETFILTER
161162
// This is the protocol used for netfilter messages

bpf-prog/block-iptables/pkg/bpfprogram/program.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@ const (
1919
// BPFMapPinPath is the directory where BPF maps are pinned
2020
BPFMapPinPath = "/sys/fs/bpf/block-iptables"
2121
// EventCounterMapName is the name used for pinning the event counter map
22-
EventCounterMapName = "event_counter"
22+
EventCounterMapName = "iptables_block_event_counter"
23+
// NetNSPath is the path to the host network namespace
24+
NetNSPath = "/proc/self/ns/net"
2325
)
2426

2527
var ErrEventCounterMapNotLoaded = errors.New("event counter map not loaded")
@@ -47,13 +49,13 @@ func (p *Program) CreatePinPath() error {
4749

4850
// pinEventCounterMap pins the event counter map to the filesystem
4951
func (p *Program) pinEventCounterMap() error {
50-
if p.objs == nil || p.objs.EventCounter == nil {
52+
if p.objs == nil || p.objs.IptablesBlockEventCounter == nil {
5153
return ErrEventCounterMapNotLoaded
5254
}
5355

5456
pinPath := filepath.Join(BPFMapPinPath, EventCounterMapName)
5557

56-
if err := p.objs.EventCounter.Pin(pinPath); err != nil {
58+
if err := p.objs.IptablesBlockEventCounter.Pin(pinPath); err != nil {
5759
return errors.Wrapf(err, "failed to pin event counter map to %s", pinPath)
5860
}
5961

@@ -75,9 +77,9 @@ func (p *Program) unpinEventCounterMap() error {
7577

7678
func getHostNetnsInode() (uint64, error) {
7779
var stat syscall.Stat_t
78-
err := syscall.Stat("/proc/self/ns/net", &stat)
80+
err := syscall.Stat(NetNSPath, &stat)
7981
if err != nil {
80-
return 0, errors.Wrap(err, "failed to stat /proc/self/ns/net")
82+
return 0, errors.Wrapf(err, "failed to stat %s", NetNSPath)
8183
}
8284

8385
log.Printf("Host network namespace inode: %d", stat.Ino)

0 commit comments

Comments
 (0)