linux-security-module.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: "Günther Noack" <gnoack3000@gmail.com>
To: Mikhail Ivanov <ivanov.mikhail1@huawei-partners.com>
Cc: mic@digikod.net, willemdebruijn.kernel@gmail.com,
	linux-security-module@vger.kernel.org, netdev@vger.kernel.org,
	netfilter-devel@vger.kernel.org, yusongping@huawei.com,
	artem.kuzin@huawei.com, konstantin.meskhidze@huawei.com
Subject: Re: [RFC PATCH v2 9/9] samples/landlock: Support LANDLOCK_ACCESS_NET_LISTEN
Date: Sat, 5 Oct 2024 18:57:24 +0200	[thread overview]
Message-ID: <20241005.92cff495291f@gnoack.org> (raw)
In-Reply-To: <20240814030151.2380280-10-ivanov.mikhail1@huawei-partners.com>

On Wed, Aug 14, 2024 at 11:01:51AM +0800, Mikhail Ivanov wrote:
> Extend sample with TCP listen control logic.
> 
> Signed-off-by: Mikhail Ivanov <ivanov.mikhail1@huawei-partners.com>
> ---
>  samples/landlock/sandboxer.c | 31 ++++++++++++++++++++++++++-----
>  1 file changed, 26 insertions(+), 5 deletions(-)
> 
> diff --git a/samples/landlock/sandboxer.c b/samples/landlock/sandboxer.c
> index e8223c3e781a..3f50cb3f8039 100644
> --- a/samples/landlock/sandboxer.c
> +++ b/samples/landlock/sandboxer.c
> @@ -55,6 +55,7 @@ static inline int landlock_restrict_self(const int ruleset_fd,
>  #define ENV_FS_RW_NAME "LL_FS_RW"
>  #define ENV_TCP_BIND_NAME "LL_TCP_BIND"
>  #define ENV_TCP_CONNECT_NAME "LL_TCP_CONNECT"
> +#define ENV_TCP_LISTEN_NAME "LL_TCP_LISTEN"
>  #define ENV_DELIMITER ":"
>  
>  static int parse_path(char *env_path, const char ***const path_list)
> @@ -208,7 +209,7 @@ static int populate_ruleset_net(const char *const env_var, const int ruleset_fd,
>  
>  /* clang-format on */
>  
> -#define LANDLOCK_ABI_LAST 5
> +#define LANDLOCK_ABI_LAST 6
>  
>  int main(const int argc, char *const argv[], char *const *const envp)
>  {
> @@ -222,15 +223,16 @@ int main(const int argc, char *const argv[], char *const *const envp)
>  	struct landlock_ruleset_attr ruleset_attr = {
>  		.handled_access_fs = access_fs_rw,
>  		.handled_access_net = LANDLOCK_ACCESS_NET_BIND_TCP |
> -				      LANDLOCK_ACCESS_NET_CONNECT_TCP,
> +				      LANDLOCK_ACCESS_NET_CONNECT_TCP |
> +				      LANDLOCK_ACCESS_NET_LISTEN_TCP,
>  	};
>  
>  	if (argc < 2) {
>  		fprintf(stderr,
> -			"usage: %s=\"...\" %s=\"...\" %s=\"...\" %s=\"...\"%s "
> +			"usage: %s=\"...\" %s=\"...\" %s=\"...\" %s=\"...\" %s=\"...\"%s "
>  			"<cmd> [args]...\n\n",
>  			ENV_FS_RO_NAME, ENV_FS_RW_NAME, ENV_TCP_BIND_NAME,
> -			ENV_TCP_CONNECT_NAME, argv[0]);
> +			ENV_TCP_CONNECT_NAME, ENV_TCP_LISTEN_NAME, argv[0]);
>  		fprintf(stderr,
>  			"Execute a command in a restricted environment.\n\n");
>  		fprintf(stderr,
> @@ -251,15 +253,19 @@ int main(const int argc, char *const argv[], char *const *const envp)
>  		fprintf(stderr,
>  			"* %s: list of ports allowed to connect (client).\n",
>  			ENV_TCP_CONNECT_NAME);
> +		fprintf(stderr,
> +			"* %s: list of ports allowed to listen (server).\n",
> +			ENV_TCP_LISTEN_NAME);
>  		fprintf(stderr,
>  			"\nexample:\n"
>  			"%s=\"${PATH}:/lib:/usr:/proc:/etc:/dev/urandom\" "
>  			"%s=\"/dev/null:/dev/full:/dev/zero:/dev/pts:/tmp\" "
>  			"%s=\"9418\" "
>  			"%s=\"80:443\" "
> +			"%s=\"9418\" "
>  			"%s bash -i\n\n",
>  			ENV_FS_RO_NAME, ENV_FS_RW_NAME, ENV_TCP_BIND_NAME,
> -			ENV_TCP_CONNECT_NAME, argv[0]);
> +			ENV_TCP_CONNECT_NAME, ENV_TCP_LISTEN_NAME, argv[0]);
>  		fprintf(stderr,
>  			"This sandboxer can use Landlock features "
>  			"up to ABI version %d.\n",
> @@ -326,6 +332,11 @@ int main(const int argc, char *const argv[], char *const *const envp)
>  	case 4:
>  		/* Removes LANDLOCK_ACCESS_FS_IOCTL_DEV for ABI < 5 */
>  		ruleset_attr.handled_access_fs &= ~LANDLOCK_ACCESS_FS_IOCTL_DEV;
> +		__attribute__((fallthrough));
> +	case 5:
> +		/* Removes LANDLOCK_ACCESS_NET_LISTEN support for ABI < 6 */
> +		ruleset_attr.handled_access_net &=
> +			~(LANDLOCK_ACCESS_NET_LISTEN_TCP);

(same remark as on other patch set)

ABI version has shifted by one in the meantime.

>  
>  		fprintf(stderr,
>  			"Hint: You should update the running kernel "
> @@ -357,6 +368,12 @@ int main(const int argc, char *const argv[], char *const *const envp)
>  		ruleset_attr.handled_access_net &=
>  			~LANDLOCK_ACCESS_NET_CONNECT_TCP;
>  	}
> +	/* Removes listen access attribute if not supported by a user. */

(also same remark as on other patch set)

Please s/supported/requested/, for consistency.

> +	env_port_name = getenv(ENV_TCP_LISTEN_NAME);
> +	if (!env_port_name) {
> +		ruleset_attr.handled_access_net &=
> +			~LANDLOCK_ACCESS_NET_LISTEN_TCP;
> +	}
>  
>  	ruleset_fd =
>  		landlock_create_ruleset(&ruleset_attr, sizeof(ruleset_attr), 0);
> @@ -380,6 +397,10 @@ int main(const int argc, char *const argv[], char *const *const envp)
>  				 LANDLOCK_ACCESS_NET_CONNECT_TCP)) {
>  		goto err_close_ruleset;
>  	}
> +	if (populate_ruleset_net(ENV_TCP_LISTEN_NAME, ruleset_fd,
> +				 LANDLOCK_ACCESS_NET_LISTEN_TCP)) {
> +		goto err_close_ruleset;
> +	}
>  
>  	if (prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0)) {
>  		perror("Failed to restrict privileges");
> -- 
> 2.34.1
> 

Reviewed-by: Günther Noack <gnoack3000@gmail.com>

  reply	other threads:[~2024-10-05 16:57 UTC|newest]

Thread overview: 40+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-08-14  3:01 [RFC PATCH v2 0/9] Support TCP listen access-control Mikhail Ivanov
2024-08-14  3:01 ` [RFC PATCH v2 1/9] landlock: Refactor current_check_access_socket() access right check Mikhail Ivanov
2024-08-19 21:37   ` Günther Noack
2024-08-20 11:20     ` Mikhail Ivanov
2024-08-14  3:01 ` [RFC PATCH v2 2/9] landlock: Support TCP listen access-control Mikhail Ivanov
2024-10-05 16:56   ` Günther Noack
2024-10-05 17:53     ` Mikhail Ivanov
2024-10-05 18:22       ` Günther Noack
2024-10-05 18:32         ` Mikhail Ivanov
2024-08-14  3:01 ` [RFC PATCH v2 3/9] selftests/landlock: Support LANDLOCK_ACCESS_NET_LISTEN_TCP Mikhail Ivanov
2024-08-19 21:52   ` Günther Noack
2024-08-20 12:32     ` Mikhail Ivanov
2024-08-20 13:14     ` Günther Noack
2024-08-20 18:27       ` Mikhail Ivanov
2024-09-25 18:31         ` Mickaël Salaün
2024-09-26 11:59           ` Mikhail Ivanov
2024-08-19 21:53   ` Günther Noack
2024-08-20 12:35     ` Mikhail Ivanov
2024-08-14  3:01 ` [RFC PATCH v2 4/9] selftests/landlock: Test listening restriction Mikhail Ivanov
2024-08-20 12:31   ` Günther Noack
2024-08-20 18:46     ` Mikhail Ivanov
2024-09-25 18:31       ` Mickaël Salaün
2024-09-26 13:51         ` Mikhail Ivanov
2024-08-14  3:01 ` [RFC PATCH v2 5/9] selftests/landlock: Test listen on connected socket Mikhail Ivanov
2024-08-20 13:01   ` Günther Noack
2024-08-20 13:42     ` Mikhail Ivanov
2024-08-14  3:01 ` [RFC PATCH v2 6/9] selftests/landlock: Test listening without explicit bind restriction Mikhail Ivanov
2024-08-20 13:02   ` Günther Noack
2024-08-20 13:46     ` Mikhail Ivanov
2024-08-21 11:52       ` Mikhail Ivanov
2024-08-14  3:01 ` [RFC PATCH v2 7/9] selftests/landlock: Test listen on ULP socket without clone method Mikhail Ivanov
2024-08-14  3:01 ` [RFC PATCH v2 8/9] selftests/landlock: Test changing socket backlog with listen(2) Mikhail Ivanov
2024-10-05 16:57   ` Günther Noack
2024-10-05 17:29     ` Mikhail Ivanov
2024-08-14  3:01 ` [RFC PATCH v2 9/9] samples/landlock: Support LANDLOCK_ACCESS_NET_LISTEN Mikhail Ivanov
2024-10-05 16:57   ` Günther Noack [this message]
2024-10-05 17:30     ` Mikhail Ivanov
2024-08-20 13:11 ` [RFC PATCH v2 0/9] Support TCP listen access-control Günther Noack
2024-08-20 13:23   ` Günther Noack
2024-08-20 13:53     ` Mikhail Ivanov

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=20241005.92cff495291f@gnoack.org \
    --to=gnoack3000@gmail.com \
    --cc=artem.kuzin@huawei.com \
    --cc=ivanov.mikhail1@huawei-partners.com \
    --cc=konstantin.meskhidze@huawei.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).