-
-
Notifications
You must be signed in to change notification settings - Fork 637
dragonflybsd: Fix NULL dereference in jail list parsing #2058
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
Open
GermanAizek
wants to merge
1
commit into
htop-dev:main
Choose a base branch
from
GermanAizek:strtok-null-not-check
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+10
−3
Open
Changes from all commits
Commits
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
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
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🩺 Stability & Availability | 🟡 Minor | ⚡ Quick win
🧩 Analysis chain
🏁 Script executed:
Repository: htop-dev/htop
Length of output: 1742
🏁 Script executed:
Repository: htop-dev/htop
Length of output: 242
🏁 Script executed:
Repository: htop-dev/htop
Length of output: 364
🌐 Web query:
POSIX atoi return value out of range behavior strtol ERANGE undefined behavior💡 Result:
Under POSIX and ISO C standards, the behavior of atoi when the input string represents a value outside the range of representable values for an int is undefined [1][2][3]. While atoi is technically defined as equivalent to (int) strtol(str, (char)NULL, 10), it explicitly lacks the error-checking mechanisms provided by strtol [1][2][4]. Specifically: 1. Undefined Behavior: If the converted value cannot be represented in an int, the C and POSIX standards classify the result as undefined behavior [1][3]. 2. Lack of Error Reporting: Unlike strtol, atoi does not provide a reliable way to detect overflows or underflows [1][5]. It does not set errno [6][5], and there is no return value that consistently distinguishes a valid result from an error [6][5]. 3. Interaction with strtol: Although implementations often internally use strtol [1][7], the resulting behavior for the user is still undefined [3]. Because strtol returns LONG_MAX or LONG_MIN and sets errno to ERANGE upon overflow/underflow [8][9][10], casting these values to an int can lead to implementation-defined results, but the standard remains strict in categorizing the overall operation as undefined behavior [1][3]. Because of these limitations, the use of atoi is discouraged in new code [1][2][5]. The strtol family of functions is the recommended alternative, as it allows for robust error checking by: - Setting errno to 0 before the call [8][10]. - Checking for ERANGE after the call to detect if the value was out of range [8][10]. - Using the endptr argument to verify that the entire string was consumed [10][11].
Citations:
🌐 Web query:
DragonFly BSD sysctl jail.list output format jailid hostname jail.list documentation💡 Result:
In DragonFly BSD, the
jail.listsysctl is a key mechanism used to retrieve information about currently running jails [1][2]. Unlike FreeBSD, which provides specific jail-related APIs, DragonFly BSD exposes this information through thejail.listsysctl MIB [1]. Format of jail.list output: Thejail.listsysctl returns a list of active jails where each entry is typically separated by a newline character [1]. Each line of the output generally follows this space-separated format [1][2]: [JailID] [Hostname] [Path] [IPs] Example Output: When queryingjail.listviasysctl, the output appears as follows [2]: # sysctl jail.list jail.list: 2 t02.local /jails/02 10.0.0.3 1 t01.local /jails/01 10.0.0.2 Technical details: 1. Retrieval: You can retrieve this list programmatically using thesysctlbyname("jail.list", ...)function [1]. Because the output size can vary based on the number of active jails, it is common practice to call the function once to determine the required buffer size, and then again to fetch the actual data [1]. 2. Parsing: Since the output includes newline characters to separate multiple jails and spaces to separate the fields within a line, it can be parsed using standard string manipulation functions such asstrchr()for line breaks andstrtok()for space-delimited fields [1]. 3. Context: This sysctl is distinct from the per-jail configuration sysctls found underjail.<id>.<setting>, which allow for individual management of jail environments [2]. For general administrative tasks, users typically use thejls(8)command, which presents this information in a more readable format [3].Citations:
Replace
atoiwith checkedstrtolconversion forstr_jailid.atoican invoke undefined behavior when the jail ID does not fit inint, and it does not report malformed input. Reject entries where the whole token is not a decimal integer in theintrange before accessingthis->jails. Add<errno.h>and<limits.h>if needed.🧰 Tools
🪛 ast-grep (0.45.0)
[warning] 339-339: Avoid
atoi/atol/atoll/atof: they perform no error detection, returning 0 on non-numeric input and invoking undefined behavior on out-of-range values. This rule flags every use of these functions regardless of data provenance. Convert strings withstrtol/strtoul/strtodand checkerrno(and theendptr) so malformed or overflowing input is rejected.Context: atoi(str_jailid)
Note: [CWE-20] Improper Input Validation.
(atoi-no-error-detection-c)
Source: Linters/SAST tools