Skip to content

Commit d5fb316

Browse files
committed
Merge branch 'add-the-missing-bpf_link_type-invocation-for-sockmap'
Hou Tao says: ==================== Add the missing BPF_LINK_TYPE invocation for sockmap From: Hou Tao <[email protected]> Hi, The tiny patch set fixes the out-of-bound read problem when reading the fdinfo of sock map link fd. And in order to spot such omission early for the newly-added link type in the future, it also checks the validity of the link->type and adds a WARN_ONCE() for missed invocation. Please see individual patches for more details. And comments are always welcome. v3: * patch #2: check and warn the validity of link->type instead of adding a static assertion for bpf_link_type_strs array. v2: http://lore.kernel.org/bpf/[email protected] ==================== Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Andrii Nakryiko <[email protected]>
2 parents 9806f28 + 8421d4c commit d5fb316

File tree

4 files changed

+16
-5
lines changed

4 files changed

+16
-5
lines changed

include/linux/bpf_types.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,7 @@ BPF_LINK_TYPE(BPF_LINK_TYPE_XDP, xdp)
146146
BPF_LINK_TYPE(BPF_LINK_TYPE_NETFILTER, netfilter)
147147
BPF_LINK_TYPE(BPF_LINK_TYPE_TCX, tcx)
148148
BPF_LINK_TYPE(BPF_LINK_TYPE_NETKIT, netkit)
149+
BPF_LINK_TYPE(BPF_LINK_TYPE_SOCKMAP, sockmap)
149150
#endif
150151
#ifdef CONFIG_PERF_EVENTS
151152
BPF_LINK_TYPE(BPF_LINK_TYPE_PERF_EVENT, perf)

include/uapi/linux/bpf.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1121,6 +1121,9 @@ enum bpf_attach_type {
11211121

11221122
#define MAX_BPF_ATTACH_TYPE __MAX_BPF_ATTACH_TYPE
11231123

1124+
/* Add BPF_LINK_TYPE(type, name) in bpf_types.h to keep bpf_link_type_strs[]
1125+
* in sync with the definitions below.
1126+
*/
11241127
enum bpf_link_type {
11251128
BPF_LINK_TYPE_UNSPEC = 0,
11261129
BPF_LINK_TYPE_RAW_TRACEPOINT = 1,

kernel/bpf/syscall.c

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3069,13 +3069,17 @@ static void bpf_link_show_fdinfo(struct seq_file *m, struct file *filp)
30693069
{
30703070
const struct bpf_link *link = filp->private_data;
30713071
const struct bpf_prog *prog = link->prog;
3072+
enum bpf_link_type type = link->type;
30723073
char prog_tag[sizeof(prog->tag) * 2 + 1] = { };
30733074

3074-
seq_printf(m,
3075-
"link_type:\t%s\n"
3076-
"link_id:\t%u\n",
3077-
bpf_link_type_strs[link->type],
3078-
link->id);
3075+
if (type < ARRAY_SIZE(bpf_link_type_strs) && bpf_link_type_strs[type]) {
3076+
seq_printf(m, "link_type:\t%s\n", bpf_link_type_strs[type]);
3077+
} else {
3078+
WARN_ONCE(1, "missing BPF_LINK_TYPE(...) for link type %u\n", type);
3079+
seq_printf(m, "link_type:\t<%u>\n", type);
3080+
}
3081+
seq_printf(m, "link_id:\t%u\n", link->id);
3082+
30793083
if (prog) {
30803084
bin2hex(prog_tag, prog->tag, sizeof(prog->tag));
30813085
seq_printf(m,

tools/include/uapi/linux/bpf.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1121,6 +1121,9 @@ enum bpf_attach_type {
11211121

11221122
#define MAX_BPF_ATTACH_TYPE __MAX_BPF_ATTACH_TYPE
11231123

1124+
/* Add BPF_LINK_TYPE(type, name) in bpf_types.h to keep bpf_link_type_strs[]
1125+
* in sync with the definitions below.
1126+
*/
11241127
enum bpf_link_type {
11251128
BPF_LINK_TYPE_UNSPEC = 0,
11261129
BPF_LINK_TYPE_RAW_TRACEPOINT = 1,

0 commit comments

Comments
 (0)