OLD | NEW |
---|---|
(Empty) | |
1 // 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> | |
9 #include <sys/socket.h> | |
10 #include <sys/stat.h> | |
11 #include <sys/un.h> | |
12 #include <unistd.h> | |
13 | |
14 #include "base/file_util.h" | |
15 #include "base/files/file_path.h" | |
16 #include "base/logging.h" | |
17 #include "base/posix/eintr_wrapper.h" | |
18 | |
19 namespace IPC { | |
20 | |
21 // Verify that kMaxSocketNameLength is a decent size. | |
22 COMPILE_ASSERT(sizeof(((sockaddr_un*)0)->sun_path) >= kMaxSocketNameLength, | |
23 BAD_SUN_PATH_LENGTH); | |
24 | |
25 namespace { | |
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. | |
30 int MakeUnixAddrForPath(const std::string& socket_name, | |
31 struct sockaddr_un* unix_addr, | |
32 size_t* unix_addr_len) { | |
33 DCHECK_GT(socket_name.length(), 0u); | |
34 DCHECK_LT(socket_name.length(), kMaxSocketNameLength); | |
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 | |
Mark Mentovai
2013/03/06 19:34:04
Thanks.
| |
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. | |
48 int fd = socket(AF_UNIX, SOCK_STREAM, 0); | |
49 if (fd < 0) { | |
50 PLOG(ERROR) << "socket"; | |
51 return -1; | |
52 } | |
53 file_util::ScopedFD scoped_fd(&fd); | |
54 | |
55 // Make socket non-blocking | |
56 if (fcntl(fd, F_SETFL, O_NONBLOCK) < 0) { | |
57 PLOG(ERROR) << "fcntl(O_NONBLOCK)"; | |
58 return -1; | |
59 } | |
60 | |
61 // Create unix_addr structure. | |
62 DCHECK(unix_addr); | |
63 memset(unix_addr, 0, sizeof(struct sockaddr_un)); | |
64 unix_addr->sun_family = AF_UNIX; | |
65 unix_addr->sun_len = socket_name.length(); | |
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(); | |
69 return *scoped_fd.release(); | |
70 } | |
71 | |
72 } // namespace | |
73 | |
74 bool 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; | |
83 int fd = MakeUnixAddrForPath(socket_name, &unix_addr, &unix_addr_len); | |
84 if (fd < 0) | |
85 return false; | |
86 file_util::ScopedFD scoped_fd(&fd); | |
87 | |
88 // Make sure the path we need exists. | |
89 if (!file_util::CreateDirectory(socket_dir)) { | |
Mark Mentovai
2013/03/06 19:34:04
Are you going to add a ScopedAllowIO here?
If so,
jeremya
2013/03/06 21:46:01
Given that this code path was previously only exec
| |
90 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; | |
Mark Mentovai
2013/03/06 19:34:04
If you’re not going to “return false” for this, is
jeremya
2013/03/06 21:46:01
I think we should probably return false when unlin
| |
97 | |
98 // Bind the socket. | |
99 if (bind(fd, reinterpret_cast<const sockaddr*>(&unix_addr), | |
100 unix_addr_len) < 0) { | |
101 PLOG(ERROR) << "bind " << socket_path.value(); | |
102 return false; | |
103 } | |
104 | |
105 // Start listening on the socket. | |
106 if (listen(fd, SOMAXCONN) < 0) { | |
107 PLOG(ERROR) << "listen " << socket_path.value(); | |
108 unlink(socket_name.c_str()); | |
109 return false; | |
110 } | |
111 | |
112 *server_listen_fd = *scoped_fd.release(); | |
113 return true; | |
114 } | |
115 | |
116 bool CreateClientUnixDomainSocket(const base::FilePath& socket_path, | |
117 int* client_socket) { | |
118 DCHECK(client_socket); | |
119 | |
120 std::string socket_name = socket_path.value(); | |
121 base::FilePath socket_dir = socket_path.DirName(); | |
122 | |
123 struct sockaddr_un unix_addr; | |
124 size_t unix_addr_len; | |
125 int fd = MakeUnixAddrForPath(socket_name, &unix_addr, &unix_addr_len); | |
126 if (fd < 0) | |
127 return false; | |
128 file_util::ScopedFD scoped_fd(&fd); | |
129 | |
130 if (HANDLE_EINTR(connect(fd, reinterpret_cast<sockaddr*>(&unix_addr), | |
131 unix_addr_len)) < 0) { | |
132 PLOG(ERROR) << "connect " << socket_path.value(); | |
133 return false; | |
134 } | |
135 | |
136 *client_socket = *scoped_fd.release(); | |
137 return true; | |
138 } | |
139 | |
140 bool GetPeerEuid(int fd, uid_t* client_euid) { | |
141 #if defined(OS_MACOSX) || defined(OS_OPENBSD) || defined(OS_FREEBSD) | |
142 uid_t peer_euid; | |
143 gid_t peer_gid; | |
144 if (getpeereid(fd, &peer_euid, &peer_gid) < 0) { | |
145 PLOG(ERROR) << "getpeereid " << fd; | |
146 return false; | |
147 } | |
148 *client_euid = peer_euid; | |
149 return true; | |
150 #else | |
151 struct ucred cred; | |
152 socklen_t cred_len = sizeof(cred); | |
153 if (getsockopt(fd, SOL_SOCKET, SO_PEERCRED, &cred, &cred_len) < 0) { | |
154 PLOG(ERROR) << "getsockopt " << fd; | |
155 return false; | |
156 } | |
157 if (static_cast<unsigned>(cred_len) < sizeof(cred)) { | |
158 NOTREACHED() << "Truncated ucred from SO_PEERCRED?"; | |
159 return false; | |
160 } | |
161 *client_euid = cred.uid; | |
162 return true; | |
163 #endif | |
164 } | |
165 | |
166 bool IsPeerAuthorized(int peer_fd) { | |
167 uid_t peer_euid; | |
168 if (!GetPeerEuid(peer_fd, &peer_euid)) | |
169 return false; | |
170 if (peer_euid != geteuid()) { | |
171 DLOG(ERROR) << "Client euid is not authorised"; | |
172 return false; | |
173 } | |
174 return true; | |
175 } | |
176 | |
177 bool IsRecoverableError(int err) { | |
178 return errno == ECONNABORTED || errno == EMFILE || errno == ENFILE || | |
Mark Mentovai
2013/03/06 19:34:04
I’m glad to see that you’ve picked out other recov
| |
179 errno == ENOMEM; | |
Mark Mentovai
2013/03/06 19:34:04
If you’re checking for ENOMEM, you should probably
jeremya
2013/03/06 21:46:01
Ah, I was wondering if it existed at all on OSX an
| |
180 } | |
181 | |
182 bool ServerAcceptConnection(int server_listen_fd, int* server_socket) { | |
183 DCHECK(server_socket); | |
Mark Mentovai
2013/03/06 19:34:04
This is obsolete now that the next line dereferenc
jeremya
2013/03/06 21:46:01
I like the DCHECKs, it's a clear indication to the
| |
184 *server_socket = -1; | |
185 | |
186 int accept_fd = HANDLE_EINTR(accept(server_listen_fd, NULL, 0)); | |
187 if (accept_fd < 0) | |
188 return IsRecoverableError(errno); | |
189 file_util::ScopedFD scoped_fd(&accept_fd); | |
190 if (fcntl(accept_fd, F_SETFL, O_NONBLOCK) < 0) { | |
191 PLOG(ERROR) << "fcntl(O_NONBLOCK) " << accept_fd; | |
192 // It's safe to keep listening on |server_listen_fd| even if the attempt to | |
193 // set O_NONBLOCK failed on the client fd. | |
194 return true; | |
195 } | |
196 | |
197 *server_socket = *scoped_fd.release(); | |
198 return true; | |
199 } | |
200 | |
201 } // namespace IPC | |
OLD | NEW |