Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(45)

Side by Side Diff: net/socket/unix_domain_socket_posix.cc

Issue 376323002: Refactor unix domain socket. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Checked uid and gid in unittests. Created 6 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "net/socket/unix_domain_socket_posix.h" 5 #include "net/socket/unix_domain_socket_posix.h"
6 6
7 #include <cstring>
8 #include <string>
9
10 #include <errno.h> 7 #include <errno.h>
11 #include <sys/socket.h> 8 #include <sys/socket.h>
12 #include <sys/stat.h> 9 #include <sys/stat.h>
13 #include <sys/types.h> 10 #include <sys/types.h>
14 #include <sys/un.h> 11 #include <sys/un.h>
15 #include <unistd.h> 12 #include <unistd.h>
16 13
14 #include <cstring>
15 #include <string>
16
17 #include "base/bind.h" 17 #include "base/bind.h"
18 #include "base/callback.h" 18 #include "base/callback.h"
19 #include "base/posix/eintr_wrapper.h" 19 #include "base/posix/eintr_wrapper.h"
20 #include "base/threading/platform_thread.h" 20 #include "base/threading/platform_thread.h"
21 #include "build/build_config.h" 21 #include "build/build_config.h"
22 #include "net/base/net_errors.h" 22 #include "net/base/net_errors.h"
23 #include "net/base/net_util.h" 23 #include "net/base/net_util.h"
24 #include "net/socket/socket_descriptor.h" 24 #include "net/socket/socket_descriptor.h"
25 #include "net/socket/unix_domain_client_socket_posix.h"
25 26
26 namespace net { 27 namespace net {
27 28
28 namespace { 29 namespace {
29 30
30 bool NoAuthenticationCallback(uid_t, gid_t) { 31 int CreateAndBind(const std::string& socket_path,
31 return true; 32 bool use_abstract_namespace,
32 } 33 SocketDescriptor* socket_fd) {
34 DCHECK(socket_fd);
33 35
34 bool GetPeerIds(int socket, uid_t* user_id, gid_t* group_id) { 36 SockaddrStorage address;
35 #if defined(OS_LINUX) || defined(OS_ANDROID) 37 if (!UnixDomainClientSocket::FillAddress(socket_path,
36 struct ucred user_cred; 38 use_abstract_namespace,
37 socklen_t len = sizeof(user_cred); 39 &address)) {
38 if (getsockopt(socket, SOL_SOCKET, SO_PEERCRED, &user_cred, &len) == -1) 40 return ERR_ADDRESS_INVALID;
39 return false; 41 }
40 *user_id = user_cred.uid; 42
41 *group_id = user_cred.gid; 43 SocketDescriptor fd = CreatePlatformSocket(PF_UNIX, SOCK_STREAM, 0);
42 #else 44 if (fd == kInvalidSocket)
43 if (getpeereid(socket, user_id, group_id) == -1) 45 return errno ? MapSystemError(errno) : ERR_UNEXPECTED;
44 return false; 46
45 #endif 47 if (bind(fd, address.addr, address.addr_len) < 0) {
46 return true; 48 int rv = MapSystemError(errno);
49 close(fd);
50 PLOG(ERROR) << "Could not bind unix domain socket to " << socket_path
51 << (use_abstract_namespace ? " (with abstract namespace)" : "");
52 return rv;
53 }
54
55 *socket_fd = fd;
56 return OK;
47 } 57 }
48 58
49 } // namespace 59 } // namespace
50 60
51 // static 61 // static
52 UnixDomainSocket::AuthCallback UnixDomainSocket::NoAuthentication() { 62 scoped_ptr<UnixDomainListenSocket>
53 return base::Bind(NoAuthenticationCallback); 63 UnixDomainListenSocket::CreateAndListenInternal(
54 }
55
56 // static
57 scoped_ptr<UnixDomainSocket> UnixDomainSocket::CreateAndListenInternal(
58 const std::string& path, 64 const std::string& path,
59 const std::string& fallback_path, 65 const std::string& fallback_path,
60 StreamListenSocket::Delegate* del, 66 StreamListenSocket::Delegate* del,
61 const AuthCallback& auth_callback, 67 const AuthCallback& auth_callback,
62 bool use_abstract_namespace) { 68 bool use_abstract_namespace) {
63 SocketDescriptor s = CreateAndBind(path, use_abstract_namespace); 69 SocketDescriptor socket_fd = kInvalidSocket;
64 if (s == kInvalidSocket && !fallback_path.empty()) 70 int rv = CreateAndBind(path, use_abstract_namespace, &socket_fd);
65 s = CreateAndBind(fallback_path, use_abstract_namespace); 71 if (rv != OK && !fallback_path.empty())
66 if (s == kInvalidSocket) 72 rv = CreateAndBind(fallback_path, use_abstract_namespace, &socket_fd);
67 return scoped_ptr<UnixDomainSocket>(); 73 if (rv != OK)
68 scoped_ptr<UnixDomainSocket> sock( 74 return scoped_ptr<UnixDomainListenSocket>();
69 new UnixDomainSocket(s, del, auth_callback)); 75 scoped_ptr<UnixDomainListenSocket> sock(
76 new UnixDomainListenSocket(socket_fd, del, auth_callback));
70 sock->Listen(); 77 sock->Listen();
71 return sock.Pass(); 78 return sock.Pass();
72 } 79 }
73 80
74 // static 81 // static
75 scoped_ptr<UnixDomainSocket> UnixDomainSocket::CreateAndListen( 82 scoped_ptr<UnixDomainListenSocket> UnixDomainListenSocket::CreateAndListen(
76 const std::string& path, 83 const std::string& path,
77 StreamListenSocket::Delegate* del, 84 StreamListenSocket::Delegate* del,
78 const AuthCallback& auth_callback) { 85 const AuthCallback& auth_callback) {
79 return CreateAndListenInternal(path, "", del, auth_callback, false); 86 return CreateAndListenInternal(path, "", del, auth_callback, false);
80 } 87 }
81 88
82 #if defined(SOCKET_ABSTRACT_NAMESPACE_SUPPORTED) 89 #if defined(SOCKET_ABSTRACT_NAMESPACE_SUPPORTED)
83 // static 90 // static
84 scoped_ptr<UnixDomainSocket> 91 scoped_ptr<UnixDomainListenSocket>
85 UnixDomainSocket::CreateAndListenWithAbstractNamespace( 92 UnixDomainListenSocket::CreateAndListenWithAbstractNamespace(
86 const std::string& path, 93 const std::string& path,
87 const std::string& fallback_path, 94 const std::string& fallback_path,
88 StreamListenSocket::Delegate* del, 95 StreamListenSocket::Delegate* del,
89 const AuthCallback& auth_callback) { 96 const AuthCallback& auth_callback) {
90 return 97 return
91 CreateAndListenInternal(path, fallback_path, del, auth_callback, true); 98 CreateAndListenInternal(path, fallback_path, del, auth_callback, true);
92 } 99 }
93 #endif 100 #endif
94 101
95 UnixDomainSocket::UnixDomainSocket( 102 UnixDomainListenSocket::UnixDomainListenSocket(
96 SocketDescriptor s, 103 SocketDescriptor s,
97 StreamListenSocket::Delegate* del, 104 StreamListenSocket::Delegate* del,
98 const AuthCallback& auth_callback) 105 const AuthCallback& auth_callback)
99 : StreamListenSocket(s, del), 106 : StreamListenSocket(s, del),
100 auth_callback_(auth_callback) {} 107 auth_callback_(auth_callback) {}
101 108
102 UnixDomainSocket::~UnixDomainSocket() {} 109 UnixDomainListenSocket::~UnixDomainListenSocket() {}
103 110
104 // static 111 void UnixDomainListenSocket::Accept() {
105 SocketDescriptor UnixDomainSocket::CreateAndBind(const std::string& path,
106 bool use_abstract_namespace) {
107 sockaddr_un addr;
108 static const size_t kPathMax = sizeof(addr.sun_path);
109 if (use_abstract_namespace + path.size() + 1 /* '\0' */ > kPathMax)
110 return kInvalidSocket;
111 const SocketDescriptor s = CreatePlatformSocket(PF_UNIX, SOCK_STREAM, 0);
112 if (s == kInvalidSocket)
113 return kInvalidSocket;
114 memset(&addr, 0, sizeof(addr));
115 addr.sun_family = AF_UNIX;
116 socklen_t addr_len;
117 if (use_abstract_namespace) {
118 // Convert the path given into abstract socket name. It must start with
119 // the '\0' character, so we are adding it. |addr_len| must specify the
120 // length of the structure exactly, as potentially the socket name may
121 // have '\0' characters embedded (although we don't support this).
122 // Note that addr.sun_path is already zero initialized.
123 memcpy(addr.sun_path + 1, path.c_str(), path.size());
124 addr_len = path.size() + offsetof(struct sockaddr_un, sun_path) + 1;
125 } else {
126 memcpy(addr.sun_path, path.c_str(), path.size());
127 addr_len = sizeof(sockaddr_un);
128 }
129 if (bind(s, reinterpret_cast<sockaddr*>(&addr), addr_len)) {
130 LOG(ERROR) << "Could not bind unix domain socket to " << path;
131 if (use_abstract_namespace)
132 LOG(ERROR) << " (with abstract namespace enabled)";
133 if (IGNORE_EINTR(close(s)) < 0)
134 LOG(ERROR) << "close() error";
135 return kInvalidSocket;
136 }
137 return s;
138 }
139
140 void UnixDomainSocket::Accept() {
141 SocketDescriptor conn = StreamListenSocket::AcceptSocket(); 112 SocketDescriptor conn = StreamListenSocket::AcceptSocket();
142 if (conn == kInvalidSocket) 113 if (conn == kInvalidSocket)
143 return; 114 return;
144 uid_t user_id; 115 uid_t user_id;
145 gid_t group_id; 116 gid_t group_id;
146 if (!GetPeerIds(conn, &user_id, &group_id) || 117 if (!UnixDomainServerSocket::GetPeerIds(conn, &user_id, &group_id) ||
147 !auth_callback_.Run(user_id, group_id)) { 118 !auth_callback_.Run(user_id, group_id)) {
148 if (IGNORE_EINTR(close(conn)) < 0) 119 if (IGNORE_EINTR(close(conn)) < 0)
149 LOG(ERROR) << "close() error"; 120 LOG(ERROR) << "close() error";
150 return; 121 return;
151 } 122 }
152 scoped_ptr<UnixDomainSocket> sock( 123 scoped_ptr<UnixDomainListenSocket> sock(
153 new UnixDomainSocket(conn, socket_delegate_, auth_callback_)); 124 new UnixDomainListenSocket(conn, socket_delegate_, auth_callback_));
154 // It's up to the delegate to AddRef if it wants to keep it around. 125 // It's up to the delegate to AddRef if it wants to keep it around.
155 sock->WatchSocket(WAITING_READ); 126 sock->WatchSocket(WAITING_READ);
156 socket_delegate_->DidAccept(this, sock.PassAs<StreamListenSocket>()); 127 socket_delegate_->DidAccept(this, sock.PassAs<StreamListenSocket>());
157 } 128 }
158 129
159 UnixDomainSocketFactory::UnixDomainSocketFactory( 130 UnixDomainListenSocketFactory::UnixDomainListenSocketFactory(
160 const std::string& path, 131 const std::string& path,
161 const UnixDomainSocket::AuthCallback& auth_callback) 132 const UnixDomainListenSocket::AuthCallback& auth_callback)
162 : path_(path), 133 : path_(path),
163 auth_callback_(auth_callback) {} 134 auth_callback_(auth_callback) {}
164 135
165 UnixDomainSocketFactory::~UnixDomainSocketFactory() {} 136 UnixDomainListenSocketFactory::~UnixDomainListenSocketFactory() {}
166 137
167 scoped_ptr<StreamListenSocket> UnixDomainSocketFactory::CreateAndListen( 138 scoped_ptr<StreamListenSocket> UnixDomainListenSocketFactory::CreateAndListen(
168 StreamListenSocket::Delegate* delegate) const { 139 StreamListenSocket::Delegate* delegate) const {
169 return UnixDomainSocket::CreateAndListen( 140 return UnixDomainListenSocket::CreateAndListen(
170 path_, delegate, auth_callback_).PassAs<StreamListenSocket>(); 141 path_, delegate, auth_callback_).PassAs<StreamListenSocket>();
171 } 142 }
172 143
173 #if defined(SOCKET_ABSTRACT_NAMESPACE_SUPPORTED) 144 #if defined(SOCKET_ABSTRACT_NAMESPACE_SUPPORTED)
174 145
175 UnixDomainSocketWithAbstractNamespaceFactory:: 146 UnixDomainListenSocketWithAbstractNamespaceFactory::
176 UnixDomainSocketWithAbstractNamespaceFactory( 147 UnixDomainListenSocketWithAbstractNamespaceFactory(
177 const std::string& path, 148 const std::string& path,
178 const std::string& fallback_path, 149 const std::string& fallback_path,
179 const UnixDomainSocket::AuthCallback& auth_callback) 150 const UnixDomainListenSocket::AuthCallback& auth_callback)
180 : UnixDomainSocketFactory(path, auth_callback), 151 : UnixDomainListenSocketFactory(path, auth_callback),
181 fallback_path_(fallback_path) {} 152 fallback_path_(fallback_path) {}
182 153
183 UnixDomainSocketWithAbstractNamespaceFactory:: 154 UnixDomainListenSocketWithAbstractNamespaceFactory::
184 ~UnixDomainSocketWithAbstractNamespaceFactory() {} 155 ~UnixDomainListenSocketWithAbstractNamespaceFactory() {}
185 156
186 scoped_ptr<StreamListenSocket> 157 scoped_ptr<StreamListenSocket>
187 UnixDomainSocketWithAbstractNamespaceFactory::CreateAndListen( 158 UnixDomainListenSocketWithAbstractNamespaceFactory::CreateAndListen(
188 StreamListenSocket::Delegate* delegate) const { 159 StreamListenSocket::Delegate* delegate) const {
189 return UnixDomainSocket::CreateAndListenWithAbstractNamespace( 160 return UnixDomainListenSocket::CreateAndListenWithAbstractNamespace(
190 path_, fallback_path_, delegate, auth_callback_) 161 path_, fallback_path_, delegate, auth_callback_)
191 .PassAs<StreamListenSocket>(); 162 .PassAs<StreamListenSocket>();
192 } 163 }
193 164
194 #endif 165 #endif
195 166
196 } // namespace net 167 } // namespace net
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698