Some loops access the fd table of a different process, and drop the
filedesc lock while iterating, so they check the table's refcount.
However, we access the table before the first iteration, in order to get
the number of table entries, and this access may represent a
use-after-free.
Fix the problem by checking the refcount before we start iterating. I
don't see a nicer way to do it. BTW, I wonder why the FILEDESC_FOREACH
macros are exported by filedesc.h when they are only needed in
kern_descrip.c, especially since they use fdlastfile_single() and thus
don't assert that the table lock is held.
Reported by: pho