From patchwork Mon Jan 13 16:11:09 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Micka=C3=ABl_Sala=C3=BCn?= X-Patchwork-Id: 13937721 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 B67B720AF68 for ; Mon, 13 Jan 2025 16:11:27 +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=1736784691; cv=none; b=dgm6MFOq0c7eIaoLtJvgqX1WetZwp9dWlvEGX4LK8qw8Kh7yovSPM1i1/MyaGWwv8aPNizdVcPwar1fEK8EDGSr9ullHHG6pw1/XgD/TIqONemK9H+a3qaD0D1wrpu1JcSitDZKsIo9kBaPDwJqqh/sce/zi4HT1xT1QUH7GPJ4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736784691; c=relaxed/simple; bh=FXG6ScdiLWonWuoDMzl1VnnlmIVi/bobbhJ2XoREFVA=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=IKVckETCgSsIDtrHLEUIJeOV/CqiG9c53MzpEbaY5lGx0jd4o/TTFpVSJRvEf+rw6x0icWq4d0MZAaU7THXUMOMC0/bBvqeIUBxwzKRXN4lvndcznGQVcuWPuIkLJ2FHZlfpAgfALRgEYTM+/Rpo1yd7mN9/vk8qxhLSXlWh+xY= 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=lin+IqWH; 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="lin+IqWH" 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 4YWy1N4s5fz147F; Mon, 13 Jan 2025 17:11:20 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=digikod.net; s=20191114; t=1736784680; bh=KA51zQCLP85BlI9Xb+JJNLsY2yl57zzkKQZ7FZ9fVzE=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=lin+IqWHsiJGe4CWe37+MPlKOrYYksTafmx6dUoViHz2u6Rme3goPm0a0HV9Py7XR v9tUcASxGrWjuE0gSROsyr3lm/SucEJ+XmCSfq7rU/0LkFBlg13wmJeJ2VYs7SXRQZ gzQKHvNcZ5NQRYsNq1DBA3r5Qj4O5Ze6IePljtKY= Received: from unknown by smtp-3-0001.mail.infomaniak.ch (Postfix) with ESMTPA id 4YWy1N0RRbzVJk; Mon, 13 Jan 2025 17:11:20 +0100 (CET) From: =?utf-8?q?Micka=C3=ABl_Sala=C3=BCn?= To: =?utf-8?q?G=C3=BCnther_Noack?= Cc: =?utf-8?q?Micka=C3=ABl_Sala=C3=BCn?= , Boqun Feng , Ingo Molnar , Konstantin Meskhidze , Matthieu Buffet , Mikhail Ivanov , Peter Zijlstra , Shervin Oloumi , Waiman Long , Will Deacon , linux-kernel@vger.kernel.org, linux-security-module@vger.kernel.org Subject: [PATCH v1 1/4] landlock: Use scoped guards for ruleset Date: Mon, 13 Jan 2025 17:11:09 +0100 Message-ID: <20250113161112.452505-2-mic@digikod.net> In-Reply-To: <20250113161112.452505-1-mic@digikod.net> References: <20250113161112.452505-1-mic@digikod.net> Precedence: bulk X-Mailing-List: linux-security-module@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Infomaniak-Routing: alpha Simplify error handling by replacing goto statements with automatic calls to landlock_put_ruleset() when going out of scope. This change will be easy to backport to v6.6 if needed, only the kernel.h include line conflicts. As for any other similar changes, we should be careful when backporting without goto statements. Add missing include file. Cc: Günther Noack Signed-off-by: Mickaël Salaün Link: https://lore.kernel.org/r/20250113161112.452505-2-mic@digikod.net Reviewed-by: Günther Noack --- security/landlock/ruleset.c | 22 ++++++++++------------ security/landlock/ruleset.h | 5 +++++ security/landlock/syscalls.c | 25 ++++++++----------------- 3 files changed, 23 insertions(+), 29 deletions(-) diff --git a/security/landlock/ruleset.c b/security/landlock/ruleset.c index a93bdbf52fff..f27b7bdb19b9 100644 --- a/security/landlock/ruleset.c +++ b/security/landlock/ruleset.c @@ -8,11 +8,13 @@ #include #include +#include #include #include #include #include #include +#include #include #include #include @@ -537,7 +539,7 @@ struct landlock_ruleset * landlock_merge_ruleset(struct landlock_ruleset *const parent, struct landlock_ruleset *const ruleset) { - struct landlock_ruleset *new_dom; + struct landlock_ruleset *new_dom __free(landlock_put_ruleset) = NULL; u32 num_layers; int err; @@ -557,29 +559,25 @@ landlock_merge_ruleset(struct landlock_ruleset *const parent, new_dom = create_ruleset(num_layers); if (IS_ERR(new_dom)) return new_dom; + new_dom->hierarchy = kzalloc(sizeof(*new_dom->hierarchy), GFP_KERNEL_ACCOUNT); - if (!new_dom->hierarchy) { - err = -ENOMEM; - goto out_put_dom; - } + if (!new_dom->hierarchy) + return ERR_PTR(-ENOMEM); + refcount_set(&new_dom->hierarchy->usage, 1); /* ...as a child of @parent... */ err = inherit_ruleset(parent, new_dom); if (err) - goto out_put_dom; + return ERR_PTR(err); /* ...and including @ruleset. */ err = merge_ruleset(new_dom, ruleset); if (err) - goto out_put_dom; - - return new_dom; + return ERR_PTR(err); -out_put_dom: - landlock_put_ruleset(new_dom); - return ERR_PTR(err); + return no_free_ptr(new_dom); } /* diff --git a/security/landlock/ruleset.h b/security/landlock/ruleset.h index 631e24d4ffe9..70e5b53d1c71 100644 --- a/security/landlock/ruleset.h +++ b/security/landlock/ruleset.h @@ -11,6 +11,8 @@ #include #include +#include +#include #include #include #include @@ -252,6 +254,9 @@ landlock_create_ruleset(const access_mask_t access_mask_fs, void landlock_put_ruleset(struct landlock_ruleset *const ruleset); void landlock_put_ruleset_deferred(struct landlock_ruleset *const ruleset); +DEFINE_FREE(landlock_put_ruleset, struct landlock_ruleset *, + if (!IS_ERR_OR_NULL(_T)) landlock_put_ruleset(_T)) + int landlock_insert_rule(struct landlock_ruleset *const ruleset, const struct landlock_id id, const access_mask_t access); diff --git a/security/landlock/syscalls.c b/security/landlock/syscalls.c index 4ed8e70c25ed..5a7f1f77292e 100644 --- a/security/landlock/syscalls.c +++ b/security/landlock/syscalls.c @@ -10,6 +10,7 @@ #include #include #include +#include #include #include #include @@ -456,10 +457,10 @@ SYSCALL_DEFINE4(landlock_add_rule, const int, ruleset_fd, SYSCALL_DEFINE2(landlock_restrict_self, const int, ruleset_fd, const __u32, flags) { - struct landlock_ruleset *new_dom, *ruleset; + struct landlock_ruleset *new_dom, + *ruleset __free(landlock_put_ruleset) = NULL; struct cred *new_cred; struct landlock_cred_security *new_llcred; - int err; if (!is_initialized()) return -EOPNOTSUPP; @@ -483,10 +484,9 @@ SYSCALL_DEFINE2(landlock_restrict_self, const int, ruleset_fd, const __u32, /* Prepares new credentials. */ new_cred = prepare_creds(); - if (!new_cred) { - err = -ENOMEM; - goto out_put_ruleset; - } + if (!new_cred) + return -ENOMEM; + new_llcred = landlock_cred(new_cred); /* @@ -495,21 +495,12 @@ SYSCALL_DEFINE2(landlock_restrict_self, const int, ruleset_fd, const __u32, */ new_dom = landlock_merge_ruleset(new_llcred->domain, ruleset); if (IS_ERR(new_dom)) { - err = PTR_ERR(new_dom); - goto out_put_creds; + abort_creds(new_cred); + return PTR_ERR(new_dom); } /* Replaces the old (prepared) domain. */ landlock_put_ruleset(new_llcred->domain); new_llcred->domain = new_dom; - - landlock_put_ruleset(ruleset); return commit_creds(new_cred); - -out_put_creds: - abort_creds(new_cred); - -out_put_ruleset: - landlock_put_ruleset(ruleset); - return err; } From patchwork Mon Jan 13 16:11:10 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Micka=C3=ABl_Sala=C3=BCn?= X-Patchwork-Id: 13937722 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 B7B9D20AF71 for ; Mon, 13 Jan 2025 16:11:29 +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=1736784692; cv=none; b=MP+XvKWAzgQ3IJw4O0cdywse1Ier9masLyMYls/mn/U1yGuEkKWeJ6NFKPwIOyv9DZBxEeSkoeuqwUcdsuPkWBo9AhvHLA4v0lS3Pvr+mjkUEv8tScWc4k/fvhgnYkuyfBwLXB4JotaCzsVIQFc2zSMKEHgAyCgZ9neN0icDabM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736784692; c=relaxed/simple; bh=e7EajQHBGxTlpsWtbyDOw7pIsgGDBwUT7WTk0sLggUE=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=J16iYYwYmHKFm9BLYd9R7ms4PfmsYagfxkybsZSUV3BbiHnUuBqqP1K+0/3wYUkwscfe266Vo7EhbWD2qSxHYKhgpMjHQ2wnLcvXVBLl+0zD5VJoXjCU7hdqWe8qokQvjrMgjBsVNoJkyF90pBnmS8nKyc1yWZUEYzkfcWhQiOI= 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=LkwwU1dF; 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="LkwwU1dF" 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 4YWy1P6695z12kn; Mon, 13 Jan 2025 17:11:21 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=digikod.net; s=20191114; t=1736784681; bh=aejfFX/wCqISAf4ObXFXZmBBY776r83KhR6yOC8AcQ0=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=LkwwU1dFSVWTwo7QkKZpXioqyA0KUIWjz811gnY0mGTogVnVKyUmpBb217vQvb3xn xOAmal7mbxYjjokwMB+oVGPSBYdIOUX5vi/uq0bQg6vXyx8D06bMh/6Qc7T1BmVd8T hEHMm0t6df7Ob/KZt/fvyv8N2KcQSyY6pgeeSJzo= Received: from unknown by smtp-3-0001.mail.infomaniak.ch (Postfix) with ESMTPA id 4YWy1P2bL4zSVx; Mon, 13 Jan 2025 17:11:21 +0100 (CET) From: =?utf-8?q?Micka=C3=ABl_Sala=C3=BCn?= To: =?utf-8?q?G=C3=BCnther_Noack?= Cc: =?utf-8?q?Micka=C3=ABl_Sala=C3=BCn?= , Boqun Feng , Ingo Molnar , Konstantin Meskhidze , Matthieu Buffet , Mikhail Ivanov , Peter Zijlstra , Shervin Oloumi , Waiman Long , Will Deacon , linux-kernel@vger.kernel.org, linux-security-module@vger.kernel.org Subject: [PATCH v1 2/4] landlock: Use scoped guards for ruleset in landlock_add_rule() Date: Mon, 13 Jan 2025 17:11:10 +0100 Message-ID: <20250113161112.452505-3-mic@digikod.net> In-Reply-To: <20250113161112.452505-1-mic@digikod.net> References: <20250113161112.452505-1-mic@digikod.net> Precedence: bulk X-Mailing-List: linux-security-module@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Infomaniak-Routing: alpha Simplify error handling by replacing goto statements with automatic calls to landlock_put_ruleset() when going out of scope. This change depends on the TCP support. Cc: Günther Noack Cc: Konstantin Meskhidze Cc: Mikhail Ivanov Signed-off-by: Mickaël Salaün Link: https://lore.kernel.org/r/20250113161112.452505-3-mic@digikod.net Reviewed-by: Günther Noack --- security/landlock/syscalls.c | 14 ++++---------- 1 file changed, 4 insertions(+), 10 deletions(-) diff --git a/security/landlock/syscalls.c b/security/landlock/syscalls.c index 5a7f1f77292e..a9760d252fc2 100644 --- a/security/landlock/syscalls.c +++ b/security/landlock/syscalls.c @@ -399,8 +399,7 @@ SYSCALL_DEFINE4(landlock_add_rule, const int, ruleset_fd, const enum landlock_rule_type, rule_type, const void __user *const, rule_attr, const __u32, flags) { - struct landlock_ruleset *ruleset; - int err; + struct landlock_ruleset *ruleset __free(landlock_put_ruleset) = NULL; if (!is_initialized()) return -EOPNOTSUPP; @@ -416,17 +415,12 @@ SYSCALL_DEFINE4(landlock_add_rule, const int, ruleset_fd, switch (rule_type) { case LANDLOCK_RULE_PATH_BENEATH: - err = add_rule_path_beneath(ruleset, rule_attr); - break; + return add_rule_path_beneath(ruleset, rule_attr); case LANDLOCK_RULE_NET_PORT: - err = add_rule_net_port(ruleset, rule_attr); - break; + return add_rule_net_port(ruleset, rule_attr); default: - err = -EINVAL; - break; + return -EINVAL; } - landlock_put_ruleset(ruleset); - return err; } /* Enforcement */ From patchwork Mon Jan 13 16:11:11 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Micka=C3=ABl_Sala=C3=BCn?= X-Patchwork-Id: 13937723 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 390FD20AF7B for ; Mon, 13 Jan 2025 16:11:31 +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=1736784694; cv=none; b=dUwcGQ0zBpJObWHmFKTU7qsWOe7Eogv2eMn/JHTyT80ebm/eTJ5dGLuC3Mkp58vimnqBuCjiO8JTMHcgIxC+O5qXNw53xwC8F0WzSuS7tACA0uJUjYn8o8z80pXqocpW2olVXv2X0quMpP/y6snFs90iUTf4MbsnqngpQ7BoA+s= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736784694; c=relaxed/simple; bh=UZgvM03awjIpbC4H/Wvaz6VduSHZQVPdX396iCrQ+Xw=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=TfFMz6hwehJW7emt2tkuiaurf78RoqVbBgsbyDy67+6cq3FN5Z1te7Ljes6VFsedICOPWmNjZGH1NS6WUXTWZtQhQWzbm6tcy8NFv/a2M1/5D1rel1kCXUClHVh0eZIMp6ymBg1cowTjXhl+XrFdM8MMWWH6KTvU17FF4TTZ8L0= 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=Tzngh41M; 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="Tzngh41M" 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 4YWy1R0DFqz14fy; Mon, 13 Jan 2025 17:11:23 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=digikod.net; s=20191114; t=1736784682; bh=eZeZ251GTZimYXCS1aJ9UsfNnMVgf5Hun1kwh684OzI=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Tzngh41Msv9tKwGuk48vYjlEHtWCLYQVZEwx2O0ImwxbA7cRMoUTcvgZDQXTXhK2W Nm9BIxXZJ2QnDypbdxGXOO2uMZVJpxCJcABRjqOXfFQV5e60O9J3t/CyUUzXXVU4jN o6uA2fdpD6yXTkGdvuN6KTsk0go+wOIvTTOR7PMI= Received: from unknown by smtp-3-0000.mail.infomaniak.ch (Postfix) with ESMTPA id 4YWy1Q3qZqz2Fn; Mon, 13 Jan 2025 17:11:22 +0100 (CET) From: =?utf-8?q?Micka=C3=ABl_Sala=C3=BCn?= To: =?utf-8?q?G=C3=BCnther_Noack?= Cc: =?utf-8?q?Micka=C3=ABl_Sala=C3=BCn?= , Boqun Feng , Ingo Molnar , Konstantin Meskhidze , Matthieu Buffet , Mikhail Ivanov , Peter Zijlstra , Shervin Oloumi , Waiman Long , Will Deacon , linux-kernel@vger.kernel.org, linux-security-module@vger.kernel.org Subject: [PATCH v1 3/4] locking/mutex: Add mutex_nest_1() scoped guard Date: Mon, 13 Jan 2025 17:11:11 +0100 Message-ID: <20250113161112.452505-4-mic@digikod.net> In-Reply-To: <20250113161112.452505-1-mic@digikod.net> References: <20250113161112.452505-1-mic@digikod.net> Precedence: bulk X-Mailing-List: linux-security-module@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Infomaniak-Routing: alpha Several places use a mutex with SINGLE_DEPTH_NESTING, but there is no proper way to use a scoped guard with that. Add a mutex_nest_1() scoped guard that call metex_lock() with SINGLE_DEPTH_NESTING (defined in lockdep.h). This will initially by used by Landlock in a following commit. Cc: Boqun Feng Cc: Günther Noack Cc: Ingo Molnar Cc: Peter Zijlstra Cc: Waiman Long Cc: Will Deacon Signed-off-by: Mickaël Salaün Link: https://lore.kernel.org/r/20250113161112.452505-4-mic@digikod.net --- include/linux/mutex.h | 2 ++ 1 file changed, 2 insertions(+) diff --git a/include/linux/mutex.h b/include/linux/mutex.h index 2bf91b57591b..dfd128a3f365 100644 --- a/include/linux/mutex.h +++ b/include/linux/mutex.h @@ -199,6 +199,8 @@ extern void mutex_unlock(struct mutex *lock); extern int atomic_dec_and_mutex_lock(atomic_t *cnt, struct mutex *lock); DEFINE_GUARD(mutex, struct mutex *, mutex_lock(_T), mutex_unlock(_T)) +DEFINE_GUARD(mutex_nest_1, struct mutex *, + mutex_lock_nested(_T, SINGLE_DEPTH_NESTING), mutex_unlock(_T)) DEFINE_GUARD_COND(mutex, _try, mutex_trylock(_T)) DEFINE_GUARD_COND(mutex, _intr, mutex_lock_interruptible(_T) == 0) From patchwork Mon Jan 13 16:11:12 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Micka=C3=ABl_Sala=C3=BCn?= X-Patchwork-Id: 13937724 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 C097420F092 for ; Mon, 13 Jan 2025 16:11:31 +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=1736784694; cv=none; b=BOfzgd/pcYZlA2ScboZ7iD47/YMh31NPrXH/SOk7qdXNJGjPuDbbhdrgHA3+PrgTpGMPYLdGcRMBd+GlHW8jBe0fYvX5TqfUSmpqLagICMOVvkwQCWGdbTUXy+NjPgBXE33jSzL8ozSBxpBJxFioD6mGB8+/m9Hld8cq6YYCRpc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736784694; c=relaxed/simple; bh=8TLaqHxOAqnpOnK0y/4l9elkW6+voC28qWJNnf/smEA=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=u80+ZNc3CvooPJ+h69fUgrAVVe92KE0IJwZ6sLokr5S7hy1v1hcMo9XPlrZFYo6+RjtteRquyZs1WTRoN1lfxARxS3kyQ0Gad5dWpQP29mBhvseNR91bG01V+8+3KNXgZ13Y0wwjGMJu1JtoP9XO7KfcVPczzI4sFzTJdq1bRRc= 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=Kn/tRBTy; 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="Kn/tRBTy" 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 4YWy1S22bQz169r; Mon, 13 Jan 2025 17:11:24 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=digikod.net; s=20191114; t=1736784684; bh=/rhNS+Hm78vBKNItT+NMBCUhZo36eNGFBlgsINUtEN0=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Kn/tRBTyC1Cp0cgsNvqz311E+cNcBWvQVqax5eJHGlq+Yy5DjDg1BumPfAK0S1JuK oDoSd/K/VL0pGxFksD7JR+Q3jfAfDRvX/0KViwyTwhcXmcUzyOAf8JG4TiToxrp0yo Gm/noGlOaH4OaB8HnbWX3v2UAFtdtkT4WB/cuSI4= Received: from unknown by smtp-3-0001.mail.infomaniak.ch (Postfix) with ESMTPA id 4YWy1R58vYzRbL; Mon, 13 Jan 2025 17:11:23 +0100 (CET) From: =?utf-8?q?Micka=C3=ABl_Sala=C3=BCn?= To: =?utf-8?q?G=C3=BCnther_Noack?= Cc: =?utf-8?q?Micka=C3=ABl_Sala=C3=BCn?= , Boqun Feng , Ingo Molnar , Konstantin Meskhidze , Matthieu Buffet , Mikhail Ivanov , Peter Zijlstra , Shervin Oloumi , Waiman Long , Will Deacon , 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 Message-ID: <20250113161112.452505-5-mic@digikod.net> In-Reply-To: <20250113161112.452505-1-mic@digikod.net> References: <20250113161112.452505-1-mic@digikod.net> Precedence: bulk X-Mailing-List: linux-security-module@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Infomaniak-Routing: alpha 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 Cc: Günther Noack Cc: Ingo Molnar Cc: Peter Zijlstra Cc: Waiman Long Cc: Will Deacon Signed-off-by: Mickaël Salaün Link: https://lore.kernel.org/r/20250113161112.452505-5-mic@digikod.net Reviewed-by: Günther Noack --- 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)