Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
62 commits
Select commit Hold shift + click to select a range
608f4dc
add test
xiaoxinz-cisco Jul 15, 2021
a3a76fe
test
xiaoxinz-cisco Jul 15, 2021
ee247f3
test
xiaoxinz-cisco Jul 15, 2021
58954cd
fix ql
xiaoxinz-cisco Jul 15, 2021
a029ad0
add one ql
xiaoxinz-cisco Jul 16, 2021
3b139cc
fix path
xiaoxinz-cisco Jul 16, 2021
b67d271
add one column
xiaoxinz-cisco Jul 16, 2021
29539c4
update select syntax
xiaoxinz-cisco Jul 16, 2021
cc9702f
4th column - string
xiaoxinz-cisco Jul 16, 2021
a910bb3
2nd column to string
xiaoxinz-cisco Jul 16, 2021
2ba9fc4
add one syslog ql
xiaoxinz-cisco Jul 21, 2021
3521131
pass getArgument(0) and see output
xiaoxinz-cisco Jul 21, 2021
5c03754
find calls have LOG_DEBUG
xiaoxinz-cisco Jul 21, 2021
09d56a7
test
xiaoxinz-cisco Jul 21, 2021
f0e8629
test
xiaoxinz-cisco Jul 22, 2021
1a0478c
test
xiaoxinz-cisco Jul 22, 2021
80a9efd
update
xiaoxinz-cisco Jul 22, 2021
0574409
test
xiaoxinz-cisco Jul 22, 2021
a1a36a9
test
xiaoxinz-cisco Jul 22, 2021
56f3be7
fix typo
xiaoxinz-cisco Jul 22, 2021
70b04a6
test
xiaoxinz-cisco Jul 23, 2021
eb3f83a
test
xiaoxinz-cisco Jul 23, 2021
ffb1212
update ql
xiaoxinz-cisco Jul 27, 2021
57ae8d2
update
xiaoxinz-cisco Jul 27, 2021
559b9ec
test
xiaoxinz-cisco Jul 27, 2021
65620b6
test
xiaoxinz-cisco Jul 27, 2021
aa78d6e
test
xiaoxinz-cisco Jul 27, 2021
a4f9636
test
xiaoxinz-cisco Jul 28, 2021
34586ca
test
xiaoxinz-cisco Jul 28, 2021
c615528
test
xiaoxinz-cisco Jul 30, 2021
8809fbe
test
xiaoxinz-cisco Jul 30, 2021
c06a19d
test
xiaoxinz-cisco Jul 30, 2021
1d12805
test
xiaoxinz-cisco Jul 30, 2021
8b6340e
test
xiaoxinz-cisco Jul 30, 2021
ea04ecc
test
xiaoxinz-cisco Jul 30, 2021
d8aa084
test
xiaoxinz-cisco Aug 4, 2021
8cf9d8b
remove comment
xiaoxinz-cisco Aug 4, 2021
ccd49b1
add two ios_msg ql
xiaoxinz-cisco Aug 6, 2021
3140533
a fix
xiaoxinz-cisco Aug 6, 2021
6c8e236
fix
xiaoxinz-cisco Aug 6, 2021
4115dd0
test
xiaoxinz-cisco Aug 6, 2021
209a2bb
test
xiaoxinz-cisco Aug 6, 2021
b185318
test
xiaoxinz-cisco Aug 6, 2021
285796e
test
xiaoxinz-cisco Aug 6, 2021
00cf0e3
add ios_debugmsg
xiaoxinz-cisco Aug 9, 2021
f6ea6a3
rename ql
xiaoxinz-cisco Aug 9, 2021
46eeab4
test
xiaoxinz-cisco Aug 10, 2021
898c313
add is_down check
xiaoxinz-cisco Aug 11, 2021
bd541c0
test
xiaoxinz-cisco Aug 11, 2021
6625528
test
xiaoxinz-cisco Aug 11, 2021
f324c97
test
xiaoxinz-cisco Aug 11, 2021
66d23e3
test
xiaoxinz-cisco Aug 11, 2021
927ff02
test
xiaoxinz-cisco Aug 12, 2021
a08ff72
test
xiaoxinz-cisco Aug 12, 2021
e1736e8
test
xiaoxinz-cisco Aug 12, 2021
3c550ae
test
xiaoxinz-cisco Aug 12, 2021
be03d65
test
xiaoxinz-cisco Aug 12, 2021
6353513
test
xiaoxinz-cisco Aug 12, 2021
b691af1
test
xiaoxinz-cisco Aug 12, 2021
cfd91a8
test
xiaoxinz-cisco Aug 12, 2021
6fd17be
test
xiaoxinz-cisco Aug 12, 2021
fbb0362
test
xiaoxinz-cisco Aug 13, 2021
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
4 changes: 2 additions & 2 deletions cpp/ql/src/Best Practices/BlockWithTooManyStatements.ql
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,6 @@ where
n > 3 and
complexStmt = b.getAStmt()
select b,
"Block with too many statements (" + n.toString() +
"Test works. Block with too many statements (" + n.toString() +
" complex statements in the block). Complex statements at: $@", complexStmt,
complexStmt.toString()
complexStmt.getEnclosingFunction().toString()
21 changes: 21 additions & 0 deletions cpp/ql/src/devx/analyzeSyslog.ql
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/**
* @name Name: Analyze syslog
* @description Description: test
* @kind problem
* @problem.severity recommendation
* @precision high
* @id cpp/analyze-syslog
* @tags testability
* readability
* maintainability
*/



