Skip to content

Commit e9d8201

Browse files
committed
get fops address
1 parent 3c48a44 commit e9d8201

File tree

2 files changed

+163
-0
lines changed

2 files changed

+163
-0
lines changed

module/Makefile

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
MODULE_FILE := procowner.ko
2+
3+
# Linux (default)
4+
X86_ARCH := x86
5+
X86_KDIR := /lib/modules/$(shell uname -r)/build
6+
7+
x86: ARCH := ${X86_ARCH}
8+
x86: KDIR := ${X86_KDIR}
9+
x86-clean : ARCH := $(X86_ARCH)
10+
11+
# Kernel module
12+
obj-m := procowner.o
13+
14+
# Build targets
15+
all: x86
16+
x86: build
17+
x86-clean: clean
18+
19+
build:
20+
@make \
21+
ARCH=$(ARCH) \
22+
CROSS_COMPILE=$(CCPATH) \
23+
-C $(KDIR) \
24+
M=$(PWD) \
25+
modules
26+
27+
clean:
28+
@rm -rf \
29+
*.o \
30+
*.ko \
31+
*.mod.c \
32+
modules.order \
33+
Module.symvers

module/procowner.c

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
#include <linux/module.h>
2+
#include <linux/kernel.h>
3+
#include <linux/proc_fs.h>
4+
#include <linux/seq_file.h>
5+
#include <linux/kallsyms.h>
6+
#include <linux/rwlock.h>
7+
8+
#define MOD "[proc-owner]: "
9+
10+
int (*xlate)(const char *name, struct proc_dir_entry **ret, const char **residual);
11+
struct proc_dir_entry* (*subdir_find)(struct proc_dir_entry *dir, const char *name, unsigned int len);
12+
void* subdir_lock;
13+
const char *(*syms_lookup)(unsigned long addr, unsigned long *symbolsize, unsigned long *offset, char **modname, char *namebuf);
14+
15+
struct proc_dir_entry {
16+
unsigned int low_ino;
17+
umode_t mode;
18+
nlink_t nlink;
19+
kuid_t uid;
20+
kgid_t gid;
21+
loff_t size;
22+
const struct inode_operations *proc_iops;
23+
const struct file_operations *proc_fops;
24+
struct proc_dir_entry *parent;
25+
struct rb_root subdir;
26+
struct rb_node subdir_node;
27+
void *data;
28+
atomic_t count; /* use count */
29+
atomic_t in_use; /* number of callers into module in progress; */
30+
/* negative -> it's going away RSN */
31+
struct completion *pde_unload_completion;
32+
struct list_head pde_openers; /* who did ->open, but not ->release */
33+
spinlock_t pde_unload_lock; /* proc_fops checks and pde_users bumps */
34+
u8 namelen;
35+
char name[];
36+
};
37+
38+
39+
// ----------------------------------------------------------------------------------------------------------
40+
static void get_owner(const char* fname) {
41+
int rv;
42+
unsigned int len;
43+
unsigned long symbolsize, offset;
44+
const char* fn = fname;
45+
char* modname = NULL, tmpstr;
46+
char namebuf[128];
47+
struct proc_dir_entry *de = NULL;
48+
49+
spin_lock(subdir_lock);
50+
rv = xlate(fname, &de, &fn);
51+
printk(KERN_INFO MOD "Ret: %d, Residual: %s\n", rv, fn);
52+
if(rv != 0) {
53+
spin_unlock(subdir_lock);
54+
return;
55+
}
56+
len = strlen(fn);
57+
de = subdir_find(de, fn, len);
58+
if(de) {
59+
printk(KERN_INFO MOD "Name: %s, proc fops: %p\n", de->name, de->proc_fops);
60+
tmpstr = syms_lookup((size_t)(de->proc_fops), &symbolsize, &offset, &modname, namebuf);
61+
printk(KERN_INFO MOD "Modname: %s\n", modname);
62+
}
63+
spin_unlock(subdir_lock);
64+
65+
}
66+
67+
68+
// ----------------------------------------------------------------------------------------------------------
69+
static int proc_read(struct seq_file *m, void *v) {
70+
seq_printf(m, "test\n");
71+
get_owner("uptime");
72+
73+
return 0;
74+
}
75+
76+
// ----------------------------------------------------------------------------------------------------------
77+
static int pm_open(struct inode *i, struct file *f) {
78+
return single_open(f, proc_read, NULL);
79+
}
80+
81+
// ----------------------------------------------------------------------------------------------------------
82+
static const struct file_operations temp_proc_fops = {
83+
.owner = THIS_MODULE,
84+
.open = pm_open,
85+
.read = seq_read,
86+
.release = single_release,
87+
};
88+
89+
// ----------------------------------------------------------------------------------------------------------
90+
static int __init procowner_init(void)
91+
{
92+
printk(KERN_INFO MOD "module start\n");
93+
proc_create("procowner", 0, NULL, &temp_proc_fops);
94+
95+
xlate = kallsyms_lookup_name("__xlate_proc_name");
96+
if(!xlate) {
97+
printk(KERN_INFO MOD "__xlate_proc_name not found!\n");
98+
return -ENODEV;
99+
}
100+
subdir_lock = kallsyms_lookup_name("proc_subdir_lock");
101+
if(!subdir_lock) {
102+
printk(KERN_INFO MOD "proc_subdir_lock not found!\n");
103+
return -ENODEV;
104+
}
105+
subdir_find = kallsyms_lookup_name("pde_subdir_find");
106+
if(!subdir_find) {
107+
printk(KERN_INFO MOD "pde_subdir_find not found!\n");
108+
return -ENODEV;
109+
}
110+
syms_lookup = kallsyms_lookup_name("kallsyms_lookup");
111+
if(!syms_lookup) {
112+
printk(KERN_INFO MOD "kallsyms_lookup not found!\n");
113+
return -ENODEV;
114+
}
115+
116+
return 0;
117+
}
118+
119+
// ----------------------------------------------------------------------------------------------------------
120+
static void __exit procowner_exit(void)
121+
{
122+
remove_proc_entry("procowner", NULL);
123+
124+
printk(KERN_INFO MOD "module end\n");
125+
}
126+
127+
128+
module_init(procowner_init);
129+
module_exit(procowner_exit);
130+
MODULE_LICENSE("GPL");

0 commit comments

Comments
 (0)