1 /*
2  * Copyright (c) 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 "domain_account_callback_stub.h"
17 
18 #include <securec.h>
19 #include "account_log_wrapper.h"
20 #include "ipc_skeleton.h"
21 
22 namespace OHOS {
23 namespace AccountSA {
24 namespace {
25 const unsigned int DOMAIN_DATA_MAX_SIZE = 6144; // os account and domain account property limits addition
26 }
27 
DomainAccountCallbackStub()28 DomainAccountCallbackStub::DomainAccountCallbackStub()
29 {}
30 
~DomainAccountCallbackStub()31 DomainAccountCallbackStub::~DomainAccountCallbackStub()
32 {}
33 
34 
OnRemoteRequest(std::uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)35 int32_t DomainAccountCallbackStub::OnRemoteRequest(
36     std::uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
37 {
38     ACCOUNT_LOGI("Received stub message: %{public}d, callingUid: %{public}d", code, IPCSkeleton::GetCallingUid());
39     if (data.ReadInterfaceToken() != GetDescriptor()) {
40         ACCOUNT_LOGE(
41             "check descriptor failed! code %{public}u, callingUid: %{public}d", code, IPCSkeleton::GetCallingUid());
42         return ERR_ACCOUNT_COMMON_CHECK_DESCRIPTOR_ERROR;
43     }
44     return ProcOnResult(data, reply);
45 }
46 
ProcOnResult(MessageParcel & data,MessageParcel & reply)47 int32_t DomainAccountCallbackStub::ProcOnResult(MessageParcel &data, MessageParcel &reply)
48 {
49     int32_t result;
50     if (!data.ReadInt32(result)) {
51         ACCOUNT_LOGE("failed to read result");
52         return ERR_ACCOUNT_COMMON_READ_PARCEL_ERROR;
53     }
54     uint32_t size;
55     if (!data.ReadUint32(size)) {
56         ACCOUNT_LOGE("failed to read size");
57         return ERR_ACCOUNT_COMMON_READ_PARCEL_ERROR;
58     }
59     if (size > DOMAIN_DATA_MAX_SIZE) {
60         return ERR_ACCOUNT_COMMON_INVALID_PARAMETER;
61     }
62     const uint8_t *buffer = data.ReadBuffer(size);
63     if (buffer == nullptr) {
64         ACCOUNT_LOGE("failed to read buffer");
65         return ERR_ACCOUNT_COMMON_READ_PARCEL_ERROR;
66     }
67     void *buffer_new = nullptr;
68     buffer_new = malloc(size);
69     if (buffer_new == nullptr) {
70         return ERR_ACCOUNT_COMMON_INSUFFICIENT_MEMORY_ERROR;
71     }
72     if (memcpy_s(buffer_new, size, buffer, size) != EOK) {
73         free(buffer_new);
74         buffer_new = nullptr;
75         return ERR_ACCOUNT_COMMON_INSUFFICIENT_MEMORY_ERROR;
76     }
77     Parcel parcel;
78     if (!parcel.ParseFrom(reinterpret_cast<uintptr_t>(buffer_new), size)) {
79         ACCOUNT_LOGE("failed to parse from");
80         free(buffer_new);
81         buffer_new = nullptr;
82         return ERR_ACCOUNT_COMMON_READ_PARCEL_ERROR;
83     }
84     OnResult(result, parcel);
85     return ERR_NONE;
86 }
87 }  // namespace AccountSA
88 }  // namespace OHOS
89