1

Given a hostile environment such as:

for word in builtin command type unfunction declare set unset alias; do
    eval "$word(){ echo $word function; };  alias $word='echo $word alias'"
done 

Is it possible to access a builtin command? Eg, is it possible to do something like:

\command \builtin \type echo

and still see:

echo is a shell builtin

Is possible for a hostile (or locked-down) environment to be able to completely redefine things such that an underlying builtin, function or pathname could never be accessed?

  • unalias builtin; unset -f builtin? I don't think this environment is hostile enough. – Michael Homer Jan 29 at 2:54
  • @MichaelHomer I like your sense of evil, and have updated with eval. – Tom Hale Jan 29 at 3:48
3

With Bash, if you enable POSIX mode:

  1. Function names may not be the same as one of the POSIX special builtins.
  2. POSIX special builtins are found before shell functions during command lookup.

Since set and unset are special builtins, you can get access to them via the POSIX mode, and then use those to get rid of the functions, and then use \unalias to get rid of the aliases:

$ for word in builtin command type  declare set unset unalias alias; do     eval "$word(){ echo $word function; };  alias $word='echo $word alias'"; done 
alias function
$ unalias  builtin command type unfunction declare set unset alias
unalias alias builtin command type unfunction declare set unset alias
$ POSIXLY_CORRECT=1
$ \unset -f builtin command type  declare set unset unalias alias
$ \unalias  builtin command type unfunction declare set unset alias
bash: unalias: unfunction: not found
bash: unalias: alias: not found

$ type -a builtin
builtin is a shell builtin

But I can't think of a way back from, say:

enable -n set unset builtin command type  declare unalias alias typeset enable shopt

(Without trying to start a new shell, which maybe disallowed in a restricted shell.)

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.