-
-
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
Invalid management of reference counting in open_files (OSX) #1127
Comments
Mmm I see what you mean. To clarify, the problem occurs in such a circumstance:
Correct? This is interesting... and worrying, because I believe there are other similar constructs elsewhere in the code. In this specific case it seems setting
No, we want it to propagate. Otherwise it would just return an incorrect result (an empty list). |
You got it right. I've already managed to find a couple of other errors related to reference counting in this file (but it's possible that they are harmless because they are never reached) e.g.: Lines 144 to 149 in c4c6e22
You should not assign
Regarding the general fix, I also thought about setting pointer to NULL after being DECREFed. It's not required in most cases but it will easily prevent us from such errors (assuming that Py_XDECREF is used in error handling section). Additionally, it's not a very uncommon style in C programming (setting pointer to NULL once it's freed). Such defensive programming makes debugging dangling pointer bugs much easier - accessing NULL pointer will crash immediately on most platforms instead of reading/overwriting some random memory and leaving your app in undefined state.
That's not entirely true. You can get EPERM error just for a part of file descriptors so the result can still have some entries. That's why I suggested omitting this error instead of raising AccessDenied exception. Example from my machine:
What do you think? |
Sorry for being late. As for the permission error, I still think PermissionError should propagate. lsof has a way to inform the user that some fds are there but cannot be accessed but psutil doesn't. It will just return an incomplete result, the user will assume that it's all there when it's not and that is bad. As for the reference counting issue, where are we at with it? Can you provide a PR? It looks like a high priority bug. |
I created a PR with appropriate changes to open_files function: #1144 I can fix the rest of _psutil_osx.c file later. However, if nobody reported any issues so far, it probably means they are not very likely to be reached. I just don't want to block this one as it's high priority bug due to Mac OS High Sierra release. |
Did this issue cause segfaults? |
Yes. Invalid handling of reference counting (double DECREF) causes memory corruption and segfault in the end. |
Fixed in #1144. |
While iterating over file descriptors of the given process, it's possible to get
EPERM
error while callingproc_pidfdinfo
:psutil/psutil/_psutil_osx.c
Lines 1137 to 1141 in c4c6e22
The errors checking part will raise exception and goto
error
label:psutil/psutil/_psutil_osx.c
Lines 1151 to 1152 in c4c6e22
If such situation happens after the first iteration of the loop (so
py_path
variable is not NULL), it will causepy_path
reference count to be decremented even though it shouldn't be:psutil/psutil/_psutil_osx.c
Line 1180 in c4c6e22
I was able to discover that issue while testing psutil on Mac OS High Sierra beta release. Issue is consistently reproducible (at least on my machine) when I try to iterate over all processes and list open files for them. Example problematic process is called
lsd
andEPERM
error is also visible while using command-linelsof
utility:Note that this is not an issue for previous Mac OS/OSX releases and probably that is why this issue wasn't found earlier.
Suggested actions:
psutil_proc_open_files
(so it won't corrupt memory in case of unexpected errors)_psutil_osx.c
The text was updated successfully, but these errors were encountered: