Skip to content

Commit 5d33f41

Browse files
ebpf: fixed dns uprobes
We were not deleting DNS entries from the hash map, so when it reached the maximum capacity (12k entries), we couldn't allocate new entries, resulting in events not being sent to userspace. (cherry picked from commit 1518cb3)
1 parent 1ae20c3 commit 5d33f41

File tree

1 file changed

+31
-5
lines changed

1 file changed

+31
-5
lines changed

ebpf_prog/opensnitch-dns.c

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,24 @@
1-
#define KBUILD_MODNAME "dummy"
1+
/* Copyright (C) 2022 calesanz
2+
// 2023-2024 Gustavo Iñiguez Goya
3+
//
4+
// This file is part of OpenSnitch.
5+
//
6+
// OpenSnitch is free software: you can redistribute it and/or modify
7+
// it under the terms of the GNU General Public License as published by
8+
// the Free Software Foundation, either version 3 of the License, or
9+
// (at your option) any later version.
10+
//
11+
// OpenSnitch is distributed in the hope that it will be useful,
12+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
// GNU General Public License for more details.
15+
//
16+
// You should have received a copy of the GNU General Public License
17+
// along with OpenSnitch. If not, see <http://www.gnu.org/licenses/>.
18+
*/
19+
20+
21+
#define KBUILD_MODNAME "opensnitch-dns"
222

323
#include <linux/in.h>
424
#include <linux/in6.h>
@@ -51,7 +71,7 @@ struct addrinfo_args_cache {
5171
// define temporary array for data
5272
struct bpf_map_def SEC("maps/addrinfo_args_hash") addrinfo_args_hash = {
5373
.type = BPF_MAP_TYPE_HASH,
54-
.max_entries = MAPSIZE,
74+
.max_entries = 256, // max entries at any time
5575
.key_size = sizeof(u32),
5676
.value_size = sizeof(struct addrinfo_args_cache),
5777
};
@@ -61,7 +81,7 @@ struct bpf_map_def SEC("maps/events") events = {
6181
.type = BPF_MAP_TYPE_PERF_EVENT_ARRAY,
6282
.key_size = sizeof(u32),
6383
.value_size = sizeof(u32),
64-
.max_entries = MAPSIZE,
84+
.max_entries = 256, // max cpus
6585
};
6686

6787
/**
@@ -170,7 +190,7 @@ int ret_addrinfo(struct pt_regs *ctx) {
170190
struct addrinfo *res;
171191
bpf_probe_read(&res, sizeof(res), res_p);
172192
if (res == NULL) {
173-
return 0;
193+
goto out;
174194
}
175195
bpf_probe_read(&data.addr_type, sizeof(data.addr_type),
176196
&res->ai_family);
@@ -186,7 +206,7 @@ int ret_addrinfo(struct pt_regs *ctx) {
186206

187207
bpf_probe_read_user(&data.ip, sizeof(data.ip), &ipv6->sin6_addr);
188208
} else {
189-
return 1;
209+
goto out;
190210
}
191211

192212
bpf_probe_read_kernel_str(&data.host, sizeof(data.host),
@@ -198,9 +218,15 @@ int ret_addrinfo(struct pt_regs *ctx) {
198218

199219
struct addrinfo * next;
200220
bpf_probe_read(&next, sizeof(next), &res->ai_next);
221+
if (next == NULL){
222+
goto out;
223+
}
201224
res_p = &next;
202225
}
203226

227+
out:
228+
bpf_map_delete_elem(&addrinfo_args_hash, &tid);
229+
204230
return 0;
205231
}
206232

0 commit comments

Comments
 (0)