Skip to content
This repository was archived by the owner on Jan 5, 2023. It is now read-only.

Golang : Add Query To Detect PAM Authorization Bugs#709

Closed
ghost wants to merge 1 commit intomainfrom
unknown repository
Closed

Golang : Add Query To Detect PAM Authorization Bugs#709
ghost wants to merge 1 commit intomainfrom
unknown repository

Conversation

@ghost
Copy link

@ghost ghost commented Mar 29, 2022

Using merely pam_authneticate call to grant access to a user can cause security issue. The pam_authenticate call only checks if the username and the password match. It does not check if the account is expired. Hence, a user with an expired login or an expired password can still login.

This issue is fairly common and was recently found in gogs/gogs and go-gitea/gitea. In this case, the bugs were assigned CVE-2022-0871 and CVE-2022-0905 respectively.

This PR aims to detect instances were an initiated PAM Transaction invokes the Authenticate method but does not invoke a call to the AcctMgmt method. Due to the nature of the query, I don't expect that may FP's or FN's. There could some unwanted alerts due to test code but to avoid that I filter out all file paths which have test in them.

With this PR, I submit two queries, one using local data flow and the other using global taint flow. The global taint flow one should be more precise but may have a performance penalty while the other local flow one may lead to FN's but should be faster to execute.

A quick run of both these queries on roughly 17 projects can be found in the LGTM runs below.

All of the results appear to be correct. The runs also includes two projects procupineyhairs/gogs_pam and porcupineyhairs/gitea_pam. These are vulnerable versions of the projects mentioned above. The alerts show a valid detection of the CVE. No alerts for the gogs and gitea projects shows detection of the patch correcting the bug.

I am also working on two more PR's for C and Python respectively which detect the same underlying issue.

@ghost ghost self-requested a review as a code owner March 29, 2022 10:30
@smowton
Copy link
Contributor

smowton commented Mar 29, 2022

Is there an assosciated bounty app?

Using merely `pam_authneticate` call to grant access to a user
can cause security issue. The `pam_authenticate` call only checks if the
username and the password match. It does not check if the account is
expired. Hence, a user with an expired login or an expired password can
still login

