Message ID | 1445841394-142865-1-git-send-email-wangnan0@huawei.com |
---|---|
State | New |
Headers | show |
On 2015/10/26 14:36, Wang Nan wrote: > Many reason can make bpf_prog_load() return EINVAL. This patch utilizes > logbuf passed from user to deliver the actual reason of failure. > > Without this patch, people is easy to forget fixing the "version" > section in their BPF objects. > > Signed-off-by: Wang Nan <wangnan0@huawei.com> > Cc: Alexei Starovoitov <ast@kernel.org> > Cc: Arnaldo Carvalho de Melo <acme@redhat.com> > Cc: David S. Miller <davem@davemloft.net> > --- > kernel/bpf/syscall.c | 41 ++++++++++++++++++++++++++++++++++++++--- > 1 file changed, 38 insertions(+), 3 deletions(-) > > diff --git a/kernel/bpf/syscall.c b/kernel/bpf/syscall.c > index 687dd6c..3a0e4e7 100644 > --- a/kernel/bpf/syscall.c > +++ b/kernel/bpf/syscall.c > @@ -574,6 +574,32 @@ struct bpf_prog *bpf_prog_get(u32 ufd) > } > EXPORT_SYMBOL_GPL(bpf_prog_get); > > +static void > +bpf_prog_load_note(union bpf_attr *attr, const char *fmt, ...) > +{ > + u32 log_level, log_size, log_len; > + char __user *log_ubuf = NULL; > + /* 64 chars should be long enough for a one line note. */ > + char log_buf[64]; > + va_list args; > + > + log_ubuf = (char __user *) (unsigned long) attr->log_buf; > + log_level = attr->log_level; > + log_size = sizeof(log_buf); > + if (attr->log_size < log_size) > + log_size = attr->log_size; > + > + if (log_level == 0 || !log_size || !log_ubuf) > + return; > + > + va_start(args, fmt); > + log_len = vscnprintf(log_buf, log_size, fmt, args); Don't need this log_len actually. Will send a v2. Thank you. -- To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to majordomo@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html Please read the FAQ at http://www.tux.org/lkml/
diff --git a/kernel/bpf/syscall.c b/kernel/bpf/syscall.c index 687dd6c..3a0e4e7 100644 --- a/kernel/bpf/syscall.c +++ b/kernel/bpf/syscall.c @@ -574,6 +574,32 @@ struct bpf_prog *bpf_prog_get(u32 ufd) } EXPORT_SYMBOL_GPL(bpf_prog_get); +static void +bpf_prog_load_note(union bpf_attr *attr, const char *fmt, ...) +{ + u32 log_level, log_size, log_len; + char __user *log_ubuf = NULL; + /* 64 chars should be long enough for a one line note. */ + char log_buf[64]; + va_list args; + + log_ubuf = (char __user *) (unsigned long) attr->log_buf; + log_level = attr->log_level; + log_size = sizeof(log_buf); + if (attr->log_size < log_size) + log_size = attr->log_size; + + if (log_level == 0 || !log_size || !log_ubuf) + return; + + va_start(args, fmt); + log_len = vscnprintf(log_buf, log_size, fmt, args); + va_end(args); + + /* Don't need to care the copying result too much */ + copy_to_user(log_ubuf, log_buf, log_size); +} + /* last field in 'union bpf_attr' used by this command */ #define BPF_PROG_LOAD_LAST_FIELD kern_version @@ -597,12 +623,19 @@ static int bpf_prog_load(union bpf_attr *attr) /* eBPF programs must be GPL compatible to use GPL-ed functions */ is_gpl = license_is_gpl_compatible(license); - if (attr->insn_cnt >= BPF_MAXINSNS) + if (attr->insn_cnt >= BPF_MAXINSNS) { + bpf_prog_load_note(attr, "Too many instructions: %d > %d\n", + attr->insn_cnt, BPF_MAXINSNS); return -EINVAL; + } if (type == BPF_PROG_TYPE_KPROBE && - attr->kern_version != LINUX_VERSION_CODE) + attr->kern_version != LINUX_VERSION_CODE) { + bpf_prog_load_note(attr, + "Kernel version mismatch: 0x%x != 0x%x\n", + attr->kern_version, LINUX_VERSION_CODE); return -EINVAL; + } if (type != BPF_PROG_TYPE_SOCKET_FILTER && !capable(CAP_SYS_ADMIN)) return -EPERM; @@ -631,8 +664,10 @@ static int bpf_prog_load(union bpf_attr *attr) /* find program type: socket_filter vs tracing_filter */ err = find_prog_type(type, prog); - if (err < 0) + if (err < 0) { + bpf_prog_load_note(attr, "Invalid program type: %d\n", type); goto free_prog; + } /* run eBPF verifier */ err = bpf_check(&prog, attr);
Many reason can make bpf_prog_load() return EINVAL. This patch utilizes logbuf passed from user to deliver the actual reason of failure. Without this patch, people is easy to forget fixing the "version" section in their BPF objects. Signed-off-by: Wang Nan <wangnan0@huawei.com> Cc: Alexei Starovoitov <ast@kernel.org> Cc: Arnaldo Carvalho de Melo <acme@redhat.com> Cc: David S. Miller <davem@davemloft.net> --- kernel/bpf/syscall.c | 41 ++++++++++++++++++++++++++++++++++++++--- 1 file changed, 38 insertions(+), 3 deletions(-) -- 1.8.3.4 -- To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to majordomo@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html Please read the FAQ at http://www.tux.org/lkml/