From 85336e0fcaaf826d7547e67033fa31d92a9b9e91 Mon Sep 17 00:00:00 2001 From: grossmj Date: Sat, 26 Apr 2014 22:04:22 -0600 Subject: [PATCH] Check for valid IOS & IOU image paths and files (by analyzing the firt 7 bytes of their ELF header). --- gns3server/modules/dynamips/nodes/router.py | 14 ++++++++++++++ gns3server/modules/iou/iou_device.py | 19 +++++++++++++++++-- 2 files changed, 31 insertions(+), 2 deletions(-) diff --git a/gns3server/modules/dynamips/nodes/router.py b/gns3server/modules/dynamips/nodes/router.py index 45f0feea..39165f1c 100644 --- a/gns3server/modules/dynamips/nodes/router.py +++ b/gns3server/modules/dynamips/nodes/router.py @@ -271,6 +271,20 @@ class Router(object): self.resume() else: + if not os.path.isfile(self._image): + raise DynamipsError("IOS image '{}' is not accessible".format(self._image)) + + try: + with open(self._image, "rb") as f: + # read the first 7 bytes of the file. + elf_header_start = f.read(7) + except OSError as e: + raise DynamipsError("Cannot read ELF header for IOS image {}: {}".format(self._image, e)) + + # IOS images must start with the ELF magic number, be 32-bit, big endian and have an ELF version of 1 + if elf_header_start != b'\x7fELF\x01\x02\x01': + raise DynamipsError("'{}' is not a valid IOU image".format(self._image)) + if self.console and self.aux: # check that console and aux ports are available try: diff --git a/gns3server/modules/iou/iou_device.py b/gns3server/modules/iou/iou_device.py index f56d38b3..517be45d 100644 --- a/gns3server/modules/iou/iou_device.py +++ b/gns3server/modules/iou/iou_device.py @@ -414,8 +414,23 @@ class IOUDevice(object): if not self.is_running(): - if not os.path.isfile(self._path): - raise IOUError("IOU '{}' is not accessible".format(self._path)) + if not os.path.isfile(iou_path): + raise IOUError("IOU image '{}' is not accessible".format(iou_path)) + + try: + with open(iou_path, "rb") as f: + # read the first 7 bytes of the file. + elf_header_start = f.read(7) + except OSError as e: + raise IOUError("Cannot read ELF header for IOU image '{}': {}".format(self._path, e)) + + # IOU images must start with the ELF magic number, be 32-bit, little endian + # and have an ELF version of 1 normal IOS image are big endian! + if elf_header_start != b'\x7fELF\x01\x01\x01': + raise IOUError("'{}' is not a valid IOU image".format(self._path)) + + if not os.access(self._path, os.X_OK): + raise IOUError("IOU image '{}' is not executable".format(self._path)) if not self._iourc or not os.path.isfile(self._iourc): raise IOUError("A iourc file is necessary to start IOU")