1 /*
2 * Copyright (C) 2015 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 #include <inttypes.h>
18 #include <limits.h>
19 #include <stdio.h>
20 #include <sys/capability.h>
21 #include <sys/mman.h>
22 #include <sys/prctl.h>
23 #include <sys/utsname.h>
24 #include <unistd.h>
25
26 #include <string>
27 #include <vector>
28
29 #include <gtest/gtest.h>
30
31 #include "android-base/file.h"
32 #include "android-base/strings.h"
33
34 #include "utils.h"
35
36 // http://b/20017123.
TEST(sys_prctl,bug_20017123)37 TEST(sys_prctl, bug_20017123) {
38 size_t page_size = static_cast<size_t>(sysconf(_SC_PAGESIZE));
39 void* p = mmap(NULL, page_size * 3, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
40 ASSERT_NE(MAP_FAILED, p);
41 ASSERT_EQ(0, mprotect(p, page_size, PROT_NONE));
42 ASSERT_NE(-1, prctl(PR_SET_VMA, PR_SET_VMA_ANON_NAME, p, page_size * 3, "anonymous map space"));
43 // Now read the maps and verify that there are no overlapped maps.
44 std::string file_data;
45 ASSERT_TRUE(android::base::ReadFileToString("/proc/self/maps", &file_data));
46
47 uintptr_t last_end = 0;
48 std::vector<std::string> lines = android::base::Split(file_data, "\n");
49 for (size_t i = 0; i < lines.size(); i++) {
50 if (lines[i].empty()) {
51 continue;
52 }
53 uintptr_t start;
54 uintptr_t end;
55 ASSERT_EQ(2, sscanf(lines[i].c_str(), "%" SCNxPTR "-%" SCNxPTR " ", &start, &end))
56 << "Failed to parse line: " << lines[i];
57 // This will never fail on the first line, so no need to do any special checking.
58 ASSERT_GE(start, last_end)
59 << "Overlapping map detected:\n" << lines[i -1] << '\n' << lines[i] << '\n';
60 last_end = end;
61 }
62
63 ASSERT_EQ(0, munmap(p, page_size * 3));
64 }
65
TEST(sys_prctl,pr_cap_ambient)66 TEST(sys_prctl, pr_cap_ambient) {
67 const std::string caps_sha =
68 "https://git.kernel.org/cgit/linux/kernel/git/torvalds/linux.git/commit/"
69 "?id=58319057b7847667f0c9585b9de0e8932b0fdb08";
70 const std::string caps_typo_sha =
71 "https://git.kernel.org/cgit/linux/kernel/git/torvalds/linux.git/commit/"
72 "?id=b7f76ea2ef6739ee484a165ffbac98deb855d3d3";
73
74 utsname u = {};
75 ASSERT_EQ(0, uname(&u));
76
77 errno = 0;
78 auto err = prctl(PR_CAP_AMBIENT, PR_CAP_AMBIENT_CLEAR_ALL, 0, 0, 0);
79 EXPECT_EQ(0, err);
80 // EINVAL -> unrecognized prctl option
81 ASSERT_NE(EINVAL, errno) << "kernel (" << u.release << ") missing required commits:\n"
82 << caps_sha << "\n"
83 << caps_typo_sha << "\n";
84
85 // Unprivileged processes shouldn't be able to raise CAP_SYS_ADMIN,
86 // but they can check or lower it
87 err = prctl(PR_CAP_AMBIENT, PR_CAP_AMBIENT_RAISE, CAP_SYS_ADMIN, 0, 0);
88 EXPECT_EQ(-1, err);
89 EXPECT_ERRNO(EPERM);
90
91 err = prctl(PR_CAP_AMBIENT, PR_CAP_AMBIENT_IS_SET, CAP_SYS_ADMIN, 0, 0);
92 EXPECT_EQ(0, err);
93
94 err = prctl(PR_CAP_AMBIENT, PR_CAP_AMBIENT_LOWER, CAP_SYS_ADMIN, 0, 0);
95 EXPECT_EQ(0, err);
96
97 // ULONG_MAX isn't a legal cap
98 err = prctl(PR_CAP_AMBIENT, PR_CAP_AMBIENT_RAISE, ULONG_MAX, 0, 0);
99 EXPECT_EQ(-1, err);
100 EXPECT_ERRNO(EINVAL);
101
102 err = prctl(PR_CAP_AMBIENT, PR_CAP_AMBIENT_IS_SET, ULONG_MAX, 0, 0);
103 EXPECT_EQ(-1, err);
104 EXPECT_ERRNO(EINVAL);
105
106 err = prctl(PR_CAP_AMBIENT, PR_CAP_AMBIENT_LOWER, ULONG_MAX, 0, 0);
107 EXPECT_EQ(-1, err);
108 EXPECT_ERRNO(EINVAL);
109 }
110