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
25 #include <string>
26
27 #include <android-base/scopeguard.h>
28
29 extern "C" pid_t gettid();
30
ProcSelfReadlinkBody()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";
40 ssize_t length = readlink(buf, buf2, sizeof(buf2));
41 ASSERT_LT(0, length) << ERRORMSG;
42 ASSERT_EQ("/dev/null", std::string(buf2, length));
43 close(fd);
44 }
45
ProcSelfReadlink(void *)46 static void* ProcSelfReadlink(void*) {
47 ProcSelfReadlinkBody();
48 return nullptr;
49 }
50
TEST(bug_26110743,ProcSelfReadlink)51 TEST(bug_26110743, ProcSelfReadlink) {
52 pthread_t t;
53 ASSERT_EQ(0, pthread_create(&t, nullptr, ProcSelfReadlink, nullptr));
54 void* result;
55 ASSERT_EQ(0, pthread_join(t, &result));
56 ASSERT_EQ(nullptr, result);
57 }
58
TEST(bug_26110743,ProcSelfReadlink_NotDumpable)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);
62 auto guard = android::base::make_scope_guard([&]() {
63 // restore dumpable
64 prctl(PR_SET_DUMPABLE, dumpable, 0, 0, 0);
65 });
66
67 pthread_t t;
68 ASSERT_EQ(0, pthread_create(&t, nullptr, ProcSelfReadlink, nullptr));
69 void* result;
70 ASSERT_EQ(0, pthread_join(t, &result));
71 ASSERT_EQ(nullptr, result);
72 }
73
ProcTaskFdReadlinkBody()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";
85 ssize_t length = readlink(buf, buf2, sizeof(buf2));
86 ASSERT_LT(0, length) << ERRORMSG;
87 ASSERT_EQ("/dev/null", std::string(buf2, length));
88 close(fd);
89 }
90
ProcTaskFdReadlink(void *)91 static void* ProcTaskFdReadlink(void*) {
92 ProcTaskFdReadlinkBody();
93 return nullptr;
94 }
95
TEST(bug_26110743,ProcTaskFdReadlink)96 TEST(bug_26110743, ProcTaskFdReadlink) {
97 pthread_t t;
98 ASSERT_EQ(0, pthread_create(&t, nullptr, ProcTaskFdReadlink, nullptr));
99 void* result;
100 ASSERT_EQ(0, pthread_join(t, &result));
101 ASSERT_EQ(nullptr, result);
102 }
103
TEST(bug_26110743,ProcTaskFdReadlink_NotDumpable)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);
107 auto guard = android::base::make_scope_guard([&]() {
108 // restore dumpable
109 prctl(PR_SET_DUMPABLE, dumpable, 0, 0, 0);
110 });
111
112 pthread_t t;
113 ASSERT_EQ(0, pthread_create(&t, nullptr, ProcTaskFdReadlink, nullptr));
114 void* result;
115 ASSERT_EQ(0, pthread_join(t, &result));
116 ASSERT_EQ(nullptr, result);
117 }
118