linux中的文件漏洞实际上是如何工作的

linux中的文件漏洞实际上是如何工作的,linux,io,operating-system,filesystems,Linux,Io,Operating System,Filesystems,我对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 ??

我对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
??不应该是BBBCCCAA吗?因为aaaa写在10000的偏移量

更新:lseek返回-1和EINVAL。

因为“您确定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);
下面是一位男士,他说:

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.
因为“您确定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);
下面是一位男士,他说:

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.

您确定
lseek
在所有调用中都成功吗?您没有检查其结果代码。它返回EINVAL,但原因是什么?基于手册页中给出的原因。您的LSEEK没有执行任何操作,因为文件中没有要查找的内容。您正在进行顺序写入。是否确定
lseek
在所有调用中都成功?您没有检查其结果代码。它返回EINVAL,但原因是什么?基于手册页中给出的原因。您的LSEEK没有执行任何操作,因为文件中没有要查找的内容。您正在进行顺序写入。感谢您的回复,因为我已经评论过它返回EINVAL:结果文件偏移量将为负,或者超出可查找设备的末端。但是我该如何创建文件孔呢?我的
lseek
手册页上写着:“EINVAL:何处无效。或者:生成的文件偏移量将为负数,或者超出可查找设备的末端。”您之所以获得EINVAL是因为以下第一个原因(其中无效),因为您的参数顺序错误,正如Sergei所说。感谢您的回复,正如我已经评论过的,它返回EINVAL:生成的文件偏移量将为负数,或者超出可查找设备的末端。但是我该如何创建文件洞呢?我的
lseek
手册页上说:“EINVAL:从哪里来是无效的。或者:生成的文件偏移量将为负数,或者超出可查找设备的末端。”正如Sergei所说,您之所以获得EINVAL是因为以下第一个原因(从哪里来是无效的),因为您的参数顺序错误。