[PATCH RFC 1/3] fs: Introduce task path helpers
Chen Linxuan via B4 Relay <[email protected]>
| Newsgroups | org.kernel.vger.linux-fsdevel,org.kernel.feeds.b4-sent,org.kernel.vger.linux-kernel,org.kernel.vger.linux-security-module,org.kvack.linux-mm |
|---|---|
| Message-ID | <[email protected]> |
From: Chen Linxuan <[email protected]> Introduce helpers that acquire a referenced struct path for a task's executable, root, and working directory. Reuse them for procfs task links and AppArmor executable-path handling instead of duplicating file and path reference handling at each call site. Assisted-by: codex:glm-5.3 Signed-off-by: Chen Linxuan <[email protected]> --- fs/fs_struct.c | 44 ++++++++++++++++++++++++++++++++++++++++++++ fs/proc/base.c | 34 ++-------------------------------- include/linux/fs_struct.h | 3 +++ include/linux/mm.h | 1 + kernel/fork.c | 21 +++++++++++++++++++++ security/apparmor/task.c | 12 +++--------- 6 files changed, 74 insertions(+), 41 deletions(-) diff --git a/fs/fs_struct.c b/fs/fs_struct.c index 34699f3b6f88..5c772896260a 100644 --- a/fs/fs_struct.c +++ b/fs/fs_struct.c @@ -10,6 +10,50 @@ #include "internal.h" #include "mount.h" +/** + * get_task_root - acquire a reference to the task's root path + * @task: The task. + * @root: The task's root path. + * + * Returns 0 if the task has a root path, or -ENOENT if it does not. The + * caller must release the path through path_put() on success. + */ +int get_task_root(struct task_struct *task, struct path *root) +{ + int ret = -ENOENT; + + task_lock(task); + if (task->real_fs) { + get_fs_root(task->real_fs, root); + ret = 0; + } + task_unlock(task); + + return ret; +} + +/** + * get_task_pwd - acquire a reference to the task's working directory + * @task: The task. + * @pwd: The task's working directory. + * + * Returns 0 if the task has a working directory, or -ENOENT if it does not. + * The caller must release the path through path_put() on success. + */ +int get_task_pwd(struct task_struct *task, struct path *pwd) +{ + int ret = -ENOENT; + + task_lock(task); + if (task->real_fs) { + get_fs_pwd(task->real_fs, pwd); + ret = 0; + } + task_unlock(task); + + return ret; +} + /* * Replace the fs->{rootmnt,root} with {mnt,dentry}. Put the old values. * It can block. diff --git a/fs/proc/base.c b/fs/proc/base.c index 6a39de424f62..7e2c0538323c 100644 --- a/fs/proc/base.c +++ b/fs/proc/base.c @@ -206,31 +206,10 @@ static unsigned int __init pid_entry_nlink(const struct pid_entry *entries, return count; } -static int get_task_root(struct task_struct *task, struct path *root) -{ - int result = -ENOENT; - - task_lock(task); - if (task->real_fs) { - get_fs_root(task->real_fs, root); - result = 0; - } - task_unlock(task); - return result; -} - static int proc_cwd_link(struct dentry *dentry, struct path *path, struct task_struct *task) { - int result = -ENOENT; - - task_lock(task); - if (task->real_fs) { - get_fs_pwd(task->real_fs, path); - result = 0; - } - task_unlock(task); - return result; + return get_task_pwd(task, path); } static int proc_root_link(struct dentry *dentry, struct path *path, @@ -1761,16 +1740,7 @@ static const struct file_operations proc_pid_set_comm_operations = { static int proc_exe_link(struct dentry *dentry, struct path *exe_path, struct task_struct *task) { - struct file *exe_file; - - exe_file = get_task_exe_file(task); - if (exe_file) { - *exe_path = exe_file->f_path; - path_get(&exe_file->f_path); - fput(exe_file); - return 0; - } else - return -ENOENT; + return get_task_exe_path(task, exe_path); } static int call_proc_get_link(struct dentry *dentry, struct inode *inode, struct path *path_out) diff --git a/include/linux/fs_struct.h b/include/linux/fs_struct.h index 97eef8d3863d..fb725707754d 100644 --- a/include/linux/fs_struct.h +++ b/include/linux/fs_struct.h @@ -42,6 +42,9 @@ static inline void get_fs_pwd(struct fs_struct *fs, struct path *pwd) read_sequnlock_excl(&fs->seq); } +int get_task_root(struct task_struct *task, struct path *root); +int get_task_pwd(struct task_struct *task, struct path *pwd); + struct fs_struct *switch_fs_struct(struct fs_struct *new_fs); extern bool current_chrooted(void); diff --git a/include/linux/mm.h b/include/linux/mm.h index 485df9c2dbdd..7610eb579b41 100644 --- a/include/linux/mm.h +++ b/include/linux/mm.h @@ -4124,6 +4124,7 @@ extern int set_mm_exe_file(struct mm_struct *mm, struct file *new_exe_file); extern int replace_mm_exe_file(struct mm_struct *mm, struct file *new_exe_file); extern struct file *get_mm_exe_file(struct mm_struct *mm); extern struct file *get_task_exe_file(struct task_struct *task); +int get_task_exe_path(struct task_struct *task, struct path *exe_path); extern void vm_stat_account(struct mm_struct *, vm_flags_t, long npages); diff --git a/kernel/fork.c b/kernel/fork.c index 1e68404bd773..16aa4c82b9c7 100644 --- a/kernel/fork.c +++ b/kernel/fork.c @@ -1371,6 +1371,27 @@ struct file *get_task_exe_file(struct task_struct *task) return exe_file; } +/** + * get_task_exe_path - acquire a reference to the task's executable path + * @task: The task. + * @exe_path: The task's executable path. + * + * Returns 0 if the task has an executable path, or -ENOENT if it does not. + * The caller must release the path through path_put() on success. + */ +int get_task_exe_path(struct task_struct *task, struct path *exe_path) +{ + struct file *exe_file = get_task_exe_file(task); + + if (!exe_file) + return -ENOENT; + + *exe_path = exe_file->f_path; + path_get(exe_path); + fput(exe_file); + return 0; +} + /** * get_task_mm - acquire a reference to the task's mm * @task: The task. diff --git a/security/apparmor/task.c b/security/apparmor/task.c index b9fb3738124e..b4a4019d77c7 100644 --- a/security/apparmor/task.c +++ b/security/apparmor/task.c @@ -13,6 +13,7 @@ */ #include <linux/gfp.h> +#include <linux/mm.h> #include <linux/ptrace.h> #include "include/path.h" @@ -303,22 +304,15 @@ int aa_may_ptrace(const struct cred *tracer_cred, struct aa_label *tracer, static const char *get_current_exe_path(char *buffer, int buffer_size) { - struct file *exe_file; - struct path p; + struct path p __free(path_put) = {}; const char *path_str; - exe_file = get_task_exe_file(current); - if (!exe_file) + if (get_task_exe_path(current, &p)) return ERR_PTR(-ENOENT); - p = exe_file->f_path; - path_get(&p); if (aa_path_name(&p, FLAG_VIEW_SUBNS, buffer, &path_str, NULL, NULL)) path_str = ERR_PTR(-ENOMEM); - fput(exe_file); - path_put(&p); - return path_str; } -- 2.53.0