1 /*
2 * Copyright (c) 2024 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include <stdio.h>
17 #include <syscall.h>
18 #include <assert.h>
19 #include <syscall_hooks.h>
20
21 #include "libc.h"
22 #include "pthread_impl.h"
23
24 #define HIJACK_SYSCALL_MAX 1024
25
26 volatile const char *g_syscall_hooks_table __attribute__((aligned(8))) = NULL;
27 volatile void *g_syscall_hooks_entry __attribute__((aligned(8))) = NULL;
28
29 /*
30 * @brief
31 * reset == 0: g_syscall_hooks_table and g_syscall_hooks_entry will be the function
32 * input parameters hooks_table and hooks_entry.
33 * reset != 0: g_syscall_hooks_table and g_syscall_hooks_entry will be default values
34 *
35 * @param
36 * hooks_table: pointer to a syscall table
37 * hooks_entry: pointer to svc0_entry
38 *
39 * @return
40 * -EINVAL: input parameters are invalid
41 * -EPERM: operation not allowed
42 * 0: set_syscall_hooks success
43 */
44
set_syscall_hooks(const char * hooks_table,int table_len,void * hooks_entry,int reset,int ** tid_addr)45 int set_syscall_hooks(const char *hooks_table, int table_len, void *hooks_entry, int reset, int **tid_addr)
46 {
47 int ret = 0;
48
49 if (reset == 0) {
50 if (hooks_table == NULL || hooks_entry == NULL || tid_addr == NULL ||
51 table_len < HIJACK_SYSCALL_MAX) {
52 ret = -EINVAL;
53 }
54 }
55
56 if (ret == 0) {
57 __tl_lock();
58 if (!libc.threads_minus_1) {
59 if (reset) {
60 g_syscall_hooks_entry = NULL;
61 g_syscall_hooks_table = NULL;
62 } else {
63 g_syscall_hooks_entry = hooks_entry;
64 g_syscall_hooks_table = hooks_table;
65 }
66 *tid_addr = &__thread_list_lock;
67 } else {
68 ret = -EPERM;
69 }
70 __tl_unlock();
71 }
72
73 return ret;
74 }