fs/bpf_fs_kfuncs.c
Source file repositories/reference/linux-study-clean/fs/bpf_fs_kfuncs.c
File Facts
- System
- Linux kernel
- Corpus path
fs/bpf_fs_kfuncs.c- Extension
.c- Size
- 12973 bytes
- Lines
- 440
- Domain
- Core OS
- Bucket
- VFS And Filesystem Core
- Inferred role
- Core OS: implementation source
- Status
- source implementation candidate
Why This File Exists
Core operating-system implementation surface: boot, tasks, memory, VFS, syscall-facing interfaces, synchronization, credentials, and isolation.
- Core operating-system implementation surface: boot, tasks, memory, VFS, syscall-facing interfaces, synchronization, credentials, and isolation.
- Defines or uses C structs; map object ownership, embedded links, reference counts, and lock ownership.
Dependency Surface
linux/bpf.hlinux/bpf_lsm.hlinux/btf.hlinux/btf_ids.hlinux/dcache.hlinux/fs.hlinux/fsnotify.hlinux/file.hlinux/kernfs.hlinux/mm.hlinux/xattr.h
Detected Declarations
function bpf_put_filefunction bpf_put_filefunction bpf_d_pathfunction match_security_bpf_prefixfunction bpf_xattr_read_permissionfunction bpf_get_dentry_xattrfunction bpf_get_file_xattrfunction bpf_xattr_write_permissionfunction bpf_set_dentry_xattr_lockedfunction bpf_remove_dentry_xattr_lockedfunction bpf_set_dentry_xattrfunction bpf_remove_dentry_xattrfunction bpf_cgroup_read_xattrfunction bpf_fs_kfuncs_filterfunction bpf_lsm_has_d_inode_lockedfunction bpf_fs_kfuncs_init
Annotated Snippet
// SPDX-License-Identifier: GPL-2.0
/* Copyright (c) 2024 Google LLC. */
#include <linux/bpf.h>
#include <linux/bpf_lsm.h>
#include <linux/btf.h>
#include <linux/btf_ids.h>
#include <linux/dcache.h>
#include <linux/fs.h>
#include <linux/fsnotify.h>
#include <linux/file.h>
#include <linux/kernfs.h>
#include <linux/mm.h>
#include <linux/xattr.h>
__bpf_kfunc_start_defs();
/**
* bpf_get_task_exe_file - get a reference on the exe_file struct file member of
* the mm_struct that is nested within the supplied
* task_struct
* @task: task_struct of which the nested mm_struct exe_file member to get a
* reference on
*
* Get a reference on the exe_file struct file member field of the mm_struct
* nested within the supplied *task*. The referenced file pointer acquired by
* this BPF kfunc must be released using bpf_put_file(). Failing to call
* bpf_put_file() on the returned referenced struct file pointer that has been
* acquired by this BPF kfunc will result in the BPF program being rejected by
* the BPF verifier.
*
* This BPF kfunc may only be called from BPF LSM programs.
*
* Internally, this BPF kfunc leans on get_task_exe_file(), such that calling
* bpf_get_task_exe_file() would be analogous to calling get_task_exe_file()
* directly in kernel context.
*
* Return: A referenced struct file pointer to the exe_file member of the
* mm_struct that is nested within the supplied *task*. On error, NULL is
* returned.
*/
__bpf_kfunc struct file *bpf_get_task_exe_file(struct task_struct *task)
{
return get_task_exe_file(task);
}
/**
* bpf_put_file - put a reference on the supplied file
* @file: file to put a reference on
*
* Put a reference on the supplied *file*. Only referenced file pointers may be
* passed to this BPF kfunc. Attempting to pass an unreferenced file pointer, or
* any other arbitrary pointer for that matter, will result in the BPF program
* being rejected by the BPF verifier.
*
* This BPF kfunc may only be called from BPF LSM programs.
*/
__bpf_kfunc void bpf_put_file(struct file *file)
{
fput(file);
}
/**
* bpf_path_d_path - resolve the pathname for the supplied path
* @path: path to resolve the pathname for
* @buf: buffer to return the resolved pathname in
* @buf__sz: length of the supplied buffer
*
* Resolve the pathname for the supplied *path* and store it in *buf*. This BPF
* kfunc is the safer variant of the legacy bpf_d_path() helper and should be
* used in place of bpf_d_path() whenever possible.
*
* This BPF kfunc may only be called from BPF LSM programs.
*
* Return: A positive integer corresponding to the length of the resolved
* pathname in *buf*, including the NUL termination character. On error, a
* negative integer is returned.
*/
__bpf_kfunc int bpf_path_d_path(const struct path *path, char *buf, size_t buf__sz)
{
int len;
char *ret;
if (!buf__sz)
return -EINVAL;
ret = d_path(path, buf, buf__sz);
if (IS_ERR(ret))
return PTR_ERR(ret);
Annotation
- Immediate include surface: `linux/bpf.h`, `linux/bpf_lsm.h`, `linux/btf.h`, `linux/btf_ids.h`, `linux/dcache.h`, `linux/fs.h`, `linux/fsnotify.h`, `linux/file.h`.
- Detected declarations: `function bpf_put_file`, `function bpf_put_file`, `function bpf_d_path`, `function match_security_bpf_prefix`, `function bpf_xattr_read_permission`, `function bpf_get_dentry_xattr`, `function bpf_get_file_xattr`, `function bpf_xattr_write_permission`, `function bpf_set_dentry_xattr_locked`, `function bpf_remove_dentry_xattr_locked`.
- Atlas domain: Core OS / VFS And Filesystem Core.
- Implementation status: source implementation candidate.
Implementation Notes
- This generated page is the file-by-file coverage layer; curated subsystem chapters should link here when they synthesize a multi-file control flow.
- Core OS pages should be promoted from atlas-only to deep-reviewed when they explain data structures, invariants, locking, lifecycle, and C implementation snippets.
- Driver-family pages are intentionally pattern-oriented unless they are part of the selected PCIe/NVMe representative device path.