blob: fb64cb29165ad98cb4414451e4e48ba84f26f3f9 [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>
8#include <fcntl.h>
avi246998d82015-12-22 02:39:049#include <stddef.h>
[email protected]bdf9bdc2013-03-13 04:23:1010#include <sys/socket.h>
11#include <sys/stat.h>
12#include <sys/un.h>
13#include <unistd.h>
14
[email protected]bdf9bdc2013-03-13 04:23:1015#include "base/files/file_path.h"
thestigc9e38a22014-09-13 01:02:1116#include "base/files/file_util.h"
[email protected]42f558fd2014-03-17 19:02:3517#include "base/files/scoped_file.h"
[email protected]bdf9bdc2013-03-13 04:23:1018#include "base/logging.h"
19#include "base/posix/eintr_wrapper.h"
avi246998d82015-12-22 02:39:0420#include "build/build_config.h"
[email protected]bdf9bdc2013-03-13 04:23:1021
22namespace IPC {
23
24// Verify that kMaxSocketNameLength is a decent size.
anujk.sharma5a7ffe2f2015-01-22 05:39:3725static_assert(sizeof(((sockaddr_un*)0)->sun_path) >= kMaxSocketNameLength,
26 "sun_path is too long.");
[email protected]bdf9bdc2013-03-13 04:23:1027
28namespace {
29
30// Returns fd (>= 0) on success, -1 on failure. If successful, fills in
31// |unix_addr| with the appropriate data for the socket, and sets
32// |unix_addr_len| to the length of the data therein.
33int MakeUnixAddrForPath(const std::string& socket_name,
34 struct sockaddr_un* unix_addr,
35 size_t* unix_addr_len) {
36 DCHECK(unix_addr);
37 DCHECK(unix_addr_len);
38
39 if (socket_name.length() == 0) {
40 LOG(ERROR) << "Empty socket name provided for unix socket address.";
41 return -1;
42 }
43 // We reject socket_name.length() == kMaxSocketNameLength to make room for
44 // the NUL terminator at the end of the string.
45 if (socket_name.length() >= kMaxSocketNameLength) {
46 LOG(ERROR) << "Socket name too long: " << socket_name;
47 return -1;
48 }
49
50 // Create socket.
[email protected]42f558fd2014-03-17 19:02:3551 base::ScopedFD fd(socket(AF_UNIX, SOCK_STREAM, 0));
52 if (!fd.is_valid()) {
[email protected]bdf9bdc2013-03-13 04:23:1053 PLOG(ERROR) << "socket";
54 return -1;
55 }
[email protected]bdf9bdc2013-03-13 04:23:1056
57 // Make socket non-blocking
[email protected]42f558fd2014-03-17 19:02:3558 if (HANDLE_EINTR(fcntl(fd.get(), F_SETFL, O_NONBLOCK)) < 0) {
[email protected]bdf9bdc2013-03-13 04:23:1059 PLOG(ERROR) << "fcntl(O_NONBLOCK)";
60 return -1;
61 }
62
63 // Create unix_addr structure.
64 memset(unix_addr, 0, sizeof(struct sockaddr_un));
65 unix_addr->sun_family = AF_UNIX;
66 strncpy(unix_addr->sun_path, socket_name.c_str(), kMaxSocketNameLength);
67 *unix_addr_len =
68 offsetof(struct sockaddr_un, sun_path) + socket_name.length();
[email protected]42f558fd2014-03-17 19:02:3569 return fd.release();
[email protected]bdf9bdc2013-03-13 04:23:1070}
71
72} // namespace
73
74bool CreateServerUnixDomainSocket(const base::FilePath& socket_path,
75 int* server_listen_fd) {
76 DCHECK(server_listen_fd);
77
78 std::string socket_name = socket_path.value();
79 base::FilePath socket_dir = socket_path.DirName();
80
81 struct sockaddr_un unix_addr;
82 size_t unix_addr_len;
[email protected]42f558fd2014-03-17 19:02:3583 base::ScopedFD fd(
84 MakeUnixAddrForPath(socket_name, &unix_addr, &unix_addr_len));
85 if (!fd.is_valid())
[email protected]bdf9bdc2013-03-13 04:23:1086 return false;
[email protected]bdf9bdc2013-03-13 04:23:1087
88 // Make sure the path we need exists.
[email protected]426d1c92013-12-03 20:08:5489 if (!base::CreateDirectory(socket_dir)) {
[email protected]bdf9bdc2013-03-13 04:23:1090 LOG(ERROR) << "Couldn't create directory: " << socket_dir.value();
91 return false;
92 }
93
94 // Delete any old FS instances.
95 if (unlink(socket_name.c_str()) < 0 && errno != ENOENT) {
96 PLOG(ERROR) << "unlink " << socket_name;
97 return false;
98 }
99
100 // Bind the socket.
[email protected]42f558fd2014-03-17 19:02:35101 if (bind(fd.get(), reinterpret_cast<const sockaddr*>(&unix_addr),
[email protected]bdf9bdc2013-03-13 04:23:10102 unix_addr_len) < 0) {
103 PLOG(ERROR) << "bind " << socket_path.value();
104 return false;
105 }
106
107 // Start listening on the socket.
[email protected]42f558fd2014-03-17 19:02:35108 if (listen(fd.get(), SOMAXCONN) < 0) {
[email protected]bdf9bdc2013-03-13 04:23:10109 PLOG(ERROR) << "listen " << socket_path.value();
110 unlink(socket_name.c_str());
111 return false;
112 }
113
[email protected]42f558fd2014-03-17 19:02:35114 *server_listen_fd = fd.release();
[email protected]bdf9bdc2013-03-13 04:23:10115 return true;
116}
117
118bool CreateClientUnixDomainSocket(const base::FilePath& socket_path,
119 int* client_socket) {
120 DCHECK(client_socket);
121
122 std::string socket_name = socket_path.value();
123 base::FilePath socket_dir = socket_path.DirName();
124
125 struct sockaddr_un unix_addr;
126 size_t unix_addr_len;
[email protected]42f558fd2014-03-17 19:02:35127 base::ScopedFD fd(
128 MakeUnixAddrForPath(socket_name, &unix_addr, &unix_addr_len));
129 if (!fd.is_valid())
[email protected]bdf9bdc2013-03-13 04:23:10130 return false;
[email protected]bdf9bdc2013-03-13 04:23:10131
[email protected]42f558fd2014-03-17 19:02:35132 if (HANDLE_EINTR(connect(fd.get(), reinterpret_cast<sockaddr*>(&unix_addr),
[email protected]bdf9bdc2013-03-13 04:23:10133 unix_addr_len)) < 0) {
134 PLOG(ERROR) << "connect " << socket_path.value();
135 return false;
136 }
137
[email protected]42f558fd2014-03-17 19:02:35138 *client_socket = fd.release();
[email protected]bdf9bdc2013-03-13 04:23:10139 return true;
140}
141
142bool GetPeerEuid(int fd, uid_t* peer_euid) {
143 DCHECK(peer_euid);
144#if defined(OS_MACOSX) || defined(OS_OPENBSD) || defined(OS_FREEBSD)
145 uid_t socket_euid;
146 gid_t socket_gid;
147 if (getpeereid(fd, &socket_euid, &socket_gid) < 0) {
148 PLOG(ERROR) << "getpeereid " << fd;
149 return false;
150 }
151 *peer_euid = socket_euid;
152 return true;
153#else
154 struct ucred cred;
155 socklen_t cred_len = sizeof(cred);
156 if (getsockopt(fd, SOL_SOCKET, SO_PEERCRED, &cred, &cred_len) < 0) {
157 PLOG(ERROR) << "getsockopt " << fd;
158 return false;
159 }
160 if (static_cast<unsigned>(cred_len) < sizeof(cred)) {
161 NOTREACHED() << "Truncated ucred from SO_PEERCRED?";
162 return false;
163 }
164 *peer_euid = cred.uid;
165 return true;
166#endif
167}
168
169bool IsPeerAuthorized(int peer_fd) {
170 uid_t peer_euid;
171 if (!GetPeerEuid(peer_fd, &peer_euid))
172 return false;
173 if (peer_euid != geteuid()) {
174 DLOG(ERROR) << "Client euid is not authorised";
175 return false;
176 }
177 return true;
178}
179
180bool IsRecoverableError(int err) {
181 return errno == ECONNABORTED || errno == EMFILE || errno == ENFILE ||
182 errno == ENOMEM || errno == ENOBUFS;
183}
184
185bool 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())
[email protected]bdf9bdc2013-03-13 04:23:10191 return IsRecoverableError(errno);
[email protected]42f558fd2014-03-17 19:02:35192 if (HANDLE_EINTR(fcntl(accept_fd.get(), F_SETFL, O_NONBLOCK)) < 0) {
193 PLOG(ERROR) << "fcntl(O_NONBLOCK) " << 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