Skip to content

Commit 03b7179

Browse files
sinkapkernel-patches-bot
authored andcommitted
bpf: Add bpf_lsm_set_bprm_opts helper
The helper allows modification of certain bits on the linux_binprm struct starting with the secureexec bit which can be updated using the BPF_LSM_F_BPRM_SECUREEXEC flag. secureexec can be set by the LSM for privilege gaining executions to set the AT_SECURE auxv for glibc. When set, the dynamic linker disables the use of certain environment variables (like LD_PRELOAD). Signed-off-by: KP Singh <kpsingh@google.com>
1 parent ce6e40b commit 03b7179

File tree

4 files changed

+65
-0
lines changed

4 files changed

+65
-0
lines changed

include/uapi/linux/bpf.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3787,6 +3787,18 @@ union bpf_attr {
37873787
* *ARG_PTR_TO_BTF_ID* of type *task_struct*.
37883788
* Return
37893789
* Pointer to the current task.
3790+
*
3791+
* long bpf_lsm_set_bprm_opts(struct linux_binprm *bprm, u64 flags)
3792+
*
3793+
* Description
3794+
* Set or clear certain options on *bprm*:
3795+
*
3796+
* **BPF_LSM_F_BPRM_SECUREEXEC** Set the secureexec bit
3797+
* which sets the **AT_SECURE** auxv for glibc. The bit
3798+
* is cleared if the flag is not specified.
3799+
* Return
3800+
* **-EINVAL** if invalid *flags* are passed.
3801+
*
37903802
*/
37913803
#define __BPF_FUNC_MAPPER(FN) \
37923804
FN(unspec), \
@@ -3948,6 +3960,7 @@ union bpf_attr {
39483960
FN(task_storage_get), \
39493961
FN(task_storage_delete), \
39503962
FN(get_current_task_btf), \
3963+
FN(lsm_set_bprm_opts), \
39513964
/* */
39523965

39533966
/* integer value in 'imm' field of BPF_CALL instruction selects which helper
@@ -4119,6 +4132,11 @@ enum bpf_lwt_encap_mode {
41194132
BPF_LWT_ENCAP_IP,
41204133
};
41214134

4135+
/* Flags for LSM helpers */
4136+
enum {
4137+
BPF_LSM_F_BPRM_SECUREEXEC = (1ULL << 0),
4138+
};
4139+
41224140
#define __bpf_md_ptr(type, name) \
41234141
union { \
41244142
type name; \

kernel/bpf/bpf_lsm.c

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include <linux/filter.h>
88
#include <linux/bpf.h>
99
#include <linux/btf.h>
10+
#include <linux/binfmts.h>
1011
#include <linux/lsm_hooks.h>
1112
#include <linux/bpf_lsm.h>
1213
#include <linux/kallsyms.h>
@@ -51,6 +52,30 @@ int bpf_lsm_verify_prog(struct bpf_verifier_log *vlog,
5152
return 0;
5253
}
5354

55+
/* Mask for all the currently supported BPRM option flags */
56+
#define BPF_LSM_F_BRPM_OPTS_MASK BPF_LSM_F_BPRM_SECUREEXEC
57+
58+
BPF_CALL_2(bpf_lsm_set_bprm_opts, struct linux_binprm *, bprm, u64, flags)
59+
{
60+
61+
if (flags & ~BPF_LSM_F_BRPM_OPTS_MASK)
62+
return -EINVAL;
63+
64+
bprm->secureexec = (flags & BPF_LSM_F_BPRM_SECUREEXEC);
65+
return 0;
66+
}
67+
68+
BTF_ID_LIST_SINGLE(bpf_lsm_set_bprm_opts_btf_ids, struct, linux_binprm)
69+
70+
const static struct bpf_func_proto bpf_lsm_set_bprm_opts_proto = {
71+
.func = bpf_lsm_set_bprm_opts,
72+
.gpl_only = false,
73+
.ret_type = RET_INTEGER,
74+
.arg1_type = ARG_PTR_TO_BTF_ID,
75+
.arg1_btf_id = &bpf_lsm_set_bprm_opts_btf_ids[0],
76+
.arg2_type = ARG_ANYTHING,
77+
};
78+
5479
static const struct bpf_func_proto *
5580
bpf_lsm_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
5681
{
@@ -71,6 +96,8 @@ bpf_lsm_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
7196
return &bpf_task_storage_get_proto;
7297
case BPF_FUNC_task_storage_delete:
7398
return &bpf_task_storage_delete_proto;
99+
case BPF_FUNC_lsm_set_bprm_opts:
100+
return &bpf_lsm_set_bprm_opts_proto;
74101
default:
75102
return tracing_prog_func_proto(func_id, prog);
76103
}

scripts/bpf_helpers_doc.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -418,6 +418,7 @@ class PrinterHelpers(Printer):
418418
'struct bpf_tcp_sock',
419419
'struct bpf_tunnel_key',
420420
'struct bpf_xfrm_state',
421+
'struct linux_binprm',
421422
'struct pt_regs',
422423
'struct sk_reuseport_md',
423424
'struct sockaddr',
@@ -465,6 +466,7 @@ class PrinterHelpers(Printer):
465466
'struct bpf_tcp_sock',
466467
'struct bpf_tunnel_key',
467468
'struct bpf_xfrm_state',
469+
'struct linux_binprm',
468470
'struct pt_regs',
469471
'struct sk_reuseport_md',
470472
'struct sockaddr',

tools/include/uapi/linux/bpf.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3787,6 +3787,18 @@ union bpf_attr {
37873787
* *ARG_PTR_TO_BTF_ID* of type *task_struct*.
37883788
* Return
37893789
* Pointer to the current task.
3790+
*
3791+
* long bpf_lsm_set_bprm_opts(struct linux_binprm *bprm, u64 flags)
3792+
*
3793+
* Description
3794+
* Set or clear certain options on *bprm*:
3795+
*
3796+
* **BPF_LSM_F_BPRM_SECUREEXEC** Set the secureexec bit
3797+
* which sets the **AT_SECURE** auxv for glibc. The bit
3798+
* is cleared if the flag is not specified.
3799+
* Return
3800+
* **-EINVAL** if invalid *flags* are passed.
3801+
*
37903802
*/
37913803
#define __BPF_FUNC_MAPPER(FN) \
37923804
FN(unspec), \
@@ -3948,6 +3960,7 @@ union bpf_attr {
39483960
FN(task_storage_get), \
39493961
FN(task_storage_delete), \
39503962
FN(get_current_task_btf), \
3963+
FN(lsm_set_bprm_opts), \
39513964
/* */
39523965

39533966
/* integer value in 'imm' field of BPF_CALL instruction selects which helper
@@ -4119,6 +4132,11 @@ enum bpf_lwt_encap_mode {
41194132
BPF_LWT_ENCAP_IP,
41204133
};
41214134

4135+
/* Flags for LSM helpers */
4136+
enum {
4137+
BPF_LSM_F_BPRM_SECUREEXEC = (1ULL << 0),
4138+
};
4139+
41224140
#define __bpf_md_ptr(type, name) \
41234141
union { \
41244142
type name; \

0 commit comments

Comments
 (0)