安全に標準コマンドを置き換える

「cdの引数を絶対パスにしてコマンドヒストリに残す」のように標準コマンドを置き換える上で考えるべき、bashの仕組みについてのメモ。

環境

$ uname -a
Linux vm-ubuntu64 3.13.0-32-generic #57-Ubuntu SMP Tue Jul 15 03:51:08 UTC 2014 x86_64 x86_64 x86_64 GNU/Linux

$ lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description:    Ubuntu 14.04.1 LTS
Release:        14.04
Codename:       trusty

$ bash --version
GNU bash, version 4.3.11(1)-release (x86_64-pc-linux-gnu)

インタラクティブシェルで起動する条件と判定方法

端末からの入力を逐次的に受け付け、対話的にコマンドを実行するように起動されたシェルはインタラクティブシェルと呼ばれる。 man bashによると、その起動条件は以下の通り。

INVOCATION
       (snip)

       An interactive shell is one started without non-option arguments and without the -c option whose standard input and error are both connected to terminals (as determined by isatty(3)), or one started with the -i option.  PS1 is set and $- includes i if bash is interactive, allowing a shell script or a startup file to test this state.

すなわち、

  • ファイルなどの非オプション引数を指定しない
  • コマンドラインからの入力オプション-cを指定しない
  • 標準入力と標準エラー出力の両方が端末になっている

または

  • 明示的に-iオプションを指定

することでインタラクティブシェルとなる。

インタラクティブシェルで起動した場合$PS1にプロンプト文字列がセットされるため、次のように記述することでインタラクティブシェルの場合のみ処理を行うことができる。

if [[ -n "$PS1" ]]; then
   echo "interactive shell"
fi
$ bash test.sh

$ bash -i test.sh
interactive shell

.bashrcなどが読み込まれる条件

bashの場合、起動時にどのファイルが読み込まれるかはいくらか複雑である。 同じくman bashによると、以下の通り。

INVOCATION
       A login shell is one whose first character of argument zero is a -, or one started with the --login option.

       (snip)

       The following paragraphs describe how bash executes its startup files.  If any of the files exist but cannot be read, bash reports an error.  Tildes are expanded in filenames as described below under Tilde Expansion in the EXPANSION section.

       When bash is invoked as an interactive login shell, or as a non-interactive shell with the --login option, it first reads and executes commands from the file /etc/profile, if that file exists.  After reading that file, it looks for ~/.bash_profile, ~/.bash_login, and ~/.profile, in that order, and reads and executes commands from the first one that exists and is readable.  The --noprofile option may be used when the shell is started to inhibit this behavior.

       When a login shell exits, bash reads and executes commands from the file ~/.bash_logout, if it exists.

       When an interactive shell that is not a login shell is started, bash reads and executes commands from /etc/bash.bashrc and ~/.bashrc, if these files exist.  This may be inhibited by using the --norc option.  The --rcfile file option will force bash to read and execute commands from file instead of /etc/bash.bashrc and ~/.bashrc.

       When bash is started non-interactively, to run a shell script, for example, it looks for the variable BASH_ENV in the environment, expands its value if it appears there, and uses the expanded value as the name of a file to read and execute.  Bash behaves as if the following command were executed:
              if [ -n "$BASH_ENV" ]; then . "$BASH_ENV"; fi
       but the value of the PATH variable is not used to search for the filename.

       If bash is invoked with the name sh, it tries to mimic the startup behavior of historical versions of sh as closely as possible, while conforming to the POSIX standard as well.  When invoked as an interactive login shell, or a non-interactive shell with the --login option, it first attempts to read and execute commands from /etc/profile and ~/.profile, in that order.  The --noprofile option may be used to inhibit this behavior.  When invoked as an interactive shell with the name sh, bash looks for the variable ENV, expands its value if it is defined, and uses the expanded value as the name of a file to read and execute.  Since a shell invoked as sh does not attempt to read and execute commands from any other startup files, the --rcfile option has no effect.  A non-interactive shell invoked with the name sh does not attempt to read any other startup files.  When invoked as sh, bash enters posix mode after the startup files are read.

       When bash is started in posix mode, as with the --posix command line option, it follows the POSIX standard for startup files.  In this mode, interactive shells expand the ENV variable and commands are read and executed from the file whose name is the expanded value.  No other startup files are read.

       Bash attempts to determine when it is being run with its standard input connected to a network connection, as when executed by the remote shell daemon, usually rshd, or the secure shell daemon sshd.  If bash determines it is being run in this fashion, it reads and executes commands from ~/.bashrc and ~/.bashrc, if these files exist and are readable.  It will not do this if invoked as sh.  The --norc option may be used to inhibit this behavior, and the --rcfile option may be used to force another file to be read, but neither rshd nor sshd generally invoke the shell with those options or allow them to be specified.

       If the shell is started with the effective user (group) id not equal to the real user (group) id, and the -p option is not supplied, no startup files are read, shell functions are not inherited from the environment, the SHELLOPTS, BASHOPTS, CDPATH, and GLOBIGNORE variables, if they appear in the environment, are ignored, and the effective user id is set to the real user id.  If the -p option is supplied at invocation, the startup behavior is the same, but the effective user id is not reset.

すなわち、bashには複数の起動モードがあり、それぞれにおいてログインシェル、インタラクティブシェルかどうかで読み込まれるファイルが異なる。

コードとしては、shell.cのrun_startup_files()が対応する。

ログインシェルかつインタラクティブシェルであれば、それぞれについてファイルが読み込まれる。 ネットワークモードはssh user@hostname "ls"などとした場合に該当し、単にssh user@hostnameとした場合は通常モードあるいはshモードのシェルが起動する。 また、uid/euid、gid/egid不一致モードについては、セキュリティの面から例外的に通常の処理を行わないモードであるといえる。

大半の状況において、ログインシェルとして起動されるのはログイン時のみであり、環境変数BASH_ENVENVに値はセットされていない。 したがって、インタラクティブシェルであれば~/.bashrcが読み込まれ、それ以外は何も読み込まれないものと考えてほぼ問題はない。

シェルスクリプトが現在のシェルプロセスで処理される条件

次の場合、スクリプトは新たに起動したシェルプロセスのもとで実行される。

  • sh test.shのように明示的にシェルを起動する
  • 実行権限を付与し./test.shのように実行することで、shebang経由でシェルを起動する

また、exec組み込みコマンドを使うと、現在のプロセスを置き換える(forkしない)形で新たなシェルプロセスが起動する。

一方、次の場合は現在のシェルプロセスのもとでスクリプトが実行される。

.bashrcで標準コマンドを置き換えたとき、他のシェルスクリプトに影響を及ぼす条件

以上より、.bashrcで置き換えた標準コマンドが影響を及ぼす起動条件は次のようになる。

  1. ~/.bash_profileなどにsource ~/.bashrcが書かれている状況で、bash --login test.sh
  2. ssh user@hostname "source test.sh"
  3. bash -i test.sh
  4. インタラクティブシェルにてsource test.sh

これらのうち1、2については$PS1がセットされないので、最初に示した判定によって回避可能である。 3、4についてはインタラクティブシェルそのものであるので回避できないが、そもそもインタラクティブシェルで一般的なシェルスクリプトを実行することに問題がある。

結論

$PS1の有無によって条件分岐し、インタラクティブシェルでのみ標準コマンドを置き換えるようにすればよい。 そもそも、明示的に指定しない限り非インタラクティブシェルとして起動したbashは~/.bashrcを読み込まない。