Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

bpf-lsm: Introduce new helper bpf_ima_file_hash()

ima_file_hash() has been modified to calculate the measurement of a file on
demand, if it has not been already performed by IMA or the measurement is
not fresh. For compatibility reasons, ima_inode_hash() remains unchanged.

Keep the same approach in eBPF and introduce the new helper
bpf_ima_file_hash() to take advantage of the modified behavior of
ima_file_hash().

Signed-off-by: Roberto Sassu <roberto.sassu@huawei.com>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
Link: https://lore.kernel.org/bpf/20220302111404.193900-4-roberto.sassu@huawei.com

authored by

Roberto Sassu and committed by
Alexei Starovoitov
174b1694 280fe836

+42
+11
include/uapi/linux/bpf.h
··· 5119 5119 * 0 on success. 5120 5120 * **-EINVAL** for invalid input 5121 5121 * **-EOPNOTSUPP** for unsupported protocol 5122 + * 5123 + * long bpf_ima_file_hash(struct file *file, void *dst, u32 size) 5124 + * Description 5125 + * Returns a calculated IMA hash of the *file*. 5126 + * If the hash is larger than *size*, then only *size* 5127 + * bytes will be copied to *dst* 5128 + * Return 5129 + * The **hash_algo** is returned on success, 5130 + * **-EOPNOTSUP** if the hash calculation failed or **-EINVAL** if 5131 + * invalid arguments are passed. 5122 5132 */ 5123 5133 #define __BPF_FUNC_MAPPER(FN) \ 5124 5134 FN(unspec), \ ··· 5324 5314 FN(xdp_store_bytes), \ 5325 5315 FN(copy_from_user_task), \ 5326 5316 FN(skb_set_tstamp), \ 5317 + FN(ima_file_hash), \ 5327 5318 /* */ 5328 5319 5329 5320 /* integer value in 'imm' field of BPF_CALL instruction selects which helper
+20
kernel/bpf/bpf_lsm.c
··· 99 99 .allowed = bpf_ima_inode_hash_allowed, 100 100 }; 101 101 102 + BPF_CALL_3(bpf_ima_file_hash, struct file *, file, void *, dst, u32, size) 103 + { 104 + return ima_file_hash(file, dst, size); 105 + } 106 + 107 + BTF_ID_LIST_SINGLE(bpf_ima_file_hash_btf_ids, struct, file) 108 + 109 + static const struct bpf_func_proto bpf_ima_file_hash_proto = { 110 + .func = bpf_ima_file_hash, 111 + .gpl_only = false, 112 + .ret_type = RET_INTEGER, 113 + .arg1_type = ARG_PTR_TO_BTF_ID, 114 + .arg1_btf_id = &bpf_ima_file_hash_btf_ids[0], 115 + .arg2_type = ARG_PTR_TO_UNINIT_MEM, 116 + .arg3_type = ARG_CONST_SIZE, 117 + .allowed = bpf_ima_inode_hash_allowed, 118 + }; 119 + 102 120 static const struct bpf_func_proto * 103 121 bpf_lsm_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog) 104 122 { ··· 139 121 return &bpf_bprm_opts_set_proto; 140 122 case BPF_FUNC_ima_inode_hash: 141 123 return prog->aux->sleepable ? &bpf_ima_inode_hash_proto : NULL; 124 + case BPF_FUNC_ima_file_hash: 125 + return prog->aux->sleepable ? &bpf_ima_file_hash_proto : NULL; 142 126 default: 143 127 return tracing_prog_func_proto(func_id, prog); 144 128 }
+11
tools/include/uapi/linux/bpf.h
··· 5119 5119 * 0 on success. 5120 5120 * **-EINVAL** for invalid input 5121 5121 * **-EOPNOTSUPP** for unsupported protocol 5122 + * 5123 + * long bpf_ima_file_hash(struct file *file, void *dst, u32 size) 5124 + * Description 5125 + * Returns a calculated IMA hash of the *file*. 5126 + * If the hash is larger than *size*, then only *size* 5127 + * bytes will be copied to *dst* 5128 + * Return 5129 + * The **hash_algo** is returned on success, 5130 + * **-EOPNOTSUP** if the hash calculation failed or **-EINVAL** if 5131 + * invalid arguments are passed. 5122 5132 */ 5123 5133 #define __BPF_FUNC_MAPPER(FN) \ 5124 5134 FN(unspec), \ ··· 5324 5314 FN(xdp_store_bytes), \ 5325 5315 FN(copy_from_user_task), \ 5326 5316 FN(skb_set_tstamp), \ 5317 + FN(ima_file_hash), \ 5327 5318 /* */ 5328 5319 5329 5320 /* integer value in 'imm' field of BPF_CALL instruction selects which helper