- This is a heavily modified version of susfs v1.5.12
- It does not comply with the upstream offical susfs v1.5.12
- sus_mount functionality still remain in v1.5.5 as backporting it to the latest version will result a mount detection leak in some apps/detectors
- Increase susfs_open_redirect UID limit to <11000
- susfs magic mount support is still implemented and enabled
- sus_map is implemented and complied with the upstream v1.5.12 codebase
This commit requires a bunch of backports commits from v4.19 and v5.x to make sus_map working:
0a8cbf3725edbacc5f1ead33eeae7e4d78823b5a proc: less memory for /proc/*/map_files readdir
37ae2444584654f6785f2cc49181f05af788c9b2 mm: smaps: split PSS into components
49a5115e11350ee68f6a5fbd56b3e817bf9e5aac fs/task_mmu: add pkeys header
6f94042bed51121f8f28a5e572cda20c21fed2e1 mm/pkeys: Add an empty arch_pkeys_enabled()
bbd5aec12b32097a71dc6a0097194a18f3ee9a17 mm/pkeys, powerpc, x86: Provide an empty vma_pkey() in linux/pkeys.h
849ca8ce954d9dbb082dcf83c98af861e98e5635 mm: /proc/pid/smaps_rollup: convert to single value seq_file
6071a482c8e603be25895cc2cac5f0eab61c4051 mm: /proc/pid/smaps: factor out common stats printing
03fd2fbe9c40da8128cec5c69ef54755c0f38c6c mm: /proc/pid/smaps: factor out mem stats gathering
95f8be4c8a86a491a1c2ac9bfe470aef9e1baa8f mm: /proc/pid/*maps remove is_pid and related wrappers
27956d255e3b012372951dd6131e07c106d2daae procfs: add seq_put_hex_ll to speed up /proc/pid/maps
7f2847d02cdc4491b5ee6d4a0043854cbd6c7a1a proc: add seq_put_decimal_ull_width to speed up /proc/pid/smaps
For KernelSU side patches for this commit you need the sidex15's KernelSU-Next fork:
https://github.com/sidex15/KernelSU-Next/tree/n3x7g3n-kernel
Or if you want to patch on your own here's the commit patch of susfs in the KernelSU-Next:
13b1dfd6e2
Co-authored-by: simonpunk <simonpunk2016@gmail.com>
Signed-off-by: Pranav Vashi <neobuddy89@gmail.com>
373 lines
8.0 KiB
C
373 lines
8.0 KiB
C
// SPDX-License-Identifier: GPL-2.0
|
|
#include <linux/sched/signal.h>
|
|
#include <linux/errno.h>
|
|
#include <linux/dcache.h>
|
|
#include <linux/path.h>
|
|
#include <linux/fdtable.h>
|
|
#include <linux/namei.h>
|
|
#include <linux/pid.h>
|
|
#include <linux/security.h>
|
|
#include <linux/file.h>
|
|
#include <linux/seq_file.h>
|
|
#include <linux/fs.h>
|
|
|
|
#include <linux/proc_fs.h>
|
|
#ifdef CONFIG_KSU_SUSFS_SUS_MOUNT
|
|
#include <linux/susfs_def.h>
|
|
#endif
|
|
|
|
#include "../mount.h"
|
|
#include "internal.h"
|
|
#include "fd.h"
|
|
|
|
static int seq_show(struct seq_file *m, void *v)
|
|
{
|
|
struct files_struct *files = NULL;
|
|
int f_flags = 0, ret = -ENOENT;
|
|
struct file *file = NULL;
|
|
struct task_struct *task;
|
|
#ifdef CONFIG_KSU_SUSFS_SUS_MOUNT
|
|
struct mount *mnt = NULL;
|
|
#endif
|
|
|
|
task = get_proc_task(m->private);
|
|
if (!task)
|
|
return -ENOENT;
|
|
|
|
files = get_files_struct(task);
|
|
put_task_struct(task);
|
|
|
|
if (files) {
|
|
unsigned int fd = proc_fd(m->private);
|
|
|
|
spin_lock(&files->file_lock);
|
|
file = fcheck_files(files, fd);
|
|
if (file) {
|
|
struct fdtable *fdt = files_fdtable(files);
|
|
|
|
f_flags = file->f_flags;
|
|
if (close_on_exec(fd, fdt))
|
|
f_flags |= O_CLOEXEC;
|
|
|
|
get_file(file);
|
|
ret = 0;
|
|
}
|
|
spin_unlock(&files->file_lock);
|
|
put_files_struct(files);
|
|
}
|
|
|
|
if (ret)
|
|
return ret;
|
|
|
|
#ifdef CONFIG_KSU_SUSFS_SUS_MOUNT
|
|
mnt = real_mount(file->f_path.mnt);
|
|
if (likely(susfs_is_current_proc_umounted()) &&
|
|
mnt->mnt_id >= DEFAULT_SUS_MNT_ID) {
|
|
for (; mnt->mnt_id >= DEFAULT_SUS_MNT_ID; mnt = mnt->mnt_parent) { }
|
|
}
|
|
seq_printf(m, "pos:\t%lli\nflags:\t0%o\nmnt_id:\t%i\n",
|
|
(long long)file->f_pos, f_flags,
|
|
mnt->mnt_id);
|
|
#else
|
|
seq_printf(m, "pos:\t%lli\nflags:\t0%o\nmnt_id:\t%i\n",
|
|
(long long)file->f_pos, f_flags,
|
|
real_mount(file->f_path.mnt)->mnt_id);
|
|
#endif
|
|
|
|
show_fd_locks(m, file, files);
|
|
if (seq_has_overflowed(m))
|
|
goto out;
|
|
|
|
if (file->f_op->show_fdinfo)
|
|
file->f_op->show_fdinfo(m, file);
|
|
|
|
out:
|
|
fput(file);
|
|
return 0;
|
|
}
|
|
|
|
static int seq_fdinfo_open(struct inode *inode, struct file *file)
|
|
{
|
|
return single_open(file, seq_show, inode);
|
|
}
|
|
|
|
static const struct file_operations proc_fdinfo_file_operations = {
|
|
.open = seq_fdinfo_open,
|
|
.read = seq_read,
|
|
.llseek = seq_lseek,
|
|
.release = single_release,
|
|
};
|
|
|
|
static int tid_fd_revalidate(struct dentry *dentry, unsigned int flags)
|
|
{
|
|
struct files_struct *files;
|
|
struct task_struct *task;
|
|
struct inode *inode;
|
|
unsigned int fd;
|
|
|
|
if (flags & LOOKUP_RCU)
|
|
return -ECHILD;
|
|
|
|
inode = d_inode(dentry);
|
|
task = get_proc_task(inode);
|
|
fd = proc_fd(inode);
|
|
|
|
if (task) {
|
|
files = get_files_struct(task);
|
|
if (files) {
|
|
struct file *file;
|
|
|
|
rcu_read_lock();
|
|
file = fcheck_files(files, fd);
|
|
if (file) {
|
|
unsigned f_mode = file->f_mode;
|
|
|
|
rcu_read_unlock();
|
|
put_files_struct(files);
|
|
|
|
task_dump_owner(task, 0, &inode->i_uid, &inode->i_gid);
|
|
|
|
if (S_ISLNK(inode->i_mode)) {
|
|
unsigned i_mode = S_IFLNK;
|
|
if (f_mode & FMODE_READ)
|
|
i_mode |= S_IRUSR | S_IXUSR;
|
|
if (f_mode & FMODE_WRITE)
|
|
i_mode |= S_IWUSR | S_IXUSR;
|
|
inode->i_mode = i_mode;
|
|
}
|
|
|
|
security_task_to_inode(task, inode);
|
|
put_task_struct(task);
|
|
return 1;
|
|
}
|
|
rcu_read_unlock();
|
|
put_files_struct(files);
|
|
}
|
|
put_task_struct(task);
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
static const struct dentry_operations tid_fd_dentry_operations = {
|
|
.d_revalidate = tid_fd_revalidate,
|
|
.d_delete = pid_delete_dentry,
|
|
};
|
|
|
|
static int proc_fd_link(struct dentry *dentry, struct path *path)
|
|
{
|
|
struct files_struct *files = NULL;
|
|
struct task_struct *task;
|
|
int ret = -ENOENT;
|
|
|
|
task = get_proc_task(d_inode(dentry));
|
|
if (task) {
|
|
files = get_files_struct(task);
|
|
put_task_struct(task);
|
|
}
|
|
|
|
if (files) {
|
|
unsigned int fd = proc_fd(d_inode(dentry));
|
|
struct file *fd_file;
|
|
|
|
spin_lock(&files->file_lock);
|
|
fd_file = fcheck_files(files, fd);
|
|
if (fd_file) {
|
|
*path = fd_file->f_path;
|
|
path_get(&fd_file->f_path);
|
|
ret = 0;
|
|
}
|
|
spin_unlock(&files->file_lock);
|
|
put_files_struct(files);
|
|
}
|
|
|
|
return ret;
|
|
}
|
|
|
|
static int
|
|
proc_fd_instantiate(struct inode *dir, struct dentry *dentry,
|
|
struct task_struct *task, const void *ptr)
|
|
{
|
|
unsigned fd = (unsigned long)ptr;
|
|
struct proc_inode *ei;
|
|
struct inode *inode;
|
|
|
|
inode = proc_pid_make_inode(dir->i_sb, task, S_IFLNK);
|
|
if (!inode)
|
|
goto out;
|
|
|
|
ei = PROC_I(inode);
|
|
ei->fd = fd;
|
|
|
|
inode->i_op = &proc_pid_link_inode_operations;
|
|
inode->i_size = 64;
|
|
|
|
ei->op.proc_get_link = proc_fd_link;
|
|
|
|
d_set_d_op(dentry, &tid_fd_dentry_operations);
|
|
d_add(dentry, inode);
|
|
|
|
/* Close the race of the process dying before we return the dentry */
|
|
if (tid_fd_revalidate(dentry, 0))
|
|
return 0;
|
|
out:
|
|
return -ENOENT;
|
|
}
|
|
|
|
static struct dentry *proc_lookupfd_common(struct inode *dir,
|
|
struct dentry *dentry,
|
|
instantiate_t instantiate)
|
|
{
|
|
struct task_struct *task = get_proc_task(dir);
|
|
int result = -ENOENT;
|
|
unsigned fd = name_to_int(&dentry->d_name);
|
|
|
|
if (!task)
|
|
goto out_no_task;
|
|
if (fd == ~0U)
|
|
goto out;
|
|
|
|
result = instantiate(dir, dentry, task, (void *)(unsigned long)fd);
|
|
out:
|
|
put_task_struct(task);
|
|
out_no_task:
|
|
return ERR_PTR(result);
|
|
}
|
|
|
|
static int proc_readfd_common(struct file *file, struct dir_context *ctx,
|
|
instantiate_t instantiate)
|
|
{
|
|
struct task_struct *p = get_proc_task(file_inode(file));
|
|
struct files_struct *files;
|
|
unsigned int fd;
|
|
|
|
if (!p)
|
|
return -ENOENT;
|
|
|
|
if (!dir_emit_dots(file, ctx))
|
|
goto out;
|
|
files = get_files_struct(p);
|
|
if (!files)
|
|
goto out;
|
|
|
|
rcu_read_lock();
|
|
for (fd = ctx->pos - 2;
|
|
fd < files_fdtable(files)->max_fds;
|
|
fd++, ctx->pos++) {
|
|
char name[PROC_NUMBUF];
|
|
int len;
|
|
|
|
if (!fcheck_files(files, fd))
|
|
continue;
|
|
rcu_read_unlock();
|
|
|
|
len = snprintf(name, sizeof(name), "%u", fd);
|
|
if (!proc_fill_cache(file, ctx,
|
|
name, len, instantiate, p,
|
|
(void *)(unsigned long)fd))
|
|
goto out_fd_loop;
|
|
cond_resched();
|
|
rcu_read_lock();
|
|
}
|
|
rcu_read_unlock();
|
|
out_fd_loop:
|
|
put_files_struct(files);
|
|
out:
|
|
put_task_struct(p);
|
|
return 0;
|
|
}
|
|
|
|
static int proc_readfd(struct file *file, struct dir_context *ctx)
|
|
{
|
|
return proc_readfd_common(file, ctx, proc_fd_instantiate);
|
|
}
|
|
|
|
const struct file_operations proc_fd_operations = {
|
|
.read = generic_read_dir,
|
|
.iterate_shared = proc_readfd,
|
|
.llseek = generic_file_llseek,
|
|
};
|
|
|
|
static struct dentry *proc_lookupfd(struct inode *dir, struct dentry *dentry,
|
|
unsigned int flags)
|
|
{
|
|
return proc_lookupfd_common(dir, dentry, proc_fd_instantiate);
|
|
}
|
|
|
|
/*
|
|
* /proc/pid/fd needs a special permission handler so that a process can still
|
|
* access /proc/self/fd after it has executed a setuid().
|
|
*/
|
|
int proc_fd_permission(struct inode *inode, int mask)
|
|
{
|
|
struct task_struct *p;
|
|
int rv;
|
|
|
|
rv = generic_permission(inode, mask);
|
|
if (rv == 0)
|
|
return rv;
|
|
|
|
rcu_read_lock();
|
|
p = pid_task(proc_pid(inode), PIDTYPE_PID);
|
|
if (p && same_thread_group(p, current))
|
|
rv = 0;
|
|
rcu_read_unlock();
|
|
|
|
return rv;
|
|
}
|
|
|
|
const struct inode_operations proc_fd_inode_operations = {
|
|
.lookup = proc_lookupfd,
|
|
.permission = proc_fd_permission,
|
|
.setattr = proc_setattr,
|
|
};
|
|
|
|
static int
|
|
proc_fdinfo_instantiate(struct inode *dir, struct dentry *dentry,
|
|
struct task_struct *task, const void *ptr)
|
|
{
|
|
unsigned fd = (unsigned long)ptr;
|
|
struct proc_inode *ei;
|
|
struct inode *inode;
|
|
|
|
inode = proc_pid_make_inode(dir->i_sb, task, S_IFREG | S_IRUSR);
|
|
if (!inode)
|
|
goto out;
|
|
|
|
ei = PROC_I(inode);
|
|
ei->fd = fd;
|
|
|
|
inode->i_fop = &proc_fdinfo_file_operations;
|
|
|
|
d_set_d_op(dentry, &tid_fd_dentry_operations);
|
|
d_add(dentry, inode);
|
|
|
|
/* Close the race of the process dying before we return the dentry */
|
|
if (tid_fd_revalidate(dentry, 0))
|
|
return 0;
|
|
out:
|
|
return -ENOENT;
|
|
}
|
|
|
|
static struct dentry *
|
|
proc_lookupfdinfo(struct inode *dir, struct dentry *dentry, unsigned int flags)
|
|
{
|
|
return proc_lookupfd_common(dir, dentry, proc_fdinfo_instantiate);
|
|
}
|
|
|
|
static int proc_readfdinfo(struct file *file, struct dir_context *ctx)
|
|
{
|
|
return proc_readfd_common(file, ctx,
|
|
proc_fdinfo_instantiate);
|
|
}
|
|
|
|
const struct inode_operations proc_fdinfo_inode_operations = {
|
|
.lookup = proc_lookupfdinfo,
|
|
.setattr = proc_setattr,
|
|
};
|
|
|
|
const struct file_operations proc_fdinfo_operations = {
|
|
.read = generic_read_dir,
|
|
.iterate_shared = proc_readfdinfo,
|
|
.llseek = generic_file_llseek,
|
|
};
|