mm: memfd_luo: preserve file seals
Pratyush Yadav
maintainerv2 cover mm: memfd_luo: preserve file seals
From: "Pratyush Yadav (Google)" <[email protected]>
Hi,
This series adds support for preserving file seals when preserving a memfd using LUO. Patch 1 exports some memfd seal manipulation functions and patch 2 adds support for preserving them. Since it makes changes to the serialized data structure for memfd, it also bumps the version number.
Changes in v2:
- Define the set of seals supported by this version. Support for any other seal would need a version bump. Make sure the memfd only has this set of seals. Reject any that don't.
- Make seals a u32 since uABI defined it as an int. Change the __reserved into flags.
Pratyush Yadav (Google) (2):
memfd: export memfd_{add,get}_seals()
mm: memfd_luo: preserve file seals
include/linux/kho/abi/memfd.h | 18 +++++++++++++++++- include/linux/memfd.h | 12 ++++++++++++ mm/memfd.c | 4 ++-- mm/memfd_luo.c | 35 +++++++++++++++++++++++++++++++++-- 4 files changed, 64 insertions(+), 5 deletions(-)
Signature · 1 line
2.53.0.335.g19a08e0c02-goog
Pratyush Yadav
maintainerPATCH v2 1/2 memfd: export memfd_{add,get}_seals()
From: "Pratyush Yadav (Google)" <[email protected]>
Support for preserving file seals will be added to memfd preservation using the Live Update Orchestrator (LUO). Export memfd_{add,get}_seals)() so memfd_luo can use them to manipulate the seals.
--- include/linux/memfd.h | 12 ++++++++++++ mm/memfd.c | 4 ++-- 2 files changed, 14 insertions(+), 2 deletions(-)
include/linux/memfd.h +12 -0
@@ -18,6 +18,8 @@ struct folio *memfd_alloc_folio(struct file *memfd, pgoff_t idx);*/ int memfd_check_seals_mmap (struct file *file,vm_flags_t *vm_flags_ptr);struct file *memfd_alloc_file (const char *name,unsigned int flags); +int memfd_get_seals (struct file *file); +int memfd_add_seals (struct file *file,unsigned int seals);#else static inline long memfd_fcntl (struct file *f,unsigned int c,unsigned int a) {
@@ -37,6 +39,16 @@ static inline struct file *memfd_alloc_file(const char *name, unsigned int flags {return ERR_PTR (-EINVAL ); } + +static inline int memfd_get_seals (struct file *file) +{ +return -EINVAL ; +} + +static inline int memfd_add_seals (struct file *file,unsigned int seals) +{ +return -EINVAL ; +}#endif #endif /* __LINUX_MEMFD_H */
mm/memfd.c +2 -2
@@ -228,7 +228,7 @@ static unsigned int *memfd_file_seals_ptr(struct file *file) F_SEAL_WRITE | \ F_SEAL_FUTURE_WRITE) -static int memfd_add_seals (struct file *file,unsigned int seals) +int memfd_add_seals (struct file *file,unsigned int seals) {struct inode *inode =file_inode (file);unsigned int *file_seals;
@@ -310,7 +310,7 @@ static int memfd_add_seals(struct file *file, unsigned int seals)return error; } -static int memfd_get_seals (struct file *file) +int memfd_get_seals (struct file *file) {unsigned int *seals =memfd_file_seals_ptr (file);
Signature · 1 line
2.53.0.335.g19a08e0c02-goog
Pratyush Yadav
maintainerPATCH v2 2/2 mm: memfd_luo: preserve file seals
In reply to Pratyush YadavFrom: "Pratyush Yadav (Google)" <[email protected]>
File seals are used on memfd for making shared memory communication with untrusted peers safer and simpler. Seals provide a guarantee that certain operations won't be allowed on the file such as writes or truncations. Maintaining these guarantees across a live update will help keeping such use cases secure.
These guarantees will also be needed for IOMMUFD preservation with LUO. Normally when IOMMUFD maps a memfd, it pins all its pages to make sure any truncation operations on the memfd don't lead to IOMMUFD using freed memory. This doesn't work with LUO since the preserved memfd might have completely different pages after a live update, and mapping them back to the IOMMUFD will cause all sorts of problems. Using and preserving the seals allows IOMMUFD preservation logic to trust the memfd.
Since the uABI defines seals as an int, preserve them by introducing a new u32 field. There are currently only 6 possible seals, so the extra bits are unused and provide room for future expansion. Since the seals are uABI, it is safe to use them directly in the ABI. While at it, also add a u32 flags field. It makes sure the struct is nicely aligned, and can be used later to support things like MFD_CLOEXEC.
Since the serialization structure is changed, bump the version number to "memfd-v2".
It is important to note that the memfd-v2 version only supports seals that existed when this version was defined. This set is defined by MEMFD_LUO_ALL_SEALS. Any new seal might bring a completely different semantic with it and the parser for memfd-v2 cannot be expected to deal with that. If there are any future seals added, they will need another version bump.
--- include/linux/kho/abi/memfd.h | 18 +++++++++++++++++- mm/memfd_luo.c | 35 +++++++++++++++++++++++++++++++++-- 2 files changed, 50 insertions(+), 3 deletions(-)
include/linux/kho/abi/memfd.h +17 -1
@@ -56,10 +56,24 @@ struct memfd_luo_folio_ser {u64 index; } __packed; +/* +* The set of seals this version supports preserving. If support for any new +* seals is needed, add it here and bump version. +*/ +#define MEMFD_LUO_ALL_SEALS (F_SEAL_SEAL | \ + F_SEAL_SHRINK | \ + F_SEAL_GROW | \ + F_SEAL_WRITE | \ + F_SEAL_FUTURE_WRITE | \ + F_SEAL_EXEC) +/** * struct memfd_luo_ser - Main serialization structure for a memfd. * @pos: The file's current position (f_pos). * @size: The total size of the file in bytes (i_size). +* @seals: The seals present on the memfd. The seals are uABI so it is safe +* to directly use them in the ABI. +* @flags: Flags for the file. Unused flag bits must be set to 0. * @nr_folios: Number of folios in the folios array. * @folios: KHO vmalloc descriptor pointing to the array of * struct memfd_luo_folio_ser.
@@ -67,11 +81,13 @@ struct memfd_luo_folio_ser {struct memfd_luo_ser {u64 pos;u64 size; +u32 seals; +u32 flags;u64 nr_folios;struct kho_vmalloc folios; } __packed;/* The compatibility string for memfd file handler */ -#define MEMFD_LUO_FH_COMPATIBLE "memfd-v1" +#define MEMFD_LUO_FH_COMPATIBLE "memfd-v2"#endif /* _LINUX_KHO_ABI_MEMFD_H */
mm/memfd_luo.c +33 -2
@@ -79,6 +79,8 @@#include <linux/shmem_fs.h> #include <linux/vmalloc.h> #include <linux/memfd.h> +#include <uapi/linux/memfd.h> +#include "internal.h" static int memfd_luo_preserve_folios(struct file *file,
@@ -222,7 +224,7 @@ static int memfd_luo_preserve(struct liveupdate_file_op_args *args)struct memfd_luo_folio_ser *folios_ser;struct memfd_luo_ser *ser;u64 nr_folios; -int err =0 ; +int err =0 , seals;inode_lock (inode);shmem_freeze (inode, true);
@@ -234,8 +236,21 @@ static int memfd_luo_preserve(struct liveupdate_file_op_args *args) goto err_unlock; } + seals =memfd_get_seals (args->file); +if (seals <0 ) { + err = seals; + goto err_free_ser; + } + +/* Make sure the file only has the seals supported by this version. */ +if (seals & ~MEMFD_LUO_ALL_SEALS ) { + err = -EOPNOTSUPP ; + goto err_free_ser; + } + ser->pos = args->file->f_pos; ser->size =i_size_read (inode); + ser->seals = seals; err =memfd_luo_preserve_folios (args->file, &ser->folios, &folios_ser, &nr_folios);
@@ -444,13 +459,29 @@ static int memfd_luo_retrieve(struct liveupdate_file_op_args *args)if (!ser)return -EINVAL ; - file =memfd_alloc_file ("" ,0 ); +/* Make sure the file only has seals supported by this version. */ +if (ser->seals & ~MEMFD_LUO_ALL_SEALS ) { + err = -EOPNOTSUPP ; + goto free_ser; + } + +/* +* The seals are preserved. Allow sealing here so they can be added +* later. +*/ + file =memfd_alloc_file ("" ,MFD_ALLOW_SEALING );if (IS_ERR (file)) {pr_err ("failed to setup file: %pe\n" , file); err =PTR_ERR (file); goto free_ser; } + err =memfd_add_seals (file, ser->seals); +if (err) { +pr_err ("failed to add seals: %pe\n" ,ERR_PTR (err)); + goto put_file; + } +vfs_setpos (file, ser->pos,MAX_LFS_FILESIZE ); file->f_inode->i_size = ser->size;
Signature · 1 line
2.53.0.335.g19a08e0c02-goog
Quoted 23 lines · Pratyush Yadav
From: "Pratyush Yadav (Google)" <[email protected]>
Hi,
This series adds support for preserving file seals when preserving a memfd using LUO. Patch 1 exports some memfd seal manipulation functions and patch 2 adds support for preserving them. Since it makes changes to the serialized data structure for memfd, it also bumps the version number.
Changes in v2:
- Define the set of seals supported by this version. Support for any other seal would need a version bump. Make sure the memfd only has this set of seals. Reject any that don't.- Make seals a u32 since uABI defined it as an int. Change the
__reservedinto flags.Pratyush Yadav (Google) (2):
memfd: export memfd_{add,get}_seals()
mm:memfd_luo: preserve file seals
Quoted 9 lines
include/linux/kho/abi/memfd.h | 18 +++++++++++++++++- include/linux/memfd.h | 12 ++++++++++++ mm/memfd.c | 4 ++-- mm/memfd_luo.c | 35 +++++++++++++++++++++++++++++++++-- 4 files changed, 64 insertions(+), 5 deletions(-)--
2.53.0.335.g19a08e0c02-goog
Tested with IOMMU persistence patch series using iommufd_liveupdate test.