Skip to content

Commit cebc409

Browse files
YutaroHayakawabrb
authored andcommitted
Avoid printing garvage skb written by previous writer
After we consume all of the print_skb_map slot, we start to reuse the slot. On the reused buffer, there's a previous skb string and we don't clear that. So, when the new skb string is shorter than the previous one, we print the garvage string. Keep the length of the string in the map and truncate unnecessary part from the output to avoid it. Signed-off-by: Yutaro Hayakawa <[email protected]>
1 parent a591840 commit cebc409

File tree

2 files changed

+27
-8
lines changed

2 files changed

+27
-8
lines changed

bpf/kprobe_pwru.c

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -100,11 +100,15 @@ struct {
100100
} print_stack_map SEC(".maps");
101101

102102
#ifdef OUTPUT_SKB
103+
struct print_skb_value {
104+
u32 len;
105+
char str[PRINT_SKB_STR_SIZE];
106+
};
103107
struct {
104108
__uint(type, BPF_MAP_TYPE_ARRAY);
105109
__uint(max_entries, 256);
106110
__type(key, u32);
107-
__type(value, char[PRINT_SKB_STR_SIZE]);
111+
__type(value, struct print_skb_value);
108112
} print_skb_map SEC(".maps");
109113
#endif
110114

@@ -230,22 +234,26 @@ static __always_inline void
230234
set_skb_btf(struct sk_buff *skb, typeof(print_skb_id) *event_id) {
231235
#ifdef OUTPUT_SKB
232236
static struct btf_ptr p = {};
237+
struct print_skb_value *v;
233238
typeof(print_skb_id) id;
234-
char *str;
239+
long n;
235240

236241
p.type_id = bpf_core_type_id_kernel(struct sk_buff);
237242
p.ptr = skb;
238243
id = __sync_fetch_and_add(&print_skb_id, 1) % 256;
239244

240-
str = bpf_map_lookup_elem(&print_skb_map, (u32 *) &id);
241-
if (!str) {
245+
v = bpf_map_lookup_elem(&print_skb_map, (u32 *) &id);
246+
if (!v) {
242247
return;
243248
}
244249

245-
if (bpf_snprintf_btf(str, PRINT_SKB_STR_SIZE, &p, sizeof(p), 0) < 0) {
250+
n = bpf_snprintf_btf(v->str, PRINT_SKB_STR_SIZE, &p, sizeof(p), 0);
251+
if (n < 0) {
246252
return;
247253
}
248254

255+
v->len = n;
256+
249257
*event_id = id;
250258
#endif
251259
}

internal/pwru/output.go

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
package pwru
66

77
import (
8+
"encoding/binary"
89
"encoding/json"
910
"errors"
1011
"fmt"
@@ -247,10 +248,20 @@ func getStackData(event *Event, o *output) (stackData string) {
247248

248249
func getSkbData(event *Event, o *output) (skbData string) {
249250
id := uint32(event.PrintSkbId)
250-
if str, err := o.printSkbMap.LookupBytes(&id); err == nil {
251-
skbData = string(str)
251+
252+
b, err := o.printSkbMap.LookupBytes(&id)
253+
if err != nil {
254+
return ""
255+
}
256+
257+
length := binary.NativeEndian.Uint32(b[:4])
258+
259+
// Bounds check
260+
if int(length+4) > len(b) {
261+
return ""
252262
}
253-
return skbData
263+
264+
return "\n" + string(b[4:4+length])
254265
}
255266

256267
func getMetaData(event *Event, o *output) (metaData string) {

0 commit comments

Comments
 (0)