All of lore.kernel.org
 help / color / mirror / Atom feed
From: Michal Clapinski <mclapinski@google.com>
To: Jonathan Corbet <corbet@lwn.net>,
	Mike Kravetz <mike.kravetz@oracle.com>,
	Muchun Song <muchun.song@linux.dev>,
	Andrew Morton <akpm@linux-foundation.org>,
	Hugh Dickins <hughd@google.com>, Shuah Khan <shuah@kernel.org>,
	Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
	Arnd Bergmann <arnd@arndb.de>, Yi Liu <yi.l.liu@intel.com>,
	Dominik Brodowski <linux@dominikbrodowski.net>,
	Hans Verkuil <hverkuil-cisco@xs4all.nl>,
	Steve French <stfrench@microsoft.com>,
	Simon Ser <contact@emersion.fr>, Jason Gunthorpe <jgg@ziepe.ca>,
	Marc Dionne <marc.dionne@auristor.com>,
	Jiri Slaby <jirislaby@kernel.org>,
	David Howells <dhowells@redhat.com>,
	Luca Vizzarro <Luca.Vizzarro@arm.com>,
	Jeff Xu <jeffxu@google.com>, Aleksa Sarai <cyphar@cyphar.com>,
	Kees Cook <keescook@chromium.org>,
	Daniel Verkamp <dverkamp@chromium.org>,
	linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org,
	linux-mm@kvack.org, linux-kselftest@vger.kernel.org
Cc: Michal Clapinski <mclapinski@google.com>
Subject: [PATCH v2 0/2] mm/memfd: add ioctl(MEMFD_CHECK_IF_ORIGINAL)
Date: Fri,  8 Sep 2023 19:57:36 +0200	[thread overview]
Message-ID: <20230908175738.41895-1-mclapinski@google.com> (raw)

This change introduces a way to check if an fd points to a memfd's
original open fd (the one created by memfd_create).

We encountered an issue with migrating memfds in CRIU (checkpoint
restore in userspace - it migrates running processes between
machines). Imagine a scenario:
1. Create a memfd. By default it's open with O_RDWR and yet one can
exec() to it (unlike with regular files, where one would get ETXTBSY).
2. Reopen that memfd with O_RDWR via /proc/self/fd/<fd>.

Now those 2 fds are indistinguishable from userspace. You can't exec()
to either of them (since the reopen incremented inode->i_writecount)
and their /proc/self/fdinfo/ are exactly the same. Unfortunately they
are not the same. If you close the second one, the first one becomes
exec()able again. If you close the first one, the other doesn't become
exec()able. Therefore during migration it does matter which is recreated
first and which is reopened but there is no way for CRIU to tell which
was first.

---
Changes since v1 at [1]:
  - Rewrote it from fcntl to ioctl. This was requested by filesystems
    maintainer.

Links:
  [1] https://lore.kernel.org/all/20230831203647.558079-1-mclapinski@google.com/

Michal Clapinski (2):
  mm/memfd: add ioctl(MEMFD_CHECK_IF_ORIGINAL)
  selftests: test ioctl(MEMFD_CHECK_IF_ORIGINAL)

 .../userspace-api/ioctl/ioctl-number.rst      |  1 +
 fs/hugetlbfs/inode.c                          |  9 ++++++
 include/linux/memfd.h                         | 12 +++++++
 mm/memfd.c                                    |  9 ++++++
 mm/shmem.c                                    |  9 ++++++
 tools/testing/selftests/memfd/memfd_test.c    | 32 +++++++++++++++++++
 6 files changed, 72 insertions(+)

-- 
2.42.0.283.g2d96d420d3-goog


             reply	other threads:[~2023-09-08 17:58 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-09-08 17:57 Michal Clapinski [this message]
2023-09-08 17:57 ` [PATCH v2 1/2] mm/memfd: add ioctl(MEMFD_CHECK_IF_ORIGINAL) Michal Clapinski
2023-09-08 20:02   ` kernel test robot
2023-09-08 17:57 ` [PATCH v2 2/2] selftests: test ioctl(MEMFD_CHECK_IF_ORIGINAL) Michal Clapinski
2023-09-08 20:34 ` [PATCH v2 0/2] mm/memfd: add ioctl(MEMFD_CHECK_IF_ORIGINAL) Jonathan Corbet
2023-09-08 21:55   ` Michał Cłapiński
2023-09-08 22:07     ` Jonathan Corbet

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=20230908175738.41895-1-mclapinski@google.com \
    --to=mclapinski@google.com \
    --cc=Luca.Vizzarro@arm.com \
    --cc=akpm@linux-foundation.org \
    --cc=arnd@arndb.de \
    --cc=contact@emersion.fr \
    --cc=corbet@lwn.net \
    --cc=cyphar@cyphar.com \
    --cc=dhowells@redhat.com \
    --cc=dverkamp@chromium.org \
    --cc=gregkh@linuxfoundation.org \
    --cc=hughd@google.com \
    --cc=hverkuil-cisco@xs4all.nl \
    --cc=jeffxu@google.com \
    --cc=jgg@ziepe.ca \
    --cc=jirislaby@kernel.org \
    --cc=keescook@chromium.org \
    --cc=linux-doc@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-kselftest@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=linux@dominikbrodowski.net \
    --cc=marc.dionne@auristor.com \
    --cc=mike.kravetz@oracle.com \
    --cc=muchun.song@linux.dev \
    --cc=shuah@kernel.org \
    --cc=stfrench@microsoft.com \
    --cc=yi.l.liu@intel.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.