Skip to content

Commit 1f40cfa

Browse files
tetragon: handle resolving null pointer
Instead of following a null pointer, note that one was found so that the selector does not determine a match against unrelated data in memory. Signed-off-by: Andy Strohman <astrohma@isovalent.com>
1 parent f89a4f1 commit 1f40cfa

File tree

3 files changed

+19
-4
lines changed

3 files changed

+19
-4
lines changed

bpf/lib/generic.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ struct msg_generic_kprobe {
8080
#ifndef __V61_BPF_PROG
8181
struct generic_path path;
8282
#endif
83+
bool resolve_null_ptr[MAX_POSSIBLE_ARGS];
8384
};
8485

8586
FUNC_INLINE size_t generic_kprobe_common_size(void)

bpf/process/generic_calls.h

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -391,14 +391,20 @@ extract_arg_depth(u32 i, struct extract_arg_data *data)
391391
{
392392
if (i >= MAX_BTF_ARG_DEPTH || !data->btf_config[i].is_initialized)
393393
return 1;
394+
/* NULL pointer */
395+
if (*data->arg == 0) {
396+
*data->null_ptr_found = true;
397+
return 1;
398+
}
394399
*data->arg = *data->arg + data->btf_config[i].offset;
395400
if (data->btf_config[i].is_pointer)
396401
probe_read((void *)data->arg, sizeof(char *), (void *)*data->arg);
397402
return 0;
398403
}
399404

400405
#ifdef __LARGE_BPF_PROG
401-
FUNC_INLINE void extract_arg(struct event_config *config, int index, unsigned long *a)
406+
FUNC_INLINE void extract_arg(struct event_config *config, int index, unsigned long *a,
407+
bool *null_ptr_found)
402408
{
403409
struct config_btf_arg *btf_config;
404410

@@ -413,6 +419,7 @@ FUNC_INLINE void extract_arg(struct event_config *config, int index, unsigned lo
413419
struct extract_arg_data extract_data = {
414420
.btf_config = btf_config,
415421
.arg = a,
422+
.null_ptr_found = null_ptr_found,
416423
};
417424
#ifndef __V61_BPF_PROG
418425
#pragma unroll
@@ -426,7 +433,8 @@ FUNC_INLINE void extract_arg(struct event_config *config, int index, unsigned lo
426433
}
427434
}
428435
#else
429-
FUNC_INLINE void extract_arg(struct event_config *config, int index, unsigned long *a) {}
436+
FUNC_INLINE void extract_arg(struct event_config *config, int index, unsigned long *a,
437+
bool *null_ptr_found) {}
430438
#endif /* __LARGE_BPF_PROG */
431439

432440
FUNC_INLINE int arg_idx(int index)
@@ -471,9 +479,11 @@ FUNC_INLINE long generic_read_arg(void *ctx, int index, long off, struct bpf_map
471479
ty = config->arg[index];
472480
am = config->arm[index];
473481

482+
e->resolve_null_ptr[index] = false;
483+
474484
#if defined(GENERIC_TRACEPOINT) || defined(GENERIC_USDT)
475485
a = (&e->a0)[index];
476-
extract_arg(config, index, &a);
486+
extract_arg(config, index, &a, &e->resolve_null_ptr[index]);
477487
#else
478488
arg_index = config->idx[index];
479489
asm volatile("%[arg_index] &= %1 ;\n"
@@ -491,7 +501,7 @@ FUNC_INLINE long generic_read_arg(void *ctx, int index, long off, struct bpf_map
491501
else
492502
a = (&e->a0)[arg_index];
493503

494-
extract_arg(config, index, &a);
504+
extract_arg(config, index, &a, &e->resolve_null_ptr[index]);
495505

496506
if (should_offload_path(ty))
497507
return generic_path_offload(ctx, ty, a, index, off, tailcals);

bpf/process/types/basic.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,7 @@ struct config_usdt_arg {
184184
struct extract_arg_data {
185185
struct config_btf_arg *btf_config;
186186
unsigned long *arg;
187+
bool *null_ptr_found;
187188
};
188189

189190
#define MAX_BTF_ARG_DEPTH 10
@@ -2024,6 +2025,9 @@ selector_arg_offset(__u8 *f, struct msg_generic_kprobe *e, __u32 selidx,
20242025
if (index > 5)
20252026
return 0;
20262027

2028+
if (e->resolve_null_ptr[index])
2029+
return 0;
2030+
20272031
args = get_arg(e, index);
20282032
switch (filter->type) {
20292033
case fd_ty:

0 commit comments

Comments
 (0)