All of lore.kernel.org
 help / color / mirror / Atom feed
From: "Konstantin Meskhidze (A)" <konstantin.meskhidze@huawei.com>
To: "Mickaël Salaün" <mic@digikod.net>, "Paul Moore" <paul@paul-moore.com>
Cc: <artem.kuzin@huawei.com>, <gnoack3000@gmail.com>,
	<willemdebruijn.kernel@gmail.com>, <yusongping@huawei.com>,
	<linux-security-module@vger.kernel.org>, <netdev@vger.kernel.org>,
	<netfilter-devel@vger.kernel.org>
Subject: Re: [PATCH v11.1] selftests/landlock: Add 11 new test suites dedicated to network
Date: Fri, 15 Sep 2023 11:54:46 +0300	[thread overview]
Message-ID: <076bfaa6-1e0b-c95b-5727-00001c79f2c0@huawei.com> (raw)
In-Reply-To: <20230914.ASu9sho1Aef0@digikod.net>



9/14/2023 11:08 AM, Mickaël Salaün пишет:
> On Mon, Sep 11, 2023 at 01:13:24PM +0300, Konstantin Meskhidze (A) wrote:
>> 
>> 
>> 8/17/2023 6:08 PM, Mickaël Salaün пишет:
>> > On Sat, Aug 12, 2023 at 05:37:00PM +0300, Konstantin Meskhidze (A) wrote:
>> > > 
>> > > 
>> > > 7/12/2023 10:02 AM, Mickaël Salaün пишет:
>> > > > > On 06/07/2023 16:55, Mickaël Salaün wrote:
>> > > > > From: Konstantin Meskhidze <konstantin.meskhidze@huawei.com>
>> > > > > > > This patch is a revamp of the v11 tests [1] with new tests
>> > > (see the
>> > > > > "Changes since v11" description).  I (Mickaël) only added the following
>> > > > > todo list and the "Changes since v11" sections in this commit message.
>> > > > > I think this patch is good but it would appreciate reviews.
>> > > > > You can find the diff of my changes here but it is not really readable:
>> > > > > https://git.kernel.org/mic/c/78edf722fba5 (landlock-net-v11 branch)
>> > > > > [1] https://lore.kernel.org/all/20230515161339.631577-11-konstantin.meskhidze@huawei.com/
>> > > > > TODO:
>> > > > > - Rename all "net_service" to "net_port".
>> > > > > - Fix the two kernel bugs found with the new tests.
>> > > > > - Update this commit message with a small description of all tests.
>> > > > > [...]
>> > 
>> > > > We should also add a test to make sure errno is the same with and
>> > > > without sandboxing when using port 0 for connect and consistent with
>> > > > bind (using an available port). The test fixture and variants should be
>> > > > quite similar to the "ipv4" ones, but we can also add AF_INET6 variants,
>> > > > which will result in 8 "ip" variants:
>> > > > > TEST_F(ip, port_zero)
>> > > > {
>> > > > 	if (variant->sandbox == TCP_SANDBOX) {
>> > > > 		/* Denies any connect and bind. */
>> > > > 	}
>> > > > 	/* Checks errno for port 0. */
>> > > > }
>> > > As I understand the would be the next test cases:
>> > > 
>> > > 	1. ip4, sandboxed, bind port 0 -> should return EACCES (denied by
>> > > landlock).
>> > 
>> > Without any allowed port, yes. This test case is useful.
>> > 
>> > By tuning /proc/sys/net/ipv4/ip_local_port_range (see
>> > inet_csk_find_open_port call) we should be able to pick a specific
>> > allowed port and test it.  We can also test for the EADDRINUSE error to
>> > make sure error ordering is correct (compared with -EACCES).
>>   Sorry, did not get this case. Could please explain it with more details?
> 
> According to bind(2), if no port are available, the syscall should
> return EADDRINUSE. And this returned value should be the same whatever
> the process is sandbox or not (and never EACCES). But as I explained
> just below, we cannot know this random port from the LSM hook, so no
> need to tweak /proc/sys/net/ipv4/ip_local_port_range, and your this is
> correct:
> 
> 1. ip4, sandboxed, bind port 0 -> should return EACCES (denied by
> landlock).

   yep, adding rule with port 0 (for bind) returns EINVAL then
   calling bind port 0 returns EACCES cause there is no rule with port 0.
> 
>> > 
>> > However, I think the current LSM API don't enable to infer this random
>> > port because the LSM hook is called before a port is picked.  If this is
>> > correct, the best way to control port binding would be to always deny
>> > binding on port zero/random (when restricting port binding, whatever
>> > exception rules are in place). This explanation should be part of a
>> > comment for this specific exception.
>> 
>>   Yep, if some LSM rule (for bind) has been applied a with specific port,
>> other attemps to bind with zero/random ports would be refused by LSM
>> security checks.
> 
> To say it another way, we should not allow to add a rule with port 0 for
> LANDLOCK_ACCESS_NET_BIND_TCP, but return -EINVAL in this case. This
> limitation should be explained, documented and tested.
> 
> With (only) LANDLOCK_ACCESS_NET_CONNECT_TCP it should be allowed though
> (except if there is also LANDLOCK_ACCESS_NET_BIND_TCP) of course.
> Another test should cover the case with a new rule with these two access
> rights and port 0.

  I think it's possible to have LANDLOCK_ACCESS_NET_CONNECT_TCP with 
