mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Michal Clapinski <mclapinski@google.com>
To: Jeff Layton <jlayton@kernel.org>,
	Chuck Lever <chuck.lever@oracle.com>,
	Alexander Viro <viro@zeniv.linux.org.uk>,
	Christian Brauner <brauner@kernel.org>,
	Shuah Khan <shuah@kernel.org>,
	Andrew Morton <akpm@linux-foundation.org>,
	Jeff Xu <jeffxu@google.com>, Aleksa Sarai <cyphar@cyphar.com>,
	Daniel Verkamp <dverkamp@chromium.org>,
	Kees Cook <keescook@chromium.org>,
	Marc Dionne <marc.dionne@auristor.com>,
	linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org,
	linux-kselftest@vger.kernel.org
Cc: Michal Clapinski <mclapinski@google.com>
Subject: [PATCH 1/2] fcntl: add fcntl(F_CHECK_ORIGINAL_MEMFD)
Date: Thu, 31 Aug 2023 22:36:46 +0200	[thread overview]
Message-ID: <20230831203647.558079-2-mclapinski@google.com> (raw)
In-Reply-To: <20230831203647.558079-1-mclapinski@google.com>

Add a way to check if an fd points to the memfd's original open fd
(the one created by memfd_create).
Useful because only the original open fd can be both writable and
executable.

Signed-off-by: Michal Clapinski <mclapinski@google.com>
---
 fs/fcntl.c                 | 3 +++
 include/uapi/linux/fcntl.h | 9 +++++++++
 2 files changed, 12 insertions(+)

diff --git a/fs/fcntl.c b/fs/fcntl.c
index e871009f6c88..301527e07a4d 100644
--- a/fs/fcntl.c
+++ b/fs/fcntl.c
@@ -419,6 +419,9 @@ static long do_fcntl(int fd, unsigned int cmd, unsigned long arg,
 	case F_SET_RW_HINT:
 		err = fcntl_rw_hint(filp, cmd, arg);
 		break;
+	case F_CHECK_ORIGINAL_MEMFD:
+		err = !(filp->f_mode & FMODE_WRITER);
+		break;
 	default:
 		break;
 	}
diff --git a/include/uapi/linux/fcntl.h b/include/uapi/linux/fcntl.h
index 6c80f96049bd..9a10fe3aafa7 100644
--- a/include/uapi/linux/fcntl.h
+++ b/include/uapi/linux/fcntl.h
@@ -56,6 +56,15 @@
 #define F_GET_FILE_RW_HINT	(F_LINUX_SPECIFIC_BASE + 13)
 #define F_SET_FILE_RW_HINT	(F_LINUX_SPECIFIC_BASE + 14)
 
+/*
+ * Check if the fd points to the memfd's original open fd (the one created by
+ * memfd_create). Returns 1 if yes, 0 if no.
+ * If the fd doesn't point to a memfd, the value should not be interpreted.
+ * Useful because only the original open fd can be both writable and
+ * executable.
+ */
+#define F_CHECK_ORIGINAL_MEMFD	(F_LINUX_SPECIFIC_BASE + 15)
+
 /*
  * Valid hint values for F_{GET,SET}_RW_HINT. 0 is "not set", or can be
  * used to clear any hints previously set.
-- 
2.42.0.283.g2d96d420d3-goog


  reply	other threads:[~2023-08-31 20:37 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-08-31 20:36 [PATCH 0/2] " Michal Clapinski
2023-08-31 20:36 ` Michal Clapinski [this message]
2023-09-01 12:56   ` [PATCH 1/2] " Christian Brauner
2023-09-01 14:50     ` Michał Cłapiński
2023-09-01 18:34       ` Kees Cook
2023-09-04  7:29         ` Christian Brauner
2023-09-04 17:57           ` Michał Cłapiński
2023-09-05  8:37             ` Christian Brauner
2023-08-31 20:36 ` [PATCH 2/2] selftests: test fcntl(F_CHECK_ORIGINAL_MEMFD) Michal Clapinski

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=20230831203647.558079-2-mclapinski@google.com \
    --to=mclapinski@google.com \
    --cc=akpm@linux-foundation.org \
    --cc=brauner@kernel.org \
    --cc=chuck.lever@oracle.com \
    --cc=cyphar@cyphar.com \
    --cc=dverkamp@chromium.org \
    --cc=jeffxu@google.com \
    --cc=jlayton@kernel.org \
    --cc=keescook@chromium.org \
    --cc=linux-fsdevel@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-kselftest@vger.kernel.org \
    --cc=marc.dionne@auristor.com \
    --cc=shuah@kernel.org \
    --cc=viro@zeniv.linux.org.uk \
    /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

Powered by JetHome