mirror of
https://gitee.com/bianbu-linux/linux-6.6
synced 2025-04-24 14:07:52 -04:00
selftests/bpf: Test task storage when local_storage->smap is NULL
The current sk storage test ensures the memory free works when the local_storage->smap is NULL. This patch adds a task storage test to ensure the memory free code path works when local_storage->smap is NULL. Signed-off-by: Martin KaFai Lau <martin.lau@kernel.org> Link: https://lore.kernel.org/r/20230322215246.1675516-5-martin.lau@linux.dev Signed-off-by: Alexei Starovoitov <ast@kernel.org>
This commit is contained in:
parent
6ae9d5e99e
commit
d8db84d71c
2 changed files with 46 additions and 17 deletions
|
@ -23,7 +23,7 @@ struct storage {
|
||||||
/* Fork and exec the provided rm binary and return the exit code of the
|
/* Fork and exec the provided rm binary and return the exit code of the
|
||||||
* forked process and its pid.
|
* forked process and its pid.
|
||||||
*/
|
*/
|
||||||
static int run_self_unlink(int *monitored_pid, const char *rm_path)
|
static int run_self_unlink(struct local_storage *skel, const char *rm_path)
|
||||||
{
|
{
|
||||||
int child_pid, child_status, ret;
|
int child_pid, child_status, ret;
|
||||||
int null_fd;
|
int null_fd;
|
||||||
|
@ -35,7 +35,7 @@ static int run_self_unlink(int *monitored_pid, const char *rm_path)
|
||||||
dup2(null_fd, STDERR_FILENO);
|
dup2(null_fd, STDERR_FILENO);
|
||||||
close(null_fd);
|
close(null_fd);
|
||||||
|
|
||||||
*monitored_pid = getpid();
|
skel->bss->monitored_pid = getpid();
|
||||||
/* Use the copied /usr/bin/rm to delete itself
|
/* Use the copied /usr/bin/rm to delete itself
|
||||||
* /tmp/copy_of_rm /tmp/copy_of_rm.
|
* /tmp/copy_of_rm /tmp/copy_of_rm.
|
||||||
*/
|
*/
|
||||||
|
@ -44,6 +44,7 @@ static int run_self_unlink(int *monitored_pid, const char *rm_path)
|
||||||
exit(errno);
|
exit(errno);
|
||||||
} else if (child_pid > 0) {
|
} else if (child_pid > 0) {
|
||||||
waitpid(child_pid, &child_status, 0);
|
waitpid(child_pid, &child_status, 0);
|
||||||
|
ASSERT_EQ(skel->data->task_storage_result, 0, "task_storage_result");
|
||||||
return WEXITSTATUS(child_status);
|
return WEXITSTATUS(child_status);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -133,7 +134,7 @@ void test_test_local_storage(void)
|
||||||
* unlink its executable. This operation should be denied by the loaded
|
* unlink its executable. This operation should be denied by the loaded
|
||||||
* LSM program.
|
* LSM program.
|
||||||
*/
|
*/
|
||||||
err = run_self_unlink(&skel->bss->monitored_pid, tmp_exec_path);
|
err = run_self_unlink(skel, tmp_exec_path);
|
||||||
if (!ASSERT_EQ(err, EPERM, "run_self_unlink"))
|
if (!ASSERT_EQ(err, EPERM, "run_self_unlink"))
|
||||||
goto close_prog_rmdir;
|
goto close_prog_rmdir;
|
||||||
|
|
||||||
|
|
|
@ -16,6 +16,7 @@ char _license[] SEC("license") = "GPL";
|
||||||
int monitored_pid = 0;
|
int monitored_pid = 0;
|
||||||
int inode_storage_result = -1;
|
int inode_storage_result = -1;
|
||||||
int sk_storage_result = -1;
|
int sk_storage_result = -1;
|
||||||
|
int task_storage_result = -1;
|
||||||
|
|
||||||
struct local_storage {
|
struct local_storage {
|
||||||
struct inode *exec_inode;
|
struct inode *exec_inode;
|
||||||
|
@ -50,26 +51,57 @@ struct {
|
||||||
__type(value, struct local_storage);
|
__type(value, struct local_storage);
|
||||||
} task_storage_map SEC(".maps");
|
} task_storage_map SEC(".maps");
|
||||||
|
|
||||||
|
struct {
|
||||||
|
__uint(type, BPF_MAP_TYPE_TASK_STORAGE);
|
||||||
|
__uint(map_flags, BPF_F_NO_PREALLOC);
|
||||||
|
__type(key, int);
|
||||||
|
__type(value, struct local_storage);
|
||||||
|
} task_storage_map2 SEC(".maps");
|
||||||
|
|
||||||
SEC("lsm/inode_unlink")
|
SEC("lsm/inode_unlink")
|
||||||
int BPF_PROG(unlink_hook, struct inode *dir, struct dentry *victim)
|
int BPF_PROG(unlink_hook, struct inode *dir, struct dentry *victim)
|
||||||
{
|
{
|
||||||
__u32 pid = bpf_get_current_pid_tgid() >> 32;
|
__u32 pid = bpf_get_current_pid_tgid() >> 32;
|
||||||
|
struct bpf_local_storage *local_storage;
|
||||||
struct local_storage *storage;
|
struct local_storage *storage;
|
||||||
|
struct task_struct *task;
|
||||||
bool is_self_unlink;
|
bool is_self_unlink;
|
||||||
|
|
||||||
if (pid != monitored_pid)
|
if (pid != monitored_pid)
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
storage = bpf_task_storage_get(&task_storage_map,
|
task = bpf_get_current_task_btf();
|
||||||
bpf_get_current_task_btf(), 0, 0);
|
if (!task)
|
||||||
if (storage) {
|
return 0;
|
||||||
|
|
||||||
|
task_storage_result = -1;
|
||||||
|
|
||||||
|
storage = bpf_task_storage_get(&task_storage_map, task, 0, 0);
|
||||||
|
if (!storage)
|
||||||
|
return 0;
|
||||||
|
|
||||||
/* Don't let an executable delete itself */
|
/* Don't let an executable delete itself */
|
||||||
is_self_unlink = storage->exec_inode == victim->d_inode;
|
is_self_unlink = storage->exec_inode == victim->d_inode;
|
||||||
if (is_self_unlink)
|
|
||||||
return -EPERM;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
storage = bpf_task_storage_get(&task_storage_map2, task, 0,
|
||||||
|
BPF_LOCAL_STORAGE_GET_F_CREATE);
|
||||||
|
if (!storage || storage->value)
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
|
if (bpf_task_storage_delete(&task_storage_map, task))
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
/* Ensure that the task_storage_map is disconnected from the storage.
|
||||||
|
* The storage memory should not be freed back to the
|
||||||
|
* bpf_mem_alloc.
|
||||||
|
*/
|
||||||
|
local_storage = task->bpf_storage;
|
||||||
|
if (!local_storage || local_storage->smap)
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
task_storage_result = 0;
|
||||||
|
|
||||||
|
return is_self_unlink ? -EPERM : 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
SEC("lsm.s/inode_rename")
|
SEC("lsm.s/inode_rename")
|
||||||
|
@ -139,11 +171,7 @@ int BPF_PROG(socket_bind, struct socket *sock, struct sockaddr *address,
|
||||||
if (bpf_sk_storage_delete(&sk_storage_map, sock->sk))
|
if (bpf_sk_storage_delete(&sk_storage_map, sock->sk))
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
/* Ensure that the sk_storage_map is disconnected from the storage.
|
/* Ensure that the sk_storage_map is disconnected from the storage. */
|
||||||
* The storage memory should not be freed back to the
|
|
||||||
* bpf_mem_alloc of the sk_bpf_storage_map because
|
|
||||||
* sk_bpf_storage_map may have been gone.
|
|
||||||
*/
|
|
||||||
if (!sock->sk->sk_bpf_storage || sock->sk->sk_bpf_storage->smap)
|
if (!sock->sk->sk_bpf_storage || sock->sk->sk_bpf_storage->smap)
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue