iTranslated by AI
Precautions for Restricting Commands in rbash
Overview
For a user logging in via ssh, I wanted to use rbash to allow only certain commands like cd or echo. However, if the settings are incorrect, rbash does not work as expected, and I found that restricted commands could be executed during an ssh connection as follows:
$ ssh foo@bar [command to restrict]
The reason was that I forgot to include a description to change the PATH for command restriction in ~/.bashrc.
~/.bash_profile can restrict commands after an ssh login, but it cannot restrict them for non-interactive commands during an ssh connection.
foo@bar:~$ cat ~/.bash_profile
PATH="$HOME/bin"
foo@bar:~$ cat ~/.bashrc
PATH="$HOME/bin"
bash loads different configuration files depending on how it is started.
For details, please refer to the INVOCATION section of the bash man page.
Additionally, the configuration files and the directory added to the PATH (e.g., $HOME/bin/ in the example above) must be write-protected.
Environment
I intended to set it up so that only cat, date, echo, and ls under $HOME/bin could be executed as shown below.
There are several Japanese articles about how to set up rbash available if you search for them.
- OS: Ubuntu 20.04.3 LTS
foo@bar:~$ uname -a
Linux ubuntu 5.4.0-1055-raspi #62-Ubuntu SMP PREEMPT Wed Mar 2 14:43:34 UTC 2022 aarch64 aarch64 aarch64 GNU/Linux
- Shell: rbash
# The shell after logging in is rbash
foo@bar:~$ echo $SHELL
/bin/rbash
# The shell during an SSH connection is rbash
$ ssh foo@bar 'echo $SHELL'
/bin/rbash
- rbash environment
foo@bar:~$ cat .bash_profile
PATH="$HOME/bin"
# In the output below, the owner of bin/ is foo and it is writable.
# Originally, the owner should be root and writing from foo must be prohibited.
foo@bar:~$ ls -al bin/
total 8
drwxrwxr-x 2 foo foo 4096 Mar 13 13:27 .
drwxr-xr-x 6 foo foo 4096 Mar 13 13:25 ..
lrwxrwxrwx 1 foo foo 8 Mar 13 13:17 cat -> /bin/cat
lrwxrwxrwx 1 foo foo 9 Mar 13 13:27 date -> /bin/date
lrwxrwxrwx 1 foo foo 9 Mar 13 11:53 echo -> /bin/echo
lrwxrwxrwx 1 foo foo 7 Mar 13 13:05 ls -> /bin/ls
Execution Details
After an SSH login, the touch command cannot be used.
foo@bar:~$ touch tmp.txt
-rbash: /usr/lib/command-not-found: restricted: cannot specify `/' in command names
During an SSH connection, you can place a symbolic link for the touch command in the $PATH as follows.
I have configured ln so that it cannot be used in rbash.
ssh foo@bar ln -s /bin/touch /home/foo/bin/
It is placed in the $PATH as shown below.
foo@bar:~$ ls -al bin/
total 8
drwxrwxr-x 2 foo foo 4096 Mar 13 14:04 .
drwxr-xr-x 6 foo foo 4096 Mar 13 13:25 ..
lrwxrwxrwx 1 foo foo 8 Mar 13 13:17 cat -> /bin/cat
lrwxrwxrwx 1 foo foo 9 Mar 13 13:27 date -> /bin/date
lrwxrwxrwx 1 foo foo 9 Mar 13 11:53 echo -> /bin/echo
lrwxrwxrwx 1 foo foo 7 Mar 13 13:05 ls -> /bin/ls
lrwxrwxrwx 1 foo foo 10 Mar 13 14:04 touch -> /bin/touch
After that, touch can be used without any problems.
foo@bar:~$ ls
bin
foo@bar:~$ touch tmp.txt
foo@bar:~$ ls
bin tmp.txt
Other Methods
If the command you want the logged-in user to use is only one, and only for SSH connections, you can achieve something similar using the ForceCommand setting in sshd.
This setting allows you to "ignore the command specified by the client and forcibly execute the command specified here."
Even if the ~/.bashrc settings were forgotten and ssh foo@bar [command to restrict] was used, only the ForceCommand was executed, and the restricted command could not be run.
Summary
I have described the mistakes I made when configuring rbash.
I wrote this article as a reminder for myself, thinking that others might make similar mistakes.
(Thanks to Kouichi IWAMOTO for pointing this out.)
Discussion
おそらく設定の問題かと。
同じ設定を
~/.bashrcに設定してみるとどうでしょうか?~/.bash_profileは、bashが対話的なログインシェルとして起動された時に実行されます。ssh user@host commandのようにsshでコマンドを指定した場合は対話的シェルでもログインシェルでもない為、~/.bash_profileは実行されません。その為、PATHの設定が行われずデフォルトの状態となるので、ln等のデフォルトのPATHに有るコマンドが実行出来てしまいます。~/.bashrcは対話的な非ログインシェルの時に実行されます。これだけだとsshでコマンド指定した時には実行されない事になりますが、他の条件として「標準入力がネットワークに接続されている時」というのが有り、これに当てはまるので~/.bashrcは実行されます。bashの起動時の動作は他にも色々と条件が有って複雑怪奇なので、詳細についてはbashのman pageのINVOCATION(起動)節を読んでみて下さい。
これだと
$SHELLの展開はローカル側で行われるので、リモート側で実際に実行されるのはecho /bin/bashとなります。リモートでの
$SHELLを確認したい場合は、ssh foo@bar 'echo $SHELL'のようにクオートする必要が有ります。何か違和感が有るなと思っていたのですが、そもそも
lnが使えたとしてもbinにリンクが作れるのがおかしいです。これを見ると、
binの所有者がfooで書き込み権が有ります。書き込み権が有るという事は、何らかの方法でファイルを作れれば新しいコマンドが追加出来る事になります。
使えるコマンドを制限したいのですから
chmod 555 binを実行してbinへの書き込み権を落とすchown root binを実行してbinの所有者をrootにするの二つを行うべきです。
他にも、
~/.bash_profile,~/.bashrcの所有者をrootにする~/.bash_profile,~/.bashrcの書き込み権を落とすも行う方がいいでしょう。(前二つは必須、後ろ二つはファイルが作れなくなるので可能ならば)
IWAMOTO Kouichiさん
非常に参考になるご指摘ありがとうございました。
記事も頂いた情報をもとに修正しました。