static long long rtkit_get_int_property(DBusConnection *connection, const char* propname, long long* propval) { DBusMessage *m = NULL, *r = NULL; DBusMessageIter iter, subiter; dbus_int64_t i64; dbus_int32_t i32; DBusError error; int current_type; long long ret; const char * interfacestr = "org.freedesktop.RealtimeKit1"; dbus_error_init(&error); if (!(m = dbus_message_new_method_call( RTKIT_SERVICE_NAME, RTKIT_OBJECT_PATH, "org.freedesktop.DBus.Properties", "Get"))) { ret = -ENOMEM; goto finish; } if (!dbus_message_append_args( m, DBUS_TYPE_STRING, &interfacestr, DBUS_TYPE_STRING, &propname, DBUS_TYPE_INVALID)) { ret = -ENOMEM; goto finish; } if (!(r = dbus_connection_send_with_reply_and_block(connection, m, -1, &error))) { ret = translate_error(error.name); goto finish; } if (dbus_set_error_from_message(&error, r)) { ret = translate_error(error.name); goto finish; } ret = -EBADMSG; dbus_message_iter_init(r, &iter); while ((current_type = dbus_message_iter_get_arg_type (&iter)) != DBUS_TYPE_INVALID) { if (current_type == DBUS_TYPE_VARIANT) { dbus_message_iter_recurse(&iter, &subiter); while ((current_type = dbus_message_iter_get_arg_type (&subiter)) != DBUS_TYPE_INVALID) { if (current_type == DBUS_TYPE_INT32) { dbus_message_iter_get_basic(&subiter, &i32); *propval = i32; ret = 0; } if (current_type == DBUS_TYPE_INT64) { dbus_message_iter_get_basic(&subiter, &i64); *propval = i64; ret = 0; } dbus_message_iter_next (&subiter); } } dbus_message_iter_next (&iter); } finish: if (m) dbus_message_unref(m); if (r) dbus_message_unref(r); dbus_error_free(&error); return ret; }
bool Yara::load_rules(const std::string& rule_filename) { if (_current_rules == rule_filename) { return true; } else { // The previous rules and compiler have to be freed manually. _clean_compiler_and_rules(); } bool res = false; int retval; // Look for a compiled version of the rule file first. if (boost::filesystem::exists(rule_filename + "c")) { // File extension is .yarac instead of .yara. retval = yr_rules_load((rule_filename + "c").c_str(), &_rules); } else { retval = yr_rules_load(rule_filename.c_str(), &_rules); } // Yara rules compiled with a previous Yara version. Delete and recompile. if (retval == ERROR_UNSUPPORTED_FILE_VERSION) { boost::filesystem::remove(rule_filename + "c"); } if (retval != ERROR_SUCCESS && retval != ERROR_INVALID_FILE && retval != ERROR_UNSUPPORTED_FILE_VERSION) { PRINT_ERROR << "Could not load yara rules (" << translate_error(retval) << ")." << std::endl; return false; } if (retval == ERROR_SUCCESS) { return true; } else if (retval == ERROR_INVALID_FILE || retval == ERROR_UNSUPPORTED_FILE_VERSION) // Uncompiled rules { if (yr_compiler_create(&_compiler) != ERROR_SUCCESS) { return false; } yr_compiler_set_callback(_compiler, compiler_callback, nullptr); FILE* rule_file = fopen(rule_filename.c_str(), "r"); if (rule_file == nullptr) { return false; } retval = yr_compiler_add_file(_compiler, rule_file, nullptr, rule_filename.c_str()); if (retval != 0) { PRINT_ERROR << "Could not compile yara rules (" << retval << " error(s))." << std::endl; goto END; } retval = yr_compiler_get_rules(_compiler, &_rules); if (retval != ERROR_SUCCESS) { goto END; } // Save the compiled rules to improve load times. // /!\ The compiled rules will have to be deleted if the original (readable) rule file is updated! // TODO: Compare timestamps and recompile automatically. retval = yr_rules_save(_rules, (rule_filename + "c").c_str()); if (retval != ERROR_SUCCESS) { goto END; } res = true; _current_rules = rule_filename; END: if (rule_file != nullptr) { fclose(rule_file); } } return res; }
// Called when PebbleKitJS does not acknowledge receipt of a message static void out_failed_handler(DictionaryIterator *failed, AppMessageResult reason, void *context) { APP_LOG(APP_LOG_LEVEL_DEBUG, "out_failed_handler: %i - %s", reason, translate_error(reason)); }
void imagedownload_out_failed(DictionaryIterator *iter, AppMessageResult reason, void *context) { APP_LOG(APP_LOG_LEVEL_DEBUG, "Failed to send message. Reason = %s", translate_error(reason)); }
void connection_send_failed(DictionaryIterator *iterator, AppMessageResult reason, void *context) { APP_LOG(APP_LOG_LEVEL_ERROR, "send failed: %s", translate_error(reason)); }
static void inbox_dropped_callback(AppMessageResult reason, void *context) { if (debugMode) APP_LOG(APP_LOG_LEVEL_ERROR, "Message dropped! Reason: %i - %s", reason, translate_error(reason)); }
static void out_dropped_handler(DictionaryIterator *iter, AppMessageResult reason, void *context) { APP_LOG(APP_LOG_LEVEL_DEBUG, "out msg dropped: %s", translate_error(reason)); }
static void outbox_failed_callback(DictionaryIterator *iterator, AppMessageResult reason, void *context) { APP_LOG(APP_LOG_LEVEL_ERROR, "Outbox send failed: %i - %s", reason, translate_error(reason)); update_status(false); send_batch(); }
// incoming data dropped void in_dropped_handler(AppMessageResult reason, void *context) { APP_LOG(APP_LOG_LEVEL_DEBUG, "Incoming message dropped (%s)", translate_error(reason)); }
int trusty_keymaster_call(uint32_t cmd, void* in, uint32_t in_size, uint8_t* out, uint32_t* out_size) { if (handle_ < 0) { ALOGE("not connected\n"); return -EINVAL; } size_t msg_size = in_size + sizeof(struct keymaster_message); struct keymaster_message* msg = reinterpret_cast<struct keymaster_message*>(malloc(msg_size)); if (!msg) { ALOGE("failed to allocate msg buffer\n"); return -EINVAL; } msg->cmd = cmd; memcpy(msg->payload, in, in_size); ssize_t rc = write(handle_, msg, msg_size); free(msg); if (rc < 0) { ALOGE("failed to send cmd (%d) to %s: %s\n", cmd, KEYMASTER_PORT, strerror(errno)); return -errno; } size_t out_max_size = *out_size; *out_size = 0; struct iovec iov[2]; struct keymaster_message header; iov[0] = {.iov_base = &header, .iov_len = sizeof(struct keymaster_message)}; while (true) { iov[1] = {.iov_base = out + *out_size, .iov_len = std::min<uint32_t>(KEYMASTER_MAX_BUFFER_LENGTH, out_max_size - *out_size)}; rc = readv(handle_, iov, 2); if (rc < 0) { ALOGE("failed to retrieve response for cmd (%d) to %s: %s\n", cmd, KEYMASTER_PORT, strerror(errno)); return -errno; } if ((size_t)rc < sizeof(struct keymaster_message)) { ALOGE("invalid response size (%d)\n", (int)rc); return -EINVAL; } if ((cmd | KEYMASTER_RESP_BIT) != (header.cmd & ~(KEYMASTER_STOP_BIT))) { ALOGE("invalid command (%d)", header.cmd); return -EINVAL; } *out_size += ((size_t)rc - sizeof(struct keymaster_message)); if (header.cmd & KEYMASTER_STOP_BIT) { break; } } return rc; } void trusty_keymaster_disconnect() { if (handle_ >= 0) { tipc_close(handle_); } handle_ = -1; } keymaster_error_t translate_error(int err) { switch (err) { case 0: return KM_ERROR_OK; case -EPERM: case -EACCES: return KM_ERROR_SECURE_HW_ACCESS_DENIED; case -ECANCELED: return KM_ERROR_OPERATION_CANCELLED; case -ENODEV: return KM_ERROR_UNIMPLEMENTED; case -ENOMEM: return KM_ERROR_MEMORY_ALLOCATION_FAILED; case -EBUSY: return KM_ERROR_SECURE_HW_BUSY; case -EIO: return KM_ERROR_SECURE_HW_COMMUNICATION_FAILED; case -EOVERFLOW: return KM_ERROR_INVALID_INPUT_LENGTH; default: return KM_ERROR_UNKNOWN_ERROR; } } keymaster_error_t trusty_keymaster_send(uint32_t command, const keymaster::Serializable& req, keymaster::KeymasterResponse* rsp) { uint32_t req_size = req.SerializedSize(); if (req_size > TRUSTY_KEYMASTER_SEND_BUF_SIZE) { ALOGE("Request too big: %u Max size: %u", req_size, TRUSTY_KEYMASTER_SEND_BUF_SIZE); return KM_ERROR_INVALID_INPUT_LENGTH; } uint8_t send_buf[TRUSTY_KEYMASTER_SEND_BUF_SIZE]; keymaster::Eraser send_buf_eraser(send_buf, TRUSTY_KEYMASTER_SEND_BUF_SIZE); req.Serialize(send_buf, send_buf + req_size); // Send it uint8_t recv_buf[TRUSTY_KEYMASTER_RECV_BUF_SIZE]; keymaster::Eraser recv_buf_eraser(recv_buf, TRUSTY_KEYMASTER_RECV_BUF_SIZE); uint32_t rsp_size = TRUSTY_KEYMASTER_RECV_BUF_SIZE; int rc = trusty_keymaster_call(command, send_buf, req_size, recv_buf, &rsp_size); if (rc < 0) { // Reset the connection on tipc error trusty_keymaster_disconnect(); trusty_keymaster_connect(); ALOGE("tipc error: %d\n", rc); // TODO(swillden): Distinguish permanent from transient errors and set error_ appropriately. return translate_error(rc); } else { ALOGV("Received %d byte response\n", rsp_size); } const uint8_t* p = recv_buf; if (!rsp->Deserialize(&p, p + rsp_size)) { ALOGE("Error deserializing response of size %d\n", (int)rsp_size); return KM_ERROR_UNKNOWN_ERROR; } else if (rsp->error != KM_ERROR_OK) { ALOGE("Response of size %d contained error code %d\n", (int)rsp_size, (int)rsp->error); return rsp->error; } return rsp->error; }
int fscc_read_with_timeout(fscc_handle h, char *buf, unsigned size, unsigned *bytes_read, unsigned timeout) { #ifdef _WIN32 OVERLAPPED o; BOOL result; memset(&o, 0, sizeof(o)); o.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL); if (o.hEvent == NULL) return GetLastError(); result = ReadFile(h, buf, size, (DWORD*)bytes_read, &o); if (result == FALSE) { DWORD status; int e; /* There was an actual error instead of a pending read */ if ((e = GetLastError()) != ERROR_IO_PENDING) { CloseHandle(o.hEvent); return e; } do { status = WaitForSingleObject(o.hEvent, timeout); switch (status) { case WAIT_TIMEOUT: *bytes_read = 0; /* Switch to CancelIoEx if using Vista or higher and prefer the way CancelIoEx operates. */ /* CancelIoEx(h, &o); */ CancelIo(h); CloseHandle(o.hEvent); return ERROR_SUCCESS; case WAIT_FAILED: e = GetLastError(); CloseHandle(o.hEvent); return e; } } while (status != WAIT_OBJECT_0); result = GetOverlappedResult(h, &o, (DWORD *)bytes_read, TRUE); } CloseHandle(o.hEvent); return (result == TRUE) ? 0 : translate_error(GetLastError()); #else struct pollfd fds[1]; int e = 0; fds[0].fd = h; fds[0].events = POLLIN; switch (poll(fds, 1, timeout)) { case -1: e = errno; break; case 0: break; default: e = fscc_read_with_blocking(h, buf, size, bytes_read); break; } return e; #endif }
int fscc_track_interrupts_with_timeout(fscc_handle h, unsigned interrupts, unsigned *matches, unsigned timeout) { #ifdef _WIN32 OVERLAPPED o; BOOL result; unsigned bytes_transferred = 0; DWORD temp; memset(&o, 0, sizeof(o)); o.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL); if (o.hEvent == NULL) return GetLastError(); result = DeviceIoControl(h, (DWORD)FSCC_TRACK_INTERRUPTS, &interrupts, sizeof(interrupts), matches, sizeof(*matches), &temp, &o); if (result == FALSE) { DWORD status; int e; /* There was an actual error instead of a pending read */ if ((e = GetLastError()) != ERROR_IO_PENDING) { CloseHandle(o.hEvent); return e; } do { status = WaitForSingleObject(o.hEvent, timeout); switch (status) { case WAIT_TIMEOUT: bytes_transferred = 0; /* Switch to CancelIoEx if using Vista or higher and prefer the way CancelIoEx operates. */ /* CancelIoEx(h, &o); */ CancelIo(h); CloseHandle(o.hEvent); return ERROR_SUCCESS; case WAIT_FAILED: e = GetLastError(); CloseHandle(o.hEvent); return e; } } while (status != WAIT_OBJECT_0); result = GetOverlappedResult(h, &o, (DWORD *)&bytes_transferred, TRUE); } CloseHandle(o.hEvent); return (result == TRUE) ? 0 : translate_error(GetLastError()); #else UNUSED(timeout); return fscc_track_interrupts(h, interrupts, matches, NULL); #endif }
/* * Display text on screen when message failed to sends out. */ void out_failed_handler(DictionaryIterator *failed, AppMessageResult reason, void *context) { // outgoing message failed APP_LOG(APP_LOG_LEVEL_DEBUG, "In dropped: %i - %s", reason, translate_error(reason)); show_message("Ooops! Message not sent successfully."); }
/* * Enqueue attach-on-demand command to the given JVM */ static int enqueue_command(jvm_t* jvm, const char* cstr, int arg_count, const char** args) { size_t size; door_arg_t door_args; char res_buffer[RES_BUF_SIZE]; int rc, i; char* buf = NULL; int result = -1; /* * First we get the command string and create the start of the * argument string to send to the target VM: * <ver>\0<cmd>\0 */ if (cstr == NULL) { print_debug("command name is NULL\n"); goto quit; } size = strlen(PROTOCOL_VERSION) + strlen(cstr) + 2; buf = (char*)malloc(size); if (buf != NULL) { char* pos = buf; strcpy(buf, PROTOCOL_VERSION); pos += strlen(PROTOCOL_VERSION)+1; strcpy(pos, cstr); } else { set_jvm_error(JVM_ERR_OUT_OF_MEMORY); print_debug("malloc failed at %d in %s\n", __LINE__, __FILE__); goto quit; } /* * Next we iterate over the arguments and extend the buffer * to include them. */ for (i=0; i<arg_count; i++) { cstr = args[i]; if (cstr != NULL) { size_t len = strlen(cstr); char* newbuf = (char*)realloc(buf, size+len+1); if (newbuf == NULL) { set_jvm_error(JVM_ERR_OUT_OF_MEMORY); print_debug("realloc failed in %s at %d\n", __FILE__, __LINE__); goto quit; } buf = newbuf; strcpy(buf+size, cstr); size += len+1; } } /* * The arguments to the door function are in 'buf' so we now * do the door call */ door_args.data_ptr = buf; door_args.data_size = size; door_args.desc_ptr = NULL; door_args.desc_num = 0; door_args.rbuf = (char*)&res_buffer; door_args.rsize = sizeof(res_buffer); RESTARTABLE(door_call(jvm->door_fd, &door_args), rc); /* * door_call failed */ if (rc == -1) { print_debug("door_call failed\n"); } else { /* * door_call succeeded but the call didn't return the the expected jint. */ if (door_args.data_size < sizeof(int)) { print_debug("Enqueue error - reason unknown as result is truncated!"); } else { int* res = (int*)(door_args.data_ptr); if (*res != 0) { const char* msg = translate_error(*res); if (msg == NULL) { print_debug("Unable to enqueue command to target VM: %d\n", *res); } else { print_debug("Unable to enqueue command to target VM: %s\n", msg); } } else { /* * The door call should return a file descriptor to one end of * a socket pair */ if ((door_args.desc_ptr != NULL) && (door_args.desc_num == 1) && (door_args.desc_ptr->d_attributes & DOOR_DESCRIPTOR)) { result = door_args.desc_ptr->d_data.d_desc.d_descriptor; } else { print_debug("Reply from enqueue missing descriptor!\n"); } } } } quit: if (buf) free(buf); return result; }
/* * Class: sun_tools_attach_VirtualMachineImpl * Method: enqueue * Signature: (JILjava/lang/String;[Ljava/lang/Object;)V */ JNIEXPORT jint JNICALL Java_sun_tools_attach_VirtualMachineImpl_enqueue (JNIEnv *env, jclass cls, jint fd, jstring cmd, jobjectArray args) { jint arg_count, i; size_t size; jboolean isCopy; door_arg_t door_args; char res_buffer[128]; jint result = -1; int rc; const char* cstr; char* buf; /* * First we get the command string and create the start of the * argument string to send to the target VM: * <ver>\0<cmd>\0 */ cstr = JNU_GetStringPlatformChars(env, cmd, &isCopy); if (cstr == NULL) { return -1; /* pending exception */ } size = strlen(PROTOCOL_VERSION) + strlen(cstr) + 2; buf = (char*)malloc(size); if (buf != NULL) { char* pos = buf; strcpy(buf, PROTOCOL_VERSION); pos += strlen(PROTOCOL_VERSION)+1; strcpy(pos, cstr); } if (isCopy) { JNU_ReleaseStringPlatformChars(env, cmd, cstr); } if (buf == NULL) { JNU_ThrowOutOfMemoryError(env, "malloc failed"); return -1; } /* * Next we iterate over the arguments and extend the buffer * to include them. */ arg_count = (*env)->GetArrayLength(env, args); for (i=0; i<arg_count; i++) { jobject obj = (*env)->GetObjectArrayElement(env, args, i); if (obj != NULL) { cstr = JNU_GetStringPlatformChars(env, obj, &isCopy); if (cstr != NULL) { size_t len = strlen(cstr); char* newbuf = (char*)realloc(buf, size+len+1); if (newbuf != NULL) { buf = newbuf; strcpy(buf+size, cstr); size += len+1; } if (isCopy) { JNU_ReleaseStringPlatformChars(env, obj, cstr); } if (newbuf == NULL) { free(buf); JNU_ThrowOutOfMemoryError(env, "realloc failed"); return -1; } } } if ((*env)->ExceptionOccurred(env)) { free(buf); return -1; } } /* * The arguments to the door function are in 'buf' so we now * do the door call */ door_args.data_ptr = buf; door_args.data_size = size; door_args.desc_ptr = NULL; door_args.desc_num = 0; door_args.rbuf = (char*)&res_buffer; door_args.rsize = sizeof(res_buffer); RESTARTABLE(door_call(fd, &door_args), rc); /* * door_call failed */ if (rc == -1) { JNU_ThrowIOExceptionWithLastError(env, "door_call"); } else { /* * door_call succeeded but the call didn't return the expected jint. */ if (door_args.data_size < sizeof(jint)) { JNU_ThrowIOException(env, "Enqueue error - reason unknown as result is truncated!"); } else { jint* res = (jint*)(door_args.data_ptr); if (*res != JNI_OK) { const char* msg = translate_error(*res); char buf[255]; if (msg == NULL) { sprintf(buf, "Unable to enqueue command to target VM: %d", *res); } else { sprintf(buf, "Unable to enqueue command to target VM: %s", msg); } JNU_ThrowIOException(env, buf); } else { /* * The door call should return a file descriptor to one end of * a socket pair */ if ((door_args.desc_ptr != NULL) && (door_args.desc_num == 1) && (door_args.desc_ptr->d_attributes & DOOR_DESCRIPTOR)) { result = door_args.desc_ptr->d_data.d_desc.d_descriptor; } else { JNU_ThrowIOException(env, "Reply from enqueue missing descriptor!"); } } } } free(buf); return result; }
static void sync_error_handler(DictionaryResult dict_error, AppMessageResult app_message_error, void *context) { APP_LOG(APP_LOG_LEVEL_WARNING, "sync error: %s", translate_error(app_message_error)); }
static void appmsg_in_dropped(AppMessageResult reason, void *context) { APP_LOG(APP_LOG_LEVEL_DEBUG, "In dropped: %s", translate_error(reason)); }
static void in_dropped_handler(AppMessageResult reason, void *context) { APP_LOG(APP_LOG_LEVEL_DEBUG, "in msg dropped: %s", translate_error(reason)); }
static void sync_error_callback(DictionaryResult dict_error, AppMessageResult app_message_error, void *context) { APP_LOG(APP_LOG_LEVEL_DEBUG, "App Message Sync Error: %d", app_message_error); APP_LOG(APP_LOG_LEVEL_DEBUG, "In dropped: %i - %s", app_message_error, translate_error(app_message_error)); }
void sync_error_callback(DictionaryResult dict_error, AppMessageResult app_message_error, void *context) { APP_LOG(APP_LOG_LEVEL_DEBUG, "... Sync Error: %s", translate_error(app_message_error)); }
void imagedownload_dropped(AppMessageResult reason, void *context) { APP_LOG(APP_LOG_LEVEL_ERROR, "Dropped message! Reason given: %s", translate_error(reason)); }
static void outbox_failed_callback(DictionaryIterator *iterator, AppMessageResult reason, void *context) { if (debugMode) APP_LOG(APP_LOG_LEVEL_ERROR, "Outbox send failed! Reason: %i - %s", reason, translate_error(reason)); text_layer_set_text(textlayer_debug, "Cannot Contact App Svc"); }
void connection_error(DictionaryResult dict_error, AppMessageResult app_message_error, void *context) { APP_LOG(APP_LOG_LEVEL_ERROR, "dict_error=%d, app_message_error=%s", dict_error, translate_error(app_message_error)); }