A secretmem inode can outlive its original file. An O_PATH descriptor opened through /proc/self/fd keeps the inode and its unevictable folios alive after the mapping and original descriptor are closed. The file release path nevertheless drops the locked-memory charge, allowing the same user to reuse RLIMIT_MEMLOCK while those folios remain allocated. Move private-state and locked-memory accounting cleanup to inode eviction, after truncating the page cache. Keep secretmem_users active over the same lifetime so that the hibernation guard covers retained secret pages. Allocate private state before inode creation. If the secure-inode helper fails, release that unattached state and its user reference at a dedicated error label. Once attached, an alloc_file_pseudo() failure drops the inode with iput(); secretmem_evict_inode() then frees the state and user reference. An LSM denial inside the helper can evict an inode before i_private is set, so eviction skips private cleanup for such inodes. In an arm64 QEMU/KVM guest with an 8 KiB RLIMIT_MEMLOCK, the baseline allowed an additional 4 KiB secretmem fault while O_PATH retained two pages. With this change, that fault raises SIGBUS until the last O_PATH reference closes. Fixes: 97d34aa65c29 ("mm/secretmem: properly account locked pages") Signed-off-by: Baek Kyu, Kim --- Changes in v3: - Move cleanup of state allocated before inode creation to a goto label. Link to v2: https://lore-kernel.gnuweeb.org/linux-mm/20261121164020.1465498-1-pocj8ur4in@gmail.com/ If alloc_path_pseudo() fails, the caller keeps its inode reference. If alloc_file() fails after d_instantiate(), alloc_file_pseudo() takes another inode reference with ihold(inode) before path_put() releases the dentry. In both cases, the caller's iput(inode) drops the remaining reference. On eviction, secretmem_evict_inode() calls secretmem_destroy_inode_priv() to release state->user. Calling that destructor after iput() could dereference a freed inode. Tests in v3: - On arm64 QEMU/KVM, the baseline allowed an extra fault while O_PATH retained two secretmem pages. V2 and v3 rejected it until the last O_PATH close, then allowed it. - Injected secure-inode helper failure returned EACCES after NULL-state eviction and err_free_state cleanup. Injected file allocation failure returned ENOMEM; inode eviction ran free_uid(). - In the product Image, /sys/power/state listed "disk" before secretmem use, omitted it while O_PATH alone retained a populated inode, and listed it again after the last O_PATH close. - The product Image built and booted after removing the test hooks. W=1 used KCFLAGS=-Wno-error for unrelated GCC 16 warnings; mm/secretmem.c had none. checkpatch.pl --strict reported no issues. - KASAN, lockdep, a full hibernation cycle and long-running concurrency were not tested. mm/secretmem.c | 63 +++++++++++++++++++++++++++++----------------------------- 1 file changed, 32 insertions(+), 31 deletions(-) diff --git a/mm/secretmem.c b/mm/secretmem.c index b3f79ad56790..433d31392379 100644 --- a/mm/secretmem.c +++ b/mm/secretmem.c @@ -189,14 +189,7 @@ static void secretmem_destroy_inode_priv(struct inode *inode) free_uid(state->user); kfree(state); inode->i_private = NULL; -} - -static int secretmem_release(struct inode *inode, struct file *file) -{ atomic_dec(&secretmem_users); - secretmem_destroy_inode_priv(inode); - - return 0; } static int secretmem_mmap_prepare(struct vm_area_desc *desc) @@ -216,7 +209,6 @@ bool vma_is_secretmem(struct vm_area_struct *vma) } static const struct file_operations secretmem_fops = { - .release = secretmem_release, .mmap_prepare = secretmem_mmap_prepare, }; @@ -264,39 +256,33 @@ static const struct inode_operations secretmem_iops = { static struct vfsmount *secretmem_mnt; -static int secretmem_init_inode_priv(struct inode *inode) -{ - struct secretmem_inode_state *state; - - state = kzalloc_obj(*state); - if (!state) - return -ENOMEM; - - state->user = get_uid(current_user()); - inode->i_private = state; - return 0; -} - static struct file *secretmem_file_create(unsigned long flags) { + struct secretmem_inode_state *state; struct file *file; struct inode *inode; const char *anon_name = "[secretmem]"; int err; + state = kzalloc_obj(*state); + if (!state) + return ERR_PTR(-ENOMEM); + state->user = get_uid(current_user()); + inode = anon_inode_make_secure_inode(secretmem_mnt->mnt_sb, anon_name, NULL); - if (IS_ERR(inode)) - return ERR_CAST(inode); + if (IS_ERR(inode)) { + err = PTR_ERR(inode); + goto err_free_state; + } - err = secretmem_init_inode_priv(inode); - if (err) - goto err_free_inode; + inode->i_private = state; + atomic_inc(&secretmem_users); file = alloc_file_pseudo(inode, secretmem_mnt, "secretmem", O_RDWR | O_LARGEFILE, &secretmem_fops); if (IS_ERR(file)) { err = PTR_ERR(file); - goto err_free_priv; + goto err_free_inode; } mapping_set_gfp_mask(inode->i_mapping, GFP_USER); @@ -309,14 +295,14 @@ static struct file *secretmem_file_create(unsigned long flags) inode->i_mode |= S_IFREG; inode->i_size = 0; - atomic_inc(&secretmem_users); - return file; -err_free_priv: - secretmem_destroy_inode_priv(inode); err_free_inode: iput(inode); return ERR_PTR(err); +err_free_state: + free_uid(state->user); + kfree(state); + return ERR_PTR(err); } SYSCALL_DEFINE1(memfd_secret, unsigned int, flags) @@ -335,6 +321,20 @@ SYSCALL_DEFINE1(memfd_secret, unsigned int, flags) return FD_ADD(flags & O_CLOEXEC, secretmem_file_create(flags)); } +static void secretmem_evict_inode(struct inode *inode) +{ + truncate_inode_pages_final(&inode->i_data); + clear_inode(inode); + /* The filesystem root and failed secure inodes have no private state. */ + if (inode->i_private) + secretmem_destroy_inode_priv(inode); +} + +static const struct super_operations secretmem_super_ops = { + .statfs = simple_statfs, + .evict_inode = secretmem_evict_inode, +}; + static int secretmem_init_fs_context(struct fs_context *fc) { struct pseudo_fs_context *ctx; @@ -342,6 +342,7 @@ static int secretmem_init_fs_context(struct fs_context *fc) ctx = init_pseudo(fc, SECRETMEM_MAGIC); if (!ctx) return -ENOMEM; + ctx->ops = &secretmem_super_ops; return 0; } -- 2.55.0