Changeset View
Changeset View
Standalone View
Standalone View
usr.sbin/bhyve/tpm_intf_crb.c
| Show First 20 Lines • Show All 159 Lines • ▼ Show 20 Lines | do { \ | ||||
| regs.rsp_size = val; \ | regs.rsp_size = val; \ | ||||
| } while (0) | } while (0) | ||||
| #define CRB_RSP_ADDR_READ(regs) (regs.rsp_addr) | #define CRB_RSP_ADDR_READ(regs) (regs.rsp_addr) | ||||
| #define CRB_RSP_ADDR_WRITE(regs, val) \ | #define CRB_RSP_ADDR_WRITE(regs, val) \ | ||||
| do { \ | do { \ | ||||
| regs.rsp_addr = val; \ | regs.rsp_addr = val; \ | ||||
| } while (0) | } while (0) | ||||
| struct tpm_cmd_hdr { | |||||
| uint16_t tag; | |||||
| uint32_t len; | |||||
| union { | |||||
| uint32_t ordinal; | |||||
| uint32_t errcode; | |||||
| }; | |||||
| } __packed; | |||||
| struct tpm_crb { | struct tpm_crb { | ||||
| struct tpm_emul *emul; | struct tpm_emul *emul; | ||||
| void *emul_sc; | void *emul_sc; | ||||
| uint8_t tpm_log_area[TPM_CRB_LOG_AREA_MINIMUM_SIZE]; | uint8_t tpm_log_area[TPM_CRB_LOG_AREA_MINIMUM_SIZE]; | ||||
| struct tpm_crb_regs regs; | struct tpm_crb_regs regs; | ||||
| pthread_t thread; | pthread_t thread; | ||||
| pthread_mutex_t mutex; | pthread_mutex_t mutex; | ||||
| pthread_cond_t cond; | pthread_cond_t cond; | ||||
| bool closing; | bool closing; | ||||
| }; | }; | ||||
| static void * | static void * | ||||
corvink: unnecessary whitespace change | |||||
| tpm_crb_thread(void *const arg) | tpm_crb_thread(void *const arg) | ||||
| { | { | ||||
| struct tpm_crb *const crb = arg; | struct tpm_crb *const crb = arg; | ||||
| pthread_mutex_lock(&crb->mutex); | pthread_mutex_lock(&crb->mutex); | ||||
| for (;;) { | for (;;) { | ||||
| /* | /* | ||||
| * We're releasing the lock after wake up. Therefore, we have to | * We're releasing the lock after wake up. Therefore, we have to | ||||
| * check the closing condition before and after going to sleep. | * check the closing condition before and after going to sleep. | ||||
| */ | */ | ||||
| if (crb->closing) | if (crb->closing) | ||||
| break; | break; | ||||
| pthread_cond_wait(&crb->cond, &crb->mutex); | pthread_cond_wait(&crb->cond, &crb->mutex); | ||||
| if (crb->closing) | if (crb->closing) | ||||
| break; | break; | ||||
| const uint64_t cmd_addr = CRB_CMD_ADDR_READ(crb->regs); | const uint64_t cmd_addr = CRB_CMD_ADDR_READ(crb->regs); | ||||
| const uint64_t rsp_addr = CRB_RSP_ADDR_READ(crb->regs); | const uint64_t rsp_addr = CRB_RSP_ADDR_READ(crb->regs); | ||||
| const uint32_t cmd_size = CRB_CMD_SIZE_READ(crb->regs); | const uint32_t cmd_size = CRB_CMD_SIZE_READ(crb->regs); | ||||
| const uint32_t rsp_size = CRB_RSP_SIZE_READ(crb->regs); | const uint32_t rsp_size = CRB_RSP_SIZE_READ(crb->regs); | ||||
| if ((cmd_addr < TPM_CRB_DATA_BUFFER_ADDRESS) || | |||||
| (cmd_size < sizeof (struct tpm_cmd_hdr)) || | |||||
| (cmd_size > TPM_CRB_DATA_BUFFER_SIZE) || | |||||
| (cmd_addr + cmd_size > | |||||
| TPM_CRB_DATA_BUFFER_ADDRESS + TPM_CRB_DATA_BUFFER_SIZE)) { | |||||
| warnx("%s: invalid cmd [%16lx/%8x] outside of TPM " | |||||
| "buffer", __func__, cmd_addr, cmd_size); | |||||
| break; | |||||
| } | |||||
| if ((rsp_addr < TPM_CRB_DATA_BUFFER_ADDRESS) || | |||||
| (rsp_size < sizeof (struct tpm_cmd_hdr)) || | |||||
| (rsp_size > TPM_CRB_DATA_BUFFER_SIZE) || | |||||
| (rsp_addr + rsp_size > | |||||
| TPM_CRB_DATA_BUFFER_ADDRESS + TPM_CRB_DATA_BUFFER_SIZE)) { | |||||
| warnx("%s: invalid rsp [%16lx/%8x] outside of TPM " | |||||
| "buffer", __func__, rsp_addr, rsp_size); | |||||
| break; | |||||
| } | |||||
| const uint64_t cmd_off = cmd_addr - TPM_CRB_DATA_BUFFER_ADDRESS; | const uint64_t cmd_off = cmd_addr - TPM_CRB_DATA_BUFFER_ADDRESS; | ||||
| const uint64_t rsp_off = rsp_addr - TPM_CRB_DATA_BUFFER_ADDRESS; | const uint64_t rsp_off = rsp_addr - TPM_CRB_DATA_BUFFER_ADDRESS; | ||||
| if (cmd_off > TPM_CRB_DATA_BUFFER_SIZE || | if (cmd_off > TPM_CRB_DATA_BUFFER_SIZE || | ||||
| cmd_off + cmd_size > TPM_CRB_DATA_BUFFER_SIZE || | cmd_off + cmd_size > TPM_CRB_DATA_BUFFER_SIZE || | ||||
| rsp_off > TPM_CRB_DATA_BUFFER_SIZE || | rsp_off > TPM_CRB_DATA_BUFFER_SIZE || | ||||
| rsp_off + rsp_size > TPM_CRB_DATA_BUFFER_SIZE) { | rsp_off + rsp_size > TPM_CRB_DATA_BUFFER_SIZE) { | ||||
| warnx( | warnx( | ||||
| "%s: invalid cmd [%16lx, %16lx] --> [%16lx, %16lx]\n\r", | "%s: invalid cmd [%16lx, %16lx] --> [%16lx, %16lx]\n\r", | ||||
| __func__, cmd_addr, cmd_addr + cmd_size, rsp_addr, | __func__, cmd_addr, cmd_addr + cmd_size, rsp_addr, | ||||
| rsp_addr + rsp_size); | rsp_addr + rsp_size); | ||||
| break; | break; | ||||
| } | } | ||||
| uint8_t cmd[TPM_CRB_DATA_BUFFER_SIZE]; | uint8_t cmd[TPM_CRB_DATA_BUFFER_SIZE]; | ||||
| memcpy(cmd, crb->regs.data_buffer, TPM_CRB_DATA_BUFFER_SIZE); | memcpy(cmd, crb->regs.data_buffer, TPM_CRB_DATA_BUFFER_SIZE); | ||||
| /* | /* | ||||
| * Do a basic sanity check of the TPM request header. We'll need | |||||
| * the TPM request length for execute_cmd() below. | |||||
| */ | |||||
| struct tpm_cmd_hdr *req = (struct tpm_cmd_hdr *)&cmd[cmd_off]; | |||||
| if (be32toh(req->len) < sizeof (struct tpm_cmd_hdr) || | |||||
| be32toh(req->len) > cmd_size) { | |||||
| warnx("%s: invalid TPM request header", __func__); | |||||
| break; | |||||
| } | |||||
| /* | |||||
| * A TPM command can take multiple seconds to execute. As we've | * A TPM command can take multiple seconds to execute. As we've | ||||
| * copied all required values and buffers at this point, we can | * copied all required values and buffers at this point, we can | ||||
| * release the mutex. | * release the mutex. | ||||
| */ | */ | ||||
| pthread_mutex_unlock(&crb->mutex); | pthread_mutex_unlock(&crb->mutex); | ||||
| /* | /* | ||||
| * The command response buffer interface uses a single buffer | * The command response buffer interface uses a single buffer | ||||
| * for sending a command to and receiving a response from the | * for sending a command to and receiving a response from the | ||||
| * tpm. To avoid reading old data from the command buffer which | * tpm. To avoid reading old data from the command buffer which | ||||
| * might be a security issue, we zero out the command buffer | * might be a security issue, we zero out the command buffer | ||||
| * before writing the response into it. The rsp_size parameter | * before writing the response into it. The rsp_size parameter | ||||
| * is controlled by the guest and it's not guaranteed that the | * is controlled by the guest and it's not guaranteed that the | ||||
| * response has a size of rsp_size (e.g. if the tpm returned an | * response has a size of rsp_size (e.g. if the tpm returned an | ||||
| * error, the response would have a different size than | * error, the response would have a different size than | ||||
| * expected). For that reason, use a second buffer for the | * expected). For that reason, use a second buffer for the | ||||
| * response. | * response. | ||||
| */ | */ | ||||
| uint8_t rsp[TPM_CRB_DATA_BUFFER_SIZE] = { 0 }; | uint8_t rsp[TPM_CRB_DATA_BUFFER_SIZE] = { 0 }; | ||||
| crb->emul->execute_cmd(crb->emul_sc, &cmd[cmd_off], cmd_size, | (void) crb->emul->execute_cmd(crb->emul_sc, req, | ||||
| &rsp[rsp_off], rsp_size); | be32toh(req->len), &rsp[rsp_off], rsp_size); | ||||
| pthread_mutex_lock(&crb->mutex); | pthread_mutex_lock(&crb->mutex); | ||||
| memset(crb->regs.data_buffer, 0, TPM_CRB_DATA_BUFFER_SIZE); | memset(crb->regs.data_buffer, 0, TPM_CRB_DATA_BUFFER_SIZE); | ||||
| memcpy(&crb->regs.data_buffer[rsp_off], &rsp[rsp_off], rsp_size); | memcpy(&crb->regs.data_buffer[rsp_off], &rsp[rsp_off], rsp_size); | ||||
| crb->regs.ctrl_start.start = false; | crb->regs.ctrl_start.start = false; | ||||
| } | } | ||||
| pthread_mutex_unlock(&crb->mutex); | pthread_mutex_unlock(&crb->mutex); | ||||
| ▲ Show 20 Lines • Show All 337 Lines • Show Last 20 Lines | |||||
unnecessary whitespace change