blob: 6e06fe753cd08b9222e043953efa67b330380a66 [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
tfarina87498552016-02-21 19:52:4027// Returns true on success, false otherwise. If successful, fills in
[email protected]bdf9bdc2013-03-13 04:23:1028// |unix_addr| with the appropriate data for the socket, and sets
29// |unix_addr_len| to the length of the data therein.
tfarina87498552016-02-21 19:52:4030bool MakeUnixAddrForPath(const std::string& socket_name,
31 struct sockaddr_un* unix_addr,
32 size_t* unix_addr_len) {
[email protected]bdf9bdc2013-03-13 04:23:1033 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.";
tfarina87498552016-02-21 19:52:4038 return false;
[email protected]bdf9bdc2013-03-13 04:23:1039 }
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;
tfarina87498552016-02-21 19:52:4044 return false;
[email protected]bdf9bdc2013-03-13 04:23:1045 }
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();
tfarina87498552016-02-21 19:52:4053 return true;
54}
55
56// Returns a valid socket on success.
57base::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]bdf9bdc2013-03-13 04:23:1072}
73
tfarina5586fa792016-01-26 21:23:4674bool IsRecoverableError() {
75 return errno == ECONNABORTED || errno == EMFILE || errno == ENFILE ||
76 errno == ENOMEM || errno == ENOBUFS;
77}
78
[email protected]bdf9bdc2013-03-13 04:23:1079} // namespace
80
81bool CreateServerUnixDomainSocket(const base::FilePath& socket_path,
82 int* server_listen_fd) {
83 DCHECK(server_listen_fd);
84
tfarina03ab5772016-02-09 01:41:3685 const std::string socket_name = socket_path.value();
[email protected]bdf9bdc2013-03-13 04:23:1086 struct sockaddr_un unix_addr;
87 size_t unix_addr_len;
tfarina87498552016-02-21 19:52:4088 if (!MakeUnixAddrForPath(socket_name, &unix_addr, &unix_addr_len))
89 return false;
90 base::ScopedFD fd(CreateUnixDomainSocket());
[email protected]42f558fd2014-03-17 19:02:3591 if (!fd.is_valid())
[email protected]bdf9bdc2013-03-13 04:23:1092 return false;
[email protected]bdf9bdc2013-03-13 04:23:1093
94 // Make sure the path we need exists.
tfarina03ab5772016-02-09 01:41:3695 base::FilePath socket_dir = socket_path.DirName();
[email protected]426d1c92013-12-03 20:08:5496 if (!base::CreateDirectory(socket_dir)) {
[email protected]bdf9bdc2013-03-13 04:23:1097 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]42f558fd2014-03-17 19:02:35108 if (bind(fd.get(), reinterpret_cast<const sockaddr*>(&unix_addr),
[email protected]bdf9bdc2013-03-13 04:23:10109 unix_addr_len) < 0) {
tfarina03ab5772016-02-09 01:41:36110 PLOG(ERROR) << "bind " << socket_name;
[email protected]bdf9bdc2013-03-13 04:23:10111 return false;
112 }
113
114 // Start listening on the socket.
[email protected]42f558fd2014-03-17 19:02:35115 if (listen(fd.get(), SOMAXCONN) < 0) {
tfarina03ab5772016-02-09 01:41:36116 PLOG(ERROR) << "listen " << socket_name;
[email protected]bdf9bdc2013-03-13 04:23:10117 unlink(socket_name.c_str());
118 return false;
119 }
120
[email protected]42f558fd2014-03-17 19:02:35121 *server_listen_fd = fd.release();
[email protected]bdf9bdc2013-03-13 04:23:10122 return true;
123}
124
125bool CreateClientUnixDomainSocket(const base::FilePath& socket_path,
126 int* client_socket) {
127 DCHECK(client_socket);
128
[email protected]bdf9bdc2013-03-13 04:23:10129 struct sockaddr_un unix_addr;
130 size_t unix_addr_len;
tfarina87498552016-02-21 19:52:40131 if (!MakeUnixAddrForPath(socket_path.value(), &unix_addr, &unix_addr_len))
132 return false;
133 base::ScopedFD fd(CreateUnixDomainSocket());
[email protected]42f558fd2014-03-17 19:02:35134 if (!fd.is_valid())
[email protected]bdf9bdc2013-03-13 04:23:10135 return false;
[email protected]bdf9bdc2013-03-13 04:23:10136
[email protected]42f558fd2014-03-17 19:02:35137 if (HANDLE_EINTR(connect(fd.get(), reinterpret_cast<sockaddr*>(&unix_addr),
[email protected]bdf9bdc2013-03-13 04:23:10138 unix_addr_len)) < 0) {
139 PLOG(ERROR) << "connect " << socket_path.value();
140 return false;
141 }
142
[email protected]42f558fd2014-03-17 19:02:35143 *client_socket = fd.release();
[email protected]bdf9bdc2013-03-13 04:23:10144 return true;
145}
146
147bool 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
174bool 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]bdf9bdc2013-03-13 04:23:10185bool ServerAcceptConnection(int server_listen_fd, int* server_socket) {
186 DCHECK(server_socket);
187 *server_socket = -1;
188
[email protected]42f558fd2014-03-17 19:02:35189 base::ScopedFD accept_fd(HANDLE_EINTR(accept(server_listen_fd, NULL, 0)));
190 if (!accept_fd.is_valid())
tfarina5586fa792016-01-26 21:23:46191 return IsRecoverableError();
tfarina3d8548c02016-01-16 02:05:38192 if (!base::SetNonBlocking(accept_fd.get())) {
193 PLOG(ERROR) << "base::SetNonBlocking() failed " << accept_fd.get();
[email protected]bdf9bdc2013-03-13 04:23:10194 // 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]42f558fd2014-03-17 19:02:35199 *server_socket = accept_fd.release();
[email protected]bdf9bdc2013-03-13 04:23:10200 return true;
201}
202
203} // namespace IPC