In unp_externalize, don't do anything if an SCM_RIGHTS control message

was sent with zero file descriptors in it. Otherwise, a zero-length
temporary storage would be allocated which triggers panic on DIAGNOSTIC
kernels (but is harmless for release kernels).
reviewed by Taylor R Campbell
This commit is contained in:
drochner 2013-08-01 19:33:21 +00:00
parent 02096e2d37
commit 3b7fa5ee7c

View File

@ -1,4 +1,4 @@
/* $NetBSD: uipc_usrreq.c,v 1.142 2013/06/27 18:54:31 christos Exp $ */
/* $NetBSD: uipc_usrreq.c,v 1.143 2013/08/01 19:33:21 drochner Exp $ */
/*-
* Copyright (c) 1998, 2000, 2004, 2008, 2009 The NetBSD Foundation, Inc.
@ -96,7 +96,7 @@
*/
#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: uipc_usrreq.c,v 1.142 2013/06/27 18:54:31 christos Exp $");
__KERNEL_RCSID(0, "$NetBSD: uipc_usrreq.c,v 1.143 2013/08/01 19:33:21 drochner Exp $");
#include <sys/param.h>
#include <sys/systm.h>
@ -1242,6 +1242,8 @@ unp_externalize(struct mbuf *rights, struct lwp *l, int flags)
const size_t nfds = (cm->cmsg_len - CMSG_ALIGN(sizeof(*cm))) /
sizeof(file_t *);
if (nfds == 0)
goto noop;
int * const fdp = kmem_alloc(nfds * sizeof(int), KM_SLEEP);
rw_enter(&p->p_cwdi->cwdi_lock, RW_READER);
@ -1346,16 +1348,16 @@ unp_externalize(struct mbuf *rights, struct lwp *l, int flags)
cm->cmsg_len = CMSG_LEN(0);
rights->m_len = CMSG_SPACE(0);
}
rw_exit(&p->p_cwdi->cwdi_lock);
kmem_free(fdp, nfds * sizeof(int));
noop:
/*
* Don't disclose kernel memory in the alignment space.
*/
KASSERT(cm->cmsg_len <= rights->m_len);
memset(&mtod(rights, char *)[cm->cmsg_len], 0, rights->m_len -
cm->cmsg_len);
rw_exit(&p->p_cwdi->cwdi_lock);
kmem_free(fdp, nfds * sizeof(int));
return error;
}