import cpp
import semmle.code.cpp.models.interfaces.FormattingFunction

from string format, FormattingFunctionCall fc
where format = fc.getFormat().getValue() and format.regexpMatch(".*")
and fc.getTarget().hasName("syslog")
select fc, "This log message format does not meet the requirements."
44 changes: 44 additions & 0 deletions cpp/ql/src/devx/checkMacros.ql
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/**
* @name Name: Check macros
* @description Description: Ensure that macros like __FUNCTION__, __FILE__ and __LINE__ are part of only debug logs, and not others.
* @kind problem
* @problem.severity recommendation
* @precision high
* @id cpp/check-macros
* @tags testability
* readability
* maintainability
*/

import cpp
import semmle.code.cpp.commons.Printf
import semmle.code.cpp.models.interfaces.FormattingFunction

// Find the syslog calls that meet two conditions

// 1. First parameter is not "LOG_DEBUG". Such as LOG_ERR.

// 2. Macros in log messages.

// Example: syslog(LOG_ERR, "%s: Failed init_producer", __FUNCTION__);

/*
Holds if the log macro is debug.
*/
predicate isLogDebug(Expr mie) {
exists(MacroInvocation mi |
mi.getExpr() = mie and
(
mi.getMacroName() = "LOG_DEBUG"
)
)
}

from string format, FormattingFunctionCall fc, int n, string name
where
format = fc.getFormat().getValue() and // format: "%s: Failed init_producer"
fc.getTarget().hasName("syslog") and
not isLogDebug(fc.getArgument(0)) and
name = fc.getConversionArgument(n).toString() and
name in ["__FUNCTION__", "__FILE__", "__LINE__"]
select fc, "Argument " + n + " of " + fc.toString() + " is " + name
49 changes: 49 additions & 0 deletions cpp/ql/src/devx/discoverInput.ql
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/**
* @name Discovering program input
* @description https://securitylab.github.com/research/bug-hunting-codeql-rsyslog/
* @kind problem
* @problem.severity recommendation
* @precision high
* @id cpp/discover-input
* @tags testability
* readability
* maintainability
*/

import cpp


// FunctionCall predicates:
// getTarget() : Gets the function called by this call.

// inheritated predicate:
// getFile() : Gets the primary file where this element occurs.

// getName() : Gets the name of this declaration.


class ReadFunctionCall extends FunctionCall {
ReadFunctionCall() {
this.getTarget().getName() = "pread" or
this.getTarget().getName() = "read" or
this.getTarget().getName() = "readv" or
this.getTarget().getName() = "recvfrom" or
this.getTarget().getName() = "recvmsg" or
this.getTarget().getName() = "recv"
}
}

from ReadFunctionCall call
select call.getFile(), call.getEnclosingFunction().toString(), call, "placeholder"

