-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathext4_undelete.c
605 lines (488 loc) · 16 KB
/
ext4_undelete.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
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
/*
* File: ext4_undelete.c
* Author: Uhliarik Lubos
*
* Created on March 2, 2014, 9:57 PM
*/
#define _LARGEFILE_SOURCE
#define _LARGEFILE64_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <linux/fs.h>
#include <asm/byteorder.h>
#include <fcntl.h>
#include <string.h>
#include <unistd.h>
#include <libgen.h>
#include "ext4_undelete.h"
#include "debug.h"
#include "ext4_extents.h"
#include <ext2fs/ext3_extents.h>
ext2_filsys current_fs = NULL;
static int list_dir_proc(ext2_ino_t dir EXT2FS_ATTR((unused)),
int entry,
struct ext2_dir_entry *dirent,
int offset EXT2FS_ATTR((unused)),
int blocksize EXT2FS_ATTR((unused)),
char *buf EXT2FS_ATTR((unused)),
void *private)
{
char name[EXT2_NAME_LEN + 1];
int thislen;
struct list_dir_struct *ls;
ls = (struct list_dir_struct *)private;
thislen = dirent->name_len & 0xFF;
strncpy(name, dirent->name, thislen);
name[thislen] = '\0';
if((!ls) || (ls->original_name == NULL)){
fprintf(stderr, "Error: pointer error in list_dir_proc\n");
return 0;
}
if (entry == DIRENT_DELETED_FILE) {
if(strcmp(name, ls->original_name) == 0){
ls->ino = dirent->inode;
}
}
return 0;
}
ext2_ino_t get_ino_deleted_file(char *original_name){
ext2_ino_t inode, cwd = EXT2_ROOT_INO, root = EXT2_ROOT_INO;
struct list_dir_struct ls;
char * dir_name = strdup(original_name);
int flags;
original_name = basename(original_name);
/** get directory inode */
int retval = ext2fs_namei(current_fs, root, cwd, dirname(dir_name), &inode);
if (retval) {
return 0;
}
flags = DIRENT_FLAG_INCLUDE_EMPTY | DIRENT_FLAG_INCLUDE_REMOVED;
ls.ino = 0;
ls.original_name = original_name;
retval = ext2fs_dir_iterate2(current_fs, inode, flags,
0, list_dir_proc, &ls);
return ls.ino;
}
static int count_trailing_zeros(char *data, int bs){
for (int i = bs-1; i >= 0; i--){
if(data[i])
return bs-(i+1);
}
return 0;
}
static int read_last_block(int fd, void *buffer, int bs){
off_t pos;
if ((pos = lseek(fd, 0L, SEEK_END)) < 0)
return -1;
if (lseek(fd, pos-bs, SEEK_SET) < 0)
return -1;
if (bs != read(fd, buffer, bs)){
return -1;
}
return 0;
}
static int strip_trailing_zeros(char *output_name){
char * block = NULL;
int fd;
unsigned int bs = current_fs->blocksize, zeros;
off_t pos;
block = (char *)malloc(bs);
if(block == NULL)
return -1;
fd = open(output_name, O_RDWR, 0600);
if(fd < 0){
free(block);
return -1;
}
if (read_last_block(fd, block, bs) < 0){
free(block);
close(fd);
return -1;
}
zeros = count_trailing_zeros(block, bs);
if (zeros){
/* get file len */
if ((pos = lseek(fd, 0L, SEEK_END)) < 0){
free(block);
close(fd);
return -1;
}
/* truncate */
if(ftruncate(fd, pos-zeros) < 0){
free(block);
close(fd);
return -1;
}
}
close(fd);
return 0;
}
static int check_extent_hdr(struct ext3_extent_header *header){
D(printf("Checking extent header..."));
if (header->eh_depth > EXT4_EXT_MAX_DEPTH) {
fprintf(stderr, "Error: invalid value of eh_depth in extent header!!!\n");
return -1;
}
if (header->eh_entries > header->eh_max) {
fprintf(stderr, "Error: invalid value of eh_depth in extent header!!!\n");
return -1;
}
/* TODO: add other extent checks */
D(printf("OK\n"));
return 0;
}
static int read_inode_full(ext2_ino_t ino, struct ext2_inode * inode,
int bufsize){
int retval;
retval = ext2fs_read_inode_full(current_fs, ino, inode, bufsize);
if (retval) {
fprintf(stderr, "Error: while reading inode %u\n", ino);
return 1;
}
return 0;
}
static int close_filesystem() {
int retval;
retval = ext2fs_close(current_fs);
if (retval)
fprintf(stderr, "ERRROR: failed to close ext2fs\n");
current_fs = NULL;
return 0;
}
static int open_filesystem(char * device, int open_flags, blk_t superblock,
blk_t blocksize) {
int retval;
io_channel data_io = 0;
if (superblock != 0 && blocksize == 0) {
fprintf(stderr, "if you specify the superblock, you must also specify the block size\n");
current_fs = NULL;
return -1;
}
retval = ext2fs_open(device, open_flags, superblock, blocksize,
unix_io_manager, ¤t_fs);
if (retval) {
fprintf(stderr, "Error: can NOT open filesystem\n");
current_fs = NULL;
return -1;
}
retval = ext2fs_read_inode_bitmap(current_fs);
if (retval) {
fprintf(stderr, "Error: while reading inode bitmap\n");
goto errout;
}
retval = ext2fs_read_block_bitmap(current_fs);
if (retval) {
fprintf(stderr, "Error: while reading block bitmap\n");
goto errout;
}
if (data_io) {
retval = ext2fs_set_data_io(current_fs, data_io);
if (retval) {
fprintf(stderr,
"Error: while setting data source\n");
goto errout;
}
}
return 0;
errout:
retval = ext2fs_close(current_fs);
if (retval)
fprintf(stderr, "Error: while trying to close filesystem\n");
current_fs = NULL;
return -1;
}
static int ext4_ext_more_to_iterate(struct ext_path *path) {
if (path->p_idx > EXT_LAST_INDEX(path->p_hdr))
return 0;
return 1;
}
static void block_release(struct block_buffer *bb) {
if (bb != NULL) {
if (bb->data != NULL) {
free(bb->data);
bb->data = NULL;
}
free(bb);
}
}
static struct block_buffer *read_block(ext4_fsblk_t pblk){
struct block_buffer *bb = NULL;
bb = malloc(sizeof(struct block_buffer));
if(bb == NULL){
fprintf(stderr, "Error: unable to allocate enough memory!\n");
return NULL;
}
bb->data = malloc(current_fs->blocksize);
if(bb->data == NULL){
free(bb);
fprintf(stderr, "Error: unable to allocate enough memory!\n");
return NULL;
}
if (io_channel_read_blk64(current_fs->io, pblk, 1, bb->data)) {
block_release(bb);
fprintf(stderr, "Error: unable to read block from device!\n");
return NULL;
}
return bb;
}
void print_ext2_exhdr_info(struct ext3_extent_header *header) {
printf("******* Printing extent header info ********\n");
printf("eh_magic: %04X\n", header->eh_magic);
printf("eh_entries: %d\n", header->eh_entries);
printf("eh_max: %d\n", header->eh_max);
printf("eh_depth: %d\n", header->eh_depth);
printf("eh_generation: %08X\n", (header->eh_generation));
}
void print_ext2_inode(struct ext2_inode * inode) {
printf("******* Printing inode info ********\n");
printf("mode: %d\n", inode->i_mode);
printf("uid: %d\n", inode->i_uid);
printf("size: %d\n", inode->i_size);
printf("atime: %d\n", inode->i_atime);
printf("ctime: %d\n", inode->i_ctime);
printf("mtime: %d\n", inode->i_mtime);
printf("dtime: %d\n", inode->i_dtime);
printf("gid: %d\n", inode->i_gid);
printf("links count: %d\n", inode->i_links_count);
printf("blocks: %d\n", inode->i_blocks);
printf("flags: %d\n", inode->i_flags);
printf("size_high: %d\n", inode->i_size_high);
}
static int append_undeleted_data(int out_fd, ext4_lblk_t ex_ee_block,
ext4_fsblk_t pblk, unsigned short ex_ee_len){
void * buffer;
int bytes_read = 0, bytes_written = 0;
off_t write_off = 0;
off_t out_offset = (off_t)ex_ee_block * current_fs->blocksize;
off_t bytes_to_transfer = ex_ee_len * current_fs->blocksize;
D(printf("Entering append_undeleted_data function \n"));
if(lseek(out_fd, out_offset, SEEK_SET) < 0){
fprintf(stderr, "Error: unable to seek to position: %ld\n", out_offset);
return -1;
}
buffer = (void *)malloc(current_fs->blocksize);
if(buffer == NULL){
fprintf(stderr, "Error: unable to allocate memory for buffer!\n");
return -1;
}
while(bytes_to_transfer){
if (io_channel_read_blk64(current_fs->io, pblk, 1, buffer)) {
fprintf(stderr, "Error: unable to read block from device!\n");
free(buffer);
return -1;
}
bytes_read = current_fs->blocksize;
while(bytes_read){
if((bytes_written = write(out_fd, ((char *)buffer)+write_off, bytes_read-write_off)) < 0){
fprintf(stderr, "Error: unable to write data!\n");
free(buffer);
return -1;
}
bytes_read -= bytes_written;
write_off += bytes_written;
}
bytes_to_transfer -= write_off;
bytes_read = bytes_written = 0;
write_off = 0;
pblk++;
}
free(buffer);
return 0;
}
static int ext4_undelete_leaf(struct ext_path *path, int out_fd) {
struct ext3_extent *ex;
ext4_lblk_t ex_ee_block;
ext4_fsblk_t pblk;
unsigned short ex_ee_len;
struct ext3_extent_header *eh;
if (!path->p_hdr){
path->p_hdr = ext_block_hdr(path->p_bb);
if (check_extent_hdr(path->p_hdr)){
return -1;
}
D(print_ext2_exhdr_info(path->p_hdr));
/* if there is value in eh_entries, try to use it! */
if(!path->p_hdr->eh_entries){
D(printf("Restoring eh_entries from eh_generation: %d\n", path->p_hdr->eh_entries));
path->p_hdr->eh_entries = ext4_ext_get_entries(path->p_hdr);
}
}
eh = path->p_hdr;
ex = EXT_FIRST_EXTENT(eh);
ex_ee_block = ex->ee_block;
ex_ee_len = ext4_ext_get_actual_len(ex);
pblk = ext4_ext_pblock(ex);
/* process all leafs */
while (ex <= EXT_LAST_EXTENT(eh)) {
D(printf("DATA: Extent starts at block: %lld (len: %d, first block's number: %d). Processing leaf node..\n", pblk, ex_ee_len, ex_ee_block));
append_undeleted_data(out_fd, ex_ee_block, pblk, ex_ee_len);
ex++;
ex_ee_block = ex->ee_block;
ex_ee_len = ext4_ext_get_actual_len(ex);
pblk = ext4_ext_pblock(ex);
}
return 0;
}
static int open_out_file(char *output_name){
int fd = open(output_name, O_CREAT | O_RDWR | O_TRUNC, 0600);
return fd;
}
static void close_out_file(int out_fd){
if(out_fd > 0)
close(out_fd);
}
int ext4_undelete_file(struct ext2_inode * inode_buf, char *output_name) {
struct ext_path *path = NULL;
int depth = 0;
int out_fd, err, i = 0;
D(print_ext2_inode(inode_buf));
D(print_ext2_exhdr_info(ext_inode_hdr(inode_buf)));
if (check_extent_hdr(ext_inode_hdr(inode_buf))){
return -1;
}
/* get depth from eh_generation and check it !!! */
D(printf("Restoring eh_depth from eh_generation: %d\n", depth));
depth = ext4_ext_get_depth(ext_inode_hdr(inode_buf));
path = calloc(depth + 1, sizeof (struct ext_path));
if (path == NULL) {
fprintf(stderr, "Error: can't allocate enough memory!\n");
return -1;
}
path[0].p_depth = depth;
path[0].p_hdr = ext_inode_hdr(inode_buf);
path[0].p_hdr->eh_entries = ext4_ext_get_entries(path[0].p_hdr);
D(printf("Restoring eh_entries from eh_generation: %d\n", path[0].p_hdr->eh_entries));
out_fd = open_out_file(output_name);
if (out_fd < 0){
fprintf(stderr, "Error: can't write to output file: %s!\n", output_name);
if (path != NULL)
free(path);
return -1;
}
i = 0;
while (i >= 0) {
/* leaf block */
if (i == depth) {
D(printf("Processing leaf node..\n"));
err = ext4_undelete_leaf(path + i, out_fd);
if (err < 0){
return -1;
}
block_release(path[i].p_bb);
path[i].p_bb = NULL;
i--;
continue;
}
/* index block */
if (!path[i].p_hdr) {
path[i].p_hdr = ext_block_hdr(path[i].p_bb);
if (check_extent_hdr(ext_inode_hdr(inode_buf))){
return -1;
}
}
if (!path[i].p_idx) {
/* this level hasn't been touched yet */
path[i].p_idx = EXT_FIRST_INDEX(path[i].p_hdr);
/* if eh_entries is 0, get eh_entries form eh_generation */
if (!path[i].p_hdr->eh_entries){
path[i].p_hdr->eh_entries = ext4_ext_get_entries(path[i].p_hdr);
}
} else {
/* we were already here, see at next index */
path[i].p_idx++;
}
if (ext4_ext_more_to_iterate(path + i)) {
struct block_buffer *bb = NULL;
D(printf("Reading block: %lld, index: %d\n", ext_idx_pblock(path[i].p_idx), i));
/* go to the next level */
memset(path + i + 1, 0, sizeof (*path));
bb = read_block(ext_idx_pblock(path[i].p_idx));
if(bb == NULL){
fprintf(stderr, "ERROR\n");
return -1;
}
if (i + 1 > depth) {
fprintf(stderr, "ERROR\n");
return -1;
}
path[i + 1].p_bb = bb;
i++;
} else {
/* root level has p_bb == NULL, block_release() eats this */
block_release(path[i].p_bb);
path[i].p_bb = NULL;
i--;
}
}
if (path != NULL)
free(path);
close_out_file(out_fd);
return 0;
}
int undelete_file(char *device, char *original_name, ext2_ino_t ino, char *output_name, bool strip) {
int retval;
struct ext2_inode * inode_buf;
int open_flags = EXT2_FLAG_SOFTSUPP_FEATURES;
/* open filesystem first */
D(printf("Opening filesystem...\n"));
retval = open_filesystem(device, open_flags, 0, 0);
if (retval) {
return -1;
}
// if original filename is specified
if (original_name != NULL){
ino = get_ino_deleted_file(original_name);
if (ino == 0){
fprintf(stderr, "Error: can't find inode number of specified "
"original filename: \"%s\"\n", original_name);
return -1;
}
}
inode_buf = (struct ext2_inode *)
malloc(EXT2_INODE_SIZE(current_fs->super));
if (!inode_buf) {
fprintf(stderr, "Error: can't allocate buffer for ext2_inode\n");
close_filesystem();
return -1;
}
/* try to read inode data */
D(printf("Trying to read inode: %d...\n", ino));
if (read_inode_full(ino, inode_buf,
EXT2_INODE_SIZE(current_fs->super))) {
free(inode_buf);
close_filesystem();
return -1;
}
/* check, if the file still exists */
if (inode_buf->i_links_count){
fprintf(stderr, "Error: Can't undelete file (file exists)!\n");
free(inode_buf);
close_filesystem();
return -1;
}
/* check, if the file is regular file */
if (!LINUX_S_ISREG(inode_buf->i_mode)){
fprintf(stderr, "Error: Can't undelete file (deleted file is "
"not regular file)!\n");
free(inode_buf);
close_filesystem();
return -1;
}
/* undelete file */
if (ext4_undelete_file(inode_buf, output_name) != 0) {
fprintf(stderr, "Error: unable to undelete file!\n");
free(inode_buf);
close_filesystem();
return -1;
}
/* free buffer */
free(inode_buf);
if(strip)
strip_trailing_zeros(output_name);
/* close filesystem */
D(printf("Closing filesystem...\n"));
if(close_filesystem() < 0){
return -1;
}
return 0;
}