xref: /aosp_15_r20/system/extras/tests/kernel.config/aslr_test.h (revision 288bf5226967eb3dac5cce6c939ccc2a7f2b4fe5)
1*288bf522SAndroid Build Coastguard Worker /*
2*288bf522SAndroid Build Coastguard Worker  * Copyright (C) 2016 The Android Open Source Project
3*288bf522SAndroid Build Coastguard Worker  *
4*288bf522SAndroid Build Coastguard Worker  * Licensed under the Apache License, Version 2.0 (the "License");
5*288bf522SAndroid Build Coastguard Worker  * you may not use this file except in compliance with the License.
6*288bf522SAndroid Build Coastguard Worker  * You may obtain a copy of the License at
7*288bf522SAndroid Build Coastguard Worker  *
8*288bf522SAndroid Build Coastguard Worker  *      http://www.apache.org/licenses/LICENSE-2.0
9*288bf522SAndroid Build Coastguard Worker  *
10*288bf522SAndroid Build Coastguard Worker  * Unless required by applicable law or agreed to in writing, software
11*288bf522SAndroid Build Coastguard Worker  * distributed under the License is distributed on an "AS IS" BASIS,
12*288bf522SAndroid Build Coastguard Worker  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*288bf522SAndroid Build Coastguard Worker  * See the License for the specific language governing permissions and
14*288bf522SAndroid Build Coastguard Worker  * limitations under the License.
15*288bf522SAndroid Build Coastguard Worker  */
16*288bf522SAndroid Build Coastguard Worker 
17*288bf522SAndroid Build Coastguard Worker #ifndef ASLR_TEST_H
18*288bf522SAndroid Build Coastguard Worker #define ASLR_TEST_H
19*288bf522SAndroid Build Coastguard Worker 
20*288bf522SAndroid Build Coastguard Worker #include <cmath>
21*288bf522SAndroid Build Coastguard Worker #include <errno.h>
22*288bf522SAndroid Build Coastguard Worker #include <fstream>
23*288bf522SAndroid Build Coastguard Worker #include <iostream>
24*288bf522SAndroid Build Coastguard Worker #include <stdint.h>
25*288bf522SAndroid Build Coastguard Worker #include <string>
26*288bf522SAndroid Build Coastguard Worker #include <sys/wait.h>
27*288bf522SAndroid Build Coastguard Worker #include <unistd.h>
28*288bf522SAndroid Build Coastguard Worker #include <unordered_set>
29*288bf522SAndroid Build Coastguard Worker 
30*288bf522SAndroid Build Coastguard Worker #include <gtest/gtest.h>
31*288bf522SAndroid Build Coastguard Worker 
32*288bf522SAndroid Build Coastguard Worker #define MAX_ADDR_LEN 256
33*288bf522SAndroid Build Coastguard Worker 
34*288bf522SAndroid Build Coastguard Worker #define PROCFS_PATH "/proc/sys/vm/mmap_rnd_bits"
35*288bf522SAndroid Build Coastguard Worker #define PROCFS_COMPAT_PATH "/proc/sys/vm/mmap_rnd_compat_bits"
36*288bf522SAndroid Build Coastguard Worker 
37*288bf522SAndroid Build Coastguard Worker #define SCRAPE_PATH_64 "/data/nativetest64/scrape_mmap_addr/scrape_mmap_addr"
38*288bf522SAndroid Build Coastguard Worker #define SCRAPE_PATH_32 "/data/nativetest/scrape_mmap_addr/scrape_mmap_addr"
39*288bf522SAndroid Build Coastguard Worker #define SCRAPE_LIB_64 "/system/bin/linker64"
40*288bf522SAndroid Build Coastguard Worker #define SCRAPE_LIB_32 "/system/bin/linker"
41*288bf522SAndroid Build Coastguard Worker 
42*288bf522SAndroid Build Coastguard Worker class AslrMmapTest : public ::testing::Test {
43*288bf522SAndroid Build Coastguard Worker   protected:
44*288bf522SAndroid Build Coastguard Worker     static void SetUpTestCase();
45*288bf522SAndroid Build Coastguard Worker     static const char *path;
46*288bf522SAndroid Build Coastguard Worker     static const char *lib;
47*288bf522SAndroid Build Coastguard Worker     static unsigned int def, min, max;
48*288bf522SAndroid Build Coastguard Worker     static bool compat, user32;
49*288bf522SAndroid Build Coastguard Worker     static unsigned int def_cmpt, min_cmpt, max_cmpt;
50*288bf522SAndroid Build Coastguard Worker 
51*288bf522SAndroid Build Coastguard Worker     void TearDown();
52*288bf522SAndroid Build Coastguard Worker };
53*288bf522SAndroid Build Coastguard Worker 
54*288bf522SAndroid Build Coastguard Worker /*
55*288bf522SAndroid Build Coastguard Worker  * gets the current mmap_rnd_bits value. requires root.
56*288bf522SAndroid Build Coastguard Worker  */
57*288bf522SAndroid Build Coastguard Worker unsigned int get_mmap_rnd_bits(bool compat);
58*288bf522SAndroid Build Coastguard Worker 
59*288bf522SAndroid Build Coastguard Worker /*
60*288bf522SAndroid Build Coastguard Worker  * sets the corresponding mmap_rnd_bits variable, returns false if couldn't
61*288bf522SAndroid Build Coastguard Worker  * change. requires root.
62*288bf522SAndroid Build Coastguard Worker  */
63*288bf522SAndroid Build Coastguard Worker bool set_mmap_rnd_bits(unsigned int new_val, bool compat);
64*288bf522SAndroid Build Coastguard Worker 
65*288bf522SAndroid Build Coastguard Worker /*
66*288bf522SAndroid Build Coastguard Worker  * scrape_addr - get the raw starting address from /proc/child_pid/mmaps
67*288bf522SAndroid Build Coastguard Worker  */
68*288bf522SAndroid Build Coastguard Worker std::string scrape_addr(const char *exec_name, const char *lib_match);
69*288bf522SAndroid Build Coastguard Worker 
70*288bf522SAndroid Build Coastguard Worker /*
71*288bf522SAndroid Build Coastguard Worker  * forks off sample_size processes and records the starting address of the
72*288bf522SAndroid Build Coastguard Worker  * indicated library as reported by exec_name.  Reports entropy observed among
73*288bf522SAndroid Build Coastguard Worker  * recorded samples.
74*288bf522SAndroid Build Coastguard Worker  */
75*288bf522SAndroid Build Coastguard Worker unsigned int calc_mmap_entropy(const char *exec_name, const char *lib_match, size_t samp_sz);
76*288bf522SAndroid Build Coastguard Worker 
77*288bf522SAndroid Build Coastguard Worker #endif //ASLR_TEST_H
78