-
Notifications
You must be signed in to change notification settings - Fork 1.7k
CPP: PAM Authorization Bypass #8775
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
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
49 changes: 49 additions & 0 deletions
49
cpp/ql/src/experimental/Security/CWE/CWE-285/PamAuthorization.qhelp
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 @@ | ||
<!DOCTYPE qhelp PUBLIC "-//Semmle//qhelp//EN" "qhelp.dtd"> | ||
<qhelp> | ||
<overview> | ||
<p> | ||
Using only a call to | ||
<code>pam_authenticate</code> | ||
to check the validity of a login can lead to authorization bypass vulnerabilities. | ||
</p> | ||
<p> | ||
A | ||
<code>pam_authenticate</code> | ||
only verifies the credentials of a user. It does not check if a user has an appropriate authorization to actually login. This means a user with a expired login or a password can still access the system. | ||
</p> | ||
|
||
</overview> | ||
|
||
<recommendation> | ||
<p> | ||
A call to | ||
<code>pam_authenticate</code> | ||
should be followed by a call to | ||
<code>pam_acct_mgmt</code> | ||
to check if a user is allowed to login. | ||
</p> | ||
</recommendation> | ||
|
||
<example> | ||
<p> | ||
In the following example, the code only checks the credentials of a user. Hence, in this case, a user expired with expired creds can still login. This can be verified by creating a new user account, expiring it with | ||
<code>chage -E0 `username` </code> | ||
and then trying to log in. | ||
</p> | ||
<sample src="PamAuthorizationBad.c" /> | ||
|
||
<p> | ||
This can be avoided by calling | ||
<code>pam_acct_mgmt</code> | ||
call to verify access as has been done in the snippet shown below. | ||
</p> | ||
<sample src="PamAuthorizationGood.c" /> | ||
This conversation was marked as resolved.
Show resolved
Hide resolved
|
||
</example> | ||
|
||
<references> | ||
<li> | ||
Man-Page: | ||
<a href="https://man7.org/linux/man-pages/man3/pam_acct_mgmt.3.html">pam_acct_mgmt</a> | ||
</li> | ||
</references> | ||
</qhelp> |
33 changes: 33 additions & 0 deletions
33
cpp/ql/src/experimental/Security/CWE/CWE-285/PamAuthorization.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,33 @@ | ||
/** | ||
* @name PAM Authorization bypass | ||
* @description Only using `pam_authenticate` call to authenticate users can lead to authorization vulnerabilities. | ||
* @kind problem | ||
* @problem.severity error | ||
* @id cpp/pam-auth-bypass | ||
* @tags security | ||
* external/cwe/cwe-285 | ||
*/ | ||
import cpp | ||
import semmle.code.cpp.dataflow.DataFlow | ||
import semmle.code.cpp.valuenumbering.GlobalValueNumbering | ||
|
||
private class PamAuthCall extends FunctionCall { | ||
PamAuthCall() { | ||
exists(Function f | f.hasName("pam_authenticate") | f.getACallToThisFunction() = this) | ||
} | ||
} | ||
|
||
private class PamActMgmtCall extends FunctionCall { | ||
PamActMgmtCall() { | ||
exists(Function f | f.hasName("pam_acct_mgmt") | f.getACallToThisFunction() = this) | ||
} | ||
} | ||
|
||
from PamAuthCall pa, Expr handle | ||
where | ||
pa.getArgument(0) = handle and | ||
not exists(PamActMgmtCall pac | | ||
globalValueNumber(handle) = globalValueNumber(pac.getArgument(0)) or | ||
DataFlow::localExprFlow(handle, pac.getArgument(0)) | ||
) | ||
select pa, "This PAM authentication call may be lead to an authorization bypass." |
20 changes: 20 additions & 0 deletions
20
cpp/ql/src/experimental/Security/CWE/CWE-285/PamAuthorizationBad.cpp
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,20 @@ | ||
bool PamAuthGood(const std::string &username_in, | ||
const std::string &password_in, | ||
std::string &authenticated_username) | ||
{ | ||
|
||
struct pam_handle *pamh = nullptr; /* pam session handle */ | ||
|
||
const char *username = username_in.c_str(); | ||
int err = pam_start("test", username, | ||
0, &pamh); | ||
if (err != PAM_SUCCESS) | ||
{ | ||
return false; | ||
} | ||
|
||
err = pam_authenticate(pamh, 0); // BAD | ||
if (err != PAM_SUCCESS) | ||
return err; | ||
return true; | ||
} |
24 changes: 24 additions & 0 deletions
24
cpp/ql/src/experimental/Security/CWE/CWE-285/PamAuthorizationGood.cpp
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,24 @@ | ||
bool PamAuthGood(const std::string &username_in, | ||
const std::string &password_in, | ||
std::string &authenticated_username) | ||
{ | ||
|
||
struct pam_handle *pamh = nullptr; /* pam session handle */ | ||
|
||
const char *username = username_in.c_str(); | ||
int err = pam_start("test", username, | ||
0, &pamh); | ||
if (err != PAM_SUCCESS) | ||
{ | ||
return false; | ||
} | ||
|
||
err = pam_authenticate(pamh, 0); | ||
if (err != PAM_SUCCESS) | ||
return err; | ||
|
||
err = pam_acct_mgmt(pamh, 0); // GOOD | ||
if (err != PAM_SUCCESS) | ||
return err; | ||
return true; | ||
} |
1 change: 1 addition & 0 deletions
1
cpp/ql/test/experimental/query-tests/Security/CWE/CWE-285/PamAuthorization.expected
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 @@ | ||
| test.cpp:29:11:29:26 | call to pam_authenticate | This PAM authentication call may be lead to an authorization bypass. | |
1 change: 1 addition & 0 deletions
1
cpp/ql/test/experimental/query-tests/Security/CWE/CWE-285/PamAuthorization.qlref
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/Security/CWE/CWE-285/PamAuthorization.ql |
59 changes: 59 additions & 0 deletions
59
cpp/ql/test/experimental/query-tests/Security/CWE/CWE-285/test.cpp
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,59 @@ | ||
#include "../../../../../library-tests/dataflow/taint-tests/stl.h" | ||
|
||
using namespace std; | ||
|
||
#define PAM_SUCCESS 1 | ||
|
||
typedef struct pam_handle | ||
{ | ||
}; | ||
int pam_start(std::string servicename, std::string username, int a, struct pam_handle **); | ||
int pam_authenticate(struct pam_handle *, int e); | ||
int pam_acct_mgmt(struct pam_handle *, int e); | ||
|
||
bool PamAuthBad(const std::string &username_in, | ||
const std::string &password_in, | ||
std::string &authenticated_username) | ||
{ | ||
|
||
struct pam_handle *pamh = nullptr; /* pam session handle */ | ||
|
||
const char *username = username_in.c_str(); | ||
int err = pam_start("test", username, | ||
0, &pamh); | ||
if (err != PAM_SUCCESS) | ||
{ | ||
return false; | ||
} | ||
|
||
err = pam_authenticate(pamh, 0); | ||
if (err != PAM_SUCCESS) | ||
return err; | ||
|
||
return true; | ||
} | ||
|
||
bool PamAuthGood(const std::string &username_in, | ||
const std::string &password_in, | ||
std::string &authenticated_username) | ||
{ | ||
|
||
struct pam_handle *pamh = nullptr; /* pam session handle */ | ||
|
||
const char *username = username_in.c_str(); | ||
int err = pam_start("test", username, | ||
0, &pamh); | ||
if (err != PAM_SUCCESS) | ||
{ | ||
return false; | ||
} | ||
|
||
err = pam_authenticate(pamh, 0); | ||
if (err != PAM_SUCCESS) | ||
return err; | ||
|
||
err = pam_acct_mgmt(pamh, 0); | ||
if (err != PAM_SUCCESS) | ||
return err; | ||
return true; | ||
} |
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.