Skip to content

[Code scan] Quote HDFS and SGE shell command arguments #605

Description

@njzjz

Found by a Codex global repository scan of deepmodeling/dpdispatcher at commit 98a9e08.

Problem
Several HDFS operations build shell command strings from user-controlled paths and run them with shell=True. The SGE submit path also interpolates the remote directory and script filename without quoting.

Relevant code

cmd = f"hadoop fs -test -e {uri}"
try:

cmd = f"hadoop fs -rm -r {uri}"
try:
ret, out, err = run_cmd_with_all_output(cmd)

cmd = f"hadoop fs -copyFromLocal -f {local_path} {to_uri}"
try:
ret, out, err = run_cmd_with_all_output(cmd)

cmd = f"hadoop fs -copyToLocal {remote} {local_path}"
try:
ret, out, err = run_cmd_with_all_output(cmd)

cmd = f"hadoop fs -text {uri}"
try:
ret, out, err = run_cmd_with_all_output(cmd)

cmd = f"hadoop fs -mv {from_uri} {to_uri}"
try:
ret, out, err = run_cmd_with_all_output(cmd)

def run_cmd_with_all_output(cmd, shell=True):
with subprocess.Popen(
cmd, shell=shell, stdout=subprocess.PIPE, stderr=subprocess.PIPE
) as proc:
out, err = proc.communicate()
ret = proc.returncode
return (ret, out, err)

script_file_dir = self.context.remote_root
stdin, stdout, stderr = self.context.block_checkcall(
"cd {} && {} {}".format(script_file_dir, "qsub", script_file_name)

Impact
Paths with spaces fail. Paths or URIs containing shell metacharacters can change the command that is executed.

Suggested fix
Prefer argv-list subprocess calls with shell=False. Where a remote shell string is unavoidable, apply shlex.quote() to every dynamic path, URI, and script filename.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    Status
    In Progress

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions