1 /*
2 * Copyright (C) 2021 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 "common/random_access_file.h"
17
18 #include <cerrno>
19 #include <fcntl.h>
20 #include <string>
21 #include <sys/mman.h>
22 #include <unistd.h>
23
24 #include "common/hap_verify_log.h"
25 #include "securec.h"
26 #include "util/hap_verify_openssl_utils.h"
27
28 namespace OHOS {
29 namespace Security {
30 namespace Verify {
31 const int RandomAccessFile::FILE_OPEN_FAIL_ERROR_NUM = -1;
32 int RandomAccessFile::memoryPageSize = sysconf(_SC_PAGESIZE);
33
RandomAccessFile()34 RandomAccessFile::RandomAccessFile()
35 : fd(FILE_OPEN_FAIL_ERROR_NUM), fileLength(0)
36 {
37 }
38
~RandomAccessFile()39 RandomAccessFile::~RandomAccessFile()
40 {
41 if (fd != FILE_OPEN_FAIL_ERROR_NUM) {
42 close(fd);
43 }
44 }
45
Init(const std::string & filePath)46 bool RandomAccessFile::Init(const std::string& filePath)
47 {
48 fd = open(filePath.c_str(), O_RDONLY);
49 if (fd == FILE_OPEN_FAIL_ERROR_NUM) {
50 return false;
51 }
52
53 if (memoryPageSize <= 0) {
54 HAPVERIFY_LOG_ERROR(LABEL, "getting pagesize failed: %{public}d", memoryPageSize);
55 return false;
56 }
57
58 fileLength = lseek(fd, 0, SEEK_END);
59 if (fileLength < 0) {
60 HAPVERIFY_LOG_ERROR(LABEL, "getting fileLength failed: %{public}lld", fileLength);
61 return false;
62 }
63 return true;
64 }
65
GetLength() const66 long long RandomAccessFile::GetLength() const
67 {
68 return fileLength;
69 }
70
DoMMap(int bufCapacity,long long offset,MmapInfo & mmapInfo)71 long long RandomAccessFile::DoMMap(int bufCapacity, long long offset, MmapInfo& mmapInfo)
72 {
73 mmapInfo.mapAddr = reinterpret_cast<char*>(MAP_FAILED);
74 if (fd == FILE_OPEN_FAIL_ERROR_NUM) {
75 return FILE_IS_CLOSE;
76 }
77 if (offset < 0 || offset > fileLength - bufCapacity) {
78 return READ_OFFSET_OUT_OF_RANGE;
79 }
80 mmapInfo.mmapPosition = (offset / memoryPageSize) * memoryPageSize;
81 mmapInfo.readMoreLen = static_cast<int>(offset - mmapInfo.mmapPosition);
82 mmapInfo.mmapSize = bufCapacity + mmapInfo.readMoreLen;
83 mmapInfo.mapAddr = reinterpret_cast<char*>(mmap(nullptr, mmapInfo.mmapSize, PROT_READ,
84 MAP_SHARED | MAP_POPULATE, fd, mmapInfo.mmapPosition));
85 if (mmapInfo.mapAddr == MAP_FAILED) {
86 HAPVERIFY_LOG_ERROR(LABEL, "MAP_FAILED: %{public}d", errno);
87 return MMAP_FAILED;
88 }
89 return 0;
90 }
91
ReadFileFullyFromOffset(char buf[],long long offset,int bufCapacity)92 long long RandomAccessFile::ReadFileFullyFromOffset(char buf[], long long offset, int bufCapacity)
93 {
94 if (buf == nullptr) {
95 return DEST_BUFFER_IS_NULL;
96 }
97
98 MmapInfo mmapInfo;
99 long long ret = DoMMap(bufCapacity, offset, mmapInfo);
100 if (ret < 0) {
101 return ret;
102 }
103
104 if (memcpy_s(buf, bufCapacity, mmapInfo.mapAddr + mmapInfo.readMoreLen,
105 mmapInfo.mmapSize - mmapInfo.readMoreLen) != EOK) {
106 munmap(mmapInfo.mapAddr, mmapInfo.mmapSize);
107 return MMAP_COPY_FAILED;
108 }
109 munmap(mmapInfo.mapAddr, mmapInfo.mmapSize);
110 return bufCapacity;
111 }
112
ReadFileFullyFromOffset(HapByteBuffer & buffer,long long offset)113 long long RandomAccessFile::ReadFileFullyFromOffset(HapByteBuffer& buffer, long long offset)
114 {
115 if (!buffer.HasRemaining()) {
116 return DEST_BUFFER_IS_NULL;
117 }
118
119 MmapInfo mmapInfo;
120 int bufCapacity = buffer.GetCapacity();
121 long long ret = DoMMap(bufCapacity, offset, mmapInfo);
122 if (ret < 0) {
123 return ret;
124 }
125
126 buffer.PutData(0, mmapInfo.mapAddr + mmapInfo.readMoreLen, bufCapacity);
127 munmap(mmapInfo.mapAddr, mmapInfo.mmapSize);
128 return bufCapacity;
129 }
130
ReadFileFromOffsetAndDigestUpdate(const DigestParameter & digestParam,int chunkSize,long long offset)131 bool RandomAccessFile::ReadFileFromOffsetAndDigestUpdate(const DigestParameter& digestParam,
132 int chunkSize, long long offset)
133 {
134 MmapInfo mmapInfo;
135 long long ret = DoMMap(chunkSize, offset, mmapInfo);
136 if (ret < 0) {
137 HAPVERIFY_LOG_ERROR(LABEL, "DoMMap failed: %{public}lld", ret);
138 return false;
139 }
140
141 unsigned char* content = reinterpret_cast<unsigned char*>(mmapInfo.mapAddr + mmapInfo.readMoreLen);
142 bool res = HapVerifyOpensslUtils::DigestUpdate(digestParam, content, chunkSize);
143 munmap(mmapInfo.mapAddr, mmapInfo.mmapSize);
144 return res;
145 }
146 } // namespace Verify
147 } // namespace Security
148 } // namespace OHOS
149