vtpm Mini-Os domain: vtpm_managerd
PATCH 1/7 vtpm Mini-Os domain: vtpm_managerd
The following is a major patch to the vtpm_manager. I apologize in advance for submitting such a large patch. Let me know if you'd like me to break it up.
This patch contains the following:
-A new routine to flush all handles from the tpm everytime vtpm_managerd is started. Previously if the manager would crash it would leave stale keys and auth handles in the tpm. After restarting the manager several times and letting it crash, the tpm would run out of memory and the manager would fail to start with a TPM_NO_SPACE error. -Several memory errors fixed such as uninitialized reads and memory leaks.
-Fixed the signal handler to work with blocking IO and threads correctly. Previously the manager would not shutdown correctly when you tried to Ctrl+C close it on 64 bit.
-Removal of VTPM_MULTI_VM #defines. These were supposed to be a partial implementation for a way to run vtpms in multiple domains but it was never finished. It doesn't appear there has been any development on this for several years. Since vtpm-stubdom performs this functionality I removed these #defines and #ifdefs to make the code a little cleaner. -New vTPM ordinals: VTPM_SAVE_KEY and VTPM_LOAD_KEY. The vtpm domains encrypt their persistent state themselves, they only request the manager to store the key.
-#define VTPM_STUBDOM will build the manager in stubdom mode. The manager can only work in either vtpm-stubdom or vtpmd mode. You cannot combine them. Clearing out the manager state will be required.
# rm /var/vtpm/VTPM /var/vtpm/vtpm_dm*
tools/vtpm_manager/README +4 -3
@@ -43,9 +43,6 @@ LOGGING_MODULES -> How extensive logging happens see util/log.h for more info -VTPM_MULTI_VM -> Defined: VTPMs run in their own VMs - Not Defined (default): VTPMs are processes - # Debugging flags that may disappear without notice in the future DUMMY_BACKEND -> vtpm_manager listens on /tmp/in.fifo and
@@ -59,6 +56,10 @@ lost. However this has no protection from malicious app issuing a TPM_OwnerClear to wipe the TPM +VTPM_STUBDOM -> vtpm_manager runs in vtpm_stubdom mode with + vtpm instances running in mini-os domains + (see: stubdom/vtpm/README for details) + Requirements ============ - xen-unstable
tools/vtpm_manager/Rules.mk +3 -3
@@ -40,12 +40,12 @@ #CFLAGS += -DLOGGING_MODULES=0x0 #CFLAGS += -DLOGGING_MODULES=0xff -# Use frontend/backend pairs between manager & DMs? -#CFLAGS += -DVTPM_MULTI_VM - # vtpm_manager listens on fifo's rather than backend #CFLAGS += -DDUMMY_BACKEND +# Build for use with vtpm-stubdom +#CFLAGS += -DVTPM_STUBDOM + # TCS talks to fifo's rather than /dev/tpm. TPM Emulator assumed on fifos #CFLAGS += -DDUMMY_TPM
tools/vtpm_manager/crypto/crypto.h +2 -0
@@ -127,6 +127,8 @@/*[IN]*/ BYTE *modulus, CRYPTO_INFO* cryptoInfo); +void Crypto_RSACryptoInfoFree (CRYPTO_INFO * cryptoInfo); +// // symmetric pack and unpack operations //
tools/vtpm_manager/crypto/rsa.c +5 -0
@@ -149,6 +149,10 @@ } +void Crypto_RSACryptoInfoFree (CRYPTO_INFO * cryptoInfo) { +RSA_free (cryptoInfo->keyInfo); +} + int Crypto_RSAEnc( CRYPTO_INFO *key, UINT32 inDataSize, BYTE *inData,
@@ -161,6 +165,7 @@if (paddedData ==NULL )return -1 ; +memset (paddedData,0 ,sizeof (BYTE ) * paddedDataSize); *outDataSize =0 ;
tools/vtpm_manager/crypto/sym_crypto.c +8 -6
@@ -41,6 +41,7 @@#include <openssl/rand.h> #include "tcg.h" +#include "log.h" #include "sym_crypto.h" typedef enum crypt_op_type_t {
@@ -119,11 +120,11 @@buffer_init_const (&iv,EVP_MAX_IV_LENGTH ,ZERO_IV ); -buffer_init (o_cipher, - clear->size + -EVP_CIPHER_iv_length (key->cipher) + -EVP_CIPHER_block_size (key->cipher), -0 ); +TPMTRYRETURN (buffer_init (o_cipher, + clear->size + +EVP_CIPHER_iv_length (key->cipher) + +EVP_CIPHER_block_size (key->cipher), +0 ) );// copy the IV into the front buffer_copy (o_cipher, &iv);
@@ -139,6 +140,7 @@ goto egress; abort_egress: +buffer_free (o_cipher); egress:
@@ -170,7 +172,7 @@// and decrypt TPMTRYRETURN (ossl_symcrypto_op (key, &cipher_alias, &iv, o_clear,CRYPT_DECRYPT ) ); - + goto egress; abort_egress:
tools/vtpm_manager/manager/dmictl.c +8 -1
@@ -116,6 +116,13 @@return (VTPM_Close_DMI_Extra (dmi_res) ); } + +void free_dmi (VTPM_DMI_RESOURCE *dmi_res) { +if (dmi_res !=NULL ) { +free (dmi_res->NVMLocation); + } +free (dmi_res); +}TPM_RESULT VTPM_Handle_New_DMI (const buffer_t *param_buf) {
@@ -254,7 +261,7 @@// Close DMI first TPMTRYRETURN (close_dmi ( dmi_res )); -free ( dmi_res ); +free_dmi (dmi_res); status=TPM_SUCCESS ; goto egress;
tools/vtpm_manager/manager/securestorage.c +255 -17
396 lines not on this page · raw message
tools/vtpm_manager/manager/vtpm_ipc.c +50 -8
100 lines not on this page · raw message
tools/vtpm_manager/manager/vtpm_ipc.h +5 -1
@@ -39,10 +39,14 @@ #ifndef __VTPM_IO_H__ #define __VTPM_IO_H__ +#include <signal.h> + #include "tcg.h" #define VTPM_IPC_CLOSED -1 +extern volatile sig_atomic_t IPC_QUIT_FLAG; + // Represents an (somewhat) abstracted io handle. typedef struct vtpm_ipc_handle_t { int fh; // IO handle.
@@ -53,7 +57,7 @@ } vtpm_ipc_handle_t; -int vtpm_ipc_init (vtpm_ipc_handle_t *ioh,char * name,int flags,BOOL create); +int vtpm_ipc_init (vtpm_ipc_handle_t *ioh,char * name,int flags,BOOL create,BOOL preopen);// Create the file that needs opening. Used only for FIFOs // FYI: This may cause problems in other file IO schemes. We'll see.
tools/vtpm_manager/manager/vtpm_manager.c +87 -5
140 lines not on this page · raw message
tools/vtpm_manager/manager/vtpm_manager.h +22 -0
@@ -61,6 +61,9 @@#define VTPM_ORD_TPMCOMMAND (VTPM_ORD_BASE + 3) // DMI issues HW TPM Command#define VTPM_ORD_GET_MIG_KEY (VTPM_ORD_BASE + 4) // Get manager's migration key#define VTPM_ORD_LOAD_MIG_KEY (VTPM_ORD_BASE + 5) // load dest migration key +#define VTPM_ORD_GETNVMSIZE (VTPM_ORD_BASE + 6) // DMI requests the size of nvm storage blob +#define VTPM_ORD_SAVEKEY (VTPM_ORD_BASE + 7) // DMI requests encryption key for persistent storage +#define VTPM_ORD_LOADKEY (VTPM_ORD_BASE + 8) // DMI requests symkey to be regenerated// Priviledged VTPM Commands (From management console) #define VTPM_ORD_OPEN (VTPM_PRIV_BASE + 1) // Creates/reopens DMI
@@ -147,4 +150,23 @@*********************************************************************/ +#ifndef VTPM_STUBDOM +#define TPM_EMULATOR_PATH "/usr/bin/vtpmd" +#endif + +#define VTPM_BE_FNAME "/dev/vtpm" +#define VTPM_DUMMY_TX_BE_FNAME "/var/vtpm/fifos/dummy_out.fifo" +#define VTPM_DUMMY_RX_BE_FNAME "/var/vtpm/fifos/dummy_in.fifo" +#ifndef VTPM_STUBDOM +#define VTPM_TX_TPM_FNAME "/var/vtpm/fifos/tpm_cmd_to_%d.fifo" +#define VTPM_RX_TPM_FNAME "/var/vtpm/fifos/tpm_rsp_from_all.fifo" +#define VTPM_TX_VTPM_FNAME "/var/vtpm/fifos/vtpm_rsp_to_%d.fifo" +#define VTPM_RX_VTPM_FNAME "/var/vtpm/fifos/vtpm_cmd_from_all.fifo" +#endif +#define VTPM_TX_HP_FNAME "/var/vtpm/fifos/to_console.fifo" +#define VTPM_RX_HP_FNAME "/var/vtpm/fifos/from_console.fifo" + +#define VTPM_TYPE_PVM_STRING "pvm" +#define VTPM_TYPE_HVM_STRING "hvm" +#endif //_VTPM_MANAGER_H_
tools/vtpm_manager/manager/vtpm_manager_handler.c +78 -22
223 lines not on this page · raw message
tools/vtpm_manager/manager/vtpmd.c +68 -59
266 lines not on this page · raw message
tools/vtpm_manager/manager/vtpmpriv.h +21 -4
@@ -40,6 +40,8 @@#ifndef __VTPMPRIV_H__#define __VTPMPRIV_H__ +#include <signal.h> +#include "vtpm_manager.h" #include "tcg.h" #include "tcs.h"
@@ -54,16 +56,19 @@#define DMI_NVM_FILE "/var/vtpm/vtpm_dm_%d.data"#define VTPM_CTL_DM 0 +extern volatile sig_atomic_t HANDLER_QUIT_FLAG ; +// ------------------------ Private Structures ----------------------- typedef struct VTPM_DMI_RESOURCE_T { +#ifndef VTPM_STUBDOM // I/O info for Manager to talk to DMI's and controllers vtpm_ipc_handle_t *tx_vtpm_ipc_h; // TX VTPM Results to DMI vtpm_ipc_handle_t *rx_vtpm_ipc_h; // RX VTPM Commands from DMI vtpm_ipc_handle_t *tx_tpm_ipc_h; // TX TPM Commands to DMI vtpm_ipc_handle_t *rx_tpm_ipc_h; // RX TPM Results from DMI + + pid_t dmi_pid; -#ifndef VTPM_MULTI_VM - pid_t dmi_pid; #endif // Non-persistent Information
@@ -77,6 +82,7 @@BYTE dmi_type;TPM_DIGEST NVM_measurement;// Equal to the SHA1 of the blob TPM_DIGEST DMI_measurement;// Correct measurement of the owning DMI + }VTPM_DMI_RESOURCE ; typedef struct tdVTPM_MIGKEY_LIST {
@@ -89,8 +95,11 @@ typedef struct tdVTPM_GLOBALS { // Non-persistent data -#ifndef VTPM_MULTI_VM - pid_t master_pid; + pthread_t master_thread; + pthread_t be_thread; + pthread_t hp_thread; +#ifndef VTPM_STUBDOM + pthread_t dmi_thread; #endif int connected_dmis; // To close guest_rx when no dmis are connected
@@ -143,6 +152,11 @@const buffer_t *inbuf, buffer_t *outbuf); +TPM_RESULT VTPM_Handle_Get_NVM_Size (VTPM_DMI_RESOURCE *myDMI, +const buffer_t *inbuf, +buffer_t *outbuf); + +TPM_RESULT VTPM_Handle_TPM_Command (VTPM_DMI_RESOURCE *dmi,buffer_t *inbuf,buffer_t *outbuf);
@@ -173,6 +187,9 @@TPM_RESULT close_dmi (VTPM_DMI_RESOURCE *dmi_res);TPM_RESULT init_dmi (UINT32 dmi_id,BYTE type,VTPM_DMI_RESOURCE **dmi_res); +/* Free's dmi_res and all of it's resources */ +void free_dmi (VTPM_DMI_RESOURCE *dmi_res); +TPM_RESULT envelope_encrypt (const buffer_t *inbuf,CRYPTO_INFO *asymkey,buffer_t *sealed_data);
tools/vtpm_manager/manager/vtsp.c +4 -6
@@ -567,8 +567,7 @@ osapSharedSecret, auth, 0) );// Unpack/return key structure -TPMTRYRETURN (buffer_init (pubKeyBuf,0 ,0 ) ); -TPMTRYRETURN (buffer_append_raw (pubKeyBuf, newKeyText.size, newKeyText.data) ); +TPMTRYRETURN (buffer_init (pubKeyBuf, newKeyText.size, newKeyText.data) ); goto egress;
@@ -664,6 +663,7 @@// Destroy rsaKeyParms BSG_Destroy (BSG_TPM_RSA_KEY_PARMS , &rsaKeyParms); +BSG_Destroy (BSG_TPM_KEY , &newKey);// Set encryption scheme cryptoinfo->encScheme =CRYPTO_ES_RSAESOAEP_SHA1_MGF1 ;
@@ -733,8 +733,7 @@ hContext) );// Unpack/return key structure -TPMTRYRETURN (buffer_init (clear_data,0 ,0 )); -TPMTRYRETURN (buffer_append_raw (clear_data, clear_data_size, clear_data_text) ); +TPMTRYRETURN (buffer_init (clear_data, clear_data_size, clear_data_text) ); goto egress;
@@ -793,8 +792,7 @@vtpmlogerror (VTPM_LOG_VTSP ,"Enc buffer just overflowed.\n" ); } -buffer_init (outData,0 ,NULL ); -buffer_append_raw (outData, out_tmp_size, out_tmp); +buffer_init (outData, out_tmp_size, out_tmp);vtpmloginfo (VTPM_LOG_TXDATA ,"Bind Generated[%d] = 0x" , out_tmp_size);for (i =0 ; i < out_tmp_size ; i++) {
tools/vtpm_manager/migration/vtpm_manager_if.c +2 -5
@@ -50,15 +50,12 @@#include "vtpm_migrator.h" #include "vtpm_manager.h" -#define VTPM_TX_HP_FNAME "/var/vtpm/fifos/from_console.fifo" -#define VTPM_RX_HP_FNAME "/var/vtpm/fifos/to_console.fifo" -static vtpm_ipc_handle_t tx_ipc_h, rx_ipc_h;TPM_RESULT vtpm_manager_open (){ -if ( (vtpm_ipc_init (&tx_ipc_h,VTPM_TX_HP_FNAME ,O_RDWR , TRUE) !=0 ) ||//FIXME: wronly - (vtpm_ipc_init (&rx_ipc_h,VTPM_RX_HP_FNAME ,O_RDWR , TRUE) !=0 ) ) {//FIXME: rdonly +if ( (vtpm_ipc_init (&tx_ipc_h,VTPM_TX_HP_FNAME ,O_RDWR , TRUE, TRUE) !=0 ) ||//FIXME: wronly + (vtpm_ipc_init (&rx_ipc_h,VTPM_RX_HP_FNAME ,O_RDWR , TRUE, TRUE) !=0 ) ) {//FIXME: rdonly vtpmlogerror (VTPM_LOG_VTPM ,"Unable to connect to vtpm_manager.\n" );return TPM_IOERROR ; }
tools/vtpm_manager/tcs/contextmgr.c +3 -1
@@ -195,6 +195,7 @@BOOL FreeHandleList (CONTEXT_HANDLE * pContextHandle) {// in HANDLE_LIST * pCurrentHandle; +HANDLE_LIST * pNext;BOOL returncode = TRUE;vtpmloginfo (VTPM_LOG_TCS_DEEP ,"Freeing all handles for context\n" );
@@ -205,6 +206,7 @@ pCurrentHandle = pContextHandle->pHandleList;while (pCurrentHandle !=NULL ) { + pNext = pCurrentHandle->pNextHandle;switch (pCurrentHandle->type) {case TPM_RT_KEY : returncode = returncode && !TCSP_EvictKey (pContextHandle->handle, pCurrentHandle->handle);
@@ -216,7 +218,7 @@ returncode = FALSE; } - pCurrentHandle = pCurrentHandle->pNextHandle; + pCurrentHandle = pNext; }
tools/vtpm_manager/tcs/tcs.c +15 -14
@@ -113,14 +113,13 @@TCS_CONTEXT_HANDLE *hContext;// Close all the TCS contexts. TCS should evict keys based on this -if (hashtable_count (context_ht) >0 ) { +while (hashtable_count (context_ht) >0 ) { context_itr =hashtable_iterator (context_ht); -do { - hContext = (TCS_CONTEXT_HANDLE *)hashtable_iterator_key (context_itr); -if (TCS_CloseContext (*hContext) !=TPM_SUCCESS ) -vtpmlogerror (VTPM_LOG_TCS ,"Failed to close context %d properly.\n" , *hContext); + + hContext = (TCS_CONTEXT_HANDLE *)hashtable_iterator_key (context_itr); +if (TCS_CloseContext (*hContext) !=TPM_SUCCESS ) +vtpmlogerror (VTPM_LOG_TCS ,"Failed to close context %d properly.\n" , *hContext); - }while (hashtable_iterator_advance (context_itr));free (context_itr); }hashtable_destroy (context_ht,1 );
@@ -534,6 +533,10 @@ BSG_TYPE_UINT32, &handle);// fill paramSize again as we now have the correct size BSG_Pack (BSG_TYPE_UINT32 , &InLength, InBuf+2 ); + +if (!DeleteHandleFromList (hContext, handle)) { +vtpmlogerror (VTPM_LOG_TCS ,"KeyHandle not removed from list\n" ); + }// call the TPM driver if ((hRes = TDDL_TransmitData(InBuf, InLength, OutBuf, &OutLength))
@@ -545,9 +548,6 @@BSG_TYPE_UINT32 , ¶mSize, BSG_TPM_COMMAND_CODE, &returnCode); -if (!DeleteHandleFromList (hContext, handle)) -vtpmlogerror (VTPM_LOG_TCS ,"KeyHandle not removed from list\n" ); -if (returnCode ==TPM_SUCCESS && tag ==TPM_TAG_RSP_COMMAND ) {// Print debug info
@@ -882,6 +882,7 @@memcpy (*prgbKey, tempBuf, *pcKeySize); +BSG_Destroy (BSG_TPM_KEY , &wrappedKey);vtpmloginfo (VTPM_LOG_TCS_DEEP ,"Received paramSize : %d\n" , paramSize); }else vtpmlogerror (VTPM_LOG_TCS ,"TCSP_CreateWrapKey Failed with return code %s\n" ,tpm_get_error_name (returnCode));
@@ -980,6 +981,10 @@BSG_Pack (BSG_TYPE_UINT32 , &InLength, InBuf+2 );vtpmloginfo (VTPM_LOG_TCS_DEEP ,"Sending paramSize = %d\n" , InLength); + +if (!DeleteHandleFromList (hContext, hKey)) { +vtpmlogerror (VTPM_LOG_TCS ,"KeyHandle not removed from list\n" ); + }// call the TPM driver if ((hRes =TDDL_TransmitData (InBuf, InLength, OutBuf, &OutLength)) ==TDDL_SUCCESS ) {
@@ -989,10 +994,6 @@BSG_TYPE_UINT32 , ¶mSize, BSG_TPM_COMMAND_CODE, &returnCode); -if (!DeleteHandleFromList (hContext, hKey)) { -vtpmlogerror (VTPM_LOG_TCS ,"KeyHandle not removed from list\n" ); - } -if (returnCode ==TPM_SUCCESS && tag ==TPM_TAG_RSP_COMMAND ) {vtpmloginfo (VTPM_LOG_TCS_DEEP ,"Received paramSize : %d\n" , paramSize); }else {
@@ -1019,7 +1020,7 @@TDDL_UINT32 OutLength =TCPA_MAX_BUFFER_LENGTH ;// check input params -if (bytesRequested ==NULL || *randomBytes ==NULL ){ +if (bytesRequested ==NULL || randomBytes ==NULL ){return TPM_BAD_PARAMETER ; }
tools/vtpm_manager/tcs/tpmddl.c new file +93 -0
93 lines not on this page · raw message
tools/vtpm_manager/tcs/tpmddl.h +3 -0
@@ -66,4 +66,7 @@TDDL_BYTE * buffer, TDDL_UINT32* size); +TDDL_RESULT TDDL_FlushSpecific (TDDL_UINT32 handle, +TDDL_UINT32 res); +#endif // __TPMDDL_H__
tools/vtpm_manager/tcs/transmit.c +1 -1
@@ -117,7 +117,7 @@ g_tx_fd =open (TPM_TX_FNAME ,O_RDWR );if (g_tx_fd <0 ) { -vtpmlogerror (VTPM_LOG_TXDATA ,"TPM open failed" ); +vtpmlogerror (VTPM_LOG_TXDATA ,"TPM open failed\n" );return TPM_IOERROR ; }
tools/vtpm_manager/util/buffer.c +30 -2
@@ -39,6 +39,7 @@#include <sys/param.h> #include "tcg.h" +#include "log.h" #include "bsg.h" #include "buffer.h"
@@ -51,6 +52,7 @@TPM_RESULT buffer_init (buffer_t * buf,tpm_size_t initsize,const BYTE * initval) {if (initsize ==0 ) {memset (buf,0 ,sizeof (*buf)); + buf->bytes =NULL ;return TPM_SUCCESS ; }
@@ -62,8 +64,11 @@ buf->size = initsize; buf->alloc_size = initsize; -if (initval) +if (initval) {memcpy (buf->bytes, initval, initsize); + }else { +memset (buf->bytes,0 , initsize); + } buf->is_owner = TRUE;
@@ -190,6 +195,30 @@return status; } +TPM_RESULT buffer_prepend_raw (buffer_t * buf,tpm_size_t len,const BYTE * bytes) { +TPM_RESULT status =TPM_SUCCESS ; +long i; + +if (buf->alloc_size < buf->size + len) { +TPMTRYRETURN (buffer_priv_realloc (buf, buf->size + len) ); + } + +if (buf->size >0 ) { +memmove (buf->bytes + len, buf->bytes, buf->size); + } +memcpy (buf->bytes, bytes, len); + + buf->size += len; + + goto egress; + + abort_egress: + + egress: + +return status; +} +tpm_size_t buffer_len (const buffer_t * buf) {return buf->size; }
@@ -199,7 +228,6 @@free (buf->bytes); buf->bytes =NULL ; buf->size = buf->alloc_size =0 ; - }return TPM_SUCCESS ;
tools/vtpm_manager/util/buffer.h +2 -0
@@ -92,4 +92,6 @@TPM_RESULT buffer_append_raw (buffer_t * buf,tpm_size_t len,const BYTE * bytes); +TPM_RESULT buffer_prepend_raw (buffer_t * buf,tpm_size_t len,const BYTE * bytes); +#endif // _TOOLS_H_
tools/vtpm_manager/util/tcg.h +96 -0
122 lines not on this page · raw message
_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xensource.com/xen-devel