• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #define LOG_TAG "DataBaseUtilsAcl"
17 #include "acl.h"
18 
19 #include "securec.h"
20 #include <cerrno>
21 #include <cstring>
22 #include <dlfcn.h>
23 #include <functional>
24 #include <iosfwd>
25 #include <memory>
26 #include <new>
27 #include <sys/stat.h>
28 #include <sys/xattr.h>
29 #include <type_traits>
30 #include "log_print.h"
31 
32 
33 namespace OHOS {
34 namespace DATABASE_UTILS {
35 using namespace DistributedKv;
36 static constexpr int32_t HEAD_SIZE = 3;
37 static constexpr int32_t END_SIZE = 3;
38 static constexpr int32_t MIN_SIZE = 9;
39 static constexpr const char *REPLACE_CHAIN = "***";
40 static constexpr const char *DEFAULT_ANONYMOUS = "******";
Acl(const std::string & path)41 Acl::Acl(const std::string &path) : path_(path), hasError_(false)
42 {
43     /* init acl from file's defaule or mode*/
44     AclFromDefault();
45 }
46 
Acl()47 Acl::Acl()
48 {
49 }
50 
ReCalcMaskPerm()51 ACL_PERM Acl::ReCalcMaskPerm()
52 {
53     ACL_PERM perm;
54     for (const auto &e : entries_) {
55         if (e.tag_ == ACL_TAG::USER || e.tag_ == ACL_TAG::GROUP_OBJ || e.tag_ == ACL_TAG::GROUP) {
56             perm.Merge(e.perm_);
57         }
58     }
59     return perm;
60 }
61 
IsEmpty()62 bool Acl::IsEmpty()
63 {
64     return entries_.empty();
65 }
66 
CompareInsertEntry(const AclXattrEntry & entry)67 void Acl::CompareInsertEntry(const AclXattrEntry &entry)
68 {
69     if (entries_.count(entry)) {
70         auto it = entries_.find(entry);
71         entries_.erase(it);
72     }
73     bool isNecessary = (entry.tag_ == ACL_TAG::USER_OBJ ||
74                         entry.tag_ == ACL_TAG::GROUP_OBJ ||
75                         entry.tag_ == ACL_TAG::OTHER);
76     if (isNecessary || entry.perm_.IsReadable() || entry.perm_.IsWritable() || entry.perm_.IsExecutable()) {
77         entries_.insert(entry);
78     }
79 }
80 
InsertEntry(const AclXattrEntry & entry)81 int Acl::InsertEntry(const AclXattrEntry &entry)
82 {
83     if (entries_.size() >= ENTRIES_MAX_NUM) {
84         return E_ERROR;
85     }
86     CompareInsertEntry(entry); // must before ReCalcMaskPerm()
87 
88     maskDemand_++;
89     /*
90     * In either case there's no or already one ACL_MASK entry in the set,
91     * we need to re-calculate MASK's permission and *insert* it (to replace
92     * the old one in latter case since we can't change std::set's element
93     * in-place). So do the following unconditionally.
94     *
95     * Be warned: do _NOT_ combine the following into one line, otherwise
96     * you can't pass the !!genius!! CI coding style check.
97     */
98     CompareInsertEntry(AclXattrEntry(ACL_TAG::MASK, AclXattrEntry::ACL_UNDEFINED_ID, ReCalcMaskPerm()));
99     return E_OK;
100 }
101 
Serialize(uint32_t & bufSize)102 std::unique_ptr<char[]> Acl::Serialize(uint32_t &bufSize)
103 {
104     bufSize = sizeof(AclXattrHeader) + sizeof(AclXattrEntry) * entries_.size();
105     if (bufSize > BUF_MAX_SIZE) {
106         bufSize = 0;
107         return nullptr;
108     }
109     auto buf = std::make_unique<char[]>(bufSize);
110     auto err = memcpy_s(buf.get(), bufSize, &header_, sizeof(AclXattrHeader));
111     if (err != EOK) {
112         bufSize = 0;
113         return nullptr;
114     }
115 
116     int32_t restSize = static_cast<int32_t>(bufSize - sizeof(AclXattrHeader));
117     AclXattrEntry *ptr = reinterpret_cast<AclXattrEntry *>(buf.get() + sizeof(AclXattrHeader));
118     for (const auto &e : entries_) {
119         auto err = memcpy_s(ptr++, restSize, &e, sizeof(AclXattrEntry));
120         if (err != EOK) {
121             bufSize = 0;
122             return nullptr;
123         }
124         restSize -= sizeof(AclXattrEntry);
125     }
126     return buf;
127 }
128 
DeSerialize(const char * p,int32_t bufSize)129 int Acl::DeSerialize(const char *p, int32_t bufSize)
130 {
131     header_ = *reinterpret_cast<const AclXattrHeader *>(p);
132     bufSize -= sizeof(AclXattrHeader);
133     p += sizeof(AclXattrHeader);
134 
135     /*
136      * `e->tag != ACL_TAG::UNDEFINED` is unreliable outside the buffer, so check
137      * it after checking the size of remaining buffer.
138      */
139     for (const AclXattrEntry *e = reinterpret_cast<const AclXattrEntry *>(p);
140             bufSize >= static_cast<int32_t>(sizeof(AclXattrEntry)) && e->tag_ != ACL_TAG::UNDEFINED;
141             e++) {
142         InsertEntry(*e);
143         bufSize -= sizeof(AclXattrEntry);
144     }
145     if (bufSize < 0) {
146         entries_.clear();
147         header_ = { 0 };
148         return -1;
149     }
150 
151     return 0;
152 }
153 
AclFromDefault()154 void Acl::AclFromDefault()
155 {
156     char buf[BUF_SIZE] = { 0 };
157     ssize_t len = getxattr(path_.c_str(), ACL_XATTR_DEFAULT, buf, BUF_SIZE);
158     if (len != -1) {
159         DeSerialize(buf, BUF_SIZE);
160     } else if (errno == ENODATA) {
161         AclFromMode();
162     } else {
163         hasError_ = true;
164         ZLOGW("The getxattr failed. error %{public}s path %{public}s", std::strerror(errno), Anonymous(path_).c_str());
165     }
166 }
167 
AclFromMode()168 void Acl::AclFromMode()
169 {
170     struct stat st;
171     if (stat(path_.c_str(), &st) == -1) {
172         return;
173     }
174 
175     InsertEntry(AclXattrEntry(ACL_TAG::USER_OBJ, AclXattrEntry::ACL_UNDEFINED_ID,
176         (st.st_mode & S_IRWXU) >> USER_OFFSET));
177     InsertEntry(AclXattrEntry(ACL_TAG::GROUP_OBJ, AclXattrEntry::ACL_UNDEFINED_ID,
178         (st.st_mode & S_IRWXG) >> GROUP_OFFSET));
179     InsertEntry(AclXattrEntry(ACL_TAG::OTHER, AclXattrEntry::ACL_UNDEFINED_ID,
180         (st.st_mode & S_IRWXO)));
181 }
182 
SetDefault()183 int32_t Acl::SetDefault()
184 {
185     if (IsEmpty()) {
186         ZLOGE("Failed to generate ACL from file's mode: %{public}s", std::strerror(errno));
187         return E_ERROR;
188     }
189 
190     /* transform to binary and write to file */
191     uint32_t bufSize;
192     auto buf = Serialize(bufSize);
193     if (buf == nullptr) {
194         ZLOGE("Failed to serialize ACL into binary: %{public}s", std::strerror(errno));
195         return E_ERROR;
196     }
197     if (setxattr(path_.c_str(), ACL_XATTR_DEFAULT, buf.get(), bufSize, 0) == -1) {
198         ZLOGE("Failed to write into file's xattr: %{public}s", std::strerror(errno));
199         return E_ERROR;
200     }
201     return E_OK;
202 }
203 
SetDefaultGroup(const uint32_t gid,const uint16_t mode)204 int32_t Acl::SetDefaultGroup(const uint32_t gid, const uint16_t mode)
205 {
206     return InsertEntry(AclXattrEntry(ACL_TAG::GROUP, gid, mode));
207 }
208 
SetDefaultUser(const uint32_t uid,const uint16_t mode)209 int32_t Acl::SetDefaultUser(const uint32_t uid, const uint16_t mode)
210 {
211     return InsertEntry(AclXattrEntry(ACL_TAG::USER, uid, mode));
212 }
213 
~Acl()214 Acl::~Acl()
215 {
216     if (!hasError_) {
217         SetDefault();
218     }
219 }
220 
HasEntry(const AclXattrEntry & Acl)221 bool Acl::HasEntry(const AclXattrEntry &Acl)
222 {
223     return entries_.find(Acl) != entries_.end();
224 }
225 
Anonymous(const std::string & name)226 std::string Acl::Anonymous(const std::string &name)
227 {
228     if (name.length() <= HEAD_SIZE) {
229         return DEFAULT_ANONYMOUS;
230     }
231 
232     if (name.length() < MIN_SIZE) {
233         return (name.substr(0, HEAD_SIZE) + REPLACE_CHAIN);
234     }
235 
236     return (name.substr(0, HEAD_SIZE) + REPLACE_CHAIN + name.substr(name.length() - END_SIZE, END_SIZE));
237 }
238 }
239 }