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

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: Comment fixed. 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 rv = 1;
bsy 2012/09/26 18:01:44 indent. either line up, or one space after type s
Petr Hosek 2012/09/26 18:49:34 Fixed.
163
164 NaClLog(3, "Entered NaClSecureThreadIfFactoryFn\n");
165 new_thread = (struct NaClSecureThreadInterface *)
166 malloc(sizeof *new_thread);
167 if (NULL == new_thread) {
168 rv = 0;
169 goto cleanup;
170 }
171
172 if (!(rv =
173 NaClReverseThreadIfCtor_protected(new_thread,
174 factory_data,
175 fn_ptr,
176 thread_data,
177 thread_stack_size))) {
178 goto cleanup;
179 }
180
181 *out_new_thread = (struct NaClThreadInterface *) new_thread;
182 new_thread = NULL;
183
184 cleanup:
185 free(new_thread);
186 NaClLog(3,
187 "Leaving NaClSecureThreadIfFactoryFn, rv %d\n",
188 rv);
189 return rv;
190 }
191
192 void NaClSecureThreadIfDtor(struct NaClRefCount *vself) {
193 struct NaClSecureThreadInterface *self =
194 (struct NaClSecureThreadInterface *) vself;
195
196 NaClRefCountUnref((struct NaClRefCount *) self->secure_service);
197 self->secure_service = NULL;
198 NACL_VTBL(NaClRefCount, self) = &kNaClRefCountVtbl;
199 (*NACL_VTBL(NaClRefCount, self)->Dtor)(vself);
200 }
201
202 void NaClSecureThreadIfLaunchCallback(struct NaClThreadInterface *self) {
203 NaClLog(4,
204 ("NaClSecureThreadIfLaunchCallback: thread 0x%"NACL_PRIxPTR
205 " is launching\n"),
206 (uintptr_t) self);
207 }
208
209 void NaClSecureThreadIfExit(struct NaClThreadInterface *vself,
210 void *exit_code) {
211 struct NaClSecureThreadInterface *self =
212 (struct NaClSecureThreadInterface *) vself;
213 NaClLog(4,
214 ("NaClSecureThreadIfExit: thread 0x%"NACL_PRIxPTR
215 " is exiting\n"),
216 (uintptr_t) vself);
217
218 NaClSecureServiceThreadCountDecr(self->secure_service);
219 NaClRefCountUnref((struct NaClRefCount *) self);
220 NaClThreadExit((int)(uintptr_t) exit_code);
221 }
222
223 struct NaClThreadInterfaceVtbl const kNaClSecureThreadInterfaceVtbl = {
224 {
225 NaClSecureThreadIfDtor,
226 },
227 NaClThreadInterfaceStartThread,
228 NaClSecureThreadIfLaunchCallback,
229 NaClSecureThreadIfExit,
230 };
231
232 struct NaClSimpleServiceVtbl const kNaClSecureServiceVtbl = {
233 {
234 NaClSecureServiceDtor,
235 },
236 NaClSecureServiceConnectionFactory,
237 NaClSimpleServiceAcceptConnection,
238 NaClSimpleServiceAcceptAndSpawnHandler,
239 NaClSimpleServiceRpcHandler,
240 };
241
242 struct NaClSecureRevClientConnHandler {
243 struct NaClSecureRevClientConnHandler *next;
244
245 /* used by NaClSimpleRevServiceClient's ClientCallback fn */
246 void (*handler)(
247 void *state,
248 struct NaClThreadInterface *tif,
249 struct NaClDesc *conn);
250 void *state;
251 };
252
253 static void NaClSecureReverseClientInternalCallback(
254 void *state,
255 struct NaClThreadInterface *tif,
256 struct NaClDesc *conn) {
257 struct NaClSecureReverseClient *self =
258 (struct NaClSecureReverseClient *) state;
259 struct NaClSecureRevClientConnHandler *hand_ptr;
260
261 UNREFERENCED_PARAMETER(tif);
bsy 2012/09/26 18:01:44 this is wrong, since tif is used in the hand_ptr->
Petr Hosek 2012/09/26 18:49:34 Removed.
262 NaClLog(4, "Entered NaClSecureReverseClientInternalCallback\n");
263 hand_ptr = (*NACL_VTBL(NaClSecureReverseClient, self)->RemoveHandler)(self);
264 NaClLog(4, " got callback object %"NACL_PRIxPTR"\n", (uintptr_t) hand_ptr);
265 NaClLog(4,
266 " callback:0x%"NACL_PRIxPTR"(0x%"NACL_PRIxPTR",0x%"NACL_PRIxPTR")\n",
267 (uintptr_t) hand_ptr->handler,
268 (uintptr_t) hand_ptr->state,
269 (uintptr_t) conn);
270 (*hand_ptr->handler)(hand_ptr->state, tif, conn);
271 NaClLog(4, "NaClSecureReverseClientInternalCallback: freeing memory\n");
272 free(hand_ptr);
273 NaClLog(4, "Leaving NaClSecureReverseClientInternalCallback\n");
274 }
275
276 /*
277 * Require an initial connection handler in the Ctor, so that it's
278 * obvious that a reverse client needs to accept an IMC connection
279 * from the server to get things bootstrapped.
280 */
281 int NaClSecureReverseClientCtor(
282 struct NaClSecureReverseClient *self,
283 void (*client_callback)(
284 void *, struct NaClThreadInterface*, struct NaClDesc *),
285 void *state,
286 struct NaClApp *nap) {
287 NaClLog(4,
288 ("Entered NaClSecureReverseClientCtor, self 0x%"NACL_PRIxPTR","
289 " nap 0x%"NACL_PRIxPTR"\n"),
290 (uintptr_t) self,
291 (uintptr_t) nap);
292 if (!NaClSimpleRevClientCtor(&self->base,
293 NaClSecureReverseClientInternalCallback,
294 (void *) self,
295 NaClThreadInterfaceThreadFactory,
296 (void *) NULL)) {
297 goto failure_simple_ctor;
298 }
299 NaClLog(4, "NaClSecureReverseClientCtor: Mutex\n");
300 if (!NaClMutexCtor(&self->mu)) {
301 goto failure_mutex_ctor;
302 }
303 self->nap = nap;
304 self->queue_head = (struct NaClSecureRevClientConnHandler *) NULL;
305 self->queue_insert = &self->queue_head;
306
307 NACL_VTBL(NaClRefCount, self) =
308 (struct NaClRefCountVtbl *) &kNaClSecureReverseClientVtbl;
309
310 NaClLog(4, "NaClSecureReverseClientCtor: InsertHandler\n");
311 if (!(*NACL_VTBL(NaClSecureReverseClient, self)->
312 InsertHandler)(self, client_callback, state)) {
313 goto failure_handler_insert;
314 }
315
316 NaClLog(4, "Leaving NaClSecureReverseClientCtor\n");
317 return 1;
318
319 failure_handler_insert:
320 NaClLog(4, "NaClSecureReverseClientCtor: InsertHandler failed\n");
321 NACL_VTBL(NaClRefCount, self) =
322 (struct NaClRefCountVtbl *) &kNaClSimpleRevClientVtbl;
323
324 self->nap = NULL;
325 self->queue_insert = (struct NaClSecureRevClientConnHandler **) NULL;
326 NaClMutexDtor(&self->mu);
327
328 failure_mutex_ctor:
329 NaClLog(4, "NaClSecureReverseClientCtor: Mutex failed\n");
330 (*NACL_VTBL(NaClRefCount, self)->Dtor)((struct NaClRefCount *) self);
331 failure_simple_ctor:
332 NaClLog(4, "Leaving NaClSecureReverseClientCtor\n");
333 return 0;
334 }
335
336 void NaClSecureReverseClientDtor(struct NaClRefCount *vself) {
337 struct NaClSecureReverseClient *self =
338 (struct NaClSecureReverseClient *) vself;
339
340 struct NaClSecureRevClientConnHandler *entry;
341 struct NaClSecureRevClientConnHandler *next;
342
343 for (entry = self->queue_head; NULL != entry; entry = next) {
344 next = entry->next;
345 free(entry);
346 }
347 NaClMutexDtor(&self->mu);
348
349 NACL_VTBL(NaClRefCount, self) = (struct NaClRefCountVtbl const *)
350 &kNaClSimpleRevClientVtbl;
351 (*NACL_VTBL(NaClRefCount, self)->Dtor)(vself);
352 }
353
354 /*
355 * Caller must set up handler before issuing connection request RPC on
356 * nap->reverse_channel, since otherwise the connection handler queue
357 * may be empty and the connect code would abort. Because the connect
358 * doesn't wait for a handler, we don't need a condvar.
359 *
360 * We do not need to serialize on the handlers, since the
361 * RPC-server/IMC-client implementation should not distinguish one
362 * connection from another: it is okay for two handlers to be
363 * inserted, and two connection request RPCs to be preformed
364 * (sequentially, since they are over a single channel), and have the
365 * server side spawn threads that asynchronously connect twice, in the
366 * "incorrect" order, etc.
367 */
368 int NaClSecureReverseClientInsertHandler(
369 struct NaClSecureReverseClient *self,
bsy 2012/09/26 18:01:44 indent. either line up or use single-space after
Petr Hosek 2012/09/26 18:49:34 Fixed.
370 void (*handler)(
371 void *handler_state,
372 struct NaClThreadInterface *thread_if,
373 struct NaClDesc *new_conn),
374 void *state) {
375 struct NaClSecureRevClientConnHandler *entry;
376 int retval = 0; /* fail */
377
378 NaClLog(4,
379 ("NaClSecureReverseClientInsertHandler: "
380 "handler 0x%"NACL_PRIxPTR", state 0x%"NACL_PRIxPTR"\n"),
381 (uintptr_t) handler, (uintptr_t) state);
382
383 NaClXMutexLock(&self->mu);
384
385 entry = (struct NaClSecureRevClientConnHandler *) malloc(sizeof *entry);
386 if (NULL == entry) {
387 goto cleanup;
388 }
389 entry->handler = handler;
390 entry->state = state;
391 entry->next = (struct NaClSecureRevClientConnHandler *) NULL;
392 *self->queue_insert = entry;
393 self->queue_insert = &entry->next;
394 retval = 1;
395
396 cleanup:
397 NaClXMutexUnlock(&self->mu);
398 return retval;
399 }
400
401 struct NaClSecureRevClientConnHandler *NaClSecureReverseClientRemoveHandler(
402 struct NaClSecureReverseClient *self) {
403 struct NaClSecureRevClientConnHandler *head;
404
405 NaClLog(4, "Entered NaClSecureReverseClientRemoveHandler, acquiring lock\n");
406 NaClXMutexLock(&self->mu);
407 NaClLog(4, "NaClSecureReverseClientRemoveHandler, got lock\n");
408 head = self->queue_head;
409 if (NULL == head) {
410 NaClLog(LOG_FATAL,
411 "NaClSecureReverseClientRemoveHandler: empty handler queue\n");
412 }
413 if (NULL == (self->queue_head = head->next)) {
414 NaClLog(4, "NaClSecureReverseClientRemoveHandler, last elt patch up\n");
415 self->queue_insert = &self->queue_head;
416 }
417 NaClLog(4, "NaClSecureReverseClientRemoveHandler, unlocking\n");
418 NaClXMutexUnlock(&self->mu);
419
420 head->next = NULL;
421 NaClLog(4,
422 ("Leaving NaClSecureReverseClientRemoveHandler:"
423 " returning %"NACL_PRIxPTR"\n"),
424 (uintptr_t) head);
425 return head;
426 }
427
428 struct NaClSecureReverseClientVtbl const kNaClSecureReverseClientVtbl = {
429 {
430 {
431 NaClSecureReverseClientDtor,
432 },
433 },
434 NaClSecureReverseClientInsertHandler,
435 NaClSecureReverseClientRemoveHandler,
436 };
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698