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 "crypto_delay_handler.h"
17 
18 #include <pthread.h>
19 
20 #include "storage_service_log.h"
21 #include "storage_service_errno.h"
22 #include "utils/storage_radar.h"
23 
24 using namespace OHOS::StorageService;
25 namespace OHOS {
26 namespace StorageDaemon {
27 constexpr int32_t WAIT_THREAD_TIMEOUT_MS = 5;
28 constexpr int32_t DEFAULT_CHECK_INTERVAL = 10 * 1000; // 10s
29 const std::string CLEAR_TASK_NAME = "clear_ece_sece_key";
30 
DelayHandler(uint32_t userId)31 DelayHandler::DelayHandler(uint32_t userId) : userId_(userId) {}
~DelayHandler()32 DelayHandler::~DelayHandler()
33 {
34     LOGD("DelayHandler Destructor.");
35     std::unique_lock<std::mutex> lock(eventMutex_);
36     if ((eventHandler_ != nullptr) && (eventHandler_->GetEventRunner() != nullptr)) {
37         eventHandler_->RemoveAllEvents();
38         eventHandler_->GetEventRunner()->Stop();
39     }
40     if (eventThread_.joinable()) {
41         eventThread_.join();
42     }
43     eventHandler_ = nullptr;
44     LOGI("success");
45 }
46 
StartDelayTask(std::shared_ptr<BaseKey> & elKey)47 void DelayHandler::StartDelayTask(std::shared_ptr<BaseKey> &elKey)
48 {
49     CancelDelayTask();
50     if (elKey == nullptr) {
51         LOGI("elKey is nullptr do not clean.");
52         return;
53     }
54     el4Key_ = elKey;
55 
56     LOGI("StartDelayTask, start delay clear key task.");
57     std::unique_lock<std::mutex> lock(eventMutex_);
58     if (eventHandler_ == nullptr) {
59         eventThread_ = std::thread(&DelayHandler::StartDelayHandler, this);
60         eventCon_.wait_for(lock, std::chrono::seconds(WAIT_THREAD_TIMEOUT_MS), [this] {
61             return eventHandler_ != nullptr;
62         });
63     }
64 
65     auto executeFunc = [this] { ClearEceSeceKey(); };
66     eventHandler_->PostTask(executeFunc, CLEAR_TASK_NAME, DEFAULT_CHECK_INTERVAL,
67                             AppExecFwk::EventHandler::Priority::IMMEDIATE);
68     LOGI("success");
69 }
70 
StartDelayHandler()71 void DelayHandler::StartDelayHandler()
72 {
73     pthread_setname_np(pthread_self(), "storage_monitor_task_event");
74     auto runner = AppExecFwk::EventRunner::Create(false);
75     if (runner == nullptr) {
76         LOGE("event runner is nullptr.");
77         return;
78     }
79     {
80         std::lock_guard<std::mutex> lock(eventMutex_);
81         eventHandler_ = std::make_shared<AppExecFwk::EventHandler>(runner);
82     }
83     eventCon_.notify_one();
84     runner->Run();
85     LOGI("success");
86 }
87 
CancelDelayTask()88 void DelayHandler::CancelDelayTask()
89 {
90     LOGI("enter");
91     if (eventHandler_ == nullptr) {
92         LOGE("eventHandler_ is nullptr !");
93         return;
94     }
95     eventHandler_->RemoveTask(CLEAR_TASK_NAME);
96     LOGI("success");
97 }
98 
ClearEceSeceKey()99 void DelayHandler::ClearEceSeceKey()
100 {
101     LOGI("enter");
102     if (el4Key_ == nullptr) {
103         LOGI("elKey is nullptr do not clean.");
104         StorageRadar::ReportUpdateUserAuth("ClearEceKey", userId_, E_PARAMS_INVAL, "EL4", "");
105         return;
106     }
107     if (!el4Key_->LockUserScreen(userId_, FSCRYPT_SDP_ECE_CLASS)) {
108         LOGE("Clear user %{public}u key failed", userId_);
109         StorageRadar::ReportUpdateUserAuth("ClearEceSeceKey::LockUserScreen", userId_, E_SYS_ERR, "EL4", "");
110         return;
111     }
112     LOGI("success");
113 }
114 } // StorageDaemon
115 } // OHOS