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

Side by Side Diff: src/trusted/service_runtime/nacl_secure_service.c

Issue 10914138: Split secure command channel and untrusted application channel (Closed) Base URL: svn://svn.chromium.org/native_client/trunk/src/native_client
Patch Set: Fixed a few nits. Created 8 years, 2 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 | Annotate | Revision Log
OLDNEW
(Empty)
1 /*
2 * Copyright (c) 2012 The Native Client Authors. All rights reserved.
3 * Use of this source code is governed by a BSD-style license that can be
4 * found in the LICENSE file.
5 */
6
7 #include "native_client/src/trusted/service_runtime/nacl_secure_service.h"
8
9 #include "native_client/src/shared/platform/nacl_exit.h"
10 #include "native_client/src/shared/platform/nacl_log.h"
11 #include "native_client/src/shared/platform/nacl_sync.h"
12 #include "native_client/src/shared/platform/nacl_sync_checked.h"
13 #include "native_client/src/shared/srpc/nacl_srpc.h"
14
15 #include "native_client/src/trusted/fault_injection/fault_injection.h"
16 #include "native_client/src/trusted/simple_service/nacl_simple_service.h"
17 #include "native_client/src/trusted/service_runtime/sel_ldr.h"
18
19
20 int NaClSecureThreadIfFactoryFn(
21 void *factory_data,
22 NaClThreadIfStartFunction fn_ptr,
23 void *thread_data,
24 size_t thread_stack_size,
25 struct NaClThreadInterface **out_new_thread);
26
27 int NaClSecureServiceCtor(struct NaClSecureService *self,
28 struct NaClSrpcHandlerDesc const *srpc_handlers,
29 struct NaClApp *nap,
30 struct NaClDesc *service_port,
31 struct NaClDesc *sock_addr) {
32 NaClLog(4,
33 "Entered NaClSecureServiceCtor: self 0x%"NACL_PRIxPTR"\n",
34 (uintptr_t) self);
35 if (NACL_FI_ERROR_COND(
36 "NaClSecureServiceCtor__NaClSimpleServiceWithSocketCtor",
37 !NaClSimpleServiceWithSocketCtor(
38 &self->base,
39 srpc_handlers,
40 NaClSecureThreadIfFactoryFn,
41 (void *) self,
42 service_port,
43 sock_addr))) {
44 goto done;
45 }
46 if (!NaClMutexCtor(&self->mu)) {
47 NaClLog(4, "NaClMutexCtor failed\n");
48 goto failure_mutex_ctor;
49 }
50 self->nap = nap;
51 self->thread_count = 0;
52 NACL_VTBL(NaClRefCount, self) =
53 (struct NaClRefCountVtbl *) &kNaClSecureServiceVtbl;
54 return 1;
55
56 failure_mutex_ctor:
57 (*NACL_VTBL(NaClRefCount, self)->Dtor)((struct NaClRefCount *) self);
58 done:
59 return 0;
60 }
61
62 void NaClSecureServiceDtor(struct NaClRefCount *vself) {
63 struct NaClSecureService *self = (struct NaClSecureService *) vself;
64
65 if (0 != self->thread_count) {
66 NaClLog(LOG_FATAL,
67 "SecureService dtor when thread count is nonzero\n");
68 }
69 NaClMutexDtor(&self->mu);
70
71 NACL_VTBL(NaClRefCount, self) = (struct NaClRefCountVtbl const *)
72 &kNaClSimpleServiceVtbl;
73 (*NACL_VTBL(NaClRefCount, self)->Dtor)(vself);
74 }
75
76 int NaClSecureServiceConnectionFactory(
77 struct NaClSimpleService *vself,
78 struct NaClDesc *conn,
79 struct NaClSimpleServiceConnection **out) {
80 struct NaClSecureService *self =
81 (struct NaClSecureService *) vself;
82
83 /* our instance_data is not connection specific */
84 return NaClSimpleServiceConnectionFactoryWithInstanceData(
85 vself, conn, (void *) self->nap, out);
86 }
87
88 static void NaClSecureServiceThreadCountIncr(
89 struct NaClSecureService *self) {
90 NaClLog(5, "NaClSecureServiceThreadCountIncr\n");
91 NaClXMutexLock(&self->mu);
92 if (0 == ++self->thread_count) {
93 NaClLog(LOG_FATAL,
94 "NaClSecureServiceThreadCountIncr: "
95 "thread count overflow!\n");
96 }
97 NaClXMutexUnlock(&self->mu);
98 }
99
100 static void NaClSecureServiceThreadCountDecr(
101 struct NaClSecureService *self) {
102 NaClLog(5, "NaClSecureServiceThreadCountDecr\n");
103 NaClXMutexLock(&self->mu);
104 if (0 == self->thread_count) {
105 NaClLog(LOG_FATAL,
106 "NaClSecureServiceThreadCountDecr: "
107 "decrementing thread count when count is zero\n");
108 }
109 if (0 == --self->thread_count) {
110 NaClLog(4, "NaClSecureServiceThread: channel closed, exiting.\n");
111 NaClExit(0);
112 }
113 NaClXMutexUnlock(&self->mu);
114 }
115
116 struct NaClSecureThreadInterface {
117 struct NaClThreadInterface base NACL_IS_REFCOUNT_SUBCLASS;
118 struct NaClSecureService *secure_service;
119 };
120
121 extern struct NaClThreadInterfaceVtbl
122 const kNaClSecureThreadInterfaceVtbl; /* fwd */
123
124 int NaClReverseThreadIfCtor_protected(
125 struct NaClSecureThreadInterface *self,
126 void *factory_data,
127 NaClThreadIfStartFunction fn_ptr,
128 void *thread_data,
129 size_t thread_stack_size) {
130 struct NaClSecureService *service = (struct NaClSecureService *) factory_data;
131
132 NaClLog(3, "Entered NaClSecureThreadIfCtor_protected\n");
133 if (!NaClThreadInterfaceCtor_protected(
134 (struct NaClThreadInterface *) self,
135 NaClSecureThreadIfFactoryFn,
136 NaClRefCountRef((struct NaClRefCount *) service),
137 fn_ptr,
138 thread_data,
139 thread_stack_size)) {
140 NaClLog(4, "NaClThreadInterfaceCtor_protected failed\n");
141 NaClRefCountUnref((struct NaClRefCount *) service);
142 return 0;
143 }
144
145 self->secure_service = service;
146 NaClSecureServiceThreadCountIncr(service);
147
148 NACL_VTBL(NaClRefCount, self) =
149 (struct NaClRefCountVtbl *) &kNaClSecureThreadInterfaceVtbl;
150
151 NaClLog(3, "Leaving NaClSecureThreadIfCtor_protected\n");
152 return 1;
153 }
154
155 int NaClSecureThreadIfFactoryFn(
156 void *factory_data,
157 NaClThreadIfStartFunction fn_ptr,
158 void *thread_data,
159 size_t thread_stack_size,
160 struct NaClThreadInterface **out_new_thread) {
161 struct NaClSecureThreadInterface *new_thread;
162 int retval = 0; /* fail */
163
164 NaClLog(3, "Entered NaClSecureThreadIfFactoryFn\n");
165 new_thread = (struct NaClSecureThreadInterface *)
166 malloc(sizeof *new_thread);
167 if (NULL == new_thread) {
168 goto cleanup;
169 }
170
171 if (!(retval =
172 NaClReverseThreadIfCtor_protected(new_thread,
173 factory_data,
174 fn_ptr,
175 thread_data,
176 thread_stack_size))) {
177 goto cleanup;
178 }
179
180 *out_new_thread = (struct NaClThreadInterface *) new_thread;
181 new_thread = NULL;
182
183 cleanup:
184 free(new_thread);
185 NaClLog(3,
186 "Leaving NaClSecureThreadIfFactoryFn, rv %d\n",
187 retval);
188 return retval;
189 }
190
191 void NaClSecureThreadIfDtor(struct NaClRefCount *vself) {
192 struct NaClSecureThreadInterface *self =
193 (struct NaClSecureThreadInterface *) vself;
194
195 NaClRefCountUnref((struct NaClRefCount *) self->secure_service);
196 self->secure_service = NULL;
197 NACL_VTBL(NaClRefCount, self) = &kNaClRefCountVtbl;
198 (*NACL_VTBL(NaClRefCount, self)->Dtor)(vself);
199 }
200
201 void NaClSecureThreadIfLaunchCallback(struct NaClThreadInterface *self) {
202 NaClLog(4,
203 ("NaClSecureThreadIfLaunchCallback: thread 0x%"NACL_PRIxPTR
204 " is launching\n"),
205 (uintptr_t) self);
206 }
207
208 void NaClSecureThreadIfExit(struct NaClThreadInterface *vself,
209 void *exit_code) {
210 struct NaClSecureThreadInterface *self =
211 (struct NaClSecureThreadInterface *) vself;
212 NaClLog(4,
213 ("NaClSecureThreadIfExit: thread 0x%"NACL_PRIxPTR
214 " is exiting\n"),
215 (uintptr_t) vself);
216
217 NaClSecureServiceThreadCountDecr(self->secure_service);
218 NaClRefCountUnref((struct NaClRefCount *) self);
219 NaClThreadExit((int)(uintptr_t) exit_code);
220 }
221
222 struct NaClThreadInterfaceVtbl const kNaClSecureThreadInterfaceVtbl = {
223 {
224 NaClSecureThreadIfDtor,
225 },
226 NaClThreadInterfaceStartThread,
227 NaClSecureThreadIfLaunchCallback,
228 NaClSecureThreadIfExit,
229 };
230
231 struct NaClSimpleServiceVtbl const kNaClSecureServiceVtbl = {
232 {
233 NaClSecureServiceDtor,
234 },
235 NaClSecureServiceConnectionFactory,
236 NaClSimpleServiceAcceptConnection,
237 NaClSimpleServiceAcceptAndSpawnHandler,
238 NaClSimpleServiceRpcHandler,
239 };
240
241 struct NaClSecureRevClientConnHandler {
242 struct NaClSecureRevClientConnHandler *next;
243
244 /* used by NaClSimpleRevServiceClient's ClientCallback fn */
245 void (*handler)(
246 void *state,
247 struct NaClThreadInterface *tif,
248 struct NaClDesc *conn);
249 void *state;
250 };
251
252 static void NaClSecureReverseClientInternalCallback(
253 void *state,
254 struct NaClThreadInterface *tif,
255 struct NaClDesc *conn) {
256 struct NaClSecureReverseClient *self =
257 (struct NaClSecureReverseClient *) state;
258 struct NaClSecureRevClientConnHandler *hand_ptr;
259
260 NaClLog(4, "Entered NaClSecureReverseClientInternalCallback\n");
261 hand_ptr = (*NACL_VTBL(NaClSecureReverseClient, self)->RemoveHandler)(self);
262 NaClLog(4, " got callback object %"NACL_PRIxPTR"\n", (uintptr_t) hand_ptr);
263 NaClLog(4,
264 " callback:0x%"NACL_PRIxPTR"(0x%"NACL_PRIxPTR",0x%"NACL_PRIxPTR")\n",
265 (uintptr_t) hand_ptr->handler,
266 (uintptr_t) hand_ptr->state,
267 (uintptr_t) conn);
268 (*hand_ptr->handler)(hand_ptr->state, tif, conn);
269 NaClLog(4, "NaClSecureReverseClientInternalCallback: freeing memory\n");
270 free(hand_ptr);
271 NaClLog(4, "Leaving NaClSecureReverseClientInternalCallback\n");
272 }
273
274 /*
275 * Require an initial connection handler in the Ctor, so that it's
276 * obvious that a reverse client needs to accept an IMC connection
277 * from the server to get things bootstrapped.
278 */
279 int NaClSecureReverseClientCtor(
280 struct NaClSecureReverseClient *self,
281 void (*client_callback)(
282 void *, struct NaClThreadInterface*, struct NaClDesc *),
283 void *state,
284 struct NaClApp *nap) {
285 NaClLog(4,
286 ("Entered NaClSecureReverseClientCtor, self 0x%"NACL_PRIxPTR","
287 " nap 0x%"NACL_PRIxPTR"\n"),
288 (uintptr_t) self,
289 (uintptr_t) nap);
290 if (!NaClSimpleRevClientCtor(&self->base,
291 NaClSecureReverseClientInternalCallback,
292 (void *) self,
293 NaClThreadInterfaceThreadFactory,
294 (void *) NULL)) {
295 goto failure_simple_ctor;
296 }
297 NaClLog(4, "NaClSecureReverseClientCtor: Mutex\n");
298 if (!NaClMutexCtor(&self->mu)) {
299 goto failure_mutex_ctor;
300 }
301 self->nap = nap;
302 self->queue_head = (struct NaClSecureRevClientConnHandler *) NULL;
303 self->queue_insert = &self->queue_head;
304
305 NACL_VTBL(NaClRefCount, self) =
306 (struct NaClRefCountVtbl *) &kNaClSecureReverseClientVtbl;
307
308 NaClLog(4, "NaClSecureReverseClientCtor: InsertHandler\n");
309 if (!(*NACL_VTBL(NaClSecureReverseClient, self)->
310 InsertHandler)(self, client_callback, state)) {
311 goto failure_handler_insert;
312 }
313
314 NaClLog(4, "Leaving NaClSecureReverseClientCtor\n");
315 return 1;
316
317 failure_handler_insert:
318 NaClLog(4, "NaClSecureReverseClientCtor: InsertHandler failed\n");
319 NACL_VTBL(NaClRefCount, self) =
320 (struct NaClRefCountVtbl *) &kNaClSimpleRevClientVtbl;
321
322 self->nap = NULL;
323 self->queue_insert = (struct NaClSecureRevClientConnHandler **) NULL;
324 NaClMutexDtor(&self->mu);
325
326 failure_mutex_ctor:
327 NaClLog(4, "NaClSecureReverseClientCtor: Mutex failed\n");
328 (*NACL_VTBL(NaClRefCount, self)->Dtor)((struct NaClRefCount *) self);
329 failure_simple_ctor:
330 NaClLog(4, "Leaving NaClSecureReverseClientCtor\n");
331 return 0;
332 }
333
334 void NaClSecureReverseClientDtor(struct NaClRefCount *vself) {
335 struct NaClSecureReverseClient *self =
336 (struct NaClSecureReverseClient *) vself;
337
338 struct NaClSecureRevClientConnHandler *entry;
339 struct NaClSecureRevClientConnHandler *next;
340
341 for (entry = self->queue_head; NULL != entry; entry = next) {
342 next = entry->next;
343 free(entry);
344 }
345 NaClMutexDtor(&self->mu);
346
347 NACL_VTBL(NaClRefCount, self) = (struct NaClRefCountVtbl const *)
348 &kNaClSimpleRevClientVtbl;
349 (*NACL_VTBL(NaClRefCount, self)->Dtor)(vself);
350 }
351
352 /*
353 * Caller must set up handler before issuing connection request RPC on
354 * nap->reverse_channel, since otherwise the connection handler queue
355 * may be empty and the connect code would abort. Because the connect
356 * doesn't wait for a handler, we don't need a condvar.
357 *
358 * We do not need to serialize on the handlers, since the
359 * RPC-server/IMC-client implementation should not distinguish one
360 * connection from another: it is okay for two handlers to be
361 * inserted, and two connection request RPCs to be preformed
362 * (sequentially, since they are over a single channel), and have the
363 * server side spawn threads that asynchronously connect twice, in the
364 * "incorrect" order, etc.
365 */
366 int NaClSecureReverseClientInsertHandler(
367 struct NaClSecureReverseClient *self,
368 void (*handler)(
369 void *handler_state,
370 struct NaClThreadInterface *thread_if,
371 struct NaClDesc *new_conn),
372 void *state) {
373 struct NaClSecureRevClientConnHandler *entry;
374 int retval = 0; /* fail */
375
376 NaClLog(4,
377 ("NaClSecureReverseClientInsertHandler: "
378 "handler 0x%"NACL_PRIxPTR", state 0x%"NACL_PRIxPTR"\n"),
379 (uintptr_t) handler, (uintptr_t) state);
380
381 NaClXMutexLock(&self->mu);
382
383 entry = (struct NaClSecureRevClientConnHandler *) malloc(sizeof *entry);
384 if (NULL == entry) {
385 goto cleanup;
386 }
387 entry->handler = handler;
388 entry->state = state;
389 entry->next = (struct NaClSecureRevClientConnHandler *) NULL;
390 *self->queue_insert = entry;
391 self->queue_insert = &entry->next;
392 retval = 1;
393
394 cleanup:
395 NaClXMutexUnlock(&self->mu);
396 return retval;
397 }
398
399 struct NaClSecureRevClientConnHandler *NaClSecureReverseClientRemoveHandler(
400 struct NaClSecureReverseClient *self) {
401 struct NaClSecureRevClientConnHandler *head;
402
403 NaClLog(4, "Entered NaClSecureReverseClientRemoveHandler, acquiring lock\n");
404 NaClXMutexLock(&self->mu);
405 NaClLog(4, "NaClSecureReverseClientRemoveHandler, got lock\n");
406 head = self->queue_head;
407 if (NULL == head) {
408 NaClLog(LOG_FATAL,
409 "NaClSecureReverseClientRemoveHandler: empty handler queue\n");
410 }
411 if (NULL == (self->queue_head = head->next)) {
412 NaClLog(4, "NaClSecureReverseClientRemoveHandler, last elt patch up\n");
413 self->queue_insert = &self->queue_head;
414 }
415 NaClLog(4, "NaClSecureReverseClientRemoveHandler, unlocking\n");
416 NaClXMutexUnlock(&self->mu);
417
418 head->next = NULL;
419 NaClLog(4,
420 ("Leaving NaClSecureReverseClientRemoveHandler:"
421 " returning %"NACL_PRIxPTR"\n"),
422 (uintptr_t) head);
423 return head;
424 }
425
426 struct NaClSecureReverseClientVtbl const kNaClSecureReverseClientVtbl = {
427 {
428 {
429 NaClSecureReverseClientDtor,
430 },
431 },
432 NaClSecureReverseClientInsertHandler,
433 NaClSecureReverseClientRemoveHandler,
434 };
OLDNEW
« no previous file with comments | « src/trusted/service_runtime/nacl_secure_service.h ('k') | src/trusted/service_runtime/sel_ldr.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698