-
Notifications
You must be signed in to change notification settings - Fork 2
/
ioctl.c
438 lines (403 loc) · 9.63 KB
/
ioctl.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
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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
#include <linux/compat.h>
#include <linux/mount.h>
#include <linux/uaccess.h>
#include "aeon.h"
#include "aeon_super.h"
#include "aeon_dir.h"
#include "aeon_compression.h"
enum failure_type {
CREATE = 1,
DELETE1,
DELETE2,
DELETE3,
CREATE_ID1,
CREATE_ID2,
CREATE_ID3,
CREATE_ID4,
RENAME_ID1,
RENAME_ID2,
MKDIR_1 = 11,
MKDIR_2,
MKDIR_3,
MKDIR_4,
MKDIR_5,
MKDIR_6,
LINK_1,
LINK_2,
UNLINK_1,
UNLINK_1_1,
UNLINK_2 = 21,
RENAME_1,
};
static inline __u32 aeon_mask_flags(umode_t mode, __u32 flags)
{
if (S_ISDIR(mode))
return flags;
else if (S_ISREG(mode))
return flags & AEON_REG_FLMASK;
else
return flags & AEON_OTHER_FLMASK;
}
long aeon_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
{
struct inode *inode = file_inode(filp);
struct super_block *sb = inode->i_sb;
struct aeon_inode *pi;
unsigned int flags;
int ret;
pi = aeon_get_inode(inode->i_sb, &AEON_I(inode)->header);
switch (cmd) {
case AEON_IOC_GETFLAGS:
flags = le32_to_cpu(pi->i_flags) & AEON_FL_USER_VISIBLE;
return put_user(flags, (int __user *)arg);
case AEON_IOC_SETFLAGS: {
unsigned int oldflags;
ret = mnt_want_write_file(filp);
if (ret)
return ret;
if (!inode_owner_or_capable(inode)) {
ret = -EACCES;
goto setflags_out;
}
if (get_user(flags, (int __user *)arg)) {
ret = -EFAULT;
goto setflags_out;
}
flags = aeon_mask_flags(inode->i_mode, flags);
inode_lock(inode);
oldflags = le32_to_cpu(pi->i_flags);
if ((flags ^ oldflags) & (AEON_APPEND_FL | AEON_IMMUTABLE_FL)) {
if (!capable(CAP_LINUX_IMMUTABLE)) {
inode_unlock(inode);
ret = -EPERM;
goto setflags_out;
}
}
flags = flags & AEON_FL_USER_MODIFIABLE;
flags |= oldflags & ~AEON_FL_USER_MODIFIABLE;
pi->i_flags = cpu_to_le32(flags);
aeon_set_inode_flags(inode, pi, flags);
inode->i_ctime = current_time(inode);
pi->i_ctime = cpu_to_le32(inode->i_ctime.tv_sec);
inode_unlock(inode);
setflags_out:
mnt_drop_write_file(filp);
return ret;
}
case AEON_IOC_GETVERSION:
return put_user(inode->i_generation, (int __user *)arg);
case AEON_IOC_SETVERSION: {
__u32 generation;
if (!inode_owner_or_capable(inode))
return -EPERM;
ret = mnt_want_write_file(filp);
if (ret)
return ret;
if (get_user(generation, (int __user *)arg)) {
ret = -EFAULT;
goto setversion_out;
}
inode_lock(inode);
inode->i_ctime = current_time(inode);
inode->i_generation = generation;
pi->i_ctime = cpu_to_le32(inode->i_ctime.tv_sec);
pi->i_generation = cpu_to_le32(generation);
inode_unlock(inode);
setversion_out:
mnt_drop_write_file(filp);
return ret;
}
case AEON_IOC_INODE_ATTACK: {
struct aeon_inode *pi;
enum failure_type type;
if (get_user(type, (int __user *)arg))
return -EFAULT;
pi = aeon_get_inode(sb, &AEON_I(inode)->header);
aeon_dbg("Destroy inode (ino %u) illegaly type %d\n",
le32_to_cpu(pi->aeon_ino), type);
switch (type) {
case CREATE:
memset(pi, 0, sizeof(*pi));
break;
/*
* valid and deleted flags are important flags.
* If valid is zero and deleted are one, AEON regards as
* inode is fully discarded.
*/
case DELETE1:
pi->valid = 0;
pi->deleted = 0;
break;
case DELETE2:
pi->valid = 1;
pi->deleted = 1;
break;
case DELETE3:
pi->valid = 1;
pi->deleted = 0;
pi->csum = 21;
break;
default:
return -EFAULT;
}
fs_persisted = 0;
return 0;
}
case AEON_IOC_DENTRY_ATTACK: {
struct aeon_inode *pi;
struct aeon_dentry *de;
u64 de_addr = 0;
enum failure_type type;
if (get_user(type, (int __user *)arg))
return -EFAULT;
pi = aeon_get_inode(sb, &AEON_I(inode)->header);
aeon_get_dentry_address(sb, pi, &de_addr);
de = (struct aeon_dentry *)de_addr;
switch (type) {
case CREATE:
memset(de, 0, sizeof(*de));
break;
default:
return -EFAULT;
}
aeon_dbg("Destroy inode (ino %u) illegaly\n",
le32_to_cpu(pi->aeon_ino));
fs_persisted = 0;
return 0;
}
case AEON_IOC_CHILD_ID_ATTACK: {
struct aeon_inode *pi;
struct aeon_dentry *de;
u64 de_addr = 0;
enum failure_type type;
if (get_user(type, (int __user *)arg))
ret = -EFAULT;
pi = aeon_get_inode(sb, &AEON_I(inode)->header);
aeon_get_dentry_address(sb, pi, &de_addr);
de = (struct aeon_dentry *)de_addr;
switch (type) {
/*
* If valid doesn't set, metadata is regarded as not exisit
* memo: CREATE_ID1 = 5
*/
case CREATE_ID1:
/* No problem in fact */
pi->csum = 532;
de->csum = 51;
break;
case CREATE_ID2:
/* A inode doen't have legal info */
memset(pi, 0, sizeof(*pi));
de->csum = 0;
break;
case CREATE_ID3:
/* A dentry doesn't have legal info and
* pi is not persisted.
*/
memset(de, 0, sizeof(*de));
pi->csum = 32;
break;
case CREATE_ID4: {
struct aeon_inode *pidir;
pidir = aeon_get_pinode(sb, &AEON_I(inode)->header);
pidir->csum = 91;
break;
}
case RENAME_ID1: {
/* links count is not added */
struct aeon_inode *pidir;
pidir = aeon_get_pinode(sb, &AEON_I(inode)->header);
pidir->i_links_count--;
pidir->csum = 91;
aeon_dbg("ino %u\n", le32_to_cpu(pidir->aeon_ino));
break;
}
case RENAME_ID2: {
/* links count is not substracted */
struct aeon_inode *pidir;
pidir = aeon_get_pinode(sb, &AEON_I(inode)->header);
pidir->i_links_count++;
pidir->csum = 91;
break;
}
/* MKDIR operation involves adding a directory entry to the
* parent directory, updating the new child inode.
* In AEON, Creating a new directory block for the child inode
* happen when the first time of creating directory entry.
*/
case MKDIR_1: {
/* Blocks dropped: C_inode
* Error: Parent - bad dir entry
* Child - Orphan block
* Key for Actiopn:
* Block/inode reclaimed on scan
* Child - Error on inode access
*/
memset(pi, 0, sizeof(*pi));
break;
}
case MKDIR_2: {
/* Blocks dropped: C_dir
* Error: Child - bad dir entry
* Key for Action:
* Child - Error on data access
*/
memset(de, 0, sizeof(*de));
break;
}
case MKDIR_3: {
/* Blocks dropped: P_dir
* Error: Child - Orphan inode
* Child - Bad dir entry
* Key for Action:
* Block/inode reclaimed on scan
*/
/* Note that P_dir and C_dir have the same structure
* in same address.
*/
//Is three cases needed?
struct aeon_inode *pidir;
pidir = aeon_get_pinode(sb, &AEON_I(inode)->header);
pidir->i_links_count--;
aeon_update_inode_csum(pidir);
memset(de, 0, sizeof(*de));
break;
}
case MKDIR_4: {
/* Blocks dropped: C_inode
* C_dir
* Error: Parent - Bad dir entry
* Child - Bad dir entry
* Key for Action:
* Error on inode access
*/
pi->i_dentry_addr = 0;
memset(de, 0, sizeof(*de));
break;
}
case MKDIR_5: {
/* Blocks dropped: C_inode
* P_dir
* Error: Child - Orphan block
* Key for Action:
* Block/inode reclaimed on scan
*/
struct aeon_inode *pidir;
pidir = aeon_get_pinode(sb, &AEON_I(inode)->header);
pidir->i_links_count--;
aeon_update_inode_csum(pidir);
pi->csum = 0;
break;
}
case MKDIR_6: {
/* Blocks dropped: C_dir
* P_dir
* Error: Child - Orphan inode
* Key for Action:
* Block/inode reclaimed on scan
*/
memset(de, 0, sizeof(*de));
break;
}
case LINK_1: {
/* Blocks dropped: C_inode
* Error: Child - Wrong hard link count
* Key for Action:
* Error on access via new path
*/
memset(pi, 0, sizeof(*pi));
break;
}
case LINK_2: {
/* Blocks dropped: P_dir
* Error: Child - Orphan inode
* Key for Action:
* Block/inode reclaimed on scan
*/
struct aeon_inode *pidir;
pidir = aeon_get_pinode(sb, &AEON_I(inode)->header);
pidir->i_links_count--;
break;
}
case UNLINK_1: {
/* Blocks dropped: C_inode
* Error: Child - Wrong hard link count
* Key for Action:
* Child - Error on access via old path
*/
struct aeon_inode *pidir;
pidir = aeon_get_pinode(sb, &AEON_I(inode)->header);
pidir->i_links_count++;
break;
}
case UNLINK_1_1: {
struct aeon_inode *pidir;
pidir = aeon_get_pinode(sb, &AEON_I(inode)->header);
pidir->i_links_count++;
aeon_update_inode_csum(pidir);
break;
}
case UNLINK_2: {
/* Block dropped: O_dir (Old file/parent directory block)
* Error: Parent - Bad dir entry
* Key for Action:
* Child - Error on inode access
*/
memset(de, 0, sizeof(*de));
break;
}
/* Block dropped: O_dir
* Error: Old & New file/parent - Multiple entries
* Key for Action:
* Child - Error on inode access
*/
case RENAME_1: {
/* trace set_link() */
/* Script needs to create 2 files in same directory */
struct aeon_dentry *src = de;
struct aeon_dentry *dest = ++de;
dest->ino = pi->aeon_ino;
dest->d_inode_addr = src->d_inode_addr;
dest->d_pinode_addr = src->d_pinode_addr;
aeon_update_dentry_csum(dest);
pi->i_dentry_addr = dest->d_dentry_addr;
aeon_update_inode_csum(pi);
/* remain old dentry */
break;
}
default:
return -EFAULT;
}
fs_persisted = 0;
return 0;
}
case AEON_IOC_TEST_COMPRESSION:
aeon_info("comression test\n");
//try_api();
return 0;
default:
return -ENOTTY;
}
}
#ifdef CONFIG_COMPAT
long aeon_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
{
switch (cmd) {
case AEON_IOC32_GETFLAGS:
cmd = AEON_IOC_GETFLAGS;
break;
case AEON_IOC32_SETFLAGS:
cmd = AEON_IOC_SETFLAGS;
break;
case AEON_IOC32_GETVERSION:
cmd = AEON_IOC_GETVERSION;
break;
case AEON_IOC32_SETVERSION:
cmd = AEON_IOC_SETVERSION;
break;
default:
return -ENOIOCTLCMD;
}
return aeon_ioctl(file, cmd, (unsigned long)compat_ptr(arg));
}
#endif