Skip to content

Commit ac4414b

Browse files
Alexei Starovoitovborkmann
authored andcommitted
bpf: Attach raw_tp program with BTF via type name
BTF type id specified at program load time has all necessary information to attach that program to raw tracepoint. Use kernel type name to find raw tracepoint. Add missing CHECK_ATTR() condition. Signed-off-by: Alexei Starovoitov <[email protected]> Signed-off-by: Daniel Borkmann <[email protected]> Acked-by: Andrii Nakryiko <[email protected]> Acked-by: Martin KaFai Lau <[email protected]> Link: https://lore.kernel.org/bpf/[email protected]
1 parent 9e15db6 commit ac4414b

File tree

1 file changed

+47
-23
lines changed

1 file changed

+47
-23
lines changed

kernel/bpf/syscall.c

Lines changed: 47 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1816,56 +1816,80 @@ static int bpf_raw_tracepoint_open(const union bpf_attr *attr)
18161816
struct bpf_raw_tracepoint *raw_tp;
18171817
struct bpf_raw_event_map *btp;
18181818
struct bpf_prog *prog;
1819-
char tp_name[128];
1819+
const char *tp_name;
1820+
char buf[128];
18201821
int tp_fd, err;
18211822

1822-
if (strncpy_from_user(tp_name, u64_to_user_ptr(attr->raw_tracepoint.name),
1823-
sizeof(tp_name) - 1) < 0)
1824-
return -EFAULT;
1825-
tp_name[sizeof(tp_name) - 1] = 0;
1823+
if (CHECK_ATTR(BPF_RAW_TRACEPOINT_OPEN))
1824+
return -EINVAL;
1825+
1826+
prog = bpf_prog_get(attr->raw_tracepoint.prog_fd);
1827+
if (IS_ERR(prog))
1828+
return PTR_ERR(prog);
1829+
1830+
if (prog->type != BPF_PROG_TYPE_RAW_TRACEPOINT &&
1831+
prog->type != BPF_PROG_TYPE_RAW_TRACEPOINT_WRITABLE) {
1832+
err = -EINVAL;
1833+
goto out_put_prog;
1834+
}
1835+
1836+
if (prog->type == BPF_PROG_TYPE_RAW_TRACEPOINT &&
1837+
prog->aux->attach_btf_id) {
1838+
if (attr->raw_tracepoint.name) {
1839+
/* raw_tp name should not be specified in raw_tp
1840+
* programs that were verified via in-kernel BTF info
1841+
*/
1842+
err = -EINVAL;
1843+
goto out_put_prog;
1844+
}
1845+
/* raw_tp name is taken from type name instead */
1846+
tp_name = kernel_type_name(prog->aux->attach_btf_id);
1847+
/* skip the prefix */
1848+
tp_name += sizeof("btf_trace_") - 1;
1849+
} else {
1850+
if (strncpy_from_user(buf,
1851+
u64_to_user_ptr(attr->raw_tracepoint.name),
1852+
sizeof(buf) - 1) < 0) {
1853+
err = -EFAULT;
1854+
goto out_put_prog;
1855+
}
1856+
buf[sizeof(buf) - 1] = 0;
1857+
tp_name = buf;
1858+
}
18261859

18271860
btp = bpf_get_raw_tracepoint(tp_name);
1828-
if (!btp)
1829-
return -ENOENT;
1861+
if (!btp) {
1862+
err = -ENOENT;
1863+
goto out_put_prog;
1864+
}
18301865

18311866
raw_tp = kzalloc(sizeof(*raw_tp), GFP_USER);
18321867
if (!raw_tp) {
18331868
err = -ENOMEM;
18341869
goto out_put_btp;
18351870
}
18361871
raw_tp->btp = btp;
1837-
1838-
prog = bpf_prog_get(attr->raw_tracepoint.prog_fd);
1839-
if (IS_ERR(prog)) {
1840-
err = PTR_ERR(prog);
1841-
goto out_free_tp;
1842-
}
1843-
if (prog->type != BPF_PROG_TYPE_RAW_TRACEPOINT &&
1844-
prog->type != BPF_PROG_TYPE_RAW_TRACEPOINT_WRITABLE) {
1845-
err = -EINVAL;
1846-
goto out_put_prog;
1847-
}
1872+
raw_tp->prog = prog;
18481873

18491874
err = bpf_probe_register(raw_tp->btp, prog);
18501875
if (err)
1851-
goto out_put_prog;
1876+
goto out_free_tp;
18521877

1853-
raw_tp->prog = prog;
18541878
tp_fd = anon_inode_getfd("bpf-raw-tracepoint", &bpf_raw_tp_fops, raw_tp,
18551879
O_CLOEXEC);
18561880
if (tp_fd < 0) {
18571881
bpf_probe_unregister(raw_tp->btp, prog);
18581882
err = tp_fd;
1859-
goto out_put_prog;
1883+
goto out_free_tp;
18601884
}
18611885
return tp_fd;
18621886

1863-
out_put_prog:
1864-
bpf_prog_put(prog);
18651887
out_free_tp:
18661888
kfree(raw_tp);
18671889
out_put_btp:
18681890
bpf_put_raw_tracepoint(btp);
1891+
out_put_prog:
1892+
bpf_prog_put(prog);
18691893
return err;
18701894
}
18711895

0 commit comments

Comments
 (0)