opnsense-src/lib/libc
Bill Paul 4c45fb08aa Correct a bug in the 'allow arbitrary number of socket descriptors' changes
made to the RPC code some months ago. The value of __svc_fdsetsize is being
calculated incorrectly.

Logically, one would assume that __svc_fdsetsize is being used as a
substitute for FD_SETSIZE, with the difference being that __svc_fdsetsize
can be expanded on the fly to accomodate more descriptors if need be.
There are two problems: first, __svc_fdsetsize is not initialized to 0.
Second, __svc_fdsetsize is being calculated in svc.c:xprt_registere() as:

                __svc_fdsetsize = howmany(sock+1, NFDBITS);

This is wrong. If we are adding a socket with index value 4 to the
descriptor set, then __svc_fdsetsize will be 1 (since fds_bits is
an unsigned long, it can support any descriptor from 0 to 31, so we
only need one of them). In order for this to make sense with the
rest of the code though, it should be:

                __svc_fdsetsize = howmany(sock+1, NFDBITS) * NFDBITS;

Now if sock == 4, __svc_fdsetsize will be 32.

This bug causes 2 errors to occur. First, in xprt_register(), it
causes the __svc_fdset descriptor array to be freed and reallocated
unnecessarily. The code checks if it needs to expand the array using
the test: if (sock + 1 > __svc_fdsetsize). The very first time through,
__svc_fdsetsize is 0, which is fine: an array has to be allocated the
first time out. However __svc_fdsetsize is incorrectly set to 1, so
on the second time through, the test (sock + 1 > __svc_fdsetsize)
will still succeed, and the __svc_fdset array will be destroyed and
reallocated for no reason.

Second, the code in svc_run.c:svc_run() can become hopelessly confused.
The svc_run() routine malloc()s its own fd_set array using the value
of __svc_fdsetsize to decide how much memory to allocate. Once the
xprt_register() function expands the __svc_fdset array the first time,
the value for __svc_fdsetsize becomes 2, which is too small: the resulting
calculation causes the code to allocate an array that's only 32 bits wide
when it actually needs 64 bits. It also uses the valuse of __svc_fdsetsize
when copying the contents of the __svc_fdset array into the new array.
The end result is that all but the first 32 file descriptors get lost.

Note: from what I can tell, this bug originated in OpenBSD and was
brought over to us when the code was merged. The bug is still there
in the OpenBSD source.

Total nervous breakdown averted by: Electric Fence 2.0.5
1997-10-14 21:50:17 +00:00
..
amd64 Moved `SRCS+= frexp.c' to the correct Makefile.inc. 1997-10-14 07:43:33 +00:00
compat-43 kill the undead 1997-07-13 14:26:00 +00:00
db Submitted by: Sin'ichiro MIYATANI / Phase One, Inc <siu@phaseone.co.jp> 1997-09-24 20:38:12 +00:00
gen Moved `SRCS+= frexp.c' to the correct Makefile.inc. 1997-10-14 07:43:18 +00:00
gmon Changed all paths to be relative to src/lib instead of src/lib/libc 1997-05-03 03:50:06 +00:00
i386 Moved `SRCS+= frexp.c' to the correct Makefile.inc. 1997-10-14 07:43:33 +00:00
locale Use revived __maskrune for digittoint 1997-09-27 04:34:35 +00:00
net Some adjustments for the resolver use of poll(). For some reason I thought 1997-09-16 06:03:54 +00:00
nls Plug even more failure memory leaks 1997-05-10 04:40:40 +00:00
quad Changed all paths to be relative to src/lib instead of src/lib/libc 1997-05-03 03:50:06 +00:00
regex Changed all paths to be relative to src/lib instead of src/lib/libc 1997-05-03 03:50:06 +00:00
rpc Correct a bug in the 'allow arbitrary number of socket descriptors' changes 1997-10-14 21:50:17 +00:00
stdio Rework previous commit.. I was confused by the number of diffs in the PR 1997-07-06 08:42:37 +00:00
stdlib environmental -> environment. 1997-09-18 06:55:21 +00:00
stdtime Merged in better support of ISO 8601 from elsie.nci.nih.gov. 1997-10-03 19:06:57 +00:00
string Added bounds checking to the example after chasing down someone's 1997-07-26 00:47:06 +00:00
sys Add the AIO/LIO to libc. They aren't fully done yet, but have been in the 1997-10-10 05:48:16 +00:00
xdr Restore Id. 1997-05-28 16:29:12 +00:00
yp Use ${DESTDIR} correctly in front of absolute paths. 1997-05-23 08:24:00 +00:00
Makefile Use ${DESTDIR} correctly in front of absolute paths. 1997-05-23 08:24:00 +00:00
Makefile.inc Changed all paths to be relative to src/lib instead of src/lib/libc 1997-05-03 03:50:06 +00:00