Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Shell escape commands and sudo commands fix #257

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions lshell/checkconfig.py
Original file line number Diff line number Diff line change
Expand Up @@ -702,6 +702,9 @@ def set_noexec(self):
# if sudo_noexec.so file is not found, write error in log file,
# but don't exit tp prevent strict dependency on sudo noexec lib
self.log.error("Error: noexec library not found")
else:
# setting the global level LD_PRELOAD env variable
os.environ["LD_PRELOAD"] = self.conf.get("path_noexec")

self.conf["allowed"] += self.conf["allowed_shell_escape"]

Expand Down
21 changes: 15 additions & 6 deletions lshell/utils.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
""" Utils for lshell """

import copy
import re
import subprocess
import os
Expand Down Expand Up @@ -174,15 +175,23 @@ def cmd_parse_execute(command_line, shell_context=None):
else:
retcode = getattr(builtincmd, executable)(shell_context.conf)
else:
if "path_noexec" in shell_context.conf:
os.environ["LD_PRELOAD"] = shell_context.conf["path_noexec"]
command = replace_exit_code(command, retcode)
retcode = exec_cmd(command)
# Create a set of allowed shell escape commands by removing builtins from the allowed list
shell_excape_commands = set(shell_context.conf["allowed_shell_escape"]) - \
set(variables.builtins_list)

# If the command is in the allowed shell escape list, modify the environment and execute it
if command.split()[0] in shell_excape_commands:
env = copy.deepcopy(os.environ)
env["LD_PRELOAD"] = ""
retcode = exec_cmd(command, env=env)
else:
retcode = exec_cmd(command)

return retcode


def exec_cmd(cmd):
def exec_cmd(cmd, env=None):
"""Execute a command exactly as entered, with support for backgrounding via Ctrl+Z."""

class CtrlZException(Exception):
Expand Down Expand Up @@ -223,15 +232,15 @@ def handle_sigcont(signum, frame):
stdin=devnull_in, # Redirect input to /dev/null
stdout=sys.stdout,
stderr=sys.stderr,
preexec_fn=os.setsid,
env=env
)
# add to background jobs and return
builtincmd.BACKGROUND_JOBS.append(proc)
job_id = len(builtincmd.BACKGROUND_JOBS)
print(f"[{job_id}] {cmd} (pid: {proc.pid})")
retcode = 0
else:
proc = subprocess.Popen(cmd_args, preexec_fn=os.setsid)
proc = subprocess.Popen(cmd_args, env=env)
proc.communicate()
retcode = proc.returncode if proc.returncode is not None else 0

Expand Down