From patchwork Fri Apr 5 14:30:40 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Vernet X-Patchwork-Id: 13619113 X-Patchwork-Delegate: bpf@iogearbox.net Received: from mail-il1-f178.google.com (mail-il1-f178.google.com [209.85.166.178]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id D417B16F856; Fri, 5 Apr 2024 14:30:50 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.166.178 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712327452; cv=none; b=iBE2s96AVWRGKem1bP7Nxvqv4NRXerIRw3xDWlVl4QIRXhdoPNl+sXulBGHeboXi6/ZMMg5eo6puw8risuzNYis/60eVHdFVCsG4N9qLwtK9eyuy9FwZDUSXa6ood3fNeXRRkg3+lEMrn3G6S+7HfKRLWL/vg3YcrlNe7GzVX9w= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712327452; c=relaxed/simple; bh=LFtBoVTcSa0WoZ4hT+3gROUd9+4JFFOPzrtM0DkuNNI=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=UEHuyNSML4YODl/JXJMMHa3+vBbpa64BQ1IEXOTH06FBnJYmtUlLgd8XhvyOTKZN2JpHrNK60ly3DUh9L91yy2Lnp4gDK+PnuEynlbYldSBa6aB2FY0oVzpUYo5bGAxOiDZioifgEXUkEFh4hiQGtk9vZi9z99OOjKZaJvqSiak= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=manifault.com; spf=pass smtp.mailfrom=gmail.com; arc=none smtp.client-ip=209.85.166.178 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=manifault.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Received: by mail-il1-f178.google.com with SMTP id e9e14a558f8ab-36a0d09e6ffso3945665ab.0; Fri, 05 Apr 2024 07:30:50 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1712327450; x=1712932250; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=gfXRwZpB8GnNWQ39P82k01Aag/6u7hx54JnENyAlqhI=; b=tw/vgfu778Q5TiSnD2FxOi7fxj8n2JoOeLn1QEjUkRHC/F6Uq8LHwbMI8sW1e8DWDK V+jM9vglg699o3qTopkdneGQJ8Pc3E7UCDsJnzh3YCD2G8CKiTbNNCazLgfWL9XbmCep iEYpJ6P23TvKHM9XfGA7FJ4h+ZxUhUIEXEniTDH6IzV4Yj1mSujxI2yROeKKv0hSl66O VN/rN0nXdgL18Zwyg2tuYEi7FlfWohNollqDjjtWYgCSVXHJwfMxQQVDR7SZoHk+uMRY Y9yg+Qbq9tGll7KeKry5Lo1FZxnxEop740dSVXPVGxbjqW7Yt4b8zZurIIlG5s63eMWe t07Q== X-Forwarded-Encrypted: i=1; AJvYcCXKxAT4oroxKQt0DXdPnJcP5ISarYCLWu6+yWRkO/LQkqPgaBfUSWMY9KvbhYQwzW7N1z4jK+rIkm/vMcpLyivjyOnRuPQvN9+9UKSX X-Gm-Message-State: AOJu0Yz6GXAWJrMGwqYg1EEw0usTVyQe2cvn/y675nd+pXT1Jec9+3Zz lV9AX3EqolNGpfg4/Ei6MK77UTnb7+lorB+9juo7MyXikKS0AK+t9uQuHL5AfIM= X-Google-Smtp-Source: AGHT+IEz5IjsSZ0rwrqyeaT3VLrS3vSgQpobPv7oPx/vy/q5pxyJq5zwjafs/cRXnl7Zn6ITFBKQYA== X-Received: by 2002:a05:6e02:1ca6:b0:368:99a9:3f1c with SMTP id x6-20020a056e021ca600b0036899a93f1cmr1562352ill.9.1712327444873; Fri, 05 Apr 2024 07:30:44 -0700 (PDT) Received: from localhost (c-76-136-75-40.hsd1.il.comcast.net. [76.136.75.40]) by smtp.gmail.com with ESMTPSA id r11-20020a056638300b00b0047bed8cd9cesm584114jak.81.2024.04.05.07.30.44 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 05 Apr 2024 07:30:44 -0700 (PDT) From: David Vernet To: bpf@vger.kernel.org Cc: ast@kernel.org, daniel@iogearbox.net, andrii@kernel.org, martin.lau@linux.dev, song@kernel.org, yonghong.song@linux.dev, john.fastabend@gmail.com, kpsingh@kernel.org, sdf@google.com, haoluo@google.com, jolsa@kernel.org, linux-kernel@vger.kernel.org, kernel-team@meta.com Subject: [PATCH bpf-next v2 1/2] bpf: Allow invoking kfuncs from BPF_PROG_TYPE_SYSCALL progs Date: Fri, 5 Apr 2024 09:30:40 -0500 Message-ID: <20240405143041.632519-2-void@manifault.com> X-Mailer: git-send-email 2.44.0 In-Reply-To: <20240405143041.632519-1-void@manifault.com> References: <20240405143041.632519-1-void@manifault.com> Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Patchwork-Delegate: bpf@iogearbox.net Currently, a set of core BPF kfuncs (e.g. bpf_task_*, bpf_cgroup_*, bpf_cpumask_*, etc) cannot be invoked from BPF_PROG_TYPE_SYSCALL programs. The whitelist approach taken for enabling kfuncs makes sense: it not safe to call these kfuncs from every program type. For example, it may not be safe to call bpf_task_acquire() in an fentry to free_task(). BPF_PROG_TYPE_SYSCALL, on the other hand, is a perfectly safe program type from which to invoke these kfuncs, as it's a very controlled environment, and we should never be able to run into any of the typical problems such as recursive invoations, acquiring references on freeing kptrs, etc. Being able to invoke these kfuncs would be useful, as BPF_PROG_TYPE_SYSCALL can be invoked with BPF_PROG_RUN, and would therefore enable user space programs to synchronously call into BPF to manipulate these kptrs. This patch therefore enables invoking the aforementioned core kfuncs from BPF_PROG_TYPE_SYSCALL progs. Acked-by: Andrii Nakryiko Acked-by: Yonghong Song Signed-off-by: David Vernet --- kernel/bpf/cpumask.c | 1 + kernel/bpf/helpers.c | 1 + 2 files changed, 2 insertions(+) diff --git a/kernel/bpf/cpumask.c b/kernel/bpf/cpumask.c index dad0fb1c8e87..33c473d676a5 100644 --- a/kernel/bpf/cpumask.c +++ b/kernel/bpf/cpumask.c @@ -474,6 +474,7 @@ static int __init cpumask_kfunc_init(void) ret = bpf_mem_alloc_init(&bpf_cpumask_ma, sizeof(struct bpf_cpumask), false); ret = ret ?: register_btf_kfunc_id_set(BPF_PROG_TYPE_TRACING, &cpumask_kfunc_set); ret = ret ?: register_btf_kfunc_id_set(BPF_PROG_TYPE_STRUCT_OPS, &cpumask_kfunc_set); + ret = ret ?: register_btf_kfunc_id_set(BPF_PROG_TYPE_SYSCALL, &cpumask_kfunc_set); return ret ?: register_btf_id_dtor_kfuncs(cpumask_dtors, ARRAY_SIZE(cpumask_dtors), THIS_MODULE); diff --git a/kernel/bpf/helpers.c b/kernel/bpf/helpers.c index d9e7aca8ae9e..8cde717137bd 100644 --- a/kernel/bpf/helpers.c +++ b/kernel/bpf/helpers.c @@ -2653,6 +2653,7 @@ static int __init kfunc_init(void) ret = ret ?: register_btf_kfunc_id_set(BPF_PROG_TYPE_SCHED_CLS, &generic_kfunc_set); ret = ret ?: register_btf_kfunc_id_set(BPF_PROG_TYPE_XDP, &generic_kfunc_set); ret = ret ?: register_btf_kfunc_id_set(BPF_PROG_TYPE_STRUCT_OPS, &generic_kfunc_set); + ret = ret ?: register_btf_kfunc_id_set(BPF_PROG_TYPE_SYSCALL, &generic_kfunc_set); ret = ret ?: register_btf_id_dtor_kfuncs(generic_dtors, ARRAY_SIZE(generic_dtors), THIS_MODULE); From patchwork Fri Apr 5 14:30:41 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Vernet X-Patchwork-Id: 13619112 X-Patchwork-Delegate: bpf@iogearbox.net Received: from mail-io1-f52.google.com (mail-io1-f52.google.com [209.85.166.52]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 8D37116F287; Fri, 5 Apr 2024 14:30:47 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.166.52 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712327449; cv=none; b=m0/ogDpwOJjr5h4s6T11g7s3BINlJdZSMyOgLRWyuQrVVr8vCbQ6sm7Q65uNqPxho3hiPcNRsJLErJKaKX0FLN9kk52CB0p1aLadZhNEGQo+vDZCnhqsjD+sFcUm7fvJIR7aY5gzGc8h4IbwAeweI8MmmUII4BglEIhyt3hSQm8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712327449; c=relaxed/simple; bh=Af7RfGng24yIeWpBRm0kOdGB8o4WJWgS/TCB+Uarq9Y=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=GJRXXgUkUs/fhwZ3h3ZvEB+HbpV9ftN1ITsIQugmJU30y0/mhLDPrPNS3n0PlWqrI+c1ObRH6ciczAH5ipdzrr6HZKLH84ROYuv1DvYcJVCIJCq14bS0Mn+xZB3p5nzNc0hL/b0jFjZa+jg3Ffvu2k9BJaEseezXglTz6ed0Zb0= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=manifault.com; spf=pass smtp.mailfrom=gmail.com; arc=none smtp.client-ip=209.85.166.52 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=manifault.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Received: by mail-io1-f52.google.com with SMTP id ca18e2360f4ac-7d0486e3250so97924239f.1; Fri, 05 Apr 2024 07:30:47 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1712327446; x=1712932246; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=PRdyQGjH5h8G6pfTdtidr525YhFZdrjl1y7kv2e1qZU=; b=vHvUwNxf2DtwtBcgkk1RyYJC3jpG3OfbjyOIVKTMyKKENdCGVN9R7b8k9pEJ7Ar0YY olU+V+0BsvNNic/F5DZkq3WoSIc1GR3i7lOqYvW42wwA+zaC3tDTSnkue+o/d4TfVmnc 2l1Tm/r4VVk3xfYdojelUnlfGlNK1oTPGHrJ5vppDpkAZgAKlWEsgZpvLYT3IUvJD8hB ZwvihycTE3psYphLnpezGEEpYEepW1b9jlB8l8wfKbANXqSvR2VOeGwvaS9FnlqgrMM7 0CtuD+ovG1CRQdr3NHNWtAw6qSrZS4dHKt2Pf2Gg99CI37Zzc6hvJP50kflE8uezzwTw TAyw== X-Forwarded-Encrypted: i=1; AJvYcCUoOKwmU2ts2m0mi2fqCrzzsoz5rSYWJEmKI5slbGG4AiSwq8QCgfgXUzTPei0SEe0W8hxVrl+3QoxkBX3RtZ+cMJPr8Y1Mxxlbx499 X-Gm-Message-State: AOJu0YytjDwk9BukpZYWoEvqPD9k79ImpVwg1LHQs1N8HWeQXCokBjMM cm6VLf0JKj7sYEPngpLTwlr/YqY7qFBFe72U6yvYSTmELSoEazStVaVWVshWp6o= X-Google-Smtp-Source: AGHT+IHwCpC+nlq92i3i64MzB7M2JZOQULTAu4iHJxah0OkDaWK28l6hGw6L6Xzey5XCVLCxEJqL0w== X-Received: by 2002:a6b:e704:0:b0:7d3:3e0d:9daa with SMTP id b4-20020a6be704000000b007d33e0d9daamr1706139ioh.7.1712327446257; Fri, 05 Apr 2024 07:30:46 -0700 (PDT) Received: from localhost (c-76-136-75-40.hsd1.il.comcast.net. [76.136.75.40]) by smtp.gmail.com with ESMTPSA id x25-20020a6b6a19000000b007d35949850asm527741iog.26.2024.04.05.07.30.45 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 05 Apr 2024 07:30:45 -0700 (PDT) From: David Vernet To: bpf@vger.kernel.org Cc: ast@kernel.org, daniel@iogearbox.net, andrii@kernel.org, martin.lau@linux.dev, song@kernel.org, yonghong.song@linux.dev, john.fastabend@gmail.com, kpsingh@kernel.org, sdf@google.com, haoluo@google.com, jolsa@kernel.org, linux-kernel@vger.kernel.org, kernel-team@meta.com Subject: [PATCH bpf-next v2 2/2] selftests/bpf: Verify calling core kfuncs from BPF_PROG_TYPE_SYCALL Date: Fri, 5 Apr 2024 09:30:41 -0500 Message-ID: <20240405143041.632519-3-void@manifault.com> X-Mailer: git-send-email 2.44.0 In-Reply-To: <20240405143041.632519-1-void@manifault.com> References: <20240405143041.632519-1-void@manifault.com> Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Patchwork-Delegate: bpf@iogearbox.net Now that we can call some kfuncs from BPF_PROG_TYPE_SYSCALL progs, let's add some selftests that verify as much. As a bonus, let's also verify that we can't call the progs from raw tracepoints. Do do this, we add a new selftest suite called verifier_kfunc_prog_types. Acked-by: Yonghong Song Signed-off-by: David Vernet --- .../prog_tests/verifier_kfunc_prog_types.c | 11 ++ .../selftests/bpf/progs/cgrp_kfunc_common.h | 2 +- .../selftests/bpf/progs/task_kfunc_common.h | 2 +- .../bpf/progs/verifier_kfunc_prog_types.c | 119 ++++++++++++++++++ 4 files changed, 132 insertions(+), 2 deletions(-) create mode 100644 tools/testing/selftests/bpf/prog_tests/verifier_kfunc_prog_types.c create mode 100644 tools/testing/selftests/bpf/progs/verifier_kfunc_prog_types.c diff --git a/tools/testing/selftests/bpf/prog_tests/verifier_kfunc_prog_types.c b/tools/testing/selftests/bpf/prog_tests/verifier_kfunc_prog_types.c new file mode 100644 index 000000000000..3918ecc2ee91 --- /dev/null +++ b/tools/testing/selftests/bpf/prog_tests/verifier_kfunc_prog_types.c @@ -0,0 +1,11 @@ +// SPDX-License-Identifier: GPL-2.0 +/* Copyright (c) 2024 Meta Platforms, Inc. and affiliates. */ + +#include + +#include "verifier_kfunc_prog_types.skel.h" + +void test_verifier_kfunc_prog_types(void) +{ + RUN_TESTS(verifier_kfunc_prog_types); +} diff --git a/tools/testing/selftests/bpf/progs/cgrp_kfunc_common.h b/tools/testing/selftests/bpf/progs/cgrp_kfunc_common.h index 22914a70db54..73ba32e9a693 100644 --- a/tools/testing/selftests/bpf/progs/cgrp_kfunc_common.h +++ b/tools/testing/selftests/bpf/progs/cgrp_kfunc_common.h @@ -13,7 +13,7 @@ struct __cgrps_kfunc_map_value { struct cgroup __kptr * cgrp; }; -struct hash_map { +struct { __uint(type, BPF_MAP_TYPE_HASH); __type(key, int); __type(value, struct __cgrps_kfunc_map_value); diff --git a/tools/testing/selftests/bpf/progs/task_kfunc_common.h b/tools/testing/selftests/bpf/progs/task_kfunc_common.h index 41f2d44f49cb..6720c4b5be41 100644 --- a/tools/testing/selftests/bpf/progs/task_kfunc_common.h +++ b/tools/testing/selftests/bpf/progs/task_kfunc_common.h @@ -13,7 +13,7 @@ struct __tasks_kfunc_map_value { struct task_struct __kptr * task; }; -struct hash_map { +struct { __uint(type, BPF_MAP_TYPE_HASH); __type(key, int); __type(value, struct __tasks_kfunc_map_value); diff --git a/tools/testing/selftests/bpf/progs/verifier_kfunc_prog_types.c b/tools/testing/selftests/bpf/progs/verifier_kfunc_prog_types.c new file mode 100644 index 000000000000..b7f17c6b3443 --- /dev/null +++ b/tools/testing/selftests/bpf/progs/verifier_kfunc_prog_types.c @@ -0,0 +1,119 @@ +// SPDX-License-Identifier: GPL-2.0 +/* Copyright (c) 2024 Meta Platforms, Inc. and affiliates. */ + +#include +#include +#include + +#include "bpf_misc.h" +#include "cgrp_kfunc_common.h" +#include "cpumask_common.h" +#include "task_kfunc_common.h" + +char _license[] SEC("license") = "GPL"; + +/*************** + * Task kfuncs * + ***************/ + +static inline void task_kfunc_load_test(void) +{ + struct task_struct *current, *ref_1, *ref_2; + + current = bpf_get_current_task_btf(); + ref_1 = bpf_task_from_pid(current->pid); + if (!ref_1) + return; + + ref_2 = bpf_task_acquire(ref_1); + if (ref_2) + bpf_task_release(ref_2); + bpf_task_release(ref_1); +} + +SEC("raw_tp") +__failure __msg("calling kernel function") +int BPF_PROG(task_kfunc_raw_tp) +{ + task_kfunc_load_test(); + return 0; +} + +SEC("syscall") __success +int BPF_PROG(task_kfunc_syscall) +{ + task_kfunc_load_test(); + return 0; +} + +/***************** + * cgroup kfuncs * + *****************/ + +static inline void cgrp_kfunc_load_test(void) +{ + struct cgroup *cgrp, *ref; + + cgrp = bpf_cgroup_from_id(0); + if (!cgrp) + return; + + ref = bpf_cgroup_acquire(cgrp); + if (!ref) { + bpf_cgroup_release(cgrp); + return; + } + + bpf_cgroup_release(ref); + bpf_cgroup_release(cgrp); +} + +SEC("raw_tp") +__failure __msg("calling kernel function") +int BPF_PROG(cgrp_kfunc_raw_tp) +{ + cgrp_kfunc_load_test(); + return 0; +} + +SEC("syscall") __success +int BPF_PROG(cgrp_kfunc_syscall) +{ + cgrp_kfunc_load_test(); + return 0; +} + +/****************** + * cpumask kfuncs * + ******************/ + +static inline void cpumask_kfunc_load_test(void) +{ + struct bpf_cpumask *alloc, *ref; + + alloc = bpf_cpumask_create(); + if (!alloc) + return; + + ref = bpf_cpumask_acquire(alloc); + bpf_cpumask_set_cpu(0, alloc); + bpf_cpumask_test_cpu(0, (const struct cpumask *)ref); + + bpf_cpumask_release(ref); + bpf_cpumask_release(alloc); +} + +SEC("raw_tp") +__failure __msg("calling kernel function") +int BPF_PROG(cpumask_kfunc_raw_tp) +{ + cpumask_kfunc_load_test(); + return 0; +} + +SEC("syscall") __success +int BPF_PROG(cpumask_kfunc_syscall) +{ + cpumask_kfunc_load_test(); + return 0; +}