From f134ad6fb4a8064aa3b942904671b1014fae9ef3 Mon Sep 17 00:00:00 2001 From: JP Smith Date: Tue, 11 Jul 2017 16:02:32 -0400 Subject: [PATCH] Check for negative fds in fstat (#384) * check for negative fds in fstat * use _get_fd --- manticore/platforms/linux.py | 27 +++++++++++++++++++++------ 1 file changed, 21 insertions(+), 6 deletions(-) diff --git a/manticore/platforms/linux.py b/manticore/platforms/linux.py index ff88d35..0297bdb 100644 --- a/manticore/platforms/linux.py +++ b/manticore/platforms/linux.py @@ -1822,9 +1822,14 @@ class Linux(Platform): :rtype: int :param fd: the file descriptor of the file that is being inquired. :param buf: a buffer where data about the file will be stored. - :return: C{0} on success. + :return: C{0} on success, EBADF when called with bad fd ''' - stat = self.files[fd].stat() + + try: + stat = self._get_fd(fd).stat() + except BadFd: + logger.info("Calling fstat with invalid fd, returning EBADF") + return -errno.EBADF def add(width, val): fformat = {2:'H', 4:'L', 8:'Q'}[width] @@ -1864,9 +1869,14 @@ class Linux(Platform): :rtype: int :param fd: the file descriptor of the file that is being inquired. :param buf: a buffer where data about the file will be stored. - :return: C{0} on success. + :return: C{0} on success, EBADF when called with bad fd ''' - stat = self.files[fd].stat() + + try: + stat = self._get_fd(fd).stat() + except BadFd: + logger.info("Calling fstat with invalid fd, returning EBADF") + return -errno.EBADF def add(width, val): fformat = {2:'H', 4:'L', 8:'Q'}[width] @@ -1903,10 +1913,15 @@ class Linux(Platform): :rtype: int :param fd: the file descriptor of the file that is being inquired. :param buf: a buffer where data about the file will be stored. - :return: C{0} on success. + :return: C{0} on success, EBADF when called with bad fd :todo: Fix device number. ''' - stat = self.files[fd].stat() + + try: + stat = self._get_fd(fd).stat() + except BadFd: + logger.info("Calling fstat with invalid fd, returning EBADF") + return -errno.EBADF def add(width, val): fformat = {2:'H', 4:'L', 8:'Q'}[width]