FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

gh-156939: Fix memory corruption check in fcntl by vstinner · Pull Request #157103 · python/cpython · GitHub

Repository navigation

Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .c  (1) All 1 file type selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
16 changes: 10 additions & 6 deletions Modules/fcntlmodule.c
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
Expand Up @@ -121,13 +121,14 @@ fcntl_fcntl_impl(PyObject *module, int fd, int code, PyObject *arg)
return PyBytes_FromStringAndSize(buf, len);
}
else {
PyBytesWriter *writer = PyBytesWriter_Create(len);
PyBytesWriter *writer = PyBytesWriter_Create(len + GUARDSZ);
if (writer == NULL) {
PyBuffer_Release(&view);
return NULL;
}
char *ptr = PyBytesWriter_GetData(writer);
memcpy(ptr, view.buf, len);
memcpy(ptr + len, guard, GUARDSZ);
PyBuffer_Release(&view);

do {
Expand All @@ -142,7 +143,7 @@ fcntl_fcntl_impl(PyObject *module, int fd, int code, PyObject *arg)
PyBytesWriter_Discard(writer);
return NULL;
}
if (ptr[len] != '\0') {
if (memcmp(ptr + len, guard, GUARDSZ) != 0) {
PyErr_SetString(PyExc_SystemError,
"Memory corruption in fcntl() due to "
"buffer overflow. "
Expand All @@ -151,7 +152,8 @@ fcntl_fcntl_impl(PyObject *module, int fd, int code, PyObject *arg)
PyBytesWriter_Discard(writer);
return NULL;
}
return PyBytesWriter_Finish(writer);
// Truncate the trailing guard bytes
return PyBytesWriter_FinishWithSize(writer, len);
}
#undef FCNTL_BUFSZ
}
Expand Down Expand Up @@ -316,13 +318,14 @@ fcntl_ioctl_impl(PyObject *module, int fd, unsigned long code, PyObject *arg,
return PyBytes_FromStringAndSize(buf, len);
}
else {
PyBytesWriter *writer = PyBytesWriter_Create(len);
PyBytesWriter *writer = PyBytesWriter_Create(len + GUARDSZ);
if (writer == NULL) {
PyBuffer_Release(&view);
return NULL;
}
char *ptr = PyBytesWriter_GetData(writer);
memcpy(ptr, view.buf, len);
memcpy(ptr + len, guard, GUARDSZ);
PyBuffer_Release(&view);

do {
Expand All @@ -337,7 +340,7 @@ fcntl_ioctl_impl(PyObject *module, int fd, unsigned long code, PyObject *arg,
PyBytesWriter_Discard(writer);
return NULL;
}
if (ptr[len] != '\0') {
if (memcmp(ptr + len, guard, GUARDSZ) != 0) {
PyErr_SetString(PyExc_SystemError,
"Memory corruption in ioctl() due to "
"buffer overflow. "
Expand All @@ -346,7 +349,8 @@ fcntl_ioctl_impl(PyObject *module, int fd, unsigned long code, PyObject *arg,
PyBytesWriter_Discard(writer);
return NULL;
}
return PyBytesWriter_Finish(writer);
// Truncate the trailing guard bytes
return PyBytesWriter_FinishWithSize(writer, len);
}
#undef IOCTL_BUFSZ
}
Expand Down
Loading

Back | FazBrowse Home | New Git URL