blob: 883280fe00b0cc7e0783e75d7bf52ba208cf757e [file] [log] [blame]
Nick Kralevichbd4d45d2015-12-23 17:42:29 -08001/*
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 Ferris2c4ec7e2018-08-23 11:17:55 -070025#include <string>
26
Tom Cherry98f016f2017-04-05 16:20:29 -070027#include <android-base/scopeguard.h>
Nick Kralevichbd4d45d2015-12-23 17:42:29 -080028
29extern "C" pid_t gettid();
30
31static 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 Ferris2c4ec7e2018-08-23 11:17:55 -070040 ssize_t length = readlink(buf, buf2, sizeof(buf2));
41 ASSERT_LT(0, length) << ERRORMSG;
42 ASSERT_EQ("/dev/null", std::string(buf2, length));
Nick Kralevichbd4d45d2015-12-23 17:42:29 -080043 close(fd);
44}
45
46static void* ProcSelfReadlink(void*) {
47 ProcSelfReadlinkBody();
Yi Kong32bc0fc2018-08-02 17:31:13 -070048 return nullptr;
Nick Kralevichbd4d45d2015-12-23 17:42:29 -080049}
50
51TEST(bug_26110743, ProcSelfReadlink) {
52 pthread_t t;
Yi Kong32bc0fc2018-08-02 17:31:13 -070053 ASSERT_EQ(0, pthread_create(&t, nullptr, ProcSelfReadlink, nullptr));
Nick Kralevichbd4d45d2015-12-23 17:42:29 -080054 void* result;
55 ASSERT_EQ(0, pthread_join(t, &result));
Yi Kong32bc0fc2018-08-02 17:31:13 -070056 ASSERT_EQ(nullptr, result);
Nick Kralevichbd4d45d2015-12-23 17:42:29 -080057}
58
59TEST(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 Cherry98f016f2017-04-05 16:20:29 -070062 auto guard = android::base::make_scope_guard([&]() {
Nick Kralevichbd4d45d2015-12-23 17:42:29 -080063 // restore dumpable
64 prctl(PR_SET_DUMPABLE, dumpable, 0, 0, 0);
65 });
66
67 pthread_t t;
Yi Kong32bc0fc2018-08-02 17:31:13 -070068 ASSERT_EQ(0, pthread_create(&t, nullptr, ProcSelfReadlink, nullptr));
Nick Kralevichbd4d45d2015-12-23 17:42:29 -080069 void* result;
70 ASSERT_EQ(0, pthread_join(t, &result));
Yi Kong32bc0fc2018-08-02 17:31:13 -070071 ASSERT_EQ(nullptr, result);
Nick Kralevichbd4d45d2015-12-23 17:42:29 -080072}
73
74static 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 Ferris2c4ec7e2018-08-23 11:17:55 -070085 ssize_t length = readlink(buf, buf2, sizeof(buf2));
86 ASSERT_LT(0, length) << ERRORMSG;
87 ASSERT_EQ("/dev/null", std::string(buf2, length));
Nick Kralevichbd4d45d2015-12-23 17:42:29 -080088 close(fd);
89}
90
91static void* ProcTaskFdReadlink(void*) {
92 ProcTaskFdReadlinkBody();
Yi Kong32bc0fc2018-08-02 17:31:13 -070093 return nullptr;
Nick Kralevichbd4d45d2015-12-23 17:42:29 -080094}
95
96TEST(bug_26110743, ProcTaskFdReadlink) {
97 pthread_t t;
Yi Kong32bc0fc2018-08-02 17:31:13 -070098 ASSERT_EQ(0, pthread_create(&t, nullptr, ProcTaskFdReadlink, nullptr));
Nick Kralevichbd4d45d2015-12-23 17:42:29 -080099 void* result;
100 ASSERT_EQ(0, pthread_join(t, &result));
Yi Kong32bc0fc2018-08-02 17:31:13 -0700101 ASSERT_EQ(nullptr, result);
Nick Kralevichbd4d45d2015-12-23 17:42:29 -0800102}
103
104TEST(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 Cherry98f016f2017-04-05 16:20:29 -0700107 auto guard = android::base::make_scope_guard([&]() {
Nick Kralevichbd4d45d2015-12-23 17:42:29 -0800108 // restore dumpable
109 prctl(PR_SET_DUMPABLE, dumpable, 0, 0, 0);
110 });
111
112 pthread_t t;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700113 ASSERT_EQ(0, pthread_create(&t, nullptr, ProcTaskFdReadlink, nullptr));
Nick Kralevichbd4d45d2015-12-23 17:42:29 -0800114 void* result;
115 ASSERT_EQ(0, pthread_join(t, &result));
Yi Kong32bc0fc2018-08-02 17:31:13 -0700116 ASSERT_EQ(nullptr, result);
Nick Kralevichbd4d45d2015-12-23 17:42:29 -0800117}