1 /*
2  * Copyright (c) 2021-2023 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 "sensor_service_stub.h"
17 
18 #include <string>
19 #include <sys/socket.h>
20 #include <tokenid_kit.h>
21 #include <unistd.h>
22 #include <vector>
23 
24 #include "accesstoken_kit.h"
25 #include "hisysevent.h"
26 #include "ipc_skeleton.h"
27 #include "message_parcel.h"
28 #include "permission_util.h"
29 #include "sensor_client_proxy.h"
30 #include "sensor_errors.h"
31 #include "sensor_parcel.h"
32 
33 #undef LOG_TAG
34 #define LOG_TAG "SensorServiceStub"
35 
36 namespace OHOS {
37 namespace Sensors {
38 using namespace OHOS::HiviewDFX;
39 
SensorServiceStub()40 SensorServiceStub::SensorServiceStub() {}
41 
~SensorServiceStub()42 SensorServiceStub::~SensorServiceStub() {}
43 
ProcessRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)44 int32_t SensorServiceStub::ProcessRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply,
45     MessageOption &option)
46 {
47     switch (code) {
48         case static_cast<int32_t>(SensorInterfaceCode::ENABLE_SENSOR): {
49             return SensorEnableInner(data, reply);
50         }
51         case static_cast<int32_t>(SensorInterfaceCode::DISABLE_SENSOR): {
52             return SensorDisableInner(data, reply);
53         }
54         case static_cast<int32_t>(SensorInterfaceCode::GET_SENSOR_LIST): {
55             return GetAllSensorsInner(data, reply);
56         }
57         case static_cast<int32_t>(SensorInterfaceCode::TRANSFER_DATA_CHANNEL): {
58             return CreateDataChannelInner(data, reply);
59         }
60         case static_cast<int32_t>(SensorInterfaceCode::DESTROY_SENSOR_CHANNEL): {
61             return DestroyDataChannelInner(data, reply);
62         }
63         case static_cast<int32_t>(SensorInterfaceCode::SUSPEND_SENSORS): {
64             return SuspendSensorsInner(data, reply);
65         }
66         case static_cast<int32_t>(SensorInterfaceCode::RESUME_SENSORS): {
67             return ResumeSensorsInner(data, reply);
68         }
69         case static_cast<int32_t>(SensorInterfaceCode::GET_ACTIVE_INFO_LIST): {
70             return GetActiveInfoListInner(data, reply);
71         }
72         case static_cast<int32_t>(SensorInterfaceCode::CREATE_SOCKET_CHANNEL): {
73             return CreateSocketChannelInner(data, reply);
74         }
75         case static_cast<int32_t>(SensorInterfaceCode::DESTROY_SOCKET_CHANNEL): {
76             return DestroySocketChannelInner(data, reply);
77         }
78         case static_cast<int32_t>(SensorInterfaceCode::ENABLE_ACTIVE_INFO_CB): {
79             return EnableActiveInfoCBInner(data, reply);
80         }
81         case static_cast<int32_t>(SensorInterfaceCode::DISABLE_ACTIVE_INFO_CB): {
82             return DisableActiveInfoCBInner(data, reply);
83         }
84         case static_cast<int32_t>(SensorInterfaceCode::RESET_SENSORS): {
85             return ResetSensorsInner(data, reply);
86         }
87         default: {
88             SEN_HILOGD("No member func supporting, applying default process");
89             return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
90         }
91     }
92 }
93 
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)94 int32_t SensorServiceStub::OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply,
95                                            MessageOption &option)
96 {
97     SEN_HILOGD("Begin, cmd:%{public}u", code);
98     std::u16string descriptor = SensorServiceStub::GetDescriptor();
99     std::u16string remoteDescriptor = data.ReadInterfaceToken();
100     if (descriptor != remoteDescriptor) {
101         SEN_HILOGE("Client and service descriptors are inconsistent");
102         return OBJECT_NULL;
103     }
104     return ProcessRemoteRequest(code, data, reply, option);
105 }
106 
IsSystemServiceCalling()107 bool SensorServiceStub::IsSystemServiceCalling()
108 {
109     const auto tokenId = IPCSkeleton::GetCallingTokenID();
110     const auto flag = Security::AccessToken::AccessTokenKit::GetTokenTypeFlag(tokenId);
111     if (flag == Security::AccessToken::ATokenTypeEnum::TOKEN_NATIVE ||
112         flag == Security::AccessToken::ATokenTypeEnum::TOKEN_SHELL) {
113         SEN_HILOGD("system service calling, tokenId: %{public}u, flag: %{public}u", tokenId, flag);
114         return true;
115     }
116     return false;
117 }
118 
IsSystemCalling()119 bool SensorServiceStub::IsSystemCalling()
120 {
121     if (IsSystemServiceCalling()) {
122         return true;
123     }
124     return Security::AccessToken::TokenIdKit::IsSystemAppByFullTokenID(IPCSkeleton::GetCallingFullTokenID());
125 }
126 
SensorEnableInner(MessageParcel & data,MessageParcel & reply)127 ErrCode SensorServiceStub::SensorEnableInner(MessageParcel &data, MessageParcel &reply)
128 {
129     (void)reply;
130     int32_t sensorId;
131     READINT32(data, sensorId, READ_PARCEL_ERR);
132     if ((sensorId == SENSOR_TYPE_ID_COLOR || sensorId == SENSOR_TYPE_ID_SAR) && !IsSystemCalling()) {
133         SEN_HILOGE("Permission check failed. A non-system application uses the system API");
134         return NON_SYSTEM_API;
135     }
136     PermissionUtil &permissionUtil = PermissionUtil::GetInstance();
137     int32_t ret = permissionUtil.CheckSensorPermission(GetCallingTokenID(), sensorId);
138     if (ret != PERMISSION_GRANTED) {
139         HiSysEventWrite(HiSysEvent::Domain::SENSOR, "VERIFY_ACCESS_TOKEN_FAIL",
140             HiSysEvent::EventType::SECURITY, "PKG_NAME", "SensorEnableInner", "ERROR_CODE", ret);
141         SEN_HILOGE("sensorId:%{public}d grant failed, result:%{public}d", sensorId, ret);
142         return PERMISSION_DENIED;
143     }
144     int64_t samplingPeriodNs;
145     int64_t maxReportDelayNs;
146     READINT64(data, samplingPeriodNs, READ_PARCEL_ERR);
147     READINT64(data, maxReportDelayNs, READ_PARCEL_ERR);
148     return EnableSensor(sensorId, samplingPeriodNs, maxReportDelayNs);
149 }
150 
SensorDisableInner(MessageParcel & data,MessageParcel & reply)151 ErrCode SensorServiceStub::SensorDisableInner(MessageParcel &data, MessageParcel &reply)
152 {
153     (void)reply;
154     int32_t sensorId;
155     READINT32(data, sensorId, READ_PARCEL_ERR);
156     if ((sensorId == SENSOR_TYPE_ID_COLOR || sensorId == SENSOR_TYPE_ID_SAR) && !IsSystemCalling()) {
157         SEN_HILOGE("Permission check failed. A non-system application uses the system API");
158         return NON_SYSTEM_API;
159     }
160     PermissionUtil &permissionUtil = PermissionUtil::GetInstance();
161     int32_t ret = permissionUtil.CheckSensorPermission(GetCallingTokenID(), sensorId);
162     if (ret != PERMISSION_GRANTED) {
163         HiSysEventWrite(HiSysEvent::Domain::SENSOR, "VERIFY_ACCESS_TOKEN_FAIL",
164             HiSysEvent::EventType::SECURITY, "PKG_NAME", "SensorDisableInner", "ERROR_CODE", ret);
165         SEN_HILOGE("sensorId:%{public}d grant failed, result:%{public}d", sensorId, ret);
166         return PERMISSION_DENIED;
167     }
168     return DisableSensor(sensorId);
169 }
170 
GetAllSensorsInner(MessageParcel & data,MessageParcel & reply)171 ErrCode SensorServiceStub::GetAllSensorsInner(MessageParcel &data, MessageParcel &reply)
172 {
173     (void)data;
174     std::vector<Sensor> sensors = GetSensorList();
175     uint32_t sensorCount = static_cast<uint32_t>(sensors.size());
176     if (sensorCount > MAX_SENSOR_COUNT) {
177         SEN_HILOGD("SensorCount:%{public}u", sensorCount);
178         sensorCount = MAX_SENSOR_COUNT;
179     }
180     WRITEUINT32(reply, sensorCount, WRITE_PARCEL_ERR);
181     for (uint32_t i = 0; i < sensorCount; ++i) {
182         if (!sensors[i].Marshalling(reply)) {
183             SEN_HILOGE("Sensor %{public}u marshalling failed", i);
184             return GET_SENSOR_LIST_ERR;
185         }
186     }
187     return NO_ERROR;
188 }
189 
CreateDataChannelInner(MessageParcel & data,MessageParcel & reply)190 ErrCode SensorServiceStub::CreateDataChannelInner(MessageParcel &data, MessageParcel &reply)
191 {
192     (void)reply;
193     sptr<SensorBasicDataChannel> sensorChannel = new (std::nothrow) SensorBasicDataChannel();
194     CHKPR(sensorChannel, OBJECT_NULL);
195     auto ret = sensorChannel->CreateSensorBasicChannel(data);
196     if (ret != ERR_OK) {
197         SEN_HILOGE("CreateSensorBasicChannel ret:%{public}d", ret);
198         return OBJECT_NULL;
199     }
200     sptr<IRemoteObject> sensorClient = data.ReadRemoteObject();
201     CHKPR(sensorClient, OBJECT_NULL);
202     return TransferDataChannel(sensorChannel, sensorClient);
203 }
204 
DestroyDataChannelInner(MessageParcel & data,MessageParcel & reply)205 ErrCode SensorServiceStub::DestroyDataChannelInner(MessageParcel &data, MessageParcel &reply)
206 {
207     sptr<IRemoteObject> sensorClient = data.ReadRemoteObject();
208     CHKPR(sensorClient, OBJECT_NULL);
209     return DestroySensorChannel(sensorClient);
210 }
211 
SuspendSensorsInner(MessageParcel & data,MessageParcel & reply)212 ErrCode SensorServiceStub::SuspendSensorsInner(MessageParcel &data, MessageParcel &reply)
213 {
214     PermissionUtil &permissionUtil = PermissionUtil::GetInstance();
215     if (!permissionUtil.IsNativeToken(GetCallingTokenID())) {
216         SEN_HILOGE("TokenType is not TOKEN_NATIVE");
217         return PERMISSION_DENIED;
218     }
219     int32_t ret = permissionUtil.CheckManageSensorPermission(GetCallingTokenID());
220     if (ret != PERMISSION_GRANTED) {
221         SEN_HILOGE("Check manage sensor permission failed, ret:%{public}d", ret);
222         return PERMISSION_DENIED;
223     }
224     (void)reply;
225     int32_t pid;
226     READINT32(data, pid, READ_PARCEL_ERR);
227     return SuspendSensors(pid);
228 }
229 
ResumeSensorsInner(MessageParcel & data,MessageParcel & reply)230 ErrCode SensorServiceStub::ResumeSensorsInner(MessageParcel &data, MessageParcel &reply)
231 {
232     PermissionUtil &permissionUtil = PermissionUtil::GetInstance();
233     if (!permissionUtil.IsNativeToken(GetCallingTokenID())) {
234         SEN_HILOGE("TokenType is not TOKEN_NATIVE");
235         return PERMISSION_DENIED;
236     }
237     int32_t ret = permissionUtil.CheckManageSensorPermission(GetCallingTokenID());
238     if (ret != PERMISSION_GRANTED) {
239         SEN_HILOGE("Check manage sensor permission failed, ret:%{public}d", ret);
240         return PERMISSION_DENIED;
241     }
242     (void)reply;
243     int32_t pid;
244     READINT32(data, pid, READ_PARCEL_ERR);
245     return ResumeSensors(pid);
246 }
247 
GetActiveInfoListInner(MessageParcel & data,MessageParcel & reply)248 ErrCode SensorServiceStub::GetActiveInfoListInner(MessageParcel &data, MessageParcel &reply)
249 {
250     PermissionUtil &permissionUtil = PermissionUtil::GetInstance();
251     if (!permissionUtil.IsNativeToken(GetCallingTokenID())) {
252         SEN_HILOGE("TokenType is not TOKEN_NATIVE");
253         return PERMISSION_DENIED;
254     }
255     int32_t pid;
256     READINT32(data, pid, READ_PARCEL_ERR);
257     std::vector<ActiveInfo> activeInfoList;
258     int32_t ret = GetActiveInfoList(pid, activeInfoList);
259     if (ret != ERR_OK) {
260         SEN_HILOGE("Get activeInfo list failed");
261         return ret;
262     }
263     uint32_t activeInfoCount = static_cast<uint32_t>(activeInfoList.size());
264     if (activeInfoCount > MAX_SENSOR_COUNT) {
265         SEN_HILOGD("ActiveInfoCount:%{public}u", activeInfoCount);
266         activeInfoCount = MAX_SENSOR_COUNT;
267     }
268     WRITEUINT32(reply, activeInfoCount, WRITE_PARCEL_ERR);
269     for (uint32_t i = 0; i < activeInfoCount; ++i) {
270         if (!activeInfoList[i].Marshalling(reply)) {
271             SEN_HILOGE("ActiveInfo %{public}u marshalling failed", i);
272             return WRITE_PARCEL_ERR;
273         }
274     }
275     return ERR_OK;
276 }
277 
CreateSocketChannelInner(MessageParcel & data,MessageParcel & reply)278 ErrCode SensorServiceStub::CreateSocketChannelInner(MessageParcel &data, MessageParcel &reply)
279 {
280     PermissionUtil &permissionUtil = PermissionUtil::GetInstance();
281     if (!permissionUtil.IsNativeToken(GetCallingTokenID())) {
282         SEN_HILOGE("TokenType is not TOKEN_NATIVE");
283         return PERMISSION_DENIED;
284     }
285     sptr<IRemoteObject> sensorClient = data.ReadRemoteObject();
286     CHKPR(sensorClient, INVALID_POINTER);
287     int32_t clientFd = -1;
288     int32_t ret = CreateSocketChannel(sensorClient, clientFd);
289     if (ret != ERR_OK) {
290         SEN_HILOGE("Create socket channel failed");
291         return ret;
292     }
293     if (!reply.WriteFileDescriptor(clientFd)) {
294         SEN_HILOGE("Parcel write file descriptor failed");
295         close(clientFd);
296         return WRITE_PARCEL_ERR;
297     }
298     close(clientFd);
299     return ERR_OK;
300 }
301 
DestroySocketChannelInner(MessageParcel & data,MessageParcel & reply)302 ErrCode SensorServiceStub::DestroySocketChannelInner(MessageParcel &data, MessageParcel &reply)
303 {
304     PermissionUtil &permissionUtil = PermissionUtil::GetInstance();
305     if (!permissionUtil.IsNativeToken(GetCallingTokenID())) {
306         SEN_HILOGE("TokenType is not TOKEN_NATIVE");
307         return PERMISSION_DENIED;
308     }
309     sptr<IRemoteObject> sensorClient = data.ReadRemoteObject();
310     CHKPR(sensorClient, INVALID_POINTER);
311     int32_t ret = DestroySocketChannel(sensorClient);
312     if (ret != ERR_OK) {
313         SEN_HILOGE("Destroy socket channel failed");
314         return ret;
315     }
316     return ERR_OK;
317 }
318 
EnableActiveInfoCBInner(MessageParcel & data,MessageParcel & reply)319 ErrCode SensorServiceStub::EnableActiveInfoCBInner(MessageParcel &data, MessageParcel &reply)
320 {
321     PermissionUtil &permissionUtil = PermissionUtil::GetInstance();
322     if (!permissionUtil.IsNativeToken(GetCallingTokenID())) {
323         SEN_HILOGE("TokenType is not TOKEN_NATIVE");
324         return PERMISSION_DENIED;
325     }
326     return EnableActiveInfoCB();
327 }
328 
DisableActiveInfoCBInner(MessageParcel & data,MessageParcel & reply)329 ErrCode SensorServiceStub::DisableActiveInfoCBInner(MessageParcel &data, MessageParcel &reply)
330 {
331     PermissionUtil &permissionUtil = PermissionUtil::GetInstance();
332     if (!permissionUtil.IsNativeToken(GetCallingTokenID())) {
333         SEN_HILOGE("TokenType is not TOKEN_NATIVE");
334         return PERMISSION_DENIED;
335     }
336     return DisableActiveInfoCB();
337 }
338 
ResetSensorsInner(MessageParcel & data,MessageParcel & reply)339 ErrCode SensorServiceStub::ResetSensorsInner(MessageParcel &data, MessageParcel &reply)
340 {
341     PermissionUtil &permissionUtil = PermissionUtil::GetInstance();
342     if (!permissionUtil.IsNativeToken(GetCallingTokenID())) {
343         SEN_HILOGE("TokenType is not TOKEN_NATIVE");
344         return PERMISSION_DENIED;
345     }
346     int32_t ret = permissionUtil.CheckManageSensorPermission(GetCallingTokenID());
347     if (ret != PERMISSION_GRANTED) {
348         SEN_HILOGE("Check manage sensor permission failed, ret:%{public}d", ret);
349         return PERMISSION_DENIED;
350     }
351     return ResetSensors();
352 }
353 } // namespace Sensors
354 } // namespace OHOS
355