Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

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 <ast@kernel.org>
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
Acked-by: Andrii Nakryiko <andriin@fb.com>
Acked-by: Martin KaFai Lau <kafai@fb.com>
Link: https://lore.kernel.org/bpf/20191016032505.2089704-8-ast@kernel.org

authored by

Alexei Starovoitov and committed by
Daniel Borkmann
ac4414b5 9e15db66

+47 -23
+47 -23
kernel/bpf/syscall.c
··· 1816 1816 struct bpf_raw_tracepoint *raw_tp; 1817 1817 struct bpf_raw_event_map *btp; 1818 1818 struct bpf_prog *prog; 1819 - char tp_name[128]; 1819 + const char *tp_name; 1820 + char buf[128]; 1820 1821 int tp_fd, err; 1821 1822 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 + } 1826 1859 1827 1860 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 + } 1830 1865 1831 1866 raw_tp = kzalloc(sizeof(*raw_tp), GFP_USER); 1832 1867 if (!raw_tp) { ··· 1869 1834 goto out_put_btp; 1870 1835 } 1871 1836 raw_tp->btp = btp; 1872 - 1873 - prog = bpf_prog_get(attr->raw_tracepoint.prog_fd); 1874 - if (IS_ERR(prog)) { 1875 - err = PTR_ERR(prog); 1876 - goto out_free_tp; 1877 - } 1878 - if (prog->type != BPF_PROG_TYPE_RAW_TRACEPOINT && 1879 - prog->type != BPF_PROG_TYPE_RAW_TRACEPOINT_WRITABLE) { 1880 - err = -EINVAL; 1881 - goto out_put_prog; 1882 - } 1837 + raw_tp->prog = prog; 1883 1838 1884 1839 err = bpf_probe_register(raw_tp->btp, prog); 1885 1840 if (err) 1886 - goto out_put_prog; 1841 + goto out_free_tp; 1887 1842 1888 - raw_tp->prog = prog; 1889 1843 tp_fd = anon_inode_getfd("bpf-raw-tracepoint", &bpf_raw_tp_fops, raw_tp, 1890 1844 O_CLOEXEC); 1891 1845 if (tp_fd < 0) { 1892 1846 bpf_probe_unregister(raw_tp->btp, prog); 1893 1847 err = tp_fd; 1894 - goto out_put_prog; 1848 + goto out_free_tp; 1895 1849 } 1896 1850 return tp_fd; 1897 1851 1898 - out_put_prog: 1899 - bpf_prog_put(prog); 1900 1852 out_free_tp: 1901 1853 kfree(raw_tp); 1902 1854 out_put_btp: 1903 1855 bpf_put_raw_tracepoint(btp); 1856 + out_put_prog: 1857 + bpf_prog_put(prog); 1904 1858 return err; 1905 1859 } 1906 1860