From patchwork Wed Sep 9 15:11:14 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jiri Olsa X-Patchwork-Id: 261256 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-10.1 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, INCLUDES_PATCH, MAILING_LIST_MULTI, SIGNED_OFF_BY, SPF_HELO_NONE, SPF_PASS autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 5012FC43461 for ; Wed, 9 Sep 2020 15:46:50 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id F02CE207DE for ; Wed, 9 Sep 2020 15:46:49 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1599666410; bh=Y6KVpwfWcgf1NpTlHdYhXIWOVqfIZ8yOIqI7b4HfiAs=; h=From:To:Cc:Subject:Date:List-ID:From; b=gYyb/S3cFmwGhFDZv5zlx9XcMgE+4IHVWZkM8Y3X9LRFPeTihQxGb6bJA8hqs/D4b yI4Rd1lL9g+H2w2zf/PhR9h5P5zNK/gTh2iYTu307sZuuEmjTvDM97sQ5DXawPwZcp 5S22XpgXBoAgMWDYDn/PUdNj34zFEtiETRvzioSM= Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1730336AbgIIPqi convert rfc822-to-8bit (ORCPT ); Wed, 9 Sep 2020 11:46:38 -0400 Received: from us-smtp-delivery-1.mimecast.com ([207.211.31.120]:49372 "EHLO us-smtp-1.mimecast.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1730462AbgIIPqS (ORCPT ); Wed, 9 Sep 2020 11:46:18 -0400 Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-574-GlHbXRhlPymod5mQwk-Pig-1; Wed, 09 Sep 2020 11:11:26 -0400 X-MC-Unique: GlHbXRhlPymod5mQwk-Pig-1 Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.phx2.redhat.com [10.5.11.22]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id D6D84801AE8; Wed, 9 Sep 2020 15:11:24 +0000 (UTC) Received: from krava.redhat.com (unknown [10.40.194.81]) by smtp.corp.redhat.com (Postfix) with ESMTP id 57AA9100238E; Wed, 9 Sep 2020 15:11:16 +0000 (UTC) From: Jiri Olsa To: Alexei Starovoitov , Daniel Borkmann Cc: Eelco Chaudron , netdev@vger.kernel.org, bpf@vger.kernel.org, Martin KaFai Lau , Song Liu , Yonghong Song , Andrii Nakryiko , John Fastabend , KP Singh , Jesper Dangaard Brouer , =?utf-8?q?Toke_H=C3=B8ilan?= =?utf-8?q?d-J=C3=B8rgensen?= Subject: [PATCH bpf-next 1/2] bpf: Fix context type resolving for extension programs Date: Wed, 9 Sep 2020 17:11:14 +0200 Message-Id: <20200909151115.1559418-1-jolsa@kernel.org> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.84 on 10.5.11.22 X-Mimecast-Spam-Score: 0.0 X-Mimecast-Originator: kernel.org Sender: netdev-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org Eelco reported we can't properly access arguments if the tracing program is attached to extension program. Having following program: SEC("classifier/test_pkt_md_access") int test_pkt_md_access(struct __sk_buff *skb) with its extension: SEC("freplace/test_pkt_md_access") int test_pkt_md_access_new(struct __sk_buff *skb) and tracing that extension with: SEC("fentry/test_pkt_md_access_new") int BPF_PROG(fentry, struct sk_buff *skb) It's not possible to access skb argument in the fentry program, with following error from verifier: ; int BPF_PROG(fentry, struct sk_buff *skb) 0: (79) r1 = *(u64 *)(r1 +0) invalid bpf_context access off=0 size=8 The problem is that btf_ctx_access gets the context type for the traced program, which is in this case the extension. But when we trace extension program, we want to get the context type of the program that the extension is attached to, so we can access the argument properly in the trace program. Reported-by: Eelco Chaudron Signed-off-by: Jiri Olsa --- kernel/bpf/btf.c | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/kernel/bpf/btf.c b/kernel/bpf/btf.c index f9ac6935ab3c..37ad01c32e5a 100644 --- a/kernel/bpf/btf.c +++ b/kernel/bpf/btf.c @@ -3859,6 +3859,14 @@ bool btf_ctx_access(int off, int size, enum bpf_access_type type, } info->reg_type = PTR_TO_BTF_ID; + + /* When we trace extension program, we want to get the context + * type of the program that the extension is attached to, so + * we can access the argument properly in the trace program. + */ + if (tgt_prog && tgt_prog->type == BPF_PROG_TYPE_EXT) + tgt_prog = tgt_prog->aux->linked_prog; + if (tgt_prog) { ret = btf_translate_to_vmlinux(log, btf, t, tgt_prog->type, arg); if (ret > 0) {