Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# Potentially unguarded protocol handler invocation
Desktop URL handlers accept every scheme registered with the operating system. Passing an untrusted URL to one of these handlers can invoke an unintended application or expose local resources.


## Recommendation
Parse the URL and allow only the schemes required by the application before invoking the protocol handler. For web links, allow only HTTP and HTTPS.


## Example

```cpp
#include <QDesktopServices>
#include <QUrl>

void openUrl(const char *input) {
QDesktopServices::openUrl(QUrl(input)); // BAD
}

void openWebUrl(const char *input) {
QUrl url(input);
if (url.scheme() == "https") {
QDesktopServices::openUrl(url); // GOOD
}
}

```
The bad example opens an untrusted URL directly. The good example first checks that the URL uses HTTPS.


## References
* CVE-2022-43550: [Jitsi Desktop Client RCE by interacting with malicious URL schemes](https://hackerone.com/reports/1692603)
* Common Weakness Enumeration: [CWE-939](https://cwe.mitre.org/data/definitions/939.html).
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#include <QDesktopServices>
#include <QUrl>

void openUrl(const char *input) {
QDesktopServices::openUrl(QUrl(input)); // BAD
}

void openWebUrl(const char *input) {
QUrl url(input);
if (url.scheme() == "https") {
QDesktopServices::openUrl(url); // GOOD
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<!DOCTYPE qhelp SYSTEM "qhelp.dtd">
<qhelp>
<overview>
<p>
Desktop URL handlers accept every scheme registered with the operating system.
Passing an untrusted URL to one of these handlers can invoke an unintended
application or expose local resources.
</p>
</overview>
<recommendation>
<p>
Parse the URL and allow only the schemes required by the application before
invoking the protocol handler. For web links, allow only HTTP and HTTPS.
</p>
</recommendation>
<example>
<sample src="PotentiallyUnguardedProtocolHandler.cpp" />
<p>
The bad example opens an untrusted URL directly. The good example first
checks that the URL uses HTTPS.
</p>
</example>
<references>
<li>
CVE-2022-43550: <a href="https://hackerone.com/reports/1692603">Jitsi Desktop
Client RCE by interacting with malicious URL schemes</a>
</li>
</references>
</qhelp>
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
/**
* @name Potentially unguarded protocol handler invocation
* @id tob/cpp/unguarded-protocol-handler
* @description Finds URL protocol handlers called with URLs from untrusted sources
* @kind path-problem
* @tags security
* external/cwe/cwe-939
* @precision medium
* @problem.severity warning
* @security-severity 6.5
* @group security
*/

import cpp
private import semmle.code.cpp.controlflow.IRGuards
private import semmle.code.cpp.ir.dataflow.TaintTracking
private import semmle.code.cpp.security.CommandExecution
private import semmle.code.cpp.security.FlowSources

predicate isProtocolHandlerLiteral(StringLiteral literal, string handlerType) {
literal.getValue().regexpMatch("(?i).*rundll32.*url\\.dll.*FileProtocolHandler.*") and
handlerType = "rundll32 url.dll,FileProtocolHandler"
or
literal.getValue().regexpMatch("(?i).*xdg-open.*") and
handlerType = "xdg-open"
or
literal.getValue().regexpMatch("(?i).*\\bopen\\b.*") and
not literal.getValue().regexpMatch("(?i).*xdg-open.*") and
handlerType = "open"
}

predicate localTaint(Expr source, Expr sink) {
exists(DataFlow::Node sourceNode, DataFlow::Node sinkNode |
source = [sourceNode.asExpr(), sourceNode.asIndirectExpr()] and
sink = [sinkNode.asExpr(), sinkNode.asIndirectExpr()] and
TaintTracking::localTaint(sourceNode, sinkNode)
)
}

predicate isShellProtocolHandlerCall(FunctionCall call, string handlerType) {
call.getTarget() instanceof SystemFunction and
exists(StringLiteral literal |
isProtocolHandlerLiteral(literal, handlerType) and
localTaint(literal, call.getArgument(0))
)
}

predicate isAllowedScheme(StringLiteral literal, Expr length) {
literal.getValue() = "http://" and length.getValue() = "7"
or
literal.getValue() = "https://" and length.getValue() = "8"
}

predicate comparisonChecksSuccess(IRGuardCondition guard, FunctionCall call, boolean branch) {
exists(EqualityOperation comparison |
comparison = guard.getUnconvertedResultExpression() and
comparison.getAnOperand() = call and
comparison.getAnOperand().getValue() = "0" and
(
comparison instanceof EQExpr and branch = true
or
comparison instanceof NEExpr and branch = false
)
)
}

predicate stringPrefixGuard(IRGuardCondition guard, Expr checked, boolean branch) {
exists(FunctionCall call, StringLiteral allowed, Expr length |
call.getTarget().getName() = ["strncmp", "strncasecmp", "_strnicmp"] and
isAllowedScheme(allowed, length) and
call.getArgument(2) = length and
(
call.getArgument(0) = checked and call.getArgument(1) = allowed
or
call.getArgument(1) = checked and call.getArgument(0) = allowed
) and
comparisonChecksSuccess(guard, call, branch)
)
or
exists(FunctionCall call, StringLiteral allowed |
call = guard.getUnconvertedResultExpression() and
call.getTarget().hasQualifiedName("", "QString", "startsWith") and
call.getArgument(0) = allowed and
allowed.getValue() = ["http://", "https://"] and
checked = call.getQualifier() and
branch = true
)
}

predicate urlSchemeGuardChecks(IRGuardCondition guard, Expr checked, boolean branch) {
stringPrefixGuard(guard, checked, branch)
or
exists(FunctionCall comparison, FunctionCall scheme, StringLiteral allowed |
comparison = guard.getUnconvertedResultExpression() and
comparison.getTarget().getName() = "operator==" and
scheme.getTarget().hasQualifiedName("", "QUrl", "scheme") and
scheme = comparison.getQualifier() and
comparison.getArgument(0) = allowed and
allowed.getValue() = ["http", "https"] and
checked = scheme.getQualifier() and
branch = true
)
}

module PotentiallyUnguardedProtocolHandlerConfig implements DataFlow::ConfigSig {
predicate isSource(DataFlow::Node source) { source instanceof RemoteFlowSource }

predicate isSink(DataFlow::Node sink) {
exists(FunctionCall call |
call.getTarget().hasQualifiedName("", "QDesktopServices", "openUrl") and
sink.asConvertedExpr() = call.getArgument(0)
)
or
exists(FunctionCall call |
isShellProtocolHandlerCall(call, _) and
sink.asIndirectExpr() = call.getArgument(0)
)
}

predicate isBarrier(DataFlow::Node node) {
node = DataFlow::BarrierGuard<urlSchemeGuardChecks/3>::getABarrierNode()
or
node = DataFlow::BarrierGuard<urlSchemeGuardChecks/3>::getAnIndirectBarrierNode()
}
}

module PotentiallyUnguardedProtocolHandlerFlow =
TaintTracking::Global<PotentiallyUnguardedProtocolHandlerConfig>;

import PotentiallyUnguardedProtocolHandlerFlow::PathGraph

from
PotentiallyUnguardedProtocolHandlerFlow::PathNode source,
PotentiallyUnguardedProtocolHandlerFlow::PathNode sink, FunctionCall call, string callType
where
PotentiallyUnguardedProtocolHandlerFlow::flowPath(source, sink) and
(
call.getTarget().hasQualifiedName("", "QDesktopServices", "openUrl") and
sink.getNode().asConvertedExpr() = call.getArgument(0) and
callType = "QDesktopServices::openUrl()"
or
isShellProtocolHandlerCall(call, callType) and
sink.getNode().asIndirectExpr() = call.getArgument(0)
)
select call, source, sink,
callType + " is called with untrusted input from $@ without URL scheme validation.",
source.getNode(), "this source"
1 change: 1 addition & 0 deletions cpp/test/include/libc/stdlib.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
extern "C" {
#endif

extern int system(const char *);
extern void *reallocf(void *, unsigned long);

int rand(void) {
Expand Down
2 changes: 2 additions & 0 deletions cpp/test/include/libc/string_stubs.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ extern int wprintf(const wchar_t * format, ...);
extern wchar_t* wcscpy(wchar_t * s1, const wchar_t * s2);
extern void perror(const char *s);
extern int puts(const char *s);
extern int sprintf(char *, const char *, ...);
extern int strncmp(const char *, const char *, unsigned long);

extern void openlog(const char*, int, int);
extern void syslog(int, const char*, ...);
Expand Down
2 changes: 2 additions & 0 deletions cpp/test/include/libc/unistd.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
extern "C" {
#endif

long read(int, void *, unsigned long);

void _exit(int);

#ifdef __cplusplus
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
#include "../../../include/libc/stdlib.h"
#include "../../../include/libc/string_stubs.h"
#include "../../../include/libc/unistd.h"

struct QString {
QString(const char *value);
bool operator==(const char *other) const;
bool startsWith(const char *prefix) const;
};

struct QUrl {
QUrl(const char *value);
QString scheme() const;
};

struct QDesktopServices {
static bool openUrl(const QUrl &url);
};

void bad_qt() {
char url[1024];
read(0, url, sizeof(url));
QDesktopServices::openUrl(QUrl(url)); // BAD
}

void good_qt_hardcoded() {
QDesktopServices::openUrl(QUrl("https://example.com")); // GOOD
}

void good_qt_prefix_check() {
char url[1024];
read(0, url, sizeof(url));
if (strncmp(url, "https://", 8) == 0) {
QDesktopServices::openUrl(QUrl(url)); // GOOD
}
}

void good_qt_reject_other_prefix() {
char url[1024];
read(0, url, sizeof(url));
if (strncmp(url, "https://", 8) != 0) {
return;
}
QDesktopServices::openUrl(QUrl(url)); // GOOD
}

void good_qt_scheme_check() {
char value[1024];
read(0, value, sizeof(value));
QUrl url(value);
if (url.scheme() == "https") {
QDesktopServices::openUrl(url); // GOOD
}
}

void bad_qt_file_scheme() {
char url[1024];
read(0, url, sizeof(url));
if (strncmp(url, "file://", 7) == 0) {
QDesktopServices::openUrl(QUrl(url)); // BAD
}
}

void bad_qt_partial_prefix() {
char url[1024];
read(0, url, sizeof(url));
if (strncmp(url, "https://", 4) == 0) {
QDesktopServices::openUrl(QUrl(url)); // BAD
}
}

void bad_qt_rejected_scheme() {
char url[1024];
read(0, url, sizeof(url));
if (strncmp(url, "https://", 8) != 0) {
QDesktopServices::openUrl(QUrl(url)); // BAD
}
}

void bad_shell() {
char url[1024];
char command[2048];
read(0, url, sizeof(url));
sprintf(command, "xdg-open '%s'", url);
system(command); // BAD
}

void bad_shell_rundll() {
char url[1024];
char command[2048];
read(0, url, sizeof(url));
sprintf(command, "rundll32 url.dll,FileProtocolHandler '%s'", url);
system(command); // BAD
}

void bad_shell_open() {
char url[1024];
char command[2048];
read(0, url, sizeof(url));
sprintf(command, "open '%s'", url);
system(command); // BAD
}

void good_shell() {
char url[1024];
char command[2048];
read(0, url, sizeof(url));
if (strncmp(url, "https://", 8) == 0) {
sprintf(command, "xdg-open '%s'", url);
system(command); // GOOD
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
edges
Comment thread
ex0dus-0x marked this conversation as resolved.
Resolved
| PotentiallyUnguardedProtocolHandler.cpp:22:11:22:13 | read output argument | PotentiallyUnguardedProtocolHandler.cpp:23:29:23:37 | call to QUrl | provenance | TaintFunction |
| PotentiallyUnguardedProtocolHandler.cpp:58:11:58:13 | read output argument | PotentiallyUnguardedProtocolHandler.cpp:60:31:60:39 | call to QUrl | provenance | TaintFunction |
| PotentiallyUnguardedProtocolHandler.cpp:66:11:66:13 | read output argument | PotentiallyUnguardedProtocolHandler.cpp:68:31:68:39 | call to QUrl | provenance | TaintFunction |
| PotentiallyUnguardedProtocolHandler.cpp:74:11:74:13 | read output argument | PotentiallyUnguardedProtocolHandler.cpp:76:31:76:39 | call to QUrl | provenance | TaintFunction |
| PotentiallyUnguardedProtocolHandler.cpp:83:11:83:13 | read output argument | PotentiallyUnguardedProtocolHandler.cpp:85:10:85:16 | *command | provenance | TaintFunction |
| PotentiallyUnguardedProtocolHandler.cpp:91:11:91:13 | read output argument | PotentiallyUnguardedProtocolHandler.cpp:93:10:93:16 | *command | provenance | TaintFunction |
| PotentiallyUnguardedProtocolHandler.cpp:99:11:99:13 | read output argument | PotentiallyUnguardedProtocolHandler.cpp:101:10:101:16 | *command | provenance | TaintFunction |
nodes
| PotentiallyUnguardedProtocolHandler.cpp:22:11:22:13 | read output argument | semmle.label | read output argument |
| PotentiallyUnguardedProtocolHandler.cpp:23:29:23:37 | call to QUrl | semmle.label | call to QUrl |
| PotentiallyUnguardedProtocolHandler.cpp:58:11:58:13 | read output argument | semmle.label | read output argument |
| PotentiallyUnguardedProtocolHandler.cpp:60:31:60:39 | call to QUrl | semmle.label | call to QUrl |
| PotentiallyUnguardedProtocolHandler.cpp:66:11:66:13 | read output argument | semmle.label | read output argument |
| PotentiallyUnguardedProtocolHandler.cpp:68:31:68:39 | call to QUrl | semmle.label | call to QUrl |
| PotentiallyUnguardedProtocolHandler.cpp:74:11:74:13 | read output argument | semmle.label | read output argument |
| PotentiallyUnguardedProtocolHandler.cpp:76:31:76:39 | call to QUrl | semmle.label | call to QUrl |
| PotentiallyUnguardedProtocolHandler.cpp:83:11:83:13 | read output argument | semmle.label | read output argument |
| PotentiallyUnguardedProtocolHandler.cpp:85:10:85:16 | *command | semmle.label | *command |
| PotentiallyUnguardedProtocolHandler.cpp:91:11:91:13 | read output argument | semmle.label | read output argument |
| PotentiallyUnguardedProtocolHandler.cpp:93:10:93:16 | *command | semmle.label | *command |
| PotentiallyUnguardedProtocolHandler.cpp:99:11:99:13 | read output argument | semmle.label | read output argument |
| PotentiallyUnguardedProtocolHandler.cpp:101:10:101:16 | *command | semmle.label | *command |
subpaths
#select
| PotentiallyUnguardedProtocolHandler.cpp:23:3:23:27 | call to openUrl | PotentiallyUnguardedProtocolHandler.cpp:22:11:22:13 | read output argument | PotentiallyUnguardedProtocolHandler.cpp:23:29:23:37 | call to QUrl | QDesktopServices::openUrl() is called with untrusted input from $@ without URL scheme validation. | PotentiallyUnguardedProtocolHandler.cpp:22:11:22:13 | read output argument | this source |
| PotentiallyUnguardedProtocolHandler.cpp:60:5:60:29 | call to openUrl | PotentiallyUnguardedProtocolHandler.cpp:58:11:58:13 | read output argument | PotentiallyUnguardedProtocolHandler.cpp:60:31:60:39 | call to QUrl | QDesktopServices::openUrl() is called with untrusted input from $@ without URL scheme validation. | PotentiallyUnguardedProtocolHandler.cpp:58:11:58:13 | read output argument | this source |
| PotentiallyUnguardedProtocolHandler.cpp:68:5:68:29 | call to openUrl | PotentiallyUnguardedProtocolHandler.cpp:66:11:66:13 | read output argument | PotentiallyUnguardedProtocolHandler.cpp:68:31:68:39 | call to QUrl | QDesktopServices::openUrl() is called with untrusted input from $@ without URL scheme validation. | PotentiallyUnguardedProtocolHandler.cpp:66:11:66:13 | read output argument | this source |
| PotentiallyUnguardedProtocolHandler.cpp:76:5:76:29 | call to openUrl | PotentiallyUnguardedProtocolHandler.cpp:74:11:74:13 | read output argument | PotentiallyUnguardedProtocolHandler.cpp:76:31:76:39 | call to QUrl | QDesktopServices::openUrl() is called with untrusted input from $@ without URL scheme validation. | PotentiallyUnguardedProtocolHandler.cpp:74:11:74:13 | read output argument | this source |
| PotentiallyUnguardedProtocolHandler.cpp:85:3:85:8 | call to system | PotentiallyUnguardedProtocolHandler.cpp:83:11:83:13 | read output argument | PotentiallyUnguardedProtocolHandler.cpp:85:10:85:16 | *command | xdg-open is called with untrusted input from $@ without URL scheme validation. | PotentiallyUnguardedProtocolHandler.cpp:83:11:83:13 | read output argument | this source |
| PotentiallyUnguardedProtocolHandler.cpp:93:3:93:8 | call to system | PotentiallyUnguardedProtocolHandler.cpp:91:11:91:13 | read output argument | PotentiallyUnguardedProtocolHandler.cpp:93:10:93:16 | *command | rundll32 url.dll,FileProtocolHandler is called with untrusted input from $@ without URL scheme validation. | PotentiallyUnguardedProtocolHandler.cpp:91:11:91:13 | read output argument | this source |
| PotentiallyUnguardedProtocolHandler.cpp:101:3:101:8 | call to system | PotentiallyUnguardedProtocolHandler.cpp:99:11:99:13 | read output argument | PotentiallyUnguardedProtocolHandler.cpp:101:10:101:16 | *command | open is called with untrusted input from $@ without URL scheme validation. | PotentiallyUnguardedProtocolHandler.cpp:99:11:99:13 | read output argument | this source |
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
security/PotentiallyUnguardedProtocolHandler/PotentiallyUnguardedProtocolHandler.ql
Loading
Loading