port 0 with LANDLOCK_ACCESS_NET_BIND_TCP at the same time, cause 
LANDLOCK_ACCESS_NET_BIND_TCP rule is allowed (by Landlock) with any 
other port but 0.

> 
>> > 
>> > Cc Paul
>> > 
>> > > 	2. ip4, non-sandboxed, bind port 0 -> should return 0 (should be bounded to
>> > > random port).
>> > 
>> > I think so but we need to make sure the random port cannot be < 1024, I
>> > guess with /proc/sys/net/ipv4/ip_local_port_range but I don't know for
>> > IPv6.
>> 
>>   For ipv4 when connecting to a server a client binds to a random port
>> within /proc/sys/net/ipv4/ip_local_port_range, by default one my machine
>> this range is: cat /proc/sys/net/ipv4/ip_local_port_range
>> 32768   60999.
>> But for ipv6 there is no such tuning range.
> 
> Ok, let's just assume that the test system doesn't have
> ip_local_port_range < 1024, put this assumption in a comment, and don't
> touch ip_local_port_range at all.
> 
>> 
>> > 
>> > > 	3. ip6, sandboxed, bind port 0 -> should return EACCES (denied by
>> > > landlock).
>> > > 	4. ip6, non-sandboxed, bind port 0 -> should return 0 (should be bounded to
>> > > random port).
>> > > 	5. ip4, sandboxed, bind some available port, connect port 0 -> should
>> > > return -EACCES (denied by landlock).
> 
> If a rule allows connecting to port 0, then it should be ECONNREFUSED,
> otherwise EACCES indeed. Both cases should be tested.
> 
>> > 
>> > Yes, but don't need to bind to anything (same for the next ones).
>> > 
>> > > 	6. ip4, non-sandboxed, bind some available port, connect port 0 -> should
>> > > return ECONNREFUSED.
>> > 
>> > Yes, but without any binding.
>> > 
>> > > 	7. ip6, sandboxed, bind some available port, connect port 0 -> should
>> > > return -EACCES (denied by landlock)
>> > > 	8. ip6, non-sandboxed, some bind available port, connect port 0 -> should
>> > > return ECONNREFUSED.
>> > > 
>> > > Correct?
>> > 
>> > Thinking more about this case, being able to add a rule with port zero
>> > *for a connect action* looks legitimate.  A rule with both connect and
>> > bind actions on port zero should then be denied.  We should fix
>> > add_rule_net_service() and test that (with a first layer allowing port
>> > zero, and a second without rule, for connect).
>> 
>>  So with first rule allowing port 0 connect action, the second rule with
>> some another port and connect action,
> 
> Yes, but the first rule being part of a first layer/restriction, and the
> second rule part of a second layer.
> 
>> as a result test should allow that.
>> Correct?
> 
> The first layer should return ECONNREFUSED when connecting on port 0
> (allowed but nothing listening), and once the second layer is enforced,
> EACCES should be returned on port 0.
> 
>> > 
>> > 
>> > > 
>> > > > > [...]
>> > > > > > +FIXTURE(inet)
>> > > > > +{
>> > > > > +	struct service_fixture srv0, srv1;
>> > > > > +};
>> > > > > The "inet" variants are useless and should be removed. The
>> > > "inet"
>> > > > fixture can then be renamed to "ipv4_tcp".
>> > > >   So inet should be changed to ipv4_tcp and ipv6_tcp with next
>> > > variants:
>> > > 
>> > >   - ipv4_tcp.no_sandbox_with_ipv4.port_endianness
>> > >   - ipv4_tcp.sandbox_with_ipv4.port_endianness
>> > >   - ipv6_tcp.no_sandbox_with_ipv6.port_endianness
>> > >   - ipv6_tcp.sandbox_with_ipv6.port_endianness
>> > > ????
>> > > 
>> > >    in this case we need double copy of TEST_F(inet, port_endianness) :
>> > > 	TEST_F(ipv4_tcp, port_endianness)
>> > > 	TEST_F(ipv6_tcp, port_endianness)
>> > 
>> > There is no need for any variant for the port_endianness test. You can
>> > rename "inet" to "ipv4_tcp" (and not "inet_tcp" like I said before).
>> > .
> .

  reply	other threads:[~2023-09-15  8:57 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) [this message]
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)
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=076bfaa6-1e0b-c95b-5727-00001c79f2c0@huawei.com \
    --to=konstantin.meskhidze@huawei.com \
    --cc=artem.kuzin@huawei.com \
    --cc=gnoack3000@gmail.com \
    --cc=linux-security-module@vger.kernel.org \
    --cc=mic@digikod.net \
    --cc=netdev@vger.kernel.org \
    --cc=netfilter-devel@vger.kernel.org \
    --cc=paul@paul-moore.com \
    --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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.