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