// Notes
// run this query on rsyslog/rsyslog in LGTM
// result: https://lgtm.com/query/6984839753043321725/
// one result example:
// col: /opt/src/action.c <--- call.getFile(), https://github.com/rsyslog/rsyslog/blob/master/action.c
// col1: checkExternalStateFile <--- call.getEnclosingFunction()
// call: call to read <--- call
// checkExternalStateFile(...):
// ...
// r = read(fd, filebuf, sizeof(filebuf) - 1);
// ...
18 changes: 18 additions & 0 deletions cpp/ql/src/devx/ios_ALLmsg.ql
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/**
* @name Name: Find ios_*msg
* @description Description: Finding all functions with name "ios_*msg"
* @kind problem
* @problem.severity recommendation
* @precision high
* @id cpp/check-macros
* @tags testability
* readability
* maintainability
*/

import cpp

from Function f
where
f.getName().regexpMatch("ios_.*msg")
select f.getACallToThisFunction(), "Function name is: "+f.getName()
18 changes: 18 additions & 0 deletions cpp/ql/src/devx/ios_debugmsg.ql
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/**
* @name Name: Find ios_debugmsg
* @description Description: Finding all functions with name "ios_debugmsg"
* @kind problem
* @problem.severity recommendation
* @precision high
* @id cpp/ios-msg
* @tags testability
* readability
* maintainability
*/

import cpp

from Function f
where
f.getName().regexpMatch("ios_debugmsg.*")
select f.getACallToThisFunction(), "Function name is: "+f.getName()
18 changes: 18 additions & 0 deletions cpp/ql/src/devx/ios_errmsg.ql
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/**
* @name Name: Find ios_errmsg
* @description Description: Finding all functions with name "ios_errmsg"
* @kind problem
* @problem.severity recommendation
* @precision high
* @id cpp/ios-msg
* @tags testability
* readability
* maintainability
*/

import cpp

from Function f
where
f.getName().regexpMatch("ios_errmsg")
select f.getACallToThisFunction(), "Function name is: "+f.getName()
18 changes: 18 additions & 0 deletions cpp/ql/src/devx/ios_msg_ALL.ql
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/**
* @name Name: Find ios_msg_*
* @description Description: Finding all functions with name "ios_msg_*"
* @kind problem
* @problem.severity recommendation
* @precision high
* @id cpp/ios-msg
* @tags testability
* readability
* maintainability
*/

import cpp

from Function f
where
f.getName().regexpMatch("ios_msg_.*")
select f.getACallToThisFunction(), "Function name is: "+f.getName()
37 changes: 37 additions & 0 deletions cpp/ql/src/devx/is_DOWN.ql
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/**
* @name Name: Contextual language check.
* @description Description: Find out the logs that not properly use contextual language.
* @kind problem
* @problem.severity recommendation
* @precision high
* @id cpp/contextual-lang
* @tags testability
* readability
* maintainability
*/

import cpp

// Example:
// There are "Interface Ethernet1/1 is DOWN"-like logs.
// The expected format is "Interface=Ethernet1/1 State=DOWN"

// Examples in xr:
// 1.
// https://gh-xr.scm.engit.cisco.com/xr/iosxr/blob/main/infra/autonomic-networking/common/src/an_event_mgr.c#L270
// DEBUG_AN_LOG(AN_LOG_CD_EVENT, AN_DEBUG_INFO, NULL,
// "\n%sInterface %s is UP", an_cd_event, an_if_info->if_name); <-------------------

// 2.
// https://gh-xr.scm.engit.cisco.com/xr/iosxr/blob/main/ip/bfd/src/bfd_api_server.c#L11078
// BFD_DEBUG_TRACE("Filtering scn for session %s"
// " Session state is Unknown, SCN state is DOWN" <-------------------
// " and session is not bundle member",
// bfd_session_key_str(&session->session_key));

from FunctionCall fc, int i
where
fc.getArgument(i).getValue().regexpMatch(".*Interface.*")
// fc.getTarget().hasName("DEBUG_AN_LOG")
// format.regexpMatch(".*Interface [a-zA-Z0-9/%]+ is (DOWN|UP)")
select fc, "Function: "+fc.getTarget().getName()+" Log: "+fc.getArgument(i).getValue()