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 "dlp_link_file.h"
17 
18 #include <securec.h>
19 #include "dlp_permission.h"
20 #include "dlp_permission_log.h"
21 #include "fuse_daemon.h"
22 
23 namespace OHOS {
24 namespace Security {
25 namespace DlpPermission {
26 namespace {
27 static constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {LOG_CORE, SECURITY_DOMAIN_DLP_PERMISSION, "DlpLinkFile"};
28 static const int DEFAULT_INODE_RO_ACCESS = 0440;
29 static const int DEFAULT_INODE_RW_ACCESS = 0640;
30 } // namespace
31 
DlpLinkFile(std::string dlpLinkName,std::shared_ptr<DlpFile> dlpFile)32 DlpLinkFile::DlpLinkFile(std::string dlpLinkName, std::shared_ptr<DlpFile> dlpFile)
33     : dlpLinkName_(dlpLinkName), dlpFile_(dlpFile), refcount_(1), stopLinkFlag_(false), hasRead_(false)
34 {
35     (void)memset_s(&fileStat_, sizeof(fileStat_), 0, sizeof(fileStat_));
36     fileStat_.st_ino = GetFileInode(this);
37     if (dlpFile != nullptr) {
38         uint32_t fileMode = (dlpFile->GetAuthPerm() == READ_ONLY) ? DEFAULT_INODE_RO_ACCESS : DEFAULT_INODE_RW_ACCESS;
39         fileStat_.st_mode = S_IFREG | fileMode;
40     } else {
41         fileStat_.st_mode = 0;
42     }
43     fileStat_.st_nlink = 1;
44     fileStat_.st_uid = getuid();
45     fileStat_.st_gid = getgid();
46 
47     UpdateCurrTimeStat(&fileStat_.st_atim);
48     UpdateCurrTimeStat(&fileStat_.st_mtim);
49     UpdateCurrTimeStat(&fileStat_.st_ctim);
50 }
51 
~DlpLinkFile()52 DlpLinkFile::~DlpLinkFile()
53 {
54 }
55 
SubAndCheckZeroRef(int ref)56 bool DlpLinkFile::SubAndCheckZeroRef(int ref)
57 {
58     if (ref <= 0) {
59         DLP_LOG_WARN(LABEL, "Need sub reference %{public}d is error", ref);
60         return false;
61     }
62     std::lock_guard<std::mutex> lock(refLock_);
63     if (refcount_ < ref) {
64         DLP_LOG_WARN(LABEL, "Need sub reference %{public}d is larger than refcount %{public}d",
65             ref, static_cast<int>(refcount_));
66         return true;
67     }
68     refcount_ -= ref;
69     return (refcount_ <= 0);
70 }
71 
IncreaseRef()72 void DlpLinkFile::IncreaseRef()
73 {
74     std::lock_guard<std::mutex> lock(refLock_);
75     if (refcount_ <= 0) {
76         DLP_LOG_WARN(LABEL, "refcount <= 0, can not increase");
77         return;
78     }
79     refcount_++;
80 }
81 
GetLinkStat()82 struct stat DlpLinkFile::GetLinkStat()
83 {
84     if (dlpFile_ == nullptr) {
85         DLP_LOG_ERROR(LABEL, "Get link file stat fail, dlpFile is null");
86         return fileStat_;
87     }
88 
89     uint32_t res = dlpFile_->GetFsContentSize();
90     if (res != INVALID_FILE_SIZE) {
91         fileStat_.st_size = res;
92     }
93     return fileStat_;
94 }
95 
Truncate(uint32_t modifySize)96 int32_t DlpLinkFile::Truncate(uint32_t modifySize)
97 {
98     if (stopLinkFlag_) {
99         DLP_LOG_INFO(LABEL, "linkFile is stopping link");
100         return DLP_LINK_FILE_NOT_ALLOW_OPERATE;
101     }
102 
103     if (modifySize >= DLP_MAX_CONTENT_SIZE) {
104         DLP_LOG_ERROR(LABEL, "Truncate link file fail, modify size %{public}u is invalid", modifySize);
105         return DLP_FUSE_ERROR_VALUE_INVALID;
106     }
107 
108     if (dlpFile_ == nullptr) {
109         DLP_LOG_ERROR(LABEL, "Truncate link file fail, dlp file is null");
110         return DLP_FUSE_ERROR_DLP_FILE_NULL;
111     }
112     int32_t res = dlpFile_->Truncate(modifySize);
113     if (res < 0) {
114         DLP_LOG_ERROR(LABEL, "Truncate %{public}u in link file fail, res=%{public}d", modifySize, res);
115     } else {
116         DLP_LOG_INFO(LABEL, "Truncate %{public}u in link file succ", modifySize);
117     }
118     UpdateMtimeStat();
119     return res;
120 }
121 
UpdateAtimeStat()122 void DlpLinkFile::UpdateAtimeStat()
123 {
124     UpdateCurrTimeStat(&fileStat_.st_atim);
125 }
126 
UpdateMtimeStat()127 void DlpLinkFile::UpdateMtimeStat()
128 {
129     UpdateCurrTimeStat(&fileStat_.st_mtim);
130 }
131 
Write(uint32_t offset,void * buf,uint32_t size)132 int32_t DlpLinkFile::Write(uint32_t offset, void* buf, uint32_t size)
133 {
134     if (stopLinkFlag_) {
135         DLP_LOG_INFO(LABEL, "linkFile is stopping link");
136         return DLP_LINK_FILE_NOT_ALLOW_OPERATE;
137     }
138 
139     if (dlpFile_ == nullptr) {
140         DLP_LOG_ERROR(LABEL, "Write link file fail, dlp file is null");
141         return DLP_FUSE_ERROR_DLP_FILE_NULL;
142     }
143     int32_t res = dlpFile_->DlpFileWrite(offset, buf, size);
144     if (res < 0) {
145         DLP_LOG_ERROR(LABEL, "Write link file fail, err=%{public}d.", res);
146     }
147     UpdateMtimeStat();
148     return res;
149 }
150 
Read(uint32_t offset,void * buf,uint32_t size,uint32_t uid)151 int32_t DlpLinkFile::Read(uint32_t offset, void* buf, uint32_t size, uint32_t uid)
152 {
153     if (stopLinkFlag_) {
154         DLP_LOG_INFO(LABEL, "linkFile is stopping link");
155         return DLP_LINK_FILE_NOT_ALLOW_OPERATE;
156     }
157 
158     if (dlpFile_ == nullptr) {
159         DLP_LOG_ERROR(LABEL, "Read link file fail, dlp file is null");
160         return DLP_FUSE_ERROR_DLP_FILE_NULL;
161     }
162     UpdateAtimeStat();
163     int32_t res = dlpFile_->DlpFileRead(offset, buf, size, hasRead_, uid);
164     if (res < 0) {
165         DLP_LOG_ERROR(LABEL, "Read link file failed, res %{public}d.", res);
166     }
167     return res;
168 }
169 }  // namespace DlpPermission
170 }  // namespace Security
171 }  // namespace OHOS
172