public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: "Mickaël Salaün" <mic@digikod.net>
To: "Christian Brauner" <brauner@kernel.org>,
	"Günther Noack" <gnoack@google.com>
Cc: "Mickaël Salaün" <mic@digikod.net>,
	"Charles Zaffery" <czaffery@roblox.com>,
	"Daniel Burgener" <dburgener@linux.microsoft.com>,
	"Francis Laniel" <flaniel@linux.microsoft.com>,
	"James Morris" <jmorris@namei.org>,
	"Jann Horn" <jannh@google.com>, "Jeff Xu" <jeffxu@google.com>,
	"Kees Cook" <kees@kernel.org>,
	"Luca Boccassi" <luca.boccassi@gmail.com>,
	"Mikhail Ivanov" <ivanov.mikhail1@huawei-partners.com>,
	"Praveen K Paladugu" <prapal@linux.microsoft.com>,
	"Robert Salvet" <robert.salvet@roblox.com>,
	"Shervin Oloumi" <enlightened@google.com>,
	"Tyler Hicks" <code@tyhicks.com>,
	linux-kernel@vger.kernel.org,
	linux-security-module@vger.kernel.org
Subject: [RFC PATCH v1 3/3] samples/landlock: Print domain ID
Date: Fri, 31 Jan 2025 17:34:47 +0100	[thread overview]
Message-ID: <20250131163447.1140564-4-mic@digikod.net> (raw)
In-Reply-To: <20250131163447.1140564-1-mic@digikod.net>

If the PIDFD_GET_INFO IOCTL command and the related
PIDFD_INFO_LANDLOCK_FIRST_DOMAIN flag are supported, print the newly
created domain before launching the sandboxed program.

Cc: Günther Noack <gnoack@google.com>
Cc: Praveen K Paladugu <prapal@linux.microsoft.com>
Signed-off-by: Mickaël Salaün <mic@digikod.net>
Link: https://lore.kernel.org/r/20250131163447.1140564-4-mic@digikod.net
---
 samples/landlock/sandboxer.c | 29 +++++++++++++++++++++++++++--
 1 file changed, 27 insertions(+), 2 deletions(-)

diff --git a/samples/landlock/sandboxer.c b/samples/landlock/sandboxer.c
index 68a5f16bacd5..018bafaa470a 100644
--- a/samples/landlock/sandboxer.c
+++ b/samples/landlock/sandboxer.c
@@ -15,15 +15,20 @@
 #include <linux/landlock.h>
 #include <linux/prctl.h>
 #include <linux/socket.h>
+#include <stdbool.h>
 #include <stddef.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
+#include <sys/ioctl.h>
 #include <sys/prctl.h>
 #include <sys/stat.h>
 #include <sys/syscall.h>
 #include <unistd.h>
-#include <stdbool.h>
+
+/* Avoids conflict with fcntl.h */
+#define _LINUX_FCNTL_H
+#include <linux/pidfd.h>
 
 #ifndef landlock_create_ruleset
 static inline int
@@ -53,6 +58,11 @@ static inline int landlock_restrict_self(const int ruleset_fd,
 }
 #endif
 
+static inline int sys_pidfd_open(const pid_t pid, const unsigned int flags)
+{
+	return syscall(__NR_pidfd_open, pid, flags);
+}
+
 #define ENV_FS_RO_NAME "LL_FS_RO"
 #define ENV_FS_RW_NAME "LL_FS_RW"
 #define ENV_TCP_BIND_NAME "LL_TCP_BIND"
@@ -343,7 +353,7 @@ int main(const int argc, char *const argv[], char *const *const envp)
 {
 	const char *cmd_path;
 	char *const *cmd_argv;
-	int ruleset_fd, abi;
+	int ruleset_fd, abi, pidfd;
 	char *env_port_name, *env_force_log;
 	__u64 access_fs_ro = ACCESS_FS_ROUGHLY_READ,
 	      access_fs_rw = ACCESS_FS_ROUGHLY_READ | ACCESS_FS_ROUGHLY_WRITE;
@@ -510,6 +520,9 @@ int main(const int argc, char *const argv[], char *const *const envp)
 		goto err_close_ruleset;
 	}
 
+	/* Opens our own pidfd before sandboxing, if supported. */
+	pidfd = sys_pidfd_open(getpid(), 0);
+
 	if (prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0)) {
 		perror("Failed to restrict privileges");
 		goto err_close_ruleset;
@@ -520,6 +533,18 @@ int main(const int argc, char *const argv[], char *const *const envp)
 	}
 	close(ruleset_fd);
 
+	if (pidfd >= 0) {
+		struct pidfd_info info = {
+			.mask = PIDFD_INFO_LANDLOCK_FIRST_DOMAIN,
+		};
+
+		if (!ioctl(pidfd, PIDFD_GET_INFO, &info) &&
+		    (info.mask & PIDFD_INFO_LANDLOCK_FIRST_DOMAIN)) {
+			fprintf(stderr, "Entered the Landlock domain %llx.\n",
+				info.landlock_first_domain);
+		}
+	}
+
 	cmd_path = argv[1];
 	cmd_argv = argv + 1;
 	fprintf(stderr, "Executing the sandboxed command...\n");
-- 
2.48.1


  parent reply	other threads:[~2025-01-31 16:35 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-01-31 16:34 [RFC PATCH v1 0/3] Expose Landlock domain IDs via pidfd Mickaël Salaün
2025-01-31 16:34 ` [RFC PATCH v1 1/3] landlock: Add landlock_read_domain_id() Mickaël Salaün
2025-01-31 16:34 ` [RFC PATCH v1 2/3] pidfd: Extend PIDFD_GET_INFO with PIDFD_INFO_LANDLOCK_*_DOMAIN Mickaël Salaün
2025-01-31 19:02   ` Paul Moore
2025-02-01 10:28     ` Mickaël Salaün
2025-02-01 11:09       ` Christian Brauner
2025-02-01 23:48       ` Paul Moore
2025-01-31 16:34 ` Mickaël Salaün [this message]
2025-01-31 17:35 ` [RFC PATCH v1 0/3] Expose Landlock domain IDs via pidfd 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=20250131163447.1140564-4-mic@digikod.net \
    --to=mic@digikod.net \
    --cc=brauner@kernel.org \
    --cc=code@tyhicks.com \
    --cc=czaffery@roblox.com \
    --cc=dburgener@linux.microsoft.com \
    --cc=enlightened@google.com \
    --cc=flaniel@linux.microsoft.com \
    --cc=gnoack@google.com \
    --cc=ivanov.mikhail1@huawei-partners.com \
    --cc=jannh@google.com \
    --cc=jeffxu@google.com \
    --cc=jmorris@namei.org \
    --cc=kees@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-security-module@vger.kernel.org \
    --cc=luca.boccassi@gmail.com \
    --cc=prapal@linux.microsoft.com \
    --cc=robert.salvet@roblox.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