Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

open() doesn't set O_CLOEXEC flag

I try to set O_CLOEXEC flag using open() and have no sucess.

Consider the following microtest:

#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 = %d\n", fd);
  printf("ret = %x, O_CLOEXEC = %x\n", ret, O_CLOEXEC);
  return 0;
} 

When running on linux with kernel version 2.6 the test succeeds and prints "OK!", but fails with 3.8 or 3.9 kernels.

What's wrong? Thanks!

like image 804
Ivan Efremov Avatar asked Aug 19 '13 03:08

Ivan Efremov


1 Answers

It was decided that exposing O_CLOEXEC flag to fcntl(fd, F_GETFL) is security leak. Change was made by this commit in kernel 3.6-rc7:

commit c6f3d81115989e274c42a852222b80d2e14ced6f
Author: Al Viro <[email protected]>
Date:   Sun Aug 26 11:01:04 2012 -0400

don't leak O_CLOEXEC into ->f_flags

Signed-off-by: Al Viro <[email protected]>

In other words, you should not have relied on O_CLOEXEC being visible in first place.

like image 174
mvp Avatar answered Sep 29 '22 17:09

mvp