xref: /aosp_15_r20/trusty/kernel/platform/generic-arm64/smc_service_access_policy.c (revision 344aa361028b423587d4ef3fa52a23d194628137)
1 /*
2  * Copyright (c) 2019 Google Inc. All rights reserved
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining
5  * a copy of this software and associated documentation files
6  * (the "Software"), to deal in the Software without restriction,
7  * including without limitation the rights to use, copy, modify, merge,
8  * publish, distribute, sublicense, and/or sell copies of the Software,
9  * and to permit persons to whom the Software is furnished to do so,
10  * subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice shall be
13  * included in all copies or substantial portions of the Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
18  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
19  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
20  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
21  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22  */
23 
24 #include <err.h>
25 #include <interface/smc/smc.h>
26 #include <lib/smc/smc_test.h>
27 #include <lib/trusty/trusty_app.h>
28 #include <services/smc/acl.h>
29 #include <stdbool.h>
30 #include <string.h>
31 #include <uapi/trusty_uuid.h>
32 
33 /* SMC test UUID : {3c321776-548e-4978-b676-843cbf1073e5} */
34 static struct uuid smc_test_uuid = {
35         0x3c321776,
36         0x548e,
37         0x4978,
38         {0xb6, 0x76, 0x84, 0x3c, 0xbf, 0x10, 0x73, 0xe5},
39 };
40 
equal_uuid(const struct uuid * a,const struct uuid * b)41 static bool equal_uuid(const struct uuid* a, const struct uuid* b) {
42     return memcmp(a, b, sizeof(struct uuid)) == 0;
43 }
44 
smc_test_access_policy(uint32_t smc_nr)45 static int smc_test_access_policy(uint32_t smc_nr) {
46     /*
47      * SMC_FC_ECHO_ONE_ARG and SMC_FC64_ECHO_ONE_ARG used to test that we
48      * can limit access to smcs.
49      */
50     if (smc_nr == SMC_FC_ECHO_ONE_ARG || smc_nr == SMC_FC64_ECHO_ONE_ARG)
51         return ERR_ACCESS_DENIED;
52 
53     /* SMC test has unrestricted access to all other SMCs. */
54     return NO_ERROR;
55 }
56 
default_access_policy(uint32_t smc_nr)57 static int default_access_policy(uint32_t smc_nr) {
58     return ERR_ACCESS_DENIED;
59 }
60 
smc_test_request_check(uint32_t smc_nr,const struct uuid * uuid,const struct smc_msg * request)61 static int smc_test_request_check(uint32_t smc_nr,
62                                   const struct uuid* uuid,
63                                   const struct smc_msg* request) {
64     if ((smc_nr == SMC_FC_ECHO_THREE_ARGS ||
65          smc_nr == SMC_FC64_ECHO_THREE_ARGS) &&
66         request->params[1] != SMC_ACCESS_CONTROL_ALLOW_ARGS) {
67         /*
68          * SMC_FC64_ECHO_THREE_ARGS is used to test that we can validate the
69          * arguments of a smcall.
70          */
71         if (request->params[1] == SMC_ACCESS_CONTROL_VALIDATE_ARGS) {
72             /*
73              * If first param is SMC_ACCESS_CONTROL_VALIDATE_ARGS, second param
74              * must be a physical address that requesting app prepared for dma.
75              */
76             paddr_t paddr = request->params[2];
77             if (!paddr || !trusty_uuid_dma_is_allowed(uuid, paddr)) {
78                 return ERR_INVALID_ARGS;
79             }
80             return NO_ERROR;
81         }
82         return ERR_INVALID_ARGS;
83     }
84 
85     /* SMC test is allowed to make any other request. */
86     return NO_ERROR;
87 }
88 
default_request_check(uint32_t smc_nr,const struct uuid * uuid,const struct smc_msg * request)89 static int default_request_check(uint32_t smc_nr,
90                                  const struct uuid* uuid,
91                                  const struct smc_msg* request) {
92     return ERR_ACCESS_DENIED;
93 }
94 
smc_load_access_policy(const struct uuid * uuid,struct smc_access_policy * policy)95 void smc_load_access_policy(const struct uuid* uuid,
96                             struct smc_access_policy* policy) {
97     /* On QEMU builds, only SMC test can have access to SMC service. */
98     if (equal_uuid(uuid, &smc_test_uuid)) {
99         policy->check_access = smc_test_access_policy;
100         policy->check_request = smc_test_request_check;
101         return;
102     }
103     policy->check_access = default_access_policy;
104     policy->check_request = default_request_check;
105 }
106