-
Notifications
You must be signed in to change notification settings - Fork 123
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
selftests/bpf: Add tests for bpf_fget_task() kfunc
This patch adds test cases for bpf_fget_task() kfunc. test_bpf_fget_task is used to test obtaining struct file based on the file descriptor in the current process. bpf_fget_task_null_task and bpf_fget_task_untrusted_task are used to test the failure cases of passing NULL or untrusted pointer as argument. Signed-off-by: Juntong Deng <[email protected]>
- Loading branch information
1 parent
570861d
commit b77bfa1
Showing
4 changed files
with
136 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
// SPDX-License-Identifier: GPL-2.0 | ||
|
||
#include "vmlinux.h" | ||
#include <bpf/bpf_tracing.h> | ||
#include <bpf/bpf_helpers.h> | ||
#include "bpf_misc.h" | ||
#include "bpf_experimental.h" | ||
|
||
char _license[] SEC("license") = "GPL"; | ||
|
||
SEC("syscall") | ||
__failure __msg("Possibly NULL pointer passed to trusted arg0") | ||
int bpf_fget_task_null_task(void *ctx) | ||
{ | ||
struct task_struct *task = NULL; | ||
|
||
bpf_fget_task(task, 1); | ||
|
||
return 0; | ||
} | ||
|
||
SEC("syscall") | ||
__failure __msg("R1 must be referenced or trusted") | ||
int bpf_fget_task_untrusted_task(void *ctx) | ||
{ | ||
struct task_struct *task; | ||
|
||
task = bpf_get_current_task_btf()->parent; | ||
|
||
bpf_fget_task(task, 1); | ||
|
||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
// SPDX-License-Identifier: GPL-2.0 | ||
|
||
#include "vmlinux.h" | ||
#include <bpf/bpf_tracing.h> | ||
#include <bpf/bpf_helpers.h> | ||
#include "bpf_misc.h" | ||
#include "bpf_experimental.h" | ||
#include "task_kfunc_common.h" | ||
|
||
char _license[] SEC("license") = "GPL"; | ||
|
||
int err, test_fd1, test_fd2; | ||
|
||
SEC("syscall") | ||
int test_bpf_fget_task(void *ctx) | ||
{ | ||
struct task_struct *task; | ||
struct file *file; | ||
|
||
task = bpf_get_current_task_btf(); | ||
if (task == NULL) { | ||
err = 1; | ||
return 0; | ||
} | ||
|
||
file = bpf_fget_task(task, test_fd1); | ||
if (file == NULL) { | ||
err = 2; | ||
return 0; | ||
} | ||
|
||
bpf_put_file(file); | ||
|
||
file = bpf_fget_task(task, test_fd2); | ||
if (file == NULL) { | ||
err = 3; | ||
return 0; | ||
} | ||
|
||
bpf_put_file(file); | ||
|
||
file = bpf_fget_task(task, 9999); | ||
if (file != NULL) { | ||
err = 4; | ||
bpf_put_file(file); | ||
} | ||
|
||
return 0; | ||
} |