-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathself.c
More file actions
414 lines (335 loc) · 8.07 KB
/
self.c
File metadata and controls
414 lines (335 loc) · 8.07 KB
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
/* Copyright (C) 2025 John Törnblom
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, 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; see the file COPYING. If not, see
<http://www.gnu.org/licenses/>. */
#include <elf.h>
#include <errno.h>
#include <fcntl.h>
#include <pthread.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include "io.h"
#include "self.h"
#include "sha256.h"
/**
* A global lock that address race conditions that may occur when
* threads atempt to read several SELF files at the same time.
**/
static pthread_mutex_t g_mutex = PTHREAD_MUTEX_INITIALIZER;
/**
* Decrypt and copy an encrypted ELF segment.
**/
static int
decrypt_segment(int self_fd, int elf_fd, const Elf64_Phdr* phdr, size_t ind) {
uint8_t* data = MAP_FAILED;
int err = 0;
pthread_mutex_lock(&g_mutex);
if((data=self_map_segment(self_fd, phdr, ind)) == MAP_FAILED) {
err = -1;
} else if(mlock(data, phdr->p_filesz)) {
err = -1;
} else if(io_pwrite(elf_fd, data, phdr->p_filesz, phdr->p_offset)) {
err = -1;
}
if(data != MAP_FAILED) {
munmap(data, phdr->p_filesz);
}
pthread_mutex_unlock(&g_mutex);
return err;
}
/**
* Copy a plaintext ELF segment.
**/
static int
copy_segment(int from_fd, off_t from_start, int to_fd, off_t to_start,
size_t size) {
return io_pcopy(from_fd, to_fd, from_start, to_start, size);
}
/**
* Compute the SHA256 sum of a file.
**/
static int
sha256sum(int fd, uint8_t hash[SHA256_BLOCK_SIZE]) {
uint8_t buf[0x1000] = {0};
SHA256_CTX sha256;
struct stat st;
off_t n;
if(fstat(fd, &st)) {
return -1;
}
sha256_init(&sha256);
for(off_t off=0; off<st.st_size;) {
if((n=pread(fd, buf, sizeof(buf), off)) < 0) {
return -1;
}
sha256_update(&sha256, buf, n);
off += n;
}
sha256_final(&sha256, hash);
return 0;
}
/**
* Add zero padding between off and off+len.
**/
static int
zeropad(int fd, off_t off, off_t len) {
struct stat st;
void *buf;
size_t n;
if(fstat(fd, &st)) {
return -1;
}
if(st.st_size >= off+len) {
return 0;
}
if(!(buf=calloc(1, IO_COPY_BUFSIZE))) {
return -1;
}
for(off_t i=st.st_size; i<off+len; i+=IO_COPY_BUFSIZE) {
n = IO_COPY_BUFSIZE;
if(i+n > off+len) {
n = off+len-i;
}
if(io_pwrite(fd, buf, n, i)) {
free(buf);
return -1;
}
}
free(buf);
return 0;
}
static size_t
self_get_elfsize_fd(int fd) {
self_head_t head;
Elf64_Ehdr ehdr;
Elf64_Phdr phdr;
off_t elf_off;
size_t size = 0;
if(io_nread(fd, &head, sizeof(head))) {
return 0;
}
if(head.magic != SELF_PS4_MAGIC && head.magic != SELF_PS5_MAGIC) {
return 0;
}
if(lseek(fd, head.num_entries * sizeof(self_entry_t), SEEK_CUR) < 0) {
return 0;
}
elf_off = lseek(fd, 0, SEEK_CUR);
if(io_nread(fd, &ehdr, sizeof(ehdr))) {
return 0;
}
if(ehdr.e_ident[0] != 0x7f || ehdr.e_ident[1] != 'E' ||
ehdr.e_ident[2] != 'L' || ehdr.e_ident[3] != 'F') {
return 0;
}
if(lseek(fd, elf_off + ehdr.e_phoff, SEEK_SET) < 0) {
return 0;
}
for(int i=0; i<ehdr.e_phnum; i++) {
if(io_nread(fd, &phdr, sizeof(phdr))) {
return 0;
}
if(phdr.p_offset + phdr.p_filesz > size) {
size = phdr.p_offset + phdr.p_filesz;
}
}
return size;
}
size_t
self_get_elfsize(const char* path) {
size_t size;
int fd;
if((fd=open(path, O_RDONLY, 0)) < 0) {
return -1;
}
size = self_get_elfsize_fd(fd);
close(fd);
return size;
}
int
self_extract_elf(int self_fd, int elf_fd) {
uint8_t hash[SHA256_BLOCK_SIZE];
self_exinfo_t extinfo;
self_entry_t* entries;
self_entry_t* entry;
self_head_t head;
Elf64_Ehdr ehdr;
Elf64_Phdr phdr;
size_t elf_size;
off_t elf_off;
off_t off;
// Ensure wholes in the ELF file are all zeroed out
elf_size = self_get_elfsize_fd(self_fd);
if(zeropad(elf_fd, 0, elf_size)) {
return -1;
}
lseek(self_fd, 0, SEEK_SET);
// Read the SELF header
if(io_nread(self_fd, &head, sizeof(head))) {
return -1;
}
// Sanity check the SELF header
if(head.magic != SELF_PS4_MAGIC && head.magic != SELF_PS5_MAGIC) {
errno = ENOEXEC;
return -1;
}
// Read SELF entries
if(!(entries=calloc(head.num_entries, sizeof(self_entry_t)))) {
return -1;
}
if(io_nread(self_fd, entries, head.num_entries * sizeof(self_entry_t))) {
free(entries);
return -1;
}
// Remember the position of the ELF header bundled within the SELF file
if((elf_off=lseek(self_fd, 0, SEEK_CUR)) < 0) {
free(entries);
return -1;
}
// Read the ELF header
if(io_nread(self_fd, &ehdr, sizeof(ehdr))) {
free(entries);
return -1;
}
// Sanity check the ELF header
if(ehdr.e_ident[0] != 0x7f || ehdr.e_ident[1] != 'E' ||
ehdr.e_ident[2] != 'L' || ehdr.e_ident[3] != 'F') {
free(entries);
errno = ENOEXEC;
return -1;
}
#if 0
// Drop section headers
ehdr.e_shnum = 0;
ehdr.e_shoff = 0;
#endif
// Skip ahead to the ELF program headers
if(lseek(self_fd, elf_off + ehdr.e_phoff, SEEK_SET) < 0) {
free(entries);
return -1;
}
// Write the ELF header
if(io_nwrite(elf_fd, &ehdr, sizeof(ehdr))) {
free(entries);
return -1;
}
// Enumerate ELF program headers
elf_size = ehdr.e_phoff + ehdr.e_phnum * sizeof(phdr);
for(int i=0; i<ehdr.e_phnum; i++) {
if(io_nread(self_fd, &phdr, sizeof(phdr))) {
free(entries);
return -1;
}
if(io_nwrite(elf_fd, &phdr, sizeof(phdr))) {
free(entries);
return -1;
}
if(!phdr.p_filesz) {
continue;
}
// PT_SCE_VERSION segment is appended at the end of the SELF file
if(phdr.p_type == 0x6fffff01) {
if(copy_segment(self_fd, head.file_size, elf_fd, phdr.p_offset,
phdr.p_filesz)) {
#if 0 // Some FSELFs are missing the version data, ignore error
free(entries);
return -1;
#endif
}
continue;
}
// Find the SELF entry for this program header
entry = 0;
for(int j=0; j<head.num_entries; j++) {
if(entries[j].props.segment_index == i &&
entries[j].props.has_blocks) {
entry = &entries[j];
break;
}
}
if(!entry) {
continue;
}
// Decrypt and copy the segment
if(entry->props.is_encrypted || entry->props.is_compressed) {
if(decrypt_segment(self_fd, elf_fd, &phdr, i)) {
free(entries);
return -1;
}
} else if(copy_segment(self_fd, entry->offset, elf_fd, phdr.p_offset,
phdr.p_filesz)) {
free(entries);
return -1;
}
}
free(entries);
// Compute the SHA256 sum of the ELF
if(sha256sum(elf_fd, hash)) {
return -1;
}
// Seek to the SELF extended info
if((off=lseek(self_fd, 0, SEEK_CUR)) < 0) {
return -1;
}
off = (((off) + (0x10-1)) & ~(0x10-1));
if(lseek(self_fd, off, SEEK_SET) < 0) {
return -1;
}
// Read the SELF extended info
if(io_nread(self_fd, &extinfo, sizeof(extinfo))) {
return -1;
}
// Compare the computed ELF SHA256 sum with the expected one
// available in the SELF extended info
if(memcmp(hash, extinfo.digest, sizeof(hash))) {
errno = EBADMSG;
return -1;
}
return 0;
}
int
self_is_valid(const char* path) {
self_head_t head;
struct stat st;
ssize_t n;
int fd;
if(stat(path, &st)) {
return -1;
}
if(!S_ISREG(st.st_mode)) {
return 0;
}
if((fd=open(path, O_RDONLY, 0)) < 0) {
return -1;
}
if((n=read(fd, &head, sizeof(head))) < 0) {
close(fd);
return -1;
}
if(n != sizeof(head)) {
close(fd);
return 0;
}
if(head.magic != SELF_PS4_MAGIC && head.magic != SELF_PS5_MAGIC) {
close(fd);
return 0;
}
close(fd);
return 1;
}
/*
Local Variables:
c-file-style: "gnu"
End:
*/