Skip to content

Commit ad9ef7f

Browse files
liu-song-6Alexei Starovoitov
authored andcommitted
bpf: Mark dentry->d_inode as trusted_or_null
LSM hooks such as security_path_mknod() and security_inode_rename() have access to newly allocated negative dentry, which has NULL d_inode. Therefore, it is necessary to do the NULL pointer check for d_inode. Also add selftests that checks the verifier enforces the NULL pointer check. Signed-off-by: Song Liu <[email protected]> Reviewed-by: Matt Bobrowski <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Alexei Starovoitov <[email protected]>
1 parent 4774cfe commit ad9ef7f

File tree

3 files changed

+35
-3
lines changed

3 files changed

+35
-3
lines changed

kernel/bpf/verifier.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7027,8 +7027,7 @@ BTF_TYPE_SAFE_TRUSTED(struct file) {
70277027
struct inode *f_inode;
70287028
};
70297029

7030-
BTF_TYPE_SAFE_TRUSTED(struct dentry) {
7031-
/* no negative dentry-s in places where bpf can see it */
7030+
BTF_TYPE_SAFE_TRUSTED_OR_NULL(struct dentry) {
70327031
struct inode *d_inode;
70337032
};
70347033

@@ -7066,7 +7065,6 @@ static bool type_is_trusted(struct bpf_verifier_env *env,
70667065
BTF_TYPE_EMIT(BTF_TYPE_SAFE_TRUSTED(struct bpf_iter__task));
70677066
BTF_TYPE_EMIT(BTF_TYPE_SAFE_TRUSTED(struct linux_binprm));
70687067
BTF_TYPE_EMIT(BTF_TYPE_SAFE_TRUSTED(struct file));
7069-
BTF_TYPE_EMIT(BTF_TYPE_SAFE_TRUSTED(struct dentry));
70707068

70717069
return btf_nested_type_is_trusted(&env->log, reg, field_name, btf_id, "__safe_trusted");
70727070
}
@@ -7076,6 +7074,7 @@ static bool type_is_trusted_or_null(struct bpf_verifier_env *env,
70767074
const char *field_name, u32 btf_id)
70777075
{
70787076
BTF_TYPE_EMIT(BTF_TYPE_SAFE_TRUSTED_OR_NULL(struct socket));
7077+
BTF_TYPE_EMIT(BTF_TYPE_SAFE_TRUSTED_OR_NULL(struct dentry));
70797078

70807079
return btf_nested_type_is_trusted(&env->log, reg, field_name, btf_id,
70817080
"__safe_trusted_or_null");

tools/testing/selftests/bpf/progs/verifier_vfs_accept.c

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
/* Copyright (c) 2024 Google LLC. */
33

44
#include <vmlinux.h>
5+
#include <errno.h>
56
#include <bpf/bpf_helpers.h>
67
#include <bpf/bpf_tracing.h>
78

@@ -82,4 +83,21 @@ int BPF_PROG(path_d_path_from_file_argument, struct file *file)
8283
return 0;
8384
}
8485

86+
SEC("lsm.s/inode_rename")
87+
__success
88+
int BPF_PROG(inode_rename, struct inode *old_dir, struct dentry *old_dentry,
89+
struct inode *new_dir, struct dentry *new_dentry,
90+
unsigned int flags)
91+
{
92+
struct inode *inode = new_dentry->d_inode;
93+
ino_t ino;
94+
95+
if (!inode)
96+
return 0;
97+
ino = inode->i_ino;
98+
if (ino == 0)
99+
return -EACCES;
100+
return 0;
101+
}
102+
85103
char _license[] SEC("license") = "GPL";

tools/testing/selftests/bpf/progs/verifier_vfs_reject.c

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
/* Copyright (c) 2024 Google LLC. */
33

44
#include <vmlinux.h>
5+
#include <errno.h>
56
#include <bpf/bpf_helpers.h>
67
#include <bpf/bpf_tracing.h>
78
#include <linux/limits.h>
@@ -158,4 +159,18 @@ int BPF_PROG(path_d_path_kfunc_non_lsm, struct path *path, struct file *f)
158159
return 0;
159160
}
160161

162+
SEC("lsm.s/inode_rename")
163+
__failure __msg("invalid mem access 'trusted_ptr_or_null_'")
164+
int BPF_PROG(inode_rename, struct inode *old_dir, struct dentry *old_dentry,
165+
struct inode *new_dir, struct dentry *new_dentry,
166+
unsigned int flags)
167+
{
168+
struct inode *inode = new_dentry->d_inode;
169+
ino_t ino;
170+
171+
ino = inode->i_ino;
172+
if (ino == 0)
173+
return -EACCES;
174+
return 0;
175+
}
161176
char _license[] SEC("license") = "GPL";

0 commit comments

Comments
 (0)