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

Python: Add shutil module sinks for path injection query #7455

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

@haby0
Copy link
Contributor

@haby0 haby0 commented Dec 20, 2021

If the path is controlled by some function calls of the shutil module, it may cause malicious attacks. For example, the rmtree function will delete the specified directory. The move function will move the specified file or move the specified file to the specified location, causing the file content to leak.

@haby0 haby0 requested a review from as a code owner Dec 20, 2021
Copy link
Member

@RasmusWL RasmusWL left a comment

Thanks 💪

Can you add the tests to the bottom of https://github.com/github/codeql/blob/cbd7434a7e5d48a16f0ae5840711ef9cabae3988/python/ql/test/library-tests/frameworks/stdlib/FileSystemAccess.py instead? Would be great to see tests using both positional and keyword arguments, like the rest of the tests 😊

Besides that, just a minor thing about one of the functions you modeled 😊

/**
* A call to the `shutil.copyfileobj` function.
*
* See https://docs.python.org/3/library/shutil.html#shutil.copyfileobj
*/
private class ShutilCopyfileobjCall extends FileSystemAccess::Range, DataFlow::CallCfgNode {
ShutilCopyfileobjCall() { this = shutil().getMember("copyfileobj").getACall() }

override DataFlow::Node getAPathArgument() {
result in [
this.getArg(0), this.getArgByName("fsrc"), this.getArg(1), this.getArgByName("fdst")
]
}
}

Copy link
Member

@RasmusWL RasmusWL Jan 5, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These arguments are not paths, but file-likes (so already opened files).

I think there is good value in keeping a note around, so we can easily remember that there is potential in modeling this function 😊

Suggested change
/**
* A call to the `shutil.copyfileobj` function.
*
* See https://docs.python.org/3/library/shutil.html#shutil.copyfileobj
*/
private class ShutilCopyfileobjCall extends FileSystemAccess::Range, DataFlow::CallCfgNode {
ShutilCopyfileobjCall() { this = shutil().getMember("copyfileobj").getACall() }
override DataFlow::Node getAPathArgument() {
result in [
this.getArg(0), this.getArgByName("fsrc"), this.getArg(1), this.getArgByName("fdst")
]
}
}
// TODO: once we have flow summaries, model `shutil.copyfileobj` which copies the content between its' file-like arguments.
// See https://docs.python.org/3/library/shutil.html#shutil.copyfileobj

Copy link
Contributor Author

@haby0 haby0 Jan 5, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does ShutilCopyfileobjCall need to be deleted?

Copy link
Member

@RasmusWL RasmusWL Jan 5, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Either that (as in my suggestion, which you can commit directly by accepting the suggestion), or by making the body of getAPathArgument() to be none()

Copy link
Contributor Author

@haby0 haby0 Jan 6, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated.

Copy link
Contributor Author

@haby0 haby0 Jan 6, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you think I can apply for a bounty next?

@haby0
Copy link
Contributor Author

@haby0 haby0 commented Jan 5, 2022

Besides that, just a minor thing about one of the functions you modeled 😊

I don't quite understand the meaning of this sentence.

@RasmusWL
Copy link
Member

@RasmusWL RasmusWL commented Jan 5, 2022

Besides that, just a minor thing about one of the functions you modeled blush

I don't quite understand the meaning of this sentence.

Oh, that was just referring to the inline comment I made.

Copy link
Member

@RasmusWL RasmusWL left a comment

Can you remove the entire file python/ql/test/query-tests/Security/CWE-022-PathInjection/shutil_path_injection.py? Since we now highlight that we model these in python/ql/test/library-tests/frameworks/stdlib/FileSystemAccess.py, that is good enough 👍

@haby0
Copy link
Contributor Author

@haby0 haby0 commented Jan 6, 2022

Can you remove the entire file python/ql/test/query-tests/Security/CWE-022-PathInjection/shutil_path_injection.py? Since we now highlight that we model these in python/ql/test/library-tests/frameworks/stdlib/FileSystemAccess.py, that is good enough 👍

No problem!

Copy link
Member

@RasmusWL RasmusWL left a comment

LGTM 👍

Will need to run tests before merging though

@haby0
Copy link
Contributor Author

@haby0 haby0 commented Jan 7, 2022

LGTM 👍

Will need to run tests before merging though

Ok. I will try to apply for the bounty next. Do you think it's okay?

@RasmusWL
Copy link
Member

@RasmusWL RasmusWL commented Jan 7, 2022

Ok. I will try to apply for the bounty next. Do you think it's okay?

I'm not officially approving/rejecting bounties. If this additional modeling finds real vulnerabilities (that you can show), I don't see why you shouldn't be able to apply for a bounty.

@haby0
Copy link
Contributor Author

@haby0 haby0 commented Jan 7, 2022

Ok. I will try to apply for the bounty next. Do you think it's okay?

I'm not officially approving/rejecting bounties. If this additional modeling finds real vulnerabilities (that you can show), I don't see why you shouldn't be able to apply for a bounty.

The application has been submitted.

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

Successfully merging this pull request may close these issues.

None yet

2 participants