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