Skip to content

Navigation Menu

Sign in
Sign up
pyllyukko edited this page Mar 28, 2026 · 26 revisions

🚧 UNDER CONSTRUCTION 🚧

chsh(1) β€” Linux manual page:

An account with a restricted login shell may not change her login shell. For this reason, placing /bin/rsh in /etc/shells is discouraged since accidentally changing to a restricted shell would prevent the user from ever changing her login shell back to its original value.

https://github.com/shadow-maint/shadow/blob/77eb67d4a83600febf326b4551949ce5e71e5a34/src/chsh.c#L124-L136:

/*
 * is_restricted_shell - return true if the shell is restricted
 *
 */
static bool is_restricted_shell (const char *sh)
{
 /*
 * Shells not listed in /etc/shells are considered to be restricted.
 * Changed this to avoid confusion with "rc" (the plan9 shell - not
 * restricted despite the name starting with 'r'). --marekm
 */
 return !shell_is_listed (sh);
}

Environment variables

PATH

  • Running rbash -c /bin/bash naturally doesn't work, but rbash -c bash will work when bash is in PATH.
  • pam_env etc. needs to dictate the PATH so that the user never has "regular" PATH e.g. from Bash itself
    • sshd_config needs to have UsePAM yes
    • i️ Setting PATH to /usr/local/rbin seems to break Ansible

Bash's builtin PATH:

Debian has /etc/skel/.profile, which modifies PATH as such:

# set PATH so it includes user's private bin if it exists
if [ -d "$HOME/bin" ] ; then
 PATH="$HOME/bin:$PATH"
fi
# set PATH so it includes user's private bin if it exists
if [ -d "$HOME/.local/bin" ] ; then
 PATH="$HOME/.local/bin:$PATH"
fi

Shell escapes

Known

Restricted Vim

https://github.com/vim/vim/commit/3849992b16011e36a5cb5be4b127f843389b96fd:

commit 3849992b16011e36a5cb5be4b127f843389b96fd (tag: v7.4.1777)
Author: Bram Moolenaar <Bram@vim.org>
Date: Fri Apr 22 20:46:52 2016 +0200
 patch 7.4.1777
 Problem: Newly added features can escape the sandbox.
 Solution: Add checks for restricted and secure. (Yasuhiro Matsumoto)

Protections

  • Use rvim instead of vim
    • And rview instead of view
  • LESSSECURE

Implementation in harden.yml

  • pam_env enforces PATH when the user's shell is /bin/rbash with the help of pam_succeed_if
    • It needs to come after the "regular" calls to pam_env in the stack so to overwrite PATH if it's being set earlier to something else (e.g. a "normal" PATH)
  • A limited amount of (hopefully safe) tools is copied into /usr/local/rbin/

Various safeguards and enforcements are implemented into /etc/profile:

  • This takes the assumption that the user is not able to initiate the shell with --noprofile. This also requires that rbash itself is not in the restricted PATH.
  • Making known dangerous variables read-only as early as possible
  • Enforcing pager into less with LESSSECURE enabled and VISUAL into rvim
  • The directory is changed to /var/empty as instructed in various documentations ("changing the current directory to a non-writable directory other than $HOME after login")
    • πŸ’‘ I really don't understand the point, as the user is able to create/edit files in other directories with rvim anyway
  • User's own startup files (e.g. ~/.profile) are still modifiable and they are executed during login, but the restrictions from /etc/profile should be effective at that point already

⚠️ Do note, that this doesn't affect stuff like procmail, so the user is still able to execute arbitrary stuff from ~/.procmailrc.

Conclusion

No guarantees. Test it, hack it and make sure it's as safe as you might assume from the description above.

Clone this wiki locally

AltStyle γ«γ‚ˆγ£γ¦ε€‰ζ›γ•γ‚ŒγŸγƒšγƒΌγ‚Έ (->γ‚ͺγƒͺγ‚ΈγƒŠγƒ«) /