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