1 /*
2 * Copyright (C) 2020 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 "adbconnection/client.h"
18
19 #include <pwd.h>
20 #include <stddef.h>
21 #include <stdint.h>
22 #include <sys/socket.h>
23 #include <sys/un.h>
24
25 #include <memory>
26 #include <optional>
27
28 #include <android-base/cmsg.h>
29 #include <android-base/logging.h>
30 #include <android-base/unique_fd.h>
31
32 using android::base::unique_fd;
33
34 static constexpr char kJdwpControlName[] = "\0jdwp-control";
35
36 struct AdbConnectionClientContext {
37 unique_fd control_socket_;
38 };
39
SocketPeerIsTrusted(int fd)40 bool SocketPeerIsTrusted(int fd) {
41 ucred cr;
42 socklen_t cr_length = sizeof(cr);
43 if (getsockopt(fd, SOL_SOCKET, SO_PEERCRED, &cr, &cr_length) != 0) {
44 PLOG(ERROR) << "couldn't get socket credentials";
45 return false;
46 }
47
48 passwd* shell = getpwnam("shell");
49 if (cr.uid != 0 && cr.uid != shell->pw_uid) {
50 LOG(ERROR) << "untrusted uid " << cr.uid << " on other end of socket";
51 return false;
52 }
53
54 return true;
55 }
56
adbconnection_client_new(const AdbConnectionClientInfo * const * info_elems,size_t info_count)57 AdbConnectionClientContext* adbconnection_client_new(
58 const AdbConnectionClientInfo* const* info_elems, size_t info_count) {
59 auto ctx = std::make_unique<AdbConnectionClientContext>();
60
61 std::optional<uint64_t> pid;
62 std::optional<bool> debuggable;
63
64 for (size_t i = 0; i < info_count; ++i) {
65 auto info = info_elems[i];
66 switch (info->type) {
67 case AdbConnectionClientInfoType::pid:
68 if (pid) {
69 LOG(ERROR) << "multiple pid entries in AdbConnectionClientInfo, ignoring";
70 continue;
71 }
72 pid = info->data.pid;
73 break;
74
75 case AdbConnectionClientInfoType::debuggable:
76 if (debuggable) {
77 LOG(ERROR) << "multiple debuggable entries in AdbConnectionClientInfo, ignoring";
78 continue;
79 }
80 debuggable = info->data.pid;
81 break;
82 }
83 }
84
85 if (!pid) {
86 LOG(ERROR) << "AdbConnectionClientInfo missing required field pid";
87 return nullptr;
88 }
89
90 if (!debuggable) {
91 LOG(ERROR) << "AdbConnectionClientInfo missing required field debuggable";
92 return nullptr;
93 }
94
95 ctx->control_socket_.reset(socket(AF_UNIX, SOCK_SEQPACKET | SOCK_CLOEXEC, 0));
96 if (ctx->control_socket_ < 0) {
97 PLOG(ERROR) << "failed to create Unix domain socket";
98 return nullptr;
99 }
100
101 struct timeval timeout;
102 timeout.tv_sec = 1;
103 timeout.tv_usec = 0;
104 setsockopt(ctx->control_socket_.get(), SOL_SOCKET, SO_SNDTIMEO, &timeout, sizeof(timeout));
105
106 sockaddr_un addr = {};
107 addr.sun_family = AF_UNIX;
108 memcpy(addr.sun_path, kJdwpControlName, sizeof(kJdwpControlName));
109 size_t addr_len = offsetof(sockaddr_un, sun_path) + sizeof(kJdwpControlName) - 1;
110
111 int rc = connect(ctx->control_socket_.get(), reinterpret_cast<sockaddr*>(&addr), addr_len);
112 if (rc != 0) {
113 PLOG(ERROR) << "failed to connect to jdwp control socket";
114 return nullptr;
115 }
116
117 bool trusted = SocketPeerIsTrusted(ctx->control_socket_.get());
118 if (!trusted) {
119 LOG(ERROR) << "adb socket is not trusted, aborting connection";
120 return nullptr;
121 }
122
123 uint32_t pid_u32 = static_cast<uint32_t>(*pid);
124 rc = TEMP_FAILURE_RETRY(write(ctx->control_socket_.get(), &pid_u32, sizeof(pid_u32)));
125 if (rc != sizeof(pid_u32)) {
126 PLOG(ERROR) << "failed to send JDWP process pid to adbd";
127 }
128
129 return ctx.release();
130 }
131
adbconnection_client_destroy(AdbConnectionClientContext * ctx)132 void adbconnection_client_destroy(AdbConnectionClientContext* ctx) {
133 delete ctx;
134 }
135
adbconnection_client_pollfd(AdbConnectionClientContext * ctx)136 int adbconnection_client_pollfd(AdbConnectionClientContext* ctx) {
137 return ctx->control_socket_.get();
138 }
139
adbconnection_client_receive_jdwp_fd(AdbConnectionClientContext * ctx)140 int adbconnection_client_receive_jdwp_fd(AdbConnectionClientContext* ctx) {
141 char dummy;
142 unique_fd jdwp_fd;
143 ssize_t rc = android::base::ReceiveFileDescriptors(ctx->control_socket_, &dummy, 1, &jdwp_fd);
144 if (rc != 1) {
145 return rc;
146 }
147 return jdwp_fd.release();
148 }
149