1 : /*
2 : * Copyright (c) 2013 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/desc_cacheability/desc_cacheability.h"
8 :
9 : #include "native_client/src/include/portability.h"
10 : #include "native_client/src/public/desc_metadata_types.h"
11 : #include "native_client/src/public/nacl_file_info.h"
12 : #include "native_client/src/shared/platform/nacl_log.h"
13 : #include "native_client/src/trusted/desc/nacl_desc_base.h"
14 : #include "native_client/src/trusted/desc/nacl_desc_file_info.h"
15 : #include "native_client/src/trusted/desc/nacl_desc_io.h"
16 : #include "native_client/src/trusted/service_runtime/include/sys/fcntl.h"
17 : #include "native_client/src/trusted/validator/rich_file_info.h"
18 : #include "native_client/src/trusted/validator/validation_cache.h"
19 :
20 : struct NaClDesc *NaClExchangeFileTokenForMappableDesc(
21 3 : struct NaClFileToken *file_token,
22 3 : struct NaClValidationCache *validation_cache) {
23 3 : int32_t new_fd;
24 3 : char *file_path;
25 3 : uint32_t file_path_length;
26 3 : struct NaClDesc *desc = NULL;
27 :
28 : /*
29 : * Not all file loading paths will produce a valid token. Zero is
30 : * an invalid token value that indicates there is nothing to
31 : * resolve. In this case, assume nothing about the providence of
32 : * the file.
33 : */
34 5 : if (NaClFileTokenIsValid(file_token) &&
35 : validation_cache->ResolveFileToken != NULL &&
36 2 : validation_cache->ResolveFileToken(validation_cache->handle,
37 : file_token,
38 : &new_fd,
39 : &file_path,
40 : &file_path_length)) {
41 2 : struct NaClRichFileInfo info;
42 :
43 2 : desc = NaClDescIoDescFromHandleAllocCtor((NaClHandle) new_fd,
44 : NACL_ABI_O_RDONLY);
45 :
46 : /* Mark the desc as OK for mmapping. */
47 2 : NaClDescMarkSafeForMmap(desc);
48 :
49 : /* Provide metadata for validation. */
50 2 : NaClRichFileInfoCtor(&info);
51 2 : info.known_file = 1;
52 2 : info.file_path = file_path; /* Takes ownership. */
53 2 : info.file_path_length = file_path_length;
54 2 : NaClSetFileOriginInfo(desc, &info);
55 2 : NaClRichFileInfoDtor(&info);
56 2 : }
57 3 : return desc;
58 : }
59 :
60 : void NaClReplaceDescIfValidationCacheAssertsMappable(
61 271 : struct NaClDesc **desc_in_out,
62 271 : struct NaClValidationCache *validation_cache) {
63 271 : struct NaClDesc *desc = *desc_in_out;
64 271 : struct NaClDesc *replacement;
65 271 : struct NaClFileToken file_token;
66 :
67 271 : if (!NaClDescGetFileToken(desc, &file_token)) {
68 269 : NaClLog(4,
69 : "NaClReplaceDescIfValidationCacheAssertsMappable: no valid"
70 : " file token\n");
71 269 : } else {
72 2 : replacement = NaClExchangeFileTokenForMappableDesc(&file_token,
73 : validation_cache);
74 2 : if (NULL != replacement) {
75 2 : NaClDescUnref(desc);
76 2 : *desc_in_out = replacement;
77 2 : }
78 : }
79 271 : }
|