Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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"
fi

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 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 at 21:31

1 Answer 1

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
fi
share|improve this answer
    
There's also filesystem ACLs to consider. –  Patrick Apr 24 at 1:41

Your Answer

 
discard

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.