From: "Mickaël Salaün" <mic@digikod.net>
To: "Günther Noack" <gnoack@google.com>
Cc: "Mickaël Salaün" <mic@digikod.net>,
"Boqun Feng" <boqun.feng@gmail.com>,
"Ingo Molnar" <mingo@redhat.com>,
"Konstantin Meskhidze" <konstantin.meskhidze@huawei.com>,
"Matthieu Buffet" <matthieu@buffet.re>,
"Mikhail Ivanov" <ivanov.mikhail1@huawei-partners.com>,
"Peter Zijlstra" <peterz@infradead.org>,
"Shervin Oloumi" <enlightened@chromium.org>,
"Waiman Long" <longman@redhat.com>,
"Will Deacon" <will@kernel.org>,
linux-kernel@vger.kernel.org,
linux-security-module@vger.kernel.org
Subject: [PATCH v1 4/4] landlock: Use scoped guards for mutex
Date: Mon, 13 Jan 2025 17:11:12 +0100 [thread overview]
Message-ID: <20250113161112.452505-5-mic@digikod.net> (raw)
In-Reply-To: <20250113161112.452505-1-mic@digikod.net>
Simplify error handling by replacing goto statements with automatic
calls to mutex_unlock() when going out of scope.
Do not initialize the err variable for compiler/linter to warn us about
inconsistent use, if any.
Cc: Boqun Feng <boqun.feng@gmail.com>
Cc: Günther Noack <gnoack@google.com>
Cc: Ingo Molnar <mingo@redhat.com>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Waiman Long <longman@redhat.com>
Cc: Will Deacon <will@kernel.org>
Signed-off-by: Mickaël Salaün <mic@digikod.net>
Link: https://lore.kernel.org/r/20250113161112.452505-5-mic@digikod.net
---
security/landlock/ruleset.c | 52 +++++++++++++++----------------------
1 file changed, 21 insertions(+), 31 deletions(-)
diff --git a/security/landlock/ruleset.c b/security/landlock/ruleset.c
index f27b7bdb19b9..f1c3104aea6c 100644
--- a/security/landlock/ruleset.c
+++ b/security/landlock/ruleset.c
@@ -367,7 +367,7 @@ static int merge_tree(struct landlock_ruleset *const dst,
static int merge_ruleset(struct landlock_ruleset *const dst,
struct landlock_ruleset *const src)
{
- int err = 0;
+ int err;
might_sleep();
/* Should already be checked by landlock_merge_ruleset() */
@@ -378,32 +378,28 @@ static int merge_ruleset(struct landlock_ruleset *const dst,
return -EINVAL;
/* Locks @dst first because we are its only owner. */
- mutex_lock(&dst->lock);
- mutex_lock_nested(&src->lock, SINGLE_DEPTH_NESTING);
+ guard(mutex)(&dst->lock);
+ guard(mutex_nest_1)(&src->lock);
/* Stacks the new layer. */
- if (WARN_ON_ONCE(src->num_layers != 1 || dst->num_layers < 1)) {
- err = -EINVAL;
- goto out_unlock;
- }
+ if (WARN_ON_ONCE(src->num_layers != 1 || dst->num_layers < 1))
+ return -EINVAL;
+
dst->access_masks[dst->num_layers - 1] = src->access_masks[0];
/* Merges the @src inode tree. */
err = merge_tree(dst, src, LANDLOCK_KEY_INODE);
if (err)
- goto out_unlock;
+ return err;
#if IS_ENABLED(CONFIG_INET)
/* Merges the @src network port tree. */
err = merge_tree(dst, src, LANDLOCK_KEY_NET_PORT);
if (err)
- goto out_unlock;
+ return err;
#endif /* IS_ENABLED(CONFIG_INET) */
-out_unlock:
- mutex_unlock(&src->lock);
- mutex_unlock(&dst->lock);
- return err;
+ return 0;
}
static int inherit_tree(struct landlock_ruleset *const parent,
@@ -441,47 +437,41 @@ static int inherit_tree(struct landlock_ruleset *const parent,
static int inherit_ruleset(struct landlock_ruleset *const parent,
struct landlock_ruleset *const child)
{
- int err = 0;
+ int err;
might_sleep();
if (!parent)
return 0;
/* Locks @child first because we are its only owner. */
- mutex_lock(&child->lock);
- mutex_lock_nested(&parent->lock, SINGLE_DEPTH_NESTING);
+ guard(mutex)(&child->lock);
+ guard(mutex_nest_1)(&parent->lock);
/* Copies the @parent inode tree. */
err = inherit_tree(parent, child, LANDLOCK_KEY_INODE);
if (err)
- goto out_unlock;
+ return err;
#if IS_ENABLED(CONFIG_INET)
/* Copies the @parent network port tree. */
err = inherit_tree(parent, child, LANDLOCK_KEY_NET_PORT);
if (err)
- goto out_unlock;
+ return err;
#endif /* IS_ENABLED(CONFIG_INET) */
- if (WARN_ON_ONCE(child->num_layers <= parent->num_layers)) {
- err = -EINVAL;
- goto out_unlock;
- }
+ if (WARN_ON_ONCE(child->num_layers <= parent->num_layers))
+ return -EINVAL;
+
/* Copies the parent layer stack and leaves a space for the new layer. */
memcpy(child->access_masks, parent->access_masks,
flex_array_size(parent, access_masks, parent->num_layers));
- if (WARN_ON_ONCE(!parent->hierarchy)) {
- err = -EINVAL;
- goto out_unlock;
- }
+ if (WARN_ON_ONCE(!parent->hierarchy))
+ return -EINVAL;
+
get_hierarchy(parent->hierarchy);
child->hierarchy->parent = parent->hierarchy;
-
-out_unlock:
- mutex_unlock(&parent->lock);
- mutex_unlock(&child->lock);
- return err;
+ return 0;
}
static void free_ruleset(struct landlock_ruleset *const ruleset)
--
2.47.1
next prev parent reply other threads:[~2025-01-13 16:11 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-01-13 16:11 [PATCH v1 0/4] Use scoped guards on Landlock Mickaël Salaün
2025-01-13 16:11 ` [PATCH v1 1/4] landlock: Use scoped guards for ruleset Mickaël Salaün
2025-01-13 22:05 ` Günther Noack
2025-01-13 16:11 ` [PATCH v1 2/4] landlock: Use scoped guards for ruleset in landlock_add_rule() Mickaël Salaün
2025-01-13 22:06 ` Günther Noack
2025-01-13 16:11 ` [PATCH v1 3/4] locking/mutex: Add mutex_nest_1() scoped guard Mickaël Salaün
2025-01-13 16:11 ` Mickaël Salaün [this message]
2025-01-14 9:25 ` [PATCH v1 4/4] landlock: Use scoped guards for mutex Günther Noack
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20250113161112.452505-5-mic@digikod.net \
--to=mic@digikod.net \
--cc=boqun.feng@gmail.com \
--cc=enlightened@chromium.org \
--cc=gnoack@google.com \
--cc=ivanov.mikhail1@huawei-partners.com \
--cc=konstantin.meskhidze@huawei.com \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-security-module@vger.kernel.org \
--cc=longman@redhat.com \
--cc=matthieu@buffet.re \
--cc=mingo@redhat.com \
--cc=peterz@infradead.org \
--cc=will@kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®