[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 | 8749855 | 2016-02-21 19:52:40 | [diff] [blame^] | 27 | // Returns true on success, false otherwise. If successful, fills in |
[email protected] | bdf9bdc | 2013-03-13 04:23:10 | [diff] [blame] | 28 | // |unix_addr| with the appropriate data for the socket, and sets |
| 29 | // |unix_addr_len| to the length of the data therein. |
tfarina | 8749855 | 2016-02-21 19:52:40 | [diff] [blame^] | 30 | bool MakeUnixAddrForPath(const std::string& socket_name, |
| 31 | struct sockaddr_un* unix_addr, |
| 32 | size_t* unix_addr_len) { |
[email protected] | bdf9bdc | 2013-03-13 04:23:10 | [diff] [blame] | 33 | DCHECK(unix_addr); |
| 34 | DCHECK(unix_addr_len); |
| 35 | |
| 36 | if (socket_name.length() == 0) { |
| 37 | LOG(ERROR) << "Empty socket name provided for unix socket address."; |
tfarina | 8749855 | 2016-02-21 19:52:40 | [diff] [blame^] | 38 | return false; |
[email protected] | bdf9bdc | 2013-03-13 04:23:10 | [diff] [blame] | 39 | } |
| 40 | // We reject socket_name.length() == kMaxSocketNameLength to make room for |
| 41 | // the NUL terminator at the end of the string. |
| 42 | if (socket_name.length() >= kMaxSocketNameLength) { |
| 43 | LOG(ERROR) << "Socket name too long: " << socket_name; |
tfarina | 8749855 | 2016-02-21 19:52:40 | [diff] [blame^] | 44 | return false; |
[email protected] | bdf9bdc | 2013-03-13 04:23:10 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | // Create unix_addr structure. |
| 48 | memset(unix_addr, 0, sizeof(struct sockaddr_un)); |
| 49 | unix_addr->sun_family = AF_UNIX; |
| 50 | strncpy(unix_addr->sun_path, socket_name.c_str(), kMaxSocketNameLength); |
| 51 | *unix_addr_len = |
| 52 | offsetof(struct sockaddr_un, sun_path) + socket_name.length(); |
tfarina | 8749855 | 2016-02-21 19:52:40 | [diff] [blame^] | 53 | return true; |
| 54 | } |
| 55 | |
| 56 | // Returns a valid socket on success. |
| 57 | base::ScopedFD CreateUnixDomainSocket() { |
| 58 | // Create socket. |
| 59 | base::ScopedFD fd(socket(AF_UNIX, SOCK_STREAM, 0)); |
| 60 | if (!fd.is_valid()) { |
| 61 | PLOG(ERROR) << "Failed to create AF_UNIX socket."; |
| 62 | return base::ScopedFD(); |
| 63 | } |
| 64 | |
| 65 | // Make socket non-blocking. |
| 66 | if (!base::SetNonBlocking(fd.get())) { |
| 67 | PLOG(ERROR) << "base::SetNonBlocking() failed " << fd.get(); |
| 68 | return base::ScopedFD(); |
| 69 | } |
| 70 | |
| 71 | return fd; |
[email protected] | bdf9bdc | 2013-03-13 04:23:10 | [diff] [blame] | 72 | } |
| 73 | |
tfarina | 5586fa79 | 2016-01-26 21:23:46 | [diff] [blame] | 74 | bool IsRecoverableError() { |
| 75 | return errno == ECONNABORTED || errno == EMFILE || errno == ENFILE || |
| 76 | errno == ENOMEM || errno == ENOBUFS; |
| 77 | } |
| 78 | |
[email protected] | bdf9bdc | 2013-03-13 04:23:10 | [diff] [blame] | 79 | } // namespace |
| 80 | |
| 81 | bool CreateServerUnixDomainSocket(const base::FilePath& socket_path, |
| 82 | int* server_listen_fd) { |
| 83 | DCHECK(server_listen_fd); |
| 84 | |
tfarina | 03ab577 | 2016-02-09 01:41:36 | [diff] [blame] | 85 | const std::string socket_name = socket_path.value(); |
[email protected] | bdf9bdc | 2013-03-13 04:23:10 | [diff] [blame] | 86 | struct sockaddr_un unix_addr; |
| 87 | size_t unix_addr_len; |
tfarina | 8749855 | 2016-02-21 19:52:40 | [diff] [blame^] | 88 | if (!MakeUnixAddrForPath(socket_name, &unix_addr, &unix_addr_len)) |
| 89 | return false; |
| 90 | base::ScopedFD fd(CreateUnixDomainSocket()); |
[email protected] | 42f558fd | 2014-03-17 19:02:35 | [diff] [blame] | 91 | if (!fd.is_valid()) |
[email protected] | bdf9bdc | 2013-03-13 04:23:10 | [diff] [blame] | 92 | return false; |
[email protected] | bdf9bdc | 2013-03-13 04:23:10 | [diff] [blame] | 93 | |
| 94 | // Make sure the path we need exists. |
tfarina | 03ab577 | 2016-02-09 01:41:36 | [diff] [blame] | 95 | base::FilePath socket_dir = socket_path.DirName(); |
[email protected] | 426d1c9 | 2013-12-03 20:08:54 | [diff] [blame] | 96 | if (!base::CreateDirectory(socket_dir)) { |
[email protected] | bdf9bdc | 2013-03-13 04:23:10 | [diff] [blame] | 97 | LOG(ERROR) << "Couldn't create directory: " << socket_dir.value(); |
| 98 | return false; |
| 99 | } |
| 100 | |
| 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 | |
| 107 | // Bind the socket. |
[email protected] | 42f558fd | 2014-03-17 19:02:35 | [diff] [blame] | 108 | if (bind(fd.get(), reinterpret_cast<const sockaddr*>(&unix_addr), |
[email protected] | bdf9bdc | 2013-03-13 04:23:10 | [diff] [blame] | 109 | unix_addr_len) < 0) { |
tfarina | 03ab577 | 2016-02-09 01:41:36 | [diff] [blame] | 110 | PLOG(ERROR) << "bind " << socket_name; |
[email protected] | bdf9bdc | 2013-03-13 04:23:10 | [diff] [blame] | 111 | return false; |
| 112 | } |
| 113 | |
| 114 | // Start listening on the socket. |
[email protected] | 42f558fd | 2014-03-17 19:02:35 | [diff] [blame] | 115 | if (listen(fd.get(), SOMAXCONN) < 0) { |
tfarina | 03ab577 | 2016-02-09 01:41:36 | [diff] [blame] | 116 | PLOG(ERROR) << "listen " << socket_name; |
[email protected] | bdf9bdc | 2013-03-13 04:23:10 | [diff] [blame] | 117 | unlink(socket_name.c_str()); |
| 118 | return false; |
| 119 | } |
| 120 | |
[email protected] | 42f558fd | 2014-03-17 19:02:35 | [diff] [blame] | 121 | *server_listen_fd = fd.release(); |
[email protected] | bdf9bdc | 2013-03-13 04:23:10 | [diff] [blame] | 122 | return true; |
| 123 | } |
| 124 | |
| 125 | bool CreateClientUnixDomainSocket(const base::FilePath& socket_path, |
| 126 | int* client_socket) { |
| 127 | DCHECK(client_socket); |
| 128 | |
[email protected] | bdf9bdc | 2013-03-13 04:23:10 | [diff] [blame] | 129 | struct sockaddr_un unix_addr; |
| 130 | size_t unix_addr_len; |
tfarina | 8749855 | 2016-02-21 19:52:40 | [diff] [blame^] | 131 | if (!MakeUnixAddrForPath(socket_path.value(), &unix_addr, &unix_addr_len)) |
| 132 | return false; |
| 133 | base::ScopedFD fd(CreateUnixDomainSocket()); |
[email protected] | 42f558fd | 2014-03-17 19:02:35 | [diff] [blame] | 134 | if (!fd.is_valid()) |
[email protected] | bdf9bdc | 2013-03-13 04:23:10 | [diff] [blame] | 135 | return false; |
[email protected] | bdf9bdc | 2013-03-13 04:23:10 | [diff] [blame] | 136 | |
[email protected] | 42f558fd | 2014-03-17 19:02:35 | [diff] [blame] | 137 | if (HANDLE_EINTR(connect(fd.get(), reinterpret_cast<sockaddr*>(&unix_addr), |
[email protected] | bdf9bdc | 2013-03-13 04:23:10 | [diff] [blame] | 138 | unix_addr_len)) < 0) { |
| 139 | PLOG(ERROR) << "connect " << socket_path.value(); |
| 140 | return false; |
| 141 | } |
| 142 | |
[email protected] | 42f558fd | 2014-03-17 19:02:35 | [diff] [blame] | 143 | *client_socket = fd.release(); |
[email protected] | bdf9bdc | 2013-03-13 04:23:10 | [diff] [blame] | 144 | return true; |
| 145 | } |
| 146 | |
| 147 | bool GetPeerEuid(int fd, uid_t* peer_euid) { |
| 148 | DCHECK(peer_euid); |
| 149 | #if defined(OS_MACOSX) || defined(OS_OPENBSD) || defined(OS_FREEBSD) |
| 150 | uid_t socket_euid; |
| 151 | gid_t socket_gid; |
| 152 | if (getpeereid(fd, &socket_euid, &socket_gid) < 0) { |
| 153 | PLOG(ERROR) << "getpeereid " << fd; |
| 154 | return false; |
| 155 | } |
| 156 | *peer_euid = socket_euid; |
| 157 | return true; |
| 158 | #else |
| 159 | struct ucred cred; |
| 160 | socklen_t cred_len = sizeof(cred); |
| 161 | if (getsockopt(fd, SOL_SOCKET, SO_PEERCRED, &cred, &cred_len) < 0) { |
| 162 | PLOG(ERROR) << "getsockopt " << fd; |
| 163 | return false; |
| 164 | } |
| 165 | if (static_cast<unsigned>(cred_len) < sizeof(cred)) { |
| 166 | NOTREACHED() << "Truncated ucred from SO_PEERCRED?"; |
| 167 | return false; |
| 168 | } |
| 169 | *peer_euid = cred.uid; |
| 170 | return true; |
| 171 | #endif |
| 172 | } |
| 173 | |
| 174 | bool IsPeerAuthorized(int peer_fd) { |
| 175 | uid_t peer_euid; |
| 176 | if (!GetPeerEuid(peer_fd, &peer_euid)) |
| 177 | return false; |
| 178 | if (peer_euid != geteuid()) { |
| 179 | DLOG(ERROR) << "Client euid is not authorised"; |
| 180 | return false; |
| 181 | } |
| 182 | return true; |
| 183 | } |
| 184 | |
[email protected] | bdf9bdc | 2013-03-13 04:23:10 | [diff] [blame] | 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()) |
tfarina | 5586fa79 | 2016-01-26 21:23:46 | [diff] [blame] | 191 | return IsRecoverableError(); |
tfarina | 3d8548c0 | 2016-01-16 02:05:38 | [diff] [blame] | 192 | if (!base::SetNonBlocking(accept_fd.get())) { |
| 193 | PLOG(ERROR) << "base::SetNonBlocking() failed " << 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 |