summaryrefslogtreecommitdiffstats
path: root/sys/kern
diff options
context:
space:
mode:
authorjhb <jhb@FreeBSD.org>2018-04-27 18:07:31 +0000
committerjhb <jhb@FreeBSD.org>2018-04-27 18:07:31 +0000
commite721d1ec9168f194b473a5f2ba6cd8efb19e30d4 (patch)
treeba57d9414d25ed24f0b03f8f437645efdf895606 /sys/kern
parent4223ca8e51c2eda332673d16f0dbf27e533a17a1 (diff)
downloadFreeBSD-src-e721d1ec9168f194b473a5f2ba6cd8efb19e30d4.zip
FreeBSD-src-e721d1ec9168f194b473a5f2ba6cd8efb19e30d4.tar.gz
MFC 332657:
Properly do a deep copy of the ioctls capability array for fget_cap(). fget_cap() tries to do a cheaper snapshot of a file descriptor without holding the file descriptor lock. This snapshot does not do a deep copy of the ioctls capability array, but instead uses a different return value to inform the caller to retry the copy with the lock held. However, filecaps_copy() was returning 1 to indicate that a retry was required, and fget_cap() was checking for 0 (actually '!filecaps_copy()'). As a result, fget_cap() did not do a deep copy of the ioctls array and just reused the original pointer. This cause multiple file descriptor entries to think they owned the same pointer and eventually resulted in duplicate frees. The only code path that I'm aware of that triggers this is to create a listen socket that has a restricted list of ioctls and then call accept() which calls fget_cap() with a valid filecaps structure from getsock_cap(). To fix, change the return value of filecaps_copy() to return true if it succeeds in copying the caps and false if it fails because the lock is required. I find this more intuitive than fixing the caller in this case. While here, change the return type from 'int' to 'bool'. Finally, make filecaps_copy() more robust in the failure case by not copying any of the source filecaps structure over. This avoids the possibility of leaking a pointer into a structure if a similar future caller doesn't properly handle the return value from filecaps_copy() at the expense of one more branch. I also added a test case that panics before this change and now passes.
Diffstat (limited to 'sys/kern')
-rw-r--r--sys/kern/kern_descrip.c10
1 files changed, 5 insertions, 5 deletions
diff --git a/sys/kern/kern_descrip.c b/sys/kern/kern_descrip.c
index e194eb2..f3da37cc 100644
--- a/sys/kern/kern_descrip.c
+++ b/sys/kern/kern_descrip.c
@@ -1446,16 +1446,16 @@ filecaps_init(struct filecaps *fcaps)
* Note that if the table was not locked, the caller has to check the relevant
* sequence counter to determine whether the operation was successful.
*/
-int
+bool
filecaps_copy(const struct filecaps *src, struct filecaps *dst, bool locked)
{
size_t size;
+ if (src->fc_ioctls != NULL && !locked)
+ return (false);
*dst = *src;
if (src->fc_ioctls == NULL)
- return (0);
- if (!locked)
- return (1);
+ return (true);
KASSERT(src->fc_nioctls > 0,
("fc_ioctls != NULL, but fc_nioctls=%hd", src->fc_nioctls));
@@ -1463,7 +1463,7 @@ filecaps_copy(const struct filecaps *src, struct filecaps *dst, bool locked)
size = sizeof(src->fc_ioctls[0]) * src->fc_nioctls;
dst->fc_ioctls = malloc(size, M_FILECAPS, M_WAITOK);
bcopy(src->fc_ioctls, dst->fc_ioctls, size);
- return (0);
+ return (true);
}
/*
OpenPOWER on IntegriCloud