This issue is fairly common and was recently found in
[gogs/gogs](https://www.github.com/gogs/gogs) and
[go-gitea/gitea](https://www.github.com/go-gitea/gitea). In this case, the bugs were assigned [CVE-2022-0871](https://nvd.nist.gov/vuln/detail/CVE-2022-0871)
 and [CVE-2022-0905](https://nvd.nist.gov/vuln/detail/CVE-2022-0905) respectiely.

This PR aims to detect instances were an initiated PAM Transaction invokes
the `Authenticate` method but does not invoke a call to the `AcctMgmt` method.
Due to the nature of the query, I don't exect that may FP's or FN's.
There could some unwanted alerts due to test code but to avoid that I
filter out all file paths which have `test` in their name.

With this PR, I submit two queries, one using local data flow and the other using
global taint flow. The global taint flow one should be more precise but
may have a performance penalty while the other local flow one may lead
to FN's but should be faster to execute.

A quick run of both these queries on roughly 17 projects can be found in
the LGTM runs below.
* [global taint flow](https://lgtm.com/query/8603256578345367962/)
* [local data flow](https://lgtm.com/query/5088026685932206344/)

All of the results appear to be correct. The runs also includes
two projects `procupineyhairs/gogs_pam` and `porcupineyhairs/gitea_pam`.
These are vulnerable versions of the projects mentioned above. The
alerts show a valid detection of the CVE. No alerts for the `gogs`
and `gitea` projects shows detection of the patch correcting the bug.

I am also working on two more PR's for C and Python respectively which detect the same underlying issue.

minor
emersion pushed a commit to emersion/webpass that referenced this pull request May 24, 2022
Not using `pam_acct_mgmt` after `pam_authenticate` to check the validity of a login can lead to an authorization bypass

## Common Weakness Enumeration(CWE) category
CWE - 863

## Root Cause Analysis

In this case, in the following PAM transaction, only a call to `pam.Authenticate` is used to login a user.

https://lgtm.com/projects/g/emersion/webpass/snapshot/c3217efc9100880b960b1e085c30d5f95c3d3471/files/config/auth_pam.go#x72bc4d6069cdb224:1

This implies that a user with expired credentials can still login.

The bug can be verified easily by creating a new user account, expiring it with <code>chage -E0 `username` </code> and then trying to log in with the expired credentials.

## Remediation

This can be fixed by invoking a call to `pam.AcctMgmt` after a successful call to `pam.Authenticate`

## Common Vulnerability Scoring System Vector

### Exploitability

The attack can be carried over the network. A complex non-standard configuration or a specialized condition is required for the attack to be successfully conducted. The attacker also requires access to a users credentials, be it expired, for an attack to be successful. There is no user interaction required for successful execution. The attack can affect components outside the scope of the target module.

### Impact
Using this attack vector, an attacker may access otherwise restricted parts of the system. The attack can be used to gain access to confidential files like passwords, login credentials and other secrets. Hence, it has a high impact on confidentiality. It may also be directly used to affect a change on a system resource. Hence has a medium to high impact on integrity. This attack may not be used to affect the availability of the system. Taking this account an appropriate CVSS v3.1 vector would be

(AV:N/AC:H/PR:L/UI:N/S:C/C:H/I:L/A:N)[https://nvd.nist.gov/vuln-metrics/cvss/v3-calculator?vector=AV:N/AC:L/PR:N/UI:N/S:C/C:H/I:N/A:L&version=3.1]

This gives it a base score of 7.7/10 and a severity rating of high.

## References
* [Man Page for pam_acct_mgmt](https://man7.org/linux/man-pages/man3/pam_acct_mgmt.3.html)
* [CWE-863](http://cwe.mitre.org/data/definitions/863.html)
* [CWE-285](http://cwe.mitre.org/data/definitions/285.html)
* github/securitylab#561
* github/codeql-go#709

### This bug was found using *[CodeQL by Github](https://codeql.github.com/)*
matzf pushed a commit to netsec-ethz/scion-apps that referenced this pull request May 27, 2022
…229)

Not using `pam_acct_mgmt` after `pam_authenticate` to check the validity of a login can lead to an authorization bypass

## Common Weakness Enumeration(CWE) category
CWE - 863

## Root Cause Analysis

In this case, in the following PAM transaction, only a call to `pam.Authenticate` is used to login a user.

https://lgtm.com/projects/g/netsec-ethz/scion-apps/snapshot/52ffbbafc507ba067d56eb9ec482b41c19fc84ed/files/ssh/server/ssh/authcallbacks.go#xecf1a4150da8e10d:1

This implies that a user with expired credentials can still login.

The bug can be verified easily by creating a new user account, expiring it with <code>chage -E0 `username` </code> and then trying to log in with the expired credentials.

## Remediation

This can be fixed by invoking a call to `pam.AcctMgmt` after a successful call to `pam.Authenticate`

## Common Vulnerability Scoring System Vector

### Exploitability

The attack can be carried over the network. A complex non-standard configuration or a specialized condition is required for the attack to be successfully conducted. The attacker also requires access to a users credentials, be it expired, for an attack to be successful. There is no user interaction required for successful execution. The attack can affect components outside the scope of the target module.

### Impact
Using this attack vector, an attacker may access otherwise restricted parts of the system. The attack can be used to gain access to confidential files like passwords, login credentials and other secrets. Hence, it has a high impact on confidentiality. It may also be directly used to affect a change on a system resource. Hence has a medium to high impact on integrity. This attack may not be used to affect the availability of the system. Taking this account an appropriate CVSS v3.1 vector would be

(AV:N/AC:H/PR:L/UI:N/S:C/C:H/I:L/A:N)[https://nvd.nist.gov/vuln-metrics/cvss/v3-calculator?vector=AV:N/AC:L/PR:N/UI:N/S:C/C:H/I:N/A:L&version=3.1]

This gives it a base score of 7.7/10 and a severity rating of high.

## References
* [Man Page for pam_acct_mgmt](https://man7.org/linux/man-pages/man3/pam_acct_mgmt.3.html)
* [CWE-863](http://cwe.mitre.org/data/definitions/863.html)
* [CWE-285](http://cwe.mitre.org/data/definitions/285.html)
* github/securitylab#561
* github/codeql-go#709

### This bug was found using *[CodeQL by Github](https://codeql.github.com/)*
edospadoni pushed a commit to nethesis/nethvoice-report that referenced this pull request May 27, 2022
…ity of a login can lead to an authorization bypass (#172)

CWE - 863

In this case, in the following PAM transaction, only a call to `pam.Authenticate` is used to login a user.

https://lgtm.com/projects/g/nethesis/nethvoice-report/snapshot/4c37ee73a69564895b9db44ec2959766f0fa7bfe/files/api/methods/auth.go#x4b20c1ca54cad81d:1

This implies that a user with expired credentials can still login.

The bug can be verified easily by creating a new user account, expiring it with <code>chage -E0 `username` </code> and then trying to log in with the expired credentials.

This can be fixed by invoking a call to `pam.AcctMgmt` after a successful call to `pam.Authenticate`

The attack can be carried over the network. A complex non-standard configuration or a specialized condition is required for the attack to be successfully conducted. The attacker also requires access to a users credentials, be it expired, for an attack to be successful. There is no user interaction required for successful execution. The attack can affect components outside the scope of the target module.

Using this attack vector, an attacker may access otherwise restricted parts of the system. The attack can be used to gain access to confidential files like passwords, login credentials and other secrets. Hence, it has a high impact on confidentiality. It may also be directly used to affect a change on a system resource. Hence has a medium to high impact on integrity. This attack may not be used to affect the availability of the system. Taking this account an appropriate CVSS v3.1 vector would be

(AV:N/AC:H/PR:L/UI:N/S:C/C:H/I:L/A:N)[https://nvd.nist.gov/vuln-metrics/cvss/v3-calculator?vector=AV:N/AC:L/PR:N/UI:N/S:C/C:H/I:N/A:L&version=3.1]

This gives it a base score of 7.7/10 and a severity rating of high.

* [Man Page for pam_acct_mgmt](https://man7.org/linux/man-pages/man3/pam_acct_mgmt.3.html)
* [CWE-863](http://cwe.mitre.org/data/definitions/863.html)
* [CWE-285](http://cwe.mitre.org/data/definitions/285.html)
* github/securitylab#561
* github/codeql-go#709
Copy link
Contributor

@smowton smowton left a comment

Choose a reason for hiding this comment

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

Please also rebase this PR against https://github.com/github/codeql/tree/main/go as the codeql-go repo has been retired.

@@ -0,0 +1,49 @@
/**
* @name PAM Authentication Bypass
Copy link
Contributor

Choose a reason for hiding this comment

The 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?

Copy link
Author

Choose a reason for hiding this comment

The 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.

@ghost
Copy link
Author

ghost commented May 30, 2022

@smowton I have created a PR with the requested changes.

I am closing this one now.

Superseded by github/codeql#9377

@ghost ghost closed this May 30, 2022
@ghost ghost deleted the pam branch May 30, 2022 20:04
This pull request was closed.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant