From patchwork Fri Mar 26 10:03:13 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: 12166211 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, URIBL_BLOCKED,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 4907DC433DB for ; Fri, 26 Mar 2021 10:04:24 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 0286661A49 for ; Fri, 26 Mar 2021 10:04:23 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229779AbhCZKDv (ORCPT ); Fri, 26 Mar 2021 06:03:51 -0400 Received: from us-smtp-delivery-124.mimecast.com ([216.205.24.124]:54858 "EHLO us-smtp-delivery-124.mimecast.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229573AbhCZKDc (ORCPT ); Fri, 26 Mar 2021 06:03:32 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1616753010; 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=AqbkoNQ8XoV32TJQbt2vnYtr9D9KJMnunF7xga9JKcE=; b=DQBVCCcoVTdgOZBVEkGh5nU5EEjC6wagiAbAI5g230sQQSIGoogFBhugHH9kB9Xr3h9kLB ZgU8p1rOEQ+YhUC/JfzffAD74mVGFLyFf39/ivXiTgpesSkvuWlLKvCWKZL5xmWGl5Ux/d yQ6+kI5f9Ccrk7c0GMXshxSVJF37Avs= Received: from mail-ej1-f71.google.com (mail-ej1-f71.google.com [209.85.218.71]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-347-6X5yPobHO9m6gJb0NrTZcg-1; Fri, 26 Mar 2021 06:03:28 -0400 X-MC-Unique: 6X5yPobHO9m6gJb0NrTZcg-1 Received: by mail-ej1-f71.google.com with SMTP id h14so3878002ejg.7 for ; Fri, 26 Mar 2021 03:03:28 -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=AqbkoNQ8XoV32TJQbt2vnYtr9D9KJMnunF7xga9JKcE=; b=iAdvc5/zfDjMyCYHK11s2c4P9rKL80IIHULw9cycKbZ8H96IEsyFqikzUU2Wc5Ncdw ournA8efHK4ddK/rQfElgBSrp/4nZLdDgj89ncP0R9M6coarUaIaBsxbZ487tFTTKFP1 X865VoZXo7E+x3oEOYhZBp9szK4y5kT3ubcvwOW2NnUPqSPbvxHX5R74GFKWWXIkSbLi 2QWZ6+X1tke9M+zdcVGAx933JH0D/wespn/lI8tccWytW4VqtMKRwfpTnkl0CrJxUJ6n jnwlx/0OJjAUQBBvQ/b0NIDsrv2nw/AmeRb6DV0RW7opn+awikgWEmPc5G7khyHTyOYT 0baw== X-Gm-Message-State: AOAM5336MRITTN8h8M88aE+x0bVQMC/n9+Y8ipwGm5dk1VbPT8eubJDF JBRrCpcJp2XT4I8Tve1v+B3AvjFC00zWCMvU21yo7Uu18EYhq89R0y9RHwxRjfxpTQaXlINiST6 413zU5QbFADe4 X-Received: by 2002:a17:906:495a:: with SMTP id f26mr14025450ejt.271.1616753007170; Fri, 26 Mar 2021 03:03:27 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzJhgy8ubAo4D3bF1qJvgBXod5/y0iR6kIN45MclrY380CjRxzFYjZIQ+0veKTNTIh10Qo7tw== X-Received: by 2002:a17:906:495a:: with SMTP id f26mr14025408ejt.271.1616753006772; Fri, 26 Mar 2021 03:03:26 -0700 (PDT) Received: from alrua-x1.borgediget.toke.dk ([2a0c:4d80:42:443::2]) by smtp.gmail.com with ESMTPSA id 90sm3943863edf.31.2021.03.26.03.03.25 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 26 Mar 2021 03:03:26 -0700 (PDT) Received: by alrua-x1.borgediget.toke.dk (Postfix, from userid 1000) id 4E1221801A3; Fri, 26 Mar 2021 11:03:25 +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 v3 1/2] bpf: enforce that struct_ops programs be GPL-only Date: Fri, 26 Mar 2021 11:03:13 +0100 Message-Id: <20210326100314.121853-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. v3: No change 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 Fri Mar 26 10:03:14 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: 12166209 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,URIBL_BLOCKED,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 698F5C433C1 for ; Fri, 26 Mar 2021 10:04:23 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 2412F61A42 for ; Fri, 26 Mar 2021 10:04:23 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229573AbhCZKDv (ORCPT ); Fri, 26 Mar 2021 06:03:51 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]:38416 "EHLO us-smtp-delivery-124.mimecast.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229744AbhCZKDc (ORCPT ); Fri, 26 Mar 2021 06:03:32 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1616753011; 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=1aHJVxe6uoiZMEQjmNfP9XIXtXVFqt+4EqtqxEvdyy8=; b=TkT1IvVPcgYHJlTHpcZ8W4F63ilV8VU7W672ocOblJPCfWZrumH/CndlDj8adBPdD6TfhP r/6Q8rLc+npFAF911WmLoINl3DjsfFm+jPWJW1tjUsXUwpdwzU27QEajMX1N6tRFzbjsa7 Ku5jMEKcPhHdNt1BDh7JmFMZ9zQj3hQ= Received: from mail-ej1-f72.google.com (mail-ej1-f72.google.com [209.85.218.72]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-240-ZP91R_PEPLaMbj04m1qjcA-1; Fri, 26 Mar 2021 06:03:29 -0400 X-MC-Unique: ZP91R_PEPLaMbj04m1qjcA-1 Received: by mail-ej1-f72.google.com with SMTP id h14so3878023ejg.7 for ; Fri, 26 Mar 2021 03:03:29 -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=1aHJVxe6uoiZMEQjmNfP9XIXtXVFqt+4EqtqxEvdyy8=; b=XnI6VSJwUY9EA0xCWi+R/qTZ2YexY632PAW/BUzZh27pAy2r0qUIybW+UgqtICniTI NdFz80NJmJv5h/qmhIWhf+YQptM0md5AbZlZkVReQNZrdMQcNIX5kbMLUe4e5kvryozB dcttoc6jOxAIss+KyuvBlf1KLI02PQX4T4yUcCb6XhLtXgfPMh9RkMPIJxUe+ek1667Z 2fft7L1/fS6P85Ds3lS/8H1rkrXerNiKgc7E32wb+/IFgOqqPKnENOIq7OwWAK2XIuNn sVnB7sd1RCijpLvivj0r0MurA+52hhaSqY1mWjNtyXrg82FfCP+iYILUFBxznvUj4JX4 Utjw== X-Gm-Message-State: AOAM531LUwv4PN+qRq/eJ2R0udo/kIw2HN7btMnKV8pFrY8ZLZ1mebov 0ClKrGkuKwtetL5XFj9PRhdWBtHUi++tloJWti2D39yDBFliH1Z7yNAONIdZIWOje8CRQu4K1Pf ZRUwUOGtGIW6O X-Received: by 2002:a05:6402:30a2:: with SMTP id df2mr13726751edb.29.1616753008829; Fri, 26 Mar 2021 03:03:28 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzb5MKbcXT1DxlGqWXsp03h4Wp2942dAkC02B5NcAWh+ValK3Ii7qW6U4P+GYnG/0O2VD3F8A== X-Received: by 2002:a05:6402:30a2:: with SMTP id df2mr13726723edb.29.1616753008628; Fri, 26 Mar 2021 03:03:28 -0700 (PDT) Received: from alrua-x1.borgediget.toke.dk ([45.145.92.2]) by smtp.gmail.com with ESMTPSA id v22sm3629988ejj.103.2021.03.26.03.03.28 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 26 Mar 2021 03:03:28 -0700 (PDT) Received: by alrua-x1.borgediget.toke.dk (Postfix, from userid 1000) id AA4CE1801A3; Fri, 26 Mar 2021 11:03:27 +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 v3 2/2] bpf/selftests: test that kernel rejects a TCP CC with an invalid license Date: Fri, 26 Mar 2021 11:03:14 +0100 Message-Id: <20210326100314.121853-2-toke@redhat.com> X-Mailer: git-send-email 2.31.0 In-Reply-To: <20210326100314.121853-1-toke@redhat.com> References: <20210326100314.121853-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. v3: - Rename prog to bpf_tcp_nogpl - Use ASSERT macros instead of CHECK - Skip unneeded initialisation, unconditionally close skeleton 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. Acked-by: Martin KaFai Lau Signed-off-by: Toke Høiland-Jørgensen --- .../selftests/bpf/prog_tests/bpf_tcp_ca.c | 44 +++++++++++++++++++ .../selftests/bpf/progs/bpf_tcp_nogpl.c | 19 ++++++++ 2 files changed, 63 insertions(+) create mode 100644 tools/testing/selftests/bpf/progs/bpf_tcp_nogpl.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..e25917f04602 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_tcp_nogpl.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; +static bool found; + +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; + struct bpf_tcp_nogpl *skel; + + err_str = "struct ops programs must have a GPL compatible license"; + found = false; + old_print_fn = libbpf_set_print(libbpf_debug_print); + + skel = bpf_tcp_nogpl__open_and_load(); + ASSERT_NULL(skel, "bpf_tcp_nogpl"); + ASSERT_EQ(found, true, "expected_err_msg"); + + bpf_tcp_nogpl__destroy(skel); + 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_tcp_nogpl.c b/tools/testing/selftests/bpf/progs/bpf_tcp_nogpl.c new file mode 100644 index 000000000000..2ecd833dcd41 --- /dev/null +++ b/tools/testing/selftests/bpf/progs/bpf_tcp_nogpl.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", +};