This repository was archived by the owner on Jan 5, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 128
Golang : Add Query To Detect PAM Authorization Bugs #709
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| /** | ||
| * @name PAM Authentication Bypass | ||
| * @description Depending on | ||
This conversation was marked as resolved.
Show resolved
Hide resolved
|
||
| * @kind problem | ||
| * @problem.severity warning | ||
| * @id go/unreachable-statement | ||
| * @tags maintainability | ||
| * correctness | ||
| * external/cwe/cwe-561 | ||
| * @precision very-high | ||
| */ | ||
|
|
||
| import go | ||
|
|
||
| predicate isInTestFile(Expr r) { | ||
| r.getFile().getAbsolutePath().matches("%test%") and | ||
| not r.getFile().getAbsolutePath().matches("%/ql/test/%") | ||
| } | ||
|
|
||
| class PamAuthenticate extends Method { | ||
| PamAuthenticate() { | ||
| this.hasQualifiedName("github.com/msteinert/pam", "Transaction", "Authenticate") | ||
| } | ||
| } | ||
|
|
||
| class PamAcctMgmt extends Method { | ||
| PamAcctMgmt() { this.hasQualifiedName("github.com/msteinert/pam", "Transaction", "AcctMgmt") } | ||
| } | ||
|
|
||
| class PamTransaction extends StructType { | ||
| PamTransaction() { this.hasQualifiedName("github.com/msteinert/pam", "Transaction") } | ||
| } | ||
|
|
||
| class PamStartFunc extends Function { | ||
| PamStartFunc() { this.hasQualifiedName("github.com/msteinert/pam", ["StartFunc", "Start"]) } | ||
| } | ||
|
|
||
| class PamAuthBypassConfiguration extends TaintTracking::Configuration { | ||
| PamAuthBypassConfiguration() { this = "PAM auth bypass" } | ||
|
|
||
| override predicate isSource(DataFlow::Node source) { | ||
| exists(PamStartFunc p | p.getACall().getResult(0) = source) | ||
| } | ||
|
|
||
| override predicate isSink(DataFlow::Node sink) { | ||
| exists(PamAcctMgmt p | p.getACall().getReceiver() = sink) | ||
| } | ||
| } | ||
|
|
||
| class PamAuthBypassConfig extends TaintTracking::Configuration { | ||
| PamAuthBypassConfig() { this = "PAM auth bypass2" } | ||
|
|
||
| override predicate isSource(DataFlow::Node source) { | ||
| exists(PamStartFunc p | p.getACall().getResult(0) = source) | ||
| } | ||
|
|
||
| override predicate isSink(DataFlow::Node sink) { | ||
| exists(PamAuthenticate p | p.getACall().getReceiver() = sink) | ||
| } | ||
| } | ||
|
|
||
| from | ||
| PamAuthBypassConfiguration config, PamAuthBypassConfig config2, DataFlow::Node source, | ||
| DataFlow::Node sink | ||
| where | ||
| not isInTestFile(source.asExpr()) and | ||
| (config2.hasFlow(source, sink) and not config.hasFlow(source, _)) | ||
| select source, "This Pam transaction may not be secure." | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| /** | ||
| * @name PAM Authentication Bypass | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What is the local version for? It should be strictly a subset of the global one, right?
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The global one has a longer runtime but is more accurate. The local one has a shorter runtime but would miss some edge cases; Not that I have found any in the wild. |
||
| * @description Depending on | ||
| * @kind problem | ||
| * @problem.severity warning | ||
| * @id go/unreachable-statement | ||
| * @tags maintainability | ||
| * correctness | ||
| * external/cwe/cwe-561 | ||
| * @precision very-high | ||
| */ | ||
|
|
||
| import go | ||
|
|
||
| predicate isInTestFile(Expr r) { | ||
| r.getFile().getAbsolutePath().matches("%test%") and | ||
| not r.getFile().getAbsolutePath().matches("%/ql/test/%") | ||
| } | ||
|
|
||
| class PamAuthenticate extends Method { | ||
| PamAuthenticate() { | ||
| this.hasQualifiedName("github.com/msteinert/pam", "Transaction", "Authenticate") | ||
| } | ||
| } | ||
|
|
||
| class PamAcctMgmt extends Method { | ||
| PamAcctMgmt() { this.hasQualifiedName("github.com/msteinert/pam", "Transaction", "AcctMgmt") } | ||
| } | ||
|
|
||
| class PamTransaction extends StructType { | ||
| PamTransaction() { this.hasQualifiedName("github.com/msteinert/pam", "Transaction") } | ||
| } | ||
|
|
||
| class PamStartFunc extends Function { | ||
| PamStartFunc() { this.hasQualifiedName("github.com/msteinert/pam", ["StartFunc", "Start"]) } | ||
| } | ||
|
|
||
| from | ||
| DataFlow::Node source, DataFlow::Node sink, PamAuthenticate auth, PamStartFunc start, | ||
| PamAcctMgmt actmgt | ||
| where | ||
| not isInTestFile(source.asExpr()) and | ||
| ( | ||
| start.getACall().getResult(0) = source and | ||
| sink = auth.getACall().getReceiver() and | ||
| DataFlow::localFlow(source, sink) and | ||
| not DataFlow::localFlow(source, actmgt.getACall().getReceiver()) | ||
| ) | ||
| select source, "This Pam transaction may not check authorization correctly." | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| | main.go:10:2:12:3 | ... := ...[0] | This Pam transaction may not be secure. | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| experimental/CWE-285/PamAuthBypass.ql |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| module main | ||
|
|
||
| go 1.18 | ||
|
|
||
| require github.com/msteinert/pam v1.0.0 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| package main | ||
|
|
||
| //go:generate depstubber -vendor github.com/msteinert/pam Style,Transaction StartFunc | ||
|
|
||
| import ( | ||
| "github.com/msteinert/pam" | ||
| ) | ||
|
|
||
| func bad() error { | ||
| t, _ := pam.StartFunc("", "", func(s pam.Style, msg string) (string, error) { | ||
| return "", nil | ||
| }) | ||
| return t.Authenticate(0) | ||
|
|
||
| } | ||
|
|
||
| func good() error { | ||
| t, err := pam.StartFunc("", "", func(s pam.Style, msg string) (string, error) { | ||
| return "", nil | ||
| }) | ||
| err = t.Authenticate(0) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| return t.AcctMgmt(0) | ||
| } | ||
|
|
||
| func main() {} |
68 changes: 68 additions & 0 deletions
68
ql/test/experimental/CWE-285/vendor/github.com/msteinert/pam/stub.go
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| # github.com/msteinert/pam v1.0.0 | ||
| ## explicit | ||
| github.com/msteinert/pam |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.