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.
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 */
#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,
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);
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);
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;