加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

open()不设置O_CLOEXEC标志

发布时间:2020-12-16 05:47:28 所属栏目:百科 来源:网络整理
导读:我尝试使用open()设置O_CLOEXEC标志,没有成功. 考虑以下微测试: #include stdio.h#include fcntl.hint main() { int fd = open("test.c",O_RDONLY | O_CLOEXEC); int ret = fcntl(fd,F_GETFL); if(ret O_CLOEXEC) { printf("OK!n"); } else { printf("FAIL
我尝试使用open()设置O_CLOEXEC标志,没有成功.

考虑以下微测试:

#include <stdio.h>
#include <fcntl.h>

int main() {
  int fd = open("test.c",O_RDONLY | O_CLOEXEC);
  int ret = fcntl(fd,F_GETFL);
  if(ret & O_CLOEXEC) {
    printf("OK!n");
  } else {
    printf("FAIL!n");
  }
  printf("fd = %dn",fd);
  printf("ret = %x,O_CLOEXEC = %xn",ret,O_CLOEXEC);
  return 0;
}

当使用内核版本2.6的linux上运行时,测试成功并打印“OK!”,但是失败了3.8或3.9内核.

怎么了?
谢谢!

解决方法

决定将O_CLOEXEC标志暴露给fcntl(fd,F_GETFL)是安全漏洞.内核3.6-rc7中的 this commit进行了更改:
commit c6f3d81115989e274c42a852222b80d2e14ced6f
Author: Al Viro <viro@zeniv.linux.org.uk>
Date:   Sun Aug 26 11:01:04 2012 -0400

don't leak O_CLOEXEC into ->f_flags

Signed-off-by: Al Viro <viro@zeniv.linux.org.uk>

换句话说,你不应该依赖O_CLOEXEC在第一位显示.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读