1 /********************************************************************************/
4 /* IBM Thomas J. Watson Research Center */
6 /* (c) Copyright IBM Corporation 2014-2015. */
8 /* All rights reserved. */
10 /* Redistribution and use in source and binary forms, with or without */
11 /* modification, are permitted provided that the following conditions are */
14 /* Redistributions of source code must retain the above copyright notice, */
15 /* this list of conditions and the following disclaimer. */
17 /* Redistributions in binary form must reproduce the above copyright */
18 /* notice, this list of conditions and the following disclaimer in the */
19 /* documentation and/or other materials provided with the distribution. */
21 /* Neither the names of the IBM Corporation nor the names of its */
22 /* contributors may be used to endorse or promote products derived from */
23 /* this software without specific prior written permission. */
25 /* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS */
26 /* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT */
27 /* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR */
28 /* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT */
29 /* HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, */
30 /* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT */
31 /* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, */
32 /* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY */
33 /* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT */
34 /* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE */
35 /* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
36 /********************************************************************************/
40 * Eric Richter, erichte@us.ibm.com
41 * Stefan Berger, stefanb@us.ibm.com
42 * David Safford, safford@us.ibm.com
54 #include <arpa/inet.h>
57 #include <fuse/cuse_lowlevel.h>
61 #include <libtpms/tpm_library.h>
62 #include <libtpms/tpm_tis.h>
63 #include <libtpms/tpm_error.h>
64 #include <libtpms/tpm_memory.h>
72 #include "tpm_ioctl.h"
73 #include "swtpm_nvfile.h"
77 #include "threadpool.h"
79 /* maximum size of request buffer */
80 #define TPM_REQ_MAX 4096
82 /* version of the TPM (1.2 or 2) */
83 static TPMLIB_TPMVersion tpmversion
;
85 /* buffer containing the TPM request */
86 static unsigned char *ptm_request
;
88 /* buffer containing the TPM response */
89 static unsigned char *ptm_response
;
91 /* the sizes of the data in the buffers */
92 static uint32_t ptm_req_len
, ptm_res_len
, ptm_res_tot
;
94 /* locality applied to TPM commands */
95 static TPM_MODIFIER_INDICATOR locality
;
97 /* whether the TPM is running (TPM_Init was received) */
98 static bool tpm_running
;
100 /* flags on how to handle locality */
101 static uint32_t locality_flags
;
103 /* the fuse_session that we will signal an exit to to exit the prg. */
104 static struct fuse_session
*ptm_fuse_session
;
106 #if GLIB_MAJOR_VERSION >= 2
107 # if GLIB_MINOR_VERSION >= 32
109 GMutex file_ops_lock
;
110 # define FILE_OPS_LOCK &file_ops_lock
114 GMutex
*file_ops_lock
;
115 # define FILE_OPS_LOCK file_ops_lock
120 #error Unsupport glib version
134 /* single message to send to the worker thread */
135 static struct thread_message msg
;
141 TPM_BOOL is_encrypted
;
144 typedef struct stateblob_desc
{
147 TPM_BOOL is_encrypted
;
149 uint32_t data_length
;
152 typedef enum tx_state_type
{
153 TX_STATE_RW_COMMAND
= 1,
154 TX_STATE_SET_STATE_BLOB
= 2,
155 TX_STATE_GET_STATE_BLOB
= 3,
158 typedef struct transfer_state
{
160 /* while in TX_STATE_GET/SET_STATEBLOB */
162 TPM_BOOL blob_is_encrypted
;
163 /* while in TX_STATE_GET */
167 typedef struct TPM_Response_Header
{
171 } __attribute__ ((packed
)) TPM_Response_Header
;
173 /*********************************** data *************************************/
175 static const char *usage
=
176 "usage: %s %s [options]\n"
178 "The following options are supported:\n"
180 "-n NAME|--name=NAME : device name (mandatory)\n"
181 "-M MAJ|--maj=MAJ : device major number\n"
182 "-m MIN|--min=MIN : device minor number\n"
183 "--key file=<path>[,mode=aes-cbc|aes-256-cbc][,format=hex|binary][,remove=[true|false]]\n"
184 " : use an AES key for the encryption of the TPM's state\n"
185 " files; use the given mode for the block encryption;\n"
186 " the key is to be provided as a hex string or in binary\n"
187 " format; the keyfile can be automatically removed using\n"
188 " the remove parameter\n"
189 "--key pwdfile=<path>[,mode=aes-cbc|aes-256-cbc][,remove=[true|false]]\n"
190 " : provide a passphrase in a file; the AES key will be\n"
191 " derived from this passphrase\n"
192 "--locality [reject-locality-4][,allow-set-locality]\n"
193 " : reject-locality-4: reject any command in locality 4\n"
194 " allow-set-locality: accept SetLocality command\n"
195 "--migration-key file=<path>[,mode=aes-cbc|aes-256-cbc][,format=hex|binary][,remove=[true|false]]\n"
196 " : use an AES key for the encryption of the TPM's state\n"
197 " when it is retrieved from the TPM via ioctls;\n"
198 " Setting this key ensures that the TPM's state will always\n"
199 " be encrypted when migrated\n"
200 "--migration-key pwdfile=<path>[,mode=aes-cbc|aes-256-cbc][,remove=[true|false]]\n"
201 " : provide a passphrase in a file; the AES key will be\n"
202 " derived from this passphrase\n"
203 "--log file=<path>|fd=<filedescriptor>[,level=n][,prefix=<prefix>][,truncate]\n"
204 " : write the TPM's log into the given file rather than\n"
205 " to the console; provide '-' for path to avoid logging\n"
206 " log level 5 and higher will enable libtpms logging;\n"
207 " all logged output will be prefixed with prefix;\n"
208 " the log file can be reset (truncate)\n"
209 "--pid file=<path>|fd=<filedescriptor>\n"
210 " : write the process ID into the given file\n"
211 "--tpmstate dir=<dir>[,mode=0...]\n"
212 " : set the directory where the TPM's state will be written\n"
213 " into; the TPM_PATH environment variable can be used\n"
215 " mode allows to set the file mode bits of the state\n"
216 " files; the default mode is 0640;\n"
217 "-r|--runas <user> : after creating the CUSE device, change to the given\n"
219 "--tpm2 : choose TPM2 functionality\n"
220 "-h|--help : display this help screen and terminate\n"
224 ptm_io_getlocality(TPM_MODIFIER_INDICATOR
*loc
, uint32_t tpmnum
)
230 static struct libtpms_callbacks cbs
= {
231 .sizeOfStruct
= sizeof(struct libtpms_callbacks
),
232 .tpm_nvram_init
= SWTPM_NVRAM_Init
,
233 .tpm_nvram_loaddata
= SWTPM_NVRAM_LoadData
,
234 .tpm_nvram_storedata
= SWTPM_NVRAM_StoreData
,
235 .tpm_nvram_deletename
= SWTPM_NVRAM_DeleteName
,
236 .tpm_io_getlocality
= ptm_io_getlocality
,
239 /* the current state the transfer interface is in */
240 static transfer_state tx_state
;
242 /* function prototypes */
243 static void ptm_cleanup(void);
245 /************************* cached stateblob *********************************/
247 static stateblob_desc cached_stateblob
;
250 * cached_stateblob_is_loaded: is the stateblob with the given properties
251 * the one in the cache?
253 static bool cached_stateblob_is_loaded(uint32_t blobtype
,
256 return (cached_stateblob
.data
!= NULL
) &&
257 (cached_stateblob
.blobtype
== blobtype
) &&
258 (cached_stateblob
.decrypt
== decrypt
);
262 * cached_stateblob_free: Free any previously loaded state blob
264 static void cached_stateblob_free(void)
266 free(cached_stateblob
.data
);
267 cached_stateblob
.data
= NULL
;
268 cached_stateblob
.data_length
= 0;
272 * cached_stateblob_get_bloblength: get the total length of the cached blob
274 static uint32_t cached_stateblob_get_bloblength(void)
276 return cached_stateblob
.data_length
;
280 * cached_statblob_get: get stateblob data without copying them
282 * @offset: at which offset to get the data
283 * @bufptr: pointer to a buffer pointer used to return buffer start
284 * @length: pointer used to return number of available bytes in returned buffer
286 static int cached_stateblob_get(uint32_t offset
,
287 unsigned char **bufptr
, size_t *length
)
289 if (cached_stateblob
.data
== NULL
||
290 offset
> cached_stateblob
.data_length
)
293 *bufptr
= &cached_stateblob
.data
[offset
];
294 *length
= cached_stateblob
.data_length
- offset
;
300 * cached_stateblob_load: load a state blob into the cache
302 * blobtype: the type of blob
303 * decrypt: whether the blob is to be decrypted
305 static TPM_RESULT
cached_stateblob_load(uint32_t blobtype
, TPM_BOOL decrypt
)
308 const char *blobname
= tpmlib_get_blobname(blobtype
);
309 uint32_t tpm_number
= 0;
312 return TPM_BAD_PARAMETER
;
314 cached_stateblob_free();
316 if (blobtype
== PTM_BLOB_TYPE_VOLATILE
)
317 res
= SWTPM_NVRAM_Store_Volatile();
320 res
= SWTPM_NVRAM_GetStateBlob(&cached_stateblob
.data
,
321 &cached_stateblob
.data_length
,
322 tpm_number
, blobname
, decrypt
,
323 &cached_stateblob
.is_encrypted
);
325 /* make sure the volatile state file is gone */
326 if (blobtype
== PTM_BLOB_TYPE_VOLATILE
)
327 SWTPM_NVRAM_DeleteName(tpm_number
, blobname
, FALSE
);
330 cached_stateblob
.blobtype
= blobtype
;
331 cached_stateblob
.decrypt
= decrypt
;
338 * cached_state_blob_copy: copy the cached state blob to a destination buffer
340 * dest: destination buffer
341 * destlen: size of the buffer
342 * srcoffset: offset to copy from
343 * copied: variable to return the number of copied bytes
344 * is_encrypted: variable to return whether the blob is encrypted
346 static int cached_stateblob_copy(void *dest
, size_t destlen
,
347 uint32_t srcoffset
, uint32_t *copied
,
348 TPM_BOOL
*is_encrypted
)
354 if (cached_stateblob
.data
!= NULL
&& cached_stateblob
.data_length
> 0) {
356 if (srcoffset
< cached_stateblob
.data_length
) {
357 *copied
= min(cached_stateblob
.data_length
- srcoffset
, destlen
);
359 memcpy(dest
, &cached_stateblob
.data
[srcoffset
], *copied
);
361 *is_encrypted
= cached_stateblob
.is_encrypted
;
370 /************************* worker thread ************************************/
373 * worker_thread: the worker thread
375 static void worker_thread(gpointer data
, gpointer user_data
)
377 struct thread_message
*msg
= (struct thread_message
*)data
;
380 case MESSAGE_TPM_CMD
:
381 TPMLIB_Process(&ptm_response
, &ptm_res_len
, &ptm_res_tot
,
382 ptm_request
, ptm_req_len
);
388 /* results are ready */
389 worker_thread_mark_done();
392 /***************************** utility functions ****************************/
395 * tpm_start: Start the TPM
397 * Check whether the TPM's state directory exists and if it does
398 * not exists, try to creat it. Start the thread pool, initilize
399 * libtpms and allocate a global TPM request buffer.
401 * @flags: libtpms init flags
403 static int tpm_start(uint32_t flags
, TPMLIB_TPMVersion l_tpmversion
)
406 const char *tpmdir
= tpmstate_get_dir();
408 dir
= opendir(tpmdir
);
412 if (mkdir(tpmdir
, 0775)) {
413 logprintf(STDERR_FILENO
,
414 "Error: Could not open tpmstate dir %s\n",
420 pool
= g_thread_pool_new(worker_thread
,
426 logprintf(STDERR_FILENO
,
427 "Error: Could not create the thread pool.\n");
432 ptm_request
= malloc(4096);
434 logprintf(STDERR_FILENO
,
435 "Error: Could not allocate memory for request buffer.\n");
439 if (tpmlib_start(flags
, l_tpmversion
) != TPM_SUCCESS
)
442 logprintf(STDOUT_FILENO
,
443 "CUSE TPM successfully initialized.\n");
448 g_thread_pool_free(pool
, TRUE
, TRUE
);
455 * ptm_write_fatal_error_response: Write fatal error response
457 * Write a fatal error response into the global ptm_response buffer.
459 static void ptm_write_fatal_error_response(TPMLIB_TPMVersion l_tpmversion
)
461 tpmlib_write_fatal_error_response(&ptm_response
,
467 /************************************ read() support ***************************/
470 * ptm_read_result: Return the TPM response packet
472 * @req: the fuse_req_t
473 * @size: the max. number of bytes to return to the requester
475 static void ptm_read_result(fuse_req_t req
, size_t size
)
480 /* wait until results are ready */
481 worker_thread_wait_done();
486 if (ptm_res_len
> size
) {
493 fuse_reply_buf(req
, (const char *)ptm_response
, len
);
497 * ptm_read_stateblob: get a TPM stateblob via the read() interface
499 * @req: the fuse_req_t
500 * @size: the number of bytes to read
502 * The internal offset into the buffer is advanced by the number
503 * of bytes that were copied. We switch back to command read/write
504 * mode if an error occurred or once all bytes were read.
506 static void ptm_read_stateblob(fuse_req_t req
, size_t size
)
508 unsigned char *bufptr
= NULL
;
512 if (cached_stateblob_get(tx_state
.offset
, &bufptr
, &numbytes
) < 0) {
513 fuse_reply_err(req
, EIO
);
514 tx_state
.state
= TX_STATE_RW_COMMAND
;
516 tocopy
= MIN(size
, numbytes
);
517 tx_state
.offset
+= tocopy
;
519 fuse_reply_buf(req
, (char *)bufptr
, tocopy
);
520 /* last transfer indicated by less bytes available than requested */
521 if (numbytes
< size
) {
522 tx_state
.state
= TX_STATE_RW_COMMAND
;
528 * ptm_read: interface to POSIX read()
531 * @size: number of bytes to read
532 * @off: offset (not used)
533 * @fi: fuse_file_info (not used)
535 * Depending on the current state of the transfer interface (read/write)
536 * return either the results of TPM commands or a data of a TPM state blob.
538 static void ptm_read(fuse_req_t req
, size_t size
, off_t off
,
539 struct fuse_file_info
*fi
)
541 switch (tx_state
.state
) {
542 case TX_STATE_RW_COMMAND
:
543 ptm_read_result(req
, size
);
545 case TX_STATE_SET_STATE_BLOB
:
546 fuse_reply_err(req
, EIO
);
547 tx_state
.state
= TX_STATE_RW_COMMAND
;
549 case TX_STATE_GET_STATE_BLOB
:
550 ptm_read_stateblob(req
, size
);
555 /*************************read/write stateblob support ***********************/
558 * ptm_set_stateblob_append: Append a piece of TPM state blob and transfer to TPM
560 * blobtype: the type of blob
561 * data: the data to append
562 * length: length of the data
563 * is_encrypted: whether the blob is encrypted
564 * is_last: whether this is the last part of the TPM state blob; if it is, the TPM
565 * state blob will then be transferred to the TPM
568 ptm_set_stateblob_append(uint32_t blobtype
,
569 const unsigned char *data
, uint32_t length
,
570 bool is_encrypted
, bool is_last
)
573 static struct stateblob stateblob
;
576 if (stateblob
.type
!= blobtype
) {
577 /* new blob; clear old data */
578 free(stateblob
.data
);
579 stateblob
.data
= NULL
;
580 stateblob
.length
= 0;
581 stateblob
.type
= blobtype
;
582 stateblob
.is_encrypted
= is_encrypted
;
585 * on the first call for a new state blob we allow 0 bytes to be written
586 * this allows the user to transfer via write()
593 tmp
= realloc(stateblob
.data
, stateblob
.length
+ length
);
595 logprintf(STDERR_FILENO
,
596 "Could not allocate %u bytes.\n", stateblob
.length
+ length
);
598 free(stateblob
.data
);
599 stateblob
.data
= NULL
;
600 stateblob
.length
= 0;
605 stateblob
.data
= tmp
;
607 memcpy(&stateblob
.data
[stateblob
.length
], data
, length
);
608 stateblob
.length
+= length
;
611 /* full packet -- expecting more data */
615 res
= SWTPM_NVRAM_SetStateBlob(stateblob
.data
,
617 stateblob
.is_encrypted
,
621 logprintf(STDERR_FILENO
,
622 "Deserialized state type %d (%s), length=%d, res=%d\n",
623 blobtype
, tpmlib_get_blobname(blobtype
),
624 stateblob
.length
, res
);
626 free(stateblob
.data
);
627 stateblob
.data
= NULL
;
628 stateblob
.length
= 0;
631 /* transfer of blob is complete */
632 tx_state
.state
= TX_STATE_RW_COMMAND
;
638 * ptm_set_stateblob: set part of a TPM state blob
641 * pss: ptm_setstate provided via ioctl()
644 ptm_set_stateblob(fuse_req_t req
, ptm_setstate
*pss
)
647 TPM_BOOL is_encrypted
=
648 ((pss
->u
.req
.state_flags
& PTM_STATE_FLAG_ENCRYPTED
) != 0);
649 bool is_last
= (sizeof(pss
->u
.req
.data
) != pss
->u
.req
.length
);
651 if (pss
->u
.req
.length
> sizeof(pss
->u
.req
.data
)) {
652 res
= TPM_BAD_PARAMETER
;
656 /* transfer of blob initiated */
657 tx_state
.state
= TX_STATE_SET_STATE_BLOB
;
658 tx_state
.blobtype
= pss
->u
.req
.type
;
659 tx_state
.blob_is_encrypted
= is_encrypted
;
662 res
= ptm_set_stateblob_append(pss
->u
.req
.type
,
669 tx_state
.state
= TX_STATE_RW_COMMAND
;
672 pss
->u
.resp
.tpm_result
= res
;
674 fuse_reply_ioctl(req
, 0, pss
, sizeof(*pss
));
678 * ptm_get_stateblob_part: get part of a state blob
680 * @blobtype: the type of blob to get
681 * @buffer: the buffer this function will write the blob into
682 * @buffer_size: the size of the buffer
683 * @offset: the offset into the state blob
684 * @copied: pointer to int to indicate the number of bytes that were copied
685 * @is_encryped: returns whether the state blob is encrypted
688 ptm_get_stateblob_part(uint32_t blobtype
,
689 unsigned char *buffer
, size_t buffer_size
,
690 uint32_t offset
, uint32_t *copied
,
691 TPM_BOOL decrypt
, TPM_BOOL
*is_encrypted
)
695 if (!cached_stateblob_is_loaded(blobtype
, decrypt
)) {
696 res
= cached_stateblob_load(blobtype
, decrypt
);
700 cached_stateblob_copy(buffer
, buffer_size
,
701 offset
, copied
, is_encrypted
);
708 * ptm_get_stateblob: Get the state blob from the TPM using ioctl()
711 ptm_get_stateblob(fuse_req_t req
, ptm_getstate
*pgs
)
714 uint32_t blobtype
= pgs
->u
.req
.type
;
716 ((pgs
->u
.req
.state_flags
& PTM_STATE_FLAG_DECRYPTED
) != 0);
717 TPM_BOOL is_encrypted
= FALSE
;
719 uint32_t offset
= pgs
->u
.req
.offset
;
722 res
= ptm_get_stateblob_part(blobtype
,
723 pgs
->u
.resp
.data
, sizeof(pgs
->u
.resp
.data
),
724 pgs
->u
.req
.offset
, &copied
,
725 decrypt
, &is_encrypted
);
727 totlength
= cached_stateblob_get_bloblength();
729 pgs
->u
.resp
.state_flags
= 0;
731 pgs
->u
.resp
.state_flags
|= PTM_STATE_FLAG_ENCRYPTED
;
733 pgs
->u
.resp
.length
= copied
;
734 pgs
->u
.resp
.totlength
= totlength
;
735 pgs
->u
.resp
.tpm_result
= res
;
736 logprintf(STDERR_FILENO
,
737 "Serialized state type %d, length=%d, totlength=%d, res=%d\n",
738 blobtype
, copied
, totlength
, res
);
741 if (offset
+ copied
< totlength
) {
742 /* last byte was not copied */
743 tx_state
.state
= TX_STATE_GET_STATE_BLOB
;
744 tx_state
.blobtype
= pgs
->u
.req
.type
;
745 tx_state
.blob_is_encrypted
= is_encrypted
;
746 tx_state
.offset
= copied
;
748 /* last byte was copied */
749 tx_state
.state
= TX_STATE_RW_COMMAND
;
753 tx_state
.state
= TX_STATE_RW_COMMAND
;
756 fuse_reply_ioctl(req
, 0, pgs
, sizeof(pgs
->u
.resp
));
759 /*********************************** write() support *************************/
762 * ptm_write_stateblob: Write the state blob using the write() interface
764 * @req: the fuse_req_t
765 * @buf: the buffer with the data
766 * @size: the number of bytes in the buffer
768 * The data are appended to an existing buffer that was created with the
771 static void ptm_write_stateblob(fuse_req_t req
, const char *buf
, size_t size
)
775 res
= ptm_set_stateblob_append(tx_state
.blobtype
,
776 (unsigned char *)buf
, size
,
777 tx_state
.blob_is_encrypted
,
780 tx_state
.state
= TX_STATE_RW_COMMAND
;
781 fuse_reply_err(req
, EIO
);
783 fuse_reply_write(req
, size
);
788 * ptm_write_cmd: User writing a TPM command
791 * buf: the buffer containing the TPM command
792 * size: the size of the buffer
793 * tpmversion: the version of the TPM
795 static void ptm_write_cmd(fuse_req_t req
, const char *buf
, size_t size
,
796 TPMLIB_TPMVersion l_tpmversion
)
801 /* prevent other threads from writing or doing ioctls */
802 g_mutex_lock(FILE_OPS_LOCK
);
805 /* ensure that we only ever work on one TPM command */
806 if (worker_thread_is_busy()) {
807 fuse_reply_err(req
, EBUSY
);
811 if (ptm_req_len
> TPM_REQ_MAX
)
812 ptm_req_len
= TPM_REQ_MAX
;
814 /* process SetLocality command, if */
815 tpmlib_process(&ptm_response
, &ptm_res_len
, &ptm_res_tot
,
816 (unsigned char *)buf
, ptm_req_len
,
817 locality_flags
, &locality
, tpmversion
);
821 if (tpmlib_is_request_cancelable(l_tpmversion
,
822 (const unsigned char*)buf
,
824 /* have command processed by thread pool */
825 memcpy(ptm_request
, buf
, ptm_req_len
);
827 msg
.type
= MESSAGE_TPM_CMD
;
829 worker_thread_mark_busy();
831 g_thread_pool_push(pool
, &msg
, NULL
);
833 /* direct processing */
834 TPMLIB_Process(&ptm_response
, &ptm_res_len
, &ptm_res_tot
,
835 (unsigned char *)buf
, ptm_req_len
);
838 /* TPM not initialized; return error */
839 ptm_write_fatal_error_response(l_tpmversion
);
843 fuse_reply_write(req
, ptm_req_len
);
846 g_mutex_unlock(FILE_OPS_LOCK
);
852 * ptm_write: low-level write() interface; calls approriate function depending
853 * on what is being transferred using the write()
855 static void ptm_write(fuse_req_t req
, const char *buf
, size_t size
,
856 off_t off
, struct fuse_file_info
*fi
)
858 switch (tx_state
.state
) {
859 case TX_STATE_RW_COMMAND
:
860 ptm_write_cmd(req
, buf
, size
, tpmversion
);
862 case TX_STATE_GET_STATE_BLOB
:
863 fuse_reply_err(req
, EIO
);
864 tx_state
.state
= TX_STATE_RW_COMMAND
;
866 case TX_STATE_SET_STATE_BLOB
:
867 ptm_write_stateblob(req
, buf
, size
);
873 * ptm_open: interface to POSIX open()
875 static void ptm_open(fuse_req_t req
, struct fuse_file_info
*fi
)
877 tx_state
.state
= TX_STATE_RW_COMMAND
;
879 fuse_reply_open(req
, fi
);
883 * ptm_ioctl : ioctl execution
885 * req: the fuse_req_t used to send response with
886 * cmd: the ioctl request code
887 * arg: the pointer the application used for calling the ioctl (3rd param)
889 * flags: some flags provided by fuse
890 * in_buf: the copy of the input buffer
891 * in_bufsz: size of the input buffer; provided by fuse and has size of
893 * out_bufsz: size of the output buffer; provided by fuse and has size of
896 static void ptm_ioctl(fuse_req_t req
, int cmd
, void *arg
,
897 struct fuse_file_info
*fi
, unsigned flags
,
898 const void *in_buf
, size_t in_bufsz
, size_t out_bufsz
)
900 TPM_RESULT res
= TPM_FAIL
;
901 bool exit_prg
= FALSE
;
903 TPM_MODIFIER_INDICATOR orig_locality
;
905 /* some commands have to wait until the worker thread is done */
907 case PTM_GET_CAPABILITY
:
908 case PTM_SET_LOCALITY
:
909 case PTM_CANCEL_TPM_CMD
:
911 case PTM_SET_BUFFERSIZE
:
912 /* no need to wait */
916 case PTM_GET_TPMESTABLISHED
:
917 case PTM_RESET_TPMESTABLISHED
:
921 case PTM_STORE_VOLATILE
:
922 case PTM_GET_STATEBLOB
:
923 case PTM_SET_STATEBLOB
:
925 worker_thread_wait_done();
929 /* prevent other threads from writing or doing ioctls */
930 g_mutex_lock(FILE_OPS_LOCK
);
933 case PTM_GET_CAPABILITY
:
934 if (out_bufsz
!= sizeof(ptm_cap
)) {
935 struct iovec iov
= { arg
, sizeof(uint8_t) };
936 fuse_reply_ioctl_retry(req
, &iov
, 1, NULL
, 0);
939 switch (tpmversion
) {
940 case TPMLIB_TPM_VERSION_2
:
941 ptm_caps
= PTM_CAP_INIT
| PTM_CAP_SHUTDOWN
942 | PTM_CAP_GET_TPMESTABLISHED
943 | PTM_CAP_SET_LOCALITY
945 | PTM_CAP_CANCEL_TPM_CMD
946 | PTM_CAP_STORE_VOLATILE
947 | PTM_CAP_RESET_TPMESTABLISHED
948 | PTM_CAP_GET_STATEBLOB
949 | PTM_CAP_SET_STATEBLOB
952 | PTM_CAP_SET_BUFFERSIZE
;
954 case TPMLIB_TPM_VERSION_1_2
:
955 ptm_caps
= PTM_CAP_INIT
| PTM_CAP_SHUTDOWN
956 | PTM_CAP_GET_TPMESTABLISHED
957 | PTM_CAP_SET_LOCALITY
959 | PTM_CAP_CANCEL_TPM_CMD
960 | PTM_CAP_STORE_VOLATILE
961 | PTM_CAP_RESET_TPMESTABLISHED
962 | PTM_CAP_GET_STATEBLOB
963 | PTM_CAP_SET_STATEBLOB
966 | PTM_CAP_SET_BUFFERSIZE
;
969 fuse_reply_ioctl(req
, 0, &ptm_caps
, sizeof(ptm_caps
));
974 if (in_bufsz
!= sizeof(ptm_init
)) {
975 struct iovec iov
= { arg
, sizeof(uint8_t) };
976 fuse_reply_ioctl_retry(req
, &iov
, 1, NULL
, 0);
978 init_p
= (ptm_init
*)in_buf
;
985 if (tpm_start(init_p
->u
.req
.init_flags
, tpmversion
) < 0) {
987 logprintf(STDERR_FILENO
,
988 "Error: Could not initialize the TPM.\n");
993 init_p
->u
.resp
.tpm_result
= res
;
994 fuse_reply_ioctl(req
, 0, init_p
, sizeof(*init_p
));
1004 tpm_running
= false;
1007 ptm_response
= NULL
;
1009 fuse_reply_ioctl(req
, 0, &res
, sizeof(res
));
1014 worker_thread_end();
1020 ptm_response
= NULL
;
1022 fuse_reply_ioctl(req
, 0, &res
, sizeof(res
));
1027 case PTM_GET_TPMESTABLISHED
:
1029 goto error_not_running
;
1031 if (out_bufsz
!= sizeof(ptm_est
)) {
1032 struct iovec iov
= { arg
, sizeof(uint8_t) };
1033 fuse_reply_ioctl_retry(req
, &iov
, 1, NULL
, 0);
1036 te
.u
.resp
.tpm_result
= TPM_IO_TpmEstablished_Get(&te
.u
.resp
.bit
);
1037 fuse_reply_ioctl(req
, 0, &te
, sizeof(te
));
1041 case PTM_RESET_TPMESTABLISHED
:
1043 goto error_not_running
;
1045 if (in_bufsz
!= sizeof(ptm_reset_est
)) {
1046 struct iovec iov
= { arg
, sizeof(uint32_t) };
1047 fuse_reply_ioctl_retry(req
, &iov
, 1, NULL
, 0);
1049 ptm_reset_est
*re
= (ptm_reset_est
*)in_buf
;
1050 if (re
->u
.req
.loc
> 4) {
1051 res
= TPM_BAD_LOCALITY
;
1053 /* set locality and reset flag in one command */
1054 orig_locality
= locality
;
1055 locality
= re
->u
.req
.loc
;
1057 res
= TPM_IO_TpmEstablished_Reset();
1059 locality
= orig_locality
;
1060 fuse_reply_ioctl(req
, 0, &res
, sizeof(res
));
1065 case PTM_SET_LOCALITY
:
1066 if (in_bufsz
!= sizeof(ptm_loc
)) {
1067 struct iovec iov
= { arg
, sizeof(uint32_t) };
1068 fuse_reply_ioctl_retry(req
, &iov
, 1, NULL
, 0);
1070 ptm_loc
*l
= (ptm_loc
*)in_buf
;
1071 if (l
->u
.req
.loc
> 4 ||
1072 (l
->u
.req
.loc
== 4 &&
1073 locality_flags
& LOCALITY_FLAG_REJECT_LOCALITY_4
)) {
1074 res
= TPM_BAD_LOCALITY
;
1077 locality
= l
->u
.req
.loc
;
1079 l
->u
.resp
.tpm_result
= res
;
1080 fuse_reply_ioctl(req
, 0, l
, sizeof(*l
));
1084 case PTM_HASH_START
:
1086 goto error_not_running
;
1088 res
= TPM_IO_Hash_Start();
1089 fuse_reply_ioctl(req
, 0, &res
, sizeof(res
));
1094 goto error_not_running
;
1096 if (in_bufsz
!= sizeof(ptm_hdata
)) {
1097 struct iovec iov
= { arg
, sizeof(uint32_t) };
1098 fuse_reply_ioctl_retry(req
, &iov
, 1, NULL
, 0);
1100 ptm_hdata
*data
= (ptm_hdata
*)in_buf
;
1101 if (data
->u
.req
.length
<= sizeof(data
->u
.req
.data
)) {
1102 res
= TPM_IO_Hash_Data(data
->u
.req
.data
,
1103 data
->u
.req
.length
);
1107 data
->u
.resp
.tpm_result
= res
;
1108 fuse_reply_ioctl(req
, 0, data
, sizeof(*data
));
1114 goto error_not_running
;
1116 res
= TPM_IO_Hash_End();
1117 fuse_reply_ioctl(req
, 0, &res
, sizeof(res
));
1120 case PTM_CANCEL_TPM_CMD
:
1122 goto error_not_running
;
1124 /* for cancellation to work, the TPM would have to
1125 * execute in another thread that polls on a cancel
1128 res
= TPMLIB_CancelCommand();
1129 fuse_reply_ioctl(req
, 0, &res
, sizeof(res
));
1132 case PTM_STORE_VOLATILE
:
1134 goto error_not_running
;
1136 res
= SWTPM_NVRAM_Store_Volatile();
1137 fuse_reply_ioctl(req
, 0, &res
, sizeof(res
));
1139 cached_stateblob_free();
1142 case PTM_GET_STATEBLOB
:
1144 goto error_not_running
;
1146 if (in_bufsz
!= sizeof(ptm_getstate
)) {
1147 struct iovec iov
= { arg
, sizeof(uint32_t) };
1148 fuse_reply_ioctl_retry(req
, &iov
, 1, NULL
, 0);
1150 ptm_get_stateblob(req
, (ptm_getstate
*)in_buf
);
1154 case PTM_SET_STATEBLOB
:
1158 /* tpm state dir must be set */
1161 if (in_bufsz
!= sizeof(ptm_setstate
)) {
1162 struct iovec iov
= { arg
, sizeof(uint32_t) };
1163 fuse_reply_ioctl_retry(req
, &iov
, 1, NULL
, 0);
1165 ptm_set_stateblob(req
, (ptm_setstate
*)in_buf
);
1169 case PTM_GET_CONFIG
:
1170 if (out_bufsz
!= sizeof(ptm_getconfig
)) {
1171 struct iovec iov
= { arg
, sizeof(uint32_t) };
1172 fuse_reply_ioctl_retry(req
, &iov
, 1, NULL
, 0);
1175 pgs
.u
.resp
.tpm_result
= 0;
1176 pgs
.u
.resp
.flags
= 0;
1177 if (SWTPM_NVRAM_Has_FileKey())
1178 pgs
.u
.resp
.flags
|= PTM_CONFIG_FLAG_FILE_KEY
;
1179 if (SWTPM_NVRAM_Has_MigrationKey())
1180 pgs
.u
.resp
.flags
|= PTM_CONFIG_FLAG_MIGRATION_KEY
;
1181 fuse_reply_ioctl(req
, 0, &pgs
, sizeof(pgs
));
1185 case PTM_SET_BUFFERSIZE
:
1186 if (out_bufsz
!= sizeof(ptm_setbuffersize
)) {
1187 struct iovec iov
= { arg
, sizeof(uint32_t) };
1188 fuse_reply_ioctl_retry(req
, &iov
, 1, NULL
, 0);
1190 ptm_setbuffersize
*in_psbs
= (ptm_setbuffersize
*)in_buf
;
1191 ptm_setbuffersize out_psbs
;
1192 uint32_t buffersize
, minsize
, maxsize
;
1194 buffersize
= in_psbs
->u
.req
.buffersize
;
1196 if (buffersize
> 0 && tpm_running
)
1199 buffersize
= TPMLIB_SetBufferSize(buffersize
,
1203 out_psbs
.u
.resp
.tpm_result
= TPM_SUCCESS
;
1204 out_psbs
.u
.resp
.buffersize
= buffersize
;
1205 out_psbs
.u
.resp
.minsize
= minsize
;
1206 out_psbs
.u
.resp
.maxsize
= maxsize
;
1207 fuse_reply_ioctl(req
, 0, &out_psbs
, sizeof(out_psbs
));
1212 if (out_bufsz
!= sizeof(ptm_getinfo
)) {
1213 struct iovec iov
= { arg
, sizeof(uint32_t) };
1214 fuse_reply_ioctl_retry(req
, &iov
, 1, NULL
, 0);
1216 ptm_getinfo
*in_pgi
= (ptm_getinfo
*)in_buf
;
1217 ptm_getinfo out_pgi
;
1219 uint32_t length
, offset
;
1221 info_data
= TPMLIB_GetInfo(in_pgi
->u
.req
.flags
);
1225 offset
= in_pgi
->u
.req
.offset
;
1226 if (offset
>= strlen(info_data
)) {
1228 goto error_bad_input
;
1231 length
= min(strlen(info_data
) + 1 - offset
,
1232 sizeof(out_pgi
.u
.resp
.buffer
));
1234 out_pgi
.u
.resp
.tpm_result
= 0;
1235 out_pgi
.u
.resp
.totlength
= strlen(info_data
) + 1;
1236 out_pgi
.u
.resp
.length
= length
;
1237 /* client has to collect whole string in case buffer is too small */
1238 memcpy(out_pgi
.u
.resp
.buffer
, &info_data
[offset
], length
);
1241 fuse_reply_ioctl(req
, 0, &out_pgi
, sizeof(out_pgi
));
1246 fuse_reply_err(req
, EINVAL
);
1250 g_mutex_unlock(FILE_OPS_LOCK
);
1253 logprintf(STDOUT_FILENO
, "CUSE TPM is shutting down.\n");
1261 res
= TPM_BAD_PARAMETER
;
1262 fuse_reply_ioctl(req
, 0, &res
, sizeof(res
));
1268 res
= TPM_BAD_ORDINAL
;
1269 fuse_reply_ioctl(req
, 0, &res
, sizeof(res
));
1275 fuse_reply_ioctl(req
, 0, &res
, sizeof(res
));
1280 static void ptm_init_done(void *userdata
)
1282 struct cuse_param
*param
= userdata
;
1285 /* at this point the entry in /dev/ is available */
1286 if (pidfile_write(getpid()) < 0) {
1292 ret
= change_process_owner(param
->runas
);
1300 static void ptm_cleanup(void)
1304 tpmstate_global_free();
1307 static const struct cuse_lowlevel_ops clops
= {
1312 .init_done
= ptm_init_done
,
1315 /* ptm_cuse_lowlevel_main is like cuse_lowlevel_main with the difference that
1316 * it uses a global ptm_fuse_session so we can call fuse_session_exit() on it
1317 * for a graceful exit with cleanups.
1320 ptm_cuse_lowlevel_main(int argc
, char *argv
[], const struct cuse_info
*ci
,
1321 const struct cuse_lowlevel_ops
*clop
, void *userdata
)
1326 ptm_fuse_session
= cuse_lowlevel_setup(argc
, argv
, ci
, clop
, &mt
,
1328 if (ptm_fuse_session
== NULL
)
1332 ret
= fuse_session_loop_mt(ptm_fuse_session
);
1334 ret
= fuse_session_loop(ptm_fuse_session
);
1336 cuse_lowlevel_teardown(ptm_fuse_session
);
1343 #ifndef HAVE_SWTPM_CUSE_MAIN
1344 int main(int argc
, char **argv
)
1346 const char *prgname
= argv
[0];
1347 const char *iface
= "";
1349 int swtpm_cuse_main(int argc
, char **argv
, const char *prgname
, const char *iface
)
1352 int opt
, longindex
= 0;
1353 static struct option longopts
[] = {
1354 {"maj" , required_argument
, 0, 'M'},
1355 {"min" , required_argument
, 0, 'm'},
1356 {"name" , required_argument
, 0, 'n'},
1357 {"runas" , required_argument
, 0, 'r'},
1358 {"log" , required_argument
, 0, 'l'},
1359 {"locality" , required_argument
, 0, 'L'},
1360 {"key" , required_argument
, 0, 'k'},
1361 {"migration-key" , required_argument
, 0, 'K'},
1362 {"pid" , required_argument
, 0, 'p'},
1363 {"tpmstate" , required_argument
, 0, 's'},
1364 {"tpm2" , no_argument
, 0, '2'},
1365 {"help" , no_argument
, 0, 'h'},
1366 {"version" , no_argument
, 0, 'v'},
1369 struct cuse_info cinfo
;
1370 struct cuse_param param
;
1371 const char *devname
= NULL
;
1372 char *cinfo_argv
[1] = { 0 };
1374 struct passwd
*passwd
;
1377 char path
[PATH_MAX
];
1380 memset(&cinfo
, 0, sizeof(cinfo
));
1381 memset(¶m
, 0, sizeof(param
));
1383 log_set_prefix("swtpm: ");
1385 tpmversion
= TPMLIB_TPM_VERSION_1_2
;
1388 opt
= getopt_long(argc
, argv
, "M:m:n:r:hv", longopts
, &longindex
);
1394 case 'M': /* major */
1395 if (sscanf(optarg
, "%u", &num
) != 1) {
1396 logprintf(STDERR_FILENO
, "Could not parse major number\n");
1401 logprintf(STDERR_FILENO
,
1402 "Major number outside valid range [0 - 65535]\n");
1406 cinfo
.dev_major
= num
;
1408 case 'm': /* minor */
1409 if (sscanf(optarg
, "%u", &num
) != 1) {
1410 logprintf(STDERR_FILENO
, "Could not parse major number\n");
1415 logprintf(STDERR_FILENO
,
1416 "Major number outside valid range [0 - 65535]\n");
1420 cinfo
.dev_minor
= num
;
1422 case 'n': /* name */
1423 if (!cinfo
.dev_info_argc
) {
1424 cinfo_argv
[0] = calloc(1, strlen("DEVNAME=") + strlen(optarg
) + 1);
1425 if (!cinfo_argv
[0]) {
1426 logprintf(STDERR_FILENO
, "Out of memory\n");
1432 strcpy(cinfo_argv
[0], "DEVNAME=");
1433 strcat(cinfo_argv
[0], optarg
);
1435 cinfo
.dev_info_argc
= 1;
1436 cinfo
.dev_info_argv
= (const char **)cinfo_argv
;
1439 case 'r': /* runas */
1440 param
.runas
= optarg
;
1443 param
.logging
= optarg
;
1446 param
.keydata
= optarg
;
1448 case 'K': /* migration-key */
1449 param
.migkeydata
= optarg
;
1452 param
.piddata
= optarg
;
1454 case 's': /* tpmstate */
1455 param
.tpmstatedata
= optarg
;
1458 param
.localitydata
= optarg
;
1461 tpmversion
= TPMLIB_TPM_VERSION_2
;
1463 case 'h': /* help */
1464 fprintf(stdout
, usage
, prgname
, iface
);
1466 case 'v': /* version */
1467 fprintf(stdout
, "TPM emulator CUSE interface version %d.%d.%d, "
1468 "Copyright (c) 2014-2015 IBM Corp.\n",
1476 if (optind
< argc
) {
1477 logprintf(STDERR_FILENO
,
1478 "Unknown parameter '%s'\n", argv
[optind
]);
1484 * choose the TPM version early so that getting/setting
1485 * buffer size works.
1487 if (TPMLIB_ChooseTPMVersion(tpmversion
) != TPM_SUCCESS
) {
1488 logprintf(STDERR_FILENO
,
1489 "Error: Could not choose TPM version.\n");
1494 SWTPM_NVRAM_Set_TPMVersion(tpmversion
);
1496 if (!cinfo
.dev_info_argv
) {
1497 logprintf(STDERR_FILENO
, "Error: device name missing\n");
1502 if (handle_log_options(param
.logging
) < 0 ||
1503 handle_key_options(param
.keydata
) < 0 ||
1504 handle_migration_key_options(param
.migkeydata
) < 0 ||
1505 handle_pid_options(param
.piddata
) < 0 ||
1506 handle_tpmstate_options(param
.tpmstatedata
) < 0 ||
1507 handle_locality_options(param
.localitydata
, &locality_flags
) < 0) {
1513 logprintf(STDERR_FILENO
, "Error: Unable to setuid root. uid = %d, "
1514 "euid = %d, gid = %d\n", getuid(), geteuid(), getgid());
1520 if (!(passwd
= getpwnam(param
.runas
))) {
1521 logprintf(STDERR_FILENO
, "User '%s' does not exist\n",
1528 tpmdir
= tpmstate_get_dir();
1529 if (tpmdir
== NULL
) {
1530 logprintf(STDERR_FILENO
,
1531 "Error: No TPM state directory is defined; "
1532 "TPM_PATH is not set\n");
1537 n
= snprintf(path
, sizeof(path
), "/dev/%s", devname
);
1539 logprintf(STDERR_FILENO
,
1540 "Error: Could not create device file name\n");
1544 if (n
>= (int)sizeof(path
)) {
1545 logprintf(STDERR_FILENO
,
1546 "Error: Buffer too small to create device file name\n");
1551 tpmfd
= open(path
, O_RDWR
);
1554 logprintf(STDERR_FILENO
,
1555 "Error: A device '%s' already exists.\n",
1561 if (tpmlib_register_callbacks(&cbs
) != TPM_SUCCESS
) {
1566 worker_thread_init();
1568 #if GLIB_MINOR_VERSION >= 32
1569 g_mutex_init(FILE_OPS_LOCK
);
1571 FILE_OPS_LOCK
= g_mutex_new();
1574 ret
= ptm_cuse_lowlevel_main(1, argv
, &cinfo
, &clops
, ¶m
);
1578 free(cinfo_argv
[0]);