bpf: Validate BPF object in BPF_OBJ_PIN before calling LSM

Do a sanity check whether provided file-to-be-pinned is actually a BPF
object (prog, map, btf) before calling security_path_mknod LSM hook. If
it's not, LSM hook doesn't have to be triggered, as the operation has no
chance of succeeding anyways.

Suggested-by: Christian Brauner <brauner@kernel.org>
Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
Reviewed-by: Christian Brauner <brauner@kernel.org>
Link: https://lore.kernel.org/bpf/20230522232917.2454595-2-andrii@kernel.org
This commit is contained in:
Andrii Nakryiko 2023-05-22 16:29:14 -07:00 коммит произвёл Daniel Borkmann
Родитель f46392ee3d
Коммит e7d85427ef
1 изменённых файлов: 5 добавлений и 6 удалений

Просмотреть файл

@ -448,18 +448,17 @@ static int bpf_obj_do_pin(const char __user *pathname, void *raw,
if (IS_ERR(dentry)) if (IS_ERR(dentry))
return PTR_ERR(dentry); return PTR_ERR(dentry);
mode = S_IFREG | ((S_IRUSR | S_IWUSR) & ~current_umask());
ret = security_path_mknod(&path, dentry, mode, 0);
if (ret)
goto out;
dir = d_inode(path.dentry); dir = d_inode(path.dentry);
if (dir->i_op != &bpf_dir_iops) { if (dir->i_op != &bpf_dir_iops) {
ret = -EPERM; ret = -EPERM;
goto out; goto out;
} }
mode = S_IFREG | ((S_IRUSR | S_IWUSR) & ~current_umask());
ret = security_path_mknod(&path, dentry, mode, 0);
if (ret)
goto out;
switch (type) { switch (type) {
case BPF_TYPE_PROG: case BPF_TYPE_PROG:
ret = vfs_mkobj(dentry, mode, bpf_mkprog, raw); ret = vfs_mkobj(dentry, mode, bpf_mkprog, raw);