Exemplo n.º 1
0
void NaClMetadataFromNaClDescCtor(struct NaClValidationMetadata *metadata,
                                  struct NaClDesc *desc) {
  struct NaClRichFileInfo info;
  int32_t fd = -1;

  NaClRichFileInfoCtor(&info);
  memset(metadata, 0, sizeof(*metadata));

  if (NACL_VTBL(NaClDesc, desc)->typeTag != NACL_DESC_HOST_IO)
    goto done;
  fd = ((struct NaClDescIoDesc *) desc)->hd->d;
  if (NaClGetFileOriginInfo(desc, &info))
    goto done;
  if (!info.known_file || info.file_path == NULL || info.file_path_length <= 0)
    goto done;
  NaClMetadataFromFDCtor(metadata, fd, info.file_path, info.file_path_length);
 done:
  NaClRichFileInfoDtor(&info);
}
struct NaClDesc *NaClExchangeFileTokenForMappableDesc(
    struct NaClFileToken *file_token,
    struct NaClValidationCache *validation_cache) {
  int32_t new_fd;
  char *file_path;
  uint32_t file_path_length;
  struct NaClDesc *desc = NULL;

  /*
   * Not all file loading paths will produce a valid token.  Zero is
   * an invalid token value that indicates there is nothing to
   * resolve.  In this case, assume nothing about the providence of
   * the file.
   */
  if (!(file_token->lo == 0 && file_token->hi == 0) &&
      validation_cache->ResolveFileToken != NULL &&
      validation_cache->ResolveFileToken(validation_cache->handle,
                                         file_token,
                                         &new_fd,
                                         &file_path,
                                         &file_path_length)) {
    struct NaClRichFileInfo info;

    desc = NaClDescIoDescFromHandleAllocCtor((NaClHandle) new_fd,
                                             NACL_ABI_O_RDONLY);

    /* Mark the desc as OK for mmapping. */
    NaClDescMarkSafeForMmap(desc);

    /* Provide metadata for validation. */
    NaClRichFileInfoCtor(&info);
    info.known_file = 1;
    info.file_path = file_path;  /* Takes ownership. */
    info.file_path_length = file_path_length;
    NaClSetFileOriginInfo(desc, &info);
    NaClRichFileInfoDtor(&info);
  }
  return desc;
}
struct NaClDesc *NaClDescCreateWithFilePathMetadata(NaClHandle handle,
                                                    const char *file_path) {
  struct NaClDesc *desc = NaClDescIoDescFromHandleAllocCtor(handle,
                                                            NACL_ABI_O_RDONLY);
  char *alloc_file_path;
  size_t file_path_length = strlen(file_path);

  struct NaClRichFileInfo info;

  if (desc == NULL)
    return NULL;

  /*
   * If there is no file path metadata, just return the created NaClDesc
   * without adding rich file info.
   */
  if (file_path_length == 0)
    return desc;

  /* Mark the desc as OK for mmapping. */
  NaClDescMarkSafeForMmap(desc);

  alloc_file_path = (char *) malloc(file_path_length + 1);
  if (alloc_file_path == NULL) {
    NaClDescUnref(desc);
    return NULL;
  }
  memcpy(alloc_file_path, file_path, file_path_length + 1);

  /* Provide metadata for validation. */
  NaClRichFileInfoCtor(&info);
  info.known_file = 1;
  info.file_path = alloc_file_path;  /* Takes ownership. */
  info.file_path_length = (uint32_t) file_path_length;
  NaClSetFileOriginInfo(desc, &info);
  NaClRichFileInfoDtor(&info);
  return desc;
}
Exemplo n.º 4
0
int NaClDeserializeNaClDescMetadata(
    const uint8_t *buffer,
    uint32_t buffer_length,
    struct NaClRichFileInfo *info) {
  /* Work around const issues. */
  char *file_path = NULL;
  uint32_t offset = 0;
  NaClRichFileInfoCtor(info);

  if (Deserialize(buffer, buffer_length, &info->known_file,
                  sizeof(info->known_file), &offset))
    goto on_error;

  if (info->known_file) {
    if (Deserialize(buffer, buffer_length, &info->file_path_length,
                    sizeof(info->file_path_length), &offset))
      goto on_error;
    file_path = malloc(info->file_path_length);
    if (NULL == file_path)
      goto on_error;
    if (Deserialize(buffer, buffer_length, file_path, info->file_path_length,
                    &offset))
      goto on_error;
    info->file_path = file_path;
    file_path = NULL;
  }

  /* Entire buffer consumed? */
  if (offset != buffer_length)
    goto on_error;
  return 0;

 on_error:
  free(file_path);
  NaClRichFileInfoDtor(info);
  return 1;
}