Home
About
Blog
Products
Forum
Support
Contact
Sunbelt Computer Software
PL/B Language Development and Support
Home
About
Blog
Products
Forum
Support
Contact
codeql/python/ql/src/Resources/FileNotAlwaysClosedQuery.qll at main · github/codeql · GitHub
Skip to content
Navigation Menu
Sign in
Appearance settings
Platform
AI CODE CREATION
GitHub Copilot
Write better code with AI
GitHub Copilot app
Direct agents from issue to merge
MCP Registry
Integrate external tools
DEVELOPER WORKFLOWS
Actions
Automate any workflow
Codespaces
Instant dev environments
Issues
Plan and track work
Code Review
Manage code changes
Code Quality
Enforce quality at merge
APPLICATION SECURITY
GitHub Advanced Security
Find and fix vulnerabilities
Code security
Secure your code as you build
Secret protection
Stop leaks before they start
EXPLORE
Why GitHub
Documentation
Blog
Changelog
Marketplace
View all features
Solutions
BY COMPANY SIZE
Enterprises
Small and medium teams
Startups
Nonprofits
BY USE CASE
App Modernization
DevSecOps
DevOps
CI/CD
View all use cases
BY INDUSTRY
Healthcare
Financial services
Manufacturing
Government
See all industries
View all solutions
Resources
EXPLORE BY TOPIC
AI
Software Development
DevOps
Security
View all topics
EXPLORE BY TYPE
Customer stories
Events & webinars
Ebooks & reports
Business insights
GitHub Skills
SUPPORT & SERVICES
Documentation
Customer support
Community forum
Trust center
Partners
View all resources
Open Source
COMMUNITY
GitHub Sponsors
Fund open source developers
PROGRAMS
Security Lab
Maintainer Community
GitHub Stars
Archive Program
REPOSITORIES
Topics
Trending
Collections
Enterprise
ENTERPRISE SOLUTIONS
Enterprise platform
AI-powered developer platform
AVAILABLE ADD-ONS
GitHub Advanced Security
Enterprise-grade security features
Copilot for Business
Enterprise-grade AI features
Premium Support
Enterprise-grade 24/7 support
Pricing
Search
/
Sign in
Sign up
Appearance settings
You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.
You switched accounts on another tab or window.
Reload
to refresh your session.
Dismiss alert
{{ message }}
github
codeql
Repository navigation
Code
Issues
1k
(1k)
Pull requests
482
(482)
Discussions
Actions
Projects
Security and quality
Insights
More
items
Files
Expand file tree
main
Breadcrumbs
codeql
/
python
/
ql
/
src
/
Resources
/
FileNotAlwaysClosedQuery.qll
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
188 lines (159 loc) · 6.67 KB
main
Breadcrumbs
codeql
/
python
/
ql
/
src
/
Resources
/
FileNotAlwaysClosedQuery.qll
Copy path
Top
File metadata and controls
Code
Blame
188 lines (159 loc) · 6.67 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
/** Definitions for reasoning about whether files are closed. */
import
python
import
semmle.python.dataflow.new.internal.DataFlowDispatch
import
semmle.python.ApiGraphs
private
import
semmle.python.dataflow.new.internal.ReExposedInstance
/** A CFG node where a file is opened. */
abstract
class
FileOpenSource
extends
DataFlow
::
CfgNode
{
}
/** A call to the builtin `open` or `os.open`. */
class
FileOpenCall
extends
FileOpenSource
{
FileOpenCall
(
)
{
this
=
[
API
::
builtin
(
"open"
)
.
getACall
(
)
,
API
::
moduleImport
(
"os"
)
.
getMember
(
"open"
)
.
getACall
(
)
]
}
}
private
DataFlow
::
TypeTrackingNode
fileOpenInstance
(
DataFlow
::
TypeTracker
t
)
{
t
.
start
(
)
and
result
instanceof
FileOpenSource
or
exists
(
DataFlow
::
TypeTracker
t2
|
result
=
fileOpenInstance
(
t2
)
.
track
(
t2
,
t
)
)
}
/**
* Holds if `node` is tracked to be an instance of an open file object.
*/
private
predicate
fileInstanceNode
(
DataFlow
::
Node
node
)
{
fileOpenInstance
(
DataFlow
::
TypeTracker
::
end
(
)
)
.
flowsTo
(
node
)
}
private
module
FileReExposed
=
ReExposedInstance
<
fileInstanceNode
/
1
>
;
/**
* A call that returns an instance of an open file object.
* This includes calls to methods that transitively call `open` or similar.
*/
class
FileOpen
extends
DataFlow
::
CallCfgNode
{
FileOpen
(
)
{
fileOpenInstance
(
DataFlow
::
TypeTracker
::
end
(
)
)
.
flowsTo
(
this
)
and
// Don't treat an accessor that merely re-exposes a file held in an instance attribute
// (e.g. `FileIO.fileno` returning `self._fd`) as opening a new file. Such flow is
// introduced by instance-attribute type tracking; the underlying open is tracked at its
// real creation site.
not
FileReExposed
::
isReExposed
(
this
)
}
}
/** A call that may wrap a file object in a wrapper class or `os.fdopen`. */
class
FileWrapperCall
extends
DataFlow
::
CallCfgNode
{
DataFlow
::
Node
wrapped
;
FileWrapperCall
(
)
{
// Approximation: Treat any passing of a file object to a class constructor as potentially a wrapper
// This could be made more precise by checking that the constructor writes the file to a field.
wrapped
=
this
.
getArg
(
_
)
.
getALocalSource
(
)
and
this
.
getFunction
(
)
=
classTracker
(
_
)
or
wrapped
=
this
.
getArg
(
0
)
and
this
=
API
::
moduleImport
(
"os"
)
.
getMember
(
"fdopen"
)
.
getACall
(
)
or
wrapped
=
this
.
getArg
(
0
)
and
this
=
API
::
moduleImport
(
"django"
)
.
getMember
(
"http"
)
.
getMember
(
"FileResponse"
)
.
getACall
(
)
}
/** Gets the file that this call wraps. */
DataFlow
::
Node
getWrapped
(
)
{
result
=
wrapped
}
}
/** A node where a file is closed. */
abstract
class
FileClose
extends
DataFlow
::
CfgNode
{
/** Holds if this file close will occur if an exception is raised at `fileRaises`. */
predicate
guardsExceptions
(
DataFlow
::
CfgNode
fileRaises
)
{
// The close call occurs after an exception edge in the cfg (a catch or finally)
bbReachableRefl
(
fileRaises
.
asCfgNode
(
)
.
getBasicBlock
(
)
.
getAnExceptionalSuccessor
(
)
,
this
.
asCfgNode
(
)
.
getBasicBlock
(
)
)
or
// The exception is after the close call.
// A full cfg reachability check is not feasible for performance, instead we use local dataflow
fileLocalFlow
(
this
,
fileRaises
)
}
}
private
predicate
bbSuccessor
(
BasicBlock
src
,
BasicBlock
sink
)
{
sink
=
src
.
getASuccessor
(
)
}
private
predicate
bbReachableStrict
(
BasicBlock
src
,
BasicBlock
sink
)
=
fastTC
(
bbSuccessor
/
2
)
(
src
,
sink
)
private
predicate
bbReachableRefl
(
BasicBlock
src
,
BasicBlock
sink
)
{
bbReachableStrict
(
src
,
sink
)
or
src
=
sink
}
/** A call to the `.close()` method of a file object. */
class
FileCloseCall
extends
FileClose
{
FileCloseCall
(
)
{
exists
(
DataFlow
::
MethodCallNode
mc
|
mc
.
calls
(
this
,
"close"
)
)
}
}
/** A call to `os.close`. */
class
OsCloseCall
extends
FileClose
{
OsCloseCall
(
)
{
this
=
API
::
moduleImport
(
"os"
)
.
getMember
(
"close"
)
.
getACall
(
)
.
getArg
(
0
)
}
}
/** A `with` statement. */
class
WithStatement
extends
FileClose
{
With
w
;
WithStatement
(
)
{
this
.
asExpr
(
)
=
w
.
getContextExpr
(
)
}
override
predicate
guardsExceptions
(
DataFlow
::
CfgNode
fileRaises
)
{
super
.
guardsExceptions
(
fileRaises
)
or
w
.
getBody
(
)
.
contains
(
fileRaises
.
asExpr
(
)
)
}
}
/** Holds if an exception may be raised at `raises` if `file` is a file object. */
private
predicate
mayRaiseWithFile
(
DataFlow
::
CfgNode
file
,
DataFlow
::
CfgNode
raises
)
{
// Currently just consider any method called on `file`; e.g. `file.write()`; as potentially raising an exception
raises
.
(
DataFlow
::
MethodCallNode
)
.
getObject
(
)
=
file
and
not
file
instanceof
FileOpen
and
not
file
instanceof
FileClose
}
/** Holds if data flows from `nodeFrom` to `nodeTo` in one step that also includes file wrapper classes. */
private
predicate
fileAdditionalLocalFlowStep
(
DataFlow
::
Node
nodeFrom
,
DataFlow
::
Node
nodeTo
)
{
exists
(
FileWrapperCall
fw
|
nodeFrom
=
fw
.
getWrapped
(
)
and
nodeTo
=
fw
)
}
private
predicate
fileLocalFlowHelper0
(
DataFlow
::
LocalSourceNode
nodeFrom
,
DataFlow
::
LocalSourceNode
nodeTo
)
{
exists
(
DataFlow
::
Node
nodeMid
|
nodeFrom
.
flowsTo
(
nodeMid
)
and
fileAdditionalLocalFlowStep
(
nodeMid
,
nodeTo
)
)
}
private
predicate
fileLocalFlowHelper1
(
DataFlow
::
LocalSourceNode
nodeFrom
,
DataFlow
::
LocalSourceNode
nodeTo
)
{
fileLocalFlowHelper0
*
(
nodeFrom
,
nodeTo
)
}
/** Holds if data flows from `source` to `sink`, including file wrapper classes. */
pragma
[
inline
]
private
predicate
fileLocalFlow
(
DataFlow
::
Node
source
,
DataFlow
::
Node
sink
)
{
exists
(
DataFlow
::
LocalSourceNode
mid
|
fileLocalFlowHelper1
(
source
,
mid
)
and
mid
.
flowsTo
(
sink
)
)
}
/** Holds if the file opened at `fo` is closed. */
predicate
fileIsClosed
(
FileOpen
fo
)
{
exists
(
FileClose
fc
|
fileLocalFlow
(
fo
,
fc
)
)
}
/** Holds if the file opened at `fo` is returned to the caller. This makes the caller responsible for closing the file. */
predicate
fileIsReturned
(
FileOpen
fo
)
{
exists
(
Return
ret
,
Expr
retVal
|
(
retVal
=
ret
.
getValue
(
)
or
retVal
=
ret
.
getValue
(
)
.
(
List
)
.
getAnElt
(
)
or
retVal
=
ret
.
getValue
(
)
.
(
Tuple
)
.
getAnElt
(
)
)
and
fileLocalFlow
(
fo
,
DataFlow
::
exprNode
(
retVal
)
)
)
}
/** Holds if the file opened at `fo` is stored in a field. We assume that another method is then responsible for closing the file. */
predicate
fileIsStoredInField
(
FileOpen
fo
)
{
exists
(
DataFlow
::
AttrWrite
aw
|
fileLocalFlow
(
fo
,
aw
.
getValue
(
)
)
)
}
/** Holds if the file opened at `fo` is not closed, and is expected to be closed. */
predicate
fileNotClosed
(
FileOpen
fo
)
{
not
fileIsClosed
(
fo
)
and
not
fileIsReturned
(
fo
)
and
not
fileIsStoredInField
(
fo
)
}
predicate
fileMayNotBeClosedOnException
(
FileOpen
fo
,
DataFlow
::
Node
raises
)
{
fileIsClosed
(
fo
)
and
exists
(
DataFlow
::
CfgNode
fileRaised
|
mayRaiseWithFile
(
fileRaised
,
raises
)
and
fileLocalFlow
(
fo
,
fileRaised
)
and
not
exists
(
FileClose
fc
|
fileLocalFlow
(
fo
,
fc
)
and
fc
.
guardsExceptions
(
fileRaised
)
)
)
}
You can’t perform that action at this time.