Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a script that needs to run as root. It also has a configuration file which is read using source:

source conf.sh

I would like to check that normal user can't edit conf.sh and therefore get root access.

I can check who's the file owner and what permissions group and others have:

if [[ "$(stat -c "%a" conf.sh | egrep ".2.|.3.|.6.|.7.|..2|..3|..6|..7")" != "" ]] || [[ "$(stat -c "%u" conf.sh)" != "0" ]]; then
    #don't execute the file"

Am I missing anything here? Are there any best practices?

share|improve this question
You also need to check all the path components for write access only to root. – Stéphane Chazelas Apr 23 '14 at 13:43
@StephaneChazelas That's no enough. This approach is fundamentally flawed. There's a non-negligible chance that someone could find another file on the system that belongs to root (like anything in /usr/bin or /usr/sbin) and causes all kinds of havoc when invoked in the wrong context. – Gilles Apr 23 '14 at 21:31
up vote 2 down vote accepted

I'd do it with arithmetic evaluation:

if [[ $(stat -c '%u' conf.sh) -ne 0 -o \
      $(( $(stat -c '%a' conf.sh) & 0044)) -ne 0 ]]; then
    # file's owner is not root or file is writable by group or world
share|improve this answer
There's also filesystem ACLs to consider. – Patrick Apr 24 '14 at 1:41

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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