Skip to content

Commit 4f19cab

Browse files
FlorentRevestborkmann
authored andcommitted
bpf: Add a bpf_sock_from_file helper
While eBPF programs can check whether a file is a socket by file->f_op == &socket_file_ops, they cannot convert the void private_data pointer to a struct socket BTF pointer. In order to do this a new helper wrapping sock_from_file is added. This is useful to tracing programs but also other program types inheriting this set of helpers such as iterators or LSM programs. Signed-off-by: Florent Revest <[email protected]> Signed-off-by: Daniel Borkmann <[email protected]> Acked-by: KP Singh <[email protected]> Acked-by: Martin KaFai Lau <[email protected]> Link: https://lore.kernel.org/bpf/[email protected]
1 parent dba4a92 commit 4f19cab

File tree

4 files changed

+42
-0
lines changed

4 files changed

+42
-0
lines changed

include/uapi/linux/bpf.h

+9
Original file line numberDiff line numberDiff line change
@@ -3822,6 +3822,14 @@ union bpf_attr {
38223822
* The **hash_algo** is returned on success,
38233823
* **-EOPNOTSUP** if IMA is disabled or **-EINVAL** if
38243824
* invalid arguments are passed.
3825+
*
3826+
* struct socket *bpf_sock_from_file(struct file *file)
3827+
* Description
3828+
* If the given file represents a socket, returns the associated
3829+
* socket.
3830+
* Return
3831+
* A pointer to a struct socket on success or NULL if the file is
3832+
* not a socket.
38253833
*/
38263834
#define __BPF_FUNC_MAPPER(FN) \
38273835
FN(unspec), \
@@ -3986,6 +3994,7 @@ union bpf_attr {
39863994
FN(bprm_opts_set), \
39873995
FN(ktime_get_coarse_ns), \
39883996
FN(ima_inode_hash), \
3997+
FN(sock_from_file), \
39893998
/* */
39903999

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

kernel/trace/bpf_trace.c

+20
Original file line numberDiff line numberDiff line change
@@ -1270,6 +1270,24 @@ const struct bpf_func_proto bpf_snprintf_btf_proto = {
12701270
.arg5_type = ARG_ANYTHING,
12711271
};
12721272

1273+
BPF_CALL_1(bpf_sock_from_file, struct file *, file)
1274+
{
1275+
return (unsigned long) sock_from_file(file);
1276+
}
1277+
1278+
BTF_ID_LIST(bpf_sock_from_file_btf_ids)
1279+
BTF_ID(struct, socket)
1280+
BTF_ID(struct, file)
1281+
1282+
static const struct bpf_func_proto bpf_sock_from_file_proto = {
1283+
.func = bpf_sock_from_file,
1284+
.gpl_only = false,
1285+
.ret_type = RET_PTR_TO_BTF_ID_OR_NULL,
1286+
.ret_btf_id = &bpf_sock_from_file_btf_ids[0],
1287+
.arg1_type = ARG_PTR_TO_BTF_ID,
1288+
.arg1_btf_id = &bpf_sock_from_file_btf_ids[1],
1289+
};
1290+
12731291
const struct bpf_func_proto *
12741292
bpf_tracing_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
12751293
{
@@ -1366,6 +1384,8 @@ bpf_tracing_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
13661384
return &bpf_per_cpu_ptr_proto;
13671385
case BPF_FUNC_bpf_this_cpu_ptr:
13681386
return &bpf_this_cpu_ptr_proto;
1387+
case BPF_FUNC_sock_from_file:
1388+
return &bpf_sock_from_file_proto;
13691389
default:
13701390
return NULL;
13711391
}

scripts/bpf_helpers_doc.py

+4
Original file line numberDiff line numberDiff line change
@@ -437,6 +437,8 @@ class PrinterHelpers(Printer):
437437
'struct path',
438438
'struct btf_ptr',
439439
'struct inode',
440+
'struct socket',
441+
'struct file',
440442
]
441443
known_types = {
442444
'...',
@@ -482,6 +484,8 @@ class PrinterHelpers(Printer):
482484
'struct path',
483485
'struct btf_ptr',
484486
'struct inode',
487+
'struct socket',
488+
'struct file',
485489
}
486490
mapped_types = {
487491
'u8': '__u8',

tools/include/uapi/linux/bpf.h

+9
Original file line numberDiff line numberDiff line change
@@ -3822,6 +3822,14 @@ union bpf_attr {
38223822
* The **hash_algo** is returned on success,
38233823
* **-EOPNOTSUP** if IMA is disabled or **-EINVAL** if
38243824
* invalid arguments are passed.
3825+
*
3826+
* struct socket *bpf_sock_from_file(struct file *file)
3827+
* Description
3828+
* If the given file represents a socket, returns the associated
3829+
* socket.
3830+
* Return
3831+
* A pointer to a struct socket on success or NULL if the file is
3832+
* not a socket.
38253833
*/
38263834
#define __BPF_FUNC_MAPPER(FN) \
38273835
FN(unspec), \
@@ -3986,6 +3994,7 @@ union bpf_attr {
39863994
FN(bprm_opts_set), \
39873995
FN(ktime_get_coarse_ns), \
39883996
FN(ima_inode_hash), \
3997+
FN(sock_from_file), \
39893998
/* */
39903999

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

0 commit comments

Comments
 (0)