Skip to content

Commit 1eac199

Browse files
committed
Merge branch 'main' into python-untrusted-flow
2 parents 4ab3fff + 931322e commit 1eac199

File tree

141 files changed

+55923
-32732
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

141 files changed

+55923
-32732
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
lgtm,codescanning
2+
* The queries `cpp/local-variable-hides-global-variable` and `cpp/missing-header-guard` now have severity `recommendation` instead of `warning`.

cpp/ql/src/Best Practices/Hiding/LocalVariableHidesGlobalVariable.ql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* @name Local variable hides global variable
33
* @description A local variable or parameter that hides a global variable of the same name. This may be confusing. Consider renaming one of the variables.
44
* @kind problem
5-
* @problem.severity warning
5+
* @problem.severity recommendation
66
* @precision very-high
77
* @id cpp/local-variable-hides-global-variable
88
* @tags maintainability

cpp/ql/src/Security/CWE/CWE-020/SafeExternalAPIFunction.qll

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
*/
44

55
private import cpp
6-
private import semmle.code.cpp.models.implementations.Pure
6+
private import semmle.code.cpp.models.interfaces.SideEffect
77

88
/**
99
* A `Function` that is considered a "safe" external API from a security perspective.
@@ -13,9 +13,12 @@ abstract class SafeExternalAPIFunction extends Function { }
1313
/** The default set of "safe" external APIs. */
1414
private class DefaultSafeExternalAPIFunction extends SafeExternalAPIFunction {
1515
DefaultSafeExternalAPIFunction() {
16-
// implementation note: this should be based on the properties of public interfaces, rather than accessing implementation classes directly. When we've done that, the three classes referenced here should be made fully private.
17-
this instanceof PureStrFunction or
18-
this instanceof StrLenFunction or
19-
this instanceof PureMemFunction
16+
// If a function does not write to any of its arguments, we consider it safe to
17+
// pass untrusted data to it. This means that string functions such as `strcmp`
18+
// and `strlen`, as well as memory functions such as `memcmp`, are considered safe.
19+
exists(SideEffectFunction model | model = this |
20+
model.hasOnlySpecificWriteSideEffects() and
21+
not model.hasSpecificWriteSideEffect(_, _, _)
22+
)
2023
}
2124
}

cpp/ql/src/Security/CWE/CWE-020/ir/SafeExternalAPIFunction.qll

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
*/
44

55
private import cpp
6-
private import semmle.code.cpp.models.implementations.Pure
6+
private import semmle.code.cpp.models.interfaces.SideEffect
77

88
/**
99
* A `Function` that is considered a "safe" external API from a security perspective.
@@ -13,9 +13,12 @@ abstract class SafeExternalAPIFunction extends Function { }
1313
/** The default set of "safe" external APIs. */
1414
private class DefaultSafeExternalAPIFunction extends SafeExternalAPIFunction {
1515
DefaultSafeExternalAPIFunction() {
16-
// implementation note: this should be based on the properties of public interfaces, rather than accessing implementation classes directly. When we've done that, the three classes referenced here should be made fully private.
17-
this instanceof PureStrFunction or
18-
this instanceof StrLenFunction or
19-
this instanceof PureMemFunction
16+
// If a function does not write to any of its arguments, we consider it safe to
17+
// pass untrusted data to it. This means that string functions such as `strcmp`
18+
// and `strlen`, as well as memory functions such as `memcmp`, are considered safe.
19+
exists(SideEffectFunction model | model = this |
20+
model.hasOnlySpecificWriteSideEffects() and
21+
not model.hasSpecificWriteSideEffect(_, _, _)
22+
)
2023
}
2124
}

cpp/ql/src/jsf/4.07 Header Files/AV Rule 35.ql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* the file from being included twice). This prevents errors and
55
* inefficiencies caused by repeated inclusion.
66
* @kind problem
7-
* @problem.severity warning
7+
* @problem.severity recommendation
88
* @precision high
99
* @id cpp/missing-header-guard
1010
* @tags efficiency

cpp/ql/src/semmle/code/cpp/Class.qll

Lines changed: 38 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -977,7 +977,12 @@ class ClassTemplateInstantiation extends Class {
977977
* specialization - see `FullClassTemplateSpecialization` and
978978
* `PartialClassTemplateSpecialization`).
979979
*/
980-
abstract class ClassTemplateSpecialization extends Class {
980+
class ClassTemplateSpecialization extends Class {
981+
ClassTemplateSpecialization() {
982+
isFullClassTemplateSpecialization(this) or
983+
isPartialClassTemplateSpecialization(this)
984+
}
985+
981986
/**
982987
* Gets the primary template for the specialization, for example on
983988
* `S<T,int>`, the result is `S<T,U>`.
@@ -997,6 +1002,16 @@ abstract class ClassTemplateSpecialization extends Class {
9971002
override string getAPrimaryQlClass() { result = "ClassTemplateSpecialization" }
9981003
}
9991004

1005+
private predicate isFullClassTemplateSpecialization(Class c) {
1006+
// This class has template arguments, but none of them involves a template parameter.
1007+
exists(c.getATemplateArgument()) and
1008+
not exists(Type ta | ta = c.getATemplateArgument() and ta.involvesTemplateParameter()) and
1009+
// This class does not have any instantiations.
1010+
not exists(c.(TemplateClass).getAnInstantiation()) and
1011+
// This class is not an instantiation of a class template.
1012+
not c instanceof ClassTemplateInstantiation
1013+
}
1014+
10001015
/**
10011016
* A full specialization of a class template. For example `MyTemplateClass<int>`
10021017
* in the following code is a `FullClassTemplateSpecialization`:
@@ -1013,19 +1028,31 @@ abstract class ClassTemplateSpecialization extends Class {
10131028
* ```
10141029
*/
10151030
class FullClassTemplateSpecialization extends ClassTemplateSpecialization {
1016-
FullClassTemplateSpecialization() {
1017-
// This class has template arguments, but none of them involves a template parameter.
1018-
exists(getATemplateArgument()) and
1019-
not exists(Type ta | ta = getATemplateArgument() and ta.involvesTemplateParameter()) and
1020-
// This class does not have any instantiations.
1021-
not exists(this.(TemplateClass).getAnInstantiation()) and
1022-
// This class is not an instantiation of a class template.
1023-
not this instanceof ClassTemplateInstantiation
1024-
}
1031+
FullClassTemplateSpecialization() { isFullClassTemplateSpecialization(this) }
10251032

10261033
override string getAPrimaryQlClass() { result = "FullClassTemplateSpecialization" }
10271034
}
10281035

1036+
private predicate isPartialClassTemplateSpecialization(Class c) {
1037+
/*
1038+
* (a) At least one of this class's template arguments involves a
1039+
* template parameter in some respect, for example T, T*, etc.
1040+
*
1041+
* (b) It is not the case that the n template arguments of this class
1042+
* are a set of n distinct template parameters.
1043+
*
1044+
* template <typename T,U> class X {}; // class template
1045+
* template <typename T> class X<T,T> {}; // partial class template specialization
1046+
* template <typename T> class X<T,int> {}; // partial class template specialization
1047+
* template <typename T> class Y {}; // class template
1048+
* template <typename T> class Y<T*> {}; // partial class template specialization
1049+
*/
1050+
1051+
exists(Type ta | ta = c.getATemplateArgument() and ta.involvesTemplateParameter()) and
1052+
count(TemplateParameter tp | tp = c.getATemplateArgument()) !=
1053+
count(int i | exists(c.getTemplateArgument(i)))
1054+
}
1055+
10291056
/**
10301057
* A partial specialization of a class template. For example `MyTemplateClass<int, T>`
10311058
* in the following code is a `PartialClassTemplateSpecialization`:
@@ -1042,25 +1069,7 @@ class FullClassTemplateSpecialization extends ClassTemplateSpecialization {
10421069
* ```
10431070
*/
10441071
class PartialClassTemplateSpecialization extends ClassTemplateSpecialization {
1045-
PartialClassTemplateSpecialization() {
1046-
/*
1047-
* (a) At least one of this class's template arguments involves a
1048-
* template parameter in some respect, for example T, T*, etc.
1049-
*
1050-
* (b) It is not the case that the n template arguments of this class
1051-
* are a set of n distinct template parameters.
1052-
*
1053-
* template <typename T,U> class X {}; // class template
1054-
* template <typename T> class X<T,T> {}; // partial class template specialization
1055-
* template <typename T> class X<T,int> {}; // partial class template specialization
1056-
* template <typename T> class Y {}; // class template
1057-
* template <typename T> class Y<T*> {}; // partial class template specialization
1058-
*/
1059-
1060-
exists(Type ta | ta = getATemplateArgument() and ta.involvesTemplateParameter()) and
1061-
count(TemplateParameter tp | tp = getATemplateArgument()) !=
1062-
count(int i | exists(getTemplateArgument(i)))
1063-
}
1072+
PartialClassTemplateSpecialization() { isPartialClassTemplateSpecialization(this) }
10641073

10651074
override string getAPrimaryQlClass() { result = "PartialClassTemplateSpecialization" }
10661075
}

cpp/ql/src/semmle/code/cpp/MemberFunction.qll

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -205,12 +205,21 @@ class Constructor extends MemberFunction {
205205
/**
206206
* A function that defines an implicit conversion.
207207
*/
208-
abstract class ImplicitConversionFunction extends MemberFunction {
208+
class ImplicitConversionFunction extends MemberFunction {
209+
ImplicitConversionFunction() {
210+
// ConversionOperator
211+
functions(underlyingElement(this), _, 4)
212+
or
213+
// ConversionConstructor (deprecated)
214+
strictcount(Parameter p | p = getAParameter() and not p.hasInitializer()) = 1 and
215+
not hasSpecifier("explicit")
216+
}
217+
209218
/** Gets the type this `ImplicitConversionFunction` takes as input. */
210-
abstract Type getSourceType();
219+
Type getSourceType() { none() } // overridden in subclasses
211220

212221
/** Gets the type this `ImplicitConversionFunction` converts to. */
213-
abstract Type getDestType();
222+
Type getDestType() { none() } // overridden in subclasses
214223
}
215224

216225
/**

cpp/ql/src/semmle/code/cpp/Print.qll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ private string getTemplateArgumentString(Declaration d, int i) {
6060
/**
6161
* A `Declaration` extended to add methods for generating strings useful only for dumps and debugging.
6262
*/
63-
abstract private class DumpDeclaration extends Declaration {
63+
private class DumpDeclaration extends Declaration {
6464
DumpDeclaration() { shouldPrintDeclaration(this) }
6565

6666
/**

cpp/ql/src/semmle/code/cpp/Type.qll

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -577,7 +577,9 @@ class BoolType extends IntegralType {
577577
* unsigned char e, f;
578578
* ```
579579
*/
580-
abstract class CharType extends IntegralType { }
580+
class CharType extends IntegralType {
581+
CharType() { builtintypes(underlyingElement(this), _, [5, 6, 7], _, _, _) }
582+
}
581583

582584
/**
583585
* The C/C++ `char` type (which is distinct from `signed char` and

cpp/ql/src/semmle/code/cpp/XML.qll

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,11 @@
44

55
import semmle.files.FileSystem
66

7+
private class TXMLLocatable =
8+
@xmldtd or @xmlelement or @xmlattribute or @xmlnamespace or @xmlcomment or @xmlcharacters;
9+
710
/** An XML element that has a location. */
8-
abstract class XMLLocatable extends @xmllocatable {
11+
class XMLLocatable extends @xmllocatable, TXMLLocatable {
912
/** Gets the source location for this element. */
1013
Location getLocation() { xmllocations(this, result) }
1114

@@ -33,7 +36,7 @@ abstract class XMLLocatable extends @xmllocatable {
3336
}
3437

3538
/** Gets a textual representation of this element. */
36-
abstract string toString();
39+
string toString() { none() } // overridden in subclasses
3740
}
3841

3942
/**
@@ -51,7 +54,7 @@ class XMLParent extends @xmlparent {
5154
* Gets a printable representation of this XML parent.
5255
* (Intended to be overridden in subclasses.)
5356
*/
54-
abstract string getName();
57+
string getName() { none() } // overridden in subclasses
5558

5659
/** Gets the file to which this XML parent belongs. */
5760
XMLFile getFile() { result = this or xmlElements(this, _, _, _, result) }

0 commit comments

Comments
 (0)