From patchwork Wed Aug 14 03:30:10 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kui-Feng Lee X-Patchwork-Id: 13762810 X-Patchwork-Delegate: bpf@iogearbox.net Received: from mail-yw1-f174.google.com (mail-yw1-f174.google.com [209.85.128.174]) (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 886F248CCD for ; Wed, 14 Aug 2024 03:30:22 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.174 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723606224; cv=none; b=g76cOIS7ROB6Yfn4V3U7q+FfYQ/mcwQwStleY3XZfIAwIdnboCprmd0C42agL0JXe4iiyJsMuWORWHgVdc6Uvfic2kpthDj908jj3w4CSRebZtk6Slnt6WZFALhPLEg3kogjsTbyNceD1wZLi7/1qJg1kilb82qiDlfBa3mE96o= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723606224; c=relaxed/simple; bh=D34mojUig8vZuHPyC8TaZRR0VKlsSdjtAlsD/lb7vLI=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=VKEBOOVmCgJ0WmKsrpdu56gL26ZNy910rMm2B1QsGHY1xuvYvKddO1MlYOn/TdXoQtZNu8A/2UN6jgj312MGbrEGDlamLzP+1teQCXUIFItO9oPeChceFe8DbcAiZTUvigUyix4htT9j6wLfwlOhCuNso4eA9syixAp9GZnH7Hc= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=kwH/NqxO; arc=none smtp.client-ip=209.85.128.174 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="kwH/NqxO" Received: by mail-yw1-f174.google.com with SMTP id 00721157ae682-699d8dc6744so4169967b3.0 for ; Tue, 13 Aug 2024 20:30:22 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1723606221; x=1724211021; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=T3GlU5Fpsfnwd7Yq8PAloGm0P5P3SvGYUSsiYTJ2NW0=; b=kwH/NqxOLqRYPpgEIoQLolGIT3MImB3i96S7sa+ZlaI8AVLtguJcEBqgreI24yFosf vt4w/OkC9NDZfi55Z4kn/dLPv5Ms53+AaqizglVIeXYdIN8kZpCk74rfEfEvQoHKrzgj RRePNR0pKC8Mdkil4S3QN+9Dn73717ug5vbxocL9I/0evuzlylY3yxr9mxvMN+V1CG87 VmYYDhZTrFHd2yPZ5RGCHDtCMI3KLUt6N9xD1x535joL9t8B4vBYb07v4BfIQdwhdyJi 6RSxKJ/ANTv6Nbo7/e85J9ThUc9HQRGdwrTiYNcV3rgsIrXDAEOXfeXQQfhscGs09pYw WrZw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1723606221; x=1724211021; 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=T3GlU5Fpsfnwd7Yq8PAloGm0P5P3SvGYUSsiYTJ2NW0=; b=XrZbIADwiyrBYmOGRjCgmbsNzPw2pRczIy7wM7iJy1qdQfSIghFTAqJYrnyfbZRdi1 0MLhgOCHI8MDvItFMeWf187QYNYhH+hVEYRUqYN+4ujDZN1CmuoIp025TYYyLzyXoNDS gB/Fj5UVqBT0CCPIZdTc+LyEGBB8jOcDBrP0OpuTVCxP009+DxMDjz/zYNg2NzZ6e/kV g1Rgl3wzIZq7v7bKyjCiafjUKmpfBnGysjNtTvmtsh8gqSOc2rcx8yi4SXlViMbRdYgI nfwaR97L2WoVOdYqv+VxHEqGWsgUCCOWKHABAxZhPWP9w++6O7yZ4WnWJ98IkoiQmaFZ SWMg== X-Gm-Message-State: AOJu0YwUniUAjpDcJLdOMg+PbtrxgUyCpG66bOfB7ZtcmI8dcjh8h1D8 ykdEXlAMfs1quVqhyv06yUZxaVnoSylnT3QvYKUze+wfi2/xtDKXHIaPdamN X-Google-Smtp-Source: AGHT+IG1fN46HQC9uLwSXiCb7uAWRd38nA2uQ8LTbRKgs0aXPusyQ4q8ASS3xuzsnANY2vfkEKMthQ== X-Received: by 2002:a05:690c:b16:b0:62f:60db:326 with SMTP id 00721157ae682-6a9e9c39937mr46499737b3.20.1723606221328; Tue, 13 Aug 2024 20:30:21 -0700 (PDT) Received: from kickker.attlocal.net ([2600:1700:6cf8:1240:3c23:99cc:16a9:8b68]) by smtp.gmail.com with ESMTPSA id 00721157ae682-6a0a451b597sm15109587b3.117.2024.08.13.20.30.20 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 13 Aug 2024 20:30:20 -0700 (PDT) From: Kui-Feng Lee To: bpf@vger.kernel.org, ast@kernel.org, martin.lau@linux.dev, song@kernel.org, kernel-team@meta.com, andrii@kernel.org Cc: sinquersw@gmail.com, kuifeng@meta.com, Kui-Feng Lee Subject: [RFC bpf-next v3 7/7] selftests/bpf: test __uptr on the value of a task storage map. Date: Tue, 13 Aug 2024 20:30:10 -0700 Message-Id: <20240814033010.2980635-8-thinker.li@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240814033010.2980635-1-thinker.li@gmail.com> References: <20240814033010.2980635-1-thinker.li@gmail.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 X-Patchwork-State: RFC Make sure the memory of uptrs have been mapped to the kernel properly. Also ensure the values of uptrs in the kernel haven't been copied to userspace. Signed-off-by: Kui-Feng Lee --- .../bpf/prog_tests/task_local_storage.c | 106 ++++++++++++++++++ .../selftests/bpf/progs/task_ls_uptr.c | 65 +++++++++++ 2 files changed, 171 insertions(+) create mode 100644 tools/testing/selftests/bpf/progs/task_ls_uptr.c diff --git a/tools/testing/selftests/bpf/prog_tests/task_local_storage.c b/tools/testing/selftests/bpf/prog_tests/task_local_storage.c index c33c05161a9e..5709b083021c 100644 --- a/tools/testing/selftests/bpf/prog_tests/task_local_storage.c +++ b/tools/testing/selftests/bpf/prog_tests/task_local_storage.c @@ -5,6 +5,7 @@ #include #include #include +#include #include /* For SYS_xxx definitions */ #include #include @@ -14,6 +15,20 @@ #include "task_ls_recursion.skel.h" #include "task_storage_nodeadlock.skel.h" +struct user_data { + int a; + int b; + int result; +}; + +struct value_type { + struct user_data *udata; +}; + +#define MAGIC_VALUE 0xabcd1234 + +#include "task_ls_uptr.skel.h" + static void test_sys_enter_exit(void) { struct task_local_storage *skel; @@ -40,6 +55,95 @@ static void test_sys_enter_exit(void) task_local_storage__destroy(skel); } +static struct user_data user_data __attribute__((aligned(16))) = { + .a = 1, + .b = 2, +}; + +static void test_uptr(void) +{ + struct task_ls_uptr *skel = NULL; + int task_fd = -1, ev_fd = -1; + struct value_type value; + int err, wstatus; + __u64 dummy = 1; + pid_t pid; + + value.udata = &user_data; + + task_fd = sys_pidfd_open(getpid(), 0); + if (!ASSERT_NEQ(task_fd, -1, "sys_pidfd_open")) + goto out; + + ev_fd = eventfd(0, 0); + if (!ASSERT_NEQ(ev_fd, -1, "eventfd")) + goto out; + + skel = task_ls_uptr__open_and_load(); + if (!ASSERT_OK_PTR(skel, "skel_open_and_load")) + goto out; + + err = bpf_map_update_elem(bpf_map__fd(skel->maps.datamap), &task_fd, &value, 0); + if (!ASSERT_OK(err, "update_datamap")) + exit(1); + + err = task_ls_uptr__attach(skel); + if (!ASSERT_OK(err, "skel_attach")) + goto out; + + fflush(stdout); + fflush(stderr); + + pid = fork(); + if (pid < 0) + goto out; + + /* Call syscall in the child process, but access the map value of + * the parent process in the BPF program to check if the user kptr + * is translated/mapped correctly. + */ + if (pid == 0) { + /* child */ + + /* Overwrite the user_data in the child process to check if + * the BPF program accesses the user_data of the parent. + */ + user_data.a = 0; + user_data.b = 0; + + /* Wait for the parent to set child_pid */ + read(ev_fd, &dummy, sizeof(dummy)); + + exit(0); + } + + skel->bss->parent_pid = syscall(SYS_gettid); + skel->bss->child_pid = pid; + + write(ev_fd, &dummy, sizeof(dummy)); + + err = waitpid(pid, &wstatus, 0); + ASSERT_EQ(err, pid, "waitpid"); + skel->bss->child_pid = 0; + + ASSERT_EQ(MAGIC_VALUE + user_data.a + user_data.b, + user_data.result, "result"); + + /* Check if user programs can access the value of user kptrs + * through bpf_map_lookup_elem(). Make sure the kernel value is not + * leaked. + */ + err = bpf_map_lookup_elem(bpf_map__fd(skel->maps.datamap), &task_fd, &value); + if (!ASSERT_OK(err, "bpf_map_lookup_elem")) + goto out; + ASSERT_EQ(value.udata, NULL, "lookup_udata"); + +out: + task_ls_uptr__destroy(skel); + close(ev_fd); + close(task_fd); +} + static void test_exit_creds(void) { struct task_local_storage_exit_creds *skel; @@ -237,4 +341,6 @@ void test_task_local_storage(void) test_recursion(); if (test__start_subtest("nodeadlock")) test_nodeadlock(); + if (test__start_subtest("uptr")) + test_uptr(); } diff --git a/tools/testing/selftests/bpf/progs/task_ls_uptr.c b/tools/testing/selftests/bpf/progs/task_ls_uptr.c new file mode 100644 index 000000000000..473e6890d522 --- /dev/null +++ b/tools/testing/selftests/bpf/progs/task_ls_uptr.c @@ -0,0 +1,65 @@ +// SPDX-License-Identifier: GPL-2.0 +/* Copyright (c) 2024 Meta Platforms, Inc. and affiliates. */ + +#include "vmlinux.h" +#include +#include +#include "task_kfunc_common.h" + +char _license[] SEC("license") = "GPL"; + +struct user_data { + int a; + int b; + int result; +}; + +struct value_type { + struct user_data __uptr *udata; +}; + +struct { + __uint(type, BPF_MAP_TYPE_TASK_STORAGE); + __uint(map_flags, BPF_F_NO_PREALLOC); + __type(key, int); + __type(value, struct value_type); +} datamap SEC(".maps"); + +#define MAGIC_VALUE 0xabcd1234 + +/* This is a workaround to avoid clang generating a forward reference for + * struct user_data. This is a known issue and will be fixed in the future. + */ +struct user_data __dummy; + +pid_t child_pid = 0; +pid_t parent_pid = 0; + +SEC("tp_btf/sys_enter") +int BPF_PROG(on_enter, struct pt_regs *regs, long id) +{ + struct task_struct *task, *data_task; + struct value_type *ptr; + struct user_data *udata; + + task = bpf_get_current_task_btf(); + if (task->pid != child_pid) + return 0; + + data_task = bpf_task_from_pid(parent_pid); + if (!data_task) + return 0; + + ptr = bpf_task_storage_get(&datamap, data_task, 0, + BPF_LOCAL_STORAGE_GET_F_CREATE); + bpf_task_release(data_task); + if (!ptr) + return 0; + + udata = ptr->udata; + if (!udata) + return 0; + udata->result = MAGIC_VALUE + udata->a + udata->b; + + return 0; +}