如何使文件中的漏洞在Linux中通过c擦除数据 [英] how to make holes in file to erase data by c in linux

查看:181
本文介绍了如何使文件中的漏洞在Linux中通过c擦除数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

FALLOC_FL_PUNCH_HOLE 最初不是由 3.0.0-17 支持的,我想我需要打补丁。



我知道linux有这个 hole 的功能,我可以在现有的文件中创建一个洞。
$ b

具体来说,我已经创建了一个名为 hole_test 的文件代码:

  18 #include< stdlib.h> 
19 #include< sys / types.h>
20 #include< sys / stat.h>
21 #include< fcntl.h>
22 #include< unistd.h>
23 #include< errno.h>
24
25 int main(int argc,char ** argv)
26 {
27 int fd;
28 char a [7] =开心!;
30 fd = open(hole_test,O_CREAT | O_RDWR,0666);
31 if(fd == -1)
32 printf(error,%s \ n,(char *)strerror(errno));
35 // fallocate(fd,0x02,0,0);
36 // pwrite(fd,a,sizeof(a),0);
37 // pwrite(fd,a,sizeof(b),65536);
38 close(fd);
39返回0;
40}

首先,我用户 L36 L37 来创建一个文件。 stat hole_test 显示:

 文件:`hole_test'
大小:65540块:16 IO块:4096个常规文件
设备:801h / 2049d inode:1052101链接:1
访问:(0664 / -rw-rw-r--)Uid :( 1000 / bxshi)Gid:(1000 / bxshi)
访问:2012-04-03 23:02:35.227664608 +0800
修改:2012-04-03 23:02:35.227664608 +0800
改变:2012-04-03 23:02:35.227664608 +0800

然后我用 L35 和评论 L36 L37 在我的文件中打个洞。 0x02等于 FALLOC_FL_PUNCH_HOLE ,我没有找到它的定义,所以只是使用它的值)

然后, stat hole_test Blocks 仍然是16。 p $ p> 文件:`hole_test'
大小:65540块:16 IO块:4096常规文件
设备:801h / 2049d索引节点:1052101链接:1
访问:(0664 / -rw-rw-r--)Uid :( 1000 / bxshi)Gid:(1000 / bxshi)
访问:2012-04-03 23:02:35.227664608 +0800
修改:2012-04-03 23:02:35.227664608 +0800
更改:2012-04-03 23:02:35.227664608 +0800

我想知道如果我可以在这个 hole_test 文件中删除现有数据?



我怎么能在 hole_test 在偏移0到7处,这样我认为 Blocks 可以变成8,我写的字符串就会消失。 / p>

希望大家可以知道我说过的话,并给我一些建议。

您可以使用 fallocate(fd,FALLOC_FL_PUNCH_HOLE,offset,len)。 (自Linux 2.6.38以来支持)请参阅 https://lwn.net/Articles/415889/ 幕后细节和附带补丁。

Updated: This FALLOC_FL_PUNCH_HOLE is not originally supported by 3.0.0-17, I think I need to patch it.

I know linux have this hole feature, and I am wondering if I could make a hole in a existing file.

For specific, I have created a file named hole_test by these codes:

 18 #include <stdlib.h>
 19 #include <sys/types.h>
 20 #include <sys/stat.h>
 21 #include <fcntl.h>
 22 #include <unistd.h>
 23 #include <errno.h>
 24 
 25 int main(int argc, char **argv)
 26 {
 27     int fd;
 28     char a[7]="happy!";
 30     fd = open("hole_test", O_CREAT|O_RDWR, 0666);
 31     if(fd == -1)
 32             printf("error, %s\n", (char *)strerror(errno));
 35 //  fallocate(fd, 0x02, 0,0);
 36 //  pwrite(fd, a, sizeof(a), 0);
 37 //  pwrite(fd, a, sizeof(b), 65536);
 38     close(fd);
 39     return 0;
 40 }

firstly, I user L36 L37 to create a file. the stat hole_test shows that:

  File: `hole_test'
  Size: 65540       Blocks: 16         IO Block: 4096   regular file
Device: 801h/2049d  Inode: 1052101     Links: 1
Access: (0664/-rw-rw-r--)  Uid: ( 1000/   bxshi)   Gid: ( 1000/   bxshi)
Access: 2012-04-03 23:02:35.227664608 +0800
Modify: 2012-04-03 23:02:35.227664608 +0800
Change: 2012-04-03 23:02:35.227664608 +0800

Then I use L35 and comment L36 L37 to make a hole in my file.(0x02 equals to FALLOC_FL_PUNCH_HOLE, I did not find where it is defined so just use its value)

and then, by stat hole_test, the Blocks is still 16.

  File: `hole_test'
  Size: 65540       Blocks: 16         IO Block: 4096   regular file
Device: 801h/2049d  Inode: 1052101     Links: 1
Access: (0664/-rw-rw-r--)  Uid: ( 1000/   bxshi)   Gid: ( 1000/   bxshi)
Access: 2012-04-03 23:02:35.227664608 +0800
Modify: 2012-04-03 23:02:35.227664608 +0800
Change: 2012-04-03 23:02:35.227664608 +0800

I want to know if I could make new holes in this hole_test file to erase existing data?

How could I make a hole in hole_test at offset 0 to 7, in that way I think the Blocks could become 8 and the string I wrote would disappear.

Hope you guys could know what I said and give me some advice.

解决方案

You use fallocate(fd, FALLOC_FL_PUNCH_HOLE, offset, len). (Supported since Linux 2.6.38) See https://lwn.net/Articles/415889/ for behind-the-scenes details and accompanying patches.

这篇关于如何使文件中的漏洞在Linux中通过c擦除数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