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 7596307
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T21:53:17+00:00 2026-05-30T21:53:17+00:00

How safe are command lists in shell (or bash) from race conditions? if [

  • 0

How safe are command lists in shell (or bash) from race conditions?

if [ -h "$dir" ]; then
  echo 'Directory exists and is a symlink'
  exit 1
fi
cd "$dir"

The above code is obviously prone to race conditions: An attacker can create a symlink after the check, but still before changing the directory into it.

Does the same apply to || command lists? In other words: is the below command immune to race conditions, or do the same rules as above still apply?

[ -h "$dir" ] || cd "$dir"

With error message:

[ -h "$dir" ]
  && { echo 'Directory exists and is a symlink'; exit 1; }
  || cd "$dir"
  • 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-30T21:53:18+00:00Added an answer on May 30, 2026 at 9:53 pm

    There are only two secure and relatively portable ways to change directory without following a symbolic link. Neither is easily possible in shell scripts.

    Assume for the sake of discussion that we’re trying to safely chdir into “foo”. The first way is to save the current directory in an open file descriptor with open(".", O_RDONLY), lstat() the “foo” directory, record the st_dev and st_ino values that result, call chdir(“foo”) and then stat() “.”. Compare the resulting the st_dev and st_ino values. If they are the same, you won the race. If not, issue an error message, fchdir() back using your saved fd, and then either abort or try again.

    The second, less portable way, is to use fd = open("foo", O_RDONLY|O_NOFOLLOW) and then fchdir(fd). You can also use openat instead of open here. The portability problem is that not all systems have O_NOFOLLOW and some older kernels won’t correctly interpret that flag (instead they ignore it, which is a security issue).

    For more information take a look at the source code of GNU find, in which I go to quite some trouble to avoid this kind of problem, using a method very similar to the one described above.

    As for solving this problem in a shell script:

    If your system has a stat(1) command or something like Perl, you can use those to perform the stat operations; you can record the result in a shell variable. This means you can more or less implement the first method in a shell script, except for the need to use fchdir to recover. If it is OK to simply abort immediately when your shell script loses the race, you can certainly adapt the first method for use in the shell. But in the end writing secure code in shell is very, very difficult.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is safe make a webform, where the user can enter sed command, like: s/\(.*\)
Is it possible to tell php to run a script from the command line
Is stopping a thread by acting the break command at some point a safe
My command is echo root_password | sudo -u root -S executable_full_path arguments The error
Is this code safe from SQL injections? Why? public void AddPlayer(string username) { var
Configure Command is Go to Glib Directory -> run this command ./configure --prefix=Installation directory
I have a problem while executing a bash script from C++ using the system
How safe is it to use Silverlight in production for a graphic form? Is
If I safe an Array and reload it, is there a possibility to get
In Visual Source Safe 6.0, you could reset a working folder by setting it

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.