periph_drivers[] stopped being a linker set in 94173c3c9b84: registering a
driver allocates a new array and frees the old one, unregistering removes
entries. cam_xpt.c still assumes neither happens. xptpdrvtraverse() and
xptplistperiphfunc() walk the array with no lock held, and
xptperiphlistmatch() hands a pointer into the array back to userland as the
XPT_DEV_MATCH resume cookie, then dereferences it on the next ioctl.
Iterate by index, re-reading the array under the topology lock on each step.
Make the cookie the periph driver itself and validate it against the array
before use; it is a void * in struct ccb_dm_cookie, so this is not an ABI
change. Look drivers up by name under the lock too.
This one is by inspection. I could not trigger the race: 400 XPT_DEV_MATCH
ioctls against 15 load/unload cycles came back clean with and without the
change, which says the window is narrow, not that it is closed.
struct periph_driver still has no refcount, so a module can go away between
fetching a driver and using it. Closing that needs the reference counting
camperiphfree() already asks for in a comment. Not freeing the old array in
periphdriver_register(), or deferring the free, may be a smaller answer than
this one; I would rather hear which you want than guess.
MFC after: 1 month
Sponsored by: Advanced Micro Devices, Inc.
Depends on D62322