[email protected] | bdf9bdc | 2013-03-13 04:23:10 | [diff] [blame] | 1 | // Copyright 2013 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "ipc/unix_domain_socket_util.h" |
| 6 | |
| 7 | #include <errno.h> |
avi | 246998d8 | 2015-12-22 02:39:04 | [diff] [blame] | 8 | #include <stddef.h> |
[email protected] | bdf9bdc | 2013-03-13 04:23:10 | [diff] [blame] | 9 | #include <sys/socket.h> |
| 10 | #include <sys/stat.h> |
| 11 | #include <sys/un.h> |
| 12 | #include <unistd.h> |
| 13 | |
[email protected] | bdf9bdc | 2013-03-13 04:23:10 | [diff] [blame] | 14 | #include "base/files/file_path.h" |
thestig | c9e38a2 | 2014-09-13 01:02:11 | [diff] [blame] | 15 | #include "base/files/file_util.h" |
[email protected] | 42f558fd | 2014-03-17 19:02:35 | [diff] [blame] | 16 | #include "base/files/scoped_file.h" |
[email protected] | bdf9bdc | 2013-03-13 04:23:10 | [diff] [blame] | 17 | #include "base/logging.h" |
| 18 | #include "base/posix/eintr_wrapper.h" |
avi | 246998d8 | 2015-12-22 02:39:04 | [diff] [blame] | 19 | #include "build/build_config.h" |
[email protected] | bdf9bdc | 2013-03-13 04:23:10 | [diff] [blame] | 20 | |
| 21 | namespace IPC { |
| 22 | |
| 23 | // Verify that kMaxSocketNameLength is a decent size. |
anujk.sharma | 5a7ffe2f | 2015-01-22 05:39:37 | [diff] [blame] | 24 | static_assert(sizeof(((sockaddr_un*)0)->sun_path) >= kMaxSocketNameLength, |
| 25 | "sun_path is too long."); |
[email protected] | bdf9bdc | 2013-03-13 04:23:10 | [diff] [blame] | 26 | |
| 27 | namespace { |
| 28 | |
| 29 | // Returns fd (>= 0) on success, -1 on failure. If successful, fills in |
| 30 | // |unix_addr| with the appropriate data for the socket, and sets |
| 31 | // |unix_addr_len| to the length of the data therein. |
| 32 | int MakeUnixAddrForPath(const std::string& socket_name, |
| 33 | struct sockaddr_un* unix_addr, |
| 34 | size_t* unix_addr_len) { |
| 35 | DCHECK(unix_addr); |
| 36 | DCHECK(unix_addr_len); |
| 37 | |
| 38 | if (socket_name.length() == 0) { |
| 39 | LOG(ERROR) << "Empty socket name provided for unix socket address."; |
| 40 | return -1; |
| 41 | } |
| 42 | // We reject socket_name.length() == kMaxSocketNameLength to make room for |
| 43 | // the NUL terminator at the end of the string. |
| 44 | if (socket_name.length() >= kMaxSocketNameLength) { |
| 45 | LOG(ERROR) << "Socket name too long: " << socket_name; |
| 46 | return -1; |
| 47 | } |
| 48 | |
| 49 | // Create socket. |
[email protected] | 42f558fd | 2014-03-17 19:02:35 | [diff] [blame] | 50 | base::ScopedFD fd(socket(AF_UNIX, SOCK_STREAM, 0)); |
| 51 | if (!fd.is_valid()) { |
[email protected] | bdf9bdc | 2013-03-13 04:23:10 | [diff] [blame] | 52 | PLOG(ERROR) << "socket"; |
| 53 | return -1; |
| 54 | } |
[email protected] | bdf9bdc | 2013-03-13 04:23:10 | [diff] [blame] | 55 | |
| 56 | // Make socket non-blocking |
tfarina | 3d8548c0 | 2016-01-16 02:05:38 | [diff] [blame^] | 57 | if (!base::SetNonBlocking(fd.get())) { |
| 58 | PLOG(ERROR) << "base::SetNonBlocking() failed " << fd.get(); |
[email protected] | bdf9bdc | 2013-03-13 04:23:10 | [diff] [blame] | 59 | return -1; |
| 60 | } |
| 61 | |
| 62 | // Create unix_addr structure. |
| 63 | memset(unix_addr, 0, sizeof(struct sockaddr_un)); |
| 64 | unix_addr->sun_family = AF_UNIX; |
| 65 | strncpy(unix_addr->sun_path, socket_name.c_str(), kMaxSocketNameLength); |
| 66 | *unix_addr_len = |
| 67 | offsetof(struct sockaddr_un, sun_path) + socket_name.length(); |
[email protected] | 42f558fd | 2014-03-17 19:02:35 | [diff] [blame] | 68 | return fd.release(); |
[email protected] | bdf9bdc | 2013-03-13 04:23:10 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | } // namespace |
| 72 | |
| 73 | bool CreateServerUnixDomainSocket(const base::FilePath& socket_path, |
| 74 | int* server_listen_fd) { |
| 75 | DCHECK(server_listen_fd); |
| 76 | |
| 77 | std::string socket_name = socket_path.value(); |
| 78 | base::FilePath socket_dir = socket_path.DirName(); |
| 79 | |
| 80 | struct sockaddr_un unix_addr; |
| 81 | size_t unix_addr_len; |
[email protected] | 42f558fd | 2014-03-17 19:02:35 | [diff] [blame] | 82 | base::ScopedFD fd( |
| 83 | MakeUnixAddrForPath(socket_name, &unix_addr, &unix_addr_len)); |
| 84 | if (!fd.is_valid()) |
[email protected] | bdf9bdc | 2013-03-13 04:23:10 | [diff] [blame] | 85 | return false; |
[email protected] | bdf9bdc | 2013-03-13 04:23:10 | [diff] [blame] | 86 | |
| 87 | // Make sure the path we need exists. |
[email protected] | 426d1c9 | 2013-12-03 20:08:54 | [diff] [blame] | 88 | if (!base::CreateDirectory(socket_dir)) { |
[email protected] | bdf9bdc | 2013-03-13 04:23:10 | [diff] [blame] | 89 | LOG(ERROR) << "Couldn't create directory: " << socket_dir.value(); |
| 90 | return false; |
| 91 | } |
| 92 | |
| 93 | // Delete any old FS instances. |
| 94 | if (unlink(socket_name.c_str()) < 0 && errno != ENOENT) { |
| 95 | PLOG(ERROR) << "unlink " << socket_name; |
| 96 | return false; |
| 97 | } |
| 98 | |
| 99 | // Bind the socket. |
[email protected] | 42f558fd | 2014-03-17 19:02:35 | [diff] [blame] | 100 | if (bind(fd.get(), reinterpret_cast<const sockaddr*>(&unix_addr), |
[email protected] | bdf9bdc | 2013-03-13 04:23:10 | [diff] [blame] | 101 | unix_addr_len) < 0) { |
| 102 | PLOG(ERROR) << "bind " << socket_path.value(); |
| 103 | return false; |
| 104 | } |
| 105 | |
| 106 | // Start listening on the socket. |
[email protected] | 42f558fd | 2014-03-17 19:02:35 | [diff] [blame] | 107 | if (listen(fd.get(), SOMAXCONN) < 0) { |
[email protected] | bdf9bdc | 2013-03-13 04:23:10 | [diff] [blame] | 108 | PLOG(ERROR) << "listen " << socket_path.value(); |
| 109 | unlink(socket_name.c_str()); |
| 110 | return false; |
| 111 | } |
| 112 | |
[email protected] | 42f558fd | 2014-03-17 19:02:35 | [diff] [blame] | 113 | *server_listen_fd = fd.release(); |
[email protected] | bdf9bdc | 2013-03-13 04:23:10 | [diff] [blame] | 114 | return true; |
| 115 | } |
| 116 | |
| 117 | bool CreateClientUnixDomainSocket(const base::FilePath& socket_path, |
| 118 | int* client_socket) { |
| 119 | DCHECK(client_socket); |
| 120 | |
| 121 | std::string socket_name = socket_path.value(); |
| 122 | base::FilePath socket_dir = socket_path.DirName(); |
| 123 | |
| 124 | struct sockaddr_un unix_addr; |
| 125 | size_t unix_addr_len; |
[email protected] | 42f558fd | 2014-03-17 19:02:35 | [diff] [blame] | 126 | base::ScopedFD fd( |
| 127 | MakeUnixAddrForPath(socket_name, &unix_addr, &unix_addr_len)); |
| 128 | if (!fd.is_valid()) |
[email protected] | bdf9bdc | 2013-03-13 04:23:10 | [diff] [blame] | 129 | return false; |
[email protected] | bdf9bdc | 2013-03-13 04:23:10 | [diff] [blame] | 130 | |
[email protected] | 42f558fd | 2014-03-17 19:02:35 | [diff] [blame] | 131 | if (HANDLE_EINTR(connect(fd.get(), reinterpret_cast<sockaddr*>(&unix_addr), |
[email protected] | bdf9bdc | 2013-03-13 04:23:10 | [diff] [blame] | 132 | unix_addr_len)) < 0) { |
| 133 | PLOG(ERROR) << "connect " << socket_path.value(); |
| 134 | return false; |
| 135 | } |
| 136 | |
[email protected] | 42f558fd | 2014-03-17 19:02:35 | [diff] [blame] | 137 | *client_socket = fd.release(); |
[email protected] | bdf9bdc | 2013-03-13 04:23:10 | [diff] [blame] | 138 | return true; |
| 139 | } |
| 140 | |
| 141 | bool GetPeerEuid(int fd, uid_t* peer_euid) { |
| 142 | DCHECK(peer_euid); |
| 143 | #if defined(OS_MACOSX) || defined(OS_OPENBSD) || defined(OS_FREEBSD) |
| 144 | uid_t socket_euid; |
| 145 | gid_t socket_gid; |
| 146 | if (getpeereid(fd, &socket_euid, &socket_gid) < 0) { |
| 147 | PLOG(ERROR) << "getpeereid " << fd; |
| 148 | return false; |
| 149 | } |
| 150 | *peer_euid = socket_euid; |
| 151 | return true; |
| 152 | #else |
| 153 | struct ucred cred; |
| 154 | socklen_t cred_len = sizeof(cred); |
| 155 | if (getsockopt(fd, SOL_SOCKET, SO_PEERCRED, &cred, &cred_len) < 0) { |
| 156 | PLOG(ERROR) << "getsockopt " << fd; |
| 157 | return false; |
| 158 | } |
| 159 | if (static_cast<unsigned>(cred_len) < sizeof(cred)) { |
| 160 | NOTREACHED() << "Truncated ucred from SO_PEERCRED?"; |
| 161 | return false; |
| 162 | } |
| 163 | *peer_euid = cred.uid; |
| 164 | return true; |
| 165 | #endif |
| 166 | } |
| 167 | |
| 168 | bool IsPeerAuthorized(int peer_fd) { |
| 169 | uid_t peer_euid; |
| 170 | if (!GetPeerEuid(peer_fd, &peer_euid)) |
| 171 | return false; |
| 172 | if (peer_euid != geteuid()) { |
| 173 | DLOG(ERROR) << "Client euid is not authorised"; |
| 174 | return false; |
| 175 | } |
| 176 | return true; |
| 177 | } |
| 178 | |
| 179 | bool IsRecoverableError(int err) { |
| 180 | return errno == ECONNABORTED || errno == EMFILE || errno == ENFILE || |
| 181 | errno == ENOMEM || errno == ENOBUFS; |
| 182 | } |
| 183 | |
| 184 | bool ServerAcceptConnection(int server_listen_fd, int* server_socket) { |
| 185 | DCHECK(server_socket); |
| 186 | *server_socket = -1; |
| 187 | |
[email protected] | 42f558fd | 2014-03-17 19:02:35 | [diff] [blame] | 188 | base::ScopedFD accept_fd(HANDLE_EINTR(accept(server_listen_fd, NULL, 0))); |
| 189 | if (!accept_fd.is_valid()) |
[email protected] | bdf9bdc | 2013-03-13 04:23:10 | [diff] [blame] | 190 | return IsRecoverableError(errno); |
tfarina | 3d8548c0 | 2016-01-16 02:05:38 | [diff] [blame^] | 191 | if (!base::SetNonBlocking(accept_fd.get())) { |
| 192 | PLOG(ERROR) << "base::SetNonBlocking() failed " << accept_fd.get(); |
[email protected] | bdf9bdc | 2013-03-13 04:23:10 | [diff] [blame] | 193 | // It's safe to keep listening on |server_listen_fd| even if the attempt to |
| 194 | // set O_NONBLOCK failed on the client fd. |
| 195 | return true; |
| 196 | } |
| 197 | |
[email protected] | 42f558fd | 2014-03-17 19:02:35 | [diff] [blame] | 198 | *server_socket = accept_fd.release(); |
[email protected] | bdf9bdc | 2013-03-13 04:23:10 | [diff] [blame] | 199 | return true; |
| 200 | } |
| 201 | |
| 202 | } // namespace IPC |