Skip to content

Conversation

@kareem-wolfssl
Copy link
Contributor

@kareem-wolfssl kareem-wolfssl commented May 24, 2024

Description

Keep RNG seed file descriptor open until the RNG is freed.
Fixes #7197

Testing

Built in tests

Checklist

  • added tests
  • updated/added doxygen
  • updated appropriate READMEs
  • Updated manual and documentation

@kareem-wolfssl kareem-wolfssl self-assigned this May 24, 2024
@kareem-wolfssl
Copy link
Contributor Author

Retest this please

…pen. This fixes the case where wc_FreeRng is called when _InitRng was not called on the RNG. Since the FD value defaults to 0 before _InitRng was called, and 0 is potentially a valid FD, it was being closed.
Gate keeping the seed FD open behind WOLFSSL_KEEP_RNG_SEED_FD_OPEN and only
enable by default for HAProxy.  It is causing issues on OS X and may
cause issues on other OSes, and is generally a major behavior change.
os->fd = open("/dev/urandom", O_RDONLY);
#if defined(DEBUG_WOLFSSL)
WOLFSSL_MSG("opened /dev/urandom.");
#ifdef WOLFSSL_KEEP_RNG_SEED_FD_OPEN
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This section might be easier to read with two separate versions of the code... one for existing and another for keeping seed file descriptor open.

@douzzer
Copy link
Contributor

douzzer commented Dec 24, 2025

retest this please

Found unhandled hudson.remoting.RequestAbortedException exception:
java.io.StreamCorruptedException: invalid stream header: 636F7272
	hudson.remoting.Request.abort(Request.java:358)
	hudson.remoting.Channel.terminate(Channel.java:1189)
	hudson.remoting.SynchronousCommandTransport$ReaderThread.run(SynchronousCommandTransport.java:95)

Copy link
Contributor

@douzzer douzzer left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks mostly ready. I left one substantive change req, some optional nits, and some suggestions.

Comment on lines 160 to 161
byte seedFdOpen:1;
byte keepSeedFdOpen:1;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

these need to be parent type WC_BITFIELD, not byte.

WOLFSSL_MSG("opened /dev/urandom.");
#endif
if (os->fd == -1)
#endif
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the #else and #endif associations are getting pretty gnarly here -- I suggest adding /* !NO_DEV_URANDOM */ after this one, and similarly annotating the others with comments.

os->seedFdOpen = 1;
}
}
#else
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is the sort of #else that's begging for annotation with a comment.


#if defined(WOLFSSL_KEEP_RNG_SEED_FD_OPEN) && !defined(USE_WINDOWS_API)
if (!rng->seed.seedFdOpen)
rng->seed.fd = -1;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be nice if we had an XBADFD macro to go with our XBADFILE macro, but alas we do not. If you're feeling inspired, add XBADFD in wc_port.h and use it here, bearing in mind that it will need to be defined for every target that needs to support WOLFSSL_KEEP_RNG_SEED_FD_OPEN.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've only added it for the default target and Zephyr for now, I'm not expecting this macro to be used on embedded systems but XBADFD can easily be expanded if needed.

Comment on lines 1380 to 1381
rng->seed.fd = -1;
rng->seed.seedFdOpen = 0;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do you need to separately track .seedFdOpen? Can't you just use .fd != -1 as a decisive proxy for the fd being open?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunately I can't. fd is initialized to 0 before InitRng is called, so I can't assume that it will be set to -1 as there are some cases where InitRng is not being called before the RNG is used or closed (which is not supposed to happen but it should still be handled gracefully). And 0 is a valid fd which could be returned by open, so I can't use that as a condition either.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: [haproxy] blocking when using chroot + wolfssl

5 participants