]> git.saurik.com Git - apple/security.git/blob - OSX/libsecurity_translocate/lib/SecTranslocateClient.cpp
Security-57740.1.18.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::translocatePathForUser(const TranslocationPath &originalPath, const string &destPath)
95 {
96 string outPath;
97
98 if (!originalPath.shouldTranslocate())
99 {
100 return originalPath.getOriginalRealPath(); //return original path if we shouldn't translocate
101 }
102
103 //We should run translocated, so get a translocation point
104 xpc_object_t msg = xpc_dictionary_create(NULL, NULL, 0);
105
106 if( msg == NULL)
107 {
108 Syslog::error("SecTranslocate: TranslocatorClient, failed to allocate message to send");
109 UnixError::throwMe(ENOMEM);
110 }
111
112 xpc_dictionary_set_string(msg, kSecTranslocateXPCMessageFunction, kSecTranslocateXPCFuncCreate);
113 /* send the original real path rather than the calculated path to let the server do all the work */
114 xpc_dictionary_set_string(msg, kSecTranslocateXPCMessageOriginalPath, originalPath.getOriginalRealPath().c_str());
115 if(!destPath.empty())
116 {
117 xpc_dictionary_set_string(msg, kSecTranslocateXPCMessageDestinationPath, destPath.c_str());
118 }
119
120 xpc_object_t reply = xpc_connection_send_message_with_reply_sync(service, msg);
121 xpc_release(msg);
122
123 if(reply == NULL)
124 {
125 Syslog::error("SecTranslocate, TranslocatorClient, create, no reply returned");
126 UnixError::throwMe(ENOMEM);
127 }
128
129 xpc_type_t type = xpc_get_type(reply);
130 if (type == XPC_TYPE_DICTIONARY)
131 {
132 if(int64_t error = xpc_dictionary_get_int64(reply, kSecTranslocateXPCReplyError))
133 {
134 Syslog::error("SecTranslocate, TranslocatorClient, create, error received %lld", error);
135 xpc_release(reply);
136 UnixError::throwMe((int)error);
137 }
138 const char * result = xpc_dictionary_get_string(reply, kSecTranslocateXPCReplySecurePath);
139 if (result == NULL)
140 {
141 Syslog::error("SecTranslocate, TranslocatorClient, create, no result path received");
142 xpc_release(reply);
143 UnixError::throwMe(EINVAL);
144 }
145 outPath=result;
146 xpc_release(reply);
147 }
148 else
149 {
150 const char* errorMsg = NULL;
151 if (type == XPC_TYPE_ERROR)
152 {
153 errorMsg = "SecTranslocate, TranslocatorClient, create, xpc error returned: %s";
154 }
155 else
156 {
157 errorMsg = "SecTranslocate, TranslocatorClient, create, unexpected type of return object: %s";
158 }
159 const char *s = xpc_copy_description(reply);
160 Syslog::error(errorMsg, s);
161 free((char*)s);
162 xpc_release(reply);
163 UnixError::throwMe(EINVAL);
164 }
165
166 return outPath;
167 }
168
169 void TranslocatorClient::appLaunchCheckin(pid_t pid)
170 {
171 xpc_object_t msg = xpc_dictionary_create(NULL, NULL, 0);
172
173 xpc_dictionary_set_string(msg, kSecTranslocateXPCMessageFunction, kSecTranslocateXPCFuncCheckIn);
174 xpc_dictionary_set_int64(msg, kSecTranslocateXPCMessagePid, pid);
175
176 /* no reply expected so just send the message and move along */
177 xpc_connection_send_message(service, msg);
178
179 xpc_release(msg);
180 }
181
182 bool TranslocatorClient::destroyTranslocatedPathForUser(const string &translocatedPath)
183 {
184 Syslog::error("SecTranslocate, TranslocatorClient, delete operation not allowed");
185 UnixError::throwMe(EPERM);
186 return false;
187 }
188
189 } //namespace SecTranslocate
190 } //namespace Security