blob: d6f5e59282ee1373fec0ffa59e874719452c574f [file] [log] [blame]
[email protected]bdf9bdc2013-03-13 04:23:101// 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]bdf9bdc2013-03-13 04:23:108#include <sys/socket.h>
[email protected]bdf9bdc2013-03-13 04:23:109#include <sys/un.h>
10#include <unistd.h>
11
[email protected]bdf9bdc2013-03-13 04:23:1012#include "base/files/file_path.h"
thestigc9e38a22014-09-13 01:02:1113#include "base/files/file_util.h"
[email protected]42f558fd2014-03-17 19:02:3514#include "base/files/scoped_file.h"
[email protected]bdf9bdc2013-03-13 04:23:1015#include "base/logging.h"
16#include "base/posix/eintr_wrapper.h"
avi246998d82015-12-22 02:39:0417#include "build/build_config.h"
[email protected]bdf9bdc2013-03-13 04:23:1018
19namespace IPC {
20
21// Verify that kMaxSocketNameLength is a decent size.
anujk.sharma5a7ffe2f2015-01-22 05:39:3722static_assert(sizeof(((sockaddr_un*)0)->sun_path) >= kMaxSocketNameLength,
23 "sun_path is too long.");
[email protected]bdf9bdc2013-03-13 04:23:1024
25namespace {
26
tfarina38ff6312016-03-11 09:04:4427// 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).
tfarina87498552016-02-21 19:52:4031bool MakeUnixAddrForPath(const std::string& socket_name,
32 struct sockaddr_un* unix_addr,
33 size_t* unix_addr_len) {
[email protected]bdf9bdc2013-03-13 04:23:1034 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.";
tfarina87498552016-02-21 19:52:4039 return false;
[email protected]bdf9bdc2013-03-13 04:23:1040 }
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;
tfarina87498552016-02-21 19:52:4045 return false;
[email protected]bdf9bdc2013-03-13 04:23:1046 }
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();
tfarina87498552016-02-21 19:52:4054 return true;
55}
56
tfarina38ff6312016-03-11 09:04:4457// This functions creates a unix domain socket, and set it as non-blocking.
tfarina3b10a102016-03-14 20:00:2458// If successful, |out_fd| will be set to the new file descriptor, and the
59// function will return true. Otherwise returns false.
60bool CreateUnixDomainSocket(base::ScopedFD* out_fd) {
61 DCHECK(out_fd);
62
63 // Create the unix domain socket.
tfarina87498552016-02-21 19:52:4064 base::ScopedFD fd(socket(AF_UNIX, SOCK_STREAM, 0));
65 if (!fd.is_valid()) {
66 PLOG(ERROR) << "Failed to create AF_UNIX socket.";
tfarina3b10a102016-03-14 20:00:2467 return false;
tfarina87498552016-02-21 19:52:4068 }
69
tfarina3b10a102016-03-14 20:00:2470 // Now set it as non-blocking.
tfarina87498552016-02-21 19:52:4071 if (!base::SetNonBlocking(fd.get())) {
72 PLOG(ERROR) << "base::SetNonBlocking() failed " << fd.get();
tfarina3b10a102016-03-14 20:00:2473 return false;
tfarina87498552016-02-21 19:52:4074 }
75
tfarina3b10a102016-03-14 20:00:2476 fd.swap(*out_fd);
77
78 return true;
[email protected]bdf9bdc2013-03-13 04:23:1079}
80
tfarina5586fa792016-01-26 21:23:4681bool IsRecoverableError() {
82 return errno == ECONNABORTED || errno == EMFILE || errno == ENFILE ||
83 errno == ENOMEM || errno == ENOBUFS;
84}
85
[email protected]bdf9bdc2013-03-13 04:23:1086} // namespace
87
88bool CreateServerUnixDomainSocket(const base::FilePath& socket_path,
89 int* server_listen_fd) {
90 DCHECK(server_listen_fd);
91
[email protected]bdf9bdc2013-03-13 04:23:1092 // Make sure the path we need exists.
tfarina03ab5772016-02-09 01:41:3693 base::FilePath socket_dir = socket_path.DirName();
[email protected]426d1c92013-12-03 20:08:5494 if (!base::CreateDirectory(socket_dir)) {
[email protected]bdf9bdc2013-03-13 04:23:1095 LOG(ERROR) << "Couldn't create directory: " << socket_dir.value();
96 return false;
97 }
98
tfarina7b852e02016-03-07 21:52:2599 const std::string socket_name = socket_path.value();
100
[email protected]bdf9bdc2013-03-13 04:23:10101 // 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
tfarina7b852e02016-03-07 21:52:25107 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
tfarina3b10a102016-03-14 20:00:24112 base::ScopedFD fd;
113 if (!CreateUnixDomainSocket(&fd))
tfarina7b852e02016-03-07 21:52:25114 return false;
115
[email protected]bdf9bdc2013-03-13 04:23:10116 // Bind the socket.
[email protected]42f558fd2014-03-17 19:02:35117 if (bind(fd.get(), reinterpret_cast<const sockaddr*>(&unix_addr),
[email protected]bdf9bdc2013-03-13 04:23:10118 unix_addr_len) < 0) {
tfarina03ab5772016-02-09 01:41:36119 PLOG(ERROR) << "bind " << socket_name;
[email protected]bdf9bdc2013-03-13 04:23:10120 return false;
121 }
122
123 // Start listening on the socket.
[email protected]42f558fd2014-03-17 19:02:35124 if (listen(fd.get(), SOMAXCONN) < 0) {
tfarina03ab5772016-02-09 01:41:36125 PLOG(ERROR) << "listen " << socket_name;
[email protected]bdf9bdc2013-03-13 04:23:10126 unlink(socket_name.c_str());
127 return false;
128 }
129
[email protected]42f558fd2014-03-17 19:02:35130 *server_listen_fd = fd.release();
[email protected]bdf9bdc2013-03-13 04:23:10131 return true;
132}
133
134bool CreateClientUnixDomainSocket(const base::FilePath& socket_path,
135 int* client_socket) {
136 DCHECK(client_socket);
137
[email protected]bdf9bdc2013-03-13 04:23:10138 struct sockaddr_un unix_addr;
139 size_t unix_addr_len;
tfarina87498552016-02-21 19:52:40140 if (!MakeUnixAddrForPath(socket_path.value(), &unix_addr, &unix_addr_len))
141 return false;
tfarina7b852e02016-03-07 21:52:25142
tfarina3b10a102016-03-14 20:00:24143 base::ScopedFD fd;
144 if (!CreateUnixDomainSocket(&fd))
[email protected]bdf9bdc2013-03-13 04:23:10145 return false;
[email protected]bdf9bdc2013-03-13 04:23:10146
[email protected]42f558fd2014-03-17 19:02:35147 if (HANDLE_EINTR(connect(fd.get(), reinterpret_cast<sockaddr*>(&unix_addr),
[email protected]bdf9bdc2013-03-13 04:23:10148 unix_addr_len)) < 0) {
149 PLOG(ERROR) << "connect " << socket_path.value();
150 return false;
151 }
152
[email protected]42f558fd2014-03-17 19:02:35153 *client_socket = fd.release();
[email protected]bdf9bdc2013-03-13 04:23:10154 return true;
155}
156
157bool 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
184bool 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
bene1bbc002016-07-05 16:04:36195bool ServerOnConnect(int server_listen_fd, int* server_socket) {
[email protected]bdf9bdc2013-03-13 04:23:10196 DCHECK(server_socket);
197 *server_socket = -1;
198
[email protected]42f558fd2014-03-17 19:02:35199 base::ScopedFD accept_fd(HANDLE_EINTR(accept(server_listen_fd, NULL, 0)));
200 if (!accept_fd.is_valid())
tfarina5586fa792016-01-26 21:23:46201 return IsRecoverableError();
tfarina7b852e02016-03-07 21:52:25202
tfarina3d8548c02016-01-16 02:05:38203 if (!base::SetNonBlocking(accept_fd.get())) {
204 PLOG(ERROR) << "base::SetNonBlocking() failed " << accept_fd.get();
[email protected]bdf9bdc2013-03-13 04:23:10205 // 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]42f558fd2014-03-17 19:02:35210 *server_socket = accept_fd.release();
[email protected]bdf9bdc2013-03-13 04:23:10211 return true;
212}
213
214} // namespace IPC