-
Notifications
You must be signed in to change notification settings - Fork 224
/
elf.c
192 lines (166 loc) · 5.91 KB
/
elf.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
/*
VitaShell
Copyright (C) 2015-2018, TheFloW
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "main.h"
#include "elf.h"
//! Module Information
typedef struct {
uint16_t attr; //!< Attribute
uint16_t ver; //!< Version
char name[27]; //!< Name
uint8_t type; //!< Type
void *gp; //!< Global Pointer
uint32_t expTop; //!< Offset of the top of export table
uint32_t expBtm; //!< Offset of the bottom of export table
uint32_t impTop; //!< Offset of the top of import table
uint32_t impBtm; //!< Offset of the bottom of import table
uint32_t nid; //!< NID
uint32_t unk[3]; //!< Unknown
uint32_t start; //!< Offset of module_start function
uint32_t stop; //!< Offset of module_stop function
uint32_t exidxTop; //!< Offset of the top of exidx section
uint32_t exidxBtm; //!< Offset of the bottom of exidx section
uint32_t extabTop; //!< Offset of the top of extab section
uint32_t extabBtm; //!< Offset of the bottom of extab section
} SceModuleInfo;
typedef struct {
uint16_t size;
uint16_t lib_version;
uint16_t attribute;
uint16_t num_functions;
uint16_t num_vars;
uint16_t num_tls_vars;
uint32_t reserved1;
uint32_t module_nid;
char *lib_name;
uint32_t reserved2;
uint32_t func_nid_table;
uint32_t func_entry_table;
uint32_t var_nid_table;
uint32_t var_entry_table;
uint32_t tls_nid_table;
uint32_t tls_entry_table;
} SceImportsTable2xx;
typedef struct {
uint16_t size;
uint16_t lib_version;
uint16_t attribute;
uint16_t num_functions;
uint16_t num_vars;
uint16_t unknown1;
uint32_t module_nid;
char *lib_name;
uint32_t func_nid_table;
uint32_t func_entry_table;
uint32_t var_nid_table;
uint32_t var_entry_table;
} SceImportsTable3xx;
void convertToImportsTable3xx(SceImportsTable2xx *import_2xx, SceImportsTable3xx *import_3xx) {
memset(import_3xx, 0, sizeof(SceImportsTable3xx));
if (import_2xx->size == sizeof(SceImportsTable2xx)) {
import_3xx->size = import_2xx->size;
import_3xx->lib_version = import_2xx->lib_version;
import_3xx->attribute = import_2xx->attribute;
import_3xx->num_functions = import_2xx->num_functions;
import_3xx->num_vars = import_2xx->num_vars;
import_3xx->module_nid = import_2xx->module_nid;
import_3xx->lib_name = import_2xx->lib_name;
import_3xx->func_nid_table = import_2xx->func_nid_table;
import_3xx->func_entry_table = import_2xx->func_entry_table;
import_3xx->var_nid_table = import_2xx->var_nid_table;
import_3xx->var_entry_table = import_2xx->var_entry_table;
} else if (import_2xx->size == sizeof(SceImportsTable3xx)) {
memcpy(import_3xx, import_2xx, sizeof(SceImportsTable3xx));
}
}
int checkForUnsafeImports(const Elf32_Ehdr *ehdr, const Elf32_Phdr *phdr, void *buffer) {
if (ehdr->e_ident[EI_MAG0] != ELFMAG0 ||
ehdr->e_ident[EI_MAG1] != ELFMAG1 ||
ehdr->e_ident[EI_MAG2] != ELFMAG2 ||
ehdr->e_ident[EI_MAG3] != ELFMAG3) {
return VITASHELL_ERROR_INVALID_MAGIC;
}
uint32_t segment = ehdr->e_entry >> 30;
uint32_t offset = ehdr->e_entry & 0x3FFFFFFF;
uint32_t text_addr = (uint32_t)buffer;
for (int i = 0; i < segment; i++) {
text_addr += phdr[segment].p_filesz;
}
SceModuleInfo *mod_info = (SceModuleInfo *)(text_addr + offset);
int has_dangerous_nids = 0;
int has_unsafe_libraries = 0;
uint32_t i = mod_info->impTop;
while (i < mod_info->impBtm) {
SceImportsTable3xx import;
convertToImportsTable3xx((void *)text_addr + i, &import);
char *libname = (char *)(text_addr + import.lib_name - phdr[segment].p_vaddr);
uint32_t *func_nid_table = (uint32_t *)(text_addr + import.func_nid_table - phdr[segment].p_vaddr);
if (strcmp(libname, "SceVshBridge") == 0) {
int j;
for (j = 0; j < import.num_functions; j++) {
// Check for dangerous _vshIoMount/vshIoUmount
if (func_nid_table[j] == 0x3C522C35 || func_nid_table[j] == 0x35BC26AC) {
has_dangerous_nids = 1;
break;
}
}
} else if (strcmp(libname, "ScePromoterUtil") == 0 || strcmp(libname, "SceShellSvc") == 0) {
has_unsafe_libraries = 1;
}
i += import.size;
}
if (has_dangerous_nids)
return 2; // Really not safe bro
if (has_unsafe_libraries)
return 1; // Unsafe, but won't kill you
return 0; // Safe
}
char *uncompressBuffer(const Elf32_Ehdr *ehdr, const Elf32_Phdr *phdr, const segment_info *segment,
const char *buffer) {
if (ehdr->e_ident[EI_MAG0] != ELFMAG0 ||
ehdr->e_ident[EI_MAG1] != ELFMAG1 ||
ehdr->e_ident[EI_MAG2] != ELFMAG2 ||
ehdr->e_ident[EI_MAG3] != ELFMAG3) {
return NULL;
}
int i;
// sum all segment size
uint32_t total_sz = 0;
for (i = 0; i < ehdr->e_phnum; i++) {
total_sz += (phdr + i)->p_filesz;
}
char *out = malloc(total_sz);
if (out == NULL) {
return NULL;
}
// uncompress each segments
char *buf = out;
for (i = 0; i < ehdr->e_phnum; i++) {
uint64_t offset = (segment + i)->offset - segment->offset;
uint32_t size = (phdr + i)->p_filesz;
if ((segment + i)->compression == 1) {
memcpy(buf, buffer + offset, (segment + i)->length);
buf += size;
continue;
}
uLongf buf_len = size;
int ret = uncompress((Bytef *)buf, &buf_len, (const Bytef *)buffer + offset, (segment + i)->length);
if (ret != Z_OK) {
free(out);
return NULL;
}
buf += size;
}
return out;
}