Computing desk | ||
---|---|---|
< May 2 | << Apr | May | Jun >> | May 4 > |
Welcome to the Wikipedia Computing Reference Desk Archives |
---|
The page you are currently viewing is an archive page. While you can leave answers for any questions shown below, please ask new questions on one of the current reference desk pages. |
Is there a way to set up a directory in Linux in such a way that a file can be created in the directory (and be written to) only if there's no file with the same name in the directory? After the newly created file is closed, it should not be possible to rewrite or delete it, except by a privileged user. --134.242.92.97 (talk) 16:22, 3 May 2017 (UTC)
open()
. If open()
succeeds, it gives the process a file descriptor, and as long as the FD remains open, the process can then (assuming the file was opened R/W) read/write to/from the file as much as it wants regardless of later changes to the file's permissions or the process's user/group. This facilitates the very common practice of a process starting as root, opening files/sockets/etc. that only root has access to, and then changing its own user/group to a normal user, for increased security. Lots of daemons and system utilities (e.g., ping(1)
) do this. --47.138.161.183 (talk) 20:07, 3 May 2017 (UTC)open()
and creat()
; if the underlying backing filesytem is root-only. But I do appreciate that "oh, just write a filesystem" is not a trivial suggestion. -- Finlay McWalter··–·Talk 19:09, 3 May 2017 (UTC)
Another approach is this. Set permissions on the directory so that only the owner can write on it. Create a new user ID and make it the owner of the directory. To write files, use a program which is owned by that user ID and is setuid. The rules about creating a new file every time and never writing to an existing file are embedded in this program (and as soon as it opens a file it removes write permission). Race conditions from two copies of the program running at once could be a problem, but not if they are forced to be unique. This could be forced by making every filename include both a timestamp and the process ID of the program creating it (and the hostname where it is running, if multiple hosts could write to the same directory). --76.71.6.254 (talk) 21:35, 3 May 2017 (UTC)