]> git.saurik.com Git - apple/security.git/blob - OSX/libsecurity_translocate/lib/SecTranslocateClient.cpp
Security-59754.80.3.tar.gz
[apple/security.git] / OSX / libsecurity_translocate / lib / SecTranslocateClient.cpp
1 /*
2 * Copyright (c) 2016 Apple Inc. All Rights Reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
11 * file.
12 *
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23
24 #include <string>
25
26 #include <dispatch/dispatch.h>
27 #include <xpc/xpc.h>
28 #include <unistd.h>
29
30 #include <security_utilities/unix++.h>
31 #include <security_utilities/logging.h>
32
33 #include "SecTranslocateClient.hpp"
34 #include "SecTranslocateShared.hpp"
35 #include "SecTranslocateInterface.hpp"
36
37 namespace Security {
38
39 namespace SecTranslocate {
40
41 using namespace std;
42
43 TranslocatorClient::TranslocatorClient(dispatch_queue_t q):syncQ(q)
44 {
45 if(syncQ == NULL)
46 {
47 Syslog::critical("SecTranslocate::TranslocatorClient initialized without a queue.");
48 UnixError::throwMe(EINVAL);
49 }
50
51 uint64_t flags = 0;
52 uid_t euid = geteuid();
53
54 /* 0 - is root so it gets the root lsd
55 1-300 = are treated by launch services as "role users" They share a copy of the LS Database with root
56 and thus must be sent to the root lsd. */
57 if (euid <= 300)
58 {
59 flags |= XPC_CONNECTION_MACH_SERVICE_PRIVILEGED; //forces call to the root lsd
60 }
61
62 service = xpc_connection_create_mach_service(SECTRANSLOCATE_XPC_SERVICE_NAME,
63 syncQ,
64 flags);
65 if (service == NULL)
66 {
67 Syslog::critical("SecTranslocate: TranslocatorClient, failed to create xpc mach service");
68 UnixError::throwMe(ENOMEM);
69 }
70 xpc_connection_set_event_handler(service, ^(xpc_object_t event) {
71 xpc_type_t type = xpc_get_type(event);
72 if (type == XPC_TYPE_ERROR)
73 {
74 Syslog::error("SecTranslocate, client, xpc error: %s", xpc_dictionary_get_string(event, XPC_ERROR_KEY_DESCRIPTION));
75 }
76 else
77 {
78 char* description = xpc_copy_description(event);
79 Syslog::error("SecTranslocate, client, xpc unexpected type: %s", description);
80 free(description);
81 }
82 });
83
84 dispatch_retain(syncQ);
85 xpc_connection_resume(service);
86 }
87
88 TranslocatorClient::~TranslocatorClient()
89 {
90 xpc_connection_cancel(service);
91 dispatch_release(syncQ);
92 }
93
94 string TranslocatorClient::requestTranslocation(const string& source,
95 const string& destination,
96 const TranslocationOptions flags)
97 {
98 string outPath;
99
100 //We should run translocated, so get a translocation point
101 xpc_object_t msg = xpc_dictionary_create(NULL, NULL, 0);
102
103 if( msg == NULL)
104 {
105 Syslog::error("SecTranslocate: TranslocatorClient, failed to allocate message to send");
106 UnixError::throwMe(ENOMEM);
107 }
108
109 xpc_dictionary_set_string(msg, kSecTranslocateXPCMessageFunction, kSecTranslocateXPCFuncCreate);
110 /* send the original real path rather than the calculated path to let the server do all the work */
111 xpc_dictionary_set_string(msg, kSecTranslocateXPCMessageOriginalPath, source.c_str());
112 xpc_dictionary_set_int64(msg, kSecTranslocateXPCMessageOptions, static_cast<int64_t>(flags));
113 if(!destination.empty())
114 {
115 xpc_dictionary_set_string(msg, kSecTranslocateXPCMessageDestinationPath, destination.c_str());
116 }
117
118 xpc_object_t reply = xpc_connection_send_message_with_reply_sync(service, msg);
119 xpc_release(msg);
120
121 if(reply == NULL)
122 {
123 Syslog::error("SecTranslocate, TranslocatorClient, create, no reply returned");
124 UnixError::throwMe(ENOMEM);
125 }
126
127 xpc_type_t type = xpc_get_type(reply);
128 if (type == XPC_TYPE_DICTIONARY)
129 {
130 if(int64_t error = xpc_dictionary_get_int64(reply, kSecTranslocateXPCReplyError))
131 {
132 Syslog::error("SecTranslocate, TranslocatorClient, create, error received %lld", error);
133 xpc_release(reply);
134 UnixError::throwMe((int)error);
135 }
136 const char * result = xpc_dictionary_get_string(reply, kSecTranslocateXPCReplySecurePath);
137 if (result == NULL)
138 {
139 Syslog::error("SecTranslocate, TranslocatorClient, create, no result path received");
140 xpc_release(reply);
141 UnixError::throwMe(EINVAL);
142 }
143 outPath=result;
144 xpc_release(reply);
145 }
146 else
147 {
148 const char* errorMsg = NULL;
149 if (type == XPC_TYPE_ERROR)
150 {
151 errorMsg = "SecTranslocate, TranslocatorClient, create, xpc error returned: %s";
152 }
153 else
154 {
155 errorMsg = "SecTranslocate, TranslocatorClient, create, unexpected type of return object: %s";
156 }
157 const char *s = xpc_copy_description(reply);
158 Syslog::error(errorMsg, s);
159 free((char*)s);
160 xpc_release(reply);
161 UnixError::throwMe(EINVAL);
162 }
163
164 return outPath;
165 }
166
167 string TranslocatorClient::translocatePathForUser(const TranslocationPath &originalPath, const string &destPath)
168 {
169 if (!originalPath.shouldTranslocate())
170 {
171 return originalPath.getOriginalRealPath(); //return original path if we shouldn't translocate
172 }
173
174 return requestTranslocation(originalPath.getOriginalRealPath(), destPath, TranslocationOptions::Default);
175 }
176
177 string TranslocatorClient::translocatePathForUser(const GenericTranslocationPath &originalPath, const string &destPath)
178 {
179 if (!originalPath.shouldTranslocate())
180 {
181 return originalPath.getOriginalRealPath(); //return original path if we shouldn't translocate
182 }
183
184 return requestTranslocation(originalPath.getOriginalRealPath(), destPath, TranslocationOptions::Generic);
185 }
186
187 void TranslocatorClient::appLaunchCheckin(pid_t pid)
188 {
189 xpc_object_t msg = xpc_dictionary_create(NULL, NULL, 0);
190
191 xpc_dictionary_set_string(msg, kSecTranslocateXPCMessageFunction, kSecTranslocateXPCFuncCheckIn);
192 xpc_dictionary_set_int64(msg, kSecTranslocateXPCMessagePid, pid);
193
194 /* no reply expected so just send the message and move along */
195 xpc_connection_send_message(service, msg);
196
197 xpc_release(msg);
198 }
199
200 bool TranslocatorClient::destroyTranslocatedPathForUser(const string &translocatedPath)
201 {
202 Syslog::error("SecTranslocate, TranslocatorClient, delete operation not allowed");
203 UnixError::throwMe(EPERM);
204 }
205
206 } //namespace SecTranslocate
207 } //namespace Security