Skip to content
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

Fix possible double close and use of unopened socket #825

Merged
merged 2 commits into from
Sep 22, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions HISTORY.rst
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ Bug tracker at https://github.com/giampaolo/psutil/issues
- #797: [Linux] net_if_stats() may raise OSError for certain NIC cards.
- #813: Process.as_dict() should ignore extraneous attribute names which gets
attached to the Process instance.
- #825: Fix possible double close and use of unopened socket
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please specify the platform ([Linux]) and the function which was affected (the high level, end-user psutil function, not the C function)



4.1.0 - 2016-03-12
Expand Down
8 changes: 4 additions & 4 deletions psutil/_psutil_linux.c
Original file line number Diff line number Diff line change
Expand Up @@ -404,7 +404,7 @@ psutil_proc_cpu_affinity_set(PyObject *self, PyObject *args) {
#else
long value = PyInt_AsLong(item);
#endif
if (value == -1 && PyErr_Occurred())
if (value == -1 || PyErr_Occurred())
goto error;
CPU_SET(value, &cpu_set);
}
Expand Down Expand Up @@ -538,17 +538,17 @@ psutil_net_if_stats(PyObject* self, PyObject* args) {
}
}

close(sock);
py_retlist = Py_BuildValue("[Oiii]", py_is_up, duplex, speed, mtu);
if (!py_retlist)
goto error;
close(sock);
Py_DECREF(py_is_up);
return py_retlist;

error:
Py_XDECREF(py_is_up);
if (sock != 0)
if (sock != -1)
close(sock);
Py_XDECREF(py_is_up);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I kinda don't like having two separate goto handlers. I think close()sing the socket twice will not be a problem,

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, once a socket is closed - the file descriptor is available for use elsewhere. And thus a double close could end up closing that - which may cause a problem elsewhere. If you don't like multiple goto handlers, you might prefer moving the close(sock) on line 541 down to 545 before the return. That way there is a close in every path in the function - and no double close.

Copy link
Owner

@giampaolo giampaolo Jun 2, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like that. Can you update the PR also including changes for the HISTORY.rst file?

PyErr_SetFromErrno(PyExc_OSError);
return NULL;
}
Expand Down
2 changes: 1 addition & 1 deletion psutil/arch/bsd/freebsd.c
Original file line number Diff line number Diff line change
Expand Up @@ -971,7 +971,7 @@ psutil_proc_cpu_affinity_set(PyObject *self, PyObject *args) {
#else
long value = PyInt_AsLong(item);
#endif
if (value == -1 && PyErr_Occurred())
if (value == -1 || PyErr_Occurred())
goto error;
CPU_SET(value, &cpu_set);
}
Expand Down