From patchwork Wed Oct 30 00:14:57 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Juntong Deng X-Patchwork-Id: 13855794 X-Patchwork-Delegate: bpf@iogearbox.net Received: from EUR02-AM0-obe.outbound.protection.outlook.com (mail-am0eur02olkn2020.outbound.protection.outlook.com [40.92.49.20]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 9A26264A; Wed, 30 Oct 2024 00:18:04 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=40.92.49.20 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730247486; cv=fail; b=iAwZW1OZpAkaDIXnMX/ivtsJ1DcoCCYgjVTe4YH2krtWqE7HOXNrGjPU++Fnm0hGnJtliNjAcr10apG91BjxLYc6fPjx0mCpAFITvVGolbiccc0z7lK0Fy31ZsR1ucGZExQafMVgNYpx8ge/qBYo3pXwXBCEtdli1RLmxIIIJVM= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730247486; c=relaxed/simple; bh=gZ4ZsfrXjLE/euxDXbRqSbrJN/oeswBzWeE0ZYDKq9c=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: Content-Type:MIME-Version; b=qN8AftBM7WS+012z6tE1wFVLVEJuMaWVt4CvzZ6dyeYnU3NSwHxf620Rs9S6IUnRh3/PdCoDwDDeuPBOa96zIl3p+R/ozKBwBp1PDxkn+anC93YwEakqwy7maq9W2mbToG+LNEQdKztIHKqZSD8mwjKty7jfQpa6+QQiZPbc6Ok= ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=outlook.com; spf=pass smtp.mailfrom=outlook.com; dkim=pass (2048-bit key) header.d=outlook.com header.i=@outlook.com header.b=esxPko0r; arc=fail smtp.client-ip=40.92.49.20 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=outlook.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=outlook.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=outlook.com header.i=@outlook.com header.b="esxPko0r" ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=eJsKc+uVQ+NdOt+xBVfEgEmURg8fNbVGiiHaokv3/sMZB/nGPL+/unb0ZP1RndQVcxPnRvpD/rtrkMdQTnEkB+GsKEgUWQkIOneeYRq/UAMSb+tfhxDaBIjHaju2rrcFF5+PStd7+HyiorYhaTpFXJ6Pyzp9Ii7txEYBWoHFvIbIFlSlSCAwivD7ll6faG0K0VMjq7cvc2HfDDLPciB9dcwBc/0xhXUjnnrvd3Wd7ceQO4gJdJpr6KTv3zikRQ337gd0PFZMljyoogKBE+hT+XSHEqezFbobm4JJhMK60PwTQEdARzDAuG+HBKuhUmbtR3P1ixX7BSeQrbNjdP9bJA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector10001; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=fqMLYqY7WZmPsDuyp6cFamd6LLYvH1cd3WsR6pnyRM8=; b=X9pnpVo5XA+C3RPxD8es4VC+7nM3DIOZCHi3TL9x0D41QjpCTmYzm46gWu4ER0I6dGy695DdF2yie11h18TOhQXQG3jq9ebSAmkIRA6IYjXAaodNB41tdRk2JLJm0lVlJ4+H0UyEsDe4nqeaLa2x1S7qALMULNhaS7zNOk9/dvMmKbNW+v286+t3w1KjwcQpeN+1rcqdydHi+IUK/3uacpWHdKOuobF/O3EY1P555Fb6UdGFjPkBKxeQ6dQwaf7yidNi9I3xYiwS5DzU7Mu0B/TOk/vSxIOCudwPMedRQoXQ4PWHfZa15FOsF8FshYmqf3YykROlAN5pjNZuaUiHkw== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=none; dmarc=none; dkim=none; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=outlook.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=fqMLYqY7WZmPsDuyp6cFamd6LLYvH1cd3WsR6pnyRM8=; b=esxPko0rNyAPSaEUKAl0FRkpx1zTDKcFEs+QdvDvlX7h4WgCYBbDQYDC6I9gzccshg3L3JKOtZla7IXQ1uVTIx2xd3C0HPuYxDZ3sjoDyB3kc2ZWPGflAco7LQuvUE7cTOo7F0/UIShZ++fmDuPbY2rqRTc05jAV7w8XYmQ4Rjt+R/1o0+EBu+7VoUi7hE2AleIbPG+ybZ0aBzVjfv4eAvVKEt5Et/UD9i2nFu7hDeJjKhZvuesDLNdG0rf/RN1Id730TbzYCWIK3avNpqdcr6gKMfch4/Bv7Mc+O2a+Z8iUcZDtixMVXnUKkJhJ2QjAAOHTG4u8K3429QIuQo+OcQ== Received: from AM6PR03MB5848.eurprd03.prod.outlook.com (2603:10a6:20b:e4::10) by DB4PR03MB10105.eurprd03.prod.outlook.com (2603:10a6:10:3ff::13) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.8093.27; Wed, 30 Oct 2024 00:18:01 +0000 Received: from AM6PR03MB5848.eurprd03.prod.outlook.com ([fe80::4b97:bbdb:e0ac:6f7]) by AM6PR03MB5848.eurprd03.prod.outlook.com ([fe80::4b97:bbdb:e0ac:6f7%4]) with mapi id 15.20.8093.027; Wed, 30 Oct 2024 00:18:01 +0000 From: Juntong Deng To: ast@kernel.org, daniel@iogearbox.net, john.fastabend@gmail.com, andrii@kernel.org, martin.lau@linux.dev, eddyz87@gmail.com, song@kernel.org, yonghong.song@linux.dev, kpsingh@kernel.org, sdf@fomichev.me, haoluo@google.com, jolsa@kernel.org, memxor@gmail.com, snorcht@gmail.com Cc: bpf@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH bpf-next v2 4/4] selftests/bpf: Add tests for struct file related CRIB kfuncs Date: Wed, 30 Oct 2024 00:14:57 +0000 Message-ID: X-Mailer: git-send-email 2.39.5 In-Reply-To: References: X-ClientProxiedBy: LO4P265CA0238.GBRP265.PROD.OUTLOOK.COM (2603:10a6:600:350::11) To AM6PR03MB5848.eurprd03.prod.outlook.com (2603:10a6:20b:e4::10) X-Microsoft-Original-Message-ID: <20241030001457.15593-4-juntong.deng@outlook.com> Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: AM6PR03MB5848:EE_|DB4PR03MB10105:EE_ X-MS-Office365-Filtering-Correlation-Id: e28593f2-66e3-488a-9c45-08dcf87850a8 X-Microsoft-Antispam: BCL:0;ARA:14566002|5072599009|19110799003|461199028|8060799006|15080799006|3412199025|440099028; X-Microsoft-Antispam-Message-Info: rE13zP6L6sZws2d2GCHrna2V7LtIP3obH71GLAxBSCmUmIDlLk2IYX1TMZIkaemIquGAuLT3a8FbMFo37s94Uhvkn/YJyQs8J12bbK348D71l4UixoC1vAljClzdV8VWtLRqreDT8QY1jwe57Y3uZA74XqMHC1MG5aqg6ITR8Z0OmMyURKF8/cKL9tBGJfUzncZjRsPeaqf3/xho6g3RnNSNM4dODPUrN/ecjHahxkRiwYPF8V04fc4CONvM6cR/Sz9+R//hn2xMgHBBA2F0slc9IJdL5RyKMfx54hXILQriRil5ey+rr/a8T1M2ghbChLXP4SDCaYDxhMGJ8hEwKY0JoET0f687UPr8pWmj0MI/Ge1whhcYI2e/4+L5HMcX5Q0f+ZVIgtfcwdRmxt+4hQ1ObFSdh9/e0xKL3NaNts+SOX4KS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JPMaJRcINqjKy/NBkwZ28As9MVXtZMseYreddJEP2XNgji5yYr8rtD/VobG2W248alYis9k0m06DEwIdwSHxyiqoTtMfwLDGIXuVsedO+2q7PBe3OECW6yIMgCAKlbs2XsWu77MAfI/ceuN4pYdq0X/EnmDs79aNW9uc1kSJyZHofRDkNCBU3f7xHPr+5FZuXHr0GInreMr63OZQuCj7FRFk3+jz2eBzU5DE07BTo8qH/y35mbL5FUbPXS7+sQFpkj6bZOf+xaN6VjuxwvBIMNryNHH6PX2lOOCs0/gqO3sd1bzOFc0LKfRvfNPehlWPhwl4H3C6pN7d4uALGCDyP+TjpxJJIF86sbteVBvcZaE7dxF8tDLahjFvcyg8LPYw6Z4sTcQN2tNRodZkCwKekI6jMA/GzjSAQ5vZs8rmGATTk1Z9huGwHXX0p0qTt+50TaP5HDFsVRwWj/3pq+PQCAWyQlNtT4/WWDxWxhB8zsfcOJQEIbZkBRrH01+ba/TZyUYDd6MsxgKN7YvQHhpEq/BL0RRdRhBDM1dXZldMSJop6LySIu5ZcKicdIi9YCfQo+VQWvFnbegddphItU55sa6YRGfRAVzrTTkrIwHf/JmB8g4bLJ+iGC0BCkeCT1xV32jXJc0LWbHsJF1O5wpEv1H0U3hTXYw+FZY5f+V2ZCY7b213XP8OYS0i4LwzWEO2ueo2qaQLJONUf0/VeesUaSKFCfSLiNsDuBqTFpLM8Gl8BzCsRpqbyiPuIvI/bVPl1iYtze2RLDSe6wKLmggc0TxOz88w7sYgzVLhf7xohyl26cUi0CDuyxyQzwmwPgf8uwhaqi37Lg+s5a4F35Y2IR6eV5jsA8bL4VvAdsH/pezHrxYxjVpILDG9N7i0Gay7u X-OriginatorOrg: outlook.com X-MS-Exchange-CrossTenant-Network-Message-Id: e28593f2-66e3-488a-9c45-08dcf87850a8 X-MS-Exchange-CrossTenant-AuthSource: AM6PR03MB5848.eurprd03.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 30 Oct 2024 00:18:01.4801 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 84df9e7f-e9f6-40af-b435-aaaaaaaaaaaa X-MS-Exchange-CrossTenant-RMS-PersistedConsumerOrg: 00000000-0000-0000-0000-000000000000 X-MS-Exchange-Transport-CrossTenantHeadersStamped: DB4PR03MB10105 X-Patchwork-Delegate: bpf@iogearbox.net This patch adds test cases for struct file related CRIB kfuncs. The test case for bpf_fget_task() is written based on files_test_process(), so there should only be 3 opened files, corresponding to file descriptors 0, 1, 2. bpf_get_file_ops_type() currently only returns FILE_OPS_UNKNOWN, so no test cases are needed for now. In addition, this patch adds failure test cases where bpf programs cannot pass the verifier due to untrusted pointer arguments. Signed-off-by: Juntong Deng --- tools/testing/selftests/bpf/prog_tests/crib.c | 1 + .../testing/selftests/bpf/progs/crib_common.h | 4 ++ .../selftests/bpf/progs/crib_files_failure.c | 22 +++++++++ .../selftests/bpf/progs/crib_files_success.c | 46 +++++++++++++++++++ 4 files changed, 73 insertions(+) diff --git a/tools/testing/selftests/bpf/prog_tests/crib.c b/tools/testing/selftests/bpf/prog_tests/crib.c index 48c5156504ad..5ef887e43170 100644 --- a/tools/testing/selftests/bpf/prog_tests/crib.c +++ b/tools/testing/selftests/bpf/prog_tests/crib.c @@ -108,6 +108,7 @@ static void run_files_success_test(const char *prog_name) static const char * const files_success_tests[] = { "test_bpf_iter_task_file", + "test_bpf_fget_task", }; void test_crib(void) diff --git a/tools/testing/selftests/bpf/progs/crib_common.h b/tools/testing/selftests/bpf/progs/crib_common.h index 93b8f9b1bdf8..0bc77d1b02b2 100644 --- a/tools/testing/selftests/bpf/progs/crib_common.h +++ b/tools/testing/selftests/bpf/progs/crib_common.h @@ -18,4 +18,8 @@ extern struct file *bpf_iter_task_file_next(struct bpf_iter_task_file *it) __ksy extern int bpf_iter_task_file_get_fd(struct bpf_iter_task_file *it__iter) __ksym; extern void bpf_iter_task_file_destroy(struct bpf_iter_task_file *it) __ksym; +extern struct file *bpf_fget_task(struct task_struct *task, unsigned int fd) __ksym; +extern unsigned int bpf_get_file_ops_type(struct file *file) __ksym; +extern void bpf_put_file(struct file *file) __ksym; + #endif /* __CRIB_COMMON_H */ diff --git a/tools/testing/selftests/bpf/progs/crib_files_failure.c b/tools/testing/selftests/bpf/progs/crib_files_failure.c index ebae01d87ff9..9360aad50c15 100644 --- a/tools/testing/selftests/bpf/progs/crib_files_failure.c +++ b/tools/testing/selftests/bpf/progs/crib_files_failure.c @@ -84,3 +84,25 @@ int bpf_iter_task_file_destroy_uninit_iter(void *ctx) return 0; } + +SEC("syscall") +__failure __msg("Possibly NULL pointer passed to trusted arg0") +int bpf_fget_task_untrusted_file(void *ctx) +{ + struct task_struct *task = NULL; + + bpf_fget_task(task, 1); + + return 0; +} + +SEC("syscall") +__failure __msg("Possibly NULL pointer passed to trusted arg0") +int bpf_get_file_ops_type_untrusted_file(void *ctx) +{ + struct file *file = NULL; + + bpf_get_file_ops_type(file); + + return 0; +} diff --git a/tools/testing/selftests/bpf/progs/crib_files_success.c b/tools/testing/selftests/bpf/progs/crib_files_success.c index 92ca7d9d44c3..8dddac6a7378 100644 --- a/tools/testing/selftests/bpf/progs/crib_files_success.c +++ b/tools/testing/selftests/bpf/progs/crib_files_success.c @@ -71,3 +71,49 @@ int test_bpf_iter_task_file(void *ctx) bpf_task_release(task); return 0; } + +SEC("syscall") +int test_bpf_fget_task(void *ctx) +{ + struct task_struct *task; + struct file *file; + + task = bpf_task_from_vpid(pid); + if (task == NULL) { + err = 1; + return 0; + } + + file = bpf_fget_task(task, 0); + if (file == NULL) { + err = 2; + goto cleanup; + } + + bpf_put_file(file); + + file = bpf_fget_task(task, 1); + if (file == NULL) { + err = 3; + goto cleanup; + } + + bpf_put_file(file); + + file = bpf_fget_task(task, 2); + if (file == NULL) { + err = 4; + goto cleanup; + } + + bpf_put_file(file); + + file = bpf_fget_task(task, 3); + if (file != NULL) { + err = 5; + bpf_put_file(file); + } +cleanup: + bpf_task_release(task); + return 0; +}