-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
[NetBSD] cmdline() can raise MemoryError #1526
Merged
Merged
Conversation
This file contains 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
Do not overallocate the buffer prompting for KERN_ARGMAX. It also fixes the code as KERN_ARGMAX was received into a size_t type, while kernel returned int. It caused argmax to contain garbage and allocation was randomly crashing with new jemalloc in the basesystem. New code prompts for exact buffer size before allocation and stores argv[] inside it. Bug investigated by Leonardo Taccari.
This header was removed from the OS as it was empty.
What was the problem with cmdline? It got truncated? |
malloc(3) returned NULL for buffer of size with garbage |
Hello giampaolo,
giampaolo writes:
What was the problem with cmdline? It got truncated?
At least calling psutil.Process(pid).as_dict() lead to MemoryError,
e.g.:
```
| % python3.7
| Python 3.7.3 (default, May 3 2019, 17:54:14)
| [GCC 7.4.0] on netbsd8
| Type "help", "copyright", "credits" or "license" for more information.
| >>> import psutil
| >>> psutil.Process(1).as_dict()
| Traceback (most recent call last):
| File "<stdin>", line 1, in <module>
| File "/usr/pkg/lib/python3.7/site-packages/psutil/__init__.py", line 638, in as_dict
| ret = meth()
| File "/usr/pkg/lib/python3.7/site-packages/psutil/__init__.py", line 790, in cmdline
| return self._proc.cmdline()
| File "/usr/pkg/lib/python3.7/site-packages/psutil/_psbsd.py", line 562, in wrapper
| return fun(self, *args, **kwargs)
| File "/usr/pkg/lib/python3.7/site-packages/psutil/_psbsd.py", line 666, in cmdline
| return cext.proc_cmdline(self.pid)
| MemoryError
```
That was because in psutil_get_cmd_args() the kern.argmax sysctl(3)
```
char *
psutil_get_cmd_args(pid_t pid, size_t *argsize) {
int mib[4];
ssize_t st;
size_t argmax;
size_t size;
char *procargs = NULL;
mib[0] = CTL_KERN;
mib[1] = KERN_ARGMAX;
size = sizeof(argmax);
st = sysctl(mib, 2, &argmax, &size, NULL, 0);
if (st == -1) {
PyErr_SetFromErrno(PyExc_OSError);
return NULL;
procargs = (char *)malloc(argmax);
if (procargs == NULL) {
PyErr_NoMemory();
return NULL;
[...]
```
Before calling sysctl() `size' was 8, after calling it `size' was 4.
However, later when calling `malloc(argmax)' `argmax' contained
uninitialized data and thus failing.
|
I plan to submit implementation of missing features for NetBSD, but I will do it with 1 PR after another. |
Thanks, merged.
Sounds sweet. I am looking forward to that. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
sys/user.h
for NetBSDpsutil_get_cmd_args