Skip to content

Commit 41d990f

Browse files
sidex15vbajs
authored andcommitted
[BACKPORT] susfs: Revive SUS_MAP feature
The backport is only to comply with upstream 4.14 task_mmu.c that's why it's different... It requires a backport commit to prevent kernel panic or system crashing on some apps if using sus_maps: sidex15/android_kernel_lge_sm8150@4eb32cd I think there's a better way than this... Signed-off-by: Yahya Wessam <yahyawessam2002@gmail.com>
1 parent 5f00d40 commit 41d990f

5 files changed

Lines changed: 146 additions & 8 deletions

File tree

fs/proc/base.c

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,9 @@
102102
#include <trace/events/oom.h>
103103
#include "internal.h"
104104
#include "fd.h"
105+
#ifdef CONFIG_KSU_SUSFS_SUS_MAP
106+
#include <linux/susfs_def.h>
107+
#endif
105108

106109
#include "../../lib/kstrtox.h"
107110

@@ -841,6 +844,9 @@ static ssize_t mem_rw(struct file *file, char __user *buf,
841844
ssize_t copied;
842845
char *page;
843846
unsigned int flags;
847+
#ifdef CONFIG_KSU_SUSFS_SUS_MAP
848+
struct vm_area_struct *vma;
849+
#endif
844850

845851
if (!mm)
846852
return 0;
@@ -857,7 +863,19 @@ static ssize_t mem_rw(struct file *file, char __user *buf,
857863

858864
while (count > 0) {
859865
size_t this_len = min_t(size_t, count, PAGE_SIZE);
860-
866+
#ifdef CONFIG_KSU_SUSFS_SUS_MAP
867+
vma = find_vma(mm, addr);
868+
if (vma && vma->vm_file) {
869+
struct inode *inode = file_inode(vma->vm_file);
870+
if (unlikely(inode->i_mapping->flags & BIT_SUS_MAPS) && susfs_is_current_non_root_user_app_proc()) {
871+
if (write) {
872+
copied = -EFAULT;
873+
} else {
874+
copied = -EIO;
875+
}
876+
}
877+
}
878+
#endif
861879
if (write && copy_from_user(page, buf, this_len)) {
862880
copied = -EFAULT;
863881
break;
@@ -2446,6 +2464,13 @@ proc_map_files_readdir(struct file *file, struct dir_context *ctx)
24462464
vma = vma->vm_next) {
24472465
if (!vma->vm_file)
24482466
continue;
2467+
#ifdef CONFIG_KSU_SUSFS_SUS_MAP
2468+
if (unlikely(file_inode(vma->vm_file)->i_mapping->flags & BIT_SUS_MAPS) &&
2469+
susfs_is_current_non_root_user_app_proc())
2470+
{
2471+
continue;
2472+
}
2473+
#endif
24492474
if (++pos <= ctx->pos)
24502475
continue;
24512476

fs/proc/task_mmu.c

Lines changed: 66 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
#include <linux/uaccess.h>
2121
#include <linux/mm_inline.h>
2222
#include <linux/ctype.h>
23-
#ifdef CONFIG_KSU_SUSFS_SUS_KSTAT
23+
#if defined(CONFIG_KSU_SUSFS_SUS_KSTAT) || defined(CONFIG_KSU_SUSFS_SUS_MAP)
2424
#include <linux/susfs_def.h>
2525
#endif
2626

@@ -29,6 +29,10 @@
2929
#include <asm/tlbflush.h>
3030
#include "internal.h"
3131

32+
#ifdef CONFIG_KSU_SUSFS_SUS_KSTAT
33+
extern void susfs_sus_ino_for_show_map_vma(unsigned long ino, dev_t *out_dev, unsigned long *out_ino);
34+
#endif
35+
3236
void task_mem(struct seq_file *m, struct mm_struct *mm)
3337
{
3438
unsigned long text, lib, swap, anon, file, shmem;
@@ -514,10 +518,6 @@ static void show_vma_header_prefix_fake(struct seq_file *m,
514518
seq_putc(m, ' ');
515519
}
516520

517-
#ifdef CONFIG_KSU_SUSFS_SUS_KSTAT
518-
extern void susfs_sus_ino_for_show_map_vma(unsigned long ino, dev_t *out_dev, unsigned long *out_ino);
519-
#endif
520-
521521
static void
522522
show_map_vma(struct seq_file *m, struct vm_area_struct *vma, int is_pid)
523523
{
@@ -532,6 +532,24 @@ show_map_vma(struct seq_file *m, struct vm_area_struct *vma, int is_pid)
532532

533533
if (file) {
534534
struct inode *inode = file_inode(vma->vm_file);
535+
#ifdef CONFIG_KSU_SUSFS_SUS_MAP
536+
if (unlikely(inode->i_mapping->flags & BIT_SUS_MAPS) && susfs_is_current_non_root_user_app_proc()) {
537+
seq_setwidth(m, 25 + sizeof(void *) * 6 - 1);
538+
seq_put_hex_ll(m, NULL, vma->vm_start, 8);
539+
seq_put_hex_ll(m, "-", vma->vm_end, 8);
540+
seq_putc(m, ' ');
541+
seq_putc(m, '-');
542+
seq_putc(m, '-');
543+
seq_putc(m, '-');
544+
seq_putc(m, 'p');
545+
seq_put_hex_ll(m, " ", pgoff, 8);
546+
seq_put_hex_ll(m, " ", MAJOR(dev), 2);
547+
seq_put_hex_ll(m, ":", MINOR(dev), 2);
548+
seq_put_decimal_ull(m, " ", ino);
549+
seq_putc(m, ' ');
550+
goto done;
551+
}
552+
#endif
535553
#ifdef CONFIG_KSU_SUSFS_SUS_KSTAT
536554
if (unlikely(inode->i_mapping->flags & BIT_SUS_KSTAT)) {
537555
susfs_sus_ino_for_show_map_vma(inode->i_ino, &dev, &ino);
@@ -1090,15 +1108,30 @@ static int show_smap(struct seq_file *m, void *v, int is_pid)
10901108
}
10911109

10921110
if (!rollup_mode)
1111+
#ifdef CONFIG_KSU_SUSFS_SUS_MAP
1112+
if (vma->vm_file &&
1113+
unlikely(file_inode(vma->vm_file)->i_mapping->flags & BIT_SUS_MAPS) &&
1114+
susfs_is_current_non_root_user_app_proc())
1115+
{
1116+
seq_printf(m,
1117+
"Size: %8lu kB\n"
1118+
"KernelPageSize: %8lu kB\n"
1119+
"MMUPageSize: %8lu kB\n",
1120+
(vma->vm_end - vma->vm_start) >> 10,
1121+
4, 4);
1122+
goto bypass_orig_flow;
1123+
}
1124+
#endif
10931125
seq_printf(m,
10941126
"Size: %8lu kB\n"
10951127
"KernelPageSize: %8lu kB\n"
10961128
"MMUPageSize: %8lu kB\n",
10971129
(vma->vm_end - vma->vm_start) >> 10,
10981130
vma_kernel_pagesize(vma) >> 10,
10991131
vma_mmu_pagesize(vma) >> 10);
1100-
1101-
1132+
#ifdef CONFIG_KSU_SUSFS_SUS_MAP
1133+
bypass_orig_flow:
1134+
#endif
11021135
if (!rollup_mode || last_vma)
11031136
seq_printf(m,
11041137
"Rss: %8lu kB\n"
@@ -1135,9 +1168,22 @@ static int show_smap(struct seq_file *m, void *v, int is_pid)
11351168
(unsigned long)(mss->pss_locked >> (10 + PSS_SHIFT)));
11361169

11371170
if (!rollup_mode) {
1171+
#ifdef CONFIG_KSU_SUSFS_SUS_MAP
1172+
if (vma->vm_file &&
1173+
unlikely(file_inode(vma->vm_file)->i_mapping->flags & BIT_SUS_MAPS) &&
1174+
susfs_is_current_non_root_user_app_proc())
1175+
{
1176+
seq_puts(m, "VmFlags: mr mw me");
1177+
seq_putc(m, '\n');
1178+
goto bypass_orig_flow2;
1179+
}
1180+
#endif
11381181
arch_show_smap(m, vma);
11391182
show_smap_vma_flags(m, vma);
11401183
}
1184+
#ifdef CONFIG_KSU_SUSFS_SUS_MAP
1185+
bypass_orig_flow2:
1186+
#endif
11411187
m_cache_vma(m, vma);
11421188
return ret;
11431189
}
@@ -1772,6 +1818,9 @@ static ssize_t pagemap_read(struct file *file, char __user *buf,
17721818
unsigned long start_vaddr;
17731819
unsigned long end_vaddr;
17741820
int ret = 0, copied = 0;
1821+
#ifdef CONFIG_KSU_SUSFS_SUS_MAP
1822+
struct vm_area_struct *vma;
1823+
#endif
17751824

17761825
if (!mm || !mmget_not_zero(mm))
17771826
goto out;
@@ -1830,6 +1879,16 @@ static ssize_t pagemap_read(struct file *file, char __user *buf,
18301879
down_read(&mm->mmap_sem);
18311880
ret = walk_page_range(start_vaddr, end, &pagemap_walk);
18321881
up_read(&mm->mmap_sem);
1882+
#ifdef CONFIG_KSU_SUSFS_SUS_MAP
1883+
vma = find_vma(mm, start_vaddr);
1884+
if (vma && vma->vm_file) {
1885+
struct inode *inode = file_inode(vma->vm_file);
1886+
if (unlikely(inode->i_mapping->flags & BIT_SUS_MAPS) && susfs_is_current_non_root_user_app_proc()) {
1887+
pm.show_pfn = false;
1888+
pm.buffer->pme = 0;
1889+
}
1890+
}
1891+
#endif
18331892
start_vaddr = end;
18341893

18351894
len = min(count, PM_ENTRY_BYTES * pm.pos);

fs/susfs.c

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1215,6 +1215,41 @@ static int copy_config_to_buf(const char *config_string, char *buf_ptr, size_t *
12151215
return 0;
12161216
}
12171217

1218+
/* sus_map */
1219+
#ifdef CONFIG_KSU_SUSFS_SUS_MAP
1220+
int susfs_add_sus_map(struct st_susfs_sus_map* __user user_info) {
1221+
struct st_susfs_sus_map info;
1222+
struct path path;
1223+
struct inode *inode = NULL;
1224+
int err = 0;
1225+
1226+
err = copy_from_user(&info, user_info, sizeof(info));
1227+
if (err) {
1228+
SUSFS_LOGE("failed copying from userspace\n");
1229+
return err;
1230+
}
1231+
1232+
err = kern_path(info.target_pathname, LOOKUP_FOLLOW, &path);
1233+
if (err) {
1234+
SUSFS_LOGE("Failed opening file '%s'\n", info.target_pathname);
1235+
return err;
1236+
}
1237+
1238+
if (!path.dentry->d_inode) {
1239+
err = -EINVAL;
1240+
goto out_path_put_path;
1241+
}
1242+
inode = d_inode(path.dentry);
1243+
spin_lock(&inode->i_lock);
1244+
set_bit(AS_FLAGS_SUS_MAP, &inode->i_mapping->flags);
1245+
SUSFS_LOGI("pathname: '%s', is flagged as AS_FLAGS_SUS_MAP\n", info.target_pathname);
1246+
spin_unlock(&inode->i_lock);
1247+
out_path_put_path:
1248+
path_put(&path);
1249+
return err;
1250+
}
1251+
#endif // #ifdef CONFIG_KSU_SUSFS_SUS_MAP
1252+
12181253
int susfs_get_enabled_features(char __user* buf, size_t bufsize) {
12191254
char *kbuf = NULL, *buf_ptr = NULL;
12201255
size_t copied_size = 0;
@@ -1291,6 +1326,11 @@ int susfs_get_enabled_features(char __user* buf, size_t bufsize) {
12911326
if (err) goto out_kfree_kbuf;
12921327
buf_ptr = kbuf + copied_size;
12931328
#endif
1329+
#ifdef CONFIG_KSU_SUSFS_SUS_MAP
1330+
err = copy_config_to_buf("CONFIG_KSU_SUSFS_SUS_MAP\n", buf_ptr, &copied_size, bufsize);
1331+
if (err) goto out_kfree_kbuf;
1332+
buf_ptr = kbuf + copied_size;
1333+
#endif
12941334
#ifdef CONFIG_KSU_SUSFS_HAS_MAGIC_MOUNT
12951335
err = copy_config_to_buf("CONFIG_KSU_SUSFS_HAS_MAGIC_MOUNT\n", buf_ptr, &copied_size, bufsize);
12961336
if (err) goto out_kfree_kbuf;

include/linux/susfs.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,13 @@ struct st_sus_su {
134134
};
135135
#endif
136136

137+
/* sus_map */
138+
#ifdef CONFIG_KSU_SUSFS_SUS_MAP
139+
struct st_susfs_sus_map {
140+
char target_pathname[SUSFS_MAX_LEN_PATHNAME];
141+
};
142+
#endif
143+
137144
/***********************/
138145
/* FORWARD DECLARATION */
139146
/***********************/
@@ -192,6 +199,10 @@ struct filename* susfs_get_redirected_path(unsigned long ino);
192199
int susfs_get_sus_su_working_mode(void);
193200
int susfs_sus_su(struct st_sus_su* __user user_info);
194201
#endif
202+
/* sus_map */
203+
#ifdef CONFIG_KSU_SUSFS_SUS_MAP
204+
int susfs_add_sus_map(struct st_susfs_sus_map* __user user_info);
205+
#endif
195206

196207
int susfs_get_enabled_features(char __user* buf, size_t bufsize);
197208
void susfs_set_avc_log_spoofing(bool enabled);

include/linux/susfs_def.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
#define CMD_SUSFS_IS_SUS_SU_READY 0x555f0
3030
#define CMD_SUSFS_SUS_SU 0x60000
3131
#define CMD_SUSFS_ENABLE_AVC_LOG_SPOOFING 0x60010
32+
#define CMD_SUSFS_ADD_SUS_MAP 0x60020
3233

3334
#define SUSFS_MAX_LEN_PATHNAME 256 // 256 should address many paths already unless you are doing some strange experimental stuff, then set your own desired length
3435
#define SUSFS_FAKE_CMDLINE_OR_BOOTCONFIG_SIZE 4096
@@ -60,12 +61,14 @@
6061
#define AS_FLAGS_OPEN_REDIRECT 27
6162
#define AS_FLAGS_ANDROID_DATA_ROOT_DIR 28
6263
#define AS_FLAGS_SDCARD_ROOT_DIR 29
64+
#define AS_FLAGS_SUS_MAP 30
6365
#define BIT_SUS_PATH BIT(24)
6466
#define BIT_SUS_MOUNT BIT(25)
6567
#define BIT_SUS_KSTAT BIT(26)
6668
#define BIT_OPEN_REDIRECT BIT(27)
6769
#define BIT_ANDROID_DATA_ROOT_DIR BIT(28)
6870
#define BIT_ANDROID_SDCARD_ROOT_DIR BIT(29)
71+
#define BIT_SUS_MAPS BIT(30)
6972

7073
#define ND_STATE_LOOKUP_LAST 32
7174
#define ND_STATE_OPEN_LAST 64

0 commit comments

Comments
 (0)