134b82d3aSKP Singh // SPDX-License-Identifier: GPL-2.0 234b82d3aSKP Singh 334b82d3aSKP Singh /* 434b82d3aSKP Singh * Copyright 2020 Google LLC. 534b82d3aSKP Singh */ 634b82d3aSKP Singh 734b82d3aSKP Singh #include "vmlinux.h" 834b82d3aSKP Singh #include <errno.h> 934b82d3aSKP Singh #include <bpf/bpf_helpers.h> 1034b82d3aSKP Singh #include <bpf/bpf_tracing.h> 1134b82d3aSKP Singh 1234b82d3aSKP Singh u32 monitored_pid = 0; 1334b82d3aSKP Singh 14f446b570SKP Singh struct { 15f446b570SKP Singh __uint(type, BPF_MAP_TYPE_RINGBUF); 16f446b570SKP Singh __uint(max_entries, 1 << 12); 17f446b570SKP Singh } ringbuf SEC(".maps"); 18f446b570SKP Singh 1934b82d3aSKP Singh char _license[] SEC("license") = "GPL"; 2034b82d3aSKP Singh 2127a77d0dSRoberto Sassu bool use_ima_file_hash; 2291e8fa25SRoberto Sassu bool enable_bprm_creds_for_exec; 23e6dcf7bbSRoberto Sassu bool enable_kernel_read_file; 24*7bae42b6SRoberto Sassu bool test_deny; 2527a77d0dSRoberto Sassu 262746de3cSRoberto Sassu static void ima_test_common(struct file *file) 2734b82d3aSKP Singh { 28f446b570SKP Singh u64 ima_hash = 0; 29f446b570SKP Singh u64 *sample; 30f446b570SKP Singh int ret; 31f446b570SKP Singh u32 pid; 3234b82d3aSKP Singh 33f446b570SKP Singh pid = bpf_get_current_pid_tgid() >> 32; 34f446b570SKP Singh if (pid == monitored_pid) { 3527a77d0dSRoberto Sassu if (!use_ima_file_hash) 362746de3cSRoberto Sassu ret = bpf_ima_inode_hash(file->f_inode, &ima_hash, 37f446b570SKP Singh sizeof(ima_hash)); 3827a77d0dSRoberto Sassu else 3927a77d0dSRoberto Sassu ret = bpf_ima_file_hash(file, &ima_hash, 4027a77d0dSRoberto Sassu sizeof(ima_hash)); 41f446b570SKP Singh if (ret < 0 || ima_hash == 0) 42f446b570SKP Singh return; 4334b82d3aSKP Singh 44f446b570SKP Singh sample = bpf_ringbuf_reserve(&ringbuf, sizeof(u64), 0); 45f446b570SKP Singh if (!sample) 46f446b570SKP Singh return; 47f446b570SKP Singh 48f446b570SKP Singh *sample = ima_hash; 49f446b570SKP Singh bpf_ringbuf_submit(sample, 0); 50f446b570SKP Singh } 51f446b570SKP Singh 52f446b570SKP Singh return; 5334b82d3aSKP Singh } 542746de3cSRoberto Sassu 55*7bae42b6SRoberto Sassu static int ima_test_deny(void) 56*7bae42b6SRoberto Sassu { 57*7bae42b6SRoberto Sassu u32 pid; 58*7bae42b6SRoberto Sassu 59*7bae42b6SRoberto Sassu pid = bpf_get_current_pid_tgid() >> 32; 60*7bae42b6SRoberto Sassu if (pid == monitored_pid && test_deny) 61*7bae42b6SRoberto Sassu return -EPERM; 62*7bae42b6SRoberto Sassu 63*7bae42b6SRoberto Sassu return 0; 64*7bae42b6SRoberto Sassu } 65*7bae42b6SRoberto Sassu 662746de3cSRoberto Sassu SEC("lsm.s/bprm_committed_creds") 672746de3cSRoberto Sassu void BPF_PROG(bprm_committed_creds, struct linux_binprm *bprm) 682746de3cSRoberto Sassu { 692746de3cSRoberto Sassu ima_test_common(bprm->file); 702746de3cSRoberto Sassu } 7191e8fa25SRoberto Sassu 7291e8fa25SRoberto Sassu SEC("lsm.s/bprm_creds_for_exec") 7391e8fa25SRoberto Sassu int BPF_PROG(bprm_creds_for_exec, struct linux_binprm *bprm) 7491e8fa25SRoberto Sassu { 7591e8fa25SRoberto Sassu if (!enable_bprm_creds_for_exec) 7691e8fa25SRoberto Sassu return 0; 7791e8fa25SRoberto Sassu 7891e8fa25SRoberto Sassu ima_test_common(bprm->file); 7991e8fa25SRoberto Sassu return 0; 8091e8fa25SRoberto Sassu } 81e6dcf7bbSRoberto Sassu 82e6dcf7bbSRoberto Sassu SEC("lsm.s/kernel_read_file") 83e6dcf7bbSRoberto Sassu int BPF_PROG(kernel_read_file, struct file *file, enum kernel_read_file_id id, 84e6dcf7bbSRoberto Sassu bool contents) 85e6dcf7bbSRoberto Sassu { 86*7bae42b6SRoberto Sassu int ret; 87*7bae42b6SRoberto Sassu 88e6dcf7bbSRoberto Sassu if (!enable_kernel_read_file) 89e6dcf7bbSRoberto Sassu return 0; 90e6dcf7bbSRoberto Sassu 91e6dcf7bbSRoberto Sassu if (!contents) 92e6dcf7bbSRoberto Sassu return 0; 93e6dcf7bbSRoberto Sassu 94e6dcf7bbSRoberto Sassu if (id != READING_POLICY) 95e6dcf7bbSRoberto Sassu return 0; 96e6dcf7bbSRoberto Sassu 97*7bae42b6SRoberto Sassu ret = ima_test_deny(); 98*7bae42b6SRoberto Sassu if (ret < 0) 99*7bae42b6SRoberto Sassu return ret; 100*7bae42b6SRoberto Sassu 101e6dcf7bbSRoberto Sassu ima_test_common(file); 102e6dcf7bbSRoberto Sassu return 0; 103e6dcf7bbSRoberto Sassu } 104