From patchwork Wed Jan 3 16:34:15 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Micka=C3=ABl_Sala=C3=BCn?= X-Patchwork-Id: 13510237 X-Patchwork-Delegate: paul@paul-moore.com Received: from smtp-42ac.mail.infomaniak.ch (smtp-42ac.mail.infomaniak.ch [84.16.66.172]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 5B5D84429 for ; Wed, 3 Jan 2024 16:34:37 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=digikod.net Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=digikod.net Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=digikod.net header.i=@digikod.net header.b="CqA9Wj2M" Received: from smtp-2-0001.mail.infomaniak.ch (unknown [10.5.36.108]) by smtp-3-3000.mail.infomaniak.ch (Postfix) with ESMTPS id 4T4wKd71NvzMq5G2; Wed, 3 Jan 2024 16:34:29 +0000 (UTC) Received: from unknown by smtp-2-0001.mail.infomaniak.ch (Postfix) with ESMTPA id 4T4wKc6gBYzMpp3b; Wed, 3 Jan 2024 17:34:28 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=digikod.net; s=20191114; t=1704299669; bh=EjKR+ktAGKzbgbl+7KSmOTO+aAwSZnEnzxyF/DB0Yf8=; h=From:To:Cc:Subject:Date:From; b=CqA9Wj2M3KLn6rdmnOeY/v0/pX4SZdgRP4ItoN01aWulf4gYdzbz48tA63Ifp4jCi IwFPfHBj/1Tr/ARY35qP+sQsBHT8lyUFkHJKkFz2nkC5Eo9JQN+9YXO4wujRs3Fjmf G6QVDHpG38VinzMGdbEqeZeANe9l2tjWjG8LRgcg= From: =?utf-8?q?Micka=C3=ABl_Sala=C3=BCn?= To: Eric Paris , Paul Moore , Stephen Smalley Cc: =?utf-8?q?Micka=C3=ABl_Sala=C3=BCn?= , =?utf-8?q?G?= =?utf-8?q?=C3=BCnther_Noack?= , Konstantin Meskhidze , Muhammad Usama Anjum , linux-security-module@vger.kernel.org, netdev@vger.kernel.org Subject: [PATCH v3] selinux: Fix error priority for bind with AF_UNSPEC on PF_INET6 socket Date: Wed, 3 Jan 2024 17:34:15 +0100 Message-ID: <20240103163415.304358-1-mic@digikod.net> Precedence: bulk X-Mailing-List: linux-security-module@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Infomaniak-Routing: alpha The IPv6 network stack first checks the sockaddr length (-EINVAL error) before checking the family (-EAFNOSUPPORT error). This was discovered thanks to commit a549d055a22e ("selftests/landlock: Add network tests"). Cc: Eric Paris Cc: Konstantin Meskhidze Cc: Paul Moore Cc: Stephen Smalley Reported-by: Muhammad Usama Anjum Closes: https://lore.kernel.org/r/0584f91c-537c-4188-9e4f-04f192565667@collabora.com Fixes: 0f8db8cc73df ("selinux: add AF_UNSPEC and INADDR_ANY checks to selinux_socket_bind()") Signed-off-by: Mickaël Salaün Tested-by: Muhammad Usama Anjum --- Changes since v2: https://lore.kernel.org/r/20231229171922.106190-1-mic@digikod.net * Add !PF_INET6 check and comments (suggested by Paul). * s/AF_INET/PF_INET/g (cosmetic change). Changes since v1: https://lore.kernel.org/r/20231228113917.62089-1-mic@digikod.net * Use the "family" variable (suggested by Paul). --- security/selinux/hooks.c | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/security/selinux/hooks.c b/security/selinux/hooks.c index feda711c6b7b..8b1429eb2db5 100644 --- a/security/selinux/hooks.c +++ b/security/selinux/hooks.c @@ -4667,6 +4667,13 @@ static int selinux_socket_bind(struct socket *sock, struct sockaddr *address, in return -EINVAL; addr4 = (struct sockaddr_in *)address; if (family_sa == AF_UNSPEC) { + if (family == PF_INET6) { + /* Length check from inet6_bind_sk() */ + if (addrlen < SIN6_LEN_RFC2133) + return -EINVAL; + /* Family check from __inet6_bind() */ + goto err_af; + } /* see __inet_bind(), we only want to allow * AF_UNSPEC if the address is INADDR_ANY */