我对文件漏洞在 Linux 上的工作方式有点困惑:
int fd = open("/tmp/file1", O_RDWR | O_TRUNC);
write(fd, "bbbb", 4);
lseek(fd, SEEK_SET, 10000);
write(fd, "aaaa", 4);
lseek(fd, SEEK_SET, 50);
write(fd, "cccc", 4);
close(fd);
为什么 cat/tmp/file1 产生
bbbbaaaacccc
?不应该是bbbcccaaa吗?因为 aaaa 是在偏移量 10000 处写入的?
更新:lseek 使用 EINVAL 返回 -1。
最佳答案
因为“你确定 lseek 在所有调用中都成功了吗?你没有检查它的结果代码。”有助于确定我建议在您的文件系统调用之后添加的问题:
int res = lseek(fd, 10000, SEEK_SET);
if (res == -1) {
perror("lseek has failed");
return 1;
}
你的问题是你以错误的顺序使用了参数:
lseek(fd, SEEK_SET, 10000); /* WRONG order for second and third parametes ! */
正确顺序:
lseek(fd, 10000, SEEK_SET);
这是一个人 lseek:
off_t lseek(int fd, off_t offset, int whence);
The lseek() function repositions the file offset of the open file
description associated with the file descriptor fd to the argument
offset according to the directive whence as follows:
SEEK_SET
The file offset is set to offset bytes.
SEEK_CUR
The file offset is set to its current location plus offset bytes.
SEEK_END
The file offset is set to the size of the file plus offset
bytes.
关于linux - Linux 中的文件漏洞是如何工作的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37119123/