Skip to content

Commit

Permalink
Test that we can call ima_file_hash helper from generic_lsm
Browse files Browse the repository at this point in the history
NoPost action is used for test.

Signed-off-by: Andrei Fedotov <anfedotoff@yandex-team.ru>
  • Loading branch information
anfedotoff committed Aug 20, 2024
1 parent 2a7a5a8 commit 7525121
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 9 deletions.
29 changes: 29 additions & 0 deletions bpf/process/bpf_generic_lsm.c
Original file line number Diff line number Diff line change
Expand Up @@ -157,3 +157,32 @@ generic_lsm_output(void *ctx)

return try_override(ctx);
}

#if defined GENERIC_LSM && defined __LARGE_MAP_KEYS
__attribute__((section("lsm.s/file_open"), used)) int
BPF_PROG(ima_file_open, struct file *file)
{

struct msg_generic_kprobe *e;
int zero = 0;
int pathlen, hash_algo = -95; // EOPNOTSUPP
int idx = 0;

e = map_lookup_elem(&process_call_heap, &zero);
if(!e)
return 0;

char *args = e->args;

args = args_off(e, e->argsoff[idx & MAX_SELECTORS_MASK]);
pathlen = *(int *)args & (MAX_STRING - 1);
args += pathlen + 6;
hash_algo = ima_file_hash(file, args + 4, 64);
*(int *)args = hash_algo;
__u64 hash = *(__u64 *)(args + 4);
bpf_printk("algo: %i\n", hash_algo);
bpf_printk("hash: %x\n", hash);

return 0;
}
#endif
14 changes: 5 additions & 9 deletions bpf/process/types/basic.h
Original file line number Diff line number Diff line change
Expand Up @@ -2272,15 +2272,6 @@ do_action(void *ctx, __u32 i, struct selector_action *actions,
e->common.flags |= MSG_COMMON_FLAG_USER_STACKTRACE;
e->user_stack_id = get_stackid(ctx, &stack_trace_map, BPF_F_USER_STACK);
}

#if defined GENERIC_LSM && defined __LARGE_MAP_KEYS
__u32 ima_arg_idx = actions->act[++i];

if (ima_arg_idx >= 0 && ima_arg_idx < 5) {
e->common.flags |= MSG_COMMON_FLAG_IMA_HASH;
get_ima_hash(maps->config, e, ima_arg_idx);
}
#endif /* GENERIC_LSM */
break;
}

Expand Down Expand Up @@ -2439,6 +2430,11 @@ generic_actions(void *ctx, struct generic_maps *maps)
postit = do_actions(ctx, actions, maps);
if (postit)
tail_call(ctx, maps->calls, TAIL_CALL_SEND);
#if defined GENERIC_LSM && defined __LARGE_MAP_KEYS
else
// TEST ima_file_hash
tail_call(ctx, maps->calls, 6);
#endif /* GENERIC_LSM */
return 0;
}

Expand Down
9 changes: 9 additions & 0 deletions pkg/sensors/program/loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -728,6 +728,15 @@ func installTailCalls(bpfDir string, spec *ebpf.CollectionSpec, coll *ebpf.Colle
}
}
}
secName := "lsm.s/file_open"
if progName, ok := secToProgName[secName]; ok {
if prog, ok := coll.Programs[progName]; ok {
err := tailCallsMap.Update(uint32(6), uint32(prog.FD()), ebpf.UpdateAny)
if err != nil {
return fmt.Errorf("update of tail-call map '%s' failed: %w", pinPath, err)
}
}
}
return nil
}

Expand Down

0 comments on commit 7525121

Please sign in to comment.