forked from Deadpoolmine/HUNTER-File-System
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mprotect.h
271 lines (222 loc) · 7.13 KB
/
mprotect.h
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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
/*
* BRIEF DESCRIPTION
*
* Memory protection definitions for the HUNTER filesystem.
*
* Copyright 2023-2024 Regents of the University of Harbin Institute of Technology, Shenzhen
* Computer science and technology, Yanqi Pan <[email protected]>
* Copyright 2012-2013 Intel Corporation
* Copyright 2009-2011 Marco Stornelli <[email protected]>
* Copyright 2003 Sony Corporation
* Copyright 2003 Matsushita Electric Industrial Co., Ltd.
* 2003-2004 (c) MontaVista Software, Inc. , Steve Longerbeam
*
* This program is free software; you can redistribute it and/or modify it
*
* This file is licensed under the terms of the GNU General Public
* License version 2. This program is licensed "as is" without any
* warranty of any kind, whether express or implied.
*/
#ifndef _HK_WPROTECT_H
#define _HK_WPROTECT_H
#include "hunter.h"
extern void hk_error_mng(struct super_block *sb, const char *fmt, ...);
static inline int hk_range_check(struct super_block *sb, void *p,
unsigned long len)
{
struct hk_sb_info *sbi = HK_SB(sb);
if (p < sbi->virt_addr ||
p + len > sbi->virt_addr + sbi->initsize) {
hk_err(sb, "access pmem out of range: pmem range 0x%lx - 0x%lx, "
"access range 0x%lx - 0x%lx\n",
(unsigned long)sbi->virt_addr,
(unsigned long)(sbi->virt_addr + sbi->initsize),
(unsigned long)p, (unsigned long)(p + len));
dump_stack();
return -EINVAL;
}
return 0;
}
static inline void wprotect_disable(void)
{
unsigned long cr0_val;
cr0_val = read_cr0();
cr0_val &= (~X86_CR0_WP);
write_cr0(cr0_val);
}
static inline void wprotect_enable(void)
{
unsigned long cr0_val;
cr0_val = read_cr0();
cr0_val |= X86_CR0_WP;
write_cr0(cr0_val);
}
/* FIXME: Assumes that we are always called in the right order.
* hk_writeable(vaddr, size, 1);
* hk_writeable(vaddr, size, 0);
*/
static inline int
hk_writeable(void *vaddr, unsigned long size, int rw, unsigned long *flags)
{
INIT_TIMING(wprotect_time);
HK_START_TIMING(wprotect_t, wprotect_time);
if (rw) {
local_irq_save(*flags);
wprotect_disable();
} else {
wprotect_enable();
local_irq_restore(*flags);
}
HK_END_TIMING(wprotect_t, wprotect_time);
return 0;
}
extern int wprotect;
static inline int hk_is_protected(struct super_block *sb)
{
struct hk_sb_info *sbi = (struct hk_sb_info *)sb->s_fs_info;
if (wprotect)
return wprotect;
return sbi->s_mount_opt & HUNTER_MOUNT_PROTECT;
}
static inline int hk_is_wprotected(struct super_block *sb)
{
return hk_is_protected(sb);
}
static inline void
__hk_memunlock_range(void *p, unsigned long len, unsigned long *flags)
{
/*
* NOTE: Ideally we should lock all the kernel to be memory safe
* and avoid to write in the protected memory,
* obviously it's not possible, so we only serialize
* the operations at fs level. We can't disable the interrupts
* because we could have a deadlock in this path.
*/
hk_writeable(p, len, 1, flags);
}
static inline void
__hk_memlock_range(void *p, unsigned long len, unsigned long *flags)
{
hk_writeable(p, len, 0, flags);
}
static inline void hk_memunlock_range(struct super_block *sb, void *p,
unsigned long len, unsigned long *flags)
{
if (hk_range_check(sb, p, len))
return;
if (hk_is_protected(sb))
__hk_memunlock_range(p, len, flags);
}
static inline void hk_memlock_range(struct super_block *sb, void *p,
unsigned long len, unsigned long *flags)
{
if (hk_is_protected(sb))
__hk_memlock_range(p, len, flags);
}
static inline void hk_memunlock_super(struct super_block *sb, unsigned long *flags)
{
struct hk_super_block *ps = hk_get_super(sb);
if (hk_is_protected(sb))
__hk_memunlock_range(ps, HK_SB_SIZE, flags);
}
static inline void hk_memlock_super(struct super_block *sb, unsigned long *flags)
{
struct hk_super_block *ps = hk_get_super(sb);
if (hk_is_protected(sb))
__hk_memlock_range(ps, HK_SB_SIZE, flags);
}
static inline void hk_memunlock_hdr(struct super_block *sb,
struct hk_header *hdr, unsigned long *flags)
{
if (hk_range_check(sb, hdr, sizeof(struct hk_header)))
return;
if (hk_is_protected(sb))
__hk_memunlock_range(hdr, sizeof(struct hk_header), flags);
}
static inline void hk_memlock_hdr(struct super_block *sb,
struct hk_header *hdr, unsigned long *flags)
{
if (hk_is_protected(sb))
__hk_memlock_range(hdr, sizeof(struct hk_header), flags);
}
static inline void hk_memunlock_attr_log(struct super_block *sb,
struct hk_attr_log *rg, unsigned long *flags)
{
if (hk_range_check(sb, rg, sizeof(struct hk_attr_log)))
return;
if (hk_is_protected(sb))
__hk_memunlock_range(rg, sizeof(struct hk_attr_log), flags);
}
static inline void hk_memlock_attr_log(struct super_block *sb,
struct hk_attr_log *rg, unsigned long *flags)
{
if (hk_is_protected(sb))
__hk_memlock_range(rg, sizeof(struct hk_attr_log), flags);
}
static inline void hk_memunlock_journal(struct super_block *sb,
struct hk_journal *jnl, unsigned long *flags)
{
if (hk_range_check(sb, jnl, sizeof(struct hk_journal)))
return;
if (hk_is_protected(sb))
__hk_memunlock_range(jnl, sizeof(struct hk_journal), flags);
}
static inline void hk_memlock_journal(struct super_block *sb,
struct hk_journal *jnl, unsigned long *flags)
{
if (hk_is_protected(sb))
__hk_memlock_range(jnl, sizeof(struct hk_journal), flags);
}
static inline void hk_memunlock_dentry(struct super_block *sb,
struct hk_dentry *direntry, unsigned long *flags)
{
if (hk_range_check(sb, direntry, sizeof(struct hk_dentry)))
return;
if (hk_is_protected(sb))
__hk_memunlock_range(direntry, sizeof(struct hk_dentry), flags);
}
static inline void hk_memlock_dentry(struct super_block *sb,
struct hk_dentry *direntry, unsigned long *flags)
{
if (hk_is_protected(sb))
__hk_memlock_range(direntry, sizeof(struct hk_dentry), flags);
}
static inline void hk_memunlock_all(struct super_block *sb, unsigned long *flags)
{
struct hk_sb_info *sbi = (struct hk_sb_info *)sb->s_fs_info;
if (hk_is_protected(sb))
__hk_memunlock_range(sbi->virt_addr, sbi->initsize, flags);
}
static inline void hk_memlock_all(struct super_block *sb, unsigned long *flags)
{
struct hk_sb_info *sbi = (struct hk_sb_info *)sb->s_fs_info;
if (hk_is_protected(sb))
__hk_memlock_range(sbi->virt_addr, sbi->initsize, flags);
}
static inline void hk_memunlock_pi(struct super_block *sb,
struct hk_inode *pi, unsigned long *flags)
{
if (hk_range_check(sb, pi, sizeof(struct hk_inode)))
return;
if (hk_is_protected(sb))
__hk_memunlock_range(pi, sizeof(struct hk_inode), flags);
}
static inline void hk_memlock_pi(struct super_block *sb,
struct hk_inode *pi, unsigned long *flags)
{
if (hk_is_protected(sb))
__hk_memlock_range(pi, sizeof(struct hk_inode), flags);
}
static inline void hk_memunlock_block(struct super_block *sb, void *bp, unsigned long *flags)
{
if (hk_range_check(sb, bp, sb->s_blocksize))
return;
if (hk_is_protected(sb))
__hk_memunlock_range(bp, sb->s_blocksize, flags);
}
static inline void hk_memlock_block(struct super_block *sb, void *bp, unsigned long *flags)
{
if (hk_is_protected(sb))
__hk_memlock_range(bp, sb->s_blocksize, flags);
}
#endif /* _HK_WPROTECT_H */