Add an fcntl() to check file creation
Subsystems (3)
Christian Brauner
maintainercover Add an fcntl() to check file creation
Systemd has a helper called openat_report_new() that returns whether a file was created anew or it already existed before for cases where O_CREAT has to be used without O_EXCL (cf. [1]). That apparently isn't something that's specific to systemd but it's where I noticed it.
The current logic is that it first attempts to open the file without O_CREAT | O_EXCL and if it gets ENOENT the helper tries again with both flags. If that succeeds all is well. If it now reports EEXIST it retries.
That works fairly well but some corner cases make this more involved. If this operates on a dangling symlink the first openat() without O_CREAT | O_EXCL will return ENOENT but the second openat() with O_CREAT | O_EXCL will fail with EEXIST. The reason is that openat() without O_CREAT | O_EXCL follows the symlink while O_CREAT | O_EXCL doesn't for security reasons. So it's not something we can really change unless we add an explicit opt-in via O_FOLLOW which seems really ugly.
The caller could try and use fanotify() to register to listen for creation events in the directory before calling openat(). The caller could then compare the returned tid to its own tid to ensure that even in threaded environments it actually created the file. That might work but is a lot of work for something that should be fairly simple and I'm uncertain about it's reliability.
The caller could use a bpf lsm hook to hook into security_file_open() to figure out whether they created the file. That also seems a bit wild.
So let's add F_CREATED which allows the caller to check whether they actually did create the file. That has caveats of course but I don't think they are problematic:
* In multi-threaded environments a thread can only be sure that it did create the file if it calls openat() with O_CREAT. In other words, it's obviously not enough to just go through it's fdtable and check these fds because another thread could've created the file.
* If there's any codepaths where an openat() with O_CREAT would yield the same struct file as that of another thread it would obviously cause wrong results. I'm not aware of any such codepaths from openat() itself. Imho, that would be a bug.
* Related to the previous point, calling the new fcntl() on files created and opened via special-purpose system calls or ioctl()s would cause wrong results only if the affected subsystem a) raises FMODE_CREATED and b) may return the same struct file for two different calls. I'm not seeing anything outside of regular VFS code that raises FMODE_CREATED.
There is code for b) in e.g., the drm layer where the same struct file is resurfaced but again FMODE_CREATED isn't used and it would be very misleading if it did.
Thanks!
Christian
---
---
base-commit: 933069701c1b507825b514317d4edd5d3fd9d417
change-id: 20240724-work-fcntl-9d2aa275affd
Christian Brauner
maintainerRFC PATCH 1/2 fcntl: add F_CREATED
Systemd has a helper called openat_report_new() that returns whether a file was created anew or it already existed before for cases where O_CREAT has to be used without O_EXCL (cf. [1]). That apparently isn't something that's specific to systemd but it's where I noticed it.
The current logic is that it first attempts to open the file without O_CREAT | O_EXCL and if it gets ENOENT the helper tries again with both flags. If that succeeds all is well. If it now reports EEXIST it retries.
That works fairly well but some corner cases make this more involved. If this operates on a dangling symlink the first openat() without O_CREAT | O_EXCL will return ENOENT but the second openat() with O_CREAT | O_EXCL will fail with EEXIST. The reason is that openat() without O_CREAT | O_EXCL follows the symlink while O_CREAT | O_EXCL doesn't for security reasons. So it's not something we can really change unless we add an explicit opt-in via O_FOLLOW which seems really ugly.
The caller could try and use fanotify() to register to listen for creation events in the directory before calling openat(). The caller could then compare the returned tid to its own tid to ensure that even in threaded environments it actually created the file. That might work but is a lot of work for something that should be fairly simple and I'm uncertain about it's reliability.
The caller could use a bpf lsm hook to hook into security_file_open() to figure out whether they created the file. That also seems a bit wild.
So let's add F_CREATED which allows the caller to check whether they actually did create the file. That has caveats of course but I don't think they are problematic:
* In multi-threaded environments a thread can only be sure that it did create the file if it calls openat() with O_CREAT. In other words, it's obviously not enough to just go through it's fdtable and check these fds because another thread could've created the file.
* If there's any codepaths where an openat() with O_CREAT would yield the same struct file as that of another thread it would obviously cause wrong results. I'm not aware of any such codepaths from openat() itself. Imho, that would be a bug.
* Related to the previous point, calling the new fcntl() on files created and opened via special-purpose system calls or ioctl()s would cause wrong results only if the affected subsystem a) raises FMODE_CREATED and b) may return the same struct file for two different calls. I'm not seeing anything outside of regular VFS code that raises FMODE_CREATED.
There is code for b) in e.g., the drm layer where the same struct file is resurfaced but again FMODE_CREATED isn't used and it would be very misleading if it did.
--- fs/fcntl.c | 10 ++++++++++ include/uapi/linux/fcntl.h | 3 +++ 2 files changed, 13 insertions(+)
fs/fcntl.c +10 -0
@@ -343,6 +343,12 @@ static long f_dupfd_query(int fd, struct file *filp)return f.file == filp; } +/* Let the caller figure out whether a given file was just created. */ +static long f_created (const struct file *filp) +{ +return !!(filp->f_mode &FMODE_CREATED ); +} +static long do_fcntl (int fd,unsigned int cmd,unsigned long arg,struct file *filp) {
@@ -352,6 +358,9 @@ static long do_fcntl(int fd, unsigned int cmd, unsigned long arg,long err = -EINVAL ;switch (cmd) { +case F_CREATED : + err =f_created (filp); +break ;case F_DUPFD : err =f_dupfd (argi, filp,0 );break ;
@@ -463,6 +472,7 @@ static long do_fcntl(int fd, unsigned int cmd, unsigned long arg,static int check_fcntl_cmd (unsigned cmd) {switch (cmd) { +case F_CREATED :case F_DUPFD :case F_DUPFD_CLOEXEC :case F_DUPFD_QUERY :
include/uapi/linux/fcntl.h +3 -0
@@ -16,6 +16,9 @@#define F_DUPFD_QUERY (F_LINUX_SPECIFIC_BASE + 3) +/* Was the file just created? */ +#define F_CREATED (F_LINUX_SPECIFIC_BASE + 4) +/* * Cancel a blocking posix lock; internal use only until we expose an * asynchronous lock api to userspace:
Signature · 1 line
2.43.0
Re: [PATCH RFC 1/2] fcntl: add F_CREATED
Quoted 28 lines · Christian Brauner
Systemd has a helper called
openat_report_new()that returns whether a file was created anew or it already existed before for cases whereO_CREAThas to be used withoutO_EXCL(cf. [1]). That apparently isn't something that's specific to systemd but it's where I noticed it.The current logic is that it first attempts to open the file without
O_CREAT|O_EXCLand if it gets ENOENT the helper tries again with both flags. If that succeeds all is well. If it now reports EEXIST it retries.That works fairly well but some corner cases make this more involved. If this operates on a dangling symlink the first
openat()withoutO_CREAT|O_EXCLwill return ENOENT but the secondopenat()withO_CREAT|O_EXCLwill fail with EEXIST. The reason is thatopenat()withoutO_CREAT|O_EXCLfollows the symlink whileO_CREAT|O_EXCLdoesn't for security reasons. So it's not something we can really change unless we add an explicit opt-in viaO_FOLLOWwhich seems really ugly.The caller could try and use
fanotify()to register to listen for creation events in the directory before callingopenat(). The caller could then compare the returned tid to its own tid to ensure that even in threaded environments it actually created the file. That might work but is a lot of work for something that should be fairly simple and I'm uncertain about it's reliability.The caller could use a bpf lsm hook to hook into
security_file_open()to figure out whether they created the file. That also seems a bit wild.
Quoted 9 lines
So let's add
F_CREATEDwhich allows the caller to check whether they actually did create the file. That has caveats of course but I don't think they are problematic:* In multi-threaded environments a thread can only be sure that it did create the file if it calls
openat()withO_CREAT. In other words, it's obviously not enough to just go through it's fdtable and check these fds because another thread could've created the file.
Not exactly. FMODE_CREATED is set in the file description. In principle a userland program should know which thread actually did the the open() that results in each fd. This new interface tells us which fd's open actually resulted in the file being created (which is good).
In any case, I don't see this as a problem. The interface does what it says on the tin.
Quoted 5 lines
* If there's any codepaths where an
openat()withO_CREATwould yield the same struct file as that of another thread it would obviously cause wrong results. I'm not aware of any such codepaths fromopenat()itself. Imho, that would be a bug.
Definitely a bug. That said, this will have interesting interactions with dup that may need to be documented. IOW, if you dup a file with FMODE_CREATED, then the new fd will also report that F_CREATED is true.
Quoted 7 lines
* Related to the previous point, calling the new
fcntl()on files created and opened via special-purpose system calls or ioctl()s would cause wrong results only if the affected subsystem a) raisesFMODE_CREATEDand b) may return the same struct file for two different calls. I'm not seeing anything outside of regular VFS code that raisesFMODE_CREATED.
Me neither. This interface is really about "traditional" filesystems. If you're dealing with some pseudo-fs (proc, sys, debugfs, etc.), I don't think you can expect to get sane results from this.
Quoted 57 lines
There is code for b) in e.g., the drm layer where the same struct file is resurfaced but again
FMODE_CREATEDisn't used and it would be very misleading if it did.Signed-off-by Christian Brauner <[email protected]>---
fs/fcntl.c | 10 ++++++++++
include/uapi/linux/fcntl.h | 3 +++2 files changed, 13 insertions(+)fs/fcntl.c +0
-0@@ -343,6 +343,12 @@ static long f_dupfd_query(int fd, struct file *filp) return f.file == filp; }fs/fcntl.c +6
-0+/* Let the caller figure out whether a given file was just created. */ +static long f_created (const struct file *filp) +{ +return !!(filp->f_mode &FMODE_CREATED ); +} +static long
do_fcntl(int fd, unsigned int cmd, unsigned long arg, struct file *filp)
{fs/fcntl.c +0
-0@@ -352,6 +358,9 @@ static long do_fcntl(int fd, unsigned int cmd, unsigned long arg, long err = -EINVAL;
switch (cmd) {fs/fcntl.c +3
-0+case F_CREATED : + err =f_created (filp); +break ;case
F_DUPFD:
err =f_dupfd(argi, filp, 0);
break;fs/fcntl.c +1
-0@@ -463,6 +472,7 @@ static long do_fcntl(int fd, unsigned int cmd, unsigned long arg, static int check_fcntl_cmd(unsigned cmd) { switch (cmd) { +case F_CREATED :case
F_DUPFD:
caseF_DUPFD_CLOEXEC:
caseF_DUPFD_QUERY:include/uapi/linux/fcntl.h +0
-0@@ -16,6 +16,9 @@
#defineF_DUPFD_QUERY(F_LINUX_SPECIFIC_BASE+ 3)include/uapi/linux/fcntl.h +3
-0+/* Was the file just created? */ +#define F_CREATED (F_LINUX_SPECIFIC_BASE + 4) +
Quoted 4 lines
/*
* Cancel a blocking posix lock; internal use only until we expose an
* asynchronous lock api to userspace:
This looks sane to me.
Christian Brauner
maintainerRe: [PATCH RFC 1/2] fcntl: add F_CREATED
Quoted 53 lines · Jeff Layton
Quoted 28 lines · Christian Brauner
Systemd has a helper called
openat_report_new()that returns whether a file was created anew or it already existed before for cases whereO_CREAThas to be used withoutO_EXCL(cf. [1]). That apparently isn't something that's specific to systemd but it's where I noticed it.The current logic is that it first attempts to open the file without
O_CREAT|O_EXCLand if it gets ENOENT the helper tries again with both flags. If that succeeds all is well. If it now reports EEXIST it retries.That works fairly well but some corner cases make this more involved. If this operates on a dangling symlink the first
openat()withoutO_CREAT|O_EXCLwill return ENOENT but the secondopenat()withO_CREAT|O_EXCLwill fail with EEXIST. The reason is thatopenat()withoutO_CREAT|O_EXCLfollows the symlink whileO_CREAT|O_EXCLdoesn't for security reasons. So it's not something we can really change unless we add an explicit opt-in viaO_FOLLOWwhich seems really ugly.The caller could try and use
fanotify()to register to listen for creation events in the directory before callingopenat(). The caller could then compare the returned tid to its own tid to ensure that even in threaded environments it actually created the file. That might work but is a lot of work for something that should be fairly simple and I'm uncertain about it's reliability.The caller could use a bpf lsm hook to hook into
security_file_open()to figure out whether they created the file. That also seems a bit wild.Quoted 9 lines
So let's add
F_CREATEDwhich allows the caller to check whether they actually did create the file. That has caveats of course but I don't think they are problematic:* In multi-threaded environments a thread can only be sure that it did create the file if it calls
openat()withO_CREAT. In other words, it's obviously not enough to just go through it's fdtable and check these fds because another thread could've created the file.Not exactly.
FMODE_CREATEDis set in the file description. In principle a userland program should know which thread actually did the theopen()that results in each fd. This new interface tells us which fd's open actually resulted in the file being created (which is good).In any case, I don't see this as a problem. The interface does what it says on the tin.
Quoted 5 lines
* If there's any codepaths where an
openat()withO_CREATwould yield the same struct file as that of another thread it would obviously cause wrong results. I'm not aware of any such codepaths fromopenat()itself. Imho, that would be a bug.
Quoted 3 lines
Definitely a bug. That said, this will have interesting interactions with dup that may need to be documented. IOW, if you dup a file with
FMODE_CREATED, then the new fd will also report thatF_CREATEDis true.
Yes, but it's the behavior one expects. Dup'ing an fd implies fd1->file == fd2->file and so it's correct behavior. IOW, someone must confuse open() with dup() to be surprised by this.
Jeff Layton
maintainerRe: [PATCH RFC 1/2] fcntl: add F_CREATED
Quoted 54 lines · Christian Brauner
Quoted 53 lines · Jeff Layton
Quoted 28 lines · Christian Brauner
Systemd has a helper called
openat_report_new()that returns whether a file was created anew or it already existed before for cases whereO_CREAThas to be used withoutO_EXCL(cf. [1]). That apparently isn't something that's specific to systemd but it's where I noticed it.The current logic is that it first attempts to open the file without
O_CREAT|O_EXCLand if it gets ENOENT the helper tries again with both flags. If that succeeds all is well. If it now reports EEXIST it retries.That works fairly well but some corner cases make this more involved. If this operates on a dangling symlink the first
openat()withoutO_CREAT|O_EXCLwill return ENOENT but the secondopenat()withO_CREAT|O_EXCLwill fail with EEXIST. The reason is thatopenat()withoutO_CREAT|O_EXCLfollows the symlink whileO_CREAT|O_EXCLdoesn't for security reasons. So it's not something we can really change unless we add an explicit opt-in viaO_FOLLOWwhich seems really ugly.The caller could try and use
fanotify()to register to listen for creation events in the directory before callingopenat(). The caller could then compare the returned tid to its own tid to ensure that even in threaded environments it actually created the file. That might work but is a lot of work for something that should be fairly simple and I'm uncertain about it's reliability.The caller could use a bpf lsm hook to hook into
security_file_open()to figure out whether they created the file. That also seems a bit wild.Quoted 9 lines
So let's add
F_CREATEDwhich allows the caller to check whether they actually did create the file. That has caveats of course but I don't think they are problematic:* In multi-threaded environments a thread can only be sure that it did create the file if it calls
openat()withO_CREAT. In other words, it's obviously not enough to just go through it's fdtable and check these fds because another thread could've created the file.Not exactly.
FMODE_CREATEDis set in the file description. In principle a userland program should know which thread actually did the theopen()that results in each fd. This new interface tells us which fd's open actually resulted in the file being created (which is good).In any case, I don't see this as a problem. The interface does what it says on the tin.
Quoted 5 lines
* If there's any codepaths where an
openat()withO_CREATwould yield the same struct file as that of another thread it would obviously cause wrong results. I'm not aware of any such codepaths fromopenat()itself. Imho, that would be a bug.
Quoted 7 lines
Quoted 3 lines
Definitely a bug. That said, this will have interesting interactions with dup that may need to be documented. IOW, if you dup a file with
FMODE_CREATED, then the new fd will also report thatF_CREATEDis true.Yes, but it's the behavior one expects. Dup'ing an fd implies
fd1->file==fd2->fileand so it's correct behavior. IOW, someone must confuseopen()withdup()to be surprised by this.
It's the behavior _we_ generally expect, but a lot of people don't understand the subtleties of file descriptions vs. file descriptors. Spelling out this behavior explicitly would probably prevent a few headaches.
Signature · 1 line
Jeff Layton <[email protected]>
Re: [PATCH RFC 1/2] fcntl: add F_CREATED
In reply to Christian BraunerQuoted 49 lines · Christian Brauner
Systemd has a helper called
openat_report_new()that returns whether a file was created anew or it already existed before for cases whereO_CREAThas to be used withoutO_EXCL(cf. [1]). That apparently isn't something that's specific to systemd but it's where I noticed it.The current logic is that it first attempts to open the file without
O_CREAT|O_EXCLand if it gets ENOENT the helper tries again with both flags. If that succeeds all is well. If it now reports EEXIST it retries.That works fairly well but some corner cases make this more involved. If this operates on a dangling symlink the first
openat()withoutO_CREAT|O_EXCLwill return ENOENT but the secondopenat()withO_CREAT|O_EXCLwill fail with EEXIST. The reason is thatopenat()withoutO_CREAT|O_EXCLfollows the symlink whileO_CREAT|O_EXCLdoesn't for security reasons. So it's not something we can really change unless we add an explicit opt-in viaO_FOLLOWwhich seems really ugly.The caller could try and use
fanotify()to register to listen for creation events in the directory before callingopenat(). The caller could then compare the returned tid to its own tid to ensure that even in threaded environments it actually created the file. That might work but is a lot of work for something that should be fairly simple and I'm uncertain about it's reliability.The caller could use a bpf lsm hook to hook into
security_file_open()to figure out whether they created the file. That also seems a bit wild.So let's add
F_CREATEDwhich allows the caller to check whether they actually did create the file. That has caveats of course but I don't think they are problematic:* In multi-threaded environments a thread can only be sure that it did create the file if it calls
openat()withO_CREAT. In other words, it's obviously not enough to just go through it's fdtable and check these fds because another thread could've created the file.* If there's any codepaths where an
openat()withO_CREATwould yield the same struct file as that of another thread it would obviously cause wrong results. I'm not aware of any such codepaths fromopenat()itself. Imho, that would be a bug.* Related to the previous point, calling the new
fcntl()on files created and opened via special-purpose system calls or ioctl()s would cause wrong results only if the affected subsystem a) raisesFMODE_CREATEDand b) may return the same struct file for two different calls. I'm not seeing anything outside of regular VFS code that raisesFMODE_CREATED.
Quoted 6 lines
There is code for b) in e.g., the drm layer where the same struct file is resurfaced but again
FMODE_CREATEDisn't used and it would be very misleading if it did.
Looks like a sensible functionality. I agree with Jeff that the behavior wrt dup(2) / fork(2) needs to be clearly stated but is hardly surprising given how everything else with file descriptors and descriptions works. So feel free to add:
Honza
Quoted 58 lines
--- fs/fcntl.c | 10 ++++++++++ include/uapi/linux/fcntl.h | 3 +++ 2 files changed, 13 insertions(+)fs/fcntl.c +10
-0@@ -343,6 +343,12 @@ static long f_dupfd_query(int fd, struct file *filp)return f.file == filp; } +/* Let the caller figure out whether a given file was just created. */ +static long f_created (const struct file *filp) +{ +return !!(filp->f_mode &FMODE_CREATED ); +} +static long do_fcntl (int fd,unsigned int cmd,unsigned long arg,struct file *filp) {@@ -352,6 +358,9 @@ static long do_fcntl(int fd, unsigned int cmd, unsigned long arg,long err = -EINVAL ;switch (cmd) { +case F_CREATED : + err =f_created (filp); +break ;case F_DUPFD : err =f_dupfd (argi, filp,0 );break ;@@ -463,6 +472,7 @@ static long do_fcntl(int fd, unsigned int cmd, unsigned long arg,static int check_fcntl_cmd (unsigned cmd) {switch (cmd) { +case F_CREATED :case F_DUPFD :case F_DUPFD_CLOEXEC :case F_DUPFD_QUERY :include/uapi/linux/fcntl.h +3
-0@@ -16,6 +16,9 @@#define F_DUPFD_QUERY (F_LINUX_SPECIFIC_BASE + 3) +/* Was the file just created? */ +#define F_CREATED (F_LINUX_SPECIFIC_BASE + 4) +/* * Cancel a blocking posix lock; internal use only until we expose an * asynchronous lock api to userspace: Signature · 2 lines
-- 2.43.0
Signature · 2 lines
Jan Kara <[email protected]> SUSE Labs, CR
Christian Brauner
maintainerRFC PATCH 2/2 selftests: add F_CREATED tests
In reply to Christian BraunerAdd simple selftests for fcntl(fd, F_CREATED, 0).
--- tools/testing/selftests/core/close_range_test.c | 39 +++++++++++++++++++++++++ 1 file changed, 39 insertions(+)
tools/testing/selftests/core/close_range_test.c +39 -0
@@ -26,6 +26,10 @@#define F_DUPFD_QUERY (F_LINUX_SPECIFIC_BASE + 3)#endif +#ifndef F_CREATED +#define F_CREATED (F_LINUX_SPECIFIC_BASE + 4) +#endif +static inline int sys_close_range (unsigned int fd,unsigned int max_fd,unsigned int flags) {
@@ -589,4 +593,39 @@ TEST(close_range_cloexec_unshare_syzbot)EXPECT_EQ (close (fd3),0 ); } +TEST (fcntl_created) +{ +for (int i =0 ; i <101 ; i++) { +int fd; +char path[PATH_MAX ]; + + fd =open ("/dev/null" ,O_RDONLY |O_CLOEXEC ); + ASSERT_GE(fd, 0) { +if (errno ==ENOENT ) +SKIP (return, +"Skipping test since /dev/null does not exist" ); + } + +/* We didn't create "/dev/null". */ +EXPECT_EQ (fcntl (fd,F_CREATED ,0 ),0 ); +close (fd); + +sprintf (path,"aaaa_%d" , i); + fd =open (path,O_CREAT |O_RDONLY |O_CLOEXEC ,0600 ); +ASSERT_GE (fd,0 ); + +/* We created "aaaa_%d". */ +EXPECT_EQ (fcntl (fd,F_CREATED ,0 ),1 ); +close (fd); + + fd =open (path,O_RDONLY |O_CLOEXEC ); +ASSERT_GE (fd,0 ); + +/* We're opening it again, so no positive creation check. */ +EXPECT_EQ (fcntl (fd,F_CREATED ,0 ),0 ); +close (fd); +unlink (path); + } +} +TEST_HARNESS_MAIN
Signature · 1 line
2.43.0