If you want atomic open-and-read-the-whole-file, you can reach for something like std::fs::read_to_string (https://doc.rust-lang.org/stable/std/fs/fn.read_to_string.ht...).
Nothing. In all major operating systems I'm aware of, opening a file creates a handle to the file contents. This handle is distinct from the handle implied by the existence of the path in the filesystem. Some filesystems allow you to create another handle to the same file by a different name (called a hardlink). Removing a file only deletes the handle implied by the path; if another handle to it exists (via a hardlink or the file currently being open in a process) the data itself is unaltered.
In general, TOCTTOU attacks are caused by introspection being done on paths instead of file handles. Once you have handles, and you are querying exclusively using handles, your TOCTTOU issues largely go away. There is still a related issue that filesystem atomicity guarantees (or rather, a general lack thereof) is still a major headache, but it's not the same issue as TOCTTOU attacks.
The point is, if I go by names, there is no guarantee of identity. File-descrpitors ensure that.
Same could be done with c++, but the libraries are not designed that way.
Still, I don't find it terribly convincing.
More broadly, this is kind of a bad example. Handling files is surprisingly difficult and that's really where the dragons lie here. IME the dragons exist for both Rust and C++, because they're platform problems rather than stdlib/language problems.
NB: the correct thing to do here for POSIX platforms is to call fstat, which is true regardless of systems language.
I absolutely understand some of the niceness about Rust preventing programmers from shooting themselves in the foot(it is still possible nonetheless), but sometimes it feels people just want to complain about C++ for the sake of complaining.
All the common FS (in linux .. ext* xfs) do not have that these.
As you intuited, a problem that arises in environments that lack transactions/locks/etc
You know that files are nameless on Unix, right?
I was referring to the transaction semantics
> /what if the file is deleted here?/
you don't need transaction semantics here.
On Unix file is basically inode with counter. If it doesn't have record (aka name) in directory but non-zero counter, it would exist.
So on open(2) counter is bumped up and is equal to 2 (or higher if there are hardlinks), one for the directory record and one for program running it.
If you remove record from directory (aka `rm filename`), counter would decrease by 1, but it is still non-zero, program still has it open, could read its content, etc. Only after close(2) counter would decrease to 0 and file would be gone.
Just tested it on Ubuntu 20.4 with simple C program
[0] https://github.com/rust-lang/rust/blob/master/library/std/sr...
[1] https://doc.rust-lang.org/std/os/windows/fs/trait.OpenOption...
You know that files are nameless on Unix, right?
I think there is no language level feature in Rust to prevent TTCTTOU. TTCTTOU can happen in both c++ and rust.
for example:
I mean even if you write it like: let f = File::open("username.txt")?;it looks atomic, it is not.
the c++ example, on the other hand, can do a filtering too, using:
http://www.cplusplus.com/reference/fstream/ifstream/is_open/
I mean Rust forces you to do error handling, which is nice. But that's not enough for preventing TTCTTOU. I don't think the first example is a strong argument.