1 /*
2  * Copyright (C) 2021-2022 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 #include <gtest/gtest.h>
16 #include <fcntl.h>
17 #include <cinttypes>
18 #include <climits>
19 #include <cstdio>
20 #include <unistd.h>
21 
22 #include <iomanip>
23 #include <iostream>
24 #include <string>
25 #include <vector>
26 
27 #include <sys/mman.h>
28 #include <sys/prctl.h>
29 #include <sys/utsname.h>
30 #include <string>
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include "securec.h"
34 #ifndef PR_SET_VMA
35 const PR_SET_VMA 0x53564d41
36 #endif
37 
38 #ifndef PR_SET_VMA_ANON_NAME
39 const int PR_SET_VMA_ANON_NAME = 0;
40 #endif
41 
42 using namespace testing::ext;
43 using namespace std;
44 
45 class PrctlApiTest : public testing::Test {
46 public:
47     static void SetUpTestCase();
48     static void TearDownTestCase();
49     void SetUp();
50     void TearDown();
51 private:
52 };
SetUp()53 void PrctlApiTest::SetUp()
54 {
55 }
TearDown()56 void PrctlApiTest::TearDown()
57 {
58 }
SetUpTestCase()59 void PrctlApiTest::SetUpTestCase()
60 {
61 }
TearDownTestCase()62 void PrctlApiTest::TearDownTestCase()
63 {
64 }
65 
handle_error(const std::string &msg)66 static bool handle_error(const std::string &msg)
67 {
68     perror(msg.c_str());
69     return false;
70 }
71 
str_split(const std::string &s, const std::string &delimiters)72 static std::vector<std::string> str_split(const std::string &s, const std::string &delimiters)
73 {
74     if (delimiters.size(), 0U)
75         abort();
76 
77     std::vector<std::string> result;
78     size_t base = 0;
79     size_t found;
80 
81     while (true) {
82         found = s.find_first_of(delimiters, base);
83         result.push_back(s.substr(base, found - base));
84         if (found == s.npos)
85             break;
86         base = found + 1;
87     }
88 
89     return result;
90 }
91 
read_fd_to_string(const std::string &path, std::string *content)92 static bool read_fd_to_string(const std::string &path, std::string *content)
93 {
94     char buf[BUFSIZ];
95     ssize_t n;
96     int fd;
97 
98     fd = TEMP_FAILURE_RETRY(open(path.c_str(), O_RDONLY | O_CLOEXEC | O_NOFOLLOW));
99     if (fd == -1) {
100         return false;
101     }
102     content->clear();
103     while ((n = TEMP_FAILURE_RETRY(read(fd, &buf[0], sizeof(buf)))) > 0)
104         content->append(buf, n);
105 
106     TEMP_FAILURE_RETRY(close(fd));
107     return (n == 0) ? true : false;
108 }
109 
SetVmaAnonName(void)110 int SetVmaAnonName(void)
111 {
112     size_t page_size = static_cast<size_t>(sysconf(_SC_PAGESIZE));
113     const int NUMBER_PAGE = 3;
114     void *ret_pm = nullptr;
115 
116     ret_pm = mmap(NULL, page_size * NUMBER_PAGE, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
117     if (static_cast< int >(*(int *)ret_pm) == static_cast<int>(-1)) {
118         handle_error("mmap fail\n");
119     }
120     if (mprotect(ret_pm, page_size, PROT_NONE)) {
121         handle_error("mprotect fail\n");
122     }
123     if (prctl(PR_SET_VMA, PR_SET_VMA_ANON_NAME, ret_pm, page_size * NUMBER_PAGE, "anonymous map space")) {
124         handle_error("prctl fail \n");
125     }
126     // Now read the maps and verify that there are no overlapped maps.
127     std::string file_data;
128     if (!read_fd_to_string("/proc/self/maps", &file_data))
129         handle_error("read string\n");
130 
131     uintptr_t last_end = 0;
132     std::vector<std::string> lines = str_split(file_data, "\n");
133     for (size_t i = 0; i < lines.size(); i++) {
134         if (lines[i].empty())
135             continue;
136 
137         uintptr_t start, end;
138         if (sscanf_s(lines[i].c_str(), "%" SCNxPTR "-%" SCNxPTR " ", &start, &end) != 2)
139             std::cout << "FAILED to parse line :" << lines[i];
140 
141         // This will never fail on the first line , so no need to do any special checking.
142         if (start < last_end)
143             std::cout << "Overlapping map detected:\n"
144                       << lines[i - 1] << '\n'
145                       << lines[i] << '\n';
146 
147         last_end = end;
148     }
149 
150     if (munmap(ret_pm, page_size * NUMBER_PAGE)) {
151         handle_error("munmap fail\n");
152     }
153     return 0;
154 }
155 
156 /*
157  * @tc.number PRCTL_SET_VMA_NAME_0100
158  * @tc.name faultlogger Detect a cpp crash happen
159  * @tc.desc inject a cppcrash fault and check faultlogger can detect the fault
160 */
HWTEST_F(PrctlApiTest, SetVmaAnonName0100, Function | MediumTest | Level1)161 HWTEST_F(PrctlApiTest, SetVmaAnonName0100, Function | MediumTest | Level1)
162 {
163     int ret = false;
164     ret = SetVmaAnonName();
165     ASSERT_TRUE(false == ret);
166 }
167