generated from ossf/project-template
-
Notifications
You must be signed in to change notification settings - Fork 184
CWE-404 #970
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
CWE-404 #970
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
92 changes: 92 additions & 0 deletions
92
docs/Secure-Coding-Guide-for-Python/CWE-664/CWE-404/README.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
# CWE-404: Improper Resource Shutdown or Release | ||
|
||
Always close resources explicitly and ensure proper cleanup even if an error occurs. | ||
|
||
Improper resource shutdown or release happens when a program allocates a resource, such as a file, socket, or database connection, and fails to release it when finished. Unlike normal objects (like numbers or strings), these resources are tied to the operating system and are not freed automatically by garbage collection. If left open, they can pile up and cause memory leaks, file handle exhaustion, or stalled network connections. | ||
|
||
In Python, use the `with` statement to ensure handles are cleaned up automatically; note that `with` manages resource cleanup, not memory deallocation. Special care is required for long-running scripts, multiprocessing, or multithreading, where lingering handles can accumulate over time and exhaust system resources. | ||
|
||
## Non-Compliant Code Example | ||
|
||
In this `noncompliant01.py` code example, two elements are added to the list. Although the list continues to hold these two elements, they are never properly released, leading to retained memory that is never reclaimed. This can cause resource exhaustion or leaks. | ||
|
||
[*noncompliant01.py:*](noncompliant01.py) | ||
|
||
```py | ||
"""Non-Compliant Code Example""" | ||
|
||
my_list = [] | ||
|
||
|
||
def append_resource(name): | ||
print(f"Allocating resource {name}") | ||
resource = {"name": name, "active": True} # Simulated resource | ||
my_list.append(resource) | ||
|
||
|
||
append_resource("A") | ||
append_resource("B") | ||
|
||
# Forgot to release resources | ||
##################### | ||
# attempting to exploit above code example | ||
##################### | ||
for resource in my_list: | ||
print(resource["name"], "active?", resource["active"]) | ||
|
||
if not any(resource["active"] for resource in my_list): | ||
print("All resources released.") | ||
|
||
``` | ||
|
||
## Compliant Solution | ||
|
||
After adding two elements, to the list, the list in this `compliant01.py` code example now contains zero elements because they have been cleared and properly released. | ||
|
||
[*compliant01.py:*](compliant01.py) | ||
|
||
```py | ||
"""Compliant Code Example""" | ||
|
||
my_list = [] | ||
|
||
|
||
def append_resource(name): | ||
print(f"Allocating resource {name}") | ||
resource = {"name": name, "active": True} # Simulated resource | ||
my_list.append(resource) | ||
|
||
|
||
append_resource("A") | ||
append_resource("B") | ||
|
||
# Properly release resources | ||
for resource in my_list: | ||
resource["active"] = False | ||
my_list.clear() | ||
|
||
|
||
##################### | ||
# attempting to exploit above code example | ||
##################### | ||
for resource in my_list: | ||
print(resource["name"], "active?", resource["active"]) | ||
|
||
if not any(resource["active"] for resource in my_list): | ||
print("All resources released.") | ||
|
||
``` | ||
|
||
## Related Guidelines | ||
|
||
||| | ||
|:---|:---| | ||
|[MITRE CWE](http://cwe.mitre.org/)|Pillar [CWE-664: Improper Control of a Resource Through its Lifetime (4.13) (mitre.org)](https://cwe.mitre.org/data/definitions/664.html)| | ||
|[MITRE CWE](http://cwe.mitre.org/)|Class [CWE-404: Improper Resource Shutdown or Release (4.12)](https://cwe.mitre.org/data/definitions/404.html)| | ||
|[SEI CERT Oracle Coding Standard for Java](https://wiki.sei.cmu.edu/confluence/display/java/SEI+CERT+Oracle+Coding+Standard+for+Java)|[EXP04-J. Do not pass arguments to certain Java Collections Framework methods that are a different type than the collection parameter type](https://wiki.sei.cmu.edu/confluence/display/java/EXP04-J.+Do+not+pass+arguments+to+certain+Java+Collections+Framework+methods+that+are+a+different+type+than+the+collection+parameter+type)| | ||
|
||
## Bibliography | ||
|
||
||| | ||
|:---|:---| | ||
|\[Python Docs\]|<https://docs.python.org/3/tutorial/datastructures.html>| |
30 changes: 30 additions & 0 deletions
30
docs/Secure-Coding-Guide-for-Python/CWE-664/CWE-404/compliant01.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
# SPDX-FileCopyrightText: OpenSSF project contributors | ||
# SPDX-License-Identifier: MIT | ||
"""Compliant Code Example""" | ||
|
||
my_list = [] | ||
|
||
|
||
def append_resource(name): | ||
print(f"Allocating resource {name}") | ||
resource = {"name": name, "active": True} # Simulated resource | ||
my_list.append(resource) | ||
|
||
|
||
append_resource("A") | ||
append_resource("B") | ||
|
||
# Properly release resources | ||
for resource in my_list: | ||
resource["active"] = False | ||
my_list.clear() | ||
|
||
|
||
##################### | ||
# attempting to exploit above code example | ||
##################### | ||
for resource in my_list: | ||
print(resource["name"], "active?", resource["active"]) | ||
|
||
if not any(resource["active"] for resource in my_list): | ||
print("All resources released.") |
25 changes: 25 additions & 0 deletions
25
docs/Secure-Coding-Guide-for-Python/CWE-664/CWE-404/noncompliant01.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
# SPDX-FileCopyrightText: OpenSSF project contributors | ||
# SPDX-License-Identifier: MIT | ||
"""Non-Compliant Code Example""" | ||
|
||
my_list = [] | ||
|
||
|
||
def append_resource(name): | ||
print(f"Allocating resource {name}") | ||
resource = {"name": name, "active": True} # Simulated resource | ||
my_list.append(resource) | ||
|
||
|
||
append_resource("A") | ||
append_resource("B") | ||
|
||
# Forgot to release resources | ||
##################### | ||
# attempting to exploit above code example | ||
##################### | ||
for resource in my_list: | ||
print(resource["name"], "active?", resource["active"]) | ||
|
||
if not any(resource["active"] for resource in my_list): | ||
print("All resources released.") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.