From patchwork Thu Aug 4 07:11:30 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sargun Dhillon X-Patchwork-Id: 9262799 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 82CA360839 for ; Thu, 4 Aug 2016 07:12:37 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 72490280F4 for ; Thu, 4 Aug 2016 07:12:37 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 6773628319; Thu, 4 Aug 2016 07:12:37 +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=unavailable 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 F3FA1280F4 for ; Thu, 4 Aug 2016 07:12:36 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1753236AbcHDHLh (ORCPT ); Thu, 4 Aug 2016 03:11:37 -0400 Received: from mail-it0-f53.google.com ([209.85.214.53]:37764 "EHLO mail-it0-f53.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752904AbcHDHLc (ORCPT ); Thu, 4 Aug 2016 03:11:32 -0400 Received: by mail-it0-f53.google.com with SMTP id f6so252838076ith.0 for ; Thu, 04 Aug 2016 00:11:32 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sargun.me; s=google; h=date:from:to:cc:subject:message-id:mime-version:content-disposition :user-agent; bh=z6xQJD8CZm1o2xQTe5Hpz8noUW6p1qTmdLB5AZlwNbI=; b=XUXzMS62IqhSBKyC8t1vagLF2etNTnsbdalkd0LUKQEvOP2l4u0e5GOVZtgYoAoxKB BcqIZxrmzztQ/Qw3+VXvTrttL+Va3MU82czLFvkUFy1vZSElNvudsl9aCBclhn3a6JOM 5UiGU0t8/8Mw/pAuMSr9+ZvKdzs/PrjQvvEfo= 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:cc:subject:message-id:mime-version :content-disposition:user-agent; bh=z6xQJD8CZm1o2xQTe5Hpz8noUW6p1qTmdLB5AZlwNbI=; b=TjtV2CsAwfZ0BU4YG0iFHkguiikJzeO505rlHOXuVSmvIIzMZy7EoRfYTLoJ/ATj0h cfSsRCfBDK+5hOHVwTK7QFko7VFcKTVdW3reEc3LBhMAZPhctdnNxlYJ+i5aU89U6rqP T9b1N6OmG1NT63FFhiOOy/x5DNkW4/QkEKvoOrmacyHg3RvtATDEM8dnKghcD9mqOQrS SSIyOkKIE3MJaGLQuNbbult1fR4IR+D3/nVhRgZwMB/ym9PuZrZrSuxerUqF5oTpqFsM PUoKSy0GF6KkFcFTj8f/+7LtP5gVPMiegdWoi/hlnMCILz0jQzxTOJvNwbjzGe06/Pte 7Kpw== X-Gm-Message-State: AEkooutcR7FLpZb80/2Ln5rq9eNV6uy7Gf0rgLAnDbSftKOQe7ZW2LN38NeEuxguWgAEkQ== X-Received: by 10.36.77.209 with SMTP id l200mr34761178itb.17.1470294692057; Thu, 04 Aug 2016 00:11:32 -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 b135sm5234200ioa.13.2016.08.04.00.11.31 (version=TLS1_2 cipher=AES128-SHA bits=128/128); Thu, 04 Aug 2016 00:11:31 -0700 (PDT) Date: Thu, 4 Aug 2016 00:11:30 -0700 From: Sargun Dhillon To: linux-kernel@vger.kernel.org Cc: alexei.starovoitov@gmail.com, daniel@iogearbox.net, linux-security-module@vger.kernel.org, netdev@vger.kernel.org Subject: [RFC 1/4] bpf: move tracing helpers to shared helpers Message-ID: <20160804071128.GA19115@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) {