-
Notifications
You must be signed in to change notification settings - Fork 13
Restricted shell
π§ UNDER CONSTRUCTION π§
- Related issue: #31
- The Restricted Shell
- https://en.wikipedia.org/wiki/Restricted_shell#Weaknesses_of_a_restricted_shell: "The restricted shell is not secure."
- Bash Startup Files
- Re: Restricted Bash - Not so restrictive (in 4.2 as well)
- π Classic Shell Scripting 15.2. Restricted Shell
- "To keep the user from overwriting
~/.profile, it is not enough to make the file read-only by the user. Either the home directory should not be writable by the user, or the commands in~/.profileshouldcdto a different directory."
- "To keep the user from overwriting
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.
/* * 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); }
-
Some Environment Variables are Dangerous:
IFS- AIX IFS Vulnerability
-
Restricted Accounts And Vi(m) Tricks in Linux And Unix also has examples of
IFS
-
rbashhandlesSHELL,PATH,HISTFILE,ENVandBASH_ENVβ οΈ Although it does not seem apply for startup files!
-
VISUAL&PAGER- π Sudo Mastery, 2nd Edition Chapter 8: Managing Pagers
- Bash Variables
- Having
PermitUserEnvironment noin yoursshd_configis probably a good idea - CVE-2023-4911: Looney Tunables β Local Privilege Escalation in the glibcβs ld.so
- Toward safer GNU C Library tunable handling
- See "secure-execution mode" in
LD.SO(8)
- Running
rbash -c /bin/bashnaturally doesn't work, butrbash -c bashwill work whenbashis inPATH. -
pam_envetc. needs to dictate thePATHso that the user never has "regular"PATHe.g. from Bash itself-
sshd_configneeds to haveUsePAM yes - iοΈ Setting
PATHto/usr/local/rbinseems to break Ansible
-
Bash's builtin PATH:
- https://github.com/bminor/bash/blob/master/variables.c#L552
- https://github.com/bminor/bash/blob/6794b5478f660256a1023712b5fc169196ed0a22/config-top.h#L63-L67
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
-
Escaping Restricted Linux Shells
- "Some restricted shells will start by running some files in an unrestricted mode before the restricted shell is applied. If your
.bash_profileis executed in an unrestricted mode and it's editable, you'll be able to execute code and commands as an unrestricted user."
- "Some restricted shells will start by running some files in an unrestricted mode before the restricted shell is applied. If your
env-
find(-exec) - Learning the vi Editor: Shell escape
- If we do not
typeset -r PATHin/etc/profilein Debian, it is possible to modify it e.g. from$HOME/.profile - CVE-2016-9401
- Restricted Accounts And Vi(m) Tricks in Linux And Unix
- GTFOBins
- Restricted Shell (rbash, rzsh) Bypass
- Linux Restricted Shell Bypass
- patch 9.0.2070: [security] disallow setting env in restricted mode
- < 9.2.0141 :perldo/:perl
- < 9.2.0156 perleval() and rubyeval()
- < 9.2.0176 External diff is allowed in restricted mode (#19696)
rvim -c 'set shell=/bin/bash' -c 'set diffopt-=internal' -c 'set shellredir=;/usr/bin/id>/dev/shm/proof.txt\ #>%s' -c 'e /etc/issue' -c 'diffsplit /etc/issue.net' -c 'qa!'
- < 9.2.0177 Vim9: Can set environment variables in restricted mode (#19705)
- < 9.2.0188 Can set environment variables in restricted mode (#19704)
-
Command injection via newline in glob() affects Vim < 9.2.0202 (#19746)
- CVE-2026-33412
- Neovim PR
- < 9.2.0198 cscope: can escape from restricted mode
- Setting certain variables can help escape
rvim. See Should P_SECURE be set to options in restricted mode? #19698.
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)
- Use
rviminstead ofvim- And
rviewinstead ofview
- And
LESSSECURE
-
pam_envenforcesPATHwhen the user's shell is/bin/rbashwith the help ofpam_succeed_if- It needs to come after the "regular" calls to
pam_envin the stack so to overwritePATHif it's being set earlier to something else (e.g. a "normal"PATH)
- It needs to come after the "regular" calls to
- 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 thatrbashitself is not in the restrictedPATH. - Making known dangerous variables read-only as early as possible
- Enforcing pager into
lesswithLESSSECUREenabled andVISUALintorvim - The directory is changed to
/var/emptyas instructed in various documentations ("changing the current directory to a non-writable directory other than$HOMEafter login")- π‘ I really don't understand the point, as the user is able to create/edit files in other directories with
rvimanyway
- π‘ I really don't understand the point, as the user is able to create/edit files in other directories with
- User's own startup files (e.g.
~/.profile) are still modifiable and they are executed during login, but the restrictions from/etc/profileshould be effective at that point already
~/.procmailrc.
No guarantees. Test it, hack it and make sure it's as safe as you might assume from the description above.