linux-mm.kvack.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v2 0/2] mm/memfd: add ioctl(MEMFD_CHECK_IF_ORIGINAL)
@ 2023-09-08 17:57 Michal Clapinski
  2023-09-08 17:57 ` [PATCH v2 1/2] " Michal Clapinski
                   ` (2 more replies)
  0 siblings, 3 replies; 7+ messages in thread
From: Michal Clapinski @ 2023-09-08 17:57 UTC (permalink / raw)
  To: Jonathan Corbet, Mike Kravetz, Muchun Song, Andrew Morton,
	Hugh Dickins, Shuah Khan, Greg Kroah-Hartman, Arnd Bergmann,
	Yi Liu, Dominik Brodowski, Hans Verkuil, Steve French, Simon Ser,
	Jason Gunthorpe, Marc Dionne, Jiri Slaby, David Howells,
	Luca Vizzarro, Jeff Xu, Aleksa Sarai, Kees Cook, Daniel Verkamp,
	linux-doc, linux-kernel, linux-mm, linux-kselftest
  Cc: Michal Clapinski

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



^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2023-09-08 22:07 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-09-08 17:57 [PATCH v2 0/2] mm/memfd: add ioctl(MEMFD_CHECK_IF_ORIGINAL) Michal Clapinski
2023-09-08 17:57 ` [PATCH v2 1/2] " 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

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox