-
Notifications
You must be signed in to change notification settings - Fork 3
/
nitara2.c
235 lines (191 loc) · 6.92 KB
/
nitara2.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
/*
* original idea: madsys, "Finding hidden kernel modules (the extrem way)"
* http://phrack.org/issues/61/3.html
*
* usage: cat /proc/nitara2 && dmesg
*/
#include <asm/page.h>
#include <asm/fixmap.h>
#include <linux/errno.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/proc_fs.h>
#include <linux/version.h>
#if LINUX_VERSION_CODE >= KERNEL_VERSION(6, 5, 0)
# include <linux/mm.h>
#elif LINUX_VERSION_CODE >= KERNEL_VERSION(5, 8, 0)
# include <linux/pgtable.h>
#elif LINUX_VERSION_CODE >= KERNEL_VERSION(4, 11, 0)
# include <asm-generic/pgtable-nop4d.h>
#else /* < 4.11 */
// at this point Linux didn't have 5-level pgtables
# include <asm/pgtable.h>
#endif
#include <linux/string.h>
#include <linux/sched.h>
#include <linux/types.h>
#include <linux/unistd.h>
#include <uapi/linux/stat.h>
#ifndef CONFIG_X86_64
# error "arch not supported :("
#endif
#define NITARA_PRINTK(fmt, args...) printk("%s: " fmt, module_name(THIS_MODULE), ##args)
#define NITARA_MODSIZE (0x1000 * PAGE_SIZE)
/* https://patchwork.kernel.org/project/linux-fsdevel/patch/[email protected]/ */
#ifndef sizeof_field
#define sizeof_field(TYPE, MEMBER) sizeof((((TYPE *)0)->MEMBER))
#endif
/* NOTE: arch-specific, we don't handle it properly yet */
#if defined(CONFIG_X86_64) && (LINUX_VERSION_CODE < KERNEL_VERSION(5, 18, 0))
static __always_inline u64 __canonical_address(u64 vaddr, u8 vaddr_bits)
{
return ((s64)vaddr << (64 - vaddr_bits)) >> (64 - vaddr_bits);
}
static __always_inline u64 __is_canonical_address(u64 vaddr, u8 vaddr_bits)
{
return __canonical_address(vaddr, vaddr_bits) == vaddr;
}
#endif
#define is_canonical_48(p) __is_canonical_address((unsigned long)p, 48)
#define is_canonical_or_zero(p) (p == NULL || is_canonical_48(p))
#define is_canonical_high_or_zero(p) (p == NULL || ((unsigned long)p >= VMALLOC_START && is_canonical_48(p)))
/*
* https://stackoverflow.com/questions/11134813/check-validity-of-virtual-memory-address
* https://stackoverflow.com/questions/66593710/how-to-check-an-address-is-accessible-in-the-kernel-space
*
* on 5-level paging introduction (6 Mar 2017, "patchset is build on top of v4.11-rc1"):
* https://lwn.net/Articles/716324/
*/
static bool valid_addr(unsigned long addr, size_t size)
{
pgd_t *pgd;
#if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 11, 0)
p4d_t *p4d;
#endif
pmd_t *pmd;
pud_t *pud;
pte_t *pte;
struct mm_struct *mm = current->mm;
unsigned long end_addr;
pgd = pgd_offset(mm, addr);
if (unlikely(!pgd) || unlikely(pgd_none(*pgd)) || unlikely(!pgd_present(*pgd)) )
return false;
#if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 11, 0)
p4d = p4d_offset(pgd, addr);
if (unlikely(!p4d) || unlikely(p4d_none(*p4d)) || unlikely(!p4d_present(*p4d)) )
return false;
pud = pud_offset(p4d, addr);
#else
pud = pud_offset(pgd, addr);
#endif
if (unlikely(!pud) || unlikely(pud_none(*pud)) || unlikely(!pud_present(*pud)))
return false;
pmd = pmd_offset(pud, addr);
if (unlikely(!pmd) || unlikely(pmd_none(*pmd)) || unlikely(!pmd_present(*pmd)))
return false;
if (pmd_trans_huge(*pmd)) {
end_addr = (((addr >> PMD_SHIFT) + 1) << PMD_SHIFT) - 1;
goto end;
}
// NOTE: pte_offset_map() is unusable out-of-tree on >=6.5.
// As pte_offset_kernel() seems to work, use it instead ¯\_(ツ)_/¯
pte = pte_offset_kernel(pmd, addr);
if (unlikely(!pte) || unlikely(!pte_present(*pte)))
return false;
end_addr = (((addr >> PAGE_SHIFT) + 1) << PAGE_SHIFT) - 1;
end:
if (end_addr >= addr + size - 1)
return true;
return valid_addr(end_addr + 1, size - (end_addr - addr + 1));
}
static bool is_within_modules(void *p)
{
return (unsigned long)p >= MODULES_VADDR && (unsigned long)p < MODULES_END;
}
__maybe_unused static bool is_within_modules_or_zero(void *p)
{
return p == NULL || is_within_modules(p);
}
static bool check_name_valid(char *s)
{
size_t i;
if (!s)
return false;
for (i = 0; i < sizeof_field(struct module, name); i += 1) {
/* we might fail here if the name is "" */
if (s[i] == '\0' && i != 0)
break;
if (s[i] < 0x20 || s[i] > 0x7e)
return false;
}
return true;
}
#if LINUX_VERSION_CODE >= KERNEL_VERSION(6, 4, 0)
#define MODSIZE(p) \
(p->mem[MOD_TEXT].size \
+ p->mem[MOD_INIT_TEXT].size \
+ p->mem[MOD_INIT_DATA].size \
+ p->mem[MOD_INIT_RODATA].size \
+ p->mem[MOD_RO_AFTER_INIT].size \
+ p->mem[MOD_RODATA].size \
+ p->mem[MOD_DATA].size)
#else
# define MODSIZE(p) (p->core_layout.size)
#endif
ssize_t showmodule_read(struct file *unused_file, char *buffer, size_t len, loff_t *off)
{
struct module *p;
unsigned long i;
NITARA_PRINTK("address module size\n");
for (
i = 0, p = (struct module *)MODULES_VADDR;
p <= (struct module*)(MODULES_END - 0x10);
p = ((struct module*)((unsigned long)p + 0x10)), i += 1
) {
if (
valid_addr((unsigned long)p, sizeof(struct module))
&& p->state >= MODULE_STATE_LIVE && p->state <= MODULE_STATE_UNFORMED
&& check_name_valid(p->name)
// may be unset for modules that can also be compiled as part of kernel (?)
&& is_within_modules_or_zero(p->init)
&& is_within_modules_or_zero(p->exit)
&& (p->init || p->exit || p->list.next || p->list.prev)
// https://elixir.bootlin.com/linux/v5.19/source/include/linux/list.h#L146
&& (is_canonical_high_or_zero(p->list.next) || p->list.next == LIST_POISON1)
&& (is_canonical_high_or_zero(p->list.prev) || p->list.prev == LIST_POISON2)
&& MODSIZE(p) && (MODSIZE(p) % PAGE_SIZE == 0)
// https://elixir.bootlin.com/linux/v5.15/source/kernel/module.c#L1130
// && p->taints
) {
NITARA_PRINTK("0x%lx: %20s %u\n", (unsigned long)p, p->name, MODSIZE(p));
}
}
NITARA_PRINTK("end check (total gone %lu steps)\n", i);
return 0;
}
#if LINUX_VERSION_CODE > KERNEL_VERSION(5, 5, 19)
static struct proc_ops nitara2_ops = {
.proc_read = showmodule_read,
.proc_lseek = default_llseek, // otherwise segfaults
};
#else /* < 4.20 (?!?) TODO: check creating proc entry for intermediate versions */
// include/linux/fs.h#L1692
static struct file_operations nitara2_ops = {
.read = showmodule_read,
.llseek = default_llseek,
};
#endif
struct proc_dir_entry *entry;
int init_module()
{
NITARA_PRINTK("[creating proc entry]\n");
entry = proc_create_data("nitara2", S_IRUSR, NULL, &nitara2_ops, NULL);
return 0;
}
void cleanup_module()
{
NITARA_PRINTK("[cleanup proc]\n");
proc_remove(entry);
}
MODULE_LICENSE("GPL");
MODULE_AUTHOR("ksen-lin");