From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-bl2nam02on0129.outbound.protection.outlook.com ([104.47.38.129]:64103 "EHLO NAM02-BL2-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1752784AbeBCSES (ORCPT ); Sat, 3 Feb 2018 13:04:18 -0500 From: Sasha Levin To: "linux-kernel@vger.kernel.org" , "stable@vger.kernel.org" CC: Marcelo Ricardo Leitner , "David S . Miller" , Sasha Levin Subject: [PATCH AUTOSEL for 4.14 105/110] sctp: add a ceiling to optlen in some sockopts Date: Sat, 3 Feb 2018 18:01:33 +0000 Message-ID: <20180203180015.29073-105-alexander.levin@microsoft.com> References: <20180203180015.29073-1-alexander.levin@microsoft.com> In-Reply-To: <20180203180015.29073-1-alexander.levin@microsoft.com> Content-Language: en-US Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Sender: stable-owner@vger.kernel.org List-ID: From: Marcelo Ricardo Leitner [ Upstream commit 5960cefab9df76600a1a7d4ff592c59e14616e88 ] Hangbin Liu reported that some sockopt calls could cause the kernel to log a warning on memory allocation failure if the user supplied a large optlen value. That is because some of them called memdup_user() without a ceiling on optlen, allowing it to try to allocate really large buffers. This patch adds a ceiling by limiting optlen to the maximum allowed that would still make sense for these sockopt. Reported-by: Hangbin Liu Signed-off-by: Marcelo Ricardo Leitner Signed-off-by: David S. Miller Signed-off-by: Sasha Levin --- net/sctp/socket.c | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/net/sctp/socket.c b/net/sctp/socket.c index 1c08d86efe94..978fc7d1cf53 100644 --- a/net/sctp/socket.c +++ b/net/sctp/socket.c @@ -3485,6 +3485,8 @@ static int sctp_setsockopt_hmac_ident(struct sock *sk= , =20 if (optlen < sizeof(struct sctp_hmacalgo)) return -EINVAL; + optlen =3D min_t(unsigned int, optlen, sizeof(struct sctp_hmacalgo) + + SCTP_AUTH_NUM_HMACS * sizeof(u16)); =20 hmacs =3D memdup_user(optval, optlen); if (IS_ERR(hmacs)) @@ -3523,6 +3525,11 @@ static int sctp_setsockopt_auth_key(struct sock *sk, =20 if (optlen <=3D sizeof(struct sctp_authkey)) return -EINVAL; + /* authkey->sca_keylength is u16, so optlen can't be bigger than + * this. + */ + optlen =3D min_t(unsigned int, optlen, USHRT_MAX + + sizeof(struct sctp_authkey)); =20 authkey =3D memdup_user(optval, optlen); if (IS_ERR(authkey)) @@ -3880,6 +3887,9 @@ static int sctp_setsockopt_reset_streams(struct sock = *sk, =20 if (optlen < sizeof(*params)) return -EINVAL; + /* srs_number_streams is u16, so optlen can't be bigger than this. */ + optlen =3D min_t(unsigned int, optlen, USHRT_MAX + + sizeof(__u16) * sizeof(*params)); =20 params =3D memdup_user(optval, optlen); if (IS_ERR(params)) --=20 2.11.0