-
Notifications
You must be signed in to change notification settings - Fork 46
/
taihen-user.c
986 lines (939 loc) · 29.7 KB
/
taihen-user.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
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
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
/* taihen-user.c -- user exports for taiHEN
*
* Copyright (C) 2016 Yifan Lu
*
* This software may be modified and distributed under the terms
* of the MIT license. See the LICENSE file for details.
*/
#include <psp2kern/types.h>
#include <psp2kern/sblacmgr.h>
#include <psp2kern/kernel/cpu.h>
#include <psp2kern/kernel/modulemgr.h>
#include <psp2kern/kernel/sysmem.h>
#include <psp2kern/kernel/threadmgr.h>
#include <psp2/kernel/error.h>
#include "error.h"
#include "module.h"
#include "patches.h"
#include "plugin.h"
#include "taihen_internal.h"
/** Limit for strings passed to kernel */
#define MAX_NAME_LEN 256
/** Limit for passing args to start module */
#define MAX_ARGS_SIZE 256
/**
* @brief Add a hook to a module function export for the calling process
*
* @see taiHookFunctionExportForKernel
*
* @param[out] p_hook A reference that can be used by the hook function
* @param[in] args Call arguments
*
* @return A tai patch reference on success, < 0 on error
* - TAI_ERROR_PATCH_EXISTS if the address is already patched
* - TAI_ERROR_HOOK_ERROR if an internal error occurred trying to
* hook
* - TAI_ERROR_NOT_IMPLEMENTED if address is in shared memory region
* - TAI_ERROR_USER_MEMORY if pointers are incorrect
* - TAI_ERROR_INVALID_MODULE if `TAI_MAIN_MODULE` is specified and
* there are multiple main modules
*/
SceUID taiHookFunctionExportForUser(tai_hook_ref_t *p_hook, tai_hook_args_t *args) {
tai_hook_args_t kargs;
uint32_t func_nid;
const void *hook_func;
uint32_t state;
char k_module[MAX_NAME_LEN];
int main_mod;
tai_hook_ref_t k_ref;
SceUID kid, ret;
SceUID pid;
ENTER_SYSCALL(state);
kargs.size = 0;
ksceKernelMemcpyUserToKernel(&kargs, (uintptr_t)args, sizeof(kargs));
if (kargs.size == sizeof(kargs)) {
pid = ksceKernelGetProcessId();
main_mod = (kargs.module == TAI_MAIN_MODULE);
if (main_mod || ksceKernelStrncpyUserToKernel(k_module, (uintptr_t)kargs.module, MAX_NAME_LEN) < MAX_NAME_LEN) {
kid = taiHookFunctionExportForKernel(pid, &k_ref, main_mod ? kargs.module : k_module, kargs.library_nid, kargs.func_nid, kargs.hook_func);
if (kid >= 0) {
ksceKernelMemcpyKernelToUser((uintptr_t)p_hook, &k_ref, sizeof(*p_hook));
ret = ksceKernelCreateUserUid(pid, kid);
LOG("kernel uid: %x, user uid: %x", kid, ret);
} else {
ret = kid;
}
} else {
ret = TAI_ERROR_USER_MEMORY;
}
} else {
LOG("invalid args size: %x", kargs.size);
ret = TAI_ERROR_USER_MEMORY;
}
EXIT_SYSCALL(state);
return ret;
}
/**
* @brief Add a hook to a module function import for the calling process
*
* @see taiHookFunctionImportForKernel
*
* @param[out] p_hook A reference that can be used by the hook function
* @param[in] args Call arguments
*
* @return A tai patch reference on success, < 0 on error
* - TAI_ERROR_PATCH_EXISTS if the address is already patched
* - TAI_ERROR_HOOK_ERROR if an internal error occurred trying to
* hook
* - TAI_ERROR_NOT_IMPLEMENTED if address is in shared memory
* region. You should hook an import from another module instead.
* - TAI_ERROR_USER_MEMORY if pointers are incorrect
* - TAI_ERROR_STUB_NOT_RESOLVED if the import has not been resolved
* yet. You should hook `sceKernelLoadStartModule`,
* `sceSysmoduleLoadModule` or whatever the application uses to
* start the imported module and add this hook after the module is
* loaded. Be sure to also hook module unloading to remove the
* hook BEFORE the imported module is unloaded!
* - TAI_ERROR_INVALID_MODULE if `TAI_MAIN_MODULE` is specified and
* there are multiple main modules
*/
SceUID taiHookFunctionImportForUser(tai_hook_ref_t *p_hook, tai_hook_args_t *args) {
tai_hook_args_t kargs;
uint32_t state;
char k_module[MAX_NAME_LEN];
int main_mod;
tai_hook_ref_t k_ref;
SceUID kid, ret;
SceUID pid;
ENTER_SYSCALL(state);
ksceKernelMemcpyUserToKernel(&kargs, (uintptr_t)args, sizeof(kargs));
if (kargs.size == sizeof(kargs)) {
pid = ksceKernelGetProcessId();
main_mod = (kargs.module == TAI_MAIN_MODULE);
if (main_mod || ksceKernelStrncpyUserToKernel(k_module, (uintptr_t)kargs.module, MAX_NAME_LEN) < MAX_NAME_LEN) {
kid = taiHookFunctionImportForKernel(pid, &k_ref, main_mod ? kargs.module : k_module, kargs.library_nid, kargs.func_nid, kargs.hook_func);
if (kid >= 0) {
ksceKernelMemcpyKernelToUser((uintptr_t)p_hook, &k_ref, sizeof(*p_hook));
ret = ksceKernelCreateUserUid(pid, kid);
LOG("kernel uid: %x, user uid: %x", kid, ret);
} else {
ret = kid;
}
} else {
ret = TAI_ERROR_USER_MEMORY;
}
} else {
LOG("invalid args size: %x", kargs.size);
ret = TAI_ERROR_USER_MEMORY;
}
EXIT_SYSCALL(state);
return ret;
}
/**
* @brief Add a hook to a module manually with an offset for the calling
* process
*
* @see taiHookFunctionOffsetForKernel
*
* @param[out] p_hook A reference that can be used by the hook function
* @param[in] args Call arguments
*
* @return A tai patch reference on success, < 0 on error
* - TAI_ERROR_PATCH_EXISTS if the address is already patched
* - TAI_ERROR_HOOK_ERROR if an internal error occurred trying to
* hook
* - TAI_ERROR_NOT_IMPLEMENTED if address is in shared memory region
* - TAI_ERROR_USER_MEMORY if pointers are incorrect
*/
SceUID taiHookFunctionOffsetForUser(tai_hook_ref_t *p_hook, tai_offset_args_t *args) {
tai_offset_args_t kargs;
uint32_t state;
tai_hook_ref_t k_ref;
SceUID ret;
SceUID pid;
SceUID kid;
ENTER_SYSCALL(state);
kargs.size = 0;
ksceKernelMemcpyUserToKernel(&kargs, (uintptr_t)args, sizeof(kargs));
if (kargs.size == sizeof(kargs)) {
pid = ksceKernelGetProcessId();
kid = ksceKernelKernelUidForUserUid(pid, kargs.modid);
if (kid >= 0) {
ret = taiHookFunctionOffsetForKernel(pid, &k_ref, kid, kargs.segidx, kargs.offset, kargs.thumb, kargs.source);
if (ret >= 0) {
ksceKernelMemcpyKernelToUser((uintptr_t)p_hook, &k_ref, sizeof(*p_hook));
ret = ksceKernelCreateUserUid(pid, ret);
LOG("user uid: %x", ret);
}
} else {
LOG("Error getting kernel uid for %x: %x", kargs.modid, kid);
ret = kid;
}
} else {
LOG("invalid args size: %x", kargs.size);
ret = TAI_ERROR_USER_MEMORY;
}
EXIT_SYSCALL(state);
return ret;
}
/**
* @brief Gets information on a currently loaded module
*
* You can use the macro `TAI_MAIN_MODULE` for `module` to specify
* the main module. This is usually the module that is loaded first
* and is usually the eboot.bin. This will only work if there is
* only one module loaded in the main memory space. Not all
* processes have this property! Make sure you check the return
* value.
*
* @see taiGetModuleInfoForKernel
*
* @param[in] module The name of the module or `TAI_MAIN_MODULE`.
* @param[out] info The information to fill
*
* @return Zero on success, < 0 on error
* - TAI_ERROR_USER_MEMORY if `info->size` is too small or large or
* `module` is invalid
* - TAI_ERROR_INVALID_MODULE if `TAI_MAIN_MODULE` is specified and
* there are multiple main modules
*/
int taiGetModuleInfo(const char *module, tai_module_info_t *info) {
int main_mod;
char k_module[MAX_NAME_LEN];
uint32_t state;
SceUID pid;
tai_module_info_t k_info;
int ret;
ENTER_SYSCALL(state);
pid = ksceKernelGetProcessId();
main_mod = (module == TAI_MAIN_MODULE);
if (main_mod || ksceKernelStrncpyUserToKernel(k_module, (uintptr_t)module, MAX_NAME_LEN) < MAX_NAME_LEN) {
ksceKernelMemcpyUserToKernel(&k_info, (uintptr_t)info, sizeof(size_t));
if (k_info.size == sizeof(k_info)) {
ret = taiGetModuleInfoForKernel(pid, main_mod ? module : k_module, &k_info);
ksceKernelMemcpyKernelToUser((uintptr_t)info, &k_info, k_info.size);
} else {
ret = TAI_ERROR_USER_MEMORY;
}
} else {
ret = TAI_ERROR_USER_MEMORY;
}
EXIT_SYSCALL(state);
return ret;
}
/**
* @brief Release a hook for the calling process
*
* @see taiHookReleaseForKernel
*
* @param[in] tai_uid The tai patch reference to free
* @param[in] hook The hook to free
*
* @return Zero on success, < 0 on error
* - TAI_ERROR_HOOK_ERROR if an internal error occurred trying to restore the function
*/
int taiHookRelease(SceUID tai_uid, tai_hook_ref_t hook) {
uint32_t state;
SceUID pid, kid;
int ret;
ENTER_SYSCALL(state);
pid = ksceKernelGetProcessId();
kid = ksceKernelKernelUidForUserUid(pid, tai_uid);
if (kid >= 0) {
ret = taiHookReleaseForKernel(kid, hook);
ksceKernelDeleteUserUid(pid, tai_uid);
} else {
ret = kid;
}
EXIT_SYSCALL(state);
return ret;
}
/**
* @brief Injects data into the current process bypassing MMU flags
*
* @see taiInjectAbsForKernel
*
* @param dest The address to inject
* @param[in] src Source data
* @param[in] size The size of the injection in bytes
*
* @return A tai patch reference on success, < 0 on error
* - TAI_ERROR_PATCH_EXISTS if the address is already patched
*/
SceUID taiInjectAbs(void *dest, const void *src, size_t size) {
uint32_t state;
tai_hook_ref_t k_ref;
SceUID ret;
SceUID pid;
ENTER_SYSCALL(state);
pid = ksceKernelGetProcessId();
ret = taiInjectAbsForKernel(pid, dest, src, size);
if (ret >= 0) {
ret = ksceKernelCreateUserUid(pid, ret);
LOG("user uid: %x", ret);
}
EXIT_SYSCALL(state);
return ret;
}
/**
* @brief Inject data into the current process bypassing MMU flags given an
* offset
*
* @see taiInjectDataForKernel
*
* @param[in] args Call arguments
*
* @return A tai patch reference on success, < 0 on error
* - TAI_ERROR_PATCH_EXISTS if the address is already patched
*/
SceUID taiInjectDataForUser(tai_offset_args_t *args) {
tai_offset_args_t kargs;
uint32_t state;
tai_hook_ref_t k_ref;
SceUID ret;
SceUID pid;
ENTER_SYSCALL(state);
kargs.size = 0;
ksceKernelMemcpyUserToKernel(&kargs, (uintptr_t)args, sizeof(kargs));
if (kargs.size == sizeof(kargs)) {
pid = ksceKernelGetProcessId();
ret = ksceKernelKernelUidForUserUid(pid, kargs.modid);
if (ret >= 0) {
kargs.modid = ret;
ret = taiInjectDataForKernel(pid, kargs.modid, kargs.segidx, kargs.offset, kargs.source, kargs.source_size);
if (ret >= 0) {
ret = ksceKernelCreateUserUid(pid, ret);
LOG("user uid: %x", ret);
}
} else {
LOG("Error getting kernel uid for %x: %x", kargs.modid, ret);
}
} else {
LOG("invalid args size: %x", kargs.size);
ret = TAI_ERROR_USER_MEMORY;
}
EXIT_SYSCALL(state);
return ret;
}
/**
* @brief Release an injection for the calling process
*
* @see taiInjectReleaseForKernel
*
* @param[in] tai_uid The tai patch reference to free
*
* @return Zero on success, < 0 on error
*/
int taiInjectRelease(SceUID tai_uid) {
uint32_t state;
SceUID pid, kid;
int ret;
ENTER_SYSCALL(state);
pid = ksceKernelGetProcessId();
kid = ksceKernelKernelUidForUserUid(pid, tai_uid);
if (kid >= 0) {
ret = taiInjectReleaseForKernel(kid);
ksceKernelDeleteUserUid(pid, tai_uid);
} else {
ret = kid;
}
EXIT_SYSCALL(state);
return ret;
}
/**
* @brief Loads a kernel module
*
* @param[in] path The path to the skprx
* @param[in] flags The flags
* @param opt Optional arguments, set to NULL
*
* @return A module reference on success, < 0 on error
* - TAI_ERROR_INVALID_ARGS if `opt` is not NULL
* - TAI_ERROR_NOT_ALLOWED if caller does not have permission
*/
SceUID taiLoadKernelModule(const char *path, int flags, void *opt) {
uint32_t state;
char k_path[MAX_NAME_LEN];
SceUID pid;
int ret;
ENTER_SYSCALL(state);
pid = ksceKernelGetProcessId();
if (ksceSblACMgrIsShell(0)) {
if (opt == NULL) {
if (ksceKernelStrncpyUserToKernel(k_path, (uintptr_t)path, MAX_NAME_LEN) < MAX_NAME_LEN) {
ret = ksceKernelLoadModule(k_path, flags, NULL);
LOG("loaded %s: %x", k_path, ret);
if (ret >= 0) {
ret = ksceKernelCreateUserUid(pid, ret);
LOG("user uid: %x", ret);
}
} else {
ret = TAI_ERROR_USER_MEMORY;
}
} else {
ret = TAI_ERROR_INVALID_ARGS;
}
} else {
ret = TAI_ERROR_NOT_ALLOWED;
}
EXIT_SYSCALL(state);
return ret;
}
/**
* @brief Starts a kernel module
*
* @param[in] modid The id from `taiLoadKernelModule`
* @param[in] args The arguments
* @param opt Optional arguments, set to NULL
* @param res Return value of `module_start`
*
* @return Zero on success, < 0 on error
* - TAI_ERROR_INVALID_ARGS if `args` is too large or `opt` is not
* NULL
* - TAI_ERROR_NOT_ALLOWED if caller does not have permission
*/
int taiStartKernelModuleForUser(SceUID modid, tai_module_args_t *args, void *opt, int *res) {
tai_module_args_t kargs;
char buf[MAX_ARGS_SIZE];
uint32_t state;
int ret;
int k_res;
SceUID pid;
ENTER_SYSCALL(state);
pid = ksceKernelGetProcessId();
if (ksceSblACMgrIsShell(0)) {
kargs.size = 0;
ksceKernelMemcpyUserToKernel(&kargs, (uintptr_t)args, sizeof(kargs));
if (kargs.size == sizeof(kargs)) {
if (kargs.args <= MAX_ARGS_SIZE && opt == NULL) {
ret = ksceKernelKernelUidForUserUid(pid, modid);
if (ret >= 0) {
modid = ret;
ret = ksceKernelMemcpyUserToKernel(buf, (uintptr_t)kargs.argp, kargs.args);
if (ret >= 0) {
k_res = 0;
ret = ksceKernelStartModule(modid, kargs.args, buf, kargs.flags, NULL, &k_res);
if (res) {
ksceKernelMemcpyKernelToUser((uintptr_t)res, &k_res, sizeof(*res));
}
}
} else {
LOG("Error getting kernel uid for %x: %x", modid, ret);
}
} else {
LOG("invalid args size: %x", kargs.size);
ret = TAI_ERROR_USER_MEMORY;
}
} else {
ret = TAI_ERROR_INVALID_ARGS;
}
} else {
ret = TAI_ERROR_NOT_ALLOWED;
}
EXIT_SYSCALL(state);
return ret;
}
/**
* @brief Loads and starts a kernel module
*
* @param[in] path The path of the skprx
* @param[in] args The arguments
*
* @return A module reference on success, < 0 on error
* - TAI_ERROR_INVALID_ARGS if `args` is too large
* - TAI_ERROR_NOT_ALLOWED if caller does not have permission
*/
SceUID taiLoadStartKernelModuleForUser(const char *path, tai_module_args_t *args) {
tai_module_args_t kargs;
char buf[MAX_ARGS_SIZE];
char k_path[MAX_NAME_LEN];
uint32_t state;
SceUID modid;
int ret;
SceUID pid;
ENTER_SYSCALL(state);
pid = ksceKernelGetProcessId();
if (ksceSblACMgrIsShell(0)) {
kargs.size = 0;
ksceKernelMemcpyUserToKernel(&kargs, (uintptr_t)args, sizeof(kargs));
if (kargs.size == sizeof(kargs)) {
if (kargs.args <= MAX_ARGS_SIZE) {
ret = ksceKernelMemcpyUserToKernel(buf, (uintptr_t)kargs.argp, kargs.args);
if (ret >= 0) {
if (ksceKernelStrncpyUserToKernel(k_path, (uintptr_t)path, MAX_NAME_LEN) < MAX_NAME_LEN) {
ret = ksceKernelLoadStartModule(k_path, kargs.args, buf, kargs.flags, NULL, NULL);
LOG("loaded %s: %x", k_path, ret);
if (ret >= 0) {
ret = ksceKernelCreateUserUid(pid, ret);
LOG("user uid: %x", ret);
}
} else {
ret = TAI_ERROR_USER_MEMORY;
}
}
} else {
ret = TAI_ERROR_INVALID_ARGS;
}
} else {
LOG("invalid args size: %x", kargs.size);
ret = TAI_ERROR_USER_MEMORY;
}
} else {
ret = TAI_ERROR_NOT_ALLOWED;
}
EXIT_SYSCALL(state);
return ret;
}
/**
* @brief Loads and starts a user module for another process
*
* @param[in] path The path of the skprx
* @param[in] args The arguments
*
* @return A module reference on success, < 0 on error
* - TAI_ERROR_INVALID_ARGS if `args` is too large
* - TAI_ERROR_NOT_ALLOWED if caller does not have permission
*/
SceUID taiLoadStartModuleForPidForUser(const char *path, tai_module_args_t *args) {
tai_module_args_t kargs;
char buf[MAX_ARGS_SIZE];
char k_path[MAX_NAME_LEN];
uint32_t state;
SceUID modid;
int ret;
ENTER_SYSCALL(state);
if (ksceSblACMgrIsShell(0)) {
kargs.size = 0;
ksceKernelMemcpyUserToKernel(&kargs, (uintptr_t)args, sizeof(kargs));
if (kargs.size == sizeof(kargs)) {
if (kargs.args <= MAX_ARGS_SIZE) {
ret = ksceKernelMemcpyUserToKernel(buf, (uintptr_t)kargs.argp, kargs.args);
if (ret >= 0) {
if (ksceKernelStrncpyUserToKernel(k_path, (uintptr_t)path, MAX_NAME_LEN) < MAX_NAME_LEN) {
ret = ksceKernelLoadStartModuleForPid(kargs.pid, k_path, kargs.args, buf, kargs.flags, NULL, NULL);
LOG("loaded %s: %x", k_path, ret);
if (ret >= 0) {
ret = ksceKernelCreateUserUid(kargs.pid, ret);
LOG("user uid: %x", ret);
}
} else {
ret = TAI_ERROR_USER_MEMORY;
}
}
} else {
ret = TAI_ERROR_INVALID_ARGS;
}
} else {
LOG("invalid args size: %x", kargs.size);
ret = TAI_ERROR_USER_MEMORY;
}
} else {
ret = TAI_ERROR_NOT_ALLOWED;
}
EXIT_SYSCALL(state);
return ret;
}
/**
* @brief Stops a kernel module
*
* @param[in] modid The loaded module reference
* @param[in] args The arguments
* @param opt Optional arguments, set to NULL
* @param res Return value of `module_stop`
*
* @return Zero on success, < 0 on error
* - TAI_ERROR_INVALID_ARGS if `args` is too large or `opt` is not NULL
* - TAI_ERROR_NOT_ALLOWED if caller does not have permission
*/
int taiStopKernelModuleForUser(SceUID modid, tai_module_args_t *args, void *opt, int *res) {
tai_module_args_t kargs;
char buf[MAX_ARGS_SIZE];
uint32_t state;
int ret;
int k_res;
SceUID pid;
SceUID kid;
ENTER_SYSCALL(state);
pid = ksceKernelGetProcessId();
if (ksceSblACMgrIsShell(0)) {
kargs.size = 0;
ksceKernelMemcpyUserToKernel(&kargs, (uintptr_t)args, sizeof(kargs));
if (kargs.size == sizeof(kargs)) {
if (kargs.args <= MAX_ARGS_SIZE && opt == NULL) {
kid = ksceKernelKernelUidForUserUid(pid, modid);
if (kid >= 0) {
ret = ksceKernelMemcpyUserToKernel(buf, (uintptr_t)kargs.argp, kargs.args);
if (ret >= 0) {
k_res = 0;
ret = ksceKernelStopModule(kid, kargs.args, buf, kargs.flags, NULL, &k_res);
if (res) {
ksceKernelMemcpyKernelToUser((uintptr_t)res, &k_res, sizeof(*res));
}
if (ret >= 0) {
ksceKernelDeleteUserUid(pid, modid);
}
}
} else {
LOG("Error getting kernel uid for %x: %x", modid, kid);
ret = kid;
}
} else {
ret = TAI_ERROR_INVALID_ARGS;
}
} else {
LOG("invalid args size: %x", kargs.size);
ret = TAI_ERROR_USER_MEMORY;
}
} else {
ret = TAI_ERROR_NOT_ALLOWED;
}
EXIT_SYSCALL(state);
return ret;
}
/**
* @brief Unloads a kernel module directly
*
* @param[in] modid The loaded module reference
* @param[in] flags The flags
* @param opt Set to `NULL`
*
* @return Zero on success, < 0 on error
* - TAI_ERROR_NOT_ALLOWED if caller does not have permission
*/
int taiUnloadKernelModule(SceUID modid, int flags, void *opt) {
uint32_t state;
SceUID pid;
SceUID kid;
int ret;
ENTER_SYSCALL(state);
pid = ksceKernelGetProcessId();
if (ksceSblACMgrIsShell(0)) {
if (opt == NULL) {
kid = ksceKernelKernelUidForUserUid(pid, modid);
if (kid >= 0) {
ret = ksceKernelUnloadModule(kid, flags, NULL);
if (ret >= 0) {
ksceKernelDeleteUserUid(pid, modid);
}
} else {
LOG("Error getting kernel uid for %x: %x", modid, kid);
ret = kid;
}
} else {
ret = TAI_ERROR_INVALID_ARGS;
}
} else {
ret = TAI_ERROR_NOT_ALLOWED;
}
EXIT_SYSCALL(state);
return ret;
}
/**
* @brief Stops and unloads a kernel module
*
* @param[in] modid The loaded module reference
* @param[in] args The arguments
* @param opt Optional arguments, set to NULL
* @param res Return value of `module_stop`
*
* @return Zero on success, < 0 on error
* - TAI_ERROR_INVALID_ARGS if `args` is too large or `opt` is not NULL
* - TAI_ERROR_NOT_ALLOWED if caller does not have permission
*/
int taiStopUnloadKernelModuleForUser(SceUID modid, tai_module_args_t *args, void *opt, int *res) {
tai_module_args_t kargs;
char buf[MAX_ARGS_SIZE];
uint32_t state;
int ret;
int k_res;
SceUID pid;
SceUID kid;
ENTER_SYSCALL(state);
pid = ksceKernelGetProcessId();
if (ksceSblACMgrIsShell(0)) {
kargs.size = 0;
ksceKernelMemcpyUserToKernel(&kargs, (uintptr_t)args, sizeof(kargs));
if (kargs.size == sizeof(kargs)) {
if (kargs.args <= MAX_ARGS_SIZE && opt == NULL) {
kid = ksceKernelKernelUidForUserUid(pid, modid);
if (kid >= 0) {
ret = ksceKernelMemcpyUserToKernel(buf, (uintptr_t)kargs.argp, kargs.args);
if (ret >= 0) {
k_res = 0;
ret = ksceKernelStopUnloadModule(kid, kargs.args, buf, kargs.flags, NULL, &k_res);
if (res) {
ksceKernelMemcpyKernelToUser((uintptr_t)res, &k_res, sizeof(*res));
}
if (ret >= 0) {
ksceKernelDeleteUserUid(pid, modid);
}
}
} else {
LOG("Error getting kernel uid for %x: %x", modid, kid);
ret = kid;
}
} else {
ret = TAI_ERROR_INVALID_ARGS;
}
} else {
LOG("invalid args size: %x", kargs.size);
ret = TAI_ERROR_USER_MEMORY;
}
} else {
ret = TAI_ERROR_NOT_ALLOWED;
}
EXIT_SYSCALL(state);
return ret;
}
/**
* @brief Stops a user module for another process
*
* @param[in] modid The loaded module reference
* @param[in] args The arguments
* @param opt Optional arguments, set to NULL
* @param res Return value of `module_stop`
*
* @return Zero on success, < 0 on error
* - TAI_ERROR_INVALID_ARGS if `args` is too large or `opt` is not NULL
* - TAI_ERROR_NOT_ALLOWED if caller does not have permission
*/
int taiStopModuleForPidForUser(SceUID modid, tai_module_args_t *args, void *opt, int *res) {
tai_module_args_t kargs;
char buf[MAX_ARGS_SIZE];
uint32_t state;
int ret;
int k_res;
SceUID kid;
ENTER_SYSCALL(state);
if (ksceSblACMgrIsShell(0)) {
kargs.size = 0;
ksceKernelMemcpyUserToKernel(&kargs, (uintptr_t)args, sizeof(kargs));
if (kargs.size == sizeof(kargs)) {
if (kargs.args <= MAX_ARGS_SIZE && opt == NULL) {
kid = ksceKernelKernelUidForUserUid(kargs.pid, modid);
if (kid >= 0) {
ret = ksceKernelMemcpyUserToKernel(buf, (uintptr_t)kargs.argp, kargs.args);
if (ret >= 0) {
k_res = 0;
ret = ksceKernelStopModuleForPid(kargs.pid, kid, kargs.args, buf, kargs.flags, NULL, &k_res);
if (res) {
ksceKernelMemcpyKernelToUser((uintptr_t)res, &k_res, sizeof(*res));
}
if (ret >= 0) {
ksceKernelDeleteUserUid(kargs.pid, modid);
}
}
} else {
LOG("Error getting kernel uid for %x: %x", modid, kid);
ret = kid;
}
} else {
ret = TAI_ERROR_INVALID_ARGS;
}
} else {
LOG("invalid args size: %x", kargs.size);
ret = TAI_ERROR_USER_MEMORY;
}
} else {
ret = TAI_ERROR_NOT_ALLOWED;
}
EXIT_SYSCALL(state);
return ret;
}
/**
* @brief Unloads a user module for a process directly
*
* @param[in] modid The loaded module reference
* @param[in] flags The flags
* @param opt Set to `NULL`
*
* @return Zero on success, < 0 on error
* - TAI_ERROR_NOT_ALLOWED if caller does not have permission
*/
int taiUnloadModuleForPid(SceUID pid, SceUID modid, int flags, void *opt) {
uint32_t state;
SceUID kid;
int ret;
ENTER_SYSCALL(state);
if (ksceSblACMgrIsShell(0)) {
if (opt == NULL) {
kid = ksceKernelKernelUidForUserUid(pid, modid);
if (kid >= 0) {
ret = ksceKernelUnloadModuleForPid(pid, kid, flags, NULL);
if (ret >= 0) {
ksceKernelDeleteUserUid(pid, modid);
}
} else {
LOG("Error getting kernel uid for %x: %x", modid, kid);
ret = kid;
}
} else {
ret = TAI_ERROR_INVALID_ARGS;
}
} else {
ret = TAI_ERROR_NOT_ALLOWED;
}
EXIT_SYSCALL(state);
return ret;
}
/**
* @brief Stops and unloads a user module for a process
*
* @param[in] modid The loaded module reference
* @param[in] args The arguments
* @param opt Optional arguments, set to NULL
* @param res Return value of `module_stop`
*
* @return Zero on success, < 0 on error
* - TAI_ERROR_INVALID_ARGS if `args` is too large or `opt` is not NULL
* - TAI_ERROR_NOT_ALLOWED if caller does not have permission
*/
int taiStopUnloadModuleForPidForUser(SceUID modid, tai_module_args_t *args, void *opt, int *res) {
tai_module_args_t kargs;
char buf[MAX_ARGS_SIZE];
uint32_t state;
int ret;
int k_res;
SceUID kid;
ENTER_SYSCALL(state);
if (ksceSblACMgrIsShell(0)) {
kargs.size = 0;
ksceKernelMemcpyUserToKernel(&kargs, (uintptr_t)args, sizeof(kargs));
if (kargs.size == sizeof(kargs)) {
if (kargs.args <= MAX_ARGS_SIZE && opt == NULL) {
kid = ksceKernelKernelUidForUserUid(kargs.pid, modid);
if (kid >= 0) {
ret = ksceKernelMemcpyUserToKernel(buf, (uintptr_t)kargs.argp, kargs.args);
if (ret >= 0) {
k_res = 0;
ret = ksceKernelStopUnloadModuleForPid(kargs.pid, kid, kargs.args, buf, kargs.flags, NULL, &k_res);
if (res) {
ksceKernelMemcpyKernelToUser((uintptr_t)res, &k_res, sizeof(*res));
}
if (ret >= 0) {
ksceKernelDeleteUserUid(kargs.pid, modid);
}
}
} else {
LOG("Error getting kernel uid for %x: %x", modid, kid);
ret = kid;
}
} else {
ret = TAI_ERROR_INVALID_ARGS;
}
} else {
LOG("invalid args size: %x", kargs.size);
ret = TAI_ERROR_USER_MEMORY;
}
} else {
ret = TAI_ERROR_NOT_ALLOWED;
}
EXIT_SYSCALL(state);
return ret;
}
/**
* @brief Gets an exported function address for a module of the calling process
*
* @param[in] modname The name of module to lookup
* @param[in] libnid NID of the exporting library. Can be `TAI_ANY_LIBRARY`.
* @param[in] funcnid NID of the exported function
* @param[out] func Output address of the function
*
* @return Zero on success, < 0 on error
*/
int taiGetModuleExportFunc(const char *modname, uint32_t libnid, uint32_t funcnid, uintptr_t *func) {
char k_module[MAX_NAME_LEN];
uintptr_t k_func;
uint32_t state;
SceUID pid;
int ret;
ENTER_SYSCALL(state);
pid = ksceKernelGetProcessId();
if (ksceKernelStrncpyUserToKernel(k_module, (uintptr_t)modname, MAX_NAME_LEN) < MAX_NAME_LEN) {
ret = module_get_export_func(pid, k_module, libnid, funcnid, &k_func);
if (ret == 0) {
ksceKernelMemcpyKernelToUser((uintptr_t)func, &k_func, sizeof(k_func));
}
} else {
ret = TAI_ERROR_USER_MEMORY;
}
EXIT_SYSCALL(state);
return ret;
}
/**
* @brief Copies data from user to kernel
*
* @param kernel_dst The kernel address
* @param[in] user_src The user address
* @param[in] len The length
*
* @return Zero on success, < 0 on error
* - TAI_ERROR_NOT_ALLOWED if caller does not have permission
*/
int taiMemcpyUserToKernel(void *kernel_dst, const void *user_src, size_t len) {
uint32_t state;
int ret;
ENTER_SYSCALL(state);
if (ksceSblACMgrIsShell(0)) {
ret = 0;
} else {
ret = TAI_ERROR_NOT_ALLOWED;
}
EXIT_SYSCALL(state);
if (ret == 0) {
return ksceKernelMemcpyUserToKernel(kernel_dst, (uintptr_t)user_src, len);
} else {
return ret;
}
}
/**
* @brief Copies data from kernel to user
*
* Does not bypass the MMU!
*
* @see taiInjectData
*
* @param user_dst The user address
* @param[in] kernel_src The kernel address
* @param[in] len The length
*
* @return Zero on success, < 0 on error
* - TAI_ERROR_NOT_ALLOWED if caller does not have permission
*/
int taiMemcpyKernelToUser(void *user_dst, const void *kernel_src, size_t len) {
uint32_t state;
int ret;
ENTER_SYSCALL(state);
if (ksceSblACMgrIsShell(0)) {
ret = 0;
} else {
ret = TAI_ERROR_NOT_ALLOWED;
}
EXIT_SYSCALL(state);
if (ret == 0) {
return ksceKernelMemcpyKernelToUser((uintptr_t)user_dst, kernel_src, len);
} else {
return ret;
}
}
/**
* @brief Reloads config.txt from the default path
*
* Note this cannot be called from a plugin start handler!
*
* @return Zero on success, < 0 on error
* - TAI_ERROR_NOT_ALLOWED if caller does not have permission
* - TAI_ERROR_BLOCKING if attempted to call recursively
*/
int taiReloadConfig(void) {
uint32_t state;
int ret;
ENTER_SYSCALL(state);
if (ksceSblACMgrIsShell(0)) {
ret = taiReloadConfigForKernel(0, 0);
} else {
ret = TAI_ERROR_NOT_ALLOWED;
}
EXIT_SYSCALL(state);
return ret;
}