Yabin Cui | 91ce715 | 2015-07-13 16:54:29 -0700 | [diff] [blame] | 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 | |
Christopher Ferris | f1649d9 | 2016-11-03 16:06:40 -0700 | [diff] [blame] | 17 | #include <inttypes.h> |
Greg Hackmann | ba1f77d | 2016-12-06 15:17:59 -0800 | [diff] [blame^] | 18 | #include <limits.h> |
Christopher Ferris | f1649d9 | 2016-11-03 16:06:40 -0700 | [diff] [blame] | 19 | #include <stdio.h> |
Greg Hackmann | ba1f77d | 2016-12-06 15:17:59 -0800 | [diff] [blame^] | 20 | #include <sys/capability.h> |
Yabin Cui | 91ce715 | 2015-07-13 16:54:29 -0700 | [diff] [blame] | 21 | #include <sys/mman.h> |
| 22 | #include <sys/prctl.h> |
| 23 | #include <unistd.h> |
Christopher Ferris | f1649d9 | 2016-11-03 16:06:40 -0700 | [diff] [blame] | 24 | |
| 25 | #include <string> |
| 26 | #include <vector> |
| 27 | |
| 28 | #include <gtest/gtest.h> |
| 29 | |
| 30 | #include "android-base/file.h" |
| 31 | #include "android-base/strings.h" |
Yabin Cui | 91ce715 | 2015-07-13 16:54:29 -0700 | [diff] [blame] | 32 | #include "private/bionic_prctl.h" |
| 33 | |
| 34 | // http://b/20017123. |
| 35 | TEST(sys_prctl, bug_20017123) { |
Elliott Hughes | a9209d7 | 2016-09-16 18:16:47 -0700 | [diff] [blame] | 36 | #if defined(__ANDROID__) // PR_SET_VMA is only available in Android kernels. |
Yabin Cui | 91ce715 | 2015-07-13 16:54:29 -0700 | [diff] [blame] | 37 | size_t page_size = static_cast<size_t>(sysconf(_SC_PAGESIZE)); |
| 38 | void* p = mmap(NULL, page_size * 3, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); |
| 39 | ASSERT_NE(MAP_FAILED, p); |
| 40 | ASSERT_EQ(0, mprotect(p, page_size, PROT_NONE)); |
| 41 | ASSERT_NE(-1, prctl(PR_SET_VMA, PR_SET_VMA_ANON_NAME, p, page_size * 3, "anonymous map space")); |
Christopher Ferris | f1649d9 | 2016-11-03 16:06:40 -0700 | [diff] [blame] | 42 | // Now read the maps and verify that there are no overlapped maps. |
| 43 | std::string file_data; |
| 44 | ASSERT_TRUE(android::base::ReadFileToString("/proc/self/maps", &file_data)); |
| 45 | |
| 46 | uintptr_t last_start = 0; |
| 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. |
Christopher Ferris | 4dd2785 | 2016-11-08 14:05:18 -0800 | [diff] [blame] | 58 | ASSERT_GE(start, last_end) |
| 59 | << "Overlapping map detected:\n" << lines[i -1] << '\n' << lines[i] << '\n'; |
Christopher Ferris | f1649d9 | 2016-11-03 16:06:40 -0700 | [diff] [blame] | 60 | last_start = start; |
| 61 | last_end = end; |
| 62 | } |
| 63 | |
Yabin Cui | 91ce715 | 2015-07-13 16:54:29 -0700 | [diff] [blame] | 64 | ASSERT_EQ(0, munmap(p, page_size * 3)); |
| 65 | #else |
| 66 | GTEST_LOG_(INFO) << "This test does nothing as it tests an Android specific kernel feature."; |
| 67 | #endif |
| 68 | } |
Greg Hackmann | ba1f77d | 2016-12-06 15:17:59 -0800 | [diff] [blame^] | 69 | |
| 70 | TEST(sys_prctl, pr_cap_ambient) { |
| 71 | // PR_CAP_AMBIENT was introduced in v4.3. Android devices should always |
| 72 | // have a backport, but we can't guarantee it's available on the host. |
| 73 | #if defined(__ANDROID__) || defined(PR_CAP_AMBIENT) |
| 74 | const std::string caps_sha = |
| 75 | "https://git.kernel.org/cgit/linux/kernel/git/torvalds/linux.git/commit/" |
| 76 | "?id=58319057b7847667f0c9585b9de0e8932b0fdb08"; |
| 77 | const std::string caps_typo_sha = |
| 78 | "https://git.kernel.org/cgit/linux/kernel/git/torvalds/linux.git/commit/" |
| 79 | "?id=b7f76ea2ef6739ee484a165ffbac98deb855d3d3"; |
| 80 | |
| 81 | auto err = prctl(PR_CAP_AMBIENT, PR_CAP_AMBIENT_CLEAR_ALL, 0, 0, 0); |
| 82 | EXPECT_EQ(0, err); |
| 83 | // EINVAL -> unrecognized prctl option |
| 84 | ASSERT_NE(EINVAL, errno) << "kernel missing required commits:\n" |
| 85 | << caps_sha << "\n" |
| 86 | << caps_typo_sha << "\n"; |
| 87 | |
| 88 | // Unprivileged processes shouldn't be able to raise CAP_SYS_ADMIN, |
| 89 | // but they can check or lower it |
| 90 | err = prctl(PR_CAP_AMBIENT, PR_CAP_AMBIENT_RAISE, CAP_SYS_ADMIN, 0, 0); |
| 91 | EXPECT_EQ(-1, err); |
| 92 | EXPECT_EQ(EPERM, errno); |
| 93 | |
| 94 | err = prctl(PR_CAP_AMBIENT, PR_CAP_AMBIENT_IS_SET, CAP_SYS_ADMIN, 0, 0); |
| 95 | EXPECT_EQ(0, err); |
| 96 | |
| 97 | err = prctl(PR_CAP_AMBIENT, PR_CAP_AMBIENT_LOWER, CAP_SYS_ADMIN, 0, 0); |
| 98 | EXPECT_EQ(0, err); |
| 99 | |
| 100 | // ULONG_MAX isn't a legal cap |
| 101 | err = prctl(PR_CAP_AMBIENT, PR_CAP_AMBIENT_RAISE, ULONG_MAX, 0, 0); |
| 102 | EXPECT_EQ(-1, err); |
| 103 | EXPECT_EQ(EINVAL, errno); |
| 104 | |
| 105 | err = prctl(PR_CAP_AMBIENT, PR_CAP_AMBIENT_IS_SET, ULONG_MAX, 0, 0); |
| 106 | EXPECT_EQ(-1, err); |
| 107 | EXPECT_EQ(EINVAL, errno); |
| 108 | |
| 109 | err = prctl(PR_CAP_AMBIENT, PR_CAP_AMBIENT_LOWER, ULONG_MAX, 0, 0); |
| 110 | EXPECT_EQ(-1, err); |
| 111 | EXPECT_EQ(EINVAL, errno); |
| 112 | #else |
| 113 | GTEST_LOG_(INFO) |
| 114 | << "Skipping test that requires host support for PR_CAP_AMBIENT."; |
| 115 | #endif |
| 116 | } |