Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4012960
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T09:18:43+00:00 2026-05-20T09:18:43+00:00

From 19 Deadly Sins of Software Security ; The following code is the poster

  • 0

From 19 Deadly Sins of Software Security;


     The following code is the poster child for the file-access race condition defect. In between the call to access(2) and open(2), the operating system could switch away from your running process and give another process a time slice. In the interveneing time, the file /tmp/splat could be deleted, and then the application crashes.

    …
    const char *filename="/tmp/splat";
    if (access(filename, R_OK)==0) {
      int fd=open(filename, O_RDONLY);
      handle_file_contents(fd);
      close(fd);
    }

and


     Again, this code is accessing the file using a filename. The code determines if the file is readable by the effective user of the Perl script and if it is, reads it. This sinful code is similar to the C/C++ code: between the file check and the read, the file may have disappeared.

    #!/user/bin/perl
    my $file="$ENV{HOME}/.config";
    read_config($file) if -r $file;

and finally,


     Use a file handle, not the filename, to verify the file exists and then open it.

    $!/ur/bin/perl
    my $file="$ENV{HOME}/.config";
    if (open(FILE, "< $file")) {
      read_config(*FILE) if is_accessible(*FILE);
    }

The point is that if you use a filename for each call to a file-related function, the file could be changed, deleted, etc. between calls, particularly on a remote server. It’s better to use a file handle or file descriptor. Unfortunately, the PHP manual seems to indicate that most file functions only work on a string representing the filename and don’t have overloads that can take a handle instead, filesize in particular:

  $fn = "somefile.txt"
  $fh = fopen($fn);
  if ($fh !== FALSE) {
    $data = fread($fh, filesize($fn));
  }

That’s not good; between the call to fopen and filesize, the file could have been altered. Worse, the file could have been altered between the call to filesize and the meat of fread!

Does anyone know of a way to use PHP file functions, especially filesize with handles instead of filenames?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T09:18:44+00:00Added an answer on May 20, 2026 at 9:18 am

    A lot of the data you’re looking for can be accessed with fstat().

    The following can be derived from an fstat() call. See stat() for more about the information fstat() returns. From the PHP manual:

    Array
    (
        [dev] => 771
        [ino] => 488704
        [mode] => 33188
        [nlink] => 1
        [uid] => 0
        [gid] => 0
        [rdev] => 0
        [size] => 1114
        [atime] => 1061067181
        [mtime] => 1056136526
        [ctime] => 1056136526
        [blksize] => 4096
        [blocks] => 8
    )
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

From the Scala API , I got the following example, which does not compile;
From your experience, are there any security measures that one should undertake on a
From GeoDjango Point Field, I get the following points: object1.point = POINT(-113.4741271000000040 53.4235217000000020) object2.point
From viewing the source code that this code makes it looks like itemValue generate
From a desktop application developer point of view, is there any difference between developing
From time to time I see an enum like the following: [Flags] public enum
From my xhtml file: <h:inputTextarea value=#{newPostForm.post.body}> <f:converter converterId=NL2BRConverter /> </h:inputTextarea> From my java file:
From my website, I have a button which calls a method and then generates
From this context: import itertools lines = itertools.cycle(open('filename')) I'm wondering how I can implement
from Apple's Documentation : @property(readonly, nonatomic) CMMagneticField magneticField Returns the magnetic field measured by

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.