From e5e3e746fe4a8c9ab197f4400a7e5e0327c0674d Mon Sep 17 00:00:00 2001 From: Matt Macy Date: Sun, 22 Jul 2018 20:02:14 +0000 Subject: [PATCH] Fix a potential use after free in getsockopt() access to inp_options Discussed with: jhb Reviewed by: sbruno, transport MFC after: 2 weeks Sponsored by: Limelight Networks Differential Revision: https://reviews.freebsd.org/D14621 --- sys/netinet/ip_output.c | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/sys/netinet/ip_output.c b/sys/netinet/ip_output.c index 90c5251204f..9d7b9cbe866 100644 --- a/sys/netinet/ip_output.c +++ b/sys/netinet/ip_output.c @@ -1256,13 +1256,23 @@ ip_ctloutput(struct socket *so, struct sockopt *sopt) switch (sopt->sopt_name) { case IP_OPTIONS: case IP_RETOPTS: - if (inp->inp_options) - error = sooptcopyout(sopt, - mtod(inp->inp_options, - char *), - inp->inp_options->m_len); - else + INP_RLOCK(inp); + if (inp->inp_options) { + struct mbuf *options; + + options = m_dup(inp->inp_options, M_NOWAIT); + INP_RUNLOCK(inp); + if (options != NULL) { + error = sooptcopyout(sopt, + mtod(options, char *), + options->m_len); + m_freem(options); + } else + error = ENOMEM; + } else { + INP_RUNLOCK(inp); sopt->sopt_valsize = 0; + } break; case IP_TOS: