From nobody Tue Nov 26 01:29:03 2024 Received: from smtp-42a9.mail.infomaniak.ch (smtp-42a9.mail.infomaniak.ch [84.16.66.169]) (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 C228470834 for ; Fri, 22 Nov 2024 14:34:11 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=84.16.66.169 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1732286056; cv=none; b=MVl+1M4nWjPBvglNudOg/Q5Ri+trzV+lmqEgKB/NPxWWRfraXp5T8gcXWDdqnS6QMi6+too0uifxRBFUGZ1YHEDbJ8Hfy2vmR3YEPSelqS3lb6ESN/fOUT6mFOod9tzMHuCNUQnXW3rUZBEC9iiW4Ac3/dbuqE1CV7jjVJ0rdjU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1732286056; c=relaxed/simple; bh=oj/6mcirhBGSz78MkVxQZgxWtQyM37QmbXlg51RuIoE=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=GrEIQFZQxlS+NCfyIJ8KvnaUmTaMDsf46CZSbOqHa6IkOp7ZBqm507rbaG09IP79cLJ1xeG5FjV7pGQjVTK5uMO9/rJVXXet4fiA43ar8EgU99QXG1JhljkxEwOjWmiWhznCjffWwU8THhYHDN026DOOu8j1b8fYDhKBeFTQS1k= 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=L3S9+VSv; arc=none smtp.client-ip=84.16.66.169 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="L3S9+VSv" Received: from smtp-4-0000.mail.infomaniak.ch (unknown [IPv6:2001:1600:7:10:40ca:feff:fe05:0]) by smtp-4-3000.mail.infomaniak.ch (Postfix) with ESMTPS id 4XvyK84CR0zdhH; Fri, 22 Nov 2024 15:34:04 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=digikod.net; s=20191114; t=1732286044; bh=t/jeJLYng7iwFnD7UmSkTCH0JnlFPYPzlVzfF2WGADk=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=L3S9+VSvysJWYQqUlRssyyXMNPfahLvq0LusLrc09cn3ux7e16vB9JTg/p8xgM6aU JX9vqipiE4WEmB4oQYUv8lNxOc0uHQv+2nMu4u8dogJLHB3DgcXxx9bLoVFTNSSKMk SFxlkPdBicP8mKz22XvI1QjMw33tI3gHfgtsGVec= Received: from unknown by smtp-4-0000.mail.infomaniak.ch (Postfix) with ESMTPA id 4XvyK76d31znDH; Fri, 22 Nov 2024 15:34:03 +0100 (CET) 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 , Francis Laniel , James Morris , Jann Horn , Jeff Xu , Jorge Lucangeli Obes , Kees Cook , Konstantin Meskhidze , Matt Bobrowski , Mikhail Ivanov , Phil Sutter , 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: [PATCH v3 01/23] lsm: Only build lsm_audit.c if CONFIG_SECURITY and CONFIG_AUDIT are set Date: Fri, 22 Nov 2024 15:33:31 +0100 Message-ID: <20241122143353.59367-2-mic@digikod.net> In-Reply-To: <20241122143353.59367-1-mic@digikod.net> References: <20241122143353.59367-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 When CONFIG_AUDIT is set, its CONFIG_NET dependency is also set, and the dev_get_by_index and init_net symbols (used by dump_common_audit_data) are found by the linker. dump_common_audit_data() should then failed to build when CONFIG_NET is not set. However, because the compiler is smart, it knows that audit_log_start() always return NULL when !CONFIG_AUDIT, and it doesn't build the body of common_lsm_audit(). As a side effect, dump_common_audit_data() is not built and the linker doesn't error out because of missing symbols. Let's only build lsm_audit.o when CONFIG_SECURITY and CONFIG_AUDIT are both set, which is checked with the new CONFIG_HAS_SECURITY_AUDIT. ipv4_skb_to_auditdata() and ipv6_skb_to_auditdata() are only used by Smack if CONFIG_AUDIT is set, so they don't need fake implementations. Because common_lsm_audit() is used in multiple places without CONFIG_AUDIT checks, add a fake implementation. Cc: Casey Schaufler Cc: James Morris Cc: Paul Moore Cc: Serge E. Hallyn Signed-off-by: Micka=C3=ABl Sala=C3=BCn Link: https://lore.kernel.org/r/20241122143353.59367-2-mic@digikod.net --- Changes since v2: - Add CONFIG_HAS_SECURITY_AUDIT to fix the build with AUDIT && !SECURITY, reported by Guenter Roeck. --- include/linux/lsm_audit.h | 14 ++++++++++++++ security/Kconfig | 5 +++++ security/Makefile | 2 +- 3 files changed, 20 insertions(+), 1 deletion(-) diff --git a/include/linux/lsm_audit.h b/include/linux/lsm_audit.h index 97a8b21eb033..c2b01380262c 100644 --- a/include/linux/lsm_audit.h +++ b/include/linux/lsm_audit.h @@ -116,14 +116,28 @@ struct common_audit_data { #define v4info fam.v4 #define v6info fam.v6 =20 +#ifdef CONFIG_AUDIT + int ipv4_skb_to_auditdata(struct sk_buff *skb, struct common_audit_data *ad, u8 *proto); =20 +#if IS_ENABLED(CONFIG_IPV6) int ipv6_skb_to_auditdata(struct sk_buff *skb, struct common_audit_data *ad, u8 *proto); +#endif /* IS_ENABLED(CONFIG_IPV6) */ =20 void common_lsm_audit(struct common_audit_data *a, void (*pre_audit)(struct audit_buffer *, void *), void (*post_audit)(struct audit_buffer *, void *)); =20 +#else /* CONFIG_AUDIT */ + +static inline void common_lsm_audit(struct common_audit_data *a, + void (*pre_audit)(struct audit_buffer *, void *), + void (*post_audit)(struct audit_buffer *, void *)) +{ +} + +#endif /* CONFIG_AUDIT */ + #endif diff --git a/security/Kconfig b/security/Kconfig index 28e685f53bd1..f10dbf15c294 100644 --- a/security/Kconfig +++ b/security/Kconfig @@ -64,6 +64,11 @@ config SECURITY =20 If you are unsure how to answer this question, answer N. =20 +config HAS_SECURITY_AUDIT + def_bool y + depends on AUDIT + depends on SECURITY + config SECURITYFS bool "Enable the securityfs filesystem" help diff --git a/security/Makefile b/security/Makefile index cc0982214b84..22ff4c8bd8ce 100644 --- a/security/Makefile +++ b/security/Makefile @@ -15,7 +15,7 @@ obj-$(CONFIG_SECURITY) +=3D security.o obj-$(CONFIG_SECURITYFS) +=3D inode.o obj-$(CONFIG_SECURITY_SELINUX) +=3D selinux/ obj-$(CONFIG_SECURITY_SMACK) +=3D smack/ -obj-$(CONFIG_SECURITY) +=3D lsm_audit.o +obj-$(CONFIG_HAS_SECURITY_AUDIT) +=3D lsm_audit.o obj-$(CONFIG_SECURITY_TOMOYO) +=3D tomoyo/ obj-$(CONFIG_SECURITY_APPARMOR) +=3D apparmor/ obj-$(CONFIG_SECURITY_YAMA) +=3D yama/ --=20 2.47.0 From nobody Tue Nov 26 01:29:03 2024 Received: from smtp-bc08.mail.infomaniak.ch (smtp-bc08.mail.infomaniak.ch [45.157.188.8]) (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 1492C5D477; Fri, 22 Nov 2024 14:34:12 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=45.157.188.8 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1732286056; cv=none; b=ZhYkkf4uPSPHmW1Zh1q5GW6ssLyKUVtUAFTWmnVj19HJtxfzvSpf2Jlz2cCtfqmHJ1+yphpeE3hoZgpIcYcIq1I8rTeixtUdsp7pCxSaMQVmXmNPWBHNZsqDMhiwjjE2ht0UriGT427RTHfyjLW2IqRr4HAkiUDuz0YwCxMh4Ag= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1732286056; c=relaxed/simple; bh=2iJktCeaTluhzGMhzwBTsU05yTdrj/X6rZ9VX/kRBo4=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=hPuyl6E1kjbletHPh5FX2oLnntHsXg0qtTVhWhOT2TGwc2yJ9OLTiaexD5RiIhxdxdmGA5bINtAVnDiY8OnCkc1tbx2r59YLr4ESq9DuAd3X+YdEAj3I9zkmuasGFJUI6GwOvhJpBj/jdK+9dIgoItYBltx6Dvgm5B6wghxFbxk= 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=14EZBYXs; arc=none smtp.client-ip=45.157.188.8 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="14EZBYXs" Received: from smtp-3-0000.mail.infomaniak.ch (unknown [IPv6:2001:1600:4:17::246b]) by smtp-3-3000.mail.infomaniak.ch (Postfix) with ESMTPS id 4XvyK94fpXzgVL; Fri, 22 Nov 2024 15:34:05 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=digikod.net; s=20191114; t=1732286045; bh=DxTkVg18bqBuopoJy37l1mwDFC0IuH3r81EWdfJlrGc=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=14EZBYXsghzpHcV4S9I/82WBIuEutrRSJbYqLSze9QEYnwPklS2xKoKvfXcVragVF be+0kf9RAHS+IJGyHlMF1Scd+I8dk97gWys97GznkAmwVStfOGqyLksLtJpdFGrR87 ovaonRdKTRPB/SndwK+NJfXY5cyf2qRZXvj4afX4= Received: from unknown by smtp-3-0000.mail.infomaniak.ch (Postfix) with ESMTPA id 4XvyK90L6Kzm5p; Fri, 22 Nov 2024 15:34:05 +0100 (CET) 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 , Francis Laniel , James Morris , Jann Horn , Jeff Xu , Jorge Lucangeli Obes , Kees Cook , Konstantin Meskhidze , Matt Bobrowski , Mikhail Ivanov , Phil Sutter , 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: [PATCH v3 02/23] lsm: Add audit_log_lsm_data() helper Date: Fri, 22 Nov 2024 15:33:32 +0100 Message-ID: <20241122143353.59367-3-mic@digikod.net> In-Reply-To: <20241122143353.59367-1-mic@digikod.net> References: <20241122143353.59367-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 Extract code from dump_common_audit_data() into the audit_log_lsm_data() helper. This helps reuse common LSM audit data while not abusing AUDIT_AVC records because of the common_lsm_audit() helper. Cc: Casey Schaufler Cc: James Morris Cc: Paul Moore Cc: Serge E. Hallyn Signed-off-by: Micka=C3=ABl Sala=C3=BCn Link: https://lore.kernel.org/r/20241122143353.59367-3-mic@digikod.net --- Changes since v1: - Fix commit message (spotted by Paul). - Constify dump_common_audit_data()'s and audit_log_lsm_data()'s "a" argument. - Fix build without CONFIG_NET: see previous patch. --- include/linux/lsm_audit.h | 8 ++++++++ security/lsm_audit.c | 27 ++++++++++++++++++--------- 2 files changed, 26 insertions(+), 9 deletions(-) diff --git a/include/linux/lsm_audit.h b/include/linux/lsm_audit.h index c2b01380262c..b62769a7c5fa 100644 --- a/include/linux/lsm_audit.h +++ b/include/linux/lsm_audit.h @@ -130,6 +130,9 @@ void common_lsm_audit(struct common_audit_data *a, void (*pre_audit)(struct audit_buffer *, void *), void (*post_audit)(struct audit_buffer *, void *)); =20 +void audit_log_lsm_data(struct audit_buffer *ab, + const struct common_audit_data *a); + #else /* CONFIG_AUDIT */ =20 static inline void common_lsm_audit(struct common_audit_data *a, @@ -138,6 +141,11 @@ static inline void common_lsm_audit(struct common_audi= t_data *a, { } =20 +static inline void audit_log_lsm_data(struct audit_buffer *ab, + const struct common_audit_data *a) +{ +} + #endif /* CONFIG_AUDIT */ =20 #endif diff --git a/security/lsm_audit.c b/security/lsm_audit.c index 849e832719e2..de29ce8ff708 100644 --- a/security/lsm_audit.c +++ b/security/lsm_audit.c @@ -189,16 +189,13 @@ static inline void print_ipv4_addr(struct audit_buffe= r *ab, __be32 addr, } =20 /** - * dump_common_audit_data - helper to dump common audit data + * audit_log_lsm_data - helper to log common LSM audit data * @ab : the audit buffer * @a : common audit data - * */ -static void dump_common_audit_data(struct audit_buffer *ab, - struct common_audit_data *a) +void audit_log_lsm_data(struct audit_buffer *ab, + const struct common_audit_data *a) { - char comm[sizeof(current->comm)]; - /* * To keep stack sizes in check force programmers to notice if they * start making this union too large! See struct lsm_network_audit @@ -206,9 +203,6 @@ static void dump_common_audit_data(struct audit_buffer = *ab, */ BUILD_BUG_ON(sizeof(a->u) > sizeof(void *)*2); =20 - audit_log_format(ab, " pid=3D%d comm=3D", task_tgid_nr(current)); - audit_log_untrustedstring(ab, memcpy(comm, current->comm, sizeof(comm))); - switch (a->type) { case LSM_AUDIT_DATA_NONE: return; @@ -428,6 +422,21 @@ static void dump_common_audit_data(struct audit_buffer= *ab, } /* switch (a->type) */ } =20 +/** + * dump_common_audit_data - helper to dump common audit data + * @ab : the audit buffer + * @a : common audit data + */ +static void dump_common_audit_data(struct audit_buffer *ab, + const struct common_audit_data *a) +{ + char comm[sizeof(current->comm)]; + + audit_log_format(ab, " pid=3D%d comm=3D", task_tgid_nr(current)); + audit_log_untrustedstring(ab, memcpy(comm, current->comm, sizeof(comm))); + audit_log_lsm_data(ab, a); +} + /** * common_lsm_audit - generic LSM auditing function * @a: auxiliary audit data --=20 2.47.0 From nobody Tue Nov 26 01:29:03 2024 Received: from smtp-bc0a.mail.infomaniak.ch (smtp-bc0a.mail.infomaniak.ch [45.157.188.10]) (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 486B613AA4E for ; Fri, 22 Nov 2024 14:40:11 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=45.157.188.10 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1732286413; cv=none; b=jr+/U/bqUDtjrVAtD8wAsF42MwBQDnOwINIXER7XfOP9EKSDjxRdVZawEULZ3v6l/X6Vpe8rSDVLyOPhoFhZwizEMZit2MsL1XQjGYGZO6ccpDxqezCJJ60mpBuwHZKO6H/OFBP5RE/in6J3qRN6DbKiwzoHAr2R+7QxYteC5U4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1732286413; c=relaxed/simple; bh=apRtCBU1pS/XutrV3xbNQz6+67PghbyHR49RJBva0bY=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=C3cpsTjNS9ALrCzkS4gZpbgHgu/VKx3Rs3+PllgJ97n1ZYEellOZLxPQqfFvX2i3c+nPRFrUNPWrGq6y5ia1Sn9/9ugjKb0xnBGyZVlyOkIoxUVLsIBCQUv07ncnSTkA/84T8P9Ju8yfWbtdiVwzYnZB0Ev6aZBhF5FQoV3X8BQ= 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=FM5wsDLX; arc=none smtp.client-ip=45.157.188.10 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="FM5wsDLX" Received: from smtp-3-0000.mail.infomaniak.ch (unknown [IPv6:2001:1600:4:17::246b]) by smtp-3-3000.mail.infomaniak.ch (Postfix) with ESMTPS id 4XvyKB6KwSzcS2; Fri, 22 Nov 2024 15:34:06 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=digikod.net; s=20191114; t=1732286046; bh=qrbMeIvoqfDB7rznGcWt/v6+wjhOfu4JkdrgrSKd1mA=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=FM5wsDLXJNQzCAtjQJCHr5I6wqKX2evC4m3sIIty243+ZqX2JqXDi9QAYLbQzeB++ Ag1pIoqnuQJjGAKuKrZlEve4b0T2tbCoZQVS4Y+NS3vt/nY2vN1Pj2BmBimUgYPiKJ gzcraYf8N/4QYkN0OE/75qjJTzWTVUCtpFu7E7Jw= Received: from unknown by smtp-3-0000.mail.infomaniak.ch (Postfix) with ESMTPA id 4XvyKB1N26zlKG; Fri, 22 Nov 2024 15:34:06 +0100 (CET) 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 , Francis Laniel , James Morris , Jann Horn , Jeff Xu , Jorge Lucangeli Obes , Kees Cook , Konstantin Meskhidze , Matt Bobrowski , Mikhail Ivanov , Phil Sutter , 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: [PATCH v3 03/23] landlock: Factor out check_access_path() Date: Fri, 22 Nov 2024 15:33:33 +0100 Message-ID: <20241122143353.59367-4-mic@digikod.net> In-Reply-To: <20241122143353.59367-1-mic@digikod.net> References: <20241122143353.59367-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 Merge check_access_path() into current_check_access_path() and make hook_path_mknod() use it. Cc: G=C3=BCnther Noack Signed-off-by: Micka=C3=ABl Sala=C3=BCn Link: https://lore.kernel.org/r/20241122143353.59367-4-mic@digikod.net --- Changes since v1: - Rebased on the TCP patch series. - Remove inlining removal which was merged. --- security/landlock/fs.c | 32 +++++++++++--------------------- 1 file changed, 11 insertions(+), 21 deletions(-) diff --git a/security/landlock/fs.c b/security/landlock/fs.c index e31b97a9f175..d911c924843f 100644 --- a/security/landlock/fs.c +++ b/security/landlock/fs.c @@ -908,28 +908,22 @@ static bool is_access_to_paths_allowed( return allowed_parent1 && allowed_parent2; } =20 -static int check_access_path(const struct landlock_ruleset *const domain, - const struct path *const path, - access_mask_t access_request) -{ - layer_mask_t layer_masks[LANDLOCK_NUM_ACCESS_FS] =3D {}; - - access_request =3D landlock_init_layer_masks( - domain, access_request, &layer_masks, LANDLOCK_KEY_INODE); - if (is_access_to_paths_allowed(domain, path, access_request, - &layer_masks, NULL, 0, NULL, NULL)) - return 0; - return -EACCES; -} - static int current_check_access_path(const struct path *const path, - const access_mask_t access_request) + access_mask_t access_request) { const struct landlock_ruleset *const dom =3D get_current_fs_domain(); + layer_mask_t layer_masks[LANDLOCK_NUM_ACCESS_FS] =3D {}; =20 if (!dom) return 0; - return check_access_path(dom, path, access_request); + + access_request =3D landlock_init_layer_masks( + dom, access_request, &layer_masks, LANDLOCK_KEY_INODE); + if (is_access_to_paths_allowed(dom, path, access_request, &layer_masks, + NULL, 0, NULL, NULL)) + return 0; + + return -EACCES; } =20 static access_mask_t get_mode_access(const umode_t mode) @@ -1414,11 +1408,7 @@ static int hook_path_mknod(const struct path *const = dir, struct dentry *const dentry, const umode_t mode, const unsigned int dev) { - const struct landlock_ruleset *const dom =3D get_current_fs_domain(); - - if (!dom) - return 0; - return check_access_path(dom, dir, get_mode_access(mode)); + return current_check_access_path(dir, get_mode_access(mode)); } =20 static int hook_path_symlink(const struct path *const dir, --=20 2.47.0 From nobody Tue Nov 26 01:29:03 2024 Received: from smtp-8fac.mail.infomaniak.ch (smtp-8fac.mail.infomaniak.ch [83.166.143.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 8AF1013A3E4 for ; Fri, 22 Nov 2024 14:34:15 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=83.166.143.172 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1732286058; cv=none; b=cILqz0Q5asz82jNslF55zs5NAWOdDnIPna0+JEjYPtNCd+ePYMpBlnSF2a3SdojP6+w3PMtmwI8ULk3AjdGBH2/0d+RjeXf0oDMhErRgNHqp+exJApFJDnP86anFvVltERvFdAVevX8NQwZuVR9hCrZYQ6q/Mtc+lDT7Uan8m5I= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1732286058; c=relaxed/simple; bh=KicGYNpQ2Ph+X10Z2pQdEyQOqoIsprgjSKUoGBa8X1M=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=D+iaq8DPd3B8g4bIEqVgWpKcwo9OUZ/fqQpfj18pq4Nrb3oV89jResW47Xm2kkQdFX4vUHv5XMzFHB21uMzG8CR7lJj7Zk4eIttt3Q7VJCefHn2EKssRHfxBv0GXFFCTCzYIFvE6KEj2m6Vgb8aWbATd8vxNK+UarHxJZBMUjTo= 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=xAJspEEc; arc=none smtp.client-ip=83.166.143.172 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="xAJspEEc" Received: from smtp-3-0001.mail.infomaniak.ch (unknown [IPv6:2001:1600:4:17::246c]) by smtp-3-3000.mail.infomaniak.ch (Postfix) with ESMTPS id 4XvyKD2Pv0zmBx; Fri, 22 Nov 2024 15:34:08 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=digikod.net; s=20191114; t=1732286048; bh=HQ5OfD43PGtkd87eOZdcZ6YZz/aEOREUrjvdRah7d7I=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=xAJspEEclNXHLPi0Sn3QOqfDjWlbhoOzL4py4YFLq9gY8CH2uKtI+R82X6TQCfVIk u68J3YiY7JuaEK+anesMw1faRlJ48vrpXJ49+Eq6T7TZkVXLT1SJ13PXxcJckLY0qO /CC3/oBfTdl36NYhJ3Mm+7RMgT8Kra6cmObbtYc0= Received: from unknown by smtp-3-0001.mail.infomaniak.ch (Postfix) with ESMTPA id 4XvyKC5P01zH4M; Fri, 22 Nov 2024 15:34:07 +0100 (CET) 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 , Francis Laniel , James Morris , Jann Horn , Jeff Xu , Jorge Lucangeli Obes , Kees Cook , Konstantin Meskhidze , Matt Bobrowski , Mikhail Ivanov , Phil Sutter , 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: [PATCH v3 04/23] landlock: Add unique ID generator Date: Fri, 22 Nov 2024 15:33:34 +0100 Message-ID: <20241122143353.59367-5-mic@digikod.net> In-Reply-To: <20241122143353.59367-1-mic@digikod.net> References: <20241122143353.59367-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 Landlock IDs can be generated to uniquely identify Landlock objects. For now, only Landlock domains get an ID at creation time. These IDs map to immutable domain hierarchies. Landlock IDs have important properties: - They are unique during the lifetime of the running system thanks to the 64-bit values: at worse, 2^60 - 2*2^32 useful IDs. - They are always greater than 2^32 and must then be stored in 64-bit integer types. - The initial ID (at boot time) is randomly picked between 2^32 and 2^33, which limits collisions in logs between different boots. - IDs are sequential, which enables users to order them. - IDs may not be consecutive but increase with a random 2^4 step, which limits side channels. Such IDs can be exposed to unprivileged processes, even if it is not the case with this audit patch series. The domain IDs will be useful for user space to identify sandboxes and get their properties. These Landlock IDs are more robust that other absolute kernel IDs such as pipe's inodes which rely on a shared global counter. For checkpoint/restore features (i.e. CRIU), we could easily implement a privileged interface (e.g. sysfs) to set the next ID counter. IDR/IDA are not used because we only need a bijection from Landlock objects to Landlock IDs, and we must not recycle IDs. This enables us to identify all Landlock objects during the lifetime of the system (e.g. in logs), but not to access an object from an ID nor know if an ID is assigned. Using a counter is simpler, it scales (i.e. avoids growing memory footprint), and it does not require locking. We'll use proper file descriptors (with IDs used as inode numbers) to access Landlock objects. Cc: G=C3=BCnther Noack Cc: Paul Moore Signed-off-by: Micka=C3=ABl Sala=C3=BCn Link: https://lore.kernel.org/r/20241122143353.59367-5-mic@digikod.net --- Changes since v2: - Extend commit message. - Rename global_counter to next_id. - Fix KUnit's test __init types, spotted by kernel test robot. Changes since v1: - New patch. --- security/landlock/.kunitconfig | 2 + security/landlock/Makefile | 2 + security/landlock/id.c | 242 +++++++++++++++++++ security/landlock/id.h | 25 ++ security/landlock/setup.c | 2 + tools/testing/kunit/configs/all_tests.config | 2 + 6 files changed, 275 insertions(+) create mode 100644 security/landlock/id.c create mode 100644 security/landlock/id.h diff --git a/security/landlock/.kunitconfig b/security/landlock/.kunitconfig index 03e119466604..f9423f01ac5b 100644 --- a/security/landlock/.kunitconfig +++ b/security/landlock/.kunitconfig @@ -1,4 +1,6 @@ +CONFIG_AUDIT=3Dy CONFIG_KUNIT=3Dy +CONFIG_NET=3Dy CONFIG_SECURITY=3Dy CONFIG_SECURITY_LANDLOCK=3Dy CONFIG_SECURITY_LANDLOCK_KUNIT_TEST=3Dy diff --git a/security/landlock/Makefile b/security/landlock/Makefile index b4538b7cf7d2..e1777abbc413 100644 --- a/security/landlock/Makefile +++ b/security/landlock/Makefile @@ -4,3 +4,5 @@ landlock-y :=3D setup.o syscalls.o object.o ruleset.o \ cred.o task.o fs.o =20 landlock-$(CONFIG_INET) +=3D net.o + +landlock-$(CONFIG_AUDIT) +=3D id.o diff --git a/security/landlock/id.c b/security/landlock/id.c new file mode 100644 index 000000000000..0ae5fee28b67 --- /dev/null +++ b/security/landlock/id.c @@ -0,0 +1,242 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* + * Landlock LSM - Unique identification number generator + * + * Copyright =C2=A9 2024 Microsoft Corporation + */ + +#include +#include +#include +#include + +#include "common.h" +#include "id.h" + +#define COUNTER_PRE_INIT 0 + +static atomic64_t next_id =3D ATOMIC64_INIT(COUNTER_PRE_INIT); + +static void __init init_id(atomic64_t *const counter, const u32 random_32b= its) +{ + u64 init; + + /* + * Ensures sure 64-bit values are always used by user space (or may + * fail with -EOVERFLOW), and makes this testable. + */ + init =3D 1ULL << 32; + + /* + * Makes a large (2^32) boot-time value to limit ID collision in logs + * from different boots, and to limit info leak about the number of + * initially (relative to the reader) created elements (e.g. domains). + */ + init +=3D random_32bits; + + /* Sets first or ignores. This will be the first ID. */ + atomic64_cmpxchg(counter, COUNTER_PRE_INIT, init); +} + +#ifdef CONFIG_SECURITY_LANDLOCK_KUNIT_TEST + +static void __init test_init_min(struct kunit *const test) +{ + atomic64_t counter =3D ATOMIC64_INIT(COUNTER_PRE_INIT); + + init_id(&counter, 0); + KUNIT_EXPECT_EQ(test, atomic64_read(&counter), 1ULL + U32_MAX); +} + +static void __init test_init_max(struct kunit *const test) +{ + atomic64_t counter =3D ATOMIC64_INIT(COUNTER_PRE_INIT); + + init_id(&counter, ~0); + KUNIT_EXPECT_EQ(test, atomic64_read(&counter), 1 + (2ULL * U32_MAX)); +} + +static void __init test_init_once(struct kunit *const test) +{ + const u64 first_init =3D 1ULL + U32_MAX; + atomic64_t counter =3D ATOMIC64_INIT(COUNTER_PRE_INIT); + + init_id(&counter, 0); + KUNIT_EXPECT_EQ(test, atomic64_read(&counter), first_init); + + init_id(&counter, ~0); + KUNIT_EXPECT_EQ(test, atomic64_read(&counter), first_init); +} + +#endif /* CONFIG_SECURITY_LANDLOCK_KUNIT_TEST */ + +void __init landlock_init_id(void) +{ + return init_id(&next_id, get_random_u32()); +} + +/* + * It's not worth it to try to hide the monotonic counter because it can s= till + * be inferred (with N counter ranges), and if we are allowed to read the = inode + * number we should also be allowed to read the time creation anyway, and = it + * can be handy to store and sort domain IDs for user space. + * + * Returns the value of next_id and increment it to let some space for the= next + * one. + */ +static u64 get_id(size_t number_of_ids, atomic64_t *const counter, + u8 random_4bits) +{ + u64 id, step; + + /* + * We should return at least 1 ID, and we may need a set of consecutive + * ones (e.g. to generate a set of inodes). + */ + if (WARN_ON_ONCE(number_of_ids <=3D 0)) + number_of_ids =3D 1; + + /* + * Blurs the next ID guess with 1/16 ratio. We get 2^(64 - 4) - + * (2 * 2^32), so a bit less than 2^60 available IDs, which should be + * much more than enough considering the number of CPU cycles required + * to get a new ID (e.g. a full landlock_restrict_self() call), and the + * cost of draining all available IDs during the system's uptime. + */ + random_4bits =3D random_4bits % (1 << 4); + step =3D number_of_ids + random_4bits; + + /* It is safe to cast a signed atomic to an unsigned value. */ + id =3D atomic64_fetch_add(step, counter); + + /* Warns if landlock_init_id() was not called. */ + WARN_ON_ONCE(id =3D=3D COUNTER_PRE_INIT); + return id; +} + +#ifdef CONFIG_SECURITY_LANDLOCK_KUNIT_TEST + +static void test_range1_rand0(struct kunit *const test) +{ + atomic64_t counter; + u64 init; + + init =3D get_random_u32(); + atomic64_set(&counter, init); + KUNIT_EXPECT_EQ(test, get_id(1, &counter, 0), init); + KUNIT_EXPECT_EQ(test, + get_id(get_random_u8(), &counter, get_random_u8()), + init + 1); +} + +static void test_range1_rand1(struct kunit *const test) +{ + atomic64_t counter; + u64 init; + + init =3D get_random_u32(); + atomic64_set(&counter, init); + KUNIT_EXPECT_EQ(test, get_id(1, &counter, 1), init); + KUNIT_EXPECT_EQ(test, + get_id(get_random_u8(), &counter, get_random_u8()), + init + 2); +} + +static void test_range1_rand16(struct kunit *const test) +{ + atomic64_t counter; + u64 init; + + init =3D get_random_u32(); + atomic64_set(&counter, init); + KUNIT_EXPECT_EQ(test, get_id(1, &counter, 16), init); + KUNIT_EXPECT_EQ(test, + get_id(get_random_u8(), &counter, get_random_u8()), + init + 1); +} + +static void test_range2_rand0(struct kunit *const test) +{ + atomic64_t counter; + u64 init; + + init =3D get_random_u32(); + atomic64_set(&counter, init); + KUNIT_EXPECT_EQ(test, get_id(2, &counter, 0), init); + KUNIT_EXPECT_EQ(test, + get_id(get_random_u8(), &counter, get_random_u8()), + init + 2); +} + +static void test_range2_rand1(struct kunit *const test) +{ + atomic64_t counter; + u64 init; + + init =3D get_random_u32(); + atomic64_set(&counter, init); + KUNIT_EXPECT_EQ(test, get_id(2, &counter, 1), init); + KUNIT_EXPECT_EQ(test, + get_id(get_random_u8(), &counter, get_random_u8()), + init + 3); +} + +static void test_range2_rand2(struct kunit *const test) +{ + atomic64_t counter; + u64 init; + + init =3D get_random_u32(); + atomic64_set(&counter, init); + KUNIT_EXPECT_EQ(test, get_id(2, &counter, 2), init); + KUNIT_EXPECT_EQ(test, + get_id(get_random_u8(), &counter, get_random_u8()), + init + 4); +} + +static void test_range2_rand16(struct kunit *const test) +{ + atomic64_t counter; + u64 init; + + init =3D get_random_u32(); + atomic64_set(&counter, init); + KUNIT_EXPECT_EQ(test, get_id(2, &counter, 16), init); + KUNIT_EXPECT_EQ(test, + get_id(get_random_u8(), &counter, get_random_u8()), + init + 2); +} + +#endif /* CONFIG_SECURITY_LANDLOCK_KUNIT_TEST */ + +u64 landlock_get_id(size_t number_of_ids) +{ + return get_id(number_of_ids, &next_id, get_random_u8()); +} + +#ifdef CONFIG_SECURITY_LANDLOCK_KUNIT_TEST + +static struct kunit_case __refdata test_cases[] =3D { + /* clang-format off */ + KUNIT_CASE(test_init_min), + KUNIT_CASE(test_init_max), + KUNIT_CASE(test_init_once), + KUNIT_CASE(test_range1_rand0), + KUNIT_CASE(test_range1_rand1), + KUNIT_CASE(test_range1_rand16), + KUNIT_CASE(test_range2_rand0), + KUNIT_CASE(test_range2_rand1), + KUNIT_CASE(test_range2_rand2), + KUNIT_CASE(test_range2_rand16), + {} + /* clang-format on */ +}; + +static struct kunit_suite test_suite =3D { + .name =3D "landlock_id", + .test_cases =3D test_cases, +}; + +kunit_test_init_section_suite(test_suite); + +#endif /* CONFIG_SECURITY_LANDLOCK_KUNIT_TEST */ diff --git a/security/landlock/id.h b/security/landlock/id.h new file mode 100644 index 000000000000..689ba7607472 --- /dev/null +++ b/security/landlock/id.h @@ -0,0 +1,25 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +/* + * Landlock LSM - Unique identification number generator + * + * Copyright =C2=A9 2024 Microsoft Corporation + */ + +#ifndef _SECURITY_LANDLOCK_ID_H +#define _SECURITY_LANDLOCK_ID_H + +#ifdef CONFIG_AUDIT + +void __init landlock_init_id(void); + +u64 landlock_get_id(size_t number_of_ids); + +#else /* CONFIG_AUDIT */ + +static inline void __init landlock_init_id(void) +{ +} + +#endif /* CONFIG_AUDIT */ + +#endif /* _SECURITY_LANDLOCK_ID_H */ diff --git a/security/landlock/setup.c b/security/landlock/setup.c index 28519a45b11f..d297083efcb1 100644 --- a/security/landlock/setup.c +++ b/security/landlock/setup.c @@ -13,6 +13,7 @@ #include "common.h" #include "cred.h" #include "fs.h" +#include "id.h" #include "net.h" #include "setup.h" #include "task.h" @@ -33,6 +34,7 @@ const struct lsm_id landlock_lsmid =3D { =20 static int __init landlock_init(void) { + landlock_init_id(); landlock_add_cred_hooks(); landlock_add_task_hooks(); landlock_add_fs_hooks(); diff --git a/tools/testing/kunit/configs/all_tests.config b/tools/testing/k= unit/configs/all_tests.config index b3b00269a52a..ea1f824ae70f 100644 --- a/tools/testing/kunit/configs/all_tests.config +++ b/tools/testing/kunit/configs/all_tests.config @@ -44,6 +44,8 @@ CONFIG_DAMON_DBGFS_DEPRECATED=3Dy =20 CONFIG_REGMAP_BUILD=3Dy =20 +CONFIG_AUDIT=3Dy + CONFIG_SECURITY=3Dy CONFIG_SECURITY_APPARMOR=3Dy CONFIG_SECURITY_LANDLOCK=3Dy --=20 2.47.0 From nobody Tue Nov 26 01:29:03 2024 Received: from smtp-42ab.mail.infomaniak.ch (smtp-42ab.mail.infomaniak.ch [84.16.66.171]) (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 B688513B5A9 for ; Fri, 22 Nov 2024 14:34:16 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=84.16.66.171 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1732286059; cv=none; b=j+O0+UtbNHuJPk/wWYJ1a9mGu0ab7eow1gJETvsrxSvxEKR+6RjgJS42ed+2NZWHVjMj8zhwfa7Nt2nw6PPQiKz6uaUoAFCC6djTPSp3p5UnK0SAJ+8XdCT+k9hLU97lOc1/3EH+gheJmHEZXWP4kB21MmvR5vm+nmT/MnfPtvE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1732286059; c=relaxed/simple; bh=NuT0tcq/43zDLvDEY8JWunzl/WOg4jV/JJW5DYCdEdc=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=X2OMVFixHxI05N+Dek1QAKlVJdavd8+vJlllGYudb1zwk/n/aCLAR4MszRwjH4Gvn1NdCFWiPNHCEVbXcz9zl2A7V1r09Yne7IxV4CWcx6yHSLpQu+NsUwWNgGjJ7rHhvHBeZ39QPqgdxsWLnOcf+EZd8OU8BPg9fpy7p9ziFaY= 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=YyjWljcv; arc=none smtp.client-ip=84.16.66.171 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="YyjWljcv" 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 4XvyKF5G5jzcBZ; Fri, 22 Nov 2024 15:34:09 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=digikod.net; s=20191114; t=1732286049; bh=8Ei6RdWiyTIEr0sm3nOOSbLrW31UhPKYLCfZTJ/6SdU=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=YyjWljcvGeJieGlqZKPvWq1H43r2bNPIPvh5Z1hXmzTAeRzubP75L/J6jMCWoGF19 15BGkb/JRQxrEeXtxpEYZDgbG2QrmUZ3NZ2tj6vqu/vVdQHrCjQTgPDpzW2LGU087A oeOhbGIl0AI1r5gLoZo1esDwOuhrtiHKB8WnANbU= Received: from unknown by smtp-4-0001.mail.infomaniak.ch (Postfix) with ESMTPA id 4XvyKF09LzzlDf; Fri, 22 Nov 2024 15:34:09 +0100 (CET) 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 , Francis Laniel , James Morris , Jann Horn , Jeff Xu , Jorge Lucangeli Obes , Kees Cook , Konstantin Meskhidze , Matt Bobrowski , Mikhail Ivanov , Phil Sutter , 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: [PATCH v3 05/23] landlock: Move access types Date: Fri, 22 Nov 2024 15:33:35 +0100 Message-ID: <20241122143353.59367-6-mic@digikod.net> In-Reply-To: <20241122143353.59367-1-mic@digikod.net> References: <20241122143353.59367-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 Move LANDLOCK_ACCESS_FS_INITIALLY_DENIED, access_mask_t, struct access_mask, and struct access_masks_all to a dedicated access.h file. Rename LANDLOCK_ACCESS_FS_INITIALLY_DENIED to _LANDLOCK_ACCESS_FS_INITIALLY_DENIED to make it clear that it's not part of UAPI. Add some newlines when appropriate. This file will be extended with following commits, and it will help to avoid dependency loops. Cc: G=C3=BCnther Noack Signed-off-by: Micka=C3=ABl Sala=C3=BCn Link: https://lore.kernel.org/r/20241122143353.59367-6-mic@digikod.net --- Changes since v2: - Rebased on the (now merged) masks improvement patches. - Move ACCESS_FS_OPTIONAL to a following patch introducing deny_masks_t, spotted by Francis Laniel. - Move and rename LANDLOCK_ACCESS_FS_INITIALLY_DENIED to _LANDLOCK_ACCESS_FS_INITIALLY_DENIED. Changes since v1: - New patch --- security/landlock/access.h | 62 +++++++++++++++++++++++++++++++++++++ security/landlock/fs.c | 3 +- security/landlock/fs.h | 1 + security/landlock/ruleset.c | 1 + security/landlock/ruleset.h | 47 ++-------------------------- 5 files changed, 68 insertions(+), 46 deletions(-) create mode 100644 security/landlock/access.h diff --git a/security/landlock/access.h b/security/landlock/access.h new file mode 100644 index 000000000000..a64694287d2c --- /dev/null +++ b/security/landlock/access.h @@ -0,0 +1,62 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +/* + * Landlock LSM - Access types and helpers + * + * Copyright =C2=A9 2016-2020 Micka=C3=ABl Sala=C3=BCn + * Copyright =C2=A9 2018-2020 ANSSI + * Copyright =C2=A9 2024 Microsoft Corporation + */ + +#ifndef _SECURITY_LANDLOCK_ACCESS_H +#define _SECURITY_LANDLOCK_ACCESS_H + +#include +#include +#include +#include + +#include "limits.h" + +/* + * All access rights that are denied by default whether they are handled o= r not + * by a ruleset/layer. This must be ORed with all ruleset->access_masks[] + * entries when we need to get the absolute handled access masks. + */ +/* clang-format off */ +#define _LANDLOCK_ACCESS_FS_INITIALLY_DENIED ( \ + LANDLOCK_ACCESS_FS_REFER) +/* clang-format on */ + +typedef u16 access_mask_t; + +/* Makes sure all filesystem access rights can be stored. */ +static_assert(BITS_PER_TYPE(access_mask_t) >=3D LANDLOCK_NUM_ACCESS_FS); +/* Makes sure all network access rights can be stored. */ +static_assert(BITS_PER_TYPE(access_mask_t) >=3D LANDLOCK_NUM_ACCESS_NET); +/* Makes sure all scoped rights can be stored. */ +static_assert(BITS_PER_TYPE(access_mask_t) >=3D LANDLOCK_NUM_SCOPE); +/* Makes sure for_each_set_bit() and for_each_clear_bit() calls are OK. */ +static_assert(sizeof(unsigned long) >=3D sizeof(access_mask_t)); + +/* Ruleset access masks. */ +struct access_masks { + access_mask_t fs : LANDLOCK_NUM_ACCESS_FS; + access_mask_t net : LANDLOCK_NUM_ACCESS_NET; + access_mask_t scope : LANDLOCK_NUM_SCOPE; +}; + +union access_masks_all { + struct access_masks masks; + u32 all; +}; + +/* Makes sure all fields are covered. */ +static_assert(sizeof(typeof_member(union access_masks_all, masks)) =3D=3D + sizeof(typeof_member(union access_masks_all, all))); + +typedef u16 layer_mask_t; + +/* Makes sure all layers can be checked. */ +static_assert(BITS_PER_TYPE(layer_mask_t) >=3D LANDLOCK_MAX_NUM_LAYERS); + +#endif /* _SECURITY_LANDLOCK_ACCESS_H */ diff --git a/security/landlock/fs.c b/security/landlock/fs.c index d911c924843f..3da5f1945158 100644 --- a/security/landlock/fs.c +++ b/security/landlock/fs.c @@ -36,6 +36,7 @@ #include #include =20 +#include "access.h" #include "common.h" #include "cred.h" #include "fs.h" @@ -393,7 +394,7 @@ get_handled_fs_accesses(const struct landlock_ruleset *= const domain) { /* Handles all initially denied by default access rights. */ return landlock_union_access_masks(domain).fs | - LANDLOCK_ACCESS_FS_INITIALLY_DENIED; + _LANDLOCK_ACCESS_FS_INITIALLY_DENIED; } =20 static const struct access_masks any_fs =3D { diff --git a/security/landlock/fs.h b/security/landlock/fs.h index 1487e1f023a1..d445f411c26a 100644 --- a/security/landlock/fs.h +++ b/security/landlock/fs.h @@ -13,6 +13,7 @@ #include #include =20 +#include "access.h" #include "ruleset.h" #include "setup.h" =20 diff --git a/security/landlock/ruleset.c b/security/landlock/ruleset.c index a93bdbf52fff..cae69f2f01d9 100644 --- a/security/landlock/ruleset.c +++ b/security/landlock/ruleset.c @@ -20,6 +20,7 @@ #include #include =20 +#include "access.h" #include "limits.h" #include "object.h" #include "ruleset.h" diff --git a/security/landlock/ruleset.h b/security/landlock/ruleset.h index 631e24d4ffe9..2f29b9f40392 100644 --- a/security/landlock/ruleset.h +++ b/security/landlock/ruleset.h @@ -9,58 +9,15 @@ #ifndef _SECURITY_LANDLOCK_RULESET_H #define _SECURITY_LANDLOCK_RULESET_H =20 -#include -#include -#include #include #include #include #include -#include =20 +#include "access.h" #include "limits.h" #include "object.h" =20 -/* - * All access rights that are denied by default whether they are handled o= r not - * by a ruleset/layer. This must be ORed with all ruleset->access_masks[] - * entries when we need to get the absolute handled access masks. - */ -/* clang-format off */ -#define LANDLOCK_ACCESS_FS_INITIALLY_DENIED ( \ - LANDLOCK_ACCESS_FS_REFER) -/* clang-format on */ - -typedef u16 access_mask_t; -/* Makes sure all filesystem access rights can be stored. */ -static_assert(BITS_PER_TYPE(access_mask_t) >=3D LANDLOCK_NUM_ACCESS_FS); -/* Makes sure all network access rights can be stored. */ -static_assert(BITS_PER_TYPE(access_mask_t) >=3D LANDLOCK_NUM_ACCESS_NET); -/* Makes sure all scoped rights can be stored. */ -static_assert(BITS_PER_TYPE(access_mask_t) >=3D LANDLOCK_NUM_SCOPE); -/* Makes sure for_each_set_bit() and for_each_clear_bit() calls are OK. */ -static_assert(sizeof(unsigned long) >=3D sizeof(access_mask_t)); - -/* Ruleset access masks. */ -struct access_masks { - access_mask_t fs : LANDLOCK_NUM_ACCESS_FS; - access_mask_t net : LANDLOCK_NUM_ACCESS_NET; - access_mask_t scope : LANDLOCK_NUM_SCOPE; -}; - -union access_masks_all { - struct access_masks masks; - u32 all; -}; - -/* Makes sure all fields are covered. */ -static_assert(sizeof(typeof_member(union access_masks_all, masks)) =3D=3D - sizeof(typeof_member(union access_masks_all, all))); - -typedef u16 layer_mask_t; -/* Makes sure all layers can be checked. */ -static_assert(BITS_PER_TYPE(layer_mask_t) >=3D LANDLOCK_MAX_NUM_LAYERS); - /** * struct landlock_layer - Access rights for a given layer */ @@ -366,7 +323,7 @@ landlock_get_fs_access_mask(const struct landlock_rules= et *const ruleset, { /* Handles all initially denied by default access rights. */ return ruleset->access_masks[layer_level].fs | - LANDLOCK_ACCESS_FS_INITIALLY_DENIED; + _LANDLOCK_ACCESS_FS_INITIALLY_DENIED; } =20 static inline access_mask_t --=20 2.47.0 From nobody Tue Nov 26 01:29:03 2024 Received: from smtp-42aa.mail.infomaniak.ch (smtp-42aa.mail.infomaniak.ch [84.16.66.170]) (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 AEDB81D9598 for ; Fri, 22 Nov 2024 14:34:18 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=84.16.66.170 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1732286062; cv=none; b=Lg4sqYVztZNaBcEjgJNAI2LyPI7LUwbIYMBRXHPWIN+svx6EL8mgZX8IJfc44LMxj31+VTWl1mbLqv7k3SLsjE+bNShep1/KH1HbNG0R1dnzBtDWQ8qO3mY42gLdEpL8EZFPwYqpOB7HSMDVD6FJNGo26JFSbQ8Egynb9Wqcwkc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1732286062; c=relaxed/simple; bh=9Fxkj6Lw7DP1eAfNHnWrp7Twfjs6P7rVD5OBZe4cpq8=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=m2OlwT9QqJ5hGiRmrR3+o0L7SN2zpY/c3cHzrLA5Xql5m9kSNdgYsovW5qaEK9JrEv4YD5ogYlTqnSsrU171hZfK+y2eh2yXVkRNyDVDKmwBryUBh4wnuyHqBLFr5+QqlrFZFmSXxWmHvZGpDbbnlc1M2/y8PCh6o4hWKRBVYd0= 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=q6BM95T0; arc=none smtp.client-ip=84.16.66.170 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="q6BM95T0" Received: from smtp-3-0000.mail.infomaniak.ch (smtp-3-0000.mail.infomaniak.ch [10.4.36.107]) by smtp-3-3000.mail.infomaniak.ch (Postfix) with ESMTPS id 4XvyKG6yVGzlKF; Fri, 22 Nov 2024 15:34:10 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=digikod.net; s=20191114; t=1732286050; bh=w3WVR4dmJCr/E++3tly2leydnVO6iM/bONHgj5qQ1yI=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=q6BM95T0+P8uMIKRgmgG7gxiJuT4hmcDLlWAlHYvGEL7lgBaMIdIKUH8wkHbEJdK5 BlroYM60s3HtzWnhD59eIRDiFFftfL//m0J8IlkjXv2wBX7N2fkQUARVW3O9aXMPf9 RPbomBAvJwrMnUAnIG7swAf5t/VavRlx0Iwzqx9o= Received: from unknown by smtp-3-0000.mail.infomaniak.ch (Postfix) with ESMTPA id 4XvyKG274Lzm5q; Fri, 22 Nov 2024 15:34:10 +0100 (CET) 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 , Francis Laniel , James Morris , Jann Horn , Jeff Xu , Jorge Lucangeli Obes , Kees Cook , Konstantin Meskhidze , Matt Bobrowski , Mikhail Ivanov , Phil Sutter , 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: [PATCH v3 06/23] landlock: Simplify initially denied access rights Date: Fri, 22 Nov 2024 15:33:36 +0100 Message-ID: <20241122143353.59367-7-mic@digikod.net> In-Reply-To: <20241122143353.59367-1-mic@digikod.net> References: <20241122143353.59367-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 Upgrade domain's handled access masks when creating a domain from a ruleset, instead of converting them at runtime. This is more consistent and helps with audit support. Cc: G=C3=BCnther Noack Signed-off-by: Micka=C3=ABl Sala=C3=BCn Link: https://lore.kernel.org/r/20241122143353.59367-7-mic@digikod.net --- Changes since v2: - New patch. --- security/landlock/access.h | 17 ++++++++++++++++- security/landlock/fs.c | 10 +--------- security/landlock/ruleset.c | 3 ++- 3 files changed, 19 insertions(+), 11 deletions(-) diff --git a/security/landlock/access.h b/security/landlock/access.h index a64694287d2c..1e302775cc23 100644 --- a/security/landlock/access.h +++ b/security/landlock/access.h @@ -20,7 +20,8 @@ /* * All access rights that are denied by default whether they are handled o= r not * by a ruleset/layer. This must be ORed with all ruleset->access_masks[] - * entries when we need to get the absolute handled access masks. + * entries when we need to get the absolute handled access masks, see + * landlock_upgrade_handled_access_masks(). */ /* clang-format off */ #define _LANDLOCK_ACCESS_FS_INITIALLY_DENIED ( \ @@ -59,4 +60,18 @@ typedef u16 layer_mask_t; /* Makes sure all layers can be checked. */ static_assert(BITS_PER_TYPE(layer_mask_t) >=3D LANDLOCK_MAX_NUM_LAYERS); =20 +/* Upgrades with all initially denied by default access rights. */ +static inline struct access_masks +landlock_upgrade_handled_access_masks(struct access_masks access_masks) +{ + /* + * All access rights that are denied by default whether they are + * explicitly handled or not. + */ + if (access_masks.fs) + access_masks.fs |=3D _LANDLOCK_ACCESS_FS_INITIALLY_DENIED; + + return access_masks; +} + #endif /* _SECURITY_LANDLOCK_ACCESS_H */ diff --git a/security/landlock/fs.c b/security/landlock/fs.c index 3da5f1945158..9779170d9199 100644 --- a/security/landlock/fs.c +++ b/security/landlock/fs.c @@ -389,14 +389,6 @@ static bool is_nouser_or_private(const struct dentry *= dentry) unlikely(IS_PRIVATE(d_backing_inode(dentry)))); } =20 -static access_mask_t -get_handled_fs_accesses(const struct landlock_ruleset *const domain) -{ - /* Handles all initially denied by default access rights. */ - return landlock_union_access_masks(domain).fs | - _LANDLOCK_ACCESS_FS_INITIALLY_DENIED; -} - static const struct access_masks any_fs =3D { .fs =3D ~0, }; @@ -788,7 +780,7 @@ static bool is_access_to_paths_allowed( * a superset of the meaningful requested accesses). */ access_masked_parent1 =3D access_masked_parent2 =3D - get_handled_fs_accesses(domain); + landlock_union_access_masks(domain).fs; is_dom_check =3D true; } else { if (WARN_ON_ONCE(dentry_child1 || dentry_child2)) diff --git a/security/landlock/ruleset.c b/security/landlock/ruleset.c index cae69f2f01d9..dbc528f5f3b7 100644 --- a/security/landlock/ruleset.c +++ b/security/landlock/ruleset.c @@ -385,7 +385,8 @@ static int merge_ruleset(struct landlock_ruleset *const= dst, err =3D -EINVAL; goto out_unlock; } - dst->access_masks[dst->num_layers - 1] =3D src->access_masks[0]; + dst->access_masks[dst->num_layers - 1] =3D + landlock_upgrade_handled_access_masks(src->access_masks[0]); =20 /* Merges the @src inode tree. */ err =3D merge_tree(dst, src, LANDLOCK_KEY_INODE); --=20 2.47.0 From nobody Tue Nov 26 01:29:03 2024 Received: from smtp-bc0c.mail.infomaniak.ch (smtp-bc0c.mail.infomaniak.ch [45.157.188.12]) (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 BEAB01442F2 for ; Fri, 22 Nov 2024 14:45:11 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=45.157.188.12 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1732286715; cv=none; b=geiQtHmIZ39ImXm/sB6gIIDRLjTDEULdLFKk6xBZFdAMqpUyQo7BGrcfX/wnu6/uWvGbcnapwmXeDoiTDldy3LY/KN7vgv83pu5TQYlKDJeAK9+1xW8fpIz2gXrG9qMpw5dIUGeynDxufOhI42DYFkE1wTn+QoEftL/eTTSFrv0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1732286715; c=relaxed/simple; bh=mVC7q9Ce6Q1K0aAdZRv8tYbd5tKt0qtqBh8/IziLuH4=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=aGD1o6Y4Vtmg9yaqxxuB63h95JoMFT3P3wSUN2grgjdLfoW9Xg5R65n8BDNwiOUaCctnvCfqvr4/3FVY0Rs+v2aqIuLM2SGF1wAzrR43uid7itMtmnTGkzpn07BclLCYJfV3f3ZlURRMCK3i0MPwTtl85bMu7kBvT6178xzcDk4= 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=jEV3Jnu2; arc=none smtp.client-ip=45.157.188.12 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="jEV3Jnu2" Received: from smtp-3-0000.mail.infomaniak.ch (unknown [IPv6:2001:1600:4:17::246b]) by smtp-3-3000.mail.infomaniak.ch (Postfix) with ESMTPS id 4XvyKJ0yNXzjWt; Fri, 22 Nov 2024 15:34:12 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=digikod.net; s=20191114; t=1732286052; bh=zrUJcPUH/dxLKFnwEajCgdM87MK2R1jazfUbUqjfOnM=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=jEV3Jnu2ZYE+EzZmP/ZKuDuPGESVVRcQyG9PuOSEXpytA/PMO7yd7yoZXbLUckrkE jdKil2DMeqRDVyS9a0+WDRDIJDcvVmDpbFUAXIUbxJpUsEjCBGYinG0fTKzjhg6qqw P0BPzI3gR1x4xg4td11cB9L9NkO5YKJENTgs+nLI= Received: from unknown by smtp-3-0000.mail.infomaniak.ch (Postfix) with ESMTPA id 4XvyKH3YGSzlPp; Fri, 22 Nov 2024 15:34:11 +0100 (CET) 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 , Francis Laniel , James Morris , Jann Horn , Jeff Xu , Jorge Lucangeli Obes , Kees Cook , Konstantin Meskhidze , Matt Bobrowski , Mikhail Ivanov , Phil Sutter , 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: [PATCH v3 07/23] landlock: Move domain hierarchy management Date: Fri, 22 Nov 2024 15:33:37 +0100 Message-ID: <20241122143353.59367-8-mic@digikod.net> In-Reply-To: <20241122143353.59367-1-mic@digikod.net> References: <20241122143353.59367-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 Create a new domain.h file containing the struct landlock_hierarchy definition and helpers. This type will grow with audit support. This also prepares for a new domain type. Cc: G=C3=BCnther Noack Signed-off-by: Micka=C3=ABl Sala=C3=BCn Link: https://lore.kernel.org/r/20241122143353.59367-8-mic@digikod.net --- Changes since v1: - New patch. --- security/landlock/domain.h | 48 +++++++++++++++++++++++++++++++++++++ security/landlock/ruleset.c | 21 +++------------- security/landlock/ruleset.h | 17 +------------ security/landlock/task.c | 1 + 4 files changed, 53 insertions(+), 34 deletions(-) create mode 100644 security/landlock/domain.h diff --git a/security/landlock/domain.h b/security/landlock/domain.h new file mode 100644 index 000000000000..015d61fd81ec --- /dev/null +++ b/security/landlock/domain.h @@ -0,0 +1,48 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +/* + * Landlock LSM - Domain management + * + * Copyright =C2=A9 2016-2020 Micka=C3=ABl Sala=C3=BCn + * Copyright =C2=A9 2018-2020 ANSSI + */ + +#ifndef _SECURITY_LANDLOCK_DOMAIN_H +#define _SECURITY_LANDLOCK_DOMAIN_H + +#include +#include + +/** + * struct landlock_hierarchy - Node in a domain hierarchy + */ +struct landlock_hierarchy { + /** + * @parent: Pointer to the parent node, or NULL if it is a root + * Landlock domain. + */ + struct landlock_hierarchy *parent; + /** + * @usage: Number of potential children domains plus their parent + * domain. + */ + refcount_t usage; +}; + +static inline void +landlock_get_hierarchy(struct landlock_hierarchy *const hierarchy) +{ + if (hierarchy) + refcount_inc(&hierarchy->usage); +} + +static inline void landlock_put_hierarchy(struct landlock_hierarchy *hiera= rchy) +{ + while (hierarchy && refcount_dec_and_test(&hierarchy->usage)) { + const struct landlock_hierarchy *const freeme =3D hierarchy; + + hierarchy =3D hierarchy->parent; + kfree(freeme); + } +} + +#endif /* _SECURITY_LANDLOCK_DOMAIN_H */ diff --git a/security/landlock/ruleset.c b/security/landlock/ruleset.c index dbc528f5f3b7..4b3dfa3e6fcb 100644 --- a/security/landlock/ruleset.c +++ b/security/landlock/ruleset.c @@ -21,6 +21,7 @@ #include =20 #include "access.h" +#include "domain.h" #include "limits.h" #include "object.h" #include "ruleset.h" @@ -305,22 +306,6 @@ int landlock_insert_rule(struct landlock_ruleset *cons= t ruleset, return insert_rule(ruleset, id, &layers, ARRAY_SIZE(layers)); } =20 -static void get_hierarchy(struct landlock_hierarchy *const hierarchy) -{ - if (hierarchy) - refcount_inc(&hierarchy->usage); -} - -static void put_hierarchy(struct landlock_hierarchy *hierarchy) -{ - while (hierarchy && refcount_dec_and_test(&hierarchy->usage)) { - const struct landlock_hierarchy *const freeme =3D hierarchy; - - hierarchy =3D hierarchy->parent; - kfree(freeme); - } -} - static int merge_tree(struct landlock_ruleset *const dst, struct landlock_ruleset *const src, const enum landlock_key_type key_type) @@ -475,7 +460,7 @@ static int inherit_ruleset(struct landlock_ruleset *con= st parent, err =3D -EINVAL; goto out_unlock; } - get_hierarchy(parent->hierarchy); + landlock_get_hierarchy(parent->hierarchy); child->hierarchy->parent =3D parent->hierarchy; =20 out_unlock: @@ -499,7 +484,7 @@ static void free_ruleset(struct landlock_ruleset *const= ruleset) free_rule(freeme, LANDLOCK_KEY_NET_PORT); #endif /* IS_ENABLED(CONFIG_INET) */ =20 - put_hierarchy(ruleset->hierarchy); + landlock_put_hierarchy(ruleset->hierarchy); kfree(ruleset); } =20 diff --git a/security/landlock/ruleset.h b/security/landlock/ruleset.h index 2f29b9f40392..39169b6860e3 100644 --- a/security/landlock/ruleset.h +++ b/security/landlock/ruleset.h @@ -15,6 +15,7 @@ #include =20 #include "access.h" +#include "domain.h" #include "limits.h" #include "object.h" =20 @@ -106,22 +107,6 @@ struct landlock_rule { struct landlock_layer layers[] __counted_by(num_layers); }; =20 -/** - * struct landlock_hierarchy - Node in a ruleset hierarchy - */ -struct landlock_hierarchy { - /** - * @parent: Pointer to the parent node, or NULL if it is a root - * Landlock domain. - */ - struct landlock_hierarchy *parent; - /** - * @usage: Number of potential children domains plus their parent - * domain. - */ - refcount_t usage; -}; - /** * struct landlock_ruleset - Landlock ruleset * diff --git a/security/landlock/task.c b/security/landlock/task.c index dc7dab78392e..98894ad1abc7 100644 --- a/security/landlock/task.c +++ b/security/landlock/task.c @@ -18,6 +18,7 @@ =20 #include "common.h" #include "cred.h" +#include "domain.h" #include "fs.h" #include "ruleset.h" #include "setup.h" --=20 2.47.0 From nobody Tue Nov 26 01:29:03 2024 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 52E1E13B791; Fri, 22 Nov 2024 14:40:11 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=84.16.66.172 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1732286414; cv=none; b=q4m7zFMkULpdChV5IYWIBvJkir1hpdmlfYZhUl7cc08ke2mJ8XAW3F3lwR4TyIo0p8CZcAZ7uYeIfM8wJF2g6jg9moznFLm+KOP7e5hxTMOFb9vFVAHN1nEhOiqFAeYW38jysSE/CLKGaNQ0KpQySE2HL1UQzuaR5OunkmLZiiY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1732286414; c=relaxed/simple; bh=NQLCs4tHDa6+sFnlPgeXPdf8gOFXJLhRqT9IMTeMugs=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=DOiZCoOZcLta3bWAqq1e6DQPm5GU6kWANfn1s8K4qV7nkVthwYHlstzTsrI0X2z9J2LDFii37lwK9nUi3FTNAz9Wcwke5NkH6NbtCBpM5cnct0pQTvHcvgVBzi6v8pS69EGJLKofF3dTMgrPuLk18pR+eAYseETlDjgMulW/EOI= 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=APOXg9cr; arc=none smtp.client-ip=84.16.66.172 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="APOXg9cr" Received: from smtp-3-0001.mail.infomaniak.ch (smtp-3-0001.mail.infomaniak.ch [10.4.36.108]) by smtp-3-3000.mail.infomaniak.ch (Postfix) with ESMTPS id 4XvyKK30ylzl0b; Fri, 22 Nov 2024 15:34:13 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=digikod.net; s=20191114; t=1732286053; bh=G8oh/GUG4yJsFLSux1Fb2WJY5z3CF18h5oN/CFHEKmI=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=APOXg9crmyn5F9BvuKLR+f80pSFNw2n6Wlj674J0emAZ1BDFkMoi097ciog+hNFkk bUJelNCxCpkdvuH6Sd1WE7sBKm1BxkSwbybTG1iv0WxzOcPIXO8G8R1pSWU578GeEC As3rlHMW31b+nxHK4G713rjGEL9hiZZCAM6n88n8= Received: from unknown by smtp-3-0001.mail.infomaniak.ch (Postfix) with ESMTPA id 4XvyKJ4Zz1zGw5; Fri, 22 Nov 2024 15:34:12 +0100 (CET) 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 , Francis Laniel , James Morris , Jann Horn , Jeff Xu , Jorge Lucangeli Obes , Kees Cook , Konstantin Meskhidze , Matt Bobrowski , Mikhail Ivanov , Phil Sutter , 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: [PATCH v3 08/23] landlock: Log ptrace denials Date: Fri, 22 Nov 2024 15:33:38 +0100 Message-ID: <20241122143353.59367-9-mic@digikod.net> In-Reply-To: <20241122143353.59367-1-mic@digikod.net> References: <20241122143353.59367-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 ptrace_access_check and ptrace_traceme hooks. Add a new AUDIT_LANDLOCK_DENY record type dedicated to any Landlock denials. Log the domain ID restricting the action, the domain's blockers that are missing to allow the requested access, and the target task. The blockers are implicit restrictions (e.g. ptrace), or explicit access rights (e.g. filesystem), or explicit scopes (e.g. signal). For the ptrace_access_check case, we log the current/parent domain and the child task. For the ptrace_traceme case, we log the parent domain and the parent task. Indeed, the requester is the current task, but the action would be performed by the parent task. The quick return for non-landlocked tasks is moved from task_ptrace() to each LSM hooks. Audit event sample: type=3DLL_DENY msg=3Daudit(1732186800.349:44): domain=3D195ba459b blocker= s=3Dptrace opid=3D1 ocomm=3D"systemd" type=3DSYSCALL msg=3Daudit(1732186800.349:44): arch=3Dc000003e syscall=3D= 101 success=3Dno [...] pid=3D300 auid=3D0 Cc: G=C3=BCnther Noack Cc: Paul Moore Signed-off-by: Micka=C3=ABl Sala=C3=BCn Link: https://lore.kernel.org/r/20241122143353.59367-9-mic@digikod.net --- Changes since v2: - Log domain IDs as hexadecimal number: this is a more compact notation (i.e. at least one less digit), it improves alignment in logs, and it makes most IDs start with 1 as leading digit (because of the 2^32 minimal value). Do not use the "0x" prefix that would add useless data to logs. - Constify function arguments. Changes since v1: - Move most audit code to this patch. - Rebase on the TCP patch series. - Don't log missing access right: simplify and make it generic for rule types. - Don't log errno and then don't wrap the error with landlock_log_request(), as suggested by Jeff. - Add a WARN_ON_ONCE() check to never dereference null pointers. - Only log when audit is enabled. - Don't log task's PID/TID with log_task() because it would be redundant with the SYSCALL record. - Move the "op" in front and rename "domain" to "denying_domain" to make it more consistent with other entries. - Don't update the request with the domain ID but add an helper to get it from the layer masks (and in a following commit with a struct file). - Revamp get_domain_id_from_layer_masks() into get_level_from_layer_masks(). - For ptrace_traceme, log the parent domain instead of the current one. - Add documentation. - Rename AUDIT_LANDLOCK_DENIAL to AUDIT_LANDLOCK_DENY. - Only log the domain ID and the target task. - Log "blockers", which are either implicit restrictions (e.g. ptrace) or explicit access rights (e.g. filesystem), or scopes (e.g. signal). - Don't log LSM hook names/operations. - Pick an audit event ID folling the IPE ones. - Add KUnit tests. --- include/uapi/linux/audit.h | 3 +- security/landlock/Makefile | 2 +- security/landlock/audit.c | 137 ++++++++++++++++++++++++++++++++++++ security/landlock/audit.h | 52 ++++++++++++++ security/landlock/domain.c | 21 ++++++ security/landlock/domain.h | 17 +++++ security/landlock/ruleset.c | 3 + security/landlock/task.c | 91 ++++++++++++++++++------ 8 files changed, 302 insertions(+), 24 deletions(-) create mode 100644 security/landlock/audit.c create mode 100644 security/landlock/audit.h create mode 100644 security/landlock/domain.c diff --git a/include/uapi/linux/audit.h b/include/uapi/linux/audit.h index 75e21a135483..60c909c396c0 100644 --- a/include/uapi/linux/audit.h +++ b/include/uapi/linux/audit.h @@ -33,7 +33,7 @@ * 1100 - 1199 user space trusted application messages * 1200 - 1299 messages internal to the audit daemon * 1300 - 1399 audit event messages - * 1400 - 1499 SE Linux use + * 1400 - 1499 access control messages * 1500 - 1599 kernel LSPP events * 1600 - 1699 kernel crypto events * 1700 - 1799 kernel anomaly records @@ -146,6 +146,7 @@ #define AUDIT_IPE_ACCESS 1420 /* IPE denial or grant */ #define AUDIT_IPE_CONFIG_CHANGE 1421 /* IPE config change */ #define AUDIT_IPE_POLICY_LOAD 1422 /* IPE policy load */ +#define AUDIT_LANDLOCK_DENY 1423 /* Landlock denial */ =20 #define AUDIT_FIRST_KERN_ANOM_MSG 1700 #define AUDIT_LAST_KERN_ANOM_MSG 1799 diff --git a/security/landlock/Makefile b/security/landlock/Makefile index e1777abbc413..31512ee4b041 100644 --- a/security/landlock/Makefile +++ b/security/landlock/Makefile @@ -5,4 +5,4 @@ landlock-y :=3D setup.o syscalls.o object.o ruleset.o \ =20 landlock-$(CONFIG_INET) +=3D net.o =20 -landlock-$(CONFIG_AUDIT) +=3D id.o +landlock-$(CONFIG_AUDIT) +=3D id.o domain.o audit.o diff --git a/security/landlock/audit.c b/security/landlock/audit.c new file mode 100644 index 000000000000..eab6b3a8a231 --- /dev/null +++ b/security/landlock/audit.c @@ -0,0 +1,137 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* + * Landlock LSM - Audit helpers + * + * Copyright =C2=A9 2023-2024 Microsoft Corporation + */ + +#include +#include +#include + +#include "audit.h" +#include "domain.h" +#include "ruleset.h" + +static const char *get_blocker(const enum landlock_request_type type) +{ + switch (type) { + case LANDLOCK_REQUEST_PTRACE: + return "ptrace"; + } + + WARN_ON_ONCE(1); + return "unknown"; +} + +static void log_blockers(struct audit_buffer *const ab, + const enum landlock_request_type type) +{ + audit_log_format(ab, "%s", get_blocker(type)); +} + +static struct landlock_hierarchy * +get_hierarchy(const struct landlock_ruleset *const domain, const size_t la= yer) +{ + struct landlock_hierarchy *node =3D domain->hierarchy; + ssize_t i; + + if (WARN_ON_ONCE(layer >=3D domain->num_layers)) + return node; + + for (i =3D domain->num_layers - 1; i > layer; i--) { + if (WARN_ON_ONCE(!node->parent)) + break; + + node =3D node->parent; + } + + return node; +} + +#ifdef CONFIG_SECURITY_LANDLOCK_KUNIT_TEST + +static void test_get_hierarchy(struct kunit *const test) +{ + struct landlock_hierarchy dom0_node =3D { + .id =3D 10, + }; + struct landlock_hierarchy dom1_node =3D { + .parent =3D &dom0_node, + .id =3D 20, + }; + struct landlock_hierarchy dom2_node =3D { + .parent =3D &dom1_node, + .id =3D 30, + }; + struct landlock_ruleset dom2 =3D { + .hierarchy =3D &dom2_node, + .num_layers =3D 3, + }; + + KUNIT_EXPECT_EQ(test, 10, get_hierarchy(&dom2, 0)->id); + KUNIT_EXPECT_EQ(test, 20, get_hierarchy(&dom2, 1)->id); + KUNIT_EXPECT_EQ(test, 30, get_hierarchy(&dom2, 2)->id); + KUNIT_EXPECT_EQ(test, 30, get_hierarchy(&dom2, -1)->id); +} + +#endif /* CONFIG_SECURITY_LANDLOCK_KUNIT_TEST */ + +static bool is_valid_request(const struct landlock_request *const request) +{ + if (WARN_ON_ONCE(!request->layer_plus_one)) + return false; + + return true; +} + +/** + * landlock_log_denial - Create audit records related to a denial + * + * @domain: The domain denying an action. + * @request: Detail of the user space request. + */ +void landlock_log_denial(const struct landlock_ruleset *const domain, + const struct landlock_request *const request) +{ + struct audit_buffer *ab; + struct landlock_hierarchy *youngest_denied; + + if (WARN_ON_ONCE(!domain || !domain->hierarchy || !request)) + return; + + if (!is_valid_request(request)) + return; + + if (!audit_enabled) + return; + + ab =3D audit_log_start(audit_context(), GFP_ATOMIC | __GFP_NOWARN, + AUDIT_LANDLOCK_DENY); + if (!ab) + return; + + youngest_denied =3D get_hierarchy(domain, request->layer_plus_one - 1); + audit_log_format(ab, "domain=3D%llx blockers=3D", youngest_denied->id); + log_blockers(ab, request->type); + audit_log_lsm_data(ab, &request->audit); + audit_log_end(ab); +} + +#ifdef CONFIG_SECURITY_LANDLOCK_KUNIT_TEST + +static struct kunit_case test_cases[] =3D { + /* clang-format off */ + KUNIT_CASE(test_get_hierarchy), + {} + /* clang-format on */ +}; + +static struct kunit_suite test_suite =3D { + .name =3D "landlock_audit", + .test_cases =3D test_cases, +}; + +kunit_test_suite(test_suite); + +#endif /* CONFIG_SECURITY_LANDLOCK_KUNIT_TEST */ diff --git a/security/landlock/audit.h b/security/landlock/audit.h new file mode 100644 index 000000000000..f33095afcd2f --- /dev/null +++ b/security/landlock/audit.h @@ -0,0 +1,52 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +/* + * Landlock LSM - Audit helpers + * + * Copyright =C2=A9 2023-2024 Microsoft Corporation + */ + +#ifndef _SECURITY_LANDLOCK_AUDIT_H +#define _SECURITY_LANDLOCK_AUDIT_H + +#include +#include + +#include "ruleset.h" + +enum landlock_request_type { + LANDLOCK_REQUEST_PTRACE =3D 1, +}; + +/* + * We should be careful to only use a variable of this type for + * landlock_log_denial(). This way, the compiler can remove it entirely if + * CONFIG_AUDIT is not set. + */ +struct landlock_request { + /* Mandatory fields. */ + enum landlock_request_type type; + struct common_audit_data audit; + + /** + * layer_plus_one: First layer level that denies the request + 1. The + * extra one is useful to detect uninitialized field. + */ + size_t layer_plus_one; +}; + +#ifdef CONFIG_AUDIT + +void landlock_log_denial(const struct landlock_ruleset *const domain, + const struct landlock_request *const request); + +#else /* CONFIG_AUDIT */ + +static inline void +landlock_log_denial(const struct landlock_ruleset *const domain, + const struct landlock_request *const request) +{ +} + +#endif /* CONFIG_AUDIT */ + +#endif /* _SECURITY_LANDLOCK_AUDIT_H */ diff --git a/security/landlock/domain.c b/security/landlock/domain.c new file mode 100644 index 000000000000..965e4dc21975 --- /dev/null +++ b/security/landlock/domain.c @@ -0,0 +1,21 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* + * Landlock LSM - Domain management + * + * Copyright =C2=A9 2024 Microsoft Corporation + */ + +#include "domain.h" +#include "id.h" + +/** + * landlock_init_current_hierarchy - Partially initialize landlock_hierarc= hy + * + * @hierarchy: The hierarchy to initialize. + * + * @hierarchy->parent and @hierarchy->usage should already be set. + */ +void landlock_init_current_hierarchy(struct landlock_hierarchy *const hier= archy) +{ + hierarchy->id =3D landlock_get_id(1); +} diff --git a/security/landlock/domain.h b/security/landlock/domain.h index 015d61fd81ec..f82d831ca0a7 100644 --- a/security/landlock/domain.h +++ b/security/landlock/domain.h @@ -4,6 +4,7 @@ * * Copyright =C2=A9 2016-2020 Micka=C3=ABl Sala=C3=BCn * Copyright =C2=A9 2018-2020 ANSSI + * Copyright =C2=A9 2024 Microsoft Corporation */ =20 #ifndef _SECURITY_LANDLOCK_DOMAIN_H @@ -26,6 +27,13 @@ struct landlock_hierarchy { * domain. */ refcount_t usage; + +#ifdef CONFIG_AUDIT + /** + * @id: Landlock domain ID, sets once at domain creation time. + */ + u64 id; +#endif /* CONFIG_AUDIT */ }; =20 static inline void @@ -45,4 +53,13 @@ static inline void landlock_put_hierarchy(struct landloc= k_hierarchy *hierarchy) } } =20 +#ifdef CONFIG_AUDIT +void landlock_init_current_hierarchy(struct landlock_hierarchy *const hier= archy); +#else /* CONFIG_AUDIT */ +static inline void +landlock_init_current_hierarchy(struct landlock_hierarchy *const hierarchy) +{ +} +#endif /* CONFIG_AUDIT */ + #endif /* _SECURITY_LANDLOCK_DOMAIN_H */ diff --git a/security/landlock/ruleset.c b/security/landlock/ruleset.c index 4b3dfa3e6fcb..7a88fd9b2473 100644 --- a/security/landlock/ruleset.c +++ b/security/landlock/ruleset.c @@ -21,6 +21,7 @@ #include =20 #include "access.h" +#include "audit.h" #include "domain.h" #include "limits.h" #include "object.h" @@ -503,6 +504,7 @@ static void free_ruleset_work(struct work_struct *const= work) free_ruleset(ruleset); } =20 +/* Only called by hook_cred_free(). */ void landlock_put_ruleset_deferred(struct landlock_ruleset *const ruleset) { if (ruleset && refcount_dec_and_test(&ruleset->usage)) { @@ -562,6 +564,7 @@ landlock_merge_ruleset(struct landlock_ruleset *const p= arent, if (err) goto out_put_dom; =20 + landlock_init_current_hierarchy(new_dom->hierarchy); return new_dom; =20 out_put_dom: diff --git a/security/landlock/task.c b/security/landlock/task.c index 98894ad1abc7..1decd6f114e8 100644 --- a/security/landlock/task.c +++ b/security/landlock/task.c @@ -10,12 +10,14 @@ #include #include #include +#include #include #include #include #include #include =20 +#include "audit.h" #include "common.h" #include "cred.h" #include "domain.h" @@ -38,41 +40,29 @@ static bool domain_scope_le(const struct landlock_rules= et *const parent, { const struct landlock_hierarchy *walker; =20 + /* Quick return for non-landlocked tasks. */ if (!parent) return true; + if (!child) return false; + for (walker =3D child->hierarchy; walker; walker =3D walker->parent) { if (walker =3D=3D parent->hierarchy) /* @parent is in the scoped hierarchy of @child. */ return true; } + /* There is no relationship between @parent and @child. */ return false; } =20 -static bool task_is_scoped(const struct task_struct *const parent, - const struct task_struct *const child) -{ - bool is_scoped; - const struct landlock_ruleset *dom_parent, *dom_child; - - rcu_read_lock(); - dom_parent =3D landlock_get_task_domain(parent); - dom_child =3D landlock_get_task_domain(child); - is_scoped =3D domain_scope_le(dom_parent, dom_child); - rcu_read_unlock(); - return is_scoped; -} - -static int task_ptrace(const struct task_struct *const parent, - const struct task_struct *const child) +static int domain_ptrace(const struct landlock_ruleset *const parent, + const struct landlock_ruleset *const child) { - /* Quick return for non-landlocked tasks. */ - if (!landlocked(parent)) - return 0; - if (task_is_scoped(parent, child)) + if (domain_scope_le(parent, child)) return 0; + return -EPERM; } =20 @@ -92,7 +82,36 @@ static int task_ptrace(const struct task_struct *const p= arent, static int hook_ptrace_access_check(struct task_struct *const child, const unsigned int mode) { - return task_ptrace(current, child); + const struct landlock_ruleset *parent_dom, *child_dom; + struct landlock_request request =3D { + .type =3D LANDLOCK_REQUEST_PTRACE, + .audit =3D { + .type =3D LSM_AUDIT_DATA_TASK, + .u.tsk =3D child, + }, + }; + int err; + + /* Quick return for non-landlocked tasks. */ + parent_dom =3D landlock_get_current_domain(); + if (!parent_dom) + return 0; + + rcu_read_lock(); + child_dom =3D landlock_get_task_domain(child); + err =3D domain_ptrace(parent_dom, child_dom); + rcu_read_unlock(); + + /* + * For the ptrace_access_check case, we log the current/parent domain + * and the child task. + */ + if (err && !(mode & PTRACE_MODE_NOAUDIT)) { + request.layer_plus_one =3D parent_dom->num_layers; + landlock_log_denial(parent_dom, &request); + } + + return err; } =20 /** @@ -109,7 +128,35 @@ static int hook_ptrace_access_check(struct task_struct= *const child, */ static int hook_ptrace_traceme(struct task_struct *const parent) { - return task_ptrace(parent, current); + const struct landlock_ruleset *parent_dom, *child_dom; + struct landlock_request request =3D { + .type =3D LANDLOCK_REQUEST_PTRACE, + .audit =3D { + .type =3D LSM_AUDIT_DATA_TASK, + .u.tsk =3D parent, + }, + }; + int err; + + child_dom =3D landlock_get_current_domain(); + rcu_read_lock(); + parent_dom =3D landlock_get_task_domain(parent); + err =3D domain_ptrace(parent_dom, child_dom); + + /* + * For the ptrace_traceme case, we log the domain which is the cause of + * the denial, which means the parent domain instead of the current + * domain. This may look weird because the ptrace_traceme action is a + * request to be traced, but the semantic is consistent with + * hook_ptrace_access_check(). + */ + if (err) { + request.layer_plus_one =3D parent_dom->num_layers; + landlock_log_denial(parent_dom, &request); + } + + rcu_read_unlock(); + return err; } =20 /** --=20 2.47.0 From nobody Tue Nov 26 01:29:03 2024 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 8CF6D1DEFDC; Fri, 22 Nov 2024 14:34:22 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=84.16.66.172 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1732286064; cv=none; b=nn70/c2bmebfh65pn5Jc367Dsv7NFcLdprbjZLMbEQkqkWzrplLIoHiXO1qIw6zcEVv7isa5ZO/SqS1gneU0igIGlX6N+b6lEmqD8sHEC829foF/p1oJl1SYZjyAhr/RuTgs2z032Aw8OJLdCWNN73qx95tgV47M+jyYCrbsIec= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1732286064; c=relaxed/simple; bh=oA5h+lIhIMGHdMq3GlqkFyWUv2dO2b0FhZB7G/chb0g=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=AoaiFzbb/C7JT6CXCLPGkjROhZ1JhJtj0oddUtwFKEPugi8PbqbD/gfPWmza41Gjpl+QEG7xBNf6/kjCTWIQy0USTx5EswTsuD6tA4KSOjjgrQy29tMaHQo0rcMXg0cw3qFJi+lk/IqgbKzYjoalyiiMfHVsG6ocAq48LOhV8U0= 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=JWzgivDs; arc=none smtp.client-ip=84.16.66.172 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="JWzgivDs" Received: from smtp-3-0000.mail.infomaniak.ch (unknown [IPv6:2001:1600:4:17::246b]) by smtp-3-3000.mail.infomaniak.ch (Postfix) with ESMTPS id 4XvyKM0JPgzjT6; Fri, 22 Nov 2024 15:34:15 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=digikod.net; s=20191114; t=1732286054; bh=1sr0WCuh3QxUACqP8dYROb6HbctHZuOp4t4pm8z1faM=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=JWzgivDsxbicKpp6/jeidUMoxCaM87H0dcpCfAXfXbYXRuB1R6g12hzW26BtQH6Yq vvG9cbTvUbgW0pCJHhPlapUrQtozAIJTx+S0xXNKmePgjfssojBIhhYOyIy/Nhim56 2HAn0wEymjIeBxgVVWfDm4yyY36ijJTY2G0d7xGQ= Received: from unknown by smtp-3-0000.mail.infomaniak.ch (Postfix) with ESMTPA id 4XvyKK5y5jzlKK; Fri, 22 Nov 2024 15:34:13 +0100 (CET) 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 , Francis Laniel , James Morris , Jann Horn , Jeff Xu , Jorge Lucangeli Obes , Kees Cook , Konstantin Meskhidze , Matt Bobrowski , Mikhail Ivanov , Phil Sutter , 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: [PATCH v3 09/23] audit: Add a new audit_get_ctime() helper Date: Fri, 22 Nov 2024 15:33:39 +0100 Message-ID: <20241122143353.59367-10-mic@digikod.net> In-Reply-To: <20241122143353.59367-1-mic@digikod.net> References: <20241122143353.59367-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 It may be useful to synchronize with the audit's timestamp e.g., to identify asynchronous events as being created with a previous audit record (see next commit). auditsc_get_stamp() does more than just getting a timestamp, so add a new helper instead of exposing it and risking side effects. It should be noted that we cannot reliably expose event's serial numbers because there may not be any related event, which would then create holes in the sequence of serial numbers. Cc: Eric Paris Cc: Paul Moore Signed-off-by: Micka=C3=ABl Sala=C3=BCn Link: https://lore.kernel.org/r/20241122143353.59367-10-mic@digikod.net --- Changes since v2: - New patch. --- include/linux/audit.h | 8 ++++++++ kernel/auditsc.c | 21 ++++++++++++++++++--- 2 files changed, 26 insertions(+), 3 deletions(-) diff --git a/include/linux/audit.h b/include/linux/audit.h index 0050ef288ab3..cff07525c6ae 100644 --- a/include/linux/audit.h +++ b/include/linux/audit.h @@ -12,6 +12,7 @@ #include #include #include +#include #include #include #include @@ -408,6 +409,7 @@ extern int __audit_socketcall(int nargs, unsigned long = *args); extern int __audit_sockaddr(int len, void *addr); extern void __audit_fd_pair(int fd1, int fd2); extern void __audit_mq_open(int oflag, umode_t mode, struct mq_attr *attr); +extern struct timespec64 audit_get_ctime(const struct audit_context *ctx); extern void __audit_mq_sendrecv(mqd_t mqdes, size_t msg_len, unsigned int = msg_prio, const struct timespec64 *abs_timeout); extern void __audit_mq_notify(mqd_t mqdes, const struct sigevent *notifica= tion); extern void __audit_mq_getsetattr(mqd_t mqdes, struct mq_attr *mqstat); @@ -653,6 +655,12 @@ static inline int audit_sockaddr(int len, void *addr) } static inline void audit_mq_open(int oflag, umode_t mode, struct mq_attr *= attr) { } +static inline struct timespec64 audit_get_ctime(const struct audit_context= *ctx) +{ + struct timespec64 t =3D {}; + + return t; +} static inline void audit_mq_sendrecv(mqd_t mqdes, size_t msg_len, unsigned int msg_prio, const struct timespec64 *abs_timeout) diff --git a/kernel/auditsc.c b/kernel/auditsc.c index cd57053b4a69..3b7dcb47f5af 100644 --- a/kernel/auditsc.c +++ b/kernel/auditsc.c @@ -2511,6 +2511,22 @@ void __audit_inode_child(struct inode *parent, } EXPORT_SYMBOL_GPL(__audit_inode_child); =20 +/** + * audit_get_ctime - get creation time of audit_context + * + * @ctx: audit_context for the task + * + * Returns an empty timespec64 if ctx is NULL. + */ +struct timespec64 audit_get_ctime(const struct audit_context *ctx) +{ + struct timespec64 t =3D {}; + + if (ctx) + return ctx->ctime; + return t; +} + /** * auditsc_get_stamp - get local copies of audit_context values * @ctx: audit_context for the task @@ -2526,9 +2542,8 @@ int auditsc_get_stamp(struct audit_context *ctx, return 0; if (!ctx->serial) ctx->serial =3D audit_serial(); - t->tv_sec =3D ctx->ctime.tv_sec; - t->tv_nsec =3D ctx->ctime.tv_nsec; - *serial =3D ctx->serial; + *t =3D audit_get_ctime(ctx); + *serial =3D ctx->serial; if (!ctx->prio) { ctx->prio =3D 1; ctx->current_state =3D AUDIT_STATE_RECORD; --=20 2.47.0 From nobody Tue Nov 26 01:29:03 2024 Received: from smtp-8fa9.mail.infomaniak.ch (smtp-8fa9.mail.infomaniak.ch [83.166.143.169]) (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 CFB3D139D19 for ; Fri, 22 Nov 2024 14:41:06 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=83.166.143.169 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1732286469; cv=none; b=nZYU43g2/hz/K4RVC4LNNWpzge7s12uhM/HVeesQKVwCaYqBPdsPVciMHCR3iiKrfGNe/4Dgc/YPw4mbq6dqu+nahNBqETVSGq9TXhUn4LHKVGpbrCn7LAaQvWwLtFUeF0YBs/VWblItOk085qeSlRuotOk2IVabXJxUN3ePj7s= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1732286469; c=relaxed/simple; bh=9/HAE6oN/4DdnJq2+IQRF9EYrlCmzEbE13r49t9ClLA=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=M33WgfELgxK95hgH9Su6/5XY6Bc34m/4vJ6tirUmLBrHIFgfnBvxt6geaOWSgjmb8/aCwBGFmV3F1R1/1KkmUtIdoIoo2E3SDH06sdlj3RtN8rZ73LDN8AN/ZlvdmzqoRDOAa7cB5im6D9EjSJ4V+/rpcPRHhB11OUFNQRRzL7Y= 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=vcI8UqR6; arc=none smtp.client-ip=83.166.143.169 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="vcI8UqR6" Received: from smtp-4-0000.mail.infomaniak.ch (smtp-4-0000.mail.infomaniak.ch [10.7.10.107]) by smtp-4-3000.mail.infomaniak.ch (Postfix) with ESMTPS id 4XvyKN230Kzcpc; Fri, 22 Nov 2024 15:34:16 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=digikod.net; s=20191114; t=1732286056; bh=4hM4h0jTTGz2zSWVoAgOj7GvhIjSm/gA9AToINwA4lw=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=vcI8UqR6Fhm4XC/bHBEiAK2EdNHJQjf5LYbcl19m7zVyrcTFadZIGUfCCcY3lu5ux 19yFvTwgxwyZodlVr5B8vptopvuBUJYyLoIEwLhZjm5lnTw1BNDlL2S2s++Cs7ym65 QPYdrfKFg8QkHyVqYN1E3jO4rGOS6pWxRoOtm/b4= Received: from unknown by smtp-4-0000.mail.infomaniak.ch (Postfix) with ESMTPA id 4XvyKM3njzzmn4; Fri, 22 Nov 2024 15:34:15 +0100 (CET) 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 , Francis Laniel , James Morris , Jann Horn , Jeff Xu , Jorge Lucangeli Obes , Kees Cook , Konstantin Meskhidze , Matt Bobrowski , Mikhail Ivanov , Phil Sutter , 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: [PATCH v3 10/23] landlock: Log domain properties and release Date: Fri, 22 Nov 2024 15:33:40 +0100 Message-ID: <20241122143353.59367-11-mic@digikod.net> In-Reply-To: <20241122143353.59367-1-mic@digikod.net> References: <20241122143353.59367-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 TODO: Update sample with audit's creation time and now-removed parent hierarchy Asynchronously log domain information when it first denies an access. This minimize the amount of generated logs, which makes it possible to always log denials since they should not happen (except with the new LANDLOCK_RESTRICT_SELF_LOGLESS flag). These records are identified by AUDIT_LANDLOCK_DOM_INFO. Log domain deletion with the AUDIT_LANDLOCK_DOM_DROP record type when a domain was previously logged. This makes it possible for user space to free potential resources when a domain ID will never show again. The logged domain properties include: - the domain ID - creation time - its creator's PID - its creator's UID - its creator's executable path (exe) - its creator's command line (comm) This require each domain to save some task properties at creation time: time, PID, credentials, exe path, and comm. It should be noted that we cannot use audit event's serial numbers because there may not be any related event. However, it is still useful to use the same potential timestamp instead of a close one. What really matter is how old the related Landlock domain is, not the uniquiness of the creation time. If audit is not enabled, Landlock creates its own timestamp. This timestamp will be exposed to user space with a future unprivileged interface. Audit event sample for a first denial: type=3DLL_DENY msg=3Daudit(1732186800.349:44): domain=3D195ba459b blocker= s=3Dptrace opid=3D1 ocomm=3D"systemd" type=3DLL_DOM_INFO msg=3Daudit(1732186800.349:44): domain=3D195ba459b cre= ation=3D1732186800.345 pid=3D300 uid=3D0 exe=3D"/root/sandboxer" comm=3D"sa= ndboxer" type=3DSYSCALL msg=3Daudit(1732186800.349:44): arch=3Dc000003e syscall=3D= 101 success=3Dno [...] pid=3D300 auid=3D0 Audit event sample for logged domains deletion: type=3DLL_DOM_DROP msg=3Daudit(1732186800.393:45): domain=3D195ba459b Cc: G=C3=BCnther Noack Cc: Paul Moore Signed-off-by: Micka=C3=ABl Sala=C3=BCn Link: https://lore.kernel.org/r/20241122143353.59367-11-mic@digikod.net --- Questions: - Should we also log the creator's loginuid? - Should we also log the creator's sessionid? Changes since v2: - Fix docstring. - Fix log_status check in log_hierarchy() to also log LANDLOCK_LOG_DISABLED. - Add audit's creation time to domain's properties. - Use hexadecimal notation for domain IDs. - Remove domain's parent records: parent domains are not really useful in the logs. They will be available with the upcoming introspection feature though. - Extend commit message with audit's timestamp explanation. Changes since v1: - Add a ruleset's version for atomic logs. - Rebased on the TCP patch series. - Rename operation using "_" instead of "-". - Rename AUDIT_LANDLOCK to AUDIT_LANDLOCK_RULESET. - Only log when audit is enabled, but always set domain IDs. - Don't log task's PID/TID with log_task() because it would be redundant with the SYSCALL record. - Remove race condition when logging ruleset creation and logging ruleset modification while the related file descriptor was already registered but the ruleset creation not logged yet. - Fix domain drop logs. - Move the domain drop record from the previous patch into this one. - Do not log domain creation but log first domain use instead. - Save task's properties that sandbox themselves. --- include/uapi/linux/audit.h | 2 + security/landlock/audit.c | 74 ++++++++++++++++++++++++++++++++++++ security/landlock/audit.h | 7 ++++ security/landlock/domain.c | 41 ++++++++++++++++++++ security/landlock/domain.h | 43 +++++++++++++++++++++ security/landlock/ruleset.c | 7 ++++ security/landlock/ruleset.h | 1 + security/landlock/syscalls.c | 1 + 8 files changed, 176 insertions(+) diff --git a/include/uapi/linux/audit.h b/include/uapi/linux/audit.h index 60c909c396c0..a72f7b3403be 100644 --- a/include/uapi/linux/audit.h +++ b/include/uapi/linux/audit.h @@ -147,6 +147,8 @@ #define AUDIT_IPE_CONFIG_CHANGE 1421 /* IPE config change */ #define AUDIT_IPE_POLICY_LOAD 1422 /* IPE policy load */ #define AUDIT_LANDLOCK_DENY 1423 /* Landlock denial */ +#define AUDIT_LANDLOCK_DOM_INFO 1424 /* Landlock domain properties */ +#define AUDIT_LANDLOCK_DOM_DROP 1425 /* Landlock domain release */ =20 #define AUDIT_FIRST_KERN_ANOM_MSG 1700 #define AUDIT_LAST_KERN_ANOM_MSG 1799 diff --git a/security/landlock/audit.c b/security/landlock/audit.c index eab6b3a8a231..2d0a96797dd4 100644 --- a/security/landlock/audit.c +++ b/security/landlock/audit.c @@ -8,8 +8,11 @@ #include #include #include +#include +#include =20 #include "audit.h" +#include "cred.h" #include "domain.h" #include "ruleset.h" =20 @@ -30,6 +33,41 @@ static void log_blockers(struct audit_buffer *const ab, audit_log_format(ab, "%s", get_blocker(type)); } =20 +static void log_node(struct landlock_hierarchy *const node) +{ + struct audit_buffer *ab; + + if (WARN_ON_ONCE(!node)) + return; + + /* Ignores already logged domains. */ + if (READ_ONCE(node->log_status) =3D=3D LANDLOCK_LOG_RECORDED) + return; + + ab =3D audit_log_start(audit_context(), GFP_ATOMIC, + AUDIT_LANDLOCK_DOM_INFO); + if (!ab) + return; + + WARN_ON_ONCE(node->id =3D=3D 0); + audit_log_format(ab, "domain=3D%llx creation=3D%llu.%03lu pid=3D%d uid=3D= %u", + node->id, + /* See audit_log_start() */ + (unsigned long long)node->creation.tv_sec, + node->creation.tv_nsec / 1000000, pid_nr(node->pid), + from_kuid(&init_user_ns, node->cred->uid)); + audit_log_d_path(ab, " exe=3D", &node->exe); + audit_log_format(ab, " comm=3D"); + audit_log_untrustedstring(ab, node->comm); + audit_log_end(ab); + + /* + * There may be race condition leading to logging of the same domain + * several times but that is OK. + */ + WRITE_ONCE(node->log_status, LANDLOCK_LOG_RECORDED); +} + static struct landlock_hierarchy * get_hierarchy(const struct landlock_ruleset *const domain, const size_t la= yer) { @@ -116,6 +154,42 @@ void landlock_log_denial(const struct landlock_ruleset= *const domain, log_blockers(ab, request->type); audit_log_lsm_data(ab, &request->audit); audit_log_end(ab); + + /* Logs this domain if it is the first time. */ + log_node(youngest_denied); +} + +/** + * landlock_log_drop_domain - Create an audit record when a domain is dele= ted + * + * Only domains which previously appeared in the audit logs are logged aga= in. + * This is useful to know when a domain will never show again in the audit= log. + * + * This record is not directly tied to a syscall entry. + * + * Called by the cred_free() hook, in an uninterruptible context. + */ +void landlock_log_drop_domain(const struct landlock_ruleset *const domain) +{ + struct audit_buffer *ab; + + if (WARN_ON_ONCE(!domain->hierarchy)) + return; + + if (!audit_enabled) + return; + + /* Ignores domains that were not logged. */ + if (READ_ONCE(domain->hierarchy->log_status) !=3D LANDLOCK_LOG_RECORDED) + return; + + ab =3D audit_log_start(audit_context(), GFP_ATOMIC, + AUDIT_LANDLOCK_DOM_DROP); + if (!ab) + return; + + audit_log_format(ab, "domain=3D%llx", domain->hierarchy->id); + audit_log_end(ab); } =20 #ifdef CONFIG_SECURITY_LANDLOCK_KUNIT_TEST diff --git a/security/landlock/audit.h b/security/landlock/audit.h index f33095afcd2f..7a1b1652f21b 100644 --- a/security/landlock/audit.h +++ b/security/landlock/audit.h @@ -36,11 +36,18 @@ struct landlock_request { =20 #ifdef CONFIG_AUDIT =20 +void landlock_log_drop_domain(const struct landlock_ruleset *const domain); + void landlock_log_denial(const struct landlock_ruleset *const domain, const struct landlock_request *const request); =20 #else /* CONFIG_AUDIT */ =20 +static inline void +landlock_log_drop_domain(const struct landlock_ruleset *const domain) +{ +} + static inline void landlock_log_denial(const struct landlock_ruleset *const domain, const struct landlock_request *const request) diff --git a/security/landlock/domain.c b/security/landlock/domain.c index 965e4dc21975..69b15c059583 100644 --- a/security/landlock/domain.c +++ b/security/landlock/domain.c @@ -5,17 +5,58 @@ * Copyright =C2=A9 2024 Microsoft Corporation */ =20 +#include +#include +#include +#include +#include +#include +#include +#include + #include "domain.h" #include "id.h" =20 +static struct path get_current_exe(void) +{ + struct path exe_path =3D {}; + struct file *exe_file; + struct mm_struct *mm =3D current->mm; + + if (!mm) + return exe_path; + + exe_file =3D get_mm_exe_file(mm); + if (!exe_file) + return exe_path; + + exe_path =3D exe_file->f_path; + path_get(&exe_path); + fput(exe_file); + return exe_path; +} + /** * landlock_init_current_hierarchy - Partially initialize landlock_hierarc= hy * * @hierarchy: The hierarchy to initialize. * + * The current task is referenced as the domain creator. The subjective + * credentials must not be in an overridden state. + * * @hierarchy->parent and @hierarchy->usage should already be set. */ void landlock_init_current_hierarchy(struct landlock_hierarchy *const hier= archy) { + hierarchy->creation =3D audit_get_ctime(audit_context()); + if (!hierarchy->creation.tv_sec) + ktime_get_coarse_real_ts64(&hierarchy->creation); + + hierarchy->log_status =3D LANDLOCK_LOG_PENDING; hierarchy->id =3D landlock_get_id(1); + WARN_ON_ONCE(current_cred() !=3D current_real_cred()); + hierarchy->cred =3D get_current_cred(); + hierarchy->pid =3D get_pid(task_pid(current)); + hierarchy->exe =3D get_current_exe(); + get_task_comm(hierarchy->comm, current); } diff --git a/security/landlock/domain.h b/security/landlock/domain.h index f82d831ca0a7..4a2864a66047 100644 --- a/security/landlock/domain.h +++ b/security/landlock/domain.h @@ -10,8 +10,18 @@ #ifndef _SECURITY_LANDLOCK_DOMAIN_H #define _SECURITY_LANDLOCK_DOMAIN_H =20 +#include #include +#include +#include #include +#include +#include + +enum landlock_log_status { + LANDLOCK_LOG_PENDING =3D 0, + LANDLOCK_LOG_RECORDED, +}; =20 /** * struct landlock_hierarchy - Node in a domain hierarchy @@ -29,10 +39,37 @@ struct landlock_hierarchy { refcount_t usage; =20 #ifdef CONFIG_AUDIT + /** + * @log_status: Whether this domain should be logged or not. Because + * concurrent log entries may be created at the same time, it is still + * possible to have several domain records of the same domain. + */ + enum landlock_log_status log_status; /** * @id: Landlock domain ID, sets once at domain creation time. */ u64 id; + /** + * @creation: Time of the domain creation (i.e. syscall entry as used + * in audit context). + */ + struct timespec64 creation; + /** + * @cred: Credential of the domain's creator. + */ + const struct cred *cred; + /** + * @pid: Creator's PID. + */ + struct pid *pid; + /** + * @exe: Creator's exe path. + */ + struct path exe; + /** + * @comm: Command line of the domain's creator at creation time. + */ + char comm[TASK_COMM_LEN]; #endif /* CONFIG_AUDIT */ }; =20 @@ -48,6 +85,12 @@ static inline void landlock_put_hierarchy(struct landloc= k_hierarchy *hierarchy) while (hierarchy && refcount_dec_and_test(&hierarchy->usage)) { const struct landlock_hierarchy *const freeme =3D hierarchy; =20 +#ifdef CONFIG_AUDIT + put_cred(hierarchy->cred); + put_pid(hierarchy->pid); + path_put(&hierarchy->exe); +#endif /* CONFIG_AUDIT */ + hierarchy =3D hierarchy->parent; kfree(freeme); } diff --git a/security/landlock/ruleset.c b/security/landlock/ruleset.c index 7a88fd9b2473..4619c7f63d05 100644 --- a/security/landlock/ruleset.c +++ b/security/landlock/ruleset.c @@ -508,6 +508,9 @@ static void free_ruleset_work(struct work_struct *const= work) void landlock_put_ruleset_deferred(struct landlock_ruleset *const ruleset) { if (ruleset && refcount_dec_and_test(&ruleset->usage)) { + /* Logs with the current context. */ + landlock_log_drop_domain(ruleset); + INIT_WORK(&ruleset->work_free, free_ruleset_work); schedule_work(&ruleset->work_free); } @@ -519,6 +522,10 @@ void landlock_put_ruleset_deferred(struct landlock_rul= eset *const ruleset) * @parent: Parent domain. * @ruleset: New ruleset to be merged. * + * The current task is referenced as the domain creator. The subjective + * credentials must not be in an overridden state (see + * landlock_init_current_hierarchy). + * * Returns the intersection of @parent and @ruleset, or returns @parent if * @ruleset is empty, or returns a duplicate of @ruleset if @parent is emp= ty. */ diff --git a/security/landlock/ruleset.h b/security/landlock/ruleset.h index 39169b6860e3..73636f214aac 100644 --- a/security/landlock/ruleset.h +++ b/security/landlock/ruleset.h @@ -12,6 +12,7 @@ #include #include #include +#include #include =20 #include "access.h" diff --git a/security/landlock/syscalls.c b/security/landlock/syscalls.c index c097d356fa45..335067e36feb 100644 --- a/security/landlock/syscalls.c +++ b/security/landlock/syscalls.c @@ -26,6 +26,7 @@ #include #include =20 +#include "audit.h" #include "cred.h" #include "fs.h" #include "limits.h" --=20 2.47.0 From nobody Tue Nov 26 01:29:03 2024 Received: from smtp-8faa.mail.infomaniak.ch (smtp-8faa.mail.infomaniak.ch [83.166.143.170]) (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 AB6B513CF9C for ; Fri, 22 Nov 2024 14:40:10 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=83.166.143.170 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1732286413; cv=none; b=hv6Ee8aZFBhXAMrLrrAilyY8Y0+KD9dHVkEjNNTAUDetfM3rDrG1AKmIXSIrHqYOr9qphh+NknvWEWFw8Vg7hvi3FgoaRVscAV03coxJ9s2xfiLyOLHrH4g/sCrdsi+aacWMe98SWtzDpZqdKaRWj71p9H2DNDq2NZ/lKbQNXbA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1732286413; c=relaxed/simple; bh=6kd3USl/v5quMElIHNSewxBHZwwNtlMc/QPwm7gFPjs=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=cwoj+YALGHP9Fh5hogh1T4KXwbD7SzKnT+c+Y1xsnfdZCw6vlvABPPm20Yk4UcgXkeTkcz3O5F5eLWeDPE2BQ+Fhqm27eq0TnjV4/bXxyhac/qBtmQTHqOwd3eH7xh8XeU3KG1dH1V7EvjhrCmFh9gaXvvKyXqpfmx6RTHHQuUg= 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=BIc+0LYp; arc=none smtp.client-ip=83.166.143.170 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="BIc+0LYp" Received: from smtp-3-0000.mail.infomaniak.ch (unknown [IPv6:2001:1600:4:17::246b]) by smtp-3-3000.mail.infomaniak.ch (Postfix) with ESMTPS id 4XvyKP6cq8zlSd; Fri, 22 Nov 2024 15:34:17 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=digikod.net; s=20191114; t=1732286057; bh=kk+ZrO9KuElri+1LTWSQtEzwM18pB5uBoFHaUi66ZKc=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=BIc+0LYp4Mg9RSnjDBBFqErOczP0XtRa5aiELSN41ta+AimREo3E7GYhJXW3VGAK3 5bfaBH64hFbfaRrzOHD4yg7EXgXOgSggsKxqvuGPyRf9uZF3Gnqv0kiGXQriZkw1Lx Kdm0DH1FbvfOn74Y18TQ3QzzQMRYRQOqp2GgCucw= Received: from unknown by smtp-3-0000.mail.infomaniak.ch (Postfix) with ESMTPA id 4XvyKN6gbTzmHf; Fri, 22 Nov 2024 15:34:16 +0100 (CET) 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 , Francis Laniel , James Morris , Jann Horn , Jeff Xu , Jorge Lucangeli Obes , Kees Cook , Konstantin Meskhidze , Matt Bobrowski , Mikhail Ivanov , Phil Sutter , 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: [PATCH v3 11/23] landlock: Log mount-related denials Date: Fri, 22 Nov 2024 15:33:41 +0100 Message-ID: <20241122143353.59367-12-mic@digikod.net> In-Reply-To: <20241122143353.59367-1-mic@digikod.net> References: <20241122143353.59367-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 for sb_mount, move_mount, sb_umount, sb_remount, and sb_pivot_root hooks. Add and use a new landlock_match_layer_level() helper. Audit event sample: type=3DLL_DENY [...]: domain=3D195ba459b blockers=3Dfs_change_layout name= =3D"/" dev=3D"tmpfs" ino=3D1 Cc: G=C3=BCnther Noack Signed-off-by: Micka=C3=ABl Sala=C3=BCn Link: https://lore.kernel.org/r/20241122143353.59367-12-mic@digikod.net --- Changes since v2: - Log the domain that denied the action because not all layers block FS layout changes. - Fix landlock_match_layer_level(). Changes since v1: - Rebased on the TCP patch series. - Don't log missing permissions, only domain layer, and then remove the permission word (suggested by G=C3=BCnther) --- security/landlock/audit.c | 3 ++ security/landlock/audit.h | 1 + security/landlock/fs.c | 64 ++++++++++++++++++++++++++++++++++--- security/landlock/ruleset.h | 31 ++++++++++++++++++ 4 files changed, 94 insertions(+), 5 deletions(-) diff --git a/security/landlock/audit.c b/security/landlock/audit.c index 2d0a96797dd4..a07b210ca524 100644 --- a/security/landlock/audit.c +++ b/security/landlock/audit.c @@ -21,6 +21,9 @@ static const char *get_blocker(const enum landlock_reques= t_type type) switch (type) { case LANDLOCK_REQUEST_PTRACE: return "ptrace"; + + case LANDLOCK_REQUEST_FS_CHANGE_LAYOUT: + return "fs_change_layout"; } =20 WARN_ON_ONCE(1); diff --git a/security/landlock/audit.h b/security/landlock/audit.h index 7a1b1652f21b..6f5ad04b83c2 100644 --- a/security/landlock/audit.h +++ b/security/landlock/audit.h @@ -15,6 +15,7 @@ =20 enum landlock_request_type { LANDLOCK_REQUEST_PTRACE =3D 1, + LANDLOCK_REQUEST_FS_CHANGE_LAYOUT, }; =20 /* diff --git a/security/landlock/fs.c b/security/landlock/fs.c index 9779170d9199..171012efb559 100644 --- a/security/landlock/fs.c +++ b/security/landlock/fs.c @@ -23,6 +23,7 @@ #include #include #include +#include #include #include #include @@ -37,6 +38,7 @@ #include =20 #include "access.h" +#include "audit.h" #include "common.h" #include "cred.h" #include "fs.h" @@ -1301,6 +1303,38 @@ static void hook_sb_delete(struct super_block *const= sb) !atomic_long_read(&landlock_superblock(sb)->inode_refs)); } =20 +static void +log_fs_change_layout_path(const struct landlock_ruleset *const domain, + const struct path *const path) +{ + const struct landlock_request request =3D { + .type =3D LANDLOCK_REQUEST_FS_CHANGE_LAYOUT, + .audit =3D { + .type =3D LSM_AUDIT_DATA_PATH, + .u.path =3D *path, + }, + .layer_plus_one =3D landlock_match_layer_level(domain, any_fs) + 1, + }; + + landlock_log_denial(domain, &request); +} + +static void +log_fs_change_layout_dentry(const struct landlock_ruleset *const domain, + struct dentry *const dentry) +{ + const struct landlock_request request =3D { + .type =3D LANDLOCK_REQUEST_FS_CHANGE_LAYOUT, + .audit =3D { + .type =3D LSM_AUDIT_DATA_DENTRY, + .u.dentry =3D dentry, + }, + .layer_plus_one =3D landlock_match_layer_level(domain, any_fs) + 1, + }; + + landlock_log_denial(domain, &request); +} + /* * Because a Landlock security policy is defined according to the filesyst= em * topology (i.e. the mount namespace), changing it may grant access to fi= les @@ -1323,16 +1357,24 @@ static int hook_sb_mount(const char *const dev_name, const struct path *const path, const char *const type, const unsigned long flags, void *const data) { - if (!get_current_fs_domain()) + const struct landlock_ruleset *const dom =3D get_current_fs_domain(); + + if (!dom) return 0; + + log_fs_change_layout_path(dom, path); return -EPERM; } =20 static int hook_move_mount(const struct path *const from_path, const struct path *const to_path) { - if (!get_current_fs_domain()) + const struct landlock_ruleset *const dom =3D get_current_fs_domain(); + + if (!dom) return 0; + + log_fs_change_layout_path(dom, to_path); return -EPERM; } =20 @@ -1342,15 +1384,23 @@ static int hook_move_mount(const struct path *const= from_path, */ static int hook_sb_umount(struct vfsmount *const mnt, const int flags) { - if (!get_current_fs_domain()) + const struct landlock_ruleset *const dom =3D get_current_fs_domain(); + + if (!dom) return 0; + + log_fs_change_layout_dentry(dom, mnt->mnt_root); return -EPERM; } =20 static int hook_sb_remount(struct super_block *const sb, void *const mnt_o= pts) { - if (!get_current_fs_domain()) + const struct landlock_ruleset *const dom =3D get_current_fs_domain(); + + if (!dom) return 0; + + log_fs_change_layout_dentry(dom, sb->s_root); return -EPERM; } =20 @@ -1365,8 +1415,12 @@ static int hook_sb_remount(struct super_block *const= sb, void *const mnt_opts) static int hook_sb_pivotroot(const struct path *const old_path, const struct path *const new_path) { - if (!get_current_fs_domain()) + const struct landlock_ruleset *const dom =3D get_current_fs_domain(); + + if (!dom) return 0; + + log_fs_change_layout_path(dom, new_path); return -EPERM; } =20 diff --git a/security/landlock/ruleset.h b/security/landlock/ruleset.h index 73636f214aac..944126a92aec 100644 --- a/security/landlock/ruleset.h +++ b/security/landlock/ruleset.h @@ -268,6 +268,37 @@ landlock_get_applicable_domain(const struct landlock_r= uleset *const domain, return NULL; } =20 +/** + * landlock_match_layer_level - Return the layer level restricting @masks + * + * @domain: Landlock ruleset (used as a domain) + * @masks: access masks + * + * Returns: the number of the layer restricting/handling any right of @acc= ess, + * or return 0 (i.e. first layer) otherwise. + */ +static inline size_t +landlock_match_layer_level(const struct landlock_ruleset *const domain, + const struct access_masks masks) +{ + const union access_masks_all masks_all =3D { + .masks =3D masks, + }; + ssize_t layer_level; + + for (layer_level =3D domain->num_layers; layer_level >=3D 0; + layer_level--) { + union access_masks_all layer =3D { + .masks =3D domain->access_masks[layer_level], + }; + + if (masks_all.all & layer.all) + return layer_level; + } + + return 0; +} + static inline void landlock_add_fs_access_mask(struct landlock_ruleset *const ruleset, const access_mask_t fs_access_mask, --=20 2.47.0 From nobody Tue Nov 26 01:29:03 2024 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 D8B6F1DE89E for ; Fri, 22 Nov 2024 14:34:20 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=84.16.66.172 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1732286064; cv=none; b=aJnzhAA9YAl3Z9MB1rwwXGE6OSY0ORMT0ag0qJsWPOD56DMC642Uo7fplJMqTckolehzLa+6FibHwlN/oAAQYlmXFSQ13V+mOJ7WUMHGPwwn5J18Ntohr8CX8mVoIZ4OUsmo/daV27neelDBSZauFmTzZtqiko/qM4FMBsFEty4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1732286064; c=relaxed/simple; bh=r9ftnbbE8+eDuDA3+7n9COnZvjC1aYV3g18xqvTZxnA=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=Bc2sT1jni8c4fUsFDDTvANRw3GUjzjdeGlZSRwxS7IOAiT+Ln76wf8AWRSovTCSC0tS4MvSK1N2E28LYTVadH0IWDZByqoZwgZRMwbfknT6yit5XK7143TGMRPj1JdYQ99tKmOSPJDmLOXk2Zeew8/Zgua5ocbW0M6ZaTWkVwwI= 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=rsDw6lvl; arc=none smtp.client-ip=84.16.66.172 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="rsDw6lvl" Received: from smtp-3-0000.mail.infomaniak.ch (unknown [IPv6:2001:1600:4:17::246b]) by smtp-3-3000.mail.infomaniak.ch (Postfix) with ESMTPS id 4XvyKR1C0Bzjcn; Fri, 22 Nov 2024 15:34:19 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=digikod.net; s=20191114; t=1732286059; bh=Tr09ZZYMmyRN9g3gpEskXCFWHcXDgxBCb8vxALpa40g=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=rsDw6lvl4iF2+us577286u/6fbvYgIUoVM04ftvujWeYE4UqaiMENZ+5R0/XXarIp qN1kw68U1e1VqslNRj1wXqYbslk7JnGfuuyrqby4T/Q1mVsMYLtoB5Tn5HWFV9hHv+ 7cSz8vS++DEVobITGRCfv0N2XB+BV2zYilr+chJ8= Received: from unknown by smtp-3-0000.mail.infomaniak.ch (Postfix) with ESMTPA id 4XvyKQ2jynzlnp; Fri, 22 Nov 2024 15:34:18 +0100 (CET) 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 , Francis Laniel , James Morris , Jann Horn , Jeff Xu , Jorge Lucangeli Obes , Kees Cook , Konstantin Meskhidze , Matt Bobrowski , Mikhail Ivanov , Phil Sutter , 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: [PATCH v3 12/23] landlock: Align partial refer access checks with final ones Date: Fri, 22 Nov 2024 15:33:42 +0100 Message-ID: <20241122143353.59367-13-mic@digikod.net> In-Reply-To: <20241122143353.59367-1-mic@digikod.net> References: <20241122143353.59367-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 Fix a logical issue that could have been visible if the source or the destination of a rename/link action was allowed for either the source or the destination but not both. However, this logical bug is unreachable because either: - the rename/link action is allowed by the access rights tied to the same mount point (without relying on access rights in a parent mount point) and the access request is allowed (i.e. allow_parent1 and allow_parent2 are true in current_check_refer_path), - or a common rule in a parent mount point updates the access check for the source and the destination (cf. is_access_to_paths_allowed). See the following layout1.refer_part_mount_tree_is_allowed test that work with and without this fix. This fix does not impact current code but it is required for the audit support. Cc: G=C3=BCnther Noack Signed-off-by: Micka=C3=ABl Sala=C3=BCn Link: https://lore.kernel.org/r/20241122143353.59367-13-mic@digikod.net --- Changes since v2: - New patch. --- security/landlock/fs.c | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/security/landlock/fs.c b/security/landlock/fs.c index 171012efb559..ddadc465581e 100644 --- a/security/landlock/fs.c +++ b/security/landlock/fs.c @@ -567,6 +567,12 @@ static void test_no_more_access(struct kunit *const te= st) #undef NMA_TRUE #undef NMA_FALSE =20 +static bool is_layer_masks_allowed( + layer_mask_t (*const layer_masks)[LANDLOCK_NUM_ACCESS_FS]) +{ + return !memchr_inv(layer_masks, 0, sizeof(*layer_masks)); +} + /* * Removes @layer_masks accesses that are not requested. * @@ -584,7 +590,8 @@ scope_to_request(const access_mask_t access_request, =20 for_each_clear_bit(access_bit, &access_req, ARRAY_SIZE(*layer_masks)) (*layer_masks)[access_bit] =3D 0; - return !memchr_inv(layer_masks, 0, sizeof(*layer_masks)); + + return is_layer_masks_allowed(layer_masks); } =20 #ifdef CONFIG_SECURITY_LANDLOCK_KUNIT_TEST @@ -773,9 +780,14 @@ static bool is_access_to_paths_allowed( if (WARN_ON_ONCE(domain->num_layers < 1 || !layer_masks_parent1)) return false; =20 + allowed_parent1 =3D is_layer_masks_allowed(layer_masks_parent1); + if (unlikely(layer_masks_parent2)) { if (WARN_ON_ONCE(!dentry_child1)) return false; + + allowed_parent2 =3D is_layer_masks_allowed(layer_masks_parent2); + /* * For a double request, first check for potential privilege * escalation by looking at domain handled accesses (which are --=20 2.47.0 From nobody Tue Nov 26 01:29:03 2024 Received: from smtp-bc0e.mail.infomaniak.ch (smtp-bc0e.mail.infomaniak.ch [45.157.188.14]) (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 463841DFD8D for ; Fri, 22 Nov 2024 14:34:27 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=45.157.188.14 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1732286069; cv=none; b=Gk54mj6ksfb4+iKCDEsRy9eqCdFVPiluG+d+TQ7EiK3GwKRS62JWhJ4+b/GEVpGVNZER62hR5osAO6c60HncEfdwnhU7e/mJrFSlT/cJFd+eve7o6QME6GjLvcfuFEInJ4FpHHfsp99boXY0ZGmFSzybWUw+MT19o5GcEabc804= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1732286069; c=relaxed/simple; bh=5jhXIrCKW9G90gP+X+JAGYcArPkac3fHOXKVNwSJI20=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=Vfg3y4UWa5DHiehjAD4U+vGb5ZR1zNrnkJwpavpUpoUAfEDS9oCU1k8gTWIfNLBIqYm8KK4SLO1aKbBgqF+6YamwzCkQTzE2Sx/g650FArUtH1j0TWY0RxioXeN56z823K47OxrHcZ05nf+eKMnmZwMQyDa6WgvAkDLRiRv6A6o= 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=rZvMXWQJ; arc=none smtp.client-ip=45.157.188.14 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="rZvMXWQJ" Received: from smtp-3-0000.mail.infomaniak.ch (smtp-3-0000.mail.infomaniak.ch [10.4.36.107]) by smtp-3-3000.mail.infomaniak.ch (Postfix) with ESMTPS id 4XvyKS3jrhzZsx; Fri, 22 Nov 2024 15:34:20 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=digikod.net; s=20191114; t=1732286060; bh=+NTh0aRLS4WQhSiSk2pWdvUKQlq2NLeswsQdqGHb16Q=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=rZvMXWQJJE1Hrk7rMLICf+JRABO9hjsv/AjjE2WomOaE+zZHQum7xuOmVVu3TnXPl p+y53yHGANGNRdMicklvaUH9p+9pv97o/pzhG7TXE7WHKDB5grJquGJlhxjv2TR8Xn wemY3kzzewoCcAy0ZPSsEA2iKEbuX0RHlDbaPey8= Received: from unknown by smtp-3-0000.mail.infomaniak.ch (Postfix) with ESMTPA id 4XvyKR5CDzzkXR; Fri, 22 Nov 2024 15:34:19 +0100 (CET) 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 , Francis Laniel , James Morris , Jann Horn , Jeff Xu , Jorge Lucangeli Obes , Kees Cook , Konstantin Meskhidze , Matt Bobrowski , Mikhail Ivanov , Phil Sutter , 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: [PATCH v3 13/23] selftests/landlock: Add test to check partial access in a mount tree Date: Fri, 22 Nov 2024 15:33:43 +0100 Message-ID: <20241122143353.59367-14-mic@digikod.net> In-Reply-To: <20241122143353.59367-1-mic@digikod.net> References: <20241122143353.59367-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 layout1.refer_part_mount_tree_is_allowed to test the masked logical issue regarding collect_domain_accesses() calls followed by the is_access_to_paths_allowed() check in current_check_refer_path(). See previous commit. This test should work without the previous fix as well, but it enables us to make sure future changes will not have impact regarding this behavior. Cc: G=C3=BCnther Noack Signed-off-by: Micka=C3=ABl Sala=C3=BCn Link: https://lore.kernel.org/r/20241122143353.59367-14-mic@digikod.net --- Changes since v2: - New patch. --- tools/testing/selftests/landlock/fs_test.c | 54 ++++++++++++++++++++-- 1 file changed, 50 insertions(+), 4 deletions(-) diff --git a/tools/testing/selftests/landlock/fs_test.c b/tools/testing/sel= ftests/landlock/fs_test.c index 6788762188fe..42ce1e79ba82 100644 --- a/tools/testing/selftests/landlock/fs_test.c +++ b/tools/testing/selftests/landlock/fs_test.c @@ -85,6 +85,9 @@ static const char file1_s3d1[] =3D TMP_DIR "/s3d1/f1"; /* dir_s3d2 is a mount point. */ static const char dir_s3d2[] =3D TMP_DIR "/s3d1/s3d2"; static const char dir_s3d3[] =3D TMP_DIR "/s3d1/s3d2/s3d3"; +static const char file1_s3d3[] =3D TMP_DIR "/s3d1/s3d2/s3d3/f1"; +static const char dir_s3d4[] =3D TMP_DIR "/s3d1/s3d2/s3d4"; +static const char file1_s3d4[] =3D TMP_DIR "/s3d1/s3d2/s3d4/f1"; =20 /* * layout1 hierarchy: @@ -108,8 +111,11 @@ static const char dir_s3d3[] =3D TMP_DIR "/s3d1/s3d2/s= 3d3"; * =E2=94=82=C2=A0=C2=A0 =E2=94=94=E2=94=80=E2=94=80 f2 * =E2=94=94=E2=94=80=E2=94=80 s3d1 * =C2=A0=C2=A0 =E2=94=9C=E2=94=80=E2=94=80 f1 - * =E2=94=94=E2=94=80=E2=94=80 s3d2 - * =E2=94=94=E2=94=80=E2=94=80 s3d3 + * =E2=94=94=E2=94=80=E2=94=80 s3d2 [mount point] + * =C2=A0=C2=A0 =E2=94=9C=E2=94=80=E2=94=80 s3d3 + * =C2=A0=C2=A0 =E2=94=82 =E2=94=94=E2=94=80=E2=94=80 f1 + * =C2=A0=C2=A0 =E2=94=94=E2=94=80=E2=94=80 s3d4 + * =C2=A0=C2=A0 =E2=94=94=E2=94=80=E2=94=80 f1 */ =20 static bool fgrep(FILE *const inf, const char *const str) @@ -358,7 +364,8 @@ static void create_layout1(struct __test_metadata *cons= t _metadata) ASSERT_EQ(0, mount_opt(&mnt_tmp, dir_s3d2)); clear_cap(_metadata, CAP_SYS_ADMIN); =20 - ASSERT_EQ(0, mkdir(dir_s3d3, 0700)); + create_file(_metadata, file1_s3d3); + create_file(_metadata, file1_s3d4); } =20 static void remove_layout1(struct __test_metadata *const _metadata) @@ -378,7 +385,8 @@ static void remove_layout1(struct __test_metadata *cons= t _metadata) EXPECT_EQ(0, remove_path(dir_s2d2)); =20 EXPECT_EQ(0, remove_path(file1_s3d1)); - EXPECT_EQ(0, remove_path(dir_s3d3)); + EXPECT_EQ(0, remove_path(file1_s3d3)); + EXPECT_EQ(0, remove_path(file1_s3d4)); set_cap(_metadata, CAP_SYS_ADMIN); umount(dir_s3d2); clear_cap(_metadata, CAP_SYS_ADMIN); @@ -2444,6 +2452,44 @@ TEST_F_FORK(layout1, refer_mount_root_deny) EXPECT_EQ(0, close(root_fd)); } =20 +TEST_F_FORK(layout1, refer_part_mount_tree_is_allowed) +{ + const struct rule layer1[] =3D { + { + /* Parent mount point. */ + .path =3D dir_s3d1, + .access =3D LANDLOCK_ACCESS_FS_REFER | + LANDLOCK_ACCESS_FS_MAKE_REG, + }, + { + /* + * Removing the source file is allowed because its + * access rights are already a superset of the + * destination. + */ + .path =3D dir_s3d4, + .access =3D LANDLOCK_ACCESS_FS_REFER | + LANDLOCK_ACCESS_FS_MAKE_REG | + LANDLOCK_ACCESS_FS_REMOVE_FILE, + }, + {}, + }; + int ruleset_fd; + + ASSERT_EQ(0, unlink(file1_s3d3)); + ruleset_fd =3D create_ruleset(_metadata, + LANDLOCK_ACCESS_FS_REFER | + LANDLOCK_ACCESS_FS_MAKE_REG | + LANDLOCK_ACCESS_FS_REMOVE_FILE, + layer1); + + ASSERT_LE(0, ruleset_fd); + enforce_ruleset(_metadata, ruleset_fd); + ASSERT_EQ(0, close(ruleset_fd)); + + ASSERT_EQ(0, rename(file1_s3d4, file1_s3d3)); +} + TEST_F_FORK(layout1, reparent_link) { const struct rule layer1[] =3D { --=20 2.47.0 From nobody Tue Nov 26 01:29:03 2024 Received: from smtp-42ab.mail.infomaniak.ch (smtp-42ab.mail.infomaniak.ch [84.16.66.171]) (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 395741DF26F for ; Fri, 22 Nov 2024 14:34:23 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=84.16.66.171 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1732286067; cv=none; b=G87v0/0LO6y6JXao+P5dcrdInzCggVtNeFNxvCBi4zkk1vq+IsPtGz2oOv/sYlPagrBVjnao7trTouuiRC5QYeQLeTO9mJitCPIRbC5Qp0Cw395H7jdx0Xqp0XDhPtHzfjNCdIdnpEJnLhq+QX/HRZ0Swi3u2MJdX2/RqkzHT/A= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1732286067; c=relaxed/simple; bh=nlZU8WDPKtn1L81Yi1gEClvOBIb9Janj80e3MicsfYE=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=hNcRpMXH9COFyZJrShqLt1BJEn8hEIOO9tsPzHd9aTmitaDK7pn+Gbn51Hhln47oe9OMvYUfBheJFgLiM/r8prIY+VcMXmZDwMUxrDSr3ehVYyUZEWepDKrasjvAwF68G3ZR8WHXSOhU0YwuMR3/DP9kig30R1oAKk0Fz19yH1U= 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=DgjbOs6X; arc=none smtp.client-ip=84.16.66.171 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="DgjbOs6X" 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 4XvyKT4g5Pzb4c; Fri, 22 Nov 2024 15:34:21 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=digikod.net; s=20191114; t=1732286061; bh=vQ9EwPFoFKamnGETMYh8GoEgBYciQrBD4rSrzQG+2aQ=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=DgjbOs6XxKkJcWhOo3zz5Y16Sf1LETDM09N5FLNrIsCNNo8M1tcLy0Asnj1NfliGo jy9TUI1MtpdXI06NaElU8HgO2GONcyvApI7uGTHE4zr12UUyf6Igx/wBnes/0qsF8N ElO1kbsWsSLngzwhLRqlU4U4KgcoTuDXD1/CV6fQ= Received: from unknown by smtp-4-0001.mail.infomaniak.ch (Postfix) with ESMTPA id 4XvyKT03sNzn9h; Fri, 22 Nov 2024 15:34:21 +0100 (CET) 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 , Francis Laniel , James Morris , Jann Horn , Jeff Xu , Jorge Lucangeli Obes , Kees Cook , Konstantin Meskhidze , Matt Bobrowski , Mikhail Ivanov , Phil Sutter , 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: [PATCH v3 14/23] landlock: Optimize file path walks and prepare for audit support Date: Fri, 22 Nov 2024 15:33:44 +0100 Message-ID: <20241122143353.59367-15-mic@digikod.net> In-Reply-To: <20241122143353.59367-1-mic@digikod.net> References: <20241122143353.59367-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 Always synchronize access_masked_parent* with access_request_parent* according to allowed_parent*. This is required for audit support to be able to get back to the reason of denial. In a rename/link action, instead of always checking a rule two times for the same parent directory of the source and the destination files, only check it when an action on a child was not already allowed. This also enables us to keep consistent allowed_parent* status, which is required to get back to the reason of denial. For internal mount points, only upgrade allowed_parent* to true but do not wrongfully set both of them to false otherwise. This is also required to get back to the reason of denial. This does not impact the current behavior but slightly optimize code and prepare for audit support that needs to know the exact reason why an access was denied. Cc: G=C3=BCnther Noack Signed-off-by: Micka=C3=ABl Sala=C3=BCn Link: https://lore.kernel.org/r/20241122143353.59367-15-mic@digikod.net --- Changes since v2: - New patch. --- security/landlock/fs.c | 44 ++++++++++++++++++++++++++---------------- 1 file changed, 27 insertions(+), 17 deletions(-) diff --git a/security/landlock/fs.c b/security/landlock/fs.c index ddadc465581e..01f9d5e78218 100644 --- a/security/landlock/fs.c +++ b/security/landlock/fs.c @@ -854,15 +854,6 @@ static bool is_access_to_paths_allowed( child1_is_directory, layer_masks_parent2, layer_masks_child2, child2_is_directory))) { - allowed_parent1 =3D scope_to_request( - access_request_parent1, layer_masks_parent1); - allowed_parent2 =3D scope_to_request( - access_request_parent2, layer_masks_parent2); - - /* Stops when all accesses are granted. */ - if (allowed_parent1 && allowed_parent2) - break; - /* * Now, downgrades the remaining checks from domain * handled accesses to requested accesses. @@ -870,15 +861,32 @@ static bool is_access_to_paths_allowed( is_dom_check =3D false; access_masked_parent1 =3D access_request_parent1; access_masked_parent2 =3D access_request_parent2; + + allowed_parent1 =3D + allowed_parent1 || + scope_to_request(access_masked_parent1, + layer_masks_parent1); + allowed_parent2 =3D + allowed_parent2 || + scope_to_request(access_masked_parent2, + layer_masks_parent2); + + /* Stops when all accesses are granted. */ + if (allowed_parent1 && allowed_parent2) + break; } =20 rule =3D find_rule(domain, walker_path.dentry); - allowed_parent1 =3D landlock_unmask_layers( - rule, access_masked_parent1, layer_masks_parent1, - ARRAY_SIZE(*layer_masks_parent1)); - allowed_parent2 =3D landlock_unmask_layers( - rule, access_masked_parent2, layer_masks_parent2, - ARRAY_SIZE(*layer_masks_parent2)); + allowed_parent1 =3D allowed_parent1 || + landlock_unmask_layers( + rule, access_masked_parent1, + layer_masks_parent1, + ARRAY_SIZE(*layer_masks_parent1)); + allowed_parent2 =3D allowed_parent2 || + landlock_unmask_layers( + rule, access_masked_parent2, + layer_masks_parent2, + ARRAY_SIZE(*layer_masks_parent2)); =20 /* Stops when a rule from each layer grants access. */ if (allowed_parent1 && allowed_parent2) @@ -902,8 +910,10 @@ static bool is_access_to_paths_allowed( * access to internal filesystems (e.g. nsfs, which is * reachable through /proc//ns/). */ - allowed_parent1 =3D allowed_parent2 =3D - !!(walker_path.mnt->mnt_flags & MNT_INTERNAL); + if (walker_path.mnt->mnt_flags & MNT_INTERNAL) { + allowed_parent1 =3D true; + allowed_parent2 =3D true; + } break; } parent_dentry =3D dget_parent(walker_path.dentry); --=20 2.47.0 From nobody Tue Nov 26 01:29:03 2024 Received: from smtp-8fa8.mail.infomaniak.ch (smtp-8fa8.mail.infomaniak.ch [83.166.143.168]) (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 BEBE41DF73A; Fri, 22 Nov 2024 14:34:24 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=83.166.143.168 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1732286069; cv=none; b=jBEcWbFmLt4BRuAzz0DAtEj0x/eSCSwfHjmBmwlAztyp9tI7Ty5n8jcsiiid/PCytr+7zsO2HHxIU2DKPDMdq/Ud92ELXLHvUwLvTCN3HH01CbA6IgigzCZyLZsZ5iArwzF8Dx2RB3P8/xsjegNJeJRx695sg1UWexhs1efCV64= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1732286069; c=relaxed/simple; bh=iM5hy4qesSdd/u8gmWNlUbQCdrQv0Y3Ie+kj4+E4KAQ=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=o/NuW7RKUMrvSS1tdNjS0ltPjMuVorLkKgpnH7AoqlI+qIO1kbRQpid6FiZPsBTFH/LTgC7kSkaCbRrN34Y8kUAGDZ8mUUFTvtHDS9cUfEL4KzCURwkUYjdHCD7JhrXYqd17kkmcd4TwKtFlv9sIw5xwMENXEhkW9PgCWU3XBAw= 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=x5lRAGJ1; arc=none smtp.client-ip=83.166.143.168 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="x5lRAGJ1" Received: from smtp-3-0001.mail.infomaniak.ch (smtp-3-0001.mail.infomaniak.ch [10.4.36.108]) by smtp-3-3000.mail.infomaniak.ch (Postfix) with ESMTPS id 4XvyKV5xCTzmF4; Fri, 22 Nov 2024 15:34:22 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=digikod.net; s=20191114; t=1732286062; bh=GhUOqgK1sJt3bK+wnj94O/47WyF+YlaEHs3CkcN3yjs=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=x5lRAGJ15Qb9RP9xL61tQ2rUBwH0Nqx7yPnMJWrZfmG+tgRuZqQpMaoNRFjZOUV6e 82eNnOLwZJhVPceuaumRNxRq8CucvZNYM/toUzqgnoRno9+CCIn6CCqh5tMOUibA/X lZL6cYXY/pyc2NDPpk2E+tptMS4kIu2H4PV2mgaI= Received: from unknown by smtp-3-0001.mail.infomaniak.ch (Postfix) with ESMTPA id 4XvyKV13YPzDq2; Fri, 22 Nov 2024 15:34:22 +0100 (CET) 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 , Francis Laniel , James Morris , Jann Horn , Jeff Xu , Jorge Lucangeli Obes , Kees Cook , Konstantin Meskhidze , Matt Bobrowski , Mikhail Ivanov , Phil Sutter , 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: [PATCH v3 15/23] landlock: Log file-related denials Date: Fri, 22 Nov 2024 15:33:45 +0100 Message-ID: <20241122143353.59367-16-mic@digikod.net> In-Reply-To: <20241122143353.59367-1-mic@digikod.net> References: <20241122143353.59367-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 for path_mkdir, path_mknod, path_symlink, path_unlink, path_rmdir, path_truncate, path_link, path_rename, and file_open hooks. Audit event sample for a link action: type=3DLL_DENY [...]: domain=3D195ba459b blockers=3Dfs_refer path=3D"/usr= /bin" dev=3D"vda2" ino=3D351 type=3DLL_DENY [...]: domain=3D195ba459b blockers=3Dfs_make_reg,fs_refer = path=3D"/usr/local" dev=3D"vda2" ino=3D365 Cc: G=C3=BCnther Noack Signed-off-by: Micka=C3=ABl Sala=C3=BCn Link: https://lore.kernel.org/r/20241122143353.59367-16-mic@digikod.net --- Changes since v2: - Replace integer with bool in log_blockers(). - Always initialize youngest_layer, spotted by Francis Laniel. - Fix incorrect log reason by using access_masked_parent1 instead of access_request_parent1 (thanks to the previous fix patches). - Clean up formatting. Changes since v1: - Move audit code to the ptrace patch. - Revamp logging and support the path_link and path_rename hooks. - Add KUnit tests. --- security/landlock/audit.c | 178 ++++++++++++++++++++++++++++++++++++-- security/landlock/audit.h | 9 ++ security/landlock/fs.c | 65 +++++++++++--- 3 files changed, 235 insertions(+), 17 deletions(-) diff --git a/security/landlock/audit.c b/security/landlock/audit.c index a07b210ca524..8a7657d12478 100644 --- a/security/landlock/audit.c +++ b/security/landlock/audit.c @@ -7,23 +7,56 @@ =20 #include #include +#include #include #include #include +#include =20 #include "audit.h" +#include "common.h" #include "cred.h" #include "domain.h" #include "ruleset.h" =20 -static const char *get_blocker(const enum landlock_request_type type) +static const char *const fs_access_strings[] =3D { + [BIT_INDEX(LANDLOCK_ACCESS_FS_EXECUTE)] =3D "fs_execute", + [BIT_INDEX(LANDLOCK_ACCESS_FS_WRITE_FILE)] =3D "fs_write_file", + [BIT_INDEX(LANDLOCK_ACCESS_FS_READ_FILE)] =3D "fs_read_file", + [BIT_INDEX(LANDLOCK_ACCESS_FS_READ_DIR)] =3D "fs_read_dir", + [BIT_INDEX(LANDLOCK_ACCESS_FS_REMOVE_DIR)] =3D "fs_remove_dir", + [BIT_INDEX(LANDLOCK_ACCESS_FS_REMOVE_FILE)] =3D "fs_remove_file", + [BIT_INDEX(LANDLOCK_ACCESS_FS_MAKE_CHAR)] =3D "fs_make_char", + [BIT_INDEX(LANDLOCK_ACCESS_FS_MAKE_DIR)] =3D "fs_make_dir", + [BIT_INDEX(LANDLOCK_ACCESS_FS_MAKE_REG)] =3D "fs_make_reg", + [BIT_INDEX(LANDLOCK_ACCESS_FS_MAKE_SOCK)] =3D "fs_make_sock", + [BIT_INDEX(LANDLOCK_ACCESS_FS_MAKE_FIFO)] =3D "fs_make_fifo", + [BIT_INDEX(LANDLOCK_ACCESS_FS_MAKE_BLOCK)] =3D "fs_make_block", + [BIT_INDEX(LANDLOCK_ACCESS_FS_MAKE_SYM)] =3D "fs_make_sym", + [BIT_INDEX(LANDLOCK_ACCESS_FS_REFER)] =3D "fs_refer", + [BIT_INDEX(LANDLOCK_ACCESS_FS_TRUNCATE)] =3D "fs_truncate", + [BIT_INDEX(LANDLOCK_ACCESS_FS_IOCTL_DEV)] =3D "fs_ioctl_dev", +}; + +static_assert(ARRAY_SIZE(fs_access_strings) =3D=3D LANDLOCK_NUM_ACCESS_FS); + +static __attribute_const__ const char * +get_blocker(const enum landlock_request_type type, + const unsigned long access_bit) { switch (type) { case LANDLOCK_REQUEST_PTRACE: + WARN_ON_ONCE(access_bit !=3D -1); return "ptrace"; =20 case LANDLOCK_REQUEST_FS_CHANGE_LAYOUT: + WARN_ON_ONCE(access_bit !=3D -1); return "fs_change_layout"; + + case LANDLOCK_REQUEST_FS_ACCESS: + if (WARN_ON_ONCE(access_bit >=3D ARRAY_SIZE(fs_access_strings))) + return "unknown"; + return fs_access_strings[access_bit]; } =20 WARN_ON_ONCE(1); @@ -31,9 +64,20 @@ static const char *get_blocker(const enum landlock_reque= st_type type) } =20 static void log_blockers(struct audit_buffer *const ab, - const enum landlock_request_type type) + const enum landlock_request_type type, + const access_mask_t access) { - audit_log_format(ab, "%s", get_blocker(type)); + const unsigned long access_mask =3D access; + unsigned long access_bit; + bool is_first =3D true; + + for_each_set_bit(access_bit, &access_mask, BITS_PER_TYPE(access)) { + audit_log_format(ab, "%s%s", is_first ? "" : ",", + get_blocker(type, access_bit)); + is_first =3D false; + } + if (is_first) + audit_log_format(ab, "%s", get_blocker(type, -1)); } =20 static void log_node(struct landlock_hierarchy *const node) @@ -118,9 +162,110 @@ static void test_get_hierarchy(struct kunit *const te= st) =20 #endif /* CONFIG_SECURITY_LANDLOCK_KUNIT_TEST */ =20 +static size_t get_denied_layer(const struct landlock_ruleset *const domain, + access_mask_t *const access_request, + const layer_mask_t (*const layer_masks)[], + const size_t layer_masks_size) +{ + const unsigned long access_req =3D *access_request; + unsigned long access_bit; + access_mask_t missing =3D 0; + long youngest_layer =3D -1; + + for_each_set_bit(access_bit, &access_req, layer_masks_size) { + const access_mask_t mask =3D (*layer_masks)[access_bit]; + long layer; + + if (!mask) + continue; + + /* __fls(1) =3D=3D 0 */ + layer =3D __fls(mask); + if (layer > youngest_layer) { + youngest_layer =3D layer; + missing =3D BIT(access_bit); + } else if (layer =3D=3D youngest_layer) { + missing |=3D BIT(access_bit); + } + } + + *access_request =3D missing; + if (youngest_layer =3D=3D -1) + return domain->num_layers - 1; + + return youngest_layer; +} + +#ifdef CONFIG_SECURITY_LANDLOCK_KUNIT_TEST + +static void test_get_denied_layer(struct kunit *const test) +{ + const struct landlock_ruleset dom =3D { + .num_layers =3D 5, + }; + const layer_mask_t layer_masks[LANDLOCK_NUM_ACCESS_FS] =3D { + [BIT_INDEX(LANDLOCK_ACCESS_FS_EXECUTE)] =3D BIT(0), + [BIT_INDEX(LANDLOCK_ACCESS_FS_READ_FILE)] =3D BIT(1), + [BIT_INDEX(LANDLOCK_ACCESS_FS_READ_DIR)] =3D BIT(1) | BIT(0), + [BIT_INDEX(LANDLOCK_ACCESS_FS_REMOVE_DIR)] =3D BIT(2), + }; + access_mask_t access; + + access =3D LANDLOCK_ACCESS_FS_EXECUTE; + KUNIT_EXPECT_EQ(test, 0, + get_denied_layer(&dom, &access, &layer_masks, + sizeof(layer_masks))); + KUNIT_EXPECT_EQ(test, access, LANDLOCK_ACCESS_FS_EXECUTE); + + access =3D LANDLOCK_ACCESS_FS_READ_FILE; + KUNIT_EXPECT_EQ(test, 1, + get_denied_layer(&dom, &access, &layer_masks, + sizeof(layer_masks))); + KUNIT_EXPECT_EQ(test, access, LANDLOCK_ACCESS_FS_READ_FILE); + + access =3D LANDLOCK_ACCESS_FS_READ_DIR; + KUNIT_EXPECT_EQ(test, 1, + get_denied_layer(&dom, &access, &layer_masks, + sizeof(layer_masks))); + KUNIT_EXPECT_EQ(test, access, LANDLOCK_ACCESS_FS_READ_DIR); + + access =3D LANDLOCK_ACCESS_FS_READ_FILE | LANDLOCK_ACCESS_FS_READ_DIR; + KUNIT_EXPECT_EQ(test, 1, + get_denied_layer(&dom, &access, &layer_masks, + sizeof(layer_masks))); + KUNIT_EXPECT_EQ(test, access, + LANDLOCK_ACCESS_FS_READ_FILE | + LANDLOCK_ACCESS_FS_READ_DIR); + + access =3D LANDLOCK_ACCESS_FS_EXECUTE | LANDLOCK_ACCESS_FS_READ_DIR; + KUNIT_EXPECT_EQ(test, 1, + get_denied_layer(&dom, &access, &layer_masks, + sizeof(layer_masks))); + KUNIT_EXPECT_EQ(test, access, LANDLOCK_ACCESS_FS_READ_DIR); + + access =3D LANDLOCK_ACCESS_FS_WRITE_FILE; + KUNIT_EXPECT_EQ(test, 4, + get_denied_layer(&dom, &access, &layer_masks, + sizeof(layer_masks))); + KUNIT_EXPECT_EQ(test, access, 0); +} + +#endif /* CONFIG_SECURITY_LANDLOCK_KUNIT_TEST */ + static bool is_valid_request(const struct landlock_request *const request) { - if (WARN_ON_ONCE(!request->layer_plus_one)) + if (WARN_ON_ONCE(!(!!request->layer_plus_one ^ !!request->access))) + return false; + + if (request->access) { + if (WARN_ON_ONCE(!request->layer_masks)) + return false; + } else { + if (WARN_ON_ONCE(request->layer_masks)) + return false; + } + + if (WARN_ON_ONCE(!!request->layer_masks ^ !!request->layer_masks_size)) return false; =20 return true; @@ -137,6 +282,7 @@ void landlock_log_denial(const struct landlock_ruleset = *const domain, { struct audit_buffer *ab; struct landlock_hierarchy *youngest_denied; + access_mask_t missing; =20 if (WARN_ON_ONCE(!domain || !domain->hierarchy || !request)) return; @@ -152,9 +298,28 @@ void landlock_log_denial(const struct landlock_ruleset= *const domain, if (!ab) return; =20 - youngest_denied =3D get_hierarchy(domain, request->layer_plus_one - 1); + missing =3D request->access; + if (missing) { + size_t youngest_layer; + + /* Gets the nearest domain that denies the request. */ + if (request->layer_masks) { + youngest_layer =3D get_denied_layer( + domain, &missing, request->layer_masks, + request->layer_masks_size); + } else { + /* This will change with the next commit. */ + WARN_ON_ONCE(1); + youngest_layer =3D domain->num_layers; + } + youngest_denied =3D get_hierarchy(domain, youngest_layer); + } else { + youngest_denied =3D + get_hierarchy(domain, request->layer_plus_one - 1); + } + audit_log_format(ab, "domain=3D%llx blockers=3D", youngest_denied->id); - log_blockers(ab, request->type); + log_blockers(ab, request->type, missing); audit_log_lsm_data(ab, &request->audit); audit_log_end(ab); =20 @@ -200,6 +365,7 @@ void landlock_log_drop_domain(const struct landlock_rul= eset *const domain) static struct kunit_case test_cases[] =3D { /* clang-format off */ KUNIT_CASE(test_get_hierarchy), + KUNIT_CASE(test_get_denied_layer), {} /* clang-format on */ }; diff --git a/security/landlock/audit.h b/security/landlock/audit.h index 6f5ad04b83c2..25fc8333cddc 100644 --- a/security/landlock/audit.h +++ b/security/landlock/audit.h @@ -11,11 +11,13 @@ #include #include =20 +#include "access.h" #include "ruleset.h" =20 enum landlock_request_type { LANDLOCK_REQUEST_PTRACE =3D 1, LANDLOCK_REQUEST_FS_CHANGE_LAYOUT, + LANDLOCK_REQUEST_FS_ACCESS, }; =20 /* @@ -33,6 +35,13 @@ struct landlock_request { * extra one is useful to detect uninitialized field. */ size_t layer_plus_one; + + /* Required field for configurable access control. */ + access_mask_t access; + + /* Required fields for requests with layer masks. */ + const layer_mask_t (*layer_masks)[]; + size_t layer_masks_size; }; =20 #ifdef CONFIG_AUDIT diff --git a/security/landlock/fs.c b/security/landlock/fs.c index 01f9d5e78218..f38c7a60b9bf 100644 --- a/security/landlock/fs.c +++ b/security/landlock/fs.c @@ -730,6 +730,7 @@ static void test_is_eacces_with_write(struct kunit *con= st test) * those identified by @access_request_parent1). This matrix can * initially refer to domain layer masks and, when the accesses for the * destination and source are the same, to requested layer masks. + * @log_request_parent1: Audit request to fill if the related access is de= nied. * @dentry_child1: Dentry to the initial child of the parent1 path. This * pointer must be NULL for non-refer actions (i.e. not link nor renam= e). * @access_request_parent2: Similar to @access_request_parent1 but for a @@ -738,6 +739,7 @@ static void test_is_eacces_with_write(struct kunit *con= st test) * the source. Must be set to 0 when using a simple path request. * @layer_masks_parent2: Similar to @layer_masks_parent1 but for a refer * action. This must be NULL otherwise. + * @log_request_parent2: Audit request to fill if the related access is de= nied. * @dentry_child2: Dentry to the initial child of the parent2 path. This * pointer is only set for RENAME_EXCHANGE actions and must be NULL * otherwise. @@ -757,10 +759,12 @@ static bool is_access_to_paths_allowed( const struct path *const path, const access_mask_t access_request_parent1, layer_mask_t (*const layer_masks_parent1)[LANDLOCK_NUM_ACCESS_FS], - const struct dentry *const dentry_child1, + struct landlock_request *const log_request_parent1, + struct dentry *const dentry_child1, const access_mask_t access_request_parent2, layer_mask_t (*const layer_masks_parent2)[LANDLOCK_NUM_ACCESS_FS], - const struct dentry *const dentry_child2) + struct landlock_request *const log_request_parent2, + struct dentry *const dentry_child2) { bool allowed_parent1 =3D false, allowed_parent2 =3D false, is_dom_check, child1_is_directory =3D true, child2_is_directory =3D true; @@ -922,6 +926,25 @@ static bool is_access_to_paths_allowed( } path_put(&walker_path); =20 + if (!allowed_parent1) { + log_request_parent1->type =3D LANDLOCK_REQUEST_FS_ACCESS, + log_request_parent1->audit.type =3D LSM_AUDIT_DATA_PATH, + log_request_parent1->audit.u.path =3D *path; + log_request_parent1->access =3D access_masked_parent1; + log_request_parent1->layer_masks =3D layer_masks_parent1; + log_request_parent1->layer_masks_size =3D + ARRAY_SIZE(*layer_masks_parent1); + } + + if (!allowed_parent2) { + log_request_parent2->type =3D LANDLOCK_REQUEST_FS_ACCESS, + log_request_parent2->audit.type =3D LSM_AUDIT_DATA_PATH, + log_request_parent2->audit.u.path =3D *path; + log_request_parent2->access =3D access_masked_parent2; + log_request_parent2->layer_masks =3D layer_masks_parent2; + log_request_parent2->layer_masks_size =3D + ARRAY_SIZE(*layer_masks_parent2); + } return allowed_parent1 && allowed_parent2; } =20 @@ -930,6 +953,7 @@ static int current_check_access_path(const struct path = *const path, { const struct landlock_ruleset *const dom =3D get_current_fs_domain(); layer_mask_t layer_masks[LANDLOCK_NUM_ACCESS_FS] =3D {}; + struct landlock_request request =3D {}; =20 if (!dom) return 0; @@ -937,9 +961,10 @@ static int current_check_access_path(const struct path= *const path, access_request =3D landlock_init_layer_masks( dom, access_request, &layer_masks, LANDLOCK_KEY_INODE); if (is_access_to_paths_allowed(dom, path, access_request, &layer_masks, - NULL, 0, NULL, NULL)) + &request, NULL, 0, NULL, NULL, NULL)) return 0; =20 + landlock_log_denial(dom, &request); return -EACCES; } =20 @@ -1108,6 +1133,7 @@ static int current_check_refer_path(struct dentry *co= nst old_dentry, struct dentry *old_parent; layer_mask_t layer_masks_parent1[LANDLOCK_NUM_ACCESS_FS] =3D {}, layer_masks_parent2[LANDLOCK_NUM_ACCESS_FS] =3D {}; + struct landlock_request request1 =3D {}, request2 =3D {}; =20 if (!dom) return 0; @@ -1139,10 +1165,13 @@ static int current_check_refer_path(struct dentry *= const old_dentry, access_request_parent1 =3D landlock_init_layer_masks( dom, access_request_parent1 | access_request_parent2, &layer_masks_parent1, LANDLOCK_KEY_INODE); - if (is_access_to_paths_allowed( - dom, new_dir, access_request_parent1, - &layer_masks_parent1, NULL, 0, NULL, NULL)) + if (is_access_to_paths_allowed(dom, new_dir, + access_request_parent1, + &layer_masks_parent1, &request1, + NULL, 0, NULL, NULL, NULL)) return 0; + + landlock_log_denial(dom, &request1); return -EACCES; } =20 @@ -1177,12 +1206,22 @@ static int current_check_refer_path(struct dentry *= const old_dentry, * parent access rights. This will be useful to compare with the * destination parent access rights. */ - if (is_access_to_paths_allowed( - dom, &mnt_dir, access_request_parent1, &layer_masks_parent1, - old_dentry, access_request_parent2, &layer_masks_parent2, - exchange ? new_dentry : NULL)) + if (is_access_to_paths_allowed(dom, &mnt_dir, access_request_parent1, + &layer_masks_parent1, &request1, + old_dentry, access_request_parent2, + &layer_masks_parent2, &request2, + exchange ? new_dentry : NULL)) return 0; =20 + if (request1.access) { + request1.audit.u.path.dentry =3D old_parent; + landlock_log_denial(dom, &request1); + } + if (request2.access) { + request2.audit.u.path.dentry =3D new_dir->dentry; + landlock_log_denial(dom, &request2); + } + /* * This prioritizes EACCES over EXDEV for all actions, including * renames with RENAME_EXCHANGE. @@ -1562,6 +1601,7 @@ static int hook_file_open(struct file *const file) const struct landlock_ruleset *const dom =3D landlock_get_applicable_domain( landlock_cred(file->f_cred)->domain, any_fs); + struct landlock_request request =3D {}; =20 if (!dom) return 0; @@ -1587,7 +1627,7 @@ static int hook_file_open(struct file *const file) dom, &file->f_path, landlock_init_layer_masks(dom, full_access_request, &layer_masks, LANDLOCK_KEY_INODE), - &layer_masks, NULL, 0, NULL, NULL)) { + &layer_masks, &request, NULL, 0, NULL, NULL, NULL)) { allowed_access =3D full_access_request; } else { unsigned long access_bit; @@ -1617,6 +1657,9 @@ static int hook_file_open(struct file *const file) if ((open_access_request & allowed_access) =3D=3D open_access_request) return 0; =20 + /* Sets access to reflect the actual request. */ + request.access =3D open_access_request; + landlock_log_denial(dom, &request); return -EACCES; } =20 --=20 2.47.0 From nobody Tue Nov 26 01:29:03 2024 Received: from smtp-42aa.mail.infomaniak.ch (smtp-42aa.mail.infomaniak.ch [84.16.66.170]) (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 51F161DEFC7 for ; Fri, 22 Nov 2024 14:34:25 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=84.16.66.170 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1732286068; cv=none; b=JPk/kze6q4+dTzcoDasW91xNAAfn015WuQOaUz2JocWE0HWK2ZGKD/yPtZXFg5LFPvyicWjXSvl0iEfR11ockxyzGh4N9FF2zPVzK+7OIL6936KaH3WnUHqkKOkf486Elmm7VAiKBxAPv+lEOQiL7CcS5eLePiqLvRORIFvBG/U= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1732286068; c=relaxed/simple; bh=NXBj20PThBt2iYjCNczmjasmi25mDb//0DutqK1ZARY=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=FiW0w+B0UF6xiBlz+OGBeipQhIBC+1it9KguUTYHb4b4fRA8sGCX8MaXvzv8G7AEd9i10dOlUmDgGbhkfUf6EUWCzyZCxzgIqs/NB/sxN7EYVioDiVZHDcLK9bqvT5v2p0ZeNZP5tpLkmu3B5G7J51DpTeFi7v6/wgYfWA3BdRo= 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=fvB/RVu5; arc=none smtp.client-ip=84.16.66.170 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="fvB/RVu5" Received: from smtp-3-0000.mail.infomaniak.ch (smtp-3-0000.mail.infomaniak.ch [10.4.36.107]) by smtp-3-3000.mail.infomaniak.ch (Postfix) with ESMTPS id 4XvyKW64hpzZt2; Fri, 22 Nov 2024 15:34:23 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=digikod.net; s=20191114; t=1732286063; bh=5WW+XuNU4PWZC+NUFAz/gM/xUoHdoTRRxfIsHVe7bck=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=fvB/RVu52UfxBRXo+4Z+quhLghOUgKVFPZCkPm461OE+ub1R0VcVpmHXDpOBxQ+hS Kmr0k3l8xz+8MhHsDG1nbko4Moijj4JjifcSM5C9jNYeBX4L2GDzfgSpvfSiDYLKR7 VJnYqnAe8x7bv0//AGd4h+EVTCUZCwqkdg7jCTPc= Received: from unknown by smtp-3-0000.mail.infomaniak.ch (Postfix) with ESMTPA id 4XvyKW1wzCzjZ3; Fri, 22 Nov 2024 15:34:23 +0100 (CET) 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 , Francis Laniel , James Morris , Jann Horn , Jeff Xu , Jorge Lucangeli Obes , Kees Cook , Konstantin Meskhidze , Matt Bobrowski , Mikhail Ivanov , Phil Sutter , 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: [PATCH v3 16/23] landlock: Log truncate and ioctl denials Date: Fri, 22 Nov 2024 15:33:46 +0100 Message-ID: <20241122143353.59367-17-mic@digikod.net> In-Reply-To: <20241122143353.59367-1-mic@digikod.net> References: <20241122143353.59367-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 the file_truncate and file_ioctl hooks. Add a deny_masks_t type and related helpers to store the domain's layer level per optional access rights (i.e. FS_TRUNCATE and FS_IOCTL_DEV) when opening a file, which cannot be inferred later. Add KUnit tests. Audit event sample: type=3DLL_DENY [...]: domain=3D195ba459b blockers=3Dfs_ioctl_dev path=3D"= /dev/tty" dev=3D"devtmpfs" ino=3D9 Cc: G=C3=BCnther Noack Signed-off-by: Micka=C3=ABl Sala=C3=BCn Link: https://lore.kernel.org/r/20241122143353.59367-17-mic@digikod.net --- Changes since v2: - Fix !CONFIG_AUDIT build warning. - Rename ACCESS_FS_OPTIONAL to _LANDLOCK_ACCESS_FS_OPTIONAL. --- security/landlock/access.h | 23 +++++++ security/landlock/audit.c | 102 ++++++++++++++++++++++++++-- security/landlock/audit.h | 4 ++ security/landlock/domain.c | 133 +++++++++++++++++++++++++++++++++++++ security/landlock/domain.h | 8 +++ security/landlock/fs.c | 51 ++++++++++++++ security/landlock/fs.h | 9 +++ 7 files changed, 325 insertions(+), 5 deletions(-) diff --git a/security/landlock/access.h b/security/landlock/access.h index 1e302775cc23..99125c7f9732 100644 --- a/security/landlock/access.h +++ b/security/landlock/access.h @@ -28,6 +28,12 @@ LANDLOCK_ACCESS_FS_REFER) /* clang-format on */ =20 +/* clang-format off */ +#define _LANDLOCK_ACCESS_FS_OPTIONAL ( \ + LANDLOCK_ACCESS_FS_TRUNCATE | \ + LANDLOCK_ACCESS_FS_IOCTL_DEV) +/* clang-format on */ + typedef u16 access_mask_t; =20 /* Makes sure all filesystem access rights can be stored. */ @@ -60,6 +66,23 @@ typedef u16 layer_mask_t; /* Makes sure all layers can be checked. */ static_assert(BITS_PER_TYPE(layer_mask_t) >=3D LANDLOCK_MAX_NUM_LAYERS); =20 +/* + * Tracks domains responsible of a denied access. This is required to avo= id + * storing in each object the full layer_masks[] required by update_reques= t(). + */ +typedef u8 deny_masks_t; + +/* + * Makes sure all optional access rights can be tied to a layer index (cf. + * get_deny_mask). + */ +static_assert(BITS_PER_TYPE(deny_masks_t) >=3D + (HWEIGHT(LANDLOCK_MAX_NUM_LAYERS - 1) * + HWEIGHT(_LANDLOCK_ACCESS_FS_OPTIONAL))); + +/* LANDLOCK_MAX_NUM_LAYERS must be a power of two (cf. deny_masks_t assert= ). */ +static_assert(HWEIGHT(LANDLOCK_MAX_NUM_LAYERS) =3D=3D 1); + /* Upgrades with all initially denied by default access rights. */ static inline struct access_masks landlock_upgrade_handled_access_masks(struct access_masks access_masks) diff --git a/security/landlock/audit.c b/security/landlock/audit.c index 8a7657d12478..e51c8ea14aec 100644 --- a/security/landlock/audit.c +++ b/security/landlock/audit.c @@ -13,10 +13,12 @@ #include #include =20 +#include "access.h" #include "audit.h" #include "common.h" #include "cred.h" #include "domain.h" +#include "fs.h" #include "ruleset.h" =20 static const char *const fs_access_strings[] =3D { @@ -252,22 +254,111 @@ static void test_get_denied_layer(struct kunit *cons= t test) =20 #endif /* CONFIG_SECURITY_LANDLOCK_KUNIT_TEST */ =20 +static size_t +get_level_from_deny_masks(access_mask_t *const access_request, + const access_mask_t all_existing_optional_access, + const deny_masks_t deny_masks) +{ + const unsigned long access_opt =3D all_existing_optional_access; + const unsigned long access_req =3D *access_request; + access_mask_t missing =3D 0; + size_t youngest_layer =3D 0; + size_t access_index =3D 0; + unsigned long access_bit; + + /* This will require change with new object types. */ + WARN_ON_ONCE(access_opt !=3D _LANDLOCK_ACCESS_FS_OPTIONAL); + + for_each_set_bit(access_bit, &access_opt, + BITS_PER_TYPE(access_mask_t)) { + if (access_req & BIT(access_bit)) { + const size_t layer =3D + (deny_masks >> (access_index * 4)) & + (LANDLOCK_MAX_NUM_LAYERS - 1); + + if (layer > youngest_layer) { + youngest_layer =3D layer; + missing =3D BIT(access_bit); + } else if (layer =3D=3D youngest_layer) { + missing |=3D BIT(access_bit); + } + } + access_index++; + } + + *access_request =3D missing; + return youngest_layer; +} + +#ifdef CONFIG_SECURITY_LANDLOCK_KUNIT_TEST + +static void test_get_level_from_deny_masks(struct kunit *const test) +{ + deny_masks_t deny_mask; + access_mask_t access; + + /* truncate:0 ioctl_dev:2 */ + deny_mask =3D 0x20; + + access =3D LANDLOCK_ACCESS_FS_TRUNCATE; + KUNIT_EXPECT_EQ(test, 0, + get_level_from_deny_masks(&access, + _LANDLOCK_ACCESS_FS_OPTIONAL, + deny_mask)); + KUNIT_EXPECT_EQ(test, access, LANDLOCK_ACCESS_FS_TRUNCATE); + + access =3D LANDLOCK_ACCESS_FS_TRUNCATE | LANDLOCK_ACCESS_FS_IOCTL_DEV; + KUNIT_EXPECT_EQ(test, 2, + get_level_from_deny_masks(&access, + _LANDLOCK_ACCESS_FS_OPTIONAL, + deny_mask)); + KUNIT_EXPECT_EQ(test, access, LANDLOCK_ACCESS_FS_IOCTL_DEV); + + /* truncate:15 ioctl_dev:15 */ + deny_mask =3D 0xff; + + access =3D LANDLOCK_ACCESS_FS_TRUNCATE; + KUNIT_EXPECT_EQ(test, 15, + get_level_from_deny_masks(&access, + _LANDLOCK_ACCESS_FS_OPTIONAL, + deny_mask)); + KUNIT_EXPECT_EQ(test, access, LANDLOCK_ACCESS_FS_TRUNCATE); + + access =3D LANDLOCK_ACCESS_FS_TRUNCATE | LANDLOCK_ACCESS_FS_IOCTL_DEV; + KUNIT_EXPECT_EQ(test, 15, + get_level_from_deny_masks(&access, + _LANDLOCK_ACCESS_FS_OPTIONAL, + deny_mask)); + KUNIT_EXPECT_EQ(test, access, + LANDLOCK_ACCESS_FS_TRUNCATE | + LANDLOCK_ACCESS_FS_IOCTL_DEV); +} + +#endif /* CONFIG_SECURITY_LANDLOCK_KUNIT_TEST */ + static bool is_valid_request(const struct landlock_request *const request) { if (WARN_ON_ONCE(!(!!request->layer_plus_one ^ !!request->access))) return false; =20 if (request->access) { - if (WARN_ON_ONCE(!request->layer_masks)) + if (WARN_ON_ONCE(!(!!request->layer_masks ^ + !!request->all_existing_optional_access))) return false; } else { - if (WARN_ON_ONCE(request->layer_masks)) + if (WARN_ON_ONCE(request->layer_masks || + request->all_existing_optional_access)) return false; } =20 if (WARN_ON_ONCE(!!request->layer_masks ^ !!request->layer_masks_size)) return false; =20 + if (request->deny_masks) { + if (WARN_ON_ONCE(!request->all_existing_optional_access)) + return false; + } + return true; } =20 @@ -308,9 +399,9 @@ void landlock_log_denial(const struct landlock_ruleset = *const domain, domain, &missing, request->layer_masks, request->layer_masks_size); } else { - /* This will change with the next commit. */ - WARN_ON_ONCE(1); - youngest_layer =3D domain->num_layers; + youngest_layer =3D get_level_from_deny_masks( + &missing, request->all_existing_optional_access, + request->deny_masks); } youngest_denied =3D get_hierarchy(domain, youngest_layer); } else { @@ -366,6 +457,7 @@ static struct kunit_case test_cases[] =3D { /* clang-format off */ KUNIT_CASE(test_get_hierarchy), KUNIT_CASE(test_get_denied_layer), + KUNIT_CASE(test_get_level_from_deny_masks), {} /* clang-format on */ }; diff --git a/security/landlock/audit.h b/security/landlock/audit.h index 25fc8333cddc..320394fd6b84 100644 --- a/security/landlock/audit.h +++ b/security/landlock/audit.h @@ -42,6 +42,10 @@ struct landlock_request { /* Required fields for requests with layer masks. */ const layer_mask_t (*layer_masks)[]; size_t layer_masks_size; + + /* Required fields for requests with deny masks. */ + const access_mask_t all_existing_optional_access; + deny_masks_t deny_masks; }; =20 #ifdef CONFIG_AUDIT diff --git a/security/landlock/domain.c b/security/landlock/domain.c index 69b15c059583..4b3b82c88815 100644 --- a/security/landlock/domain.c +++ b/security/landlock/domain.c @@ -5,7 +5,10 @@ * Copyright =C2=A9 2024 Microsoft Corporation */ =20 +#include #include +#include +#include #include #include #include @@ -14,6 +17,8 @@ #include #include =20 +#include "access.h" +#include "common.h" #include "domain.h" #include "id.h" =20 @@ -60,3 +65,131 @@ void landlock_init_current_hierarchy(struct landlock_hi= erarchy *const hierarchy) hierarchy->exe =3D get_current_exe(); get_task_comm(hierarchy->comm, current); } + +static deny_masks_t +get_layer_deny_mask(const access_mask_t all_existing_optional_access, + const unsigned long access_bit, const size_t layer) +{ + unsigned long access_weight; + + /* This may require change with new object types. */ + WARN_ON_ONCE(all_existing_optional_access !=3D + _LANDLOCK_ACCESS_FS_OPTIONAL); + + if (WARN_ON_ONCE(layer >=3D LANDLOCK_MAX_NUM_LAYERS)) + return 0; + + access_weight =3D hweight_long(all_existing_optional_access & + GENMASK(access_bit, 0)); + if (WARN_ON_ONCE(access_weight < 1)) + return 0; + + return layer + << ((access_weight - 1) * HWEIGHT(LANDLOCK_MAX_NUM_LAYERS - 1)); +} + +#ifdef CONFIG_SECURITY_LANDLOCK_KUNIT_TEST + +static void test_get_layer_deny_mask(struct kunit *const test) +{ + const unsigned long truncate =3D BIT_INDEX(LANDLOCK_ACCESS_FS_TRUNCATE); + const unsigned long ioctl_dev =3D BIT_INDEX(LANDLOCK_ACCESS_FS_IOCTL_DEV); + + KUNIT_EXPECT_EQ(test, 0, + get_layer_deny_mask(_LANDLOCK_ACCESS_FS_OPTIONAL, + truncate, 0)); + KUNIT_EXPECT_EQ(test, 0x3, + get_layer_deny_mask(_LANDLOCK_ACCESS_FS_OPTIONAL, + truncate, 3)); + + KUNIT_EXPECT_EQ(test, 0, + get_layer_deny_mask(_LANDLOCK_ACCESS_FS_OPTIONAL, + ioctl_dev, 0)); + KUNIT_EXPECT_EQ(test, 0xf0, + get_layer_deny_mask(_LANDLOCK_ACCESS_FS_OPTIONAL, + ioctl_dev, 15)); +} + +#endif /* CONFIG_SECURITY_LANDLOCK_KUNIT_TEST */ + +deny_masks_t +landlock_get_deny_masks(const access_mask_t all_existing_optional_access, + const access_mask_t optional_access, + const layer_mask_t (*const layer_masks)[], + const size_t layer_masks_size) +{ + const unsigned long access_opt =3D optional_access; + unsigned long access_bit; + deny_masks_t deny_masks =3D 0; + + /* This may require change with new object types. */ + WARN_ON_ONCE(access_opt !=3D + (optional_access & all_existing_optional_access)); + + if (WARN_ON_ONCE(!layer_masks)) + return 0; + + if (WARN_ON_ONCE(!access_opt)) + return 0; + + for_each_set_bit(access_bit, &access_opt, layer_masks_size) { + const layer_mask_t mask =3D (*layer_masks)[access_bit]; + + if (!mask) + continue; + + /* __fls(1) =3D=3D 0 */ + deny_masks |=3D get_layer_deny_mask(all_existing_optional_access, + access_bit, __fls(mask)); + } + return deny_masks; +} + +#ifdef CONFIG_SECURITY_LANDLOCK_KUNIT_TEST + +static void test_landlock_get_deny_masks(struct kunit *const test) +{ + const layer_mask_t layers1[BITS_PER_TYPE(access_mask_t)] =3D { + [BIT_INDEX(LANDLOCK_ACCESS_FS_EXECUTE)] =3D BIT_ULL(0) | + BIT_ULL(9), + [BIT_INDEX(LANDLOCK_ACCESS_FS_TRUNCATE)] =3D BIT_ULL(1), + [BIT_INDEX(LANDLOCK_ACCESS_FS_IOCTL_DEV)] =3D BIT_ULL(2) | + BIT_ULL(0), + }; + + KUNIT_EXPECT_EQ(test, 0x1, + landlock_get_deny_masks(_LANDLOCK_ACCESS_FS_OPTIONAL, + LANDLOCK_ACCESS_FS_TRUNCATE, + &layers1, ARRAY_SIZE(layers1))); + KUNIT_EXPECT_EQ(test, 0x20, + landlock_get_deny_masks(_LANDLOCK_ACCESS_FS_OPTIONAL, + LANDLOCK_ACCESS_FS_IOCTL_DEV, + &layers1, ARRAY_SIZE(layers1))); + KUNIT_EXPECT_EQ( + test, 0x21, + landlock_get_deny_masks(_LANDLOCK_ACCESS_FS_OPTIONAL, + LANDLOCK_ACCESS_FS_TRUNCATE | + LANDLOCK_ACCESS_FS_IOCTL_DEV, + &layers1, ARRAY_SIZE(layers1))); +} + +#endif /* CONFIG_SECURITY_LANDLOCK_KUNIT_TEST */ + +#ifdef CONFIG_SECURITY_LANDLOCK_KUNIT_TEST + +static struct kunit_case test_cases[] =3D { + /* clang-format off */ + KUNIT_CASE(test_get_layer_deny_mask), + KUNIT_CASE(test_landlock_get_deny_masks), + {} + /* clang-format on */ +}; + +static struct kunit_suite test_suite =3D { + .name =3D "landlock_domain", + .test_cases =3D test_cases, +}; + +kunit_test_suite(test_suite); + +#endif /* CONFIG_SECURITY_LANDLOCK_KUNIT_TEST */ diff --git a/security/landlock/domain.h b/security/landlock/domain.h index 4a2864a66047..ffc8b1bb58be 100644 --- a/security/landlock/domain.h +++ b/security/landlock/domain.h @@ -18,6 +18,8 @@ #include #include =20 +#include "access.h" + enum landlock_log_status { LANDLOCK_LOG_PENDING =3D 0, LANDLOCK_LOG_RECORDED, @@ -98,6 +100,12 @@ static inline void landlock_put_hierarchy(struct landlo= ck_hierarchy *hierarchy) =20 #ifdef CONFIG_AUDIT void landlock_init_current_hierarchy(struct landlock_hierarchy *const hier= archy); + +deny_masks_t +landlock_get_deny_masks(const access_mask_t all_existing_optional_access, + const access_mask_t optional_access, + const layer_mask_t (*const layer_masks)[], + size_t layer_masks_size); #else /* CONFIG_AUDIT */ static inline void landlock_init_current_hierarchy(struct landlock_hierarchy *const hierarchy) diff --git a/security/landlock/fs.c b/security/landlock/fs.c index f38c7a60b9bf..6404961ecbc7 100644 --- a/security/landlock/fs.c +++ b/security/landlock/fs.c @@ -246,6 +246,17 @@ is_masked_device_ioctl_compat(const unsigned int cmd) } } =20 +static void +update_request(struct landlock_request *const request, + const struct landlock_file_security *const file_security, + const access_mask_t access) +{ + request->access =3D access; +#ifdef CONFIG_AUDIT + request->deny_masks =3D file_security->deny_masks; +#endif /* CONFIG_AUDIT */ +} + /* Ruleset management */ =20 static struct landlock_object *get_inode_object(struct inode *const inode) @@ -1653,6 +1664,11 @@ static int hook_file_open(struct file *const file) * file access rights in the opened struct file. */ landlock_file(file)->allowed_access =3D allowed_access; +#ifdef CONFIG_AUDIT + landlock_file(file)->deny_masks =3D landlock_get_deny_masks( + _LANDLOCK_ACCESS_FS_OPTIONAL, optional_access, &layer_masks, + ARRAY_SIZE(layer_masks)); +#endif /* CONFIG_AUDIT */ =20 if ((open_access_request & allowed_access) =3D=3D open_access_request) return 0; @@ -1665,6 +1681,15 @@ static int hook_file_open(struct file *const file) =20 static int hook_file_truncate(struct file *const file) { + struct landlock_request request =3D { + .type =3D LANDLOCK_REQUEST_FS_ACCESS, + .audit =3D { + .type =3D LSM_AUDIT_DATA_FILE, + .u.file =3D file, + }, + .all_existing_optional_access =3D _LANDLOCK_ACCESS_FS_OPTIONAL, + }; + /* * Allows truncation if the truncate right was available at the time of * opening the file, to get a consistent access check as for read, write @@ -1677,12 +1702,24 @@ static int hook_file_truncate(struct file *const fi= le) */ if (landlock_file(file)->allowed_access & LANDLOCK_ACCESS_FS_TRUNCATE) return 0; + + update_request(&request, landlock_file(file), + LANDLOCK_ACCESS_FS_TRUNCATE); + landlock_log_denial(landlock_cred(file->f_cred)->domain, &request); return -EACCES; } =20 static int hook_file_ioctl(struct file *file, unsigned int cmd, unsigned long arg) { + struct landlock_request request =3D { + .type =3D LANDLOCK_REQUEST_FS_ACCESS, + .audit =3D { + .type =3D LSM_AUDIT_DATA_FILE, + .u.file =3D file, + }, + .all_existing_optional_access =3D _LANDLOCK_ACCESS_FS_OPTIONAL, + }; access_mask_t allowed_access =3D landlock_file(file)->allowed_access; =20 /* @@ -1700,12 +1737,23 @@ static int hook_file_ioctl(struct file *file, unsig= ned int cmd, if (is_masked_device_ioctl(cmd)) return 0; =20 + update_request(&request, landlock_file(file), + LANDLOCK_ACCESS_FS_IOCTL_DEV); + landlock_log_denial(landlock_cred(file->f_cred)->domain, &request); return -EACCES; } =20 static int hook_file_ioctl_compat(struct file *file, unsigned int cmd, unsigned long arg) { + struct landlock_request request =3D { + .type =3D LANDLOCK_REQUEST_FS_ACCESS, + .audit =3D { + .type =3D LSM_AUDIT_DATA_FILE, + .u.file =3D file, + }, + .all_existing_optional_access =3D _LANDLOCK_ACCESS_FS_OPTIONAL, + }; access_mask_t allowed_access =3D landlock_file(file)->allowed_access; =20 /* @@ -1723,6 +1771,9 @@ static int hook_file_ioctl_compat(struct file *file, = unsigned int cmd, if (is_masked_device_ioctl_compat(cmd)) return 0; =20 + update_request(&request, landlock_file(file), + LANDLOCK_ACCESS_FS_IOCTL_DEV); + landlock_log_denial(landlock_cred(file->f_cred)->domain, &request); return -EACCES; } =20 diff --git a/security/landlock/fs.h b/security/landlock/fs.h index d445f411c26a..9f52c9b37898 100644 --- a/security/landlock/fs.h +++ b/security/landlock/fs.h @@ -53,6 +53,15 @@ struct landlock_file_security { * needed to authorize later operations on the open file. */ access_mask_t allowed_access; + +#ifdef CONFIG_AUDIT + /** + * @deny_masks: Domain layer levels that deny an optional access (see + * _LANDLOCK_ACCESS_FS_OPTIONAL). + */ + deny_masks_t deny_masks; +#endif /* CONFIG_AUDIT */ + /** * @fown_domain: Domain of the task that set the PID that may receive a * signal e.g., SIGURG when writing MSG_OOB to the related socket. --=20 2.47.0 From nobody Tue Nov 26 01:29:03 2024 Received: from smtp-8fa9.mail.infomaniak.ch (smtp-8fa9.mail.infomaniak.ch [83.166.143.169]) (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 4B07C1E0090 for ; Fri, 22 Nov 2024 14:34:32 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=83.166.143.169 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1732286075; cv=none; b=cghoF8Bkwa5gwxdD2hW5BfFBZuHpCJQDCmAERQa8fu8QHMxGDkDSybF90EsUlAQ40vO0gFKsYDgTHEMnAszQerByNXpI7TzZwfjAJMbAefXcwL7fbH6HHDb5aQUYxXaOLAZMfRd09eSdfoVgvLPuGqWmssOECKIJBENCG16Ybko= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1732286075; c=relaxed/simple; bh=QsQSxv/WhvBRrMYFrV2ICnawqtPw5uSoPLiDFGPPsts=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=VuwmG9IEFWfY819zW9zXWfmBi1VIM5L53koiaoBrNvNFvx8p1wRDTBZ15CNK9F+TV3kp07dItEy5UpTbkXwD15jWfWWGIKIfGv9zGL0Kb5R6bixqTkUNQ3dDzDDaVHudtpMMQajqNjJV/nCEmYXrX3A0WBNRpOJsOt4xa9YwzMs= 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=oS+QpsIm; arc=none smtp.client-ip=83.166.143.169 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="oS+QpsIm" Received: from smtp-4-0000.mail.infomaniak.ch (unknown [IPv6:2001:1600:7:10:40ca:feff:fe05:0]) by smtp-4-3000.mail.infomaniak.ch (Postfix) with ESMTPS id 4XvyKX6w3nzbGs; Fri, 22 Nov 2024 15:34:24 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=digikod.net; s=20191114; t=1732286064; bh=V/5g48g0Gz7yzpagv3LBm4Owx7G0nGyyHFJUl5/sdwQ=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=oS+QpsIm2Z1nhmo+zVYslCKyOz1qolUI0ySHK6keG+w6NZ8/TE6eW9xhEFDVeT1EH 6aFcR2jXr2dQfpRt6IiRl5Vm4nNoqUAZjkfVuxZbLeG/GvtiImtPEfjDHR9vnnxg9N h+4GkQUHEhFXUOUlyg9KgFzNtKZ+6cJHBUR4wDLA= Received: from unknown by smtp-4-0000.mail.infomaniak.ch (Postfix) with ESMTPA id 4XvyKX23VTzmLr; Fri, 22 Nov 2024 15:34:24 +0100 (CET) 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 , Francis Laniel , James Morris , Jann Horn , Jeff Xu , Jorge Lucangeli Obes , Kees Cook , Konstantin Meskhidze , Matt Bobrowski , Mikhail Ivanov , Phil Sutter , 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: [PATCH v3 17/23] landlock: Log TCP bind and connect denials Date: Fri, 22 Nov 2024 15:33:47 +0100 Message-ID: <20241122143353.59367-18-mic@digikod.net> In-Reply-To: <20241122143353.59367-1-mic@digikod.net> References: <20241122143353.59367-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 event sample: type=3DLL_DENY [...]: domain=3D195ba459b blockers=3Dnet_connect_tcp daddr= =3D127.0.0.1 dest=3D80 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/20241122143353.59367-18-mic@digikod.net --- Changes since v2: - Remove potentially superfluous IPv6 saddr log, spotted by Francis Laniel. - Cosmetic improvements. --- security/landlock/audit.c | 12 +++++++++ security/landlock/audit.h | 1 + security/landlock/net.c | 51 ++++++++++++++++++++++++++++++++++++--- 3 files changed, 60 insertions(+), 4 deletions(-) diff --git a/security/landlock/audit.c b/security/landlock/audit.c index e51c8ea14aec..0b69b4c9ba1c 100644 --- a/security/landlock/audit.c +++ b/security/landlock/audit.c @@ -42,6 +42,13 @@ static const char *const fs_access_strings[] =3D { =20 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) @@ -59,6 +66,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 d5dcc4407a19..45c80fa417c4 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" @@ -57,6 +59,10 @@ static int current_check_access_socket(struct socket *co= nst sock, const struct landlock_ruleset *const dom =3D landlock_get_applicable_domain(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; @@ -73,18 +79,48 @@ 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; + + 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: @@ -153,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 From nobody Tue Nov 26 01:29:03 2024 Received: from smtp-190f.mail.infomaniak.ch (smtp-190f.mail.infomaniak.ch [185.125.25.15]) (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 30A861E00B6 for ; Fri, 22 Nov 2024 14:34:33 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=185.125.25.15 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1732286076; cv=none; b=DhQTGg2y0P/cXAngNgBV5EJF50GsHQy3HfE3v78FuZ6N4quVI9iBzFhd/0cAXXNVvYtUK9Hcq+6yIA2xFd9L7AQvni47HC4TkKUuLK+fIHFGBnCF1o3weiKewwdlBrp7kZQTpr021DBrzV4rnkyYwzgdMR5PjiNtklUpXJ6fVMw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1732286076; c=relaxed/simple; bh=USmnvWm36M0c97ARvRM1m0YWzludoSF5lONc+2fkOzQ=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=i2ighUNR0NRLdroXfa5Tnf9Cm6GGu3IWvv7Z84/5Yk6bumHnOdGRW053tlzRd9fVKvUfEPb2oSRCr1BOe9+0T6GzFZluHtHhtdgtk6P9/71js9fKD13hfH7ymet1UErNDbXVwCXBH/tnvpyRh+4/oZUE5XyKIfmezAaPSfrpOH8= 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=mT6MCM9n; arc=none smtp.client-ip=185.125.25.15 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="mT6MCM9n" Received: from smtp-4-0000.mail.infomaniak.ch (unknown [IPv6:2001:1600:7:10:40ca:feff:fe05:0]) by smtp-4-3000.mail.infomaniak.ch (Postfix) with ESMTPS id 4XvyKZ1P31zbc9; Fri, 22 Nov 2024 15:34:26 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=digikod.net; s=20191114; t=1732286066; bh=gtx2VSieXbkLGqKYte00kz6TjPK2PPAbtO/Q3N+TNSo=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=mT6MCM9nkxU8z+5Y26G/Wi7fnFuj915tXQrregzkvu3mhsHilLn0qS6W7sqkGvaoW z019AbC6c9D5LB6Obv8VKDH6E0vHGNYxEkPC1VZAUT4/WWACPCZbloYf+uyLfDOKNW 8TqAILm1p7L7Nkt2UQdjl9GTH97dMWKP1E/F301E= Received: from unknown by smtp-4-0000.mail.infomaniak.ch (Postfix) with ESMTPA id 4XvyKY2w7mzlpc; Fri, 22 Nov 2024 15:34:25 +0100 (CET) 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 , Francis Laniel , James Morris , Jann Horn , Jeff Xu , Jorge Lucangeli Obes , Kees Cook , Konstantin Meskhidze , Matt Bobrowski , Mikhail Ivanov , Phil Sutter , 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: [PATCH v3 18/23] landlock: Log scoped denials Date: Fri, 22 Nov 2024 15:33:48 +0100 Message-ID: <20241122143353.59367-19-mic@digikod.net> In-Reply-To: <20241122143353.59367-1-mic@digikod.net> References: <20241122143353.59367-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 for unix_stream_connect, unix_may_send, task_kill, and file_send_sigiotask hooks. Audit event sample: type=3DLL_DENY [...]: domain=3D195ba459b blockers=3Dscope_abstract_unix_s= ocket path=3D00666F6F Cc: G=C3=BCnther Noack Cc: Tahera Fahimi Signed-off-by: Micka=C3=ABl Sala=C3=BCn Link: https://lore.kernel.org/r/20241122143353.59367-19-mic@digikod.net --- Changes since v1: - New patch. --- security/landlock/audit.c | 8 ++++++ security/landlock/audit.h | 2 ++ security/landlock/task.c | 58 ++++++++++++++++++++++++++++++++++++--- 3 files changed, 64 insertions(+), 4 deletions(-) diff --git a/security/landlock/audit.c b/security/landlock/audit.c index 0b69b4c9ba1c..7e4acc746cd6 100644 --- a/security/landlock/audit.c +++ b/security/landlock/audit.c @@ -71,6 +71,14 @@ get_blocker(const enum landlock_request_type type, if (WARN_ON_ONCE(access_bit >=3D ARRAY_SIZE(net_access_strings))) return "unknown"; return net_access_strings[access_bit]; + + case LANDLOCK_REQUEST_SCOPE_ABSTRACT_UNIX_SOCKET: + WARN_ON_ONCE(access_bit !=3D -1); + return "scope_abstract_unix_socket"; + + case LANDLOCK_REQUEST_SCOPE_SIGNAL: + WARN_ON_ONCE(access_bit !=3D -1); + return "scope_signal"; } =20 WARN_ON_ONCE(1); diff --git a/security/landlock/audit.h b/security/landlock/audit.h index 1075b0c8401f..1e0a9164bacc 100644 --- a/security/landlock/audit.h +++ b/security/landlock/audit.h @@ -19,6 +19,8 @@ enum landlock_request_type { LANDLOCK_REQUEST_FS_CHANGE_LAYOUT, LANDLOCK_REQUEST_FS_ACCESS, LANDLOCK_REQUEST_NET_ACCESS, + LANDLOCK_REQUEST_SCOPE_ABSTRACT_UNIX_SOCKET, + LANDLOCK_REQUEST_SCOPE_SIGNAL, }; =20 /* diff --git a/security/landlock/task.c b/security/landlock/task.c index 1decd6f114e8..bdf7adc14e32 100644 --- a/security/landlock/task.c +++ b/security/landlock/task.c @@ -263,13 +263,27 @@ static int hook_unix_stream_connect(struct sock *cons= t sock, const struct landlock_ruleset *const dom =3D landlock_get_applicable_domain(landlock_get_current_domain(), unix_scope); + struct lsm_network_audit audit_net =3D { + .sk =3D other, + }; + struct landlock_request request =3D { + .type =3D LANDLOCK_REQUEST_SCOPE_ABSTRACT_UNIX_SOCKET, + .audit =3D { + .type =3D LSM_AUDIT_DATA_NET, + .u.net =3D &audit_net, + }, + }; =20 /* Quick return for non-landlocked tasks. */ if (!dom) return 0; =20 - if (is_abstract_socket(other) && sock_is_scoped(other, dom)) + if (is_abstract_socket(other) && sock_is_scoped(other, dom)) { + request.layer_plus_one =3D + landlock_match_layer_level(dom, unix_scope) + 1; + landlock_log_denial(dom, &request); return -EPERM; + } =20 return 0; } @@ -280,6 +294,16 @@ static int hook_unix_may_send(struct socket *const soc= k, const struct landlock_ruleset *const dom =3D landlock_get_applicable_domain(landlock_get_current_domain(), unix_scope); + struct lsm_network_audit audit_net =3D { + .sk =3D other->sk, + }; + struct landlock_request request =3D { + .type =3D LANDLOCK_REQUEST_SCOPE_ABSTRACT_UNIX_SOCKET, + .audit =3D { + .type =3D LSM_AUDIT_DATA_NET, + .u.net =3D &audit_net, + }, + }; =20 if (!dom) return 0; @@ -291,8 +315,12 @@ static int hook_unix_may_send(struct socket *const soc= k, if (unix_peer(sock->sk) =3D=3D other->sk) return 0; =20 - if (is_abstract_socket(other->sk) && sock_is_scoped(other->sk, dom)) + if (is_abstract_socket(other->sk) && sock_is_scoped(other->sk, dom)) { + request.layer_plus_one =3D + landlock_match_layer_level(dom, unix_scope) + 1; + landlock_log_denial(dom, &request); return -EPERM; + } =20 return 0; } @@ -307,6 +335,13 @@ static int hook_task_kill(struct task_struct *const p, { bool is_scoped; const struct landlock_ruleset *dom; + struct landlock_request request =3D { + .type =3D LANDLOCK_REQUEST_SCOPE_SIGNAL, + .audit =3D { + .type =3D LSM_AUDIT_DATA_TASK, + .u.tsk =3D p, + }, + }; =20 if (cred) { /* Dealing with USB IO. */ @@ -324,8 +359,12 @@ static int hook_task_kill(struct task_struct *const p, is_scoped =3D domain_is_scoped(dom, landlock_get_task_domain(p), LANDLOCK_SCOPE_SIGNAL); rcu_read_unlock(); - if (is_scoped) + if (is_scoped) { + request.layer_plus_one =3D + landlock_match_layer_level(dom, signal_scope) + 1; + landlock_log_denial(dom, &request); return -EPERM; + } =20 return 0; } @@ -334,6 +373,13 @@ static int hook_file_send_sigiotask(struct task_struct= *tsk, struct fown_struct *fown, int signum) { const struct landlock_ruleset *dom; + struct landlock_request request =3D { + .type =3D LANDLOCK_REQUEST_SCOPE_SIGNAL, + .audit =3D { + .type =3D LSM_AUDIT_DATA_TASK, + .u.tsk =3D tsk, + }, + }; bool is_scoped =3D false; =20 /* Lock already held by send_sigio() and send_sigurg(). */ @@ -349,8 +395,12 @@ static int hook_file_send_sigiotask(struct task_struct= *tsk, is_scoped =3D domain_is_scoped(dom, landlock_get_task_domain(tsk), LANDLOCK_SCOPE_SIGNAL); rcu_read_unlock(); - if (is_scoped) + if (is_scoped) { + request.layer_plus_one =3D + landlock_match_layer_level(dom, signal_scope) + 1; + landlock_log_denial(dom, &request); return -EPERM; + } =20 return 0; } --=20 2.47.0 From nobody Tue Nov 26 01:29:03 2024 Received: from smtp-42ab.mail.infomaniak.ch (smtp-42ab.mail.infomaniak.ch [84.16.66.171]) (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 B16111DFDB2 for ; Fri, 22 Nov 2024 14:34:28 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=84.16.66.171 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1732286071; cv=none; b=UyqzJE/ua7sdzXLYAXUdV4PH7mjgN9ZSoeWRKwczKqm30BospchVGiic9wpTI4YBUpvjYEMP8kVcmns299PVN9gAnsoUcXM0sg6mrBcykCYILatwa49XytOYsEZmZ8SqY5tfBwMu2dvEyxSDCNPcDnNsJ6hEPXUB2LYlqJ8DpQA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1732286071; c=relaxed/simple; bh=9vkpqOXOOas8sOVHBkBZnkbCiu2ImKo+E65/I4xkif4=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=G0ljp5Hm7gtnNj3LmVQ9N7WGHekQWuS5MbEdBnyfGO7JWNiNFeeKZA++I1+K8NJdy7iaY2PLkN47nRFfaAO7XK0OrCsuHRF4dvasV8STf9vQGJdm+ImeTzy62FlI6yhN8PNLtvAURAvSGh++b5HuBOMw5srjiv81dkZXVIRXuqk= 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=VuCBceqR; arc=none smtp.client-ip=84.16.66.171 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="VuCBceqR" 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 4XvyKb1rhczdGx; Fri, 22 Nov 2024 15:34:27 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=digikod.net; s=20191114; t=1732286067; bh=eDiO7RWLuAlP8xysyN4lIXeckZHgELRnTxejsb+EFS8=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=VuCBceqRWuVn5tQNWDbuCjiMxnDmO3rKpO6s2MuPH5UKgu1hA0ZyNO5dbAf3guC2l H4eWKSlzuLCCdQvxPssDfT4UMYJZ0v0Wp0J4vNZN3oIwQktiZlYWxjCaDXns6uYACS 9fw4GVSEipotp4GhP1xEpyS/YaFtqDhZBCcnIhsg= Received: from unknown by smtp-4-0001.mail.infomaniak.ch (Postfix) with ESMTPA id 4XvyKZ4jwqznDC; Fri, 22 Nov 2024 15:34:26 +0100 (CET) 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 , Francis Laniel , James Morris , Jann Horn , Jeff Xu , Jorge Lucangeli Obes , Kees Cook , Konstantin Meskhidze , Matt Bobrowski , Mikhail Ivanov , Phil Sutter , 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: [PATCH v3 19/23] landlock: Control log events with LANDLOCK_RESTRICT_SELF_LOGLESS Date: Fri, 22 Nov 2024 15:33:49 +0100 Message-ID: <20241122143353.59367-20-mic@digikod.net> In-Reply-To: <20241122143353.59367-1-mic@digikod.net> References: <20241122143353.59367-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 Most of the time we want to log denied access because they should not happen and such information helps diagnose issues. However, when sandboxing processes that we know will try to access denied resources (e.g. unknown, bogus, or malicious binary), we might want to not log related access requests that might fill up logs. To disable any log for a specific Landlock domain, add a LANDLOCK_RESTRICT_SELF_LOGLESS optional flag to the landlock_restrict_self() system call. Because this flag is set for a specific Landlock domain, it makes it possible to selectively mask some access requests that would be logged by a parent domain, which might be handy for unprivileged processes to limit logs. However, system administrators should still use the audit filtering mechanism. There is intentionally no audit nor sysctl configuration to re-enable these logless domains. This is delegated to the user space program. Increment the Landlock ABI version to reflect this interface change. Cc: G=C3=BCnther Noack Cc: Paul Moore Closes: https://github.com/landlock-lsm/linux/issues/3 Signed-off-by: Micka=C3=ABl Sala=C3=BCn Link: https://lore.kernel.org/r/20241122143353.59367-20-mic@digikod.net --- Changes since v2: - Update ABI version test. --- Documentation/userspace-api/landlock.rst | 2 +- include/uapi/linux/landlock.h | 14 +++++++++++ security/landlock/audit.c | 13 ++++++---- security/landlock/domain.h | 1 + security/landlock/syscalls.c | 25 ++++++++++++++++---- tools/testing/selftests/landlock/base_test.c | 2 +- 6 files changed, 45 insertions(+), 12 deletions(-) diff --git a/Documentation/userspace-api/landlock.rst b/Documentation/users= pace-api/landlock.rst index d639c61cb472..bd0696e2ea41 100644 --- a/Documentation/userspace-api/landlock.rst +++ b/Documentation/userspace-api/landlock.rst @@ -8,7 +8,7 @@ Landlock: unprivileged access control =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D =20 :Author: Micka=C3=ABl Sala=C3=BCn -:Date: October 2024 +:Date: November 2024 =20 The goal of Landlock is to enable restriction of ambient rights (e.g. glob= al filesystem or network access) for a set of processes. Because Landlock diff --git a/include/uapi/linux/landlock.h b/include/uapi/linux/landlock.h index 33745642f787..3b31d373ef74 100644 --- a/include/uapi/linux/landlock.h +++ b/include/uapi/linux/landlock.h @@ -62,6 +62,20 @@ struct landlock_ruleset_attr { #define LANDLOCK_CREATE_RULESET_VERSION (1U << 0) /* clang-format on */ =20 +/* + * sys_landlock_restrict_self() flags: + * + * - %LANDLOCK_RESTRICT_SELF_LOGLESS: Do not create any log related to the + * enforced restrictions. This should only be set by tools launching un= known + * or untrusted programs (e.g. a sandbox tool, container runtime, system + * service manager). Because programs sandboxing themselves should fix = any + * denied access, they should not set this flag to be aware of potential + * issues reported by system's logs (i.e. audit). + */ +/* clang-format off */ +#define LANDLOCK_RESTRICT_SELF_LOGLESS (1U << 0) +/* clang-format on */ + /** * enum landlock_rule_type - Landlock rule type * diff --git a/security/landlock/audit.c b/security/landlock/audit.c index 7e4acc746cd6..d299baedd4e4 100644 --- a/security/landlock/audit.c +++ b/security/landlock/audit.c @@ -404,11 +404,6 @@ void landlock_log_denial(const struct landlock_ruleset= *const domain, if (!audit_enabled) return; =20 - ab =3D audit_log_start(audit_context(), GFP_ATOMIC | __GFP_NOWARN, - AUDIT_LANDLOCK_DENY); - if (!ab) - return; - missing =3D request->access; if (missing) { size_t youngest_layer; @@ -429,6 +424,14 @@ void landlock_log_denial(const struct landlock_ruleset= *const domain, get_hierarchy(domain, request->layer_plus_one - 1); } =20 + if (READ_ONCE(youngest_denied->log_status) =3D=3D LANDLOCK_LOG_DISABLED) + return; + + ab =3D audit_log_start(audit_context(), GFP_ATOMIC | __GFP_NOWARN, + AUDIT_LANDLOCK_DENY); + if (!ab) + return; + audit_log_format(ab, "domain=3D%llx blockers=3D", youngest_denied->id); log_blockers(ab, request->type, missing); audit_log_lsm_data(ab, &request->audit); diff --git a/security/landlock/domain.h b/security/landlock/domain.h index ffc8b1bb58be..fe4b2de734d2 100644 --- a/security/landlock/domain.h +++ b/security/landlock/domain.h @@ -23,6 +23,7 @@ enum landlock_log_status { LANDLOCK_LOG_PENDING =3D 0, LANDLOCK_LOG_RECORDED, + LANDLOCK_LOG_DISABLED, }; =20 /** diff --git a/security/landlock/syscalls.c b/security/landlock/syscalls.c index 335067e36feb..48c26ed8c099 100644 --- a/security/landlock/syscalls.c +++ b/security/landlock/syscalls.c @@ -151,7 +151,12 @@ static const struct file_operations ruleset_fops =3D { .write =3D fop_dummy_write, }; =20 -#define LANDLOCK_ABI_VERSION 6 +/* + * The Landlock ABI version should be incremented for each new Landlock-re= lated + * user space visible change (e.g. Landlock syscalls). Only increment this + * version once per Linux release. + */ +#define LANDLOCK_ABI_VERSION 7 =20 /** * sys_landlock_create_ruleset - Create a new ruleset @@ -452,7 +457,7 @@ SYSCALL_DEFINE4(landlock_add_rule, const int, ruleset_f= d, * sys_landlock_restrict_self - Enforce a ruleset on the calling thread * * @ruleset_fd: File descriptor tied to the ruleset to merge with the targ= et. - * @flags: Must be 0. + * @flags: Supported value: %LANDLOCK_RESTRICT_SELF_LOGLESS. * * This system call enables to enforce a Landlock ruleset on the current * thread. Enforcing a ruleset requires that the task has %CAP_SYS_ADMIN = in its @@ -478,6 +483,7 @@ SYSCALL_DEFINE2(landlock_restrict_self, const int, rule= set_fd, const __u32, struct cred *new_cred; struct landlock_cred_security *new_llcred; int err; + bool is_logless =3D false; =20 if (!is_initialized()) return -EOPNOTSUPP; @@ -490,9 +496,12 @@ SYSCALL_DEFINE2(landlock_restrict_self, const int, rul= eset_fd, const __u32, !ns_capable_noaudit(current_user_ns(), CAP_SYS_ADMIN)) return -EPERM; =20 - /* No flag for now. */ - if (flags) - return -EINVAL; + if (flags) { + if (flags =3D=3D LANDLOCK_RESTRICT_SELF_LOGLESS) + is_logless =3D true; + else + return -EINVAL; + } =20 /* Gets and checks the ruleset. */ ruleset =3D get_ruleset_from_fd(ruleset_fd, FMODE_CAN_READ); @@ -517,6 +526,12 @@ SYSCALL_DEFINE2(landlock_restrict_self, const int, rul= eset_fd, const __u32, goto out_put_creds; } =20 + if (is_logless) { +#ifdef CONFIG_AUDIT + new_dom->hierarchy->log_status =3D LANDLOCK_LOG_DISABLED; +#endif /* CONFIG_AUDIT */ + } + /* Replaces the old (prepared) domain. */ landlock_put_ruleset(new_llcred->domain); new_llcred->domain =3D new_dom; diff --git a/tools/testing/selftests/landlock/base_test.c b/tools/testing/s= elftests/landlock/base_test.c index 1bc16fde2e8a..fbd687691b3c 100644 --- a/tools/testing/selftests/landlock/base_test.c +++ b/tools/testing/selftests/landlock/base_test.c @@ -76,7 +76,7 @@ TEST(abi_version) const struct landlock_ruleset_attr ruleset_attr =3D { .handled_access_fs =3D LANDLOCK_ACCESS_FS_READ_FILE, }; - ASSERT_EQ(6, landlock_create_ruleset(NULL, 0, + ASSERT_EQ(7, landlock_create_ruleset(NULL, 0, LANDLOCK_CREATE_RULESET_VERSION)); =20 ASSERT_EQ(-1, landlock_create_ruleset(&ruleset_attr, 0, --=20 2.47.0 From nobody Tue Nov 26 01:29:03 2024 Received: from smtp-8fa8.mail.infomaniak.ch (smtp-8fa8.mail.infomaniak.ch [83.166.143.168]) (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 49ACF1DF990 for ; Fri, 22 Nov 2024 14:34:29 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=83.166.143.168 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1732286073; cv=none; b=UVELblxbzMw4MB2C8BjV0z5DOiE8kDoL/GNY/XshBYdZN7x7sJlQpAIdIVzFOIB7UaiM0HpNxipqdchyxmmeTDSlkdl0DF71xYrOjMQkbBNAw3EPgfd9H/trE8EGwyZHyzE3qxpmkTlsARdU2Lodmsc0kgDi8Btsb54jZCJLp/4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1732286073; c=relaxed/simple; bh=mcO299gnz9ndiaMSwPmQbuZYoMNokkhMIATxSJ2kdIU=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=E6wUDq4exAprGMiVwtAcE61UCsVf1Mu87+GUwUHA27GLdsTUDGkYIN2p/GnhPs18z3w93/GY6xMEZz+ZJjbAwGsE0UzGg9d9Vq/SB73PQGQOY6SJo146h8+z4eaT2DZzgoGMA8nrM34k6moBScD9o2KslFnHSPDG+Ph1eS2cOF0= 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=asEjhPON; arc=none smtp.client-ip=83.166.143.168 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="asEjhPON" Received: from smtp-3-0001.mail.infomaniak.ch (smtp-3-0001.mail.infomaniak.ch [10.4.36.108]) by smtp-3-3000.mail.infomaniak.ch (Postfix) with ESMTPS id 4XvyKc2zV8zlSf; Fri, 22 Nov 2024 15:34:28 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=digikod.net; s=20191114; t=1732286068; bh=Lq3kha7+yN0YJbY/oShjU2K3cOge2QbVPhVQ4nd4Oko=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=asEjhPONGwsuFqUltq6GBOetEvMHRIQ25/kGigkrREcVN+PMoTAvVWB8w2A5JxWsK wNs5d3uxtQpNrguLbelsMOivgIyfRBABijXEoDpPA6ZznNBYBRe3QxeLQNd1dqy9mJ omltWKAT21kZLpRN1LidHmQdAmw3UEyUS9MEjwZc= Received: from unknown by smtp-3-0001.mail.infomaniak.ch (Postfix) with ESMTPA id 4XvyKb5VmxzHkl; Fri, 22 Nov 2024 15:34:27 +0100 (CET) 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 , Francis Laniel , James Morris , Jann Horn , Jeff Xu , Jorge Lucangeli Obes , Kees Cook , Konstantin Meskhidze , Matt Bobrowski , Mikhail Ivanov , Phil Sutter , 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: [PATCH v3 20/23] samples/landlock: Do not log denials from the sandboxer by default Date: Fri, 22 Nov 2024 15:33:50 +0100 Message-ID: <20241122143353.59367-21-mic@digikod.net> In-Reply-To: <20241122143353.59367-1-mic@digikod.net> References: <20241122143353.59367-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 Do not pollute audit logs because of unknown sandboxed programs. Indeed, the sandboxer's security policy might not be fitted to the set of sandboxed processes that could be spawned (e.g. from a shell). The LANDLOCK_RESTRICT_SELF_LOGLESS flag should be used for all similar sandboxer tools by default. Only natively-sandboxed programs should not use this flag. For test purpose, parse the LL_FORCE_LOG environment variable to still log denials. Cc: G=C3=BCnther Noack Signed-off-by: Micka=C3=ABl Sala=C3=BCn Link: https://lore.kernel.org/r/20241122143353.59367-21-mic@digikod.net --- Changes since v2: - New patch. --- samples/landlock/sandboxer.c | 35 ++++++++++++++++++++++++++++++++--- 1 file changed, 32 insertions(+), 3 deletions(-) diff --git a/samples/landlock/sandboxer.c b/samples/landlock/sandboxer.c index 57565dfd74a2..49fd4fdb1dcf 100644 --- a/samples/landlock/sandboxer.c +++ b/samples/landlock/sandboxer.c @@ -58,6 +58,7 @@ static inline int landlock_restrict_self(const int rulese= t_fd, #define ENV_TCP_BIND_NAME "LL_TCP_BIND" #define ENV_TCP_CONNECT_NAME "LL_TCP_CONNECT" #define ENV_SCOPED_NAME "LL_SCOPED" +#define ENV_FORCE_LOG_NAME "LL_FORCE_LOG" #define ENV_DELIMITER ":" =20 static int str2num(const char *numstr, __u64 *num_dst) @@ -288,7 +289,7 @@ static bool check_ruleset_scope(const char *const env_v= ar, =20 /* clang-format on */ =20 -#define LANDLOCK_ABI_LAST 6 +#define LANDLOCK_ABI_LAST 7 =20 #define XSTR(s) #s #define STR(s) XSTR(s) @@ -315,6 +316,9 @@ static const char help[] =3D " - \"a\" to restrict opening abstract unix sockets\n" " - \"s\" to restrict sending signals\n" "\n" + "A sandboxer should not log denied access requests to avoid spamming logs= , " + "but to test audit we can set " ENV_FORCE_LOG_NAME "=3D1\n" + "\n" "Example:\n" ENV_FS_RO_NAME "=3D\"${PATH}:/lib:/usr:/proc:/etc:/dev/urandom\" " ENV_FS_RW_NAME "=3D\"/dev/null:/dev/full:/dev/zero:/dev/pts:/tmp\" " @@ -333,7 +337,7 @@ int main(const int argc, char *const argv[], char *cons= t *const envp) const char *cmd_path; char *const *cmd_argv; int ruleset_fd, abi; - char *env_port_name; + char *env_port_name, *env_force_log; __u64 access_fs_ro =3D ACCESS_FS_ROUGHLY_READ, access_fs_rw =3D ACCESS_FS_ROUGHLY_READ | ACCESS_FS_ROUGHLY_WRITE; =20 @@ -344,6 +348,8 @@ int main(const int argc, char *const argv[], char *cons= t *const envp) .scoped =3D LANDLOCK_SCOPE_ABSTRACT_UNIX_SOCKET | LANDLOCK_SCOPE_SIGNAL, }; + /* Do not pollute audit logs because of unknown sandboxed programs. */ + int restrict_flags =3D LANDLOCK_RESTRICT_SELF_LOGLESS; =20 if (argc < 2) { fprintf(stderr, help, argv[0]); @@ -415,6 +421,12 @@ int main(const int argc, char *const argv[], char *con= st *const envp) /* Removes LANDLOCK_SCOPE_* for ABI < 6 */ ruleset_attr.scoped &=3D ~(LANDLOCK_SCOPE_ABSTRACT_UNIX_SOCKET | LANDLOCK_SCOPE_SIGNAL); + __attribute__((fallthrough)); + case 6: + /* Removes LANDLOCK_RESTRICT_SELF_LOGLESS for ABI < 7 */ + restrict_flags &=3D ~LANDLOCK_RESTRICT_SELF_LOGLESS; + + /* Must be printed for any ABI < LANDLOCK_ABI_LAST. */ fprintf(stderr, "Hint: You should update the running kernel " "to leverage Landlock features " @@ -449,6 +461,23 @@ int main(const int argc, char *const argv[], char *con= st *const envp) if (check_ruleset_scope(ENV_SCOPED_NAME, &ruleset_attr)) return 1; =20 + /* Enables optional logs. */ + env_force_log =3D getenv(ENV_FORCE_LOG_NAME); + if (env_force_log) { + if (strcmp(env_force_log, "1") !=3D 0) { + fprintf(stderr, + "Unknown value for " ENV_FORCE_LOG_NAME "\n"); + return 1; + } + if (!(restrict_flags & LANDLOCK_RESTRICT_SELF_LOGLESS)) { + fprintf(stderr, + "Audit logs not supported by current kernel\n"); + return 1; + } + restrict_flags &=3D ~LANDLOCK_RESTRICT_SELF_LOGLESS; + unsetenv(ENV_FORCE_LOG_NAME); + } + ruleset_fd =3D landlock_create_ruleset(&ruleset_attr, sizeof(ruleset_attr), 0); if (ruleset_fd < 0) { @@ -476,7 +505,7 @@ int main(const int argc, char *const argv[], char *cons= t *const envp) perror("Failed to restrict privileges"); goto err_close_ruleset; } - if (landlock_restrict_self(ruleset_fd, 0)) { + if (landlock_restrict_self(ruleset_fd, restrict_flags)) { perror("Failed to enforce ruleset"); goto err_close_ruleset; } --=20 2.47.0 From nobody Tue Nov 26 01:29:03 2024 Received: from smtp-1909.mail.infomaniak.ch (smtp-1909.mail.infomaniak.ch [185.125.25.9]) (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 74BE11DFDA1 for ; Fri, 22 Nov 2024 14:34:31 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=185.125.25.9 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1732286074; cv=none; b=IKCFHwdxevXOylaLpy8K8Ebeb3w4hA/2kFkTLM99PEX8+6RxXxU/I5GFMxt9ACSclqBPwzGfK7vQullibOxq7BNT/uBL6BBQqPHSk7k7uzxjCt6IPF6I3m6t26Y71Y2VeXhniLTkiTCFdlq0EyWFDeH6NM20LonAywRNogSS7iM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1732286074; c=relaxed/simple; bh=bQqucovuqXsfcpnCRY6ioAvHAX0oJC0TChC0D8tZzOw=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=rBMUat9bluGWbBO13vs961nthJh5mkeSync2IzP8kU+tJoKyWv4FUKyAafpvG8b3On8S6Y1Igr1C60q85PB+J+AU5tzivODyMSVcnfw0FQrQ/7v3jpY5sClaRupGR+DcmVW3TxJneGqBrUn9aR9Oeg/EUv9Xazx8OoefjK2w1FM= 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=PX2OshPo; arc=none smtp.client-ip=185.125.25.9 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="PX2OshPo" 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 4XvyKd4sJ5zcvq; Fri, 22 Nov 2024 15:34:29 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=digikod.net; s=20191114; t=1732286069; bh=9jecdmy7jrK2+6zmM20QZ0wgXA3NijoGLJiaDMUhMsk=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=PX2OshPoniRIe+tqtSJRTa2QBiSu+SwASCo/3G9G/9AzmOJAa6S6uHDAm/zhDuQot twlxyLum7gbhnZ2UeiPtErbEjdEvfOqdMiCnM6GnLutRYkBg7sgmtq2CD1eNQ5O3Xn rLS5OKXYX90DFUTP1ws9/9sYb0LyYe4jZBYdKOqw= Received: from unknown by smtp-4-0001.mail.infomaniak.ch (Postfix) with ESMTPA id 4XvyKc6YKKzn2K; Fri, 22 Nov 2024 15:34:28 +0100 (CET) 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 , Francis Laniel , James Morris , Jann Horn , Jeff Xu , Jorge Lucangeli Obes , Kees Cook , Konstantin Meskhidze , Matt Bobrowski , Mikhail Ivanov , Phil Sutter , 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: [PATCH v3 21/23] selftests/landlock: Extend tests for landlock_restrict_self()'s flags Date: Fri, 22 Nov 2024 15:33:51 +0100 Message-ID: <20241122143353.59367-22-mic@digikod.net> In-Reply-To: <20241122143353.59367-1-mic@digikod.net> References: <20241122143353.59367-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 the restrict_self_flags test suite to check that LANDLOCK_RESTRICT_SELF_LOGLESS is valid but not the next bit. Some checks are similar to restrict_self_checks_ordering's ones. Cc: G=C3=BCnther Noack Signed-off-by: Micka=C3=ABl Sala=C3=BCn Link: https://lore.kernel.org/r/20241122143353.59367-22-mic@digikod.net --- Changes since v2: - New patch. --- tools/testing/selftests/landlock/base_test.c | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/tools/testing/selftests/landlock/base_test.c b/tools/testing/s= elftests/landlock/base_test.c index fbd687691b3c..4e0104f1efa0 100644 --- a/tools/testing/selftests/landlock/base_test.c +++ b/tools/testing/selftests/landlock/base_test.c @@ -233,6 +233,22 @@ TEST(restrict_self_checks_ordering) ASSERT_EQ(0, close(ruleset_fd)); } =20 +TEST(restrict_self_flags) +{ + ASSERT_EQ(-1, landlock_restrict_self(-1, 0)); + ASSERT_EQ(EBADF, errno); + + ASSERT_EQ(-1, + landlock_restrict_self(-1, LANDLOCK_RESTRICT_SELF_LOGLESS)); + ASSERT_EQ(EBADF, errno); + + ASSERT_EQ(-1, landlock_restrict_self(-1, 1U << 1)); + ASSERT_EQ(EINVAL, errno); + + ASSERT_EQ(-1, landlock_restrict_self(-1, -1)); + ASSERT_EQ(EINVAL, errno); +} + TEST(ruleset_fd_io) { struct landlock_ruleset_attr ruleset_attr =3D { --=20 2.47.0 From nobody Tue Nov 26 01:29:03 2024 Received: from smtp-8fae.mail.infomaniak.ch (smtp-8fae.mail.infomaniak.ch [83.166.143.174]) (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 5EFDF13C80C for ; Fri, 22 Nov 2024 14:34:38 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=83.166.143.174 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1732286080; cv=none; b=jspYNbiiX/lHhHwjOUCfWpgm6MEVkB5Et8vKaZ7I+1UyjjHAHyodFnLTLETCqvVuid5T6e6fRMNWhl936fzFcqRlRWK4ZL0rnxMkB0PuwrKL01YH8KtrhwOwXCw5JYSznEwnD+KebBMao1odfRvEUPq5wfCSsd6LKWQtNgx/iY4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1732286080; c=relaxed/simple; bh=yqIV3JokOrPL6fJpSmvCdfso7UefVNGNi9cxShXHoSk=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=IKTtFjdfshA0gI11uQb0FcqzoeHa8K/HoYLBzdGrIGF0aTNtKsZqRyT/xWnv5uMFAIlVUNv7Uearbt9v5HTaCUEXyldSAULmbBR12kaOrZLEdLhVQtGlBhmMx2uq1eZ6vVeFG32mXDVC7FP1OCmIHIKGG8cZlD+5mPwq9MWQJ8w= 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=gb1wR04x; arc=none smtp.client-ip=83.166.143.174 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="gb1wR04x" Received: from smtp-3-0000.mail.infomaniak.ch (smtp-3-0000.mail.infomaniak.ch [10.4.36.107]) by smtp-3-3000.mail.infomaniak.ch (Postfix) with ESMTPS id 4XvyKf5bWMzj3q; Fri, 22 Nov 2024 15:34:30 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=digikod.net; s=20191114; t=1732286070; bh=N1kG0yjS/ipUTLbH7ZaWMfoEW1SfPQZh0ziwxs6aNoA=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=gb1wR04xjyL9bDUB2QA84Ko8QuzoOQA8Yx3rwbwV+/kYOb5NbsqzymngFMRUFJT21 bOCqGJJhBMCHamV4eesFx0d1JwBApVDBesASo8/4vhaBFVMgRfsi2Mv6Gp88p56f92 WhITlOCvrY06wm6ZgFvysk7Aoi4IKH1GTZc7YqGM= Received: from unknown by smtp-3-0000.mail.infomaniak.ch (Postfix) with ESMTPA id 4XvyKf0yFszlYn; Fri, 22 Nov 2024 15:34:30 +0100 (CET) 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 , Francis Laniel , James Morris , Jann Horn , Jeff Xu , Jorge Lucangeli Obes , Kees Cook , Konstantin Meskhidze , Matt Bobrowski , Mikhail Ivanov , Phil Sutter , 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: [PATCH v3 22/23] selftests/landlock: Add tests for audit Date: Fri, 22 Nov 2024 15:33:52 +0100 Message-ID: <20241122143353.59367-23-mic@digikod.net> In-Reply-To: <20241122143353.59367-1-mic@digikod.net> References: <20241122143353.59367-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_test.c to check with and without LANDLOCK_RESTRICT_SELF_LOGLESS against the three Landlock audit record types: AUDIT_LANDLOCK_DENY, AUDIT_LANDLOCK_DOM_INFO, and AUDIT_LANDLOCK_DOM_DROP. Tests are run with audit filters to ensure the audit records come from the running tests. Moreover, because there can only be one audit process, tests would failed if run in parallel. Because of audit limitations, tests can only be run in the initial namespace. The audit test helpers were inspired by libaudit and tools/testing/selftests/net/netfilter/audit_logread.c Cc: G=C3=BCnther Noack Cc: Paul Moore Cc: Phil Sutter Signed-off-by: Micka=C3=ABl Sala=C3=BCn Link: https://lore.kernel.org/r/20241122143353.59367-23-mic@digikod.net --- Changes since v2: - New patch. --- tools/testing/selftests/landlock/audit.h | 308 ++++++++++++++++++ tools/testing/selftests/landlock/audit_test.c | 168 ++++++++++ tools/testing/selftests/landlock/common.h | 2 + tools/testing/selftests/landlock/config | 1 + 4 files changed, 479 insertions(+) create mode 100644 tools/testing/selftests/landlock/audit.h create mode 100644 tools/testing/selftests/landlock/audit_test.c diff --git a/tools/testing/selftests/landlock/audit.h b/tools/testing/selft= ests/landlock/audit.h new file mode 100644 index 000000000000..4977e8fac081 --- /dev/null +++ b/tools/testing/selftests/landlock/audit.h @@ -0,0 +1,308 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* + * Landlock audit helpers + * + * Copyright =C2=A9 2024 Microsoft Corporation + */ + +#define _GNU_SOURCE +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define REGEX_LANDLOCK_PREFIX "^audit([0-9.:]\\+): domain=3D[0-9a-f]\\+" + +struct audit_state { + int fd; + size_t exe_len; + char exe[PATH_MAX]; +}; + +struct audit_message { + struct nlmsghdr header; + union { + struct audit_status status; + struct audit_features features; + struct audit_rule_data rule; + struct nlmsgerr err; + char data[PATH_MAX + 200]; + }; +}; + +static int audit_send(const int fd, const struct audit_message *const msg) +{ + struct sockaddr_nl addr =3D { + .nl_family =3D AF_NETLINK, + }; + int ret; + + do { + ret =3D sendto(fd, msg, msg->header.nlmsg_len, 0, + (struct sockaddr *)&addr, sizeof(addr)); + } while (ret < 0 && errno =3D=3D EINTR); + + if (ret < 0) + return -errno; + + if (ret !=3D msg->header.nlmsg_len) + return -E2BIG; + + return 0; +} + +static int audit_recv(const int fd, struct audit_message *const msg) +{ + struct sockaddr_nl addr; + socklen_t addrlen =3D sizeof(addr); + int err; + + do { + err =3D recvfrom(fd, msg, sizeof(*msg), 0, + (struct sockaddr *)&addr, &addrlen); + } while (err < 0 && errno =3D=3D EINTR); + + if (err < 0) + return -errno; + + if (addrlen !=3D sizeof(addr) || addr.nl_pid !=3D 0) + return -EINVAL; + + if (msg->header.nlmsg_type =3D=3D NLMSG_ERROR && msg->err.error) + return msg->err.error; + + return 0; +} + +static int audit_request(const int fd, + const struct audit_message *const request, + struct audit_message *reply) +{ + struct audit_message msg; + int ret; + + ret =3D audit_send(fd, request); + if (ret) + return ret; + + if (!reply) + reply =3D &msg; + + return audit_recv(fd, reply); +} + +static int audit_filter_exe(const struct audit_state *const state, + const __u16 type) +{ + struct audit_message msg =3D { + .header =3D { + .nlmsg_len =3D NLMSG_SPACE(sizeof(msg.rule)) + + NLMSG_ALIGN(state->exe_len), + .nlmsg_type =3D type, + .nlmsg_flags =3D NLM_F_REQUEST | NLM_F_ACK, + }, + .rule =3D { + .flags =3D AUDIT_FILTER_EXCLUDE, + .action =3D AUDIT_NEVER, + .field_count =3D 1, + .fields[0] =3D AUDIT_EXE, + .fieldflags[0] =3D AUDIT_NOT_EQUAL, + .values[0] =3D state->exe_len, + .buflen =3D state->exe_len, + } + }; + + memcpy(msg.rule.buf, state->exe, state->exe_len); + return audit_request(state->fd, &msg, NULL); +} + +static int audit_filter_drop(const struct audit_state *const state, + const __u16 type) +{ + struct audit_message msg =3D { + .header =3D { + .nlmsg_len =3D NLMSG_SPACE(sizeof(msg.rule)), + .nlmsg_type =3D type, + .nlmsg_flags =3D NLM_F_REQUEST | NLM_F_ACK, + }, + .rule =3D { + .flags =3D AUDIT_FILTER_EXCLUDE, + .action =3D AUDIT_NEVER, + .field_count =3D 1, + .fields[0] =3D AUDIT_MSGTYPE, + .fieldflags[0] =3D AUDIT_NOT_EQUAL, + .values[0] =3D AUDIT_LANDLOCK_DOM_DROP, + } + }; + + return audit_request(state->fd, &msg, NULL); +} + +static int audit_set_status(int fd, __u32 key, __u32 val) +{ + const struct audit_message msg =3D { + .header =3D { + .nlmsg_len =3D NLMSG_SPACE(sizeof(msg.status)), + .nlmsg_type =3D AUDIT_SET, + .nlmsg_flags =3D NLM_F_REQUEST | NLM_F_ACK, + }, + .status =3D { + .mask =3D key, + .enabled =3D key =3D=3D AUDIT_STATUS_ENABLED ? val : 0, + .pid =3D key =3D=3D AUDIT_STATUS_PID ? val : 0, + } + }; + + return audit_request(fd, &msg, NULL); +} + +static int audit_match_record(struct audit_state *const state, const __u16= type, + const char *const pattern) +{ + struct audit_message msg; + int ret, err =3D 0; + bool matches_record =3D !type; + regex_t regex; + + ret =3D regcomp(®ex, pattern, 0); + if (ret) + return -EINVAL; + + do { + memset(&msg, 0, sizeof(msg)); + err =3D audit_recv(state->fd, &msg); + if (err) + goto out; + + if (msg.header.nlmsg_type =3D=3D type) + matches_record =3D true; + } while (!matches_record); + + ret =3D regexec(®ex, msg.data, 0, NULL, 0); + if (ret) { + printf("DATA: %s\n", msg.data); + printf("ERROR: no match for pattern: %s\n", pattern); + err =3D -ENOENT; + } + +out: + regfree(®ex); + return err; +} + +struct audit_records { + size_t deny; + size_t info; + size_t drop; +}; + +static void audit_count_records(struct audit_state *const state, + struct audit_records *records) +{ + struct audit_message msg; + + records->deny =3D 0; + records->info =3D 0; + records->drop =3D 0; + + do { + memset(&msg, 0, sizeof(msg)); + if (audit_recv(state->fd, &msg)) + return; + + switch (msg.header.nlmsg_type) { + case AUDIT_LANDLOCK_DENY: + records->deny++; + break; + case AUDIT_LANDLOCK_DOM_INFO: + records->info++; + break; + case AUDIT_LANDLOCK_DOM_DROP: + records->drop++; + break; + } + } while (true); +} + +static int audit_init_state(struct audit_state *state) +{ + int err; + + state->fd =3D socket(PF_NETLINK, SOCK_RAW, NETLINK_AUDIT); + if (state->fd < 0) + return -errno; + + err =3D audit_set_status(state->fd, AUDIT_STATUS_ENABLED, 1); + if (err) + return err; + + err =3D audit_set_status(state->fd, AUDIT_STATUS_PID, getpid()); + if (err) + return err; + + state->exe_len =3D + readlink("/proc/self/exe", state->exe, sizeof(state->exe) - 1); + if (state->exe_len < 0) + return -errno; + + return 0; +} + +static int audit_cleanup(struct audit_state *state) +{ + struct audit_state state_clean; + + if (!state) { + int err; + + /* Requires when called from FIXTURE_TEARDOWN_PARENT(). */ + state =3D &state_clean; + err =3D audit_init_state(state); + if (err) + return err; + } + + /* Filters might not be in place. */ + audit_filter_exe(state, AUDIT_DEL_RULE); + audit_filter_drop(state, AUDIT_DEL_RULE); + + /* + * Because audit_cleanup() might not be called by the test auditd + * process, it might not be possible to explicitly set it. Anyway, + * AUDIT_STATUS_ENABLED will implicitly be set to 0 when the auditd + * process will exit. + */ + return close(state->fd); +} + +static int audit_init(struct audit_state *state) +{ + const struct timeval tv =3D { + .tv_usec =3D 1, + }; + int err; + + err =3D audit_init_state(state); + if (err) + return err; + + // TODO: Make sure there is no rule already filtering. + + err =3D audit_filter_exe(state, AUDIT_ADD_RULE); + if (err) + return err; + + /* Sets a timeout for negative tests. */ + err =3D setsockopt(state->fd, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv)); + if (err) + return -errno; + + return 0; +} diff --git a/tools/testing/selftests/landlock/audit_test.c b/tools/testing/= selftests/landlock/audit_test.c new file mode 100644 index 000000000000..f09273dc164a --- /dev/null +++ b/tools/testing/selftests/landlock/audit_test.c @@ -0,0 +1,168 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * Landlock tests - Audit + * + * Copyright =C2=A9 2024 Microsoft Corporation + */ + +#define _GNU_SOURCE +#include +#include +#include +#include +#include +#include +#include + +#include "audit.h" +#include "common.h" + +static int matches_log_umount(struct __test_metadata *const _metadata, + struct audit_state *const state) +{ + // TODO: return -errno with AUDIT_SYSCALL + return !audit_match_record(state, AUDIT_LANDLOCK_DENY, + REGEX_LANDLOCK_PREFIX " blockers=3D.*"); +} + +FIXTURE(audit) +{ + struct audit_state state; +}; + +FIXTURE_VARIANT(audit) +{ + const int restrict_flags; +}; + +/* clang-format off */ +FIXTURE_VARIANT_ADD(audit, default) {}; +/* clang-format on */ + +/* clang-format off */ +FIXTURE_VARIANT_ADD(audit, logless) { + /* clang-format on */ + .restrict_flags =3D LANDLOCK_RESTRICT_SELF_LOGLESS, +}; + +FIXTURE_SETUP(audit) +{ + int err; + + disable_caps(_metadata); + set_cap(_metadata, CAP_AUDIT_CONTROL); + err =3D audit_init(&self->state); + EXPECT_EQ(0, err) + { + const char *error_msg; + + if (err =3D=3D -EEXIST) + error_msg =3D "socket already in use (e.g. auditd)"; + /* kill "$(auditctl -s | sed -ne 's/^pid \([0-9]\+\)$/\1/p')" */ + else + error_msg =3D strerror(-err); + TH_LOG("Failed to initialize audit: %s", error_msg); + } + clear_cap(_metadata, CAP_AUDIT_CONTROL); +} + +FIXTURE_TEARDOWN(audit) +{ + set_cap(_metadata, CAP_AUDIT_CONTROL); + EXPECT_EQ(0, audit_cleanup(&self->state)); +} + +TEST_F(audit, fs_deny) +{ + int status, ret; + pid_t child; + struct audit_records records; + + child =3D fork(); + ASSERT_LE(0, child); + if (child =3D=3D 0) { + const struct landlock_ruleset_attr ruleset_attr =3D { + .handled_access_fs =3D LANDLOCK_ACCESS_FS_EXECUTE, + }; + int ruleset_fd; + + /* Add filesystem restrictions. */ + ruleset_fd =3D landlock_create_ruleset(&ruleset_attr, + sizeof(ruleset_attr), 0); + ASSERT_LE(0, ruleset_fd); + EXPECT_EQ(0, prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0)); + ASSERT_EQ(0, landlock_restrict_self(ruleset_fd, + variant->restrict_flags)); + EXPECT_EQ(0, close(ruleset_fd)); + + /* First umount checks to test log entries. */ + set_cap(_metadata, CAP_SYS_ADMIN); + EXPECT_EQ(-1, umount("/")); + EXPECT_EQ(EPERM, errno); + clear_cap(_metadata, CAP_SYS_ADMIN); + + if (variant->restrict_flags & LANDLOCK_RESTRICT_SELF_LOGLESS) { + EXPECT_EQ(0, + matches_log_umount(_metadata, &self->state)); + } else { + EXPECT_EQ(1, + matches_log_umount(_metadata, &self->state)); + + /* Checks domain information records. */ + ret =3D audit_match_record( + &self->state, AUDIT_LANDLOCK_DOM_INFO, + REGEX_LANDLOCK_PREFIX + " creation=3D[0-9.]\\+ pid=3D[0-9]\\+ uid=3D[0-9]\\+ exe=3D\"[^\"]\\+\= " comm=3D\"audit_test\"$"); + EXPECT_EQ(0, ret); + } + + /* Second umount checks to test audit_count_records(). */ + set_cap(_metadata, CAP_SYS_ADMIN); + EXPECT_EQ(-1, umount("/")); + EXPECT_EQ(EPERM, errno); + clear_cap(_metadata, CAP_SYS_ADMIN); + + /* Makes sure there is no superfluous logged records. */ + audit_count_records(&self->state, &records); + if (variant->restrict_flags & LANDLOCK_RESTRICT_SELF_LOGLESS) { + EXPECT_EQ(0, records.deny); + } else { + EXPECT_EQ(1, records.deny); + } + EXPECT_EQ(0, records.info); + EXPECT_EQ(0, records.drop); + + /* Updates filter rules to match the drop record. */ + set_cap(_metadata, CAP_AUDIT_CONTROL); + EXPECT_EQ(0, audit_filter_drop(&self->state, AUDIT_ADD_RULE)); + EXPECT_EQ(0, audit_filter_exe(&self->state, AUDIT_DEL_RULE)); + clear_cap(_metadata, CAP_AUDIT_CONTROL); + + _exit(_metadata->exit_code); + return; + } + + ASSERT_EQ(child, waitpid(child, &status, 0)); + if (WIFSIGNALED(status) || !WIFEXITED(status) || + WEXITSTATUS(status) !=3D EXIT_SUCCESS) + _metadata->exit_code =3D KSFT_FAIL; + + /* FIXME: How to match the domain drop without race condition? */ +#if 0 + ret =3D audit_match_record(&self->state, AUDIT_LANDLOCK_DOM_DROP, + REGEX_LANDLOCK_PREFIX "$"); + if (variant->restrict_flags & LANDLOCK_RESTRICT_SELF_LOGLESS) { + EXPECT_EQ(-EAGAIN, ret); + } else { + EXPECT_EQ(0, ret); + } + + /* Makes sure there is no superfluous logged records. */ + audit_count_records(&self->state, &records); + EXPECT_EQ(0, records.deny); + EXPECT_EQ(0, records.info); + EXPECT_EQ(0, records.drop); +#endif +} + +TEST_HARNESS_MAIN diff --git a/tools/testing/selftests/landlock/common.h b/tools/testing/self= tests/landlock/common.h index 61056fa074bb..07770c3eb190 100644 --- a/tools/testing/selftests/landlock/common.h +++ b/tools/testing/selftests/landlock/common.h @@ -12,6 +12,7 @@ #include #include #include +#include #include #include #include @@ -64,6 +65,7 @@ static void _init_caps(struct __test_metadata *const _met= adata, bool drop_all) /* Only these three capabilities are useful for the tests. */ const cap_value_t caps[] =3D { /* clang-format off */ + CAP_AUDIT_CONTROL, CAP_DAC_OVERRIDE, CAP_MKNOD, CAP_NET_ADMIN, diff --git a/tools/testing/selftests/landlock/config b/tools/testing/selfte= sts/landlock/config index 29af19c4e9f9..3d4eb994d62b 100644 --- a/tools/testing/selftests/landlock/config +++ b/tools/testing/selftests/landlock/config @@ -1,3 +1,4 @@ +CONFIG_AUDIT=3Dy CONFIG_CGROUPS=3Dy CONFIG_CGROUP_SCHED=3Dy CONFIG_INET=3Dy --=20 2.47.0 From nobody Tue Nov 26 01:29:03 2024 Received: from smtp-bc0a.mail.infomaniak.ch (smtp-bc0a.mail.infomaniak.ch [45.157.188.10]) (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 167431E04A1 for ; Fri, 22 Nov 2024 14:34:33 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=45.157.188.10 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1732286077; cv=none; b=bIk9utsY9R3Haef3iKYA4trYieR02r+Ie3pt0igiCHi+eDE4I8op8AxmA9LZw9Tv+l0o8NAGloQ7a22c0NAXCgxOxpykDHAvaCgyeApZcEF9y63jSYst8ITUH8GxmGmSoN/o2+ULoVA+fsV2SxYz6YmwOGR0hxBiash89t/Y65w= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1732286077; c=relaxed/simple; bh=36b7Lyz3k8FScTc5AXG1dY/03AGd/FzjGBeIK5Rrh6o=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=Mn/kxtql3xe9RHmvFyhf87jqkMRZ0b8KF4StpC8+5jkdlg9fiI9lP/qI6dTMgRb7WcRE1frhRmizWf83Vi/CPprShxdK5JSfA1+j8RqFAf02ud9ZbgHwoVlsvvXDdQGrU6LncJIrKNdUsv3jKDcPjLc+6Lw3rpZst5veXS5jWLg= 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=r8HHBOBo; arc=none smtp.client-ip=45.157.188.10 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="r8HHBOBo" Received: from smtp-3-0000.mail.infomaniak.ch (smtp-3-0000.mail.infomaniak.ch [10.4.36.107]) by smtp-3-3000.mail.infomaniak.ch (Postfix) with ESMTPS id 4XvyKh0XG5zj9g; Fri, 22 Nov 2024 15:34:32 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=digikod.net; s=20191114; t=1732286071; bh=zplGSbTIB54PAhZp2mb0y83mOQueBGo/UaXX9cN25G4=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=r8HHBOBoxJTalyzqRRUChQvrA0j19XWI4RyAyrcpebIx7xf4wESqk0DXKKZkl4LgQ 1wGQ6ZyOWgHUsKYfI0cemIeuUpAY3dPntjxxGV0hi4kkd9zf/3jt5EUSHn+yKlTs77 6j2YYaixRFk3IgNmNoHtaOACW/LkCzcjnLA84vcQ= Received: from unknown by smtp-3-0000.mail.infomaniak.ch (Postfix) with ESMTPA id 4XvyKg2NbzzjZ6; Fri, 22 Nov 2024 15:34:31 +0100 (CET) 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 , Francis Laniel , James Morris , Jann Horn , Jeff Xu , Jorge Lucangeli Obes , Kees Cook , Konstantin Meskhidze , Matt Bobrowski , Mikhail Ivanov , Phil Sutter , 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: [PATCH v3 23/23] selftests/landlock: Add audit tests for ptrace Date: Fri, 22 Nov 2024 15:33:53 +0100 Message-ID: <20241122143353.59367-24-mic@digikod.net> In-Reply-To: <20241122143353.59367-1-mic@digikod.net> References: <20241122143353.59367-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 tests for all ptrace actions. This improve all the ptrace tests by making sure that the restrictions comes from Landlock, and with the expected objects. These are like enhanced errno checks. Test coverage for security/landlock is 93.3% of 1604 lines according to gcc/gcov-14. Cc: G=C3=BCnther Noack Cc: Paul Moore Signed-off-by: Micka=C3=ABl Sala=C3=BCn Link: https://lore.kernel.org/r/20241122143353.59367-24-mic@digikod.net --- Changes since v2: - New patch. --- .../testing/selftests/landlock/ptrace_test.c | 62 +++++++++++++++++-- 1 file changed, 58 insertions(+), 4 deletions(-) diff --git a/tools/testing/selftests/landlock/ptrace_test.c b/tools/testing= /selftests/landlock/ptrace_test.c index a19db4d0b3bd..592927059cc3 100644 --- a/tools/testing/selftests/landlock/ptrace_test.c +++ b/tools/testing/selftests/landlock/ptrace_test.c @@ -4,6 +4,7 @@ * * Copyright =C2=A9 2017-2020 Micka=C3=ABl Sala=C3=BCn * Copyright =C2=A9 2019-2020 ANSSI + * Copyright =C2=A9 2024 Microsoft Corporation */ =20 #define _GNU_SOURCE @@ -17,6 +18,7 @@ #include #include =20 +#include "audit.h" #include "common.h" =20 /* Copied from security/yama/yama_lsm.c */ @@ -85,9 +87,27 @@ static int get_yama_ptrace_scope(void) return ret; } =20 -/* clang-format off */ -FIXTURE(hierarchy) {}; -/* clang-format on */ +static int matches_log_ptrace(struct __test_metadata *const _metadata, + struct audit_state *const state, const pid_t opid) +{ + static const char log_template[] =3D REGEX_LANDLOCK_PREFIX + " blockers=3Dptrace opid=3D%d ocomm=3D\"ptrace_test\"$"; + char log_match[sizeof(log_template) + 10]; + int log_match_len; + + log_match_len =3D + snprintf(log_match, sizeof(log_match), log_template, opid); + if (log_match_len > sizeof(log_match)) + return -E2BIG; + + // TODO: return -errno with AUDIT_SYSCALL + return !audit_match_record(state, AUDIT_LANDLOCK_DENY, log_match); +} + +FIXTURE(hierarchy) +{ + struct audit_state state; +}; =20 FIXTURE_VARIANT(hierarchy) { @@ -245,10 +265,15 @@ FIXTURE_VARIANT_ADD(hierarchy, deny_with_forked_domai= n) { =20 FIXTURE_SETUP(hierarchy) { + disable_caps(_metadata); + set_cap(_metadata, CAP_AUDIT_CONTROL); + EXPECT_EQ(0, audit_init(&self->state)); + clear_cap(_metadata, CAP_AUDIT_CONTROL); } =20 -FIXTURE_TEARDOWN(hierarchy) +FIXTURE_TEARDOWN_PARENT(hierarchy) { + EXPECT_EQ(0, audit_cleanup(NULL)); } =20 /* Test PTRACE_TRACEME and PTRACE_ATTACH for parent and child. */ @@ -261,6 +286,7 @@ TEST_F(hierarchy, trace) char buf_parent; long ret; bool can_read_child, can_trace_child, can_read_parent, can_trace_parent; + struct audit_records records; =20 yama_ptrace_scope =3D get_yama_ptrace_scope(); ASSERT_LE(0, yama_ptrace_scope); @@ -336,17 +362,26 @@ TEST_F(hierarchy, trace) err_proc_read =3D test_ptrace_read(parent); if (can_read_parent) { EXPECT_EQ(0, err_proc_read); + EXPECT_EQ(0, matches_log_ptrace(_metadata, &self->state, + parent)); } else { EXPECT_EQ(EACCES, err_proc_read); + EXPECT_EQ(1, matches_log_ptrace(_metadata, &self->state, + parent)); } =20 /* Tests PTRACE_ATTACH on the parent. */ ret =3D ptrace(PTRACE_ATTACH, parent, NULL, 0); if (can_trace_parent) { EXPECT_EQ(0, ret); + EXPECT_EQ(0, matches_log_ptrace(_metadata, &self->state, + parent)); } else { EXPECT_EQ(-1, ret); EXPECT_EQ(EPERM, errno); + EXPECT_EQ(!can_read_parent, + matches_log_ptrace(_metadata, &self->state, + parent)); } if (ret =3D=3D 0) { ASSERT_EQ(parent, waitpid(parent, &status, 0)); @@ -358,9 +393,15 @@ TEST_F(hierarchy, trace) ret =3D ptrace(PTRACE_TRACEME); if (can_trace_child) { EXPECT_EQ(0, ret); + EXPECT_EQ(0, matches_log_ptrace(_metadata, &self->state, + parent)); } else { EXPECT_EQ(-1, ret); EXPECT_EQ(EPERM, errno); + /* We should indeed see the parent process. */ + EXPECT_EQ(!can_read_child, + matches_log_ptrace(_metadata, &self->state, + parent)); } =20 /* @@ -408,17 +449,25 @@ TEST_F(hierarchy, trace) err_proc_read =3D test_ptrace_read(child); if (can_read_child) { EXPECT_EQ(0, err_proc_read); + EXPECT_EQ(0, + matches_log_ptrace(_metadata, &self->state, child)); } else { EXPECT_EQ(EACCES, err_proc_read); + EXPECT_EQ(1, + matches_log_ptrace(_metadata, &self->state, child)); } =20 /* Tests PTRACE_ATTACH on the child. */ ret =3D ptrace(PTRACE_ATTACH, child, NULL, 0); if (can_trace_child) { EXPECT_EQ(0, ret); + EXPECT_EQ(0, + matches_log_ptrace(_metadata, &self->state, child)); } else { EXPECT_EQ(-1, ret); EXPECT_EQ(EPERM, errno); + EXPECT_EQ(!can_read_child, + matches_log_ptrace(_metadata, &self->state, child)); } =20 if (ret =3D=3D 0) { @@ -434,6 +483,11 @@ TEST_F(hierarchy, trace) if (WIFSIGNALED(status) || !WIFEXITED(status) || WEXITSTATUS(status) !=3D EXIT_SUCCESS) _metadata->exit_code =3D KSFT_FAIL; + + /* Makes sure there is no superfluous logged records. */ + audit_count_records(&self->state, &records); + EXPECT_EQ(0, records.deny); + EXPECT_EQ(0, records.info); } =20 TEST_HARNESS_MAIN --=20 2.47.0