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