mirror of
https://github.com/python/cpython.git
synced 2025-11-02 03:01:58 +00:00
bpo-33871: Fix os.sendfile(), os.writev(), os.readv(), etc. (GH-7931)
* Fix integer overflow in os.readv(), os.writev(), os.preadv() and os.pwritev() and in os.sendfile() with headers or trailers arguments (on BSD-based OSes and MacOS). * Fix sending the part of the file in os.sendfile() on MacOS. Using the trailers argument could cause sending more bytes from the input file than was specified. Thanks Ned Deily for testing on 32-bit MacOS.
This commit is contained in:
parent
f1d36d8efa
commit
9d5727326a
5 changed files with 114 additions and 32 deletions
|
|
@ -8321,12 +8321,13 @@ os_read_impl(PyObject *module, int fd, Py_ssize_t length)
|
|||
}
|
||||
|
||||
#if (defined(HAVE_SENDFILE) && (defined(__FreeBSD__) || defined(__DragonFly__) \
|
||||
|| defined(__APPLE__))) || defined(HAVE_READV) || defined(HAVE_WRITEV)
|
||||
static Py_ssize_t
|
||||
|| defined(__APPLE__))) \
|
||||
|| defined(HAVE_READV) || defined(HAVE_PREADV) || defined (HAVE_PREADV2) \
|
||||
|| defined(HAVE_WRITEV) || defined(HAVE_PWRITEV) || defined (HAVE_PWRITEV2)
|
||||
static int
|
||||
iov_setup(struct iovec **iov, Py_buffer **buf, PyObject *seq, Py_ssize_t cnt, int type)
|
||||
{
|
||||
Py_ssize_t i, j;
|
||||
Py_ssize_t blen, total = 0;
|
||||
|
||||
*iov = PyMem_New(struct iovec, cnt);
|
||||
if (*iov == NULL) {
|
||||
|
|
@ -8351,11 +8352,9 @@ iov_setup(struct iovec **iov, Py_buffer **buf, PyObject *seq, Py_ssize_t cnt, in
|
|||
}
|
||||
Py_DECREF(item);
|
||||
(*iov)[i].iov_base = (*buf)[i].buf;
|
||||
blen = (*buf)[i].len;
|
||||
(*iov)[i].iov_len = blen;
|
||||
total += blen;
|
||||
(*iov)[i].iov_len = (*buf)[i].len;
|
||||
}
|
||||
return total;
|
||||
return 0;
|
||||
|
||||
fail:
|
||||
PyMem_Del(*iov);
|
||||
|
|
@ -8652,12 +8651,20 @@ posix_sendfile(PyObject *self, PyObject *args, PyObject *kwdict)
|
|||
}
|
||||
if (i > 0) {
|
||||
sf.hdr_cnt = (int)i;
|
||||
i = iov_setup(&(sf.headers), &hbuf,
|
||||
headers, sf.hdr_cnt, PyBUF_SIMPLE);
|
||||
if (i < 0)
|
||||
if (iov_setup(&(sf.headers), &hbuf,
|
||||
headers, sf.hdr_cnt, PyBUF_SIMPLE) < 0)
|
||||
return NULL;
|
||||
#ifdef __APPLE__
|
||||
sbytes += i;
|
||||
for (i = 0; i < sf.hdr_cnt; i++) {
|
||||
Py_ssize_t blen = sf.headers[i].iov_len;
|
||||
# define OFF_T_MAX 0x7fffffffffffffff
|
||||
if (sbytes >= OFF_T_MAX - blen) {
|
||||
PyErr_SetString(PyExc_OverflowError,
|
||||
"sendfile() header is too large");
|
||||
return NULL;
|
||||
}
|
||||
sbytes += blen;
|
||||
}
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
|
@ -8678,13 +8685,9 @@ posix_sendfile(PyObject *self, PyObject *args, PyObject *kwdict)
|
|||
}
|
||||
if (i > 0) {
|
||||
sf.trl_cnt = (int)i;
|
||||
i = iov_setup(&(sf.trailers), &tbuf,
|
||||
trailers, sf.trl_cnt, PyBUF_SIMPLE);
|
||||
if (i < 0)
|
||||
if (iov_setup(&(sf.trailers), &tbuf,
|
||||
trailers, sf.trl_cnt, PyBUF_SIMPLE) < 0)
|
||||
return NULL;
|
||||
#ifdef __APPLE__
|
||||
sbytes += i;
|
||||
#endif
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue