wolfSSL / wolfTPM

wolfTPM is a highly portable TPM 2.0 library, designed for embedded use.
https://www.wolfssl.com
GNU General Public License v2.0
240 stars 57 forks source link

tpm2_linux open() file descriptor 0 should be allowed #365

Closed radomik closed 1 month ago

radomik commented 1 month ago

Hi, I'm using older version of WolfTPM but see this part of code is same in newest version.

What my program does is intense usage of TPM2_GetRandom from few threads in parallel, for couple of minutes.

Observation is that in some cases /dev/tpm0 cannot be opened with errno=Success that caused then cascade of errno=Device or resource busy.

As from Linux man for open(0 can be seen that open() valid return value is nonnegative integer, thus 0 is valid value.

On success, open(), openat(), and creat() return the new file
       descriptor (a nonnegative integer).  On error, -1 is returned and
       [errno](https://man7.org/linux/man-pages/man3/errno.3.html) is set to indicate the error.

Suggest to change a condition fro > to >= to:

    fd = open(TPM2_LINUX_DEV, O_RDWR | O_NONBLOCK);
    if (fd >= 0) {
        /* Send the TPM command */

After fix, I see no more issues with my code using wolfTPM library.

dgarske commented 1 month ago

Hi @radomik , thank you so much for the detailed report! I'll put up a fix shortly and reference it here. Thanks, David Garske, wolfSSL

dgarske commented 1 month ago

Hi @radomik , I posted the fix in PR #366.