From patchwork Tue Aug 2 23:26:56 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sargun Dhillon X-Patchwork-Id: 9260479 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 437D26077C for ; Tue, 2 Aug 2016 23:27:19 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 331CC28508 for ; Tue, 2 Aug 2016 23:27:19 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 2803728521; Tue, 2 Aug 2016 23:27:19 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.8 required=2.0 tests=BAYES_00,DKIM_SIGNED, RCVD_IN_DNSWL_HI,T_DKIM_INVALID autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id AD67D28508 for ; Tue, 2 Aug 2016 23:27:17 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751409AbcHBX1O (ORCPT ); Tue, 2 Aug 2016 19:27:14 -0400 Received: from mail-io0-f171.google.com ([209.85.223.171]:33403 "EHLO mail-io0-f171.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1753779AbcHBX1M (ORCPT ); Tue, 2 Aug 2016 19:27:12 -0400 Received: by mail-io0-f171.google.com with SMTP id 38so227516933iol.0 for ; Tue, 02 Aug 2016 16:26:59 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sargun.me; s=google; h=date:from:to:subject:message-id:mime-version:content-disposition :user-agent; bh=z6xQJD8CZm1o2xQTe5Hpz8noUW6p1qTmdLB5AZlwNbI=; b=hkPO614niydP5bJbPkdZny0iuTEN/iPKMBbCmMNgtcK/oeyPiYS4tXxlw+m6QY1SVc TVZAZyFKoHj4qxVQ9fhP7KOTYw27xujlN1eRJ41+aRNvB1vmqKiJlRFKHNbBaBFrfl2u YBkgvSkhEfazxAHltVdD6vGRiB6A85Yd4OKJk= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:date:from:to:subject:message-id:mime-version :content-disposition:user-agent; bh=z6xQJD8CZm1o2xQTe5Hpz8noUW6p1qTmdLB5AZlwNbI=; b=MCt6K5Zm1rdhv84HSGjGmrgNHPJ6cBBbsILUPirA7b/uyF5+kYQMkpA42YtjmdrOFQ DD57ntq0RuFq/CNrScRTpuW1FiBLKtqkak+mFq01BxSfCjtniQE8iNb+bMN+Y9lE23VY zM3RyPJG30zc+hedAOGjX9y9AyBH6gOJ3s9vP0SeCxXN6TUbCGymsI8xujgtZYZ0asAO 1LJ0E/G0QMJyYDlLpaLx6MoHqHKzQmaX6Uphcb19mkH80+zkeOqE9lta9kDu/gBQP6RM aTak49cHw6cr7BsHqoNaPM04ohBsBl+BKUPxSFTvegMNus0BHKgJ6F3qHeeZvckPzgPP b6sA== X-Gm-Message-State: AEkoouvoh9UhMCkNQz+15T4JjtKJgN/zulXP2yBmcTis4/QSxZsrIVK1xMPXcWhLyn9tEA== X-Received: by 10.107.22.66 with SMTP id 63mr67908975iow.195.1470180418231; Tue, 02 Aug 2016 16:26:58 -0700 (PDT) Received: from ircssh.c.rugged-nimbus-611.internal (55.145.251.23.bc.googleusercontent.com. [23.251.145.55]) by smtp.gmail.com with ESMTPSA id n128sm260294ith.1.2016.08.02.16.26.57 for (version=TLS1_2 cipher=AES128-SHA bits=128/128); Tue, 02 Aug 2016 16:26:58 -0700 (PDT) Date: Tue, 2 Aug 2016 16:26:56 -0700 From: Sargun Dhillon To: linux-security-module@vger.kernel.org Subject: [RFC 1/4] bpf: move tracing helpers to shared helpers Message-ID: <20160802232654.GA25284@ircssh.c.rugged-nimbus-611.internal> MIME-Version: 1.0 Content-Disposition: inline User-Agent: Mutt/1.5.21 (2010-09-15) Sender: owner-linux-security-module@vger.kernel.org Precedence: bulk List-ID: X-Virus-Scanned: ClamAV using ClamSMTP Move bpf_probe_read and bpf_get_current_task to the shared helpers so that Checmate can use them. Signed-off-by: Sargun Dhillon --- include/linux/bpf.h | 2 ++ kernel/bpf/helpers.c | 34 ++++++++++++++++++++++++++++++++++ kernel/trace/bpf_trace.c | 33 --------------------------------- 3 files changed, 36 insertions(+), 33 deletions(-) diff --git a/include/linux/bpf.h b/include/linux/bpf.h index 1113423..4e1fa57 100644 --- a/include/linux/bpf.h +++ b/include/linux/bpf.h @@ -314,6 +314,8 @@ extern const struct bpf_func_proto bpf_get_current_comm_proto; extern const struct bpf_func_proto bpf_skb_vlan_push_proto; extern const struct bpf_func_proto bpf_skb_vlan_pop_proto; extern const struct bpf_func_proto bpf_get_stackid_proto; +extern const struct bpf_func_proto bpf_get_current_task_proto; +extern const struct bpf_func_proto bpf_probe_read_proto; /* Shared helpers among cBPF and eBPF. */ void bpf_user_rnd_init_once(void); diff --git a/kernel/bpf/helpers.c b/kernel/bpf/helpers.c index 1ea3afb..c439afc 100644 --- a/kernel/bpf/helpers.c +++ b/kernel/bpf/helpers.c @@ -16,6 +16,7 @@ #include #include #include +#include /* If kernel subsystem is allowing eBPF programs to call this function, * inside its own verifier_ops->get_func_proto() callback it should return @@ -186,3 +187,36 @@ const struct bpf_func_proto bpf_get_current_comm_proto = { .arg1_type = ARG_PTR_TO_RAW_STACK, .arg2_type = ARG_CONST_STACK_SIZE, }; + +static u64 bpf_get_current_task(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5) +{ + return (long) current; +} + +const struct bpf_func_proto bpf_get_current_task_proto = { + .func = bpf_get_current_task, + .gpl_only = true, + .ret_type = RET_INTEGER, +}; + +static u64 bpf_probe_read(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5) +{ + void *dst = (void *) (long) r1; + int ret, size = (int) r2; + void *unsafe_ptr = (void *) (long) r3; + + ret = probe_kernel_read(dst, unsafe_ptr, size); + if (unlikely(ret < 0)) + memset(dst, 0, size); + + return ret; +} + +const struct bpf_func_proto bpf_probe_read_proto = { + .func = bpf_probe_read, + .gpl_only = true, + .ret_type = RET_INTEGER, + .arg1_type = ARG_PTR_TO_RAW_STACK, + .arg2_type = ARG_CONST_STACK_SIZE, + .arg3_type = ARG_ANYTHING, +}; diff --git a/kernel/trace/bpf_trace.c b/kernel/trace/bpf_trace.c index b20438f..f7a107b 100644 --- a/kernel/trace/bpf_trace.c +++ b/kernel/trace/bpf_trace.c @@ -59,28 +59,6 @@ unsigned int trace_call_bpf(struct bpf_prog *prog, void *ctx) } EXPORT_SYMBOL_GPL(trace_call_bpf); -static u64 bpf_probe_read(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5) -{ - void *dst = (void *) (long) r1; - int ret, size = (int) r2; - void *unsafe_ptr = (void *) (long) r3; - - ret = probe_kernel_read(dst, unsafe_ptr, size); - if (unlikely(ret < 0)) - memset(dst, 0, size); - - return ret; -} - -static const struct bpf_func_proto bpf_probe_read_proto = { - .func = bpf_probe_read, - .gpl_only = true, - .ret_type = RET_INTEGER, - .arg1_type = ARG_PTR_TO_RAW_STACK, - .arg2_type = ARG_CONST_STACK_SIZE, - .arg3_type = ARG_ANYTHING, -}; - static u64 bpf_probe_write_user(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5) { void *unsafe_ptr = (void *) (long) r1; @@ -365,17 +343,6 @@ u64 bpf_event_output(struct bpf_map *map, u64 flags, void *meta, u64 meta_size, return __bpf_perf_event_output(regs, map, flags, &raw); } -static u64 bpf_get_current_task(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5) -{ - return (long) current; -} - -static const struct bpf_func_proto bpf_get_current_task_proto = { - .func = bpf_get_current_task, - .gpl_only = true, - .ret_type = RET_INTEGER, -}; - static const struct bpf_func_proto *tracing_func_proto(enum bpf_func_id func_id) { switch (func_id) {