Skip to content

Commit

Permalink
pyverbs: Protect the write and read methods of MR
Browse files Browse the repository at this point in the history
For the implicit MR registrations the internal MR's buffer
doesn't have to be allocated (controlled by the length argument).
Therefore we should protect the write and read methods of MR object
by checking before the operation if the buf attribute and the
passed arguments are valid.

Signed-off-by: Itay Aveksis <itayav@mellanox.com>
Signed-off-by: Edward Srouji <edwards@mellanox.com>
  • Loading branch information
itayave authored and EdwardSro committed Jul 19, 2020
1 parent af4668b commit 91833c6
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion pyverbs/mr.pyx
Expand Up @@ -6,7 +6,8 @@ import logging

from posix.mman cimport mmap, munmap, MAP_PRIVATE, PROT_READ, PROT_WRITE, \
MAP_ANONYMOUS, MAP_HUGETLB
from pyverbs.pyverbs_error import PyverbsError, PyverbsRDMAError
from pyverbs.pyverbs_error import PyverbsError, PyverbsRDMAError, \
PyverbsUserError
from libc.stdint cimport uintptr_t, SIZE_MAX
from pyverbs.base import PyverbsRDMAErrno
from posix.stdlib cimport posix_memalign
Expand Down Expand Up @@ -135,6 +136,9 @@ cdef class MR(PyverbsCM):
:param length: Length of the data to write
:return: None
"""
if not self.buf or length < 0:
raise PyverbsUserError('The MR buffer isn\'t allocated or length'
f' {length} is invalid')
# If data is a string, cast it to bytes as Python3 doesn't
# automatically convert it.
if isinstance(data, str):
Expand All @@ -151,6 +155,11 @@ cdef class MR(PyverbsCM):
cdef char *data
cdef int off = offset # we can't use offset in the next line, as it is
# a Python object and not C
if offset < 0:
raise PyverbsUserError(f'Invalid offset {offset}')
if not self.buf or length < 0:
raise PyverbsUserError('The MR buffer isn\'t allocated or length'
f' {length} is invalid')
data = <char*>(self.buf + off)
return data[:length]

Expand Down

0 comments on commit 91833c6

Please sign in to comment.