blob: bbc1c6735e05aa760617693915a71e89b877ee82 [file] [log] [blame]
Yabin Cui91ce7152015-07-13 16:54:29 -07001/*
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 Ferrisf1649d92016-11-03 16:06:40 -070017#include <inttypes.h>
Greg Hackmannba1f77d2016-12-06 15:17:59 -080018#include <limits.h>
Christopher Ferrisf1649d92016-11-03 16:06:40 -070019#include <stdio.h>
Greg Hackmannba1f77d2016-12-06 15:17:59 -080020#include <sys/capability.h>
Yabin Cui91ce7152015-07-13 16:54:29 -070021#include <sys/mman.h>
22#include <sys/prctl.h>
Elliott Hughes910a2a82018-12-18 15:19:40 -080023#include <sys/utsname.h>
Yabin Cui91ce7152015-07-13 16:54:29 -070024#include <unistd.h>
Christopher Ferrisf1649d92016-11-03 16:06:40 -070025
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"
Yabin Cui91ce7152015-07-13 16:54:29 -070033
Elliott Hughes95646e62023-09-21 14:11:19 -070034#include "utils.h"
35
Yabin Cui91ce7152015-07-13 16:54:29 -070036// http://b/20017123.
37TEST(sys_prctl, bug_20017123) {
Elliott Hughes99d54652018-08-22 10:36:23 -070038#if defined(PR_SET_VMA) // PR_SET_VMA is only available in Android kernels.
Yabin Cui91ce7152015-07-13 16:54:29 -070039 size_t page_size = static_cast<size_t>(sysconf(_SC_PAGESIZE));
40 void* p = mmap(NULL, page_size * 3, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
41 ASSERT_NE(MAP_FAILED, p);
42 ASSERT_EQ(0, mprotect(p, page_size, PROT_NONE));
43 ASSERT_NE(-1, prctl(PR_SET_VMA, PR_SET_VMA_ANON_NAME, p, page_size * 3, "anonymous map space"));
Christopher Ferrisf1649d92016-11-03 16:06:40 -070044 // Now read the maps and verify that there are no overlapped maps.
45 std::string file_data;
46 ASSERT_TRUE(android::base::ReadFileToString("/proc/self/maps", &file_data));
47
Christopher Ferrisf1649d92016-11-03 16:06:40 -070048 uintptr_t last_end = 0;
49 std::vector<std::string> lines = android::base::Split(file_data, "\n");
50 for (size_t i = 0; i < lines.size(); i++) {
51 if (lines[i].empty()) {
52 continue;
53 }
54 uintptr_t start;
55 uintptr_t end;
56 ASSERT_EQ(2, sscanf(lines[i].c_str(), "%" SCNxPTR "-%" SCNxPTR " ", &start, &end))
57 << "Failed to parse line: " << lines[i];
58 // This will never fail on the first line, so no need to do any special checking.
Christopher Ferris4dd27852016-11-08 14:05:18 -080059 ASSERT_GE(start, last_end)
60 << "Overlapping map detected:\n" << lines[i -1] << '\n' << lines[i] << '\n';
Christopher Ferrisf1649d92016-11-03 16:06:40 -070061 last_end = end;
62 }
63
Yabin Cui91ce7152015-07-13 16:54:29 -070064 ASSERT_EQ(0, munmap(p, page_size * 3));
65#else
Elliott Hughesbcaa4542019-03-08 15:20:23 -080066 GTEST_SKIP() << "PR_SET_VMA not available";
Yabin Cui91ce7152015-07-13 16:54:29 -070067#endif
68}
Greg Hackmannba1f77d2016-12-06 15:17:59 -080069
70TEST(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
Elliott Hughes910a2a82018-12-18 15:19:40 -080081 utsname u = {};
82 ASSERT_EQ(0, uname(&u));
83
84 errno = 0;
Greg Hackmannba1f77d2016-12-06 15:17:59 -080085 auto err = prctl(PR_CAP_AMBIENT, PR_CAP_AMBIENT_CLEAR_ALL, 0, 0, 0);
86 EXPECT_EQ(0, err);
87 // EINVAL -> unrecognized prctl option
Elliott Hughes910a2a82018-12-18 15:19:40 -080088 ASSERT_NE(EINVAL, errno) << "kernel (" << u.release << ") missing required commits:\n"
Greg Hackmannba1f77d2016-12-06 15:17:59 -080089 << caps_sha << "\n"
90 << caps_typo_sha << "\n";
91
92 // Unprivileged processes shouldn't be able to raise CAP_SYS_ADMIN,
93 // but they can check or lower it
94 err = prctl(PR_CAP_AMBIENT, PR_CAP_AMBIENT_RAISE, CAP_SYS_ADMIN, 0, 0);
95 EXPECT_EQ(-1, err);
Elliott Hughes95646e62023-09-21 14:11:19 -070096 EXPECT_ERRNO(EPERM);
Greg Hackmannba1f77d2016-12-06 15:17:59 -080097
98 err = prctl(PR_CAP_AMBIENT, PR_CAP_AMBIENT_IS_SET, CAP_SYS_ADMIN, 0, 0);
99 EXPECT_EQ(0, err);
100
101 err = prctl(PR_CAP_AMBIENT, PR_CAP_AMBIENT_LOWER, CAP_SYS_ADMIN, 0, 0);
102 EXPECT_EQ(0, err);
103
104 // ULONG_MAX isn't a legal cap
105 err = prctl(PR_CAP_AMBIENT, PR_CAP_AMBIENT_RAISE, ULONG_MAX, 0, 0);
106 EXPECT_EQ(-1, err);
Elliott Hughes95646e62023-09-21 14:11:19 -0700107 EXPECT_ERRNO(EINVAL);
Greg Hackmannba1f77d2016-12-06 15:17:59 -0800108
109 err = prctl(PR_CAP_AMBIENT, PR_CAP_AMBIENT_IS_SET, ULONG_MAX, 0, 0);
110 EXPECT_EQ(-1, err);
Elliott Hughes95646e62023-09-21 14:11:19 -0700111 EXPECT_ERRNO(EINVAL);
Greg Hackmannba1f77d2016-12-06 15:17:59 -0800112
113 err = prctl(PR_CAP_AMBIENT, PR_CAP_AMBIENT_LOWER, ULONG_MAX, 0, 0);
114 EXPECT_EQ(-1, err);
Elliott Hughes95646e62023-09-21 14:11:19 -0700115 EXPECT_ERRNO(EINVAL);
Greg Hackmannba1f77d2016-12-06 15:17:59 -0800116#else
Elliott Hughesbcaa4542019-03-08 15:20:23 -0800117 GTEST_SKIP() << "PR_CAP_AMBIENT not available";
Greg Hackmannba1f77d2016-12-06 15:17:59 -0800118#endif
119}