blob: bfc7399ebb28bba68b4186ac78d6cdf70224e34e [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
27// Returns fd (>= 0) on success, -1 on failure. If successful, fills in
28// |unix_addr| with the appropriate data for the socket, and sets
29// |unix_addr_len| to the length of the data therein.
30int MakeUnixAddrForPath(const std::string& socket_name,
31 struct sockaddr_un* unix_addr,
32 size_t* unix_addr_len) {
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.";
38 return -1;
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;
44 return -1;
45 }
46
47 // Create socket.
[email protected]42f558fd2014-03-17 19:02:3548 base::ScopedFD fd(socket(AF_UNIX, SOCK_STREAM, 0));
49 if (!fd.is_valid()) {
[email protected]bdf9bdc2013-03-13 04:23:1050 PLOG(ERROR) << "socket";
51 return -1;
52 }
[email protected]bdf9bdc2013-03-13 04:23:1053
54 // Make socket non-blocking
tfarina3d8548c02016-01-16 02:05:3855 if (!base::SetNonBlocking(fd.get())) {
56 PLOG(ERROR) << "base::SetNonBlocking() failed " << fd.get();
[email protected]bdf9bdc2013-03-13 04:23:1057 return -1;
58 }
59
60 // Create unix_addr structure.
61 memset(unix_addr, 0, sizeof(struct sockaddr_un));
62 unix_addr->sun_family = AF_UNIX;
63 strncpy(unix_addr->sun_path, socket_name.c_str(), kMaxSocketNameLength);
64 *unix_addr_len =
65 offsetof(struct sockaddr_un, sun_path) + socket_name.length();
[email protected]42f558fd2014-03-17 19:02:3566 return fd.release();
[email protected]bdf9bdc2013-03-13 04:23:1067}
68
tfarina5586fa792016-01-26 21:23:4669bool IsRecoverableError() {
70 return errno == ECONNABORTED || errno == EMFILE || errno == ENFILE ||
71 errno == ENOMEM || errno == ENOBUFS;
72}
73
[email protected]bdf9bdc2013-03-13 04:23:1074} // namespace
75
76bool CreateServerUnixDomainSocket(const base::FilePath& socket_path,
77 int* server_listen_fd) {
78 DCHECK(server_listen_fd);
79
80 std::string socket_name = socket_path.value();
81 base::FilePath socket_dir = socket_path.DirName();
82
83 struct sockaddr_un unix_addr;
84 size_t unix_addr_len;
[email protected]42f558fd2014-03-17 19:02:3585 base::ScopedFD fd(
86 MakeUnixAddrForPath(socket_name, &unix_addr, &unix_addr_len));
87 if (!fd.is_valid())
[email protected]bdf9bdc2013-03-13 04:23:1088 return false;
[email protected]bdf9bdc2013-03-13 04:23:1089
90 // Make sure the path we need exists.
[email protected]426d1c92013-12-03 20:08:5491 if (!base::CreateDirectory(socket_dir)) {
[email protected]bdf9bdc2013-03-13 04:23:1092 LOG(ERROR) << "Couldn't create directory: " << socket_dir.value();
93 return false;
94 }
95
96 // Delete any old FS instances.
97 if (unlink(socket_name.c_str()) < 0 && errno != ENOENT) {
98 PLOG(ERROR) << "unlink " << socket_name;
99 return false;
100 }
101
102 // Bind the socket.
[email protected]42f558fd2014-03-17 19:02:35103 if (bind(fd.get(), reinterpret_cast<const sockaddr*>(&unix_addr),
[email protected]bdf9bdc2013-03-13 04:23:10104 unix_addr_len) < 0) {
105 PLOG(ERROR) << "bind " << socket_path.value();
106 return false;
107 }
108
109 // Start listening on the socket.
[email protected]42f558fd2014-03-17 19:02:35110 if (listen(fd.get(), SOMAXCONN) < 0) {
[email protected]bdf9bdc2013-03-13 04:23:10111 PLOG(ERROR) << "listen " << socket_path.value();
112 unlink(socket_name.c_str());
113 return false;
114 }
115
[email protected]42f558fd2014-03-17 19:02:35116 *server_listen_fd = fd.release();
[email protected]bdf9bdc2013-03-13 04:23:10117 return true;
118}
119
120bool CreateClientUnixDomainSocket(const base::FilePath& socket_path,
121 int* client_socket) {
122 DCHECK(client_socket);
123
[email protected]bdf9bdc2013-03-13 04:23:10124 struct sockaddr_un unix_addr;
125 size_t unix_addr_len;
[email protected]42f558fd2014-03-17 19:02:35126 base::ScopedFD fd(
tfarina4b0ab872016-01-26 00:16:38127 MakeUnixAddrForPath(socket_path.value(), &unix_addr, &unix_addr_len));
[email protected]42f558fd2014-03-17 19:02:35128 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
[email protected]bdf9bdc2013-03-13 04:23:10179bool ServerAcceptConnection(int server_listen_fd, int* server_socket) {
180 DCHECK(server_socket);
181 *server_socket = -1;
182
[email protected]42f558fd2014-03-17 19:02:35183 base::ScopedFD accept_fd(HANDLE_EINTR(accept(server_listen_fd, NULL, 0)));
184 if (!accept_fd.is_valid())
tfarina5586fa792016-01-26 21:23:46185 return IsRecoverableError();
tfarina3d8548c02016-01-16 02:05:38186 if (!base::SetNonBlocking(accept_fd.get())) {
187 PLOG(ERROR) << "base::SetNonBlocking() failed " << accept_fd.get();
[email protected]bdf9bdc2013-03-13 04:23:10188 // It's safe to keep listening on |server_listen_fd| even if the attempt to
189 // set O_NONBLOCK failed on the client fd.
190 return true;
191 }
192
[email protected]42f558fd2014-03-17 19:02:35193 *server_socket = accept_fd.release();
[email protected]bdf9bdc2013-03-13 04:23:10194 return true;
195}
196
197} // namespace IPC