Nick Kralevich | bd4d45d | 2015-12-23 17:42:29 -0800 | [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 | |
| 17 | #include <gtest/gtest.h> |
| 18 | #include <pthread.h> |
| 19 | #include <fcntl.h> |
| 20 | #include <unistd.h> |
| 21 | #include <sys/types.h> |
| 22 | #include <sys/stat.h> |
| 23 | #include <sys/prctl.h> |
| 24 | |
Christopher Ferris | 2c4ec7e | 2018-08-23 11:17:55 -0700 | [diff] [blame] | 25 | #include <string> |
| 26 | |
Tom Cherry | 98f016f | 2017-04-05 16:20:29 -0700 | [diff] [blame] | 27 | #include <android-base/scopeguard.h> |
Nick Kralevich | bd4d45d | 2015-12-23 17:42:29 -0800 | [diff] [blame] | 28 | |
| 29 | extern "C" pid_t gettid(); |
| 30 | |
| 31 | static void ProcSelfReadlinkBody() { |
| 32 | char buf[100]; |
| 33 | char buf2[1024]; |
| 34 | int fd = open("/dev/null", O_RDWR | O_CLOEXEC); |
| 35 | ASSERT_NE(-1, fd); |
| 36 | snprintf(buf, sizeof(buf), "/proc/self/fd/%d", fd); |
| 37 | const char* ERRORMSG = "Please apply the following two kernel patches:\n" |
| 38 | "* https://git.kernel.org/cgit/linux/kernel/git/torvalds/linux.git/commit/?id=73af963f9f3036dffed55c3a2898598186db1045\n" |
| 39 | "* https://git.kernel.org/cgit/linux/kernel/git/torvalds/linux.git/commit/?id=96d0df79f2644fc823f26c06491e182d87a90c2a\n"; |
Christopher Ferris | 2c4ec7e | 2018-08-23 11:17:55 -0700 | [diff] [blame] | 40 | ssize_t length = readlink(buf, buf2, sizeof(buf2)); |
| 41 | ASSERT_LT(0, length) << ERRORMSG; |
| 42 | ASSERT_EQ("/dev/null", std::string(buf2, length)); |
Nick Kralevich | bd4d45d | 2015-12-23 17:42:29 -0800 | [diff] [blame] | 43 | close(fd); |
| 44 | } |
| 45 | |
| 46 | static void* ProcSelfReadlink(void*) { |
| 47 | ProcSelfReadlinkBody(); |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 48 | return nullptr; |
Nick Kralevich | bd4d45d | 2015-12-23 17:42:29 -0800 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | TEST(bug_26110743, ProcSelfReadlink) { |
| 52 | pthread_t t; |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 53 | ASSERT_EQ(0, pthread_create(&t, nullptr, ProcSelfReadlink, nullptr)); |
Nick Kralevich | bd4d45d | 2015-12-23 17:42:29 -0800 | [diff] [blame] | 54 | void* result; |
| 55 | ASSERT_EQ(0, pthread_join(t, &result)); |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 56 | ASSERT_EQ(nullptr, result); |
Nick Kralevich | bd4d45d | 2015-12-23 17:42:29 -0800 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | TEST(bug_26110743, ProcSelfReadlink_NotDumpable) { |
| 60 | int dumpable = prctl(PR_GET_DUMPABLE, 0, 0, 0, 0); |
| 61 | prctl(PR_SET_DUMPABLE, 0, 0, 0, 0); |
Tom Cherry | 98f016f | 2017-04-05 16:20:29 -0700 | [diff] [blame] | 62 | auto guard = android::base::make_scope_guard([&]() { |
Nick Kralevich | bd4d45d | 2015-12-23 17:42:29 -0800 | [diff] [blame] | 63 | // restore dumpable |
| 64 | prctl(PR_SET_DUMPABLE, dumpable, 0, 0, 0); |
| 65 | }); |
| 66 | |
| 67 | pthread_t t; |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 68 | ASSERT_EQ(0, pthread_create(&t, nullptr, ProcSelfReadlink, nullptr)); |
Nick Kralevich | bd4d45d | 2015-12-23 17:42:29 -0800 | [diff] [blame] | 69 | void* result; |
| 70 | ASSERT_EQ(0, pthread_join(t, &result)); |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 71 | ASSERT_EQ(nullptr, result); |
Nick Kralevich | bd4d45d | 2015-12-23 17:42:29 -0800 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | static void ProcTaskFdReadlinkBody() { |
| 75 | char buf[200]; |
| 76 | char buf2[1024]; |
| 77 | int fd = open("/dev/null", O_RDWR | O_CLOEXEC); |
| 78 | ASSERT_NE(-1, fd); |
| 79 | pid_t mypid = getpid(); |
| 80 | pid_t mytid = gettid(); |
| 81 | ASSERT_NE(mypid, mytid); |
| 82 | snprintf(buf, sizeof(buf), "/proc/%d/task/%d/fd/%d", mypid, mytid, fd); |
| 83 | const char* ERRORMSG = "Please apply the following kernel patch:\n" |
| 84 | "* https://git.kernel.org/cgit/linux/kernel/git/torvalds/linux.git/commit/?id=54708d2858e79a2bdda10bf8a20c80eb96c20613\n"; |
Christopher Ferris | 2c4ec7e | 2018-08-23 11:17:55 -0700 | [diff] [blame] | 85 | ssize_t length = readlink(buf, buf2, sizeof(buf2)); |
| 86 | ASSERT_LT(0, length) << ERRORMSG; |
| 87 | ASSERT_EQ("/dev/null", std::string(buf2, length)); |
Nick Kralevich | bd4d45d | 2015-12-23 17:42:29 -0800 | [diff] [blame] | 88 | close(fd); |
| 89 | } |
| 90 | |
| 91 | static void* ProcTaskFdReadlink(void*) { |
| 92 | ProcTaskFdReadlinkBody(); |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 93 | return nullptr; |
Nick Kralevich | bd4d45d | 2015-12-23 17:42:29 -0800 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | TEST(bug_26110743, ProcTaskFdReadlink) { |
| 97 | pthread_t t; |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 98 | ASSERT_EQ(0, pthread_create(&t, nullptr, ProcTaskFdReadlink, nullptr)); |
Nick Kralevich | bd4d45d | 2015-12-23 17:42:29 -0800 | [diff] [blame] | 99 | void* result; |
| 100 | ASSERT_EQ(0, pthread_join(t, &result)); |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 101 | ASSERT_EQ(nullptr, result); |
Nick Kralevich | bd4d45d | 2015-12-23 17:42:29 -0800 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | TEST(bug_26110743, ProcTaskFdReadlink_NotDumpable) { |
| 105 | int dumpable = prctl(PR_GET_DUMPABLE, 0, 0, 0, 0); |
| 106 | prctl(PR_SET_DUMPABLE, 0, 0, 0, 0); |
Tom Cherry | 98f016f | 2017-04-05 16:20:29 -0700 | [diff] [blame] | 107 | auto guard = android::base::make_scope_guard([&]() { |
Nick Kralevich | bd4d45d | 2015-12-23 17:42:29 -0800 | [diff] [blame] | 108 | // restore dumpable |
| 109 | prctl(PR_SET_DUMPABLE, dumpable, 0, 0, 0); |
| 110 | }); |
| 111 | |
| 112 | pthread_t t; |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 113 | ASSERT_EQ(0, pthread_create(&t, nullptr, ProcTaskFdReadlink, nullptr)); |
Nick Kralevich | bd4d45d | 2015-12-23 17:42:29 -0800 | [diff] [blame] | 114 | void* result; |
| 115 | ASSERT_EQ(0, pthread_join(t, &result)); |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 116 | ASSERT_EQ(nullptr, result); |
Nick Kralevich | bd4d45d | 2015-12-23 17:42:29 -0800 | [diff] [blame] | 117 | } |