From: "Günther Noack" <gnoack3000@gmail.com>
To: "Alejandro Colomar" <alx.manpages@gmail.com>,
"Mickaël Salaün" <mic@digikod.net>
Cc: Michael Kerrisk <mtk.manpages@gmail.com>, linux-man@vger.kernel.org
Subject: Re: [PATCH v5 3/3] landlock.7: Explain the best-effort fallback mechanism in the example
Date: Fri, 24 Mar 2023 19:24:52 +0100 [thread overview]
Message-ID: <20230324.449c0a64f654@gnoack.org> (raw)
In-Reply-To: <20230324172419.117632-3-gnoack3000@gmail.com>
P.S.: I went back and forth a bit with the example,
but ended up implementing the full backwards compatibility
across all three existing Landlock versions for now.
The example corresponds to the simple case 1 from
https://blog.gnoack.org/post/landlock-best-effort/,
where the program does *not* need the refer right
after Landlock restriction.
This is not much more complicated than the variant
which is compatible with ABI V2 and V3.
Below the new part of the example,
I added a paragraph to talk about case 2,
where the program *does* need the refer right.
I hope this will be enough to keep most people
from implementing the fallback the wrong way.
Case 3 from the weblog article (where it can be either)
can be deduced with normal logic reasoning and hopefully
should not need additional explanation. -- You need
to figure out at runtime whether you are in case 1 or 2,
and then apply the respective logic.
Let me know what you think!
–Günther
On Fri, Mar 24, 2023 at 06:24:19PM +0100, Günther Noack wrote:
> Signed-off-by: Günther Noack <gnoack3000@gmail.com>
> ---
> man7/landlock.7 | 65 ++++++++++++++++++++++++++++++++++++++++++++++---
> 1 file changed, 61 insertions(+), 4 deletions(-)
>
> diff --git a/man7/landlock.7 b/man7/landlock.7
> index 9c305edef..d1214ba27 100644
> --- a/man7/landlock.7
> +++ b/man7/landlock.7
> @@ -393,11 +393,14 @@ accessible through these system call families:
> Future Landlock evolutions will enable to restrict them.
> .SH EXAMPLES
> We first need to create the ruleset that will contain our rules.
> +.PP
> For this example,
> the ruleset will contain rules that only allow read actions,
> but write actions will be denied.
> The ruleset then needs to handle both of these kinds of actions.
> -See below for the description of filesystem actions.
> +See the
> +.B DESCRIPTION
> +section for the description of filesystem actions.
> .PP
> .in +4n
> .EX
> @@ -420,7 +423,57 @@ attr.handled_access_fs =
> LANDLOCK_ACCESS_FS_MAKE_SYM |
> LANDLOCK_ACCESS_FS_REFER |
> LANDLOCK_ACCESS_FS_TRUNCATE;
> +.EE
> +.in
> +.PP
> +To be compatible with older Linux versions,
> +we detect the available Landlock ABI version,
> +and only use the available subset of access rights:
> +.PP
> +.in +4n
> +.EX
> +/* Table of available file system access rights by ABI version */
> +__u64 landlock_fs_access_rights[] = {
> + (1ULL << 13) \- 1, /* ABI v1 */
> + (1ULL << 14) \- 1, /* ABI v2: add "refer" */
> + (1ULL << 15) \- 1, /* ABI v3: add "truncate" */
> +};
> +
> +int abi = landlock_create_ruleset(NULL, 0,
> + LANDLOCK_CREATE_RULESET_VERSION);
> +if (abi <= 0) {
> + perror("Giving up \- No Landlock support");
> + exit(EXIT_FAILURE);
> +}
> +if (abi > 3)
> + abi = 3;
>
> +/* Only use the available rights in the ruleset. */
> +attr.handled_access_fs &= landlock_fs_access_rights[abi \- 1];
> +.EE
> +.in
> +.PP
> +The available access rights for each ABI version are listed in the
> +.B VERSIONS
> +section.
> +.PP
> +If our program needed to create hard links or rename files between different directories
> +.RB ( LANDLOCK_ACCESS_FS_REFER ),
> +we would require the following change to the backwards compatibility logic:
> +Directory reparenting is not possible in a process restricted with Landlock ABI version 1.
> +Therefore,
> +if the program needed to do file reparenting,
> +and if only Landlock ABI version 1 was available,
> +we could not restrict the process.
> +.PP
> +Now that the ruleset attributes are determined,
> +we create the Landlock ruleset
> +and acquire a file descriptor as a handle to it,
> +using
> +.BR landlock_create_ruleset (2):
> +.PP
> +.in +4n
> +.EX
> ruleset_fd = landlock_create_ruleset(&attr, sizeof(attr), 0);
> if (ruleset_fd == \-1) {
> perror("Failed to create a ruleset");
> @@ -429,9 +482,13 @@ if (ruleset_fd == \-1) {
> .EE
> .in
> .PP
> -We can now add a new rule to this ruleset thanks to the returned file
> -descriptor referring to this ruleset.
> -The rule will only allow reading the file hierarchy
> +We can now add a new rule to the ruleset through the ruleset's file descriptor.
> +The requested access rights must be a subset of the access rights
> +which were specified in
> +.I attr.handled_access_fs
> +at ruleset creation time.
> +.PP
> +In this example, the rule will only allow reading the file hierarchy
> .IR /usr .
> Without another rule, write actions would then be denied by the ruleset.
> To add
> --
> 2.39.2
>
next prev parent reply other threads:[~2023-03-24 18:24 UTC|newest]
Thread overview: 15+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-03-24 17:24 [PATCH v5 1/3] landlock.7: Document Landlock ABI v2 (file reparenting; Linux 5.19) Günther Noack
2023-03-24 17:24 ` [PATCH v5 2/3] landlock.7: Document Landlock ABI v3 (file truncation; Linux 6.2) Günther Noack
2023-03-31 22:20 ` Alejandro Colomar
2023-03-24 17:24 ` [PATCH v5 3/3] landlock.7: Explain the best-effort fallback mechanism in the example Günther Noack
2023-03-24 18:24 ` Günther Noack [this message]
2023-03-31 22:29 ` Alejandro Colomar
2023-04-01 17:19 ` Günther Noack
2023-04-01 22:01 ` Alejandro Colomar
2023-04-04 7:33 ` Günther Noack
2023-04-05 2:50 ` Alejandro Colomar
2023-04-17 21:13 ` Mickaël Salaün
2023-04-18 14:47 ` Alejandro Colomar
2023-04-02 1:21 ` Alejandro Colomar
2023-04-04 7:17 ` Günther Noack
2023-03-31 22:17 ` [PATCH v5 1/3] landlock.7: Document Landlock ABI v2 (file reparenting; Linux 5.19) Alejandro Colomar
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=20230324.449c0a64f654@gnoack.org \
--to=gnoack3000@gmail.com \
--cc=alx.manpages@gmail.com \
--cc=linux-man@vger.kernel.org \
--cc=mic@digikod.net \
--cc=mtk.manpages@gmail.com \
/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