Skip to content

Commit 174b169

Browse files
robertosassuAlexei Starovoitov
authored and
Alexei Starovoitov
committed
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 <[email protected]> Signed-off-by: Alexei Starovoitov <[email protected]> Link: https://lore.kernel.org/bpf/[email protected]
1 parent 280fe83 commit 174b169

File tree

3 files changed

+42
-0
lines changed

3 files changed

+42
-0
lines changed

include/uapi/linux/bpf.h

+11
Original file line numberDiff line numberDiff line change
@@ -5119,6 +5119,16 @@ union bpf_attr {
51195119
* 0 on success.
51205120
* **-EINVAL** for invalid input
51215121
* **-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.
51225132
*/
51235133
#define __BPF_FUNC_MAPPER(FN) \
51245134
FN(unspec), \
@@ -5314,6 +5324,7 @@ union bpf_attr {
53145324
FN(xdp_store_bytes), \
53155325
FN(copy_from_user_task), \
53165326
FN(skb_set_tstamp), \
5327+
FN(ima_file_hash), \
53175328
/* */
53185329

53195330
/* integer value in 'imm' field of BPF_CALL instruction selects which helper

kernel/bpf/bpf_lsm.c

+20
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,24 @@ static const struct bpf_func_proto bpf_ima_inode_hash_proto = {
9999
.allowed = bpf_ima_inode_hash_allowed,
100100
};
101101

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+
102120
static const struct bpf_func_proto *
103121
bpf_lsm_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
104122
{
@@ -121,6 +139,8 @@ bpf_lsm_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
121139
return &bpf_bprm_opts_set_proto;
122140
case BPF_FUNC_ima_inode_hash:
123141
return prog->aux->sleepable ? &bpf_ima_inode_hash_proto : NULL;
142+
case BPF_FUNC_ima_file_hash:
143+
return prog->aux->sleepable ? &bpf_ima_file_hash_proto : NULL;
124144
default:
125145
return tracing_prog_func_proto(func_id, prog);
126146
}

tools/include/uapi/linux/bpf.h

+11
Original file line numberDiff line numberDiff line change
@@ -5119,6 +5119,16 @@ union bpf_attr {
51195119
* 0 on success.
51205120
* **-EINVAL** for invalid input
51215121
* **-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.
51225132
*/
51235133
#define __BPF_FUNC_MAPPER(FN) \
51245134
FN(unspec), \
@@ -5314,6 +5324,7 @@ union bpf_attr {
53145324
FN(xdp_store_bytes), \
53155325
FN(copy_from_user_task), \
53165326
FN(skb_set_tstamp), \
5327+
FN(ima_file_hash), \
53175328
/* */
53185329

53195330
/* integer value in 'imm' field of BPF_CALL instruction selects which helper

0 commit comments

Comments
 (0)