Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751901AbdCCSGy (ORCPT ); Fri, 3 Mar 2017 13:06:54 -0500 Received: from mail-it0-f54.google.com ([209.85.214.54]:35132 "EHLO mail-it0-f54.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751641AbdCCSGx (ORCPT ); Fri, 3 Mar 2017 13:06:53 -0500 MIME-Version: 1.0 In-Reply-To: <20170303172330.83421-1-glider@google.com> References: <20170303172330.83421-1-glider@google.com> From: Eric Dumazet Date: Fri, 3 Mar 2017 09:35:49 -0800 Message-ID: Subject: Re: [PATCH] selinux: check for address length in selinux_socket_bind() To: Alexander Potapenko Cc: Dmitry Vyukov , Kostya Serebryany , Kees Cook , Paul Moore , sds@tycho.nsa.gov, LKML , netdev , selinux@tycho.nsa.gov Content-Type: text/plain; charset=UTF-8 Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Length: 2019 Lines: 54 On Fri, Mar 3, 2017 at 9:23 AM, Alexander Potapenko wrote: > This happens because bind() unconditionally copies |size| bytes of > |addr| to the kernel, leaving the rest uninitialized. Then > security_socket_bind() reads the IP address bytes, including the > uninitialized ones, to determine the port, or e.g. pass them further to > sel_netnode_find(), which uses them to calculate a hash. > > Signed-off-by: Alexander Potapenko > --- > security/selinux/hooks.c | 9 +++++++++ > 1 file changed, 9 insertions(+) > > diff --git a/security/selinux/hooks.c b/security/selinux/hooks.c > index 0a4b4b040e0a..eba54489b11b 100644 > --- a/security/selinux/hooks.c > +++ b/security/selinux/hooks.c > @@ -4351,10 +4351,19 @@ static int selinux_socket_bind(struct socket *sock, struct sockaddr *address, in > u32 sid, node_perm; > > if (family == PF_INET) { > + if (addrlen != sizeof(struct sockaddr_in)) { Please take a look at inet_bind() The correct test would be : if (addrlen < sizeof(struct sockaddr_in)) err = -EINVAL; ... > + err = -EINVAL; > + goto out; > + } > addr4 = (struct sockaddr_in *)address; > snum = ntohs(addr4->sin_port); > addrp = (char *)&addr4->sin_addr.s_addr; > + > } else { > + if (addrlen != sizeof(struct sockaddr_in6)) { Look at inet6_bind() if (addrlen < SIN6_LEN_RFC2133) > + err = -EINVAL; > + goto out; > + } > addr6 = (struct sockaddr_in6 *)address; > snum = ntohs(addr6->sin6_port); > addrp = (char *)&addr6->sin6_addr.s6_addr; > -- > 2.12.0.rc1.440.g5b76565f74-goog >