From patchwork Mon Oct 26 07:13:08 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Wang Nan X-Patchwork-Id: 55522 Delivered-To: patch@linaro.org Received: by 10.112.59.35 with SMTP id w3csp1033082lbq; Mon, 26 Oct 2015 00:13:57 -0700 (PDT) X-Received: by 10.66.66.196 with SMTP id h4mr19957453pat.113.1445843637838; Mon, 26 Oct 2015 00:13:57 -0700 (PDT) Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id mz8si51228510pbc.89.2015.10.26.00.13.57; Mon, 26 Oct 2015 00:13:57 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1753235AbbJZHN4 (ORCPT + 28 others); Mon, 26 Oct 2015 03:13:56 -0400 Received: from szxga01-in.huawei.com ([58.251.152.64]:4076 "EHLO szxga01-in.huawei.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752844AbbJZHNy (ORCPT ); Mon, 26 Oct 2015 03:13:54 -0400 Received: from 172.24.1.51 (EHLO szxeml427-hub.china.huawei.com) ([172.24.1.51]) by szxrg01-dlp.huawei.com (MOS 4.3.7-GA FastPath queued) with ESMTP id CXU89750; Mon, 26 Oct 2015 15:13:32 +0800 (CST) Received: from linux-4hy3.site (10.107.193.248) by szxeml427-hub.china.huawei.com (10.82.67.182) with Microsoft SMTP Server id 14.3.235.1; Mon, 26 Oct 2015 15:13:19 +0800 From: Wang Nan To: , CC: , , , Wang Nan , Arnaldo Carvalho de Melo , "David S. Miller" , Wu Fengguang Subject: [net-next PATCHv2] bpf: Output error message to logbuf when loading failure Date: Mon, 26 Oct 2015 07:13:08 +0000 Message-ID: <1445843588-143137-1-git-send-email-wangnan0@huawei.com> X-Mailer: git-send-email 1.8.3.4 In-Reply-To: <1445841394-142865-1-git-send-email-wangnan0@huawei.com> References: <1445841394-142865-1-git-send-email-wangnan0@huawei.com> MIME-Version: 1.0 X-Originating-IP: [10.107.193.248] X-CFilter-Loop: Reflected Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Many reasons can make bpf_prog_load() return EINVAL. This patch utilizes logbuf to deliver the actual reason of the failure. Without this patch, it is very easy for user to pass an object with "version" section not match the kernel version code, and the problem is hard to determine from return code (EINVAL). Signed-off-by: Wang Nan Cc: Alexei Starovoitov Cc: Arnaldo Carvalho de Melo Cc: David S. Miller Cc: Wu Fengguang --- kernel/bpf/syscall.c | 45 ++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 42 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/ diff --git a/kernel/bpf/syscall.c b/kernel/bpf/syscall.c index 687dd6c..719d0cb 100644 --- a/kernel/bpf/syscall.c +++ b/kernel/bpf/syscall.c @@ -17,6 +17,7 @@ #include #include #include +#include int sysctl_unprivileged_bpf_disabled __read_mostly; @@ -574,6 +575,35 @@ 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; + 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); + vscnprintf(log_buf, log_size, fmt, args); + va_end(args); + log_buf[sizeof(log_buf) - 1] = '\0'; + + /* Don't need care the copying result too much */ + WARN(copy_to_user(log_ubuf, log_buf, log_size), + KERN_WARNING "Failed to copy BPF error note '%s' to log buffer\n", + log_buf); +} + /* last field in 'union bpf_attr' used by this command */ #define BPF_PROG_LOAD_LAST_FIELD kern_version @@ -597,12 +627,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 +668,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);