I'm actually fixing driver under Linux. Klokwork said that code like:

file = fopen(fileName, "w+"); // w+,r,a and any mix of those is used here
if (file != NULL) { /* do things*/ }
else { /* throw error */ }

can end as Time-of-check to time-of-use vulnerability.

However as far as I read it need do something like if(access()) before fopen to make it happen.

And there is question: Does that code example I show have any weak point? or I don't need to care?


A call to fopen is not in itself a TOCTOU vulnerability. By definition, TOCTOU involves two operations: a “check” and a “use”.

A common example of TOCTOU vulnerability is checking access permissions with access before opening a file. It's a bug (race condition) because the permissions might change between checking and opening, and it's usually a vulnerability because file permissions are important for security.

The access system call is highly suspicious because there aren't many ways to use it that don't introduce a TOCTOU. The fopen call in itself is not particularly suspicious because there are many ways to use it that are safe. However this doesn't mean that access is the only way to make a TOCTOU with fopen.

An example of race condition that can occur when opening files is if you got the file name from some external source, and you make assumptions about that name. For example, if you build the argument to fopen by concatenating a directory name with a file name, and you've made some checks on the directory, that's a TOCTOU vulnerability as well. The checks on the directory may no longer be valid by the time you use the directory to open a file. That's why Linux has the openat system call: so you can call opendir on a directory, perform checks on the directory, then call openat to open a file inside that directory (whereas open with concatenated names would open a file in a directory which now has this name, but may not be the one you checked).

So yes, you do need to care. Your code may or may not have a vulnerability. In my very limited experience, Klocwork does have a lot of false positives, but not everything is a false positive. Start by reading the complete message carefully. Review your code carefully, with written-down security objectives, and track how these security objectives are met (or not). There's no miracle: writing correct and secure code is harder than just applying a checklist.

  • Then to sum up that. 1. if i successful open file I'm safe and there is not that much to do for attacker. 2. but i need make sure user open right file – user209896 Jun 10 at 10:41
  • 6
    @user209896 Right. If fopen succeeds and you know that you opened the file you meant to open, you're safe. – Gilles Jun 10 at 10:45
  • 1
    Not all uses of access(2) lead to TOCTOU vulnerabilities. Also see AOSP Issue 36941868, sdcard.c, do_node_get_path: possible TOCTOU due to use of access(2) – jww Jun 11 at 17:16
  • In the general case, it is extremely difficult to avoid TOCTOU issues. For example, if you check whether a file exists, by any means whatsoever, then your thread could get preempted and another thread could create or delete the file right after the check returns. But unless an attacker can leverage that bug into a security vulnerability, it's (mostly) uninteresting. Not all TOCTOU's are security vulnerabilities. – Kevin Jun 11 at 18:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.