From nobody Tue Nov 26 03:30:37 2024 Received: from smtp-8fad.mail.infomaniak.ch (smtp-8fad.mail.infomaniak.ch [83.166.143.173]) (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 989661BD4E1 for ; Tue, 22 Oct 2024 16:10:37 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=83.166.143.173 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729613440; cv=none; b=FYMuQm9V7MBTVgLmgOIsoLqFUU0HA47VIE5+nks2inpyWgfG/PX1dYbIpGBhvsHpaYJimA2opzw0QRgu0Dj/KSIBx043eRfrKQw2qUYazQ2WTAYlXt/lccbItZEvXx5WiAUARDcqUtKw+uZ3eFuEFfF+zk++4v4b2NtO8z4+WVs= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729613440; c=relaxed/simple; bh=G9TEg3HND4LdWjYAiNhvnUyB/nAl4YVa85872ufJ0jw=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=LtHHN9PJWlL5A3t89BSpSy4GBB8VQvSpZKRvwpJ2OCHFkNSqLOPVOGZ74yXGNrXrrxh5yVfaa95lR2assXlYgTEKTmf4LVP3kBFqOp4gZmek1d1uXIpd8SJUHo/gq9Dmkzur6W+m6klObFjFFVrSujDOkUKSCGK07tpFkpcU0v8= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=digikod.net; spf=pass smtp.mailfrom=digikod.net; dkim=pass (1024-bit key) header.d=digikod.net header.i=@digikod.net header.b=1C4rHIsb; arc=none smtp.client-ip=83.166.143.173 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="1C4rHIsb" Received: from smtp-4-0001.mail.infomaniak.ch (unknown [IPv6:2001:1600:7:10:40ca:feff:fe05:1]) by smtp-4-3000.mail.infomaniak.ch (Postfix) with ESMTPS id 4XXxwq72dhz1BWj; Tue, 22 Oct 2024 18:10:35 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=digikod.net; s=20191114; t=1729613435; bh=UlOuSO/p6IwMwoHL3XID4K4bzg4p/fofuCyDr7AkBj0=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=1C4rHIsbDLoqGpXA2BKX3pB5r+q6FRnp9tugaz1n59n9kmQ1/Ma1yuqxSell4TulT By9x9JZq0idBUrepthlZxwqnPaLZ603K2zb9wVUNLuVHuKe6GHyACRvkZ7eO+WPjD+ H0NE3GFLelvafjwj6A3zZaZ2Y0cin77IDdJCxsZU= Received: from unknown by smtp-4-0001.mail.infomaniak.ch (Postfix) with ESMTPA id 4XXxwq1jZjz6gb; Tue, 22 Oct 2024 18:10:35 +0200 (CEST) From: =?UTF-8?q?Micka=C3=ABl=20Sala=C3=BCn?= To: Eric Paris , Paul Moore , =?UTF-8?q?G=C3=BCnther=20Noack?= , "Serge E . Hallyn" Cc: =?UTF-8?q?Micka=C3=ABl=20Sala=C3=BCn?= , Ben Scarlato , Casey Schaufler , Charles Zaffery , James Morris , Jann Horn , Jeff Xu , Jorge Lucangeli Obes , Kees Cook , Konstantin Meskhidze , Matt Bobrowski , Mikhail Ivanov , Praveen K Paladugu , Robert Salvet , Shervin Oloumi , Song Liu , Tahera Fahimi , audit@vger.kernel.org, linux-kernel@vger.kernel.org, linux-security-module@vger.kernel.org Subject: [RFC PATCH v2 12/14] landlock: Log TCP bind and connect denials Date: Tue, 22 Oct 2024 18:10:07 +0200 Message-ID: <20241022161009.982584-13-mic@digikod.net> In-Reply-To: <20241022161009.982584-1-mic@digikod.net> References: <20241022161009.982584-1-mic@digikod.net> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-Infomaniak-Routing: alpha Add audit support to socket_bind and socket_connect hooks. Audit record sample: DENY: domain=3D4533720601 blockers=3Dnet_connect_tcp daddr=3D127.0.0.1= dest=3D80 SYSCALL: arch=3Dc000003e syscall=3D42 success=3Dno exit=3D-13 ... Cc: G=C3=BCnther Noack Cc: Konstantin Meskhidze Cc: Mikhail Ivanov Signed-off-by: Micka=C3=ABl Sala=C3=BCn Link: https://lore.kernel.org/r/20241022161009.982584-13-mic@digikod.net --- security/landlock/audit.c | 11 +++++++++ security/landlock/audit.h | 1 + security/landlock/net.c | 52 ++++++++++++++++++++++++++++++++++++--- 3 files changed, 60 insertions(+), 4 deletions(-) diff --git a/security/landlock/audit.c b/security/landlock/audit.c index 898c95ebe847..c31a4a8719ee 100644 --- a/security/landlock/audit.c +++ b/security/landlock/audit.c @@ -41,6 +41,12 @@ static const char *const fs_access_strings[] =3D { }; static_assert(ARRAY_SIZE(fs_access_strings) =3D=3D LANDLOCK_NUM_ACCESS_FS); =20 +static const char *const net_access_strings[] =3D { + [BIT_INDEX(LANDLOCK_ACCESS_NET_BIND_TCP)] =3D "net_bind_tcp", + [BIT_INDEX(LANDLOCK_ACCESS_NET_CONNECT_TCP)] =3D "net_connect_tcp", +}; +static_assert(ARRAY_SIZE(net_access_strings) =3D=3D LANDLOCK_NUM_ACCESS_NE= T); + static __attribute_const__ const char * get_blocker(const enum landlock_request_type type, const unsigned long access_bit) @@ -58,6 +64,11 @@ get_blocker(const enum landlock_request_type type, if (WARN_ON_ONCE(access_bit >=3D ARRAY_SIZE(fs_access_strings))) return "unknown"; return fs_access_strings[access_bit]; + + case LANDLOCK_REQUEST_NET_ACCESS: + if (WARN_ON_ONCE(access_bit >=3D ARRAY_SIZE(net_access_strings))) + return "unknown"; + return net_access_strings[access_bit]; } =20 WARN_ON_ONCE(1); diff --git a/security/landlock/audit.h b/security/landlock/audit.h index 320394fd6b84..1075b0c8401f 100644 --- a/security/landlock/audit.h +++ b/security/landlock/audit.h @@ -18,6 +18,7 @@ enum landlock_request_type { LANDLOCK_REQUEST_PTRACE =3D 1, LANDLOCK_REQUEST_FS_CHANGE_LAYOUT, LANDLOCK_REQUEST_FS_ACCESS, + LANDLOCK_REQUEST_NET_ACCESS, }; =20 /* diff --git a/security/landlock/net.c b/security/landlock/net.c index 27872d0f7e11..c21afd6e0b4d 100644 --- a/security/landlock/net.c +++ b/security/landlock/net.c @@ -7,10 +7,12 @@ */ =20 #include +#include #include #include #include =20 +#include "audit.h" #include "common.h" #include "cred.h" #include "limits.h" @@ -56,6 +58,10 @@ static int current_check_access_socket(struct socket *co= nst sock, }; const struct landlock_ruleset *const dom =3D landlock_match_ruleset(landlock_get_current_domain(), any_net); + struct lsm_network_audit audit_net =3D {}; + struct landlock_request request =3D { + .type =3D LANDLOCK_REQUEST_NET_ACCESS, + }; =20 if (!dom) return 0; @@ -72,18 +78,49 @@ static int current_check_access_socket(struct socket *c= onst sock, =20 switch (address->sa_family) { case AF_UNSPEC: - case AF_INET: + case AF_INET: { + const struct sockaddr_in *addr4; + if (addrlen < sizeof(struct sockaddr_in)) return -EINVAL; - port =3D ((struct sockaddr_in *)address)->sin_port; + + addr4 =3D (struct sockaddr_in *)address; + port =3D addr4->sin_port; + + if (access_request =3D=3D LANDLOCK_ACCESS_NET_CONNECT_TCP) { + audit_net.dport =3D port; + audit_net.v4info.daddr =3D addr4->sin_addr.s_addr; + } else if (access_request =3D=3D LANDLOCK_ACCESS_NET_BIND_TCP) { + audit_net.sport =3D port; + audit_net.v4info.saddr =3D addr4->sin_addr.s_addr; + } else { + WARN_ON_ONCE(1); + } break; + } =20 #if IS_ENABLED(CONFIG_IPV6) - case AF_INET6: + case AF_INET6: { + const struct sockaddr_in6 *addr6; + if (addrlen < SIN6_LEN_RFC2133) return -EINVAL; - port =3D ((struct sockaddr_in6 *)address)->sin6_port; + + addr6 =3D (struct sockaddr_in6 *)address; + port =3D addr6->sin6_port; + audit_net.v6info.saddr =3D addr6->sin6_addr; + + if (access_request =3D=3D LANDLOCK_ACCESS_NET_CONNECT_TCP) { + audit_net.dport =3D port; + audit_net.v6info.daddr =3D addr6->sin6_addr; + } else if (access_request =3D=3D LANDLOCK_ACCESS_NET_BIND_TCP) { + audit_net.sport =3D port; + audit_net.v6info.saddr =3D addr6->sin6_addr; + } else { + WARN_ON_ONCE(1); + } break; + } #endif /* IS_ENABLED(CONFIG_IPV6) */ =20 default: @@ -152,6 +189,13 @@ static int current_check_access_socket(struct socket *= const sock, ARRAY_SIZE(layer_masks))) return 0; =20 + audit_net.family =3D address->sa_family; + request.audit.type =3D LSM_AUDIT_DATA_NET; + request.audit.u.net =3D &audit_net; + request.access =3D access_request; + request.layer_masks =3D &layer_masks; + request.layer_masks_size =3D ARRAY_SIZE(layer_masks); + landlock_log_denial(dom, &request); return -EACCES; } =20 --=20 2.47.0