/*
* Demonstrate file creation bug on NFS v4 and linux kernel 4.4+
*
* mktemp() is used on purpose.
*/
int
main(int argc, char *argv[])
{
const char *name = argv[1];
char tmp[] = "./tmpXXXXXXXXXX";
struct stat buf;
mode_t expected;
int fd, i, n = 40;
umask(S_IWGRP | S_IWOTH);
expected = 0666 & ~(S_IWGRP | S_IWOTH);
if (argv[1] == NULL)
name = mktemp(tmp);
for (i = 0; i < n; i++) {
fd = open(name, O_RDWR|O_CREAT|O_EXCL, 0666);
if (fd < 0)
err(1, "open %s", name);
memset(&buf, 0, sizeof(buf));
if (stat(name, &buf) < 0)
err(1, "stat %s", name);
if ((buf.st_mode & (S_IRWXU|S_IRWXG|S_IRWXO)) != expected)
printf("%s: %o\n", name, (int)buf.st_mode & (S_IRWXU|S_IRWXG|S_IRWXO));
else
printf("%s: ok\n", name);
unlink(name);
}
exit(0);
}
Appended is a small program to reproduce the issue:
#include <sys/types.h>
#include <sys/stat.h>
#include <err.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
/*
* Demonstrate file creation bug on NFS v4 and linux kernel 4.4+
*
* mktemp() is used on purpose.
*/
int
main(int argc, char *argv[])
{
const char *name = argv[1];
char tmp[] = "./tmpXXXXXXXXXX";
struct stat buf;
mode_t expected;
int fd, i, n = 40;
umask(S_IWGRP | S_IWOTH); O_CREAT| O_EXCL, 0666); S_IRWXG| S_IRWXO) ) != expected)
(int)buf. st_mode & (S_IRWXU| S_IRWXG| S_IRWXO) );
expected = 0666 & ~(S_IWGRP | S_IWOTH);
if (argv[1] == NULL)
name = mktemp(tmp);
for (i = 0; i < n; i++) {
fd = open(name, O_RDWR|
if (fd < 0)
err(1, "open %s", name);
memset(&buf, 0, sizeof(buf));
if (stat(name, &buf) < 0)
err(1, "stat %s", name);
if ((buf.st_mode & (S_IRWXU|
printf("%s: %o\n", name,
else
printf("%s: ok\n", name);
unlink(name);
}
exit(0);
}