blob: a21df2fea00c9b8f355b980f1b51dcdb608341c8 [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>
avi246998d82015-12-22 02:39:048#include <stddef.h>
[email protected]bdf9bdc2013-03-13 04:23:109#include <sys/socket.h>
10#include <sys/stat.h>
11#include <sys/un.h>
12#include <unistd.h>
13
[email protected]bdf9bdc2013-03-13 04:23:1014#include "base/files/file_path.h"
thestigc9e38a22014-09-13 01:02:1115#include "base/files/file_util.h"
[email protected]42f558fd2014-03-17 19:02:3516#include "base/files/scoped_file.h"
[email protected]bdf9bdc2013-03-13 04:23:1017#include "base/logging.h"
18#include "base/posix/eintr_wrapper.h"
avi246998d82015-12-22 02:39:0419#include "build/build_config.h"
[email protected]bdf9bdc2013-03-13 04:23:1020
21namespace IPC {
22
23// Verify that kMaxSocketNameLength is a decent size.
anujk.sharma5a7ffe2f2015-01-22 05:39:3724static_assert(sizeof(((sockaddr_un*)0)->sun_path) >= kMaxSocketNameLength,
25 "sun_path is too long.");
[email protected]bdf9bdc2013-03-13 04:23:1026
27namespace {
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.
32int 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]42f558fd2014-03-17 19:02:3550 base::ScopedFD fd(socket(AF_UNIX, SOCK_STREAM, 0));
51 if (!fd.is_valid()) {
[email protected]bdf9bdc2013-03-13 04:23:1052 PLOG(ERROR) << "socket";
53 return -1;
54 }
[email protected]bdf9bdc2013-03-13 04:23:1055
56 // Make socket non-blocking
tfarina3d8548c02016-01-16 02:05:3857 if (!base::SetNonBlocking(fd.get())) {
58 PLOG(ERROR) << "base::SetNonBlocking() failed " << fd.get();
[email protected]bdf9bdc2013-03-13 04:23:1059 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]42f558fd2014-03-17 19:02:3568 return fd.release();
[email protected]bdf9bdc2013-03-13 04:23:1069}
70
71} // namespace
72
73bool 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]42f558fd2014-03-17 19:02:3582 base::ScopedFD fd(
83 MakeUnixAddrForPath(socket_name, &unix_addr, &unix_addr_len));
84 if (!fd.is_valid())
[email protected]bdf9bdc2013-03-13 04:23:1085 return false;
[email protected]bdf9bdc2013-03-13 04:23:1086
87 // Make sure the path we need exists.
[email protected]426d1c92013-12-03 20:08:5488 if (!base::CreateDirectory(socket_dir)) {
[email protected]bdf9bdc2013-03-13 04:23:1089 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]42f558fd2014-03-17 19:02:35100 if (bind(fd.get(), reinterpret_cast<const sockaddr*>(&unix_addr),
[email protected]bdf9bdc2013-03-13 04:23:10101 unix_addr_len) < 0) {
102 PLOG(ERROR) << "bind " << socket_path.value();
103 return false;
104 }
105
106 // Start listening on the socket.
[email protected]42f558fd2014-03-17 19:02:35107 if (listen(fd.get(), SOMAXCONN) < 0) {
[email protected]bdf9bdc2013-03-13 04:23:10108 PLOG(ERROR) << "listen " << socket_path.value();
109 unlink(socket_name.c_str());
110 return false;
111 }
112
[email protected]42f558fd2014-03-17 19:02:35113 *server_listen_fd = fd.release();
[email protected]bdf9bdc2013-03-13 04:23:10114 return true;
115}
116
117bool 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]42f558fd2014-03-17 19:02:35126 base::ScopedFD fd(
127 MakeUnixAddrForPath(socket_name, &unix_addr, &unix_addr_len));
128 if (!fd.is_valid())
[email protected]bdf9bdc2013-03-13 04:23:10129 return false;
[email protected]bdf9bdc2013-03-13 04:23:10130
[email protected]42f558fd2014-03-17 19:02:35131 if (HANDLE_EINTR(connect(fd.get(), reinterpret_cast<sockaddr*>(&unix_addr),
[email protected]bdf9bdc2013-03-13 04:23:10132 unix_addr_len)) < 0) {
133 PLOG(ERROR) << "connect " << socket_path.value();
134 return false;
135 }
136
[email protected]42f558fd2014-03-17 19:02:35137 *client_socket = fd.release();
[email protected]bdf9bdc2013-03-13 04:23:10138 return true;
139}
140
141bool 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
168bool 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
179bool IsRecoverableError(int err) {
180 return errno == ECONNABORTED || errno == EMFILE || errno == ENFILE ||
181 errno == ENOMEM || errno == ENOBUFS;
182}
183
184bool ServerAcceptConnection(int server_listen_fd, int* server_socket) {
185 DCHECK(server_socket);
186 *server_socket = -1;
187
[email protected]42f558fd2014-03-17 19:02:35188 base::ScopedFD accept_fd(HANDLE_EINTR(accept(server_listen_fd, NULL, 0)));
189 if (!accept_fd.is_valid())
[email protected]bdf9bdc2013-03-13 04:23:10190 return IsRecoverableError(errno);
tfarina3d8548c02016-01-16 02:05:38191 if (!base::SetNonBlocking(accept_fd.get())) {
192 PLOG(ERROR) << "base::SetNonBlocking() failed " << accept_fd.get();
[email protected]bdf9bdc2013-03-13 04:23:10193 // 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]42f558fd2014-03-17 19:02:35198 *server_socket = accept_fd.release();
[email protected]bdf9bdc2013-03-13 04:23:10199 return true;
200}
201
202} // namespace IPC