From nobody Thu Dec 18 11:45:14 2025 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 6CA411FBBEA for ; Mon, 13 Jan 2025 16:11:26 +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=1736784691; cv=none; b=ouOGKB5Tw700PWe9lRrEAqV4y+2dqFATYVIvP5HgmWiou7CKP7h/3gay2k9iCfhF3Z8zGn1wbYP4NpkSbTk9ajUnJF9Ia4JajvLyWCzag9/13FZhRd2G5R5M7tw8dXHgfHQ1DW0nEOrv4ccZAyr0Wc3Se78JJgkmJasvDa06TPY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736784691; 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=pB0cQzTsZh5oJoNf6xkTnkiCmORuxmHzu2ZyO9dzyQdIzh6lxIYf+T1qh1wo1eEjssMeqh3Wj+yispDPSYURzEldFXhaiXUsXS8GC/6/9p5l4XPJASE3g0Au0ilk7qPq349qsPqcgRaa7l+NayZBNhLLqFru7BwK4woNNvY0ylk= 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=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="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=20Sala=C3=BCn?= To: =?UTF-8?q?G=C3=BCnther=20Noack?= Cc: =?UTF-8?q?Micka=C3=ABl=20Sala=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-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 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=C3=BCnther Noack Cc: Ingo Molnar Cc: Peter Zijlstra Cc: Waiman Long Cc: Will Deacon Signed-off-by: Micka=C3=ABl Sala=C3=BCn Link: https://lore.kernel.org/r/20250113161112.452505-5-mic@digikod.net Reviewed-by: G=C3=BCnther 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 ds= t, static int merge_ruleset(struct landlock_ruleset *const dst, struct landlock_ruleset *const src) { - int err =3D 0; + int err; =20 might_sleep(); /* Should already be checked by landlock_merge_ruleset() */ @@ -378,32 +378,28 @@ static int merge_ruleset(struct landlock_ruleset *con= st dst, return -EINVAL; =20 /* 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); =20 /* Stacks the new layer. */ - if (WARN_ON_ONCE(src->num_layers !=3D 1 || dst->num_layers < 1)) { - err =3D -EINVAL; - goto out_unlock; - } + if (WARN_ON_ONCE(src->num_layers !=3D 1 || dst->num_layers < 1)) + return -EINVAL; + dst->access_masks[dst->num_layers - 1] =3D src->access_masks[0]; =20 /* Merges the @src inode tree. */ err =3D merge_tree(dst, src, LANDLOCK_KEY_INODE); if (err) - goto out_unlock; + return err; =20 #if IS_ENABLED(CONFIG_INET) /* Merges the @src network port tree. */ err =3D merge_tree(dst, src, LANDLOCK_KEY_NET_PORT); if (err) - goto out_unlock; + return err; #endif /* IS_ENABLED(CONFIG_INET) */ =20 -out_unlock: - mutex_unlock(&src->lock); - mutex_unlock(&dst->lock); - return err; + return 0; } =20 static int inherit_tree(struct landlock_ruleset *const parent, @@ -441,47 +437,41 @@ static int inherit_tree(struct landlock_ruleset *cons= t parent, static int inherit_ruleset(struct landlock_ruleset *const parent, struct landlock_ruleset *const child) { - int err =3D 0; + int err; =20 might_sleep(); if (!parent) return 0; =20 /* 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); =20 /* Copies the @parent inode tree. */ err =3D inherit_tree(parent, child, LANDLOCK_KEY_INODE); if (err) - goto out_unlock; + return err; =20 #if IS_ENABLED(CONFIG_INET) /* Copies the @parent network port tree. */ err =3D inherit_tree(parent, child, LANDLOCK_KEY_NET_PORT); if (err) - goto out_unlock; + return err; #endif /* IS_ENABLED(CONFIG_INET) */ =20 - if (WARN_ON_ONCE(child->num_layers <=3D parent->num_layers)) { - err =3D -EINVAL; - goto out_unlock; - } + if (WARN_ON_ONCE(child->num_layers <=3D 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)); =20 - if (WARN_ON_ONCE(!parent->hierarchy)) { - err =3D -EINVAL; - goto out_unlock; - } + if (WARN_ON_ONCE(!parent->hierarchy)) + return -EINVAL; + get_hierarchy(parent->hierarchy); child->hierarchy->parent =3D parent->hierarchy; - -out_unlock: - mutex_unlock(&parent->lock); - mutex_unlock(&child->lock); - return err; + return 0; } =20 static void free_ruleset(struct landlock_ruleset *const ruleset) --=20 2.47.1