Visitar URL original
False positive: CWE-78 OS Command Injection · Issue #12753 · github/codeql · GitHub
The Wayback Machine - https://web.archive.org/web/20230521235405/https://github.com/github/codeql/issues/12753
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

False positive: CWE-78 OS Command Injection #12753

Closed
LostBenjamin opened this issue Apr 4, 2023 · 2 comments
Closed

False positive: CWE-78 OS Command Injection #12753

LostBenjamin opened this issue Apr 4, 2023 · 2 comments

Comments

@LostBenjamin
Copy link

LostBenjamin commented Apr 4, 2023 •

This query produces different results for the two semantically equivalent programs below:

CodeQL reports this as a bug:

from flask import Flask, request
import subprocess

app = Flask(__name__)

@app.route("/ls")
def ls():
    dirname = request.args.get('dirname')
    ls_cmd = ["ls", dirname]
    ls_out = subprocess.check_output(ls_cmd, shell=True)
    return ls_out

CodeQL does not report this as a bug:

from flask import Flask, request
import subprocess

app = Flask(__name__)

@app.route("/ls")
def ls():
    dirname = request.args.get('dirname')
    ls_out = subprocess.check_output(["ls", dirname], shell=True)
    return ls_out

The first program seems a false positive. Why does this happen?

@RasmusWL
Copy link
Member

RasmusWL commented Apr 4, 2023

Hi @LostBenjamin. Note that your code does not do what you intend, since you're using BOTH shell=True and passing a list of arguments.

Quoting from the documentation:

On POSIX with shell=True, the shell defaults to /bin/sh. (...) If args is a sequence, the first item specifies the command string, and any additional items will be treated as additional arguments to the shell itself. That is to say, Popen does the equivalent of:

Popen(['/bin/sh', '-c', args[0], args[1], ...])

So you should probably rewrite this to be

subprocess.check_output(["ls", dirname])

Difference in behavior

Overall, this query tries to only flag when a user can control which executable is used, not what arguments are passed to it. So in the case of subprocess.check_output(["ls", dirname]) we recognize the list-literal, and only care if the user could control the first element ("ls") which is not the case... so no alert.

I completely agree that the first case should be handled in the same way, such that this is a false positive. The reason it's treated differently, is that we currently taint the entire list when it contains a tainted element. We've been discussing how we can drop this behavior, but it'll take a bit of work to achieve.

We've also discussed writing a new query to alert when a user can control the argument to an executable (as in your case), but don't think it has quite the same severity as being able to control the entire command.

@LostBenjamin
Copy link
Author

@RasmusWL Thank you for the detailed answer!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants