1
1
/**
2
2
* @name LinuxPrivilegeDroppingOutoforder
3
3
* @description A syscall commonly associated with privilege dropping is being called out of order.
4
- Normally a process drops group ID and sets supplimental groups for the target user
5
- before setting the target user ID. This can have security impact if the return code
6
- from these methods is not checked.
4
+ * Normally a process drops group ID and sets supplimental groups for the target user
5
+ * before setting the target user ID. This can have security impact if the return code
6
+ * from these methods is not checked.
7
7
* @kind problem
8
8
* @problem.severity recommendation
9
9
* @id cpp/drop-linux-privileges-outoforder
@@ -16,7 +16,7 @@ import cpp
16
16
17
17
predicate argumentMayBeRoot ( Expr e ) {
18
18
e .getValue ( ) = "0" or
19
- e .( VariableAccess ) .getTarget ( ) .getName ( ) .matches ( "%oot %" )
19
+ e .( VariableAccess ) .getTarget ( ) .getName ( ) .toLowerCase ( ) . matches ( "%root %" )
20
20
}
21
21
22
22
class SetuidLikeFunctionCall extends FunctionCall {
@@ -31,16 +31,15 @@ class SetuidLikeWrapperCall extends FunctionCall {
31
31
SetuidLikeFunctionCall baseCall ;
32
32
33
33
SetuidLikeWrapperCall ( ) {
34
- this = baseCall or
34
+ this = baseCall
35
+ or
35
36
exists ( SetuidLikeWrapperCall fc |
36
37
this .getTarget ( ) = fc .getEnclosingFunction ( ) and
37
38
baseCall = fc .getBaseCall ( )
38
39
)
39
40
}
40
41
41
- SetuidLikeFunctionCall getBaseCall ( ) {
42
- result = baseCall
43
- }
42
+ SetuidLikeFunctionCall getBaseCall ( ) { result = baseCall }
44
43
}
45
44
46
45
class CallBeforeSetuidFunctionCall extends FunctionCall {
@@ -62,43 +61,41 @@ class CallBeforeSetuidWrapperCall extends FunctionCall {
62
61
CallBeforeSetuidFunctionCall baseCall ;
63
62
64
63
CallBeforeSetuidWrapperCall ( ) {
65
- this = baseCall or
64
+ this = baseCall
65
+ or
66
66
exists ( CallBeforeSetuidWrapperCall fc |
67
67
this .getTarget ( ) = fc .getEnclosingFunction ( ) and
68
68
baseCall = fc .getBaseCall ( )
69
69
)
70
70
}
71
71
72
- CallBeforeSetuidFunctionCall getBaseCall ( ) {
73
- result = baseCall
74
- }
72
+ CallBeforeSetuidFunctionCall getBaseCall ( ) { result = baseCall }
75
73
}
76
74
77
75
predicate setuidBeforeSetgid (
78
- SetuidLikeWrapperCall setuidWrapper ,
79
- CallBeforeSetuidWrapperCall setgidWrapper ) {
76
+ SetuidLikeWrapperCall setuidWrapper , CallBeforeSetuidWrapperCall setgidWrapper
77
+ ) {
80
78
setgidWrapper .getAPredecessor + ( ) = setuidWrapper
81
79
}
82
80
83
81
predicate isAccessed ( FunctionCall fc ) {
84
- exists ( Variable v | v .getAnAssignedValue ( ) = fc ) or
85
- exists ( Operation c | fc = c .getAChild ( ) | c .isCondition ( ) ) or
82
+ exists ( Variable v | v .getAnAssignedValue ( ) = fc )
83
+ or
84
+ exists ( Operation c | fc = c .getAChild ( ) | c .isCondition ( ) )
85
+ or
86
86
// ignore pattern where result is intentionally ignored by a cast to void.
87
87
fc .hasExplicitConversion ( )
88
88
}
89
89
90
- from
91
- Function func ,
92
- CallBeforeSetuidFunctionCall fc ,
93
- SetuidLikeFunctionCall setuid
90
+ from Function func , CallBeforeSetuidFunctionCall fc , SetuidLikeFunctionCall setuid
94
91
where
95
92
setuidBeforeSetgid ( setuid , fc ) and
96
93
// Require the call return code to be used in a condition or assigned.
97
94
// This introduces false negatives where the return is checked but then
98
95
// errno == EPERM allows execution to continue.
99
96
not isAccessed ( fc ) and
100
97
func = fc .getEnclosingFunction ( )
101
- select fc , "This function is called within " + func + ", and potentially after " +
102
- "$@, and may not succeed. Be sure to check the return code and errno, otherwise permissions " +
103
- " may not be dropped." ,
104
- setuid , setuid .getTarget ( ) .getName ( )
98
+ select fc ,
99
+ "This function is called within " + func + ", and potentially after " +
100
+ "$@, and may not succeed. Be sure to check the return code and errno, otherwise permissions " +
101
+ "may not be dropped." , setuid , setuid .getTarget ( ) .getName ( )
0 commit comments