Race-free unlinking of directory entries
Hi!
Linux kernel currently does not provide any race-free way for calling unlink() syscall on file entry which points to opened file descriptor.
On the other hand Linux kernel already provides race-free way for creating file entry by linkat() syscall with AT_EMPTY_PATH or AT_SYMLINK_FOLLOW flags. unlinkat() does not.
There was already discussion about unlink issue in bugzilla:
https://bugzilla.kernel.org/show_bug.cgi?id=93441
Because file descriptor describes inode number which can be stored in more directories as hard links, there is a proposed funlinkat() syscall with following API:
int funlinkat(int fd, int dirfd, const char *pathname, int flags);
It should atomically check if file descriptor fd and pathname (according to dirfd) are same, and if then just unlinkat(dirfd, pathname, flags). If are not same, throw error.
What userspace application basically needs:
Open file, test it stat (or probably content) and based on test result decide if file needs to be removed or not.
Or delete a file behind a file descriptor opened with O_PATH.
Both cases are currently not possible without introducing race condition between open/stat and unlink. Between those two calls, some other process can exchange files.
Signature · 2 lines
Pali Rohár [email protected]
Hi! I would like to remind this my older email about race free unlinking. Is there any plan to provide such support?
Quoted 32 lines · On Wednesday 20 December 2017 20:18:44 Pali Rohár wrote:
Hi!
Linux kernel currently does not provide any race-free way for calling
unlink()syscall on file entry which points to opened file descriptor.On the other hand Linux kernel already provides race-free way for creating file entry by
linkat()syscall withAT_EMPTY_PATHorAT_SYMLINK_FOLLOWflags.unlinkat()does not.There was already discussion about unlink issue in bugzilla:
https://bugzilla.kernel.org/show_bug.cgi?id=93441Because file descriptor describes inode number which can be stored in more directories as hard links, there is a proposed
funlinkat()syscall with following API:int
funlinkat(int fd, int dirfd, const char *pathname, int flags);It should atomically check if file descriptor fd and pathname (according to dirfd) are same, and if then just
unlinkat(dirfd, pathname, flags). If are not same, throw error.What userspace application basically needs:
Open file, test it stat (or probably content) and based on test result decide if file needs to be removed or not.
Or delete a file behind a file descriptor opened with
O_PATH.Both cases are currently not possible without introducing race condition between open/stat and unlink. Between those two calls, some other process can exchange files.
Signature · 2 lines
Pali Rohár [email protected]
Quoted 2 lines · Pali Roh�r
Hi! I would like to remind this my older email about race free unlinking. Is there any plan to provide such support?
Given that in Linux we know which directory entry a given fd refers to, we shouldn't even need a new syscall, but simply support the AT_EMPTY_PATH flag for funlinkat.
Quoted 7 lines · On Monday 09 April 2018 02:59:19 Christoph Hellwig wrote:
Quoted 2 lines · Pali Rohár
Hi! I would like to remind this my older email about race free unlinking. Is there any plan to provide such support?
Given that in Linux we know which directory entry a given fd refers to, we shouldn't even need a new syscall, but simply support the
AT_EMPTY_PATHflag for funlinkat.
I do not think it is enough, specially in cases when hard links are in use. Example:
fd = open("/a")link("/a", "/b")link("/a", "/c")unlink("/a")
And now I want to call funlink for "fd". Which file should be unlinked? "/b" or "/c" or none or both?
Another example:
fd = open("/a")link("/a", "/b")unlink("/a")
Calling funlink for fd should unlink "/b" or it should fail?
And another example:
fd = open("/a")rename("/a", "/b")
What should funlink do for fd now?
Note that there is difference which symlink is shown in /proc/<pid>/fd between last two examples -- even those two examples results in the same state (if there is no race). For last example <fd> points to "/b", for previous <fd> points to "/a (deleted)".
Signature · 2 lines
Pali Rohár [email protected]
Quoted 10 lines · Pali Roh�r
I do not think it is enough, specially in cases when hard links are in use. Example:
fd =
open("/a")link("/a", "/b")link("/a", "/c")unlink("/a")And now I want to call funlink for "fd". Which file should be unlinked? "/b" or "/c" or none or both?
'/a' as that is what fd refers to.
Quoted 8 lines
Another example:
fd =
open("/a")link("/a", "/b")unlink("/a")Calling funlink for fd should unlink "/b" or it should fail?
It should fail, as '/a' doesn't refer to name that is visible in the namespace.
Quoted 6 lines
And another example:
fd =
open("/a")rename("/a", "/b")What should funlink do for fd now?
remove the directory entry refering to '/b' as that is what fd refers to.
Quoted 4 lines
Note that there is difference which symlink is shown in /proc/<pid>/fd between last two examples -- even those two examples results in the same state (if there is no race). For last example <fd> points to "/b", for previous <fd> points to "/a (deleted)".
Yes, that is fully expected.
Quoted 14 lines · On Monday 09 April 2018 03:24:14 Christoph Hellwig wrote:
Quoted 7 lines · Pali Rohár
Another example:
fd =
open("/a")link("/a", "/b")unlink("/a")Calling funlink for fd should unlink "/b" or it should fail?
It should fail, as '/a' doesn't refer to name that is visible in the namespace.
Quoted 2 lines
And another example:
Quoted 7 lines
Quoted 4 lines
fd =
open("/a")rename("/a", "/b")What should funlink do for fd now?
remove the directory entry refering to '/b' as that is what fd refers to.
Why it should differ in these two cases? Calling /bin/ln /a /b followed by /bin/rm /a results in the same state as calling /bin/mv /a /b. This is something which works in POSIX systems.
I think it is strange that new possible funlink call would work only if external applications uses /bin/mv and would fail if /bin/ln and /bin/rm are used.
This is reason why I suggested two parameters funlink, it takes fd for unlinking and pathname which must contain same inode as fd. So when you call it with fd+"/b" it unlink "/b" without failing.
Signature · 2 lines
Pali Rohár [email protected]
Quoted 27 lines · On Monday 09 April 2018 12:35:54 Pali Rohár wrote:
Quoted 14 lines · On Monday 09 April 2018 03:24:14 Christoph Hellwig wrote:
Quoted 7 lines · Pali Rohár
Another example:
fd =
open("/a")link("/a", "/b")unlink("/a")Calling funlink for fd should unlink "/b" or it should fail?
It should fail, as '/a' doesn't refer to name that is visible in the namespace.
Quoted 2 lines
And another example:
Quoted 7 lines
Quoted 4 lines
fd =
open("/a")rename("/a", "/b")What should funlink do for fd now?
remove the directory entry refering to '/b' as that is what fd refers to.
Why it should differ in these two cases? Calling
/bin/ln/a /b followed by/bin/rm/a results in the same state as calling/bin/mv/a /b. This is something which works in POSIX systems.
Quoted 7 lines
I think it is strange that new possible funlink call would work only if external applications uses
/bin/mvand would fail if/bin/lnand/bin/rmare used.This is reason why I suggested two parameters funlink, it takes fd for unlinking and pathname which must contain same inode as fd. So when you call it with fd+"/b" it unlink "/b" without failing.
Imagine that you have opened file descriptor in your program and you want to unlink file behind that file descriptor.
Now some external process rename that file via rename() syscall and based on your argument unlinking file descriptor in your program pass.
But if that external process do link() syscall followed by unlink() syscall -- to simulate rename operation -- then based on your argument unlinking file descriptor in your program fails.
I think this is really inconsistency which is not so good.
...
Has somebody else opinion on this problem?
Because I still think that funlinkat() syscall has its usage.
Signature · 2 lines
Pali Rohár [email protected]