From patchwork Thu Mar 25 21:11:21 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Toke_H=C3=B8iland-J=C3=B8rgensen?= X-Patchwork-Id: 12164995 X-Patchwork-Delegate: bpf@iogearbox.net 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=-19.0 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS, USER_AGENT_GIT 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 722CCC433E0 for ; Thu, 25 Mar 2021 21:12:42 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 4570A61A3C for ; Thu, 25 Mar 2021 21:12:42 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230313AbhCYVMG (ORCPT ); Thu, 25 Mar 2021 17:12:06 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]:49241 "EHLO us-smtp-delivery-124.mimecast.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230329AbhCYVLq (ORCPT ); Thu, 25 Mar 2021 17:11:46 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1616706706; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=TNs0UXcUiaKuVEx3mnWkq8N4wvcF6O32GQhcLyoLTFo=; b=A7AHh29DtKEFcNbGoXWL7BqoyE9daMP6Ak7b/aJewoev00mKJWUs0sOczkmKexjWA3TBRo lCS+wz8/jw/V/E75SYEbnqm1CBi3zRaEbqZHAzh8Nga/lAKvm11SUWh0/BSgFeMXJb09Fz zDOp/ck2+3GFdeWfHMCoMf2CMv09eO4= Received: from mail-ed1-f72.google.com (mail-ed1-f72.google.com [209.85.208.72]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-376-4v9OKOZgPp-LEXHK4bWGYg-1; Thu, 25 Mar 2021 17:11:43 -0400 X-MC-Unique: 4v9OKOZgPp-LEXHK4bWGYg-1 Received: by mail-ed1-f72.google.com with SMTP id f9so3310696edd.13 for ; Thu, 25 Mar 2021 14:11:43 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=TNs0UXcUiaKuVEx3mnWkq8N4wvcF6O32GQhcLyoLTFo=; b=TnW5D687WvSpsH1Q/6LIdGy2S6IcWi2T49MkLdGqkPnvmeGKSVTsdcLyUyBHsmDvf5 mzUfTIzq5aFQpvaomA/9CkIL5lt1UWbZf8rewQfjofeAnmo5354PzQSExRZQJz0KUYHI 0+/th8G0REZVuuaKeJ9+me73tB6GD4UyEYAf7I/jjv8e5zZtEonaTuQziF3l9M2u3n3r fdWINusHMB9vvIDMnKiTVpy3zgbn759APff/NAaqbGANNfNFPVAZcgkYxA7FLF6w+30s WN3cnzOef2lKlQ0k36ckX/Z/G8cU5KLotmnXn83hB1iLCU9y/ZxwYSxsccHLzAi+SEcG PE9w== X-Gm-Message-State: AOAM530EGbDdNhQFzVi6G9oUxWzDOVq9+pIf06RYsNR+l9x7kSw9z/sr McU11t0Zm92CwFDX60L9IrEjBnvWCuFD01SeoOFpsKJWvOJYHZtPlwylaHcBsFh5p2O0bbTzrVv Bj3Leasn31b70 X-Received: by 2002:a05:6402:19a:: with SMTP id r26mr11191659edv.44.1616706702147; Thu, 25 Mar 2021 14:11:42 -0700 (PDT) X-Google-Smtp-Source: ABdhPJx1FteeQQVFdTUCTr+XxdOclFOaKp764+qfSaHVpvq+RfJpExeEgB2GKDjn/XIsjUepv/bonA== X-Received: by 2002:a05:6402:19a:: with SMTP id r26mr11191617edv.44.1616706701660; Thu, 25 Mar 2021 14:11:41 -0700 (PDT) Received: from alrua-x1.borgediget.toke.dk ([2a0c:4d80:42:443::2]) by smtp.gmail.com with ESMTPSA id u1sm3163116edv.90.2021.03.25.14.11.40 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 25 Mar 2021 14:11:41 -0700 (PDT) Received: by alrua-x1.borgediget.toke.dk (Postfix, from userid 1000) id 851171801A3; Thu, 25 Mar 2021 22:11:40 +0100 (CET) From: =?utf-8?q?Toke_H=C3=B8iland-J=C3=B8rgensen?= To: Alexei Starovoitov , Daniel Borkmann Cc: =?utf-8?q?Toke_H=C3=B8iland-J=C3=B8rgensen?= , Andrii Nakryiko , Martin KaFai Lau , Song Liu , Yonghong Song , John Fastabend , KP Singh , Stanislav Fomichev , "David S. Miller" , Jesper Dangaard Brouer , Andrea Arcangeli , Clark Williams , bpf@vger.kernel.org, netdev@vger.kernel.org Subject: [PATCH bpf v2 1/2] bpf: enforce that struct_ops programs be GPL-only Date: Thu, 25 Mar 2021 22:11:21 +0100 Message-Id: <20210325211122.98620-1-toke@redhat.com> X-Mailer: git-send-email 2.31.0 MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: bpf@vger.kernel.org X-Patchwork-Delegate: bpf@iogearbox.net With the introduction of the struct_ops program type, it became possible to implement kernel functionality in BPF, making it viable to use BPF in place of a regular kernel module for these particular operations. Thus far, the only user of this mechanism is for implementing TCP congestion control algorithms. These are clearly marked as GPL-only when implemented as modules (as seen by the use of EXPORT_SYMBOL_GPL for tcp_register_congestion_control()), so it seems like an oversight that this was not carried over to BPF implementations. Since this is the only user of the struct_ops mechanism, just enforcing GPL-only for the struct_ops program type seems like the simplest way to fix this. v2: Move check to the top of check_struct_ops_btf_id(). Fixes: 0baf26b0fcd7 ("bpf: tcp: Support tcp_congestion_ops in bpf") Acked-by: Martin KaFai Lau Signed-off-by: Toke Høiland-Jørgensen --- kernel/bpf/verifier.c | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c index 44e4ec1640f1..3a738724a380 100644 --- a/kernel/bpf/verifier.c +++ b/kernel/bpf/verifier.c @@ -12158,6 +12158,11 @@ static int check_struct_ops_btf_id(struct bpf_verifier_env *env) u32 btf_id, member_idx; const char *mname; + if (!prog->gpl_compatible) { + verbose(env, "struct ops programs must have a GPL compatible license\n"); + return -EINVAL; + } + btf_id = prog->aux->attach_btf_id; st_ops = bpf_struct_ops_find(btf_id); if (!st_ops) { From patchwork Thu Mar 25 21:11:22 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Toke_H=C3=B8iland-J=C3=B8rgensen?= X-Patchwork-Id: 12164997 X-Patchwork-Delegate: bpf@iogearbox.net 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=-16.2 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS, UNWANTED_LANGUAGE_BODY,USER_AGENT_GIT 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 D0BB3C433E4 for ; Thu, 25 Mar 2021 21:12:42 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id A06F461A4A for ; Thu, 25 Mar 2021 21:12:42 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230366AbhCYVMH (ORCPT ); Thu, 25 Mar 2021 17:12:07 -0400 Received: from us-smtp-delivery-124.mimecast.com ([216.205.24.124]:28249 "EHLO us-smtp-delivery-124.mimecast.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230296AbhCYVLv (ORCPT ); Thu, 25 Mar 2021 17:11:51 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1616706711; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=jlGDo/RcYeA5m4cect+KpXswi7G/nCn2CqYsQ+YjVg0=; b=Z9ebUxyFw0xUh9YNm1p735Ybb8H+VAFU4qCLAgmpwFtZe6YfdhyADRorrU0Y6XP141DIBo RHGD6iYtN3DbdoH75Vzpfhet1zllAoZhY0vsCV104pBValS6xibf4YcwmjcjnQ911FQkUt oMOVJRJY/wx3UA0SDdpguCrBEmiIwZo= Received: from mail-ej1-f70.google.com (mail-ej1-f70.google.com [209.85.218.70]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-443-_214n4EjOtWcYe36NkLWjg-1; Thu, 25 Mar 2021 17:11:48 -0400 X-MC-Unique: _214n4EjOtWcYe36NkLWjg-1 Received: by mail-ej1-f70.google.com with SMTP id gv58so3177356ejc.6 for ; Thu, 25 Mar 2021 14:11:48 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=jlGDo/RcYeA5m4cect+KpXswi7G/nCn2CqYsQ+YjVg0=; b=JNgu6GtddJpH/0XTcrbEulv1cMgcrRa3L0tGFNhwY8gdjM2ewLjwKzWxfTJxMGCq1R kBAlBXO0tkJXMEhnvMwaYgBE7UWIlX1hJW6kGbhW5H2g82XV0JX/HH/4NPgC+o9G19BN nVZvYZo44VZClStJGR9RllKvX0ghLfJvDK7PCS9zbC1S757qao9Xj4AXSM1y1cAWDGtW OxQEhaj7KJgdAwPkggCT4RtD8gzN3Qt2iWD6ETzujYls/IK1Sd4yzqKxThDeETOiE408 qhuiaPtb0GGYITvryGZP+dKpR+eNDj3v+8BCUppVzDsIqbiwpn9mbq+/6kaseoZOqejJ 1Z9w== X-Gm-Message-State: AOAM530I8XCB1bdL309wJejt5dJGfT3ocWzJxa15VKPj7t0Vx40ybwer EMFqkeviVIkaRXmc6XsddMyZVtbSdjXUCy2kvSNkvlf7I+KBjK5yyaGskc0f8OGg35HqmfFPgRs CW0OD4dWfTYhT X-Received: by 2002:a17:907:b06:: with SMTP id h6mr11721525ejl.144.1616706706828; Thu, 25 Mar 2021 14:11:46 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzV91xj7stafOjFdpFlgWeWMBqeffBoCSoVClDRD7zgxzF+eMvN86Tz0UAKzI4F3xrZuBWPHg== X-Received: by 2002:a17:907:b06:: with SMTP id h6mr11721476ejl.144.1616706706281; Thu, 25 Mar 2021 14:11:46 -0700 (PDT) Received: from alrua-x1.borgediget.toke.dk ([2a0c:4d80:42:443::2]) by smtp.gmail.com with ESMTPSA id k12sm3208706edo.50.2021.03.25.14.11.45 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 25 Mar 2021 14:11:45 -0700 (PDT) Received: by alrua-x1.borgediget.toke.dk (Postfix, from userid 1000) id 59DD11801A3; Thu, 25 Mar 2021 22:11:45 +0100 (CET) From: =?utf-8?q?Toke_H=C3=B8iland-J=C3=B8rgensen?= To: Alexei Starovoitov , Daniel Borkmann Cc: =?utf-8?q?Toke_H=C3=B8iland-J=C3=B8rgensen?= , Andrii Nakryiko , Martin KaFai Lau , Song Liu , Yonghong Song , John Fastabend , KP Singh , Stanislav Fomichev , "David S. Miller" , Jesper Dangaard Brouer , Andrea Arcangeli , Clark Williams , bpf@vger.kernel.org, netdev@vger.kernel.org Subject: [PATCH bpf v2 2/2] bpf/selftests: test that kernel rejects a TCP CC with an invalid license Date: Thu, 25 Mar 2021 22:11:22 +0100 Message-Id: <20210325211122.98620-2-toke@redhat.com> X-Mailer: git-send-email 2.31.0 In-Reply-To: <20210325211122.98620-1-toke@redhat.com> References: <20210325211122.98620-1-toke@redhat.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: bpf@vger.kernel.org X-Patchwork-Delegate: bpf@iogearbox.net This adds a selftest to check that the verifier rejects a TCP CC struct_ops with a non-GPL license. v2: - Use a minimal struct_ops BPF program instead of rewriting bpf_dctcp's license in memory. - Check for the verifier reject message instead of just the return code. Signed-off-by: Toke Høiland-Jørgensen Acked-by: Martin KaFai Lau --- .../selftests/bpf/prog_tests/bpf_tcp_ca.c | 44 +++++++++++++++++++ .../selftests/bpf/progs/bpf_nogpltcp.c | 19 ++++++++ 2 files changed, 63 insertions(+) create mode 100644 tools/testing/selftests/bpf/progs/bpf_nogpltcp.c diff --git a/tools/testing/selftests/bpf/prog_tests/bpf_tcp_ca.c b/tools/testing/selftests/bpf/prog_tests/bpf_tcp_ca.c index 37c5494a0381..a09c716528e1 100644 --- a/tools/testing/selftests/bpf/prog_tests/bpf_tcp_ca.c +++ b/tools/testing/selftests/bpf/prog_tests/bpf_tcp_ca.c @@ -6,6 +6,7 @@ #include #include "bpf_dctcp.skel.h" #include "bpf_cubic.skel.h" +#include "bpf_nogpltcp.skel.h" #define min(a, b) ((a) < (b) ? (a) : (b)) @@ -227,10 +228,53 @@ static void test_dctcp(void) bpf_dctcp__destroy(dctcp_skel); } +static char *err_str = NULL; +static bool found = false; + +static int libbpf_debug_print(enum libbpf_print_level level, + const char *format, va_list args) +{ + char *log_buf; + + if (level != LIBBPF_WARN || + strcmp(format, "libbpf: \n%s\n")) { + vprintf(format, args); + return 0; + } + + log_buf = va_arg(args, char *); + if (!log_buf) + goto out; + if (err_str && strstr(log_buf, err_str) != NULL) + found = true; +out: + printf(format, log_buf); + return 0; +} + +static void test_invalid_license(void) +{ + libbpf_print_fn_t old_print_fn = NULL; + struct bpf_nogpltcp *skel; + + err_str = "struct ops programs must have a GPL compatible license"; + old_print_fn = libbpf_set_print(libbpf_debug_print); + + skel = bpf_nogpltcp__open_and_load(); + if (CHECK(skel, "bpf_nogplgtcp__open_and_load()", "didn't fail\n")) + bpf_nogpltcp__destroy(skel); + + CHECK(!found, "errmsg check", "expected string '%s'", err_str); + + libbpf_set_print(old_print_fn); +} + void test_bpf_tcp_ca(void) { if (test__start_subtest("dctcp")) test_dctcp(); if (test__start_subtest("cubic")) test_cubic(); + if (test__start_subtest("invalid_license")) + test_invalid_license(); } diff --git a/tools/testing/selftests/bpf/progs/bpf_nogpltcp.c b/tools/testing/selftests/bpf/progs/bpf_nogpltcp.c new file mode 100644 index 000000000000..2ecd833dcd41 --- /dev/null +++ b/tools/testing/selftests/bpf/progs/bpf_nogpltcp.c @@ -0,0 +1,19 @@ +// SPDX-License-Identifier: GPL-2.0 + +#include +#include +#include +#include +#include "bpf_tcp_helpers.h" + +char _license[] SEC("license") = "X"; + +void BPF_STRUCT_OPS(nogpltcp_init, struct sock *sk) +{ +} + +SEC(".struct_ops") +struct tcp_congestion_ops bpf_nogpltcp = { + .init = (void *)nogpltcp_init, + .name = "bpf_nogpltcp", +};