From: "Konstantin Meskhidze (A)" <konstantin.meskhidze@huawei.com>
To: "Mickaël Salaün" <mic@digikod.net>, "Günther Noack" <gnoack@google.com>
Cc: <willemdebruijn.kernel@gmail.com>, <gnoack3000@gmail.com>,
<linux-security-module@vger.kernel.org>, <netdev@vger.kernel.org>,
<netfilter-devel@vger.kernel.org>, <yusongping@huawei.com>,
<artem.kuzin@huawei.com>
Subject: Re: [PATCH v11 11/12] samples/landlock: Add network demo
Date: Mon, 19 Jun 2023 17:24:17 +0300 [thread overview]
Message-ID: <c0713bf1-a65e-c4cd-08b9-c60bd79fc86f@huawei.com> (raw)
In-Reply-To: <8c09fc5a-e3a5-4792-65a8-b84c6044128a@digikod.net>
6/13/2023 11:38 PM, Mickaël Salaün пишет:
>
> On 13/06/2023 12:54, Konstantin Meskhidze (A) wrote:
>>
>>
>> 6/6/2023 6:17 PM, Günther Noack пишет:
>>> Hi Konstantin!
>>>
>>> Apologies if some of this was discussed before, in this case,
>>> Mickaël's review overrules my opinions from the sidelines ;)
>>>
>>> On Tue, May 16, 2023 at 12:13:38AM +0800, Konstantin Meskhidze wrote:
>>>> This commit adds network demo. It's possible to allow a sandboxer to
>>>> bind/connect to a list of particular ports restricting network
>>>> actions to the rest of ports.
>>>>
>>>> Signed-off-by: Konstantin Meskhidze <konstantin.meskhidze@huawei.com>
>>>
>>>
>>>> diff --git a/samples/landlock/sandboxer.c b/samples/landlock/sandboxer.c
>>>> index e2056c8b902c..b0250edb6ccb 100644
>>>> --- a/samples/landlock/sandboxer.c
>>>> +++ b/samples/landlock/sandboxer.c
>>>
>>> ...
>>>
>>>> +static int populate_ruleset_net(const char *const env_var, const int ruleset_fd,
>>>> + const __u64 allowed_access)
>>>> +{
>>>> + int num_ports, i, ret = 1;
>>>
>>> I thought the convention was normally to set ret = 0 initially and to
>>> override it in case of error, rather than the other way around?
>
> Which convention? In this case, by default the return code is an error.
>
>
>>>
>> Well, I just followed Mickaёl's way of logic here. >
>>
>>>> + char *env_port_name;
>>>> + struct landlock_net_service_attr net_service = {
>>>> + .allowed_access = allowed_access,
>>>> + .port = 0,
>>>> + };
>>>> +
>>>> + env_port_name = getenv(env_var);
>>>> + if (!env_port_name)
>>>> + return 0;
>>>> + env_port_name = strdup(env_port_name);
>>>> + unsetenv(env_var);
>>>> + num_ports = parse_port_num(env_port_name);
>>>> +
>>>> + if (num_ports == 1 && (strtok(env_port_name, ENV_PATH_TOKEN) == NULL)) {
>>>> + ret = 0;
>>>> + goto out_free_name;
>>>> + }
>>>
>>> I don't understand why parse_port_num and strtok are necessary in this
>>> program. The man-page for strsep(3) describes it as a replacement to
>>> strtok(3) (in the HISTORY section), and it has a very short example
>>> for how it is used.
>>>
>>> Wouldn't it work like this as well?
>>>
>>> while ((strport = strsep(&env_port_name, ":"))) {
>>> net_service.port = atoi(strport);
>>> /* etc */
>>> }
>>
>> Thanks for a tip. I think it's a better solution here. Now this
>> commit is in Mickaёl's -next branch. I could send a one-commit patch later.
>> Mickaёl, what do you think?
>
> I removed this series from -next because there is some issues (see the
> bot's emails), but anyway, this doesn't mean these patches don't need to
> be changed, they do. The goal of -next is to test more widely a patch
> series and get more feedbacks, especially from bots. When this series
> will be fully ready (and fuzzed with syzkaller), I'll push it to Linus
> Torvalds.
>
> I'll review the remaining tests and sample code this week, but you can
> still take into account the documentation review.
Hi, Mickaёl.
I have a few quetions?
- Are you going to fix warnings for bots, meanwhile I run syzcaller?
- I will fix documentation and sandbox demo and sent patch v12?
>
>
>>
>>>
>>>> +
>>>> + for (i = 0; i < num_ports; i++) {
>>>> + net_service.port = atoi(strsep(&env_port_name, ENV_PATH_TOKEN));
>>>
>>> Naming of ENV_PATH_TOKEN:
>>> This usage is not related to paths, maybe rename the variable?
>>> It's also technically not the token, but the delimiter.
>>>
>> What do you think of ENV_PORT_TOKEN or ENV_PORT_DELIMITER???
>
> You can rename ENV_PATH_TOKEN to ENV_DELIMITER for the FS and network parts.
>
Ok. Got it.
>
>>
>>>> + if (landlock_add_rule(ruleset_fd, LANDLOCK_RULE_NET_SERVICE,
>>>> + &net_service, 0)) {
>>>> + fprintf(stderr,
>>>> + "Failed to update the ruleset with port \"%lld\": %s\n",
>>>> + net_service.port, strerror(errno));
>>>> + goto out_free_name;
>>>> + }
>>>> + }
>>>> + ret = 0;
>>>> +
>>>> +out_free_name:
>>>> + free(env_port_name);
>>>> + return ret;
>>>> +}
>>>
>>>
>>>> fprintf(stderr,
>>>> "Launch a command in a restricted environment.\n\n");
>>>> - fprintf(stderr, "Environment variables containing paths, "
>>>> - "each separated by a colon:\n");
>>>> + fprintf(stderr,
>>>> + "Environment variables containing paths and ports "
>>>> + "each separated by a colon:\n");
>>>> fprintf(stderr,
>>>> "* %s: list of paths allowed to be used in a read-only way.\n",
>>>> ENV_FS_RO_NAME);
>>>> fprintf(stderr,
>>>> - "* %s: list of paths allowed to be used in a read-write way.\n",
>>>> + "* %s: list of paths allowed to be used in a read-write way.\n\n",
>>>> ENV_FS_RW_NAME);
>>>> + fprintf(stderr,
>>>> + "Environment variables containing ports are optional "
>>>> + "and could be skipped.\n");
>>>
>>> As it is, I believe the program does something different when I'm
>>> setting these to the empty string (ENV_TCP_BIND_NAME=""), compared to
>>> when I'm unsetting them?
>>>
>>> I think the case where we want to forbid all handle-able networking is
>>> a legit and very common use case - it could be clearer in the
>>> documentation how this is done with the tool. (And maybe the interface
>>> could be something more explicit than setting the environment variable
>>> to empty?)
>
> I'd like to keep it simple, and it should be seen as an example code,
> not a full-feature sandboxer, but still a consistent and useful one.
> What would you suggest?
>
> This sandboxer tool relies on environment variables for its
> configuration. This is definitely not a good fit for all use cases, but
> I think it is simple and flexible enough. One use case might be to
> export a set of environment variables and simply call this tool. I'd
> prefer to not deal with argument parsing, but maybe that was too
> simplistic? We might want to revisit this approach but probably not with
> this series.
>
>
>>>
>>>
>>>> + /* Removes bind access attribute if not supported by a user. */
>>>> + env_port_name = getenv(ENV_TCP_BIND_NAME);
>>>> + if (!env_port_name) {
>>>> + ruleset_attr.handled_access_net &=
>>>> + ~LANDLOCK_ACCESS_NET_BIND_TCP;
>>>> + }
>>>> + /* Removes connect access attribute if not supported by a user. */
>>>> + env_port_name = getenv(ENV_TCP_CONNECT_NAME);
>>>> + if (!env_port_name) {
>>>> + ruleset_attr.handled_access_net &=
>>>> + ~LANDLOCK_ACCESS_NET_CONNECT_TCP;
>>>> + }
>>>
>>> This is the code where the program does not restrict network usage,
>>> if the corresponding environment variable is not set.
>>
>> Yep. Right.
>>>
>>> It's slightly inconsistent with what this tool does for filesystem
>>> paths. - If you don't specify any file paths, it will still restrict
>>> file operations there, independent of whether that env variable was
>>> set or not. (Apologies if it was discussed before.)
>>
>> Mickaёl wanted to make network ports optional here.
>> Please check:
>>
>> https://lore.kernel.org/linux-security-module/179ac2ee-37ff-92da-c381-c2c716725045@digikod.net/
>
> Right, the rationale is for compatibility with the previous version of
> this tool. We should not break compatibility when possible. A comment
> should explain the rationale though.
>
>>
>> https://lore.kernel.org/linux-security-module/fe3bc928-14f8-5e2b-359e-9a87d6cf5b01@digikod.net/
>>>
>>> —Günther
>>>
> .
next prev parent reply other threads:[~2023-06-19 14:24 UTC|newest]
Thread overview: 95+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-05-15 16:13 [PATCH v11 00/12] Network support for Landlock Konstantin Meskhidze
2023-05-15 16:13 ` [PATCH v11 01/12] landlock: Make ruleset's access masks more generic Konstantin Meskhidze
2023-05-15 16:13 ` [PATCH v11 02/12] landlock: Allow filesystem layout changes for domains without such rule type Konstantin Meskhidze
2023-05-15 16:13 ` [PATCH v11 03/12] landlock: Refactor landlock_find_rule/insert_rule Konstantin Meskhidze
2023-06-26 18:40 ` Mickaël Salaün
2023-07-01 14:37 ` Konstantin Meskhidze (A)
2023-06-26 18:58 ` Mickaël Salaün
2023-07-01 14:38 ` Konstantin Meskhidze (A)
2023-07-06 14:34 ` Mickaël Salaün
2023-07-10 12:30 ` Konstantin Meskhidze (A)
2023-05-15 16:13 ` [PATCH v11 04/12] landlock: Refactor merge/inherit_ruleset functions Konstantin Meskhidze
2023-06-26 18:40 ` Mickaël Salaün
2023-07-01 14:52 ` Konstantin Meskhidze (A)
2023-07-05 10:16 ` Mickaël Salaün
2023-07-05 10:36 ` Konstantin Meskhidze (A)
2023-05-15 16:13 ` [PATCH v11 05/12] landlock: Move and rename layer helpers Konstantin Meskhidze
2023-05-15 16:13 ` [PATCH v11 06/12] landlock: Refactor " Konstantin Meskhidze
2023-05-15 16:13 ` [PATCH v11 07/12] landlock: Refactor landlock_add_rule() syscall Konstantin Meskhidze
2023-05-15 16:13 ` [PATCH v11 08/12] landlock: Add network rules and TCP hooks support Konstantin Meskhidze
2023-06-26 18:41 ` Mickaël Salaün
2023-07-01 14:54 ` Konstantin Meskhidze (A)
2023-06-26 18:57 ` Mickaël Salaün
2023-07-03 10:36 ` Konstantin Meskhidze (A)
2023-07-03 17:06 ` Mickaël Salaün
2023-07-04 12:37 ` Konstantin Meskhidze (A)
2023-06-27 16:14 ` Mickaël Salaün
2023-06-29 14:04 ` Mickaël Salaün
2023-07-03 10:44 ` Konstantin Meskhidze (A)
2023-07-03 10:43 ` Konstantin Meskhidze (A)
2023-06-27 19:48 ` Günther Noack
2023-07-03 12:39 ` Konstantin Meskhidze (A)
2023-08-03 14:12 ` Mickaël Salaün
2023-08-03 14:13 ` Konstantin Meskhidze (A)
2023-05-15 16:13 ` [PATCH v11 09/12] selftests/landlock: Share enforce_ruleset() Konstantin Meskhidze
2023-05-15 16:13 ` [PATCH v11 10/12] selftests/landlock: Add 11 new test suites dedicated to network Konstantin Meskhidze
2023-07-01 19:07 ` Günther Noack
2023-07-02 8:45 ` Mickaël Salaün
2023-07-03 8:37 ` Konstantin Meskhidze (A)
2023-07-03 9:36 ` Günther Noack
2023-07-06 14:55 ` [PATCH v11.1] " Mickaël Salaün
2023-07-06 16:09 ` Mickaël Salaün
2023-07-10 12:24 ` Konstantin Meskhidze (A)
2023-07-10 16:06 ` Mickaël Salaün
2023-07-12 8:42 ` Konstantin Meskhidze (A)
2023-07-12 7:02 ` Mickaël Salaün
2023-07-12 9:57 ` Konstantin Meskhidze (A)
2023-08-12 14:37 ` Konstantin Meskhidze (A)
2023-08-17 15:08 ` Mickaël Salaün
2023-09-11 10:13 ` Konstantin Meskhidze (A)
2023-09-14 8:08 ` Mickaël Salaün
2023-09-15 8:54 ` Konstantin Meskhidze (A)
2023-09-18 6:56 ` Mickaël Salaün
2023-09-20 10:00 ` Konstantin Meskhidze (A)
2023-08-13 20:09 ` Konstantin Meskhidze (A)
2023-08-17 13:19 ` Mickaël Salaün
2023-08-17 14:04 ` Konstantin Meskhidze (A)
2023-08-17 15:34 ` Mickaël Salaün
2023-08-18 14:05 ` Konstantin Meskhidze (A)
2023-08-11 21:03 ` Konstantin Meskhidze (A)
2023-08-17 12:54 ` Mickaël Salaün
2023-08-17 13:00 ` [PATCH] landlock: Fix and test network AF inconsistencies Mickaël Salaün
2023-08-17 14:13 ` Konstantin Meskhidze (A)
2023-08-17 15:36 ` Mickaël Salaün
2023-08-18 14:05 ` Konstantin Meskhidze (A)
2023-05-15 16:13 ` [PATCH v11 11/12] samples/landlock: Add network demo Konstantin Meskhidze
2023-06-06 15:17 ` Günther Noack
2023-06-13 10:54 ` Konstantin Meskhidze (A)
2023-06-13 20:38 ` Mickaël Salaün
2023-06-19 14:24 ` Konstantin Meskhidze (A) [this message]
2023-06-19 18:19 ` Mickaël Salaün
2023-06-22 8:00 ` Konstantin Meskhidze (A)
2023-06-22 10:18 ` Mickaël Salaün
2023-07-03 12:50 ` Konstantin Meskhidze (A)
2023-07-03 17:09 ` Mickaël Salaün
2023-07-04 12:33 ` Konstantin Meskhidze (A)
2023-07-06 14:35 ` Mickaël Salaün
2023-07-10 12:26 ` Konstantin Meskhidze (A)
2023-05-15 16:13 ` [PATCH v11 12/12] landlock: Document Landlock's network support Konstantin Meskhidze
2023-06-06 14:08 ` Günther Noack
2023-06-07 5:46 ` Jeff Xu
2023-06-13 10:13 ` Konstantin Meskhidze (A)
2023-06-13 20:12 ` Mickaël Salaün
2023-06-22 16:50 ` Mickaël Salaün
2023-06-23 14:35 ` Jeff Xu
2023-07-03 9:04 ` Konstantin Meskhidze (A)
2023-07-03 17:04 ` Mickaël Salaün
2023-06-13 19:56 ` Mickaël Salaün
2023-06-19 14:25 ` Konstantin Meskhidze (A)
2023-06-26 18:59 ` Mickaël Salaün
2023-07-03 10:42 ` Konstantin Meskhidze (A)
2023-06-05 15:02 ` [PATCH v11 00/12] Network support for Landlock Mickaël Salaün
2023-06-06 9:10 ` Konstantin Meskhidze (A)
2023-06-06 9:40 ` Mickaël Salaün
2023-06-19 14:28 ` Konstantin Meskhidze (A)
2023-06-19 18:23 ` Mickaël Salaün
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=c0713bf1-a65e-c4cd-08b9-c60bd79fc86f@huawei.com \
--to=konstantin.meskhidze@huawei.com \
--cc=artem.kuzin@huawei.com \
--cc=gnoack3000@gmail.com \
--cc=gnoack@google.com \
--cc=linux-security-module@vger.kernel.org \
--cc=mic@digikod.net \
--cc=netdev@vger.kernel.org \
--cc=netfilter-devel@vger.kernel.org \
--cc=willemdebruijn.kernel@gmail.com \
--cc=yusongping@huawei.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;
as well as URLs for NNTP newsgroup(s).