1/*
2 * Copyright (c) 2022 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#include <string.h>
16#include "init_module_engine.h"
17#include "plugin_adapter.h"
18#include "seccomp_policy.h"
19
20#define SA_MAIN_PATH ("/system/bin/sa_main")
21
22static int SetSystemSeccompPolicy(int id, const char *name, int argc, const char **argv)
23{
24    PLUGIN_CHECK(argc == 2, return -1, "Invalid parameter");
25    SeccompFilterType type = SYSTEM_SA;
26    if (strncmp(SA_MAIN_PATH, argv[1], strlen(SA_MAIN_PATH)) != 0) {
27        type = SYSTEM_OTHERS;
28    }
29    bool ret = SetSeccompPolicyWithName(type, argv[0]);
30    PLUGIN_CHECK(ret == true, return -1;
31                 exit(PROCESS_EXIT_CODE), "Set system seccomp spolicy failed and exit");
32
33    return 0;
34}
35
36static int32_t g_executorId = -1;
37static int SetSeccompPolicyInit(void)
38{
39    if (g_executorId == -1) {
40        g_executorId = AddCmdExecutor("SetSeccompPolicy", SetSystemSeccompPolicy);
41    }
42    return 0;
43}
44
45static int SeccompHook(const HOOK_INFO *info, void *cookie)
46{
47    SetSeccompPolicyInit();
48    PLUGIN_LOGI("seccomp enabled.");
49    return 0;
50}
51
52MODULE_CONSTRUCTOR(void)
53{
54    InitAddPostCfgLoadHook(0, SeccompHook);
55}
56