From nobody Fri Dec 19 08:07:12 2025 Received: from smtp-190c.mail.infomaniak.ch (smtp-190c.mail.infomaniak.ch [185.125.25.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 F3DB41F2C4C for ; Fri, 31 Jan 2025 16:31:35 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=185.125.25.12 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1738341099; cv=none; b=TkULwxlz9AI08DODM/fphbe71Ft8i2fB6xNoKaQ4IaBti3LpBAbi2wLpVuRCv+2iv612EezLTQ/FS5SqHW/JyqwksbJCMORLZGxSLoSdpj/fAAaZ23mdh3a1/S26jQ9h28skdtTCllRffhcj7p33nGElsCXOHyz350t2e6PUPkY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1738341099; c=relaxed/simple; bh=I8woJ3SyjtgGPYZKUdUy9dzDMdWbMHtKx9NutBD74KA=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=M11KAZKJ0Y1lHu8y7SuKq0UEKgU7A1vOUY1XpEoa12FFR6xeHn6JaHH3peSkD0oboAXmYE52QSJ04DgWszbUf/kPiGWej6laBRO8p6f0icyK1rMb3P516v5LUHZRtgjjL00AtQkQa11Ru1PnmyXjoRXMMZqKhT9q63y7+TnZZgw= 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=Hk1BoMdW; arc=none smtp.client-ip=185.125.25.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="Hk1BoMdW" 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 4Yl1cQ11RKzQty; Fri, 31 Jan 2025 17:31:34 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=digikod.net; s=20191114; t=1738341094; bh=N2kD3NXgW5ye/IpgX+oZFK3FVpNqjZpF79u2QKfOPMU=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Hk1BoMdWF1DLylII1XvZNeUtsC6dZuskNfh3BH9EFCKD44XovBtKXJ0TVjfZ7bq/X Bu4iUC+B5Tl9KtnObHYNPRpus8wsB/RUdO5BIQfDyY6bJq96rT7PEWN/DqZvJZxji4 Na7PT0LWQUitlkcwelp8MNU7JTv2CmuPs7xo6WkI= Received: from unknown by smtp-3-0001.mail.infomaniak.ch (Postfix) with ESMTPA id 4Yl1cN72fnzcxK; Fri, 31 Jan 2025 17:31:32 +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 , Daniel Burgener , 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 , Tyler Hicks , audit@vger.kernel.org, linux-kernel@vger.kernel.org, linux-security-module@vger.kernel.org Subject: [PATCH v5 11/24] landlock: Log mount-related denials Date: Fri, 31 Jan 2025 17:30:46 +0100 Message-ID: <20250131163059.1139617-12-mic@digikod.net> In-Reply-To: <20250131163059.1139617-1-mic@digikod.net> References: <20250131163059.1139617-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. The new related blocker is "fs.change_layout". Audit event sample: type=3DLANDLOCK_DENY msg=3Daudit(1729738800.349:44): domain=3D195ba459b b= lockers=3Dfs.change_layout name=3D"/" dev=3D"tmpfs" ino=3D1 Remove landlock_get_applicable_domain() and get_current_fs_domain() which are now fully replaced with landlock_get_applicable_subject(). Cc: G=C3=BCnther Noack Signed-off-by: Micka=C3=ABl Sala=C3=BCn Link: https://lore.kernel.org/r/20250131163059.1139617-12-mic@digikod.net --- Changes since v4: - Rebase on top of the landlock_log_denial() and subject type changes. - Fix off-by-one error in landlock_match_layer_level(), now merged into landlock_get_applicable_domain(). Changes since v3: - Cosmetic change to the "fs.change_layout" name. 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 | 85 ++++++++++++++++++++++++++++++++----- security/landlock/ruleset.h | 30 ------------- 4 files changed, 78 insertions(+), 41 deletions(-) diff --git a/security/landlock/audit.c b/security/landlock/audit.c index a5b055306757..091b7cba7e9f 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 0608241eb7e1..258b7e3cd9a5 100644 --- a/security/landlock/audit.h +++ b/security/landlock/audit.h @@ -16,6 +16,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 276cbcffe6f5..1370641eb0a4 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" @@ -393,12 +395,6 @@ static const struct access_masks any_fs =3D { .fs =3D ~0, }; =20 -static const struct landlock_ruleset *get_current_fs_domain(void) -{ - return landlock_get_applicable_domain(landlock_get_current_domain(), - any_fs); -} - /* * Check that a destination file hierarchy has more restrictions than a so= urce * file hierarchy. This is only used for link and rename actions. @@ -1333,6 +1329,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_cred_security *const subje= ct, + size_t handle_layer, 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 handle_layer + 1, + }; + + landlock_log_denial(subject, &request); +} + +static void +log_fs_change_layout_dentry(const struct landlock_cred_security *const sub= ject, + size_t handle_layer, 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 handle_layer + 1, + }; + + landlock_log_denial(subject, &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 @@ -1355,16 +1383,30 @@ 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()) + size_t handle_layer; + const struct landlock_cred_security *const subject =3D + landlock_get_applicable_subject(current_cred(), any_fs, + &handle_layer); + + if (!subject) return 0; + + log_fs_change_layout_path(subject, handle_layer, 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()) + size_t handle_layer; + const struct landlock_cred_security *const subject =3D + landlock_get_applicable_subject(current_cred(), any_fs, + &handle_layer); + + if (!subject) return 0; + + log_fs_change_layout_path(subject, handle_layer, to_path); return -EPERM; } =20 @@ -1374,15 +1416,29 @@ 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()) + size_t handle_layer; + const struct landlock_cred_security *const subject =3D + landlock_get_applicable_subject(current_cred(), any_fs, + &handle_layer); + + if (!subject) return 0; + + log_fs_change_layout_dentry(subject, handle_layer, 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()) + size_t handle_layer; + const struct landlock_cred_security *const subject =3D + landlock_get_applicable_subject(current_cred(), any_fs, + &handle_layer); + + if (!subject) return 0; + + log_fs_change_layout_dentry(subject, handle_layer, sb->s_root); return -EPERM; } =20 @@ -1397,8 +1453,15 @@ 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()) + size_t handle_layer; + const struct landlock_cred_security *const subject =3D + landlock_get_applicable_subject(current_cred(), any_fs, + &handle_layer); + + if (!subject) return 0; + + log_fs_change_layout_path(subject, handle_layer, new_path); return -EPERM; } =20 diff --git a/security/landlock/ruleset.h b/security/landlock/ruleset.h index bbb5996545d2..27a4f92ae82c 100644 --- a/security/landlock/ruleset.h +++ b/security/landlock/ruleset.h @@ -242,36 +242,6 @@ landlock_union_access_masks(const struct landlock_rule= set *const domain) return matches.masks; } =20 -/** - * landlock_get_applicable_domain - Return @domain if it applies to (handl= es) - * at least one of the access rights specified - * in @masks - * - * @domain: Landlock ruleset (used as a domain) - * @masks: access masks - * - * Returns: @domain if any access rights specified in @masks is handled, or - * NULL otherwise. - */ -static inline const struct landlock_ruleset * -landlock_get_applicable_domain(const struct landlock_ruleset *const domain, - const struct access_masks masks) -{ - const union access_masks_all masks_all =3D { - .masks =3D masks, - }; - union access_masks_all merge =3D {}; - - if (!domain) - return NULL; - - merge.masks =3D landlock_union_access_masks(domain); - if (merge.all & masks_all.all) - return domain; - - return NULL; -} - static inline void landlock_add_fs_access_mask(struct landlock_ruleset *const ruleset, const access_mask_t fs_access_mask, --=20 2.48.1