| 1 |
/* |
| 2 |
Copyright (c) 2018, David Anderson |
| 3 |
All rights reserved. |
| 4 |
|
| 5 |
Redistribution and use in source and binary forms, with |
| 6 |
or without modification, are permitted provided that the |
| 7 |
following conditions are met: |
| 8 |
|
| 9 |
Redistributions of source code must retain the above |
| 10 |
copyright notice, this list of conditions and the following |
| 11 |
disclaimer. |
| 12 |
|
| 13 |
Redistributions in binary form must reproduce the above |
| 14 |
copyright notice, this list of conditions and the following |
| 15 |
disclaimer in the documentation and/or other materials |
| 16 |
provided with the distribution. |
| 17 |
|
| 18 |
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND |
| 19 |
CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, |
| 20 |
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
| 21 |
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 22 |
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR |
| 23 |
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 24 |
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
| 25 |
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 26 |
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 27 |
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 28 |
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR |
| 29 |
OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, |
| 30 |
EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 31 |
*/ |
| 32 |
|
| 33 |
#include <config.h> |
| 34 |
#include <stddef.h> /* size_t */ |
| 35 |
#include <stdio.h> /* SEEK_END SEEK_SET */ |
| 36 |
|
| 37 |
#include "dwarf.h" |
| 38 |
#include "libdwarf.h" |
| 39 |
#include "libdwarf_private.h" |
| 40 |
#include "dwarf_base_types.h" |
| 41 |
#include "dwarf_opaque.h" |
| 42 |
#include "dwarf_safe_strcpy.h" |
| 43 |
#include "dwarf_object_read_common.h" |
| 44 |
|
| 45 |
/* Neither off_t nor ssize_t is in C90. |
| 46 |
However, both are in Posix: |
| 47 |
IEEE Std 1003.1-1990, aka |
| 48 |
ISO/IEC 9954-1:1990. |
| 49 |
This gets asked to read large sections sometimes. |
| 50 |
The Linux kernel allows at most 0x7ffff000 |
| 51 |
bytes in a read()*/ |
| 52 |
int |
| 53 |
_dwarf_object_read_random(int fd, char *buf, Dwarf_Unsigned loc, |
| 54 |
Dwarf_Unsigned size, Dwarf_Unsigned filesize, int *errc) |
| 55 |
{ |
| 56 |
Dwarf_Unsigned endpoint = 0; |
| 57 |
int res = 0; |
| 58 |
|
| 59 |
if (loc >= filesize) { |
| 60 |
/* Seek can seek off the end. Lets not allow that. |
| 61 |
The object is corrupt. */ |
| 62 |
*errc = DW_DLE_SEEK_OFF_END; |
| 63 |
return DW_DLV_ERROR; |
| 64 |
} |
| 65 |
endpoint = loc+size; |
| 66 |
if (endpoint < loc) { |
| 67 |
/* Overflow! The object is corrupt. */ |
| 68 |
*errc = DW_DLE_READ_OFF_END; |
| 69 |
return DW_DLV_ERROR; |
| 70 |
} |
| 71 |
if (endpoint > filesize) { |
| 72 |
/* Let us -not- try to read past end of object. |
| 73 |
The object is corrupt. */ |
| 74 |
*errc = DW_DLE_READ_OFF_END; |
| 75 |
return DW_DLV_ERROR; |
| 76 |
} |
| 77 |
res = _dwarf_seekr(fd,loc,SEEK_SET,0); |
| 78 |
if (res != DW_DLV_OK) { |
| 79 |
*errc = DW_DLE_SEEK_ERROR; |
| 80 |
return DW_DLV_ERROR; |
| 81 |
} |
| 82 |
res = _dwarf_readr(fd,buf,size,0); |
| 83 |
if (res != DW_DLV_OK) { |
| 84 |
*errc = DW_DLE_READ_ERROR; |
| 85 |
return DW_DLV_ERROR; |
| 86 |
} |
| 87 |
return DW_DLV_OK; |
| 88 |
} |