1/* @LICENSE(UNSW_OZPLB) */
2
3/*
4 * Australian Public Licence B (OZPLB)
5 *
6 * Version 1-0
7 *
8 * Copyright (c) 2004 University of New South Wales
9 *
10 * All rights reserved.
11 *
12 * Developed by: Operating Systems and Distributed Systems Group (DiSy)
13 *               University of New South Wales
14 *               http://www.disy.cse.unsw.edu.au
15 *
16 * Permission is granted by University of New South Wales, free of charge, to
17 * any person obtaining a copy of this software and any associated
18 * documentation files (the "Software") to deal with the Software without
19 * restriction, including (without limitation) the rights to use, copy,
20 * modify, adapt, merge, publish, distribute, communicate to the public,
21 * sublicense, and/or sell, lend or rent out copies of the Software, and
22 * to permit persons to whom the Software is furnished to do so, subject
23 * to the following conditions:
24 *
25 *     * Redistributions of source code must retain the above copyright
26 *       notice, this list of conditions and the following disclaimers.
27 *
28 *     * Redistributions in binary form must reproduce the above
29 *       copyright notice, this list of conditions and the following
30 *       disclaimers in the documentation and/or other materials provided
31 *       with the distribution.
32 *
33 *     * Neither the name of University of New South Wales, nor the names of its
34 *       contributors, may be used to endorse or promote products derived
35 *       from this Software without specific prior written permission.
36 *
37 * EXCEPT AS EXPRESSLY STATED IN THIS LICENCE AND TO THE FULL EXTENT
38 * PERMITTED BY APPLICABLE LAW, THE SOFTWARE IS PROVIDED "AS-IS", AND
39 * NATIONAL ICT AUSTRALIA AND ITS CONTRIBUTORS MAKE NO REPRESENTATIONS,
40 * WARRANTIES OR CONDITIONS OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
41 * BUT NOT LIMITED TO ANY REPRESENTATIONS, WARRANTIES OR CONDITIONS
42 * REGARDING THE CONTENTS OR ACCURACY OF THE SOFTWARE, OR OF TITLE,
43 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, NONINFRINGEMENT,
44 * THE ABSENCE OF LATENT OR OTHER DEFECTS, OR THE PRESENCE OR ABSENCE OF
45 * ERRORS, WHETHER OR NOT DISCOVERABLE.
46 *
47 * TO THE FULL EXTENT PERMITTED BY APPLICABLE LAW, IN NO EVENT SHALL
48 * NATIONAL ICT AUSTRALIA OR ITS CONTRIBUTORS BE LIABLE ON ANY LEGAL
49 * THEORY (INCLUDING, WITHOUT LIMITATION, IN AN ACTION OF CONTRACT,
50 * NEGLIGENCE OR OTHERWISE) FOR ANY CLAIM, LOSS, DAMAGES OR OTHER
51 * LIABILITY, INCLUDING (WITHOUT LIMITATION) LOSS OF PRODUCTION OR
52 * OPERATION TIME, LOSS, DAMAGE OR CORRUPTION OF DATA OR RECORDS; OR LOSS
53 * OF ANTICIPATED SAVINGS, OPPORTUNITY, REVENUE, PROFIT OR GOODWILL, OR
54 * OTHER ECONOMIC LOSS; OR ANY SPECIAL, INCIDENTAL, INDIRECT,
55 * CONSEQUENTIAL, PUNITIVE OR EXEMPLARY DAMAGES, ARISING OUT OF OR IN
56 * CONNECTION WITH THIS LICENCE, THE SOFTWARE OR THE USE OF OR OTHER
57 * DEALINGS WITH THE SOFTWARE, EVEN IF NATIONAL ICT AUSTRALIA OR ITS
58 * CONTRIBUTORS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH CLAIM, LOSS,
59 * DAMAGES OR OTHER LIABILITY.
60 *
61 * If applicable legislation implies representations, warranties, or
62 * conditions, or imposes obligations or liability on University of New South
63 * Wales or one of its contributors in respect of the Software that
64 * cannot be wholly or partly excluded, restricted or modified, the
65 * liability of University of New South Wales or the contributor is limited, to
66 * the full extent permitted by the applicable legislation, at its
67 * option, to:
68 * a.  in the case of goods, any one or more of the following:
69 * i.  the replacement of the goods or the supply of equivalent goods;
70 * ii.  the repair of the goods;
71 * iii. the payment of the cost of replacing the goods or of acquiring
72 *  equivalent goods;
73 * iv.  the payment of the cost of having the goods repaired; or
74 * b.  in the case of services:
75 * i.  the supplying of the services again; or
76 * ii.  the payment of the cost of having the services supplied again.
77 *
78 * The construction, validity and performance of this licence is governed
79 * by the laws in force in New South Wales, Australia.
80 */
81#include <elf/elf.h>
82#include <elf/elf32.h>
83#include <inttypes.h>
84#include <string.h>
85
86/* ELF header functions */
87int elf32_checkFile(elf_t *elf)
88{
89    if (elf->elfSize < sizeof(Elf32_Ehdr)) {
90        return -1; /* file smaller than ELF header */
91    }
92
93    if (elf_check_magic(elf->elfFile) < 0) {
94        return -1; /* not an ELF file */
95    }
96
97    Elf32_Ehdr *header = elf->elfFile;
98    if (header->e_ident[EI_CLASS] != ELFCLASS32) {
99        return -1; /* not a 32-bit ELF */
100    }
101
102    if (header->e_phentsize != sizeof(Elf32_Phdr)) {
103        return -1; /* unexpected program header size */
104    }
105
106    if (header->e_shentsize != sizeof(Elf32_Shdr)) {
107        return -1; /* unexpected section header size */
108    }
109
110    if (header->e_shstrndx >= header->e_shnum) {
111        return -1; /* invalid section header string table section */
112    }
113
114    elf->elfClass = header->e_ident[EI_CLASS];
115    return 0; /* elf header looks OK */
116}
117
118int elf32_checkProgramHeaderTable(elf_t *elf)
119{
120    Elf32_Ehdr *header = elf->elfFile;
121    size_t ph_end = header->e_phoff + header->e_phentsize * header->e_phnum;
122    if (elf->elfSize < ph_end || ph_end < header->e_phoff) {
123        return -1; /* invalid program header table */
124    }
125
126    return 0;
127}
128
129int elf32_checkSectionTable(elf_t *elf)
130{
131    Elf32_Ehdr *header = elf->elfFile;
132    size_t sh_end = header->e_shoff + header->e_shentsize * header->e_shnum;
133    if (elf->elfSize < sh_end || sh_end < header->e_shoff) {
134        return -1; /* invalid section header table */
135    }
136
137    return 0;
138}
139