Skip to content

Commit d741c27

Browse files
shivankgarg98smb49
authored andcommitted
fs: export anon_inode_make_secure_inode() and fix secretmem LSM bypass
BugLink: https://bugs.launchpad.net/bugs/2122072 [ Upstream commit cbe4134ea4bc493239786220bd69cb8a13493190 ] Export anon_inode_make_secure_inode() to allow KVM guest_memfd to create anonymous inodes with proper security context. This replaces the current pattern of calling alloc_anon_inode() followed by inode_init_security_anon() for creating security context manually. This change also fixes a security regression in secretmem where the S_PRIVATE flag was not cleared after alloc_anon_inode(), causing LSM/SELinux checks to be bypassed for secretmem file descriptors. As guest_memfd currently resides in the KVM module, we need to export this symbol for use outside the core kernel. In the future, guest_memfd might be moved to core-mm, at which point the symbols no longer would have to be exported. When/if that happens is still unclear. Fixes: 2bfe15c ("mm: create security context for memfd_secret inodes") Suggested-by: David Hildenbrand <david@redhat.com> Suggested-by: Mike Rapoport <rppt@kernel.org> Signed-off-by: Shivank Garg <shivankg@amd.com> Link: https://lore.kernel.org/20250620070328.803704-3-shivankg@amd.com Acked-by: "Mike Rapoport (Microsoft)" <rppt@kernel.org> Signed-off-by: Christian Brauner <brauner@kernel.org> Signed-off-by: Sasha Levin <sashal@kernel.org> Signed-off-by: Noah Wager <noah.wager@canonical.com> Signed-off-by: Stefan Bader <stefan.bader@canonical.com>
1 parent b78588c commit d741c27

File tree

3 files changed

+21
-13
lines changed

3 files changed

+21
-13
lines changed

fs/anon_inodes.c

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -57,14 +57,25 @@ static struct file_system_type anon_inode_fs_type = {
5757
.kill_sb = kill_anon_super,
5858
};
5959

60-
static struct inode *anon_inode_make_secure_inode(
61-
const char *name,
62-
const struct inode *context_inode)
60+
/**
61+
* anon_inode_make_secure_inode - allocate an anonymous inode with security context
62+
* @sb: [in] Superblock to allocate from
63+
* @name: [in] Name of the class of the newfile (e.g., "secretmem")
64+
* @context_inode:
65+
* [in] Optional parent inode for security inheritance
66+
*
67+
* The function ensures proper security initialization through the LSM hook
68+
* security_inode_init_security_anon().
69+
*
70+
* Return: Pointer to new inode on success, ERR_PTR on failure.
71+
*/
72+
struct inode *anon_inode_make_secure_inode(struct super_block *sb, const char *name,
73+
const struct inode *context_inode)
6374
{
6475
struct inode *inode;
6576
int error;
6677

67-
inode = alloc_anon_inode(anon_inode_mnt->mnt_sb);
78+
inode = alloc_anon_inode(sb);
6879
if (IS_ERR(inode))
6980
return inode;
7081
inode->i_flags &= ~S_PRIVATE;
@@ -76,6 +87,7 @@ static struct inode *anon_inode_make_secure_inode(
7687
}
7788
return inode;
7889
}
90+
EXPORT_SYMBOL_GPL_FOR_MODULES(anon_inode_make_secure_inode, "kvm");
7991

8092
static struct file *__anon_inode_getfile(const char *name,
8193
const struct file_operations *fops,
@@ -90,7 +102,8 @@ static struct file *__anon_inode_getfile(const char *name,
90102
return ERR_PTR(-ENOENT);
91103

92104
if (make_inode) {
93-
inode = anon_inode_make_secure_inode(name, context_inode);
105+
inode = anon_inode_make_secure_inode(anon_inode_mnt->mnt_sb,
106+
name, context_inode);
94107
if (IS_ERR(inode)) {
95108
file = ERR_CAST(inode);
96109
goto err;

include/linux/fs.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3542,6 +3542,8 @@ extern int simple_write_begin(struct file *file, struct address_space *mapping,
35423542
extern const struct address_space_operations ram_aops;
35433543
extern int always_delete_dentry(const struct dentry *);
35443544
extern struct inode *alloc_anon_inode(struct super_block *);
3545+
struct inode *anon_inode_make_secure_inode(struct super_block *sb, const char *name,
3546+
const struct inode *context_inode);
35453547
extern int simple_nosetlease(struct file *, int, struct file_lease **, void **);
35463548
extern const struct dentry_operations simple_dentry_operations;
35473549

mm/secretmem.c

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -195,18 +195,11 @@ static struct file *secretmem_file_create(unsigned long flags)
195195
struct file *file;
196196
struct inode *inode;
197197
const char *anon_name = "[secretmem]";
198-
int err;
199198

200-
inode = alloc_anon_inode(secretmem_mnt->mnt_sb);
199+
inode = anon_inode_make_secure_inode(secretmem_mnt->mnt_sb, anon_name, NULL);
201200
if (IS_ERR(inode))
202201
return ERR_CAST(inode);
203202

204-
err = security_inode_init_security_anon(inode, &QSTR(anon_name), NULL);
205-
if (err) {
206-
file = ERR_PTR(err);
207-
goto err_free_inode;
208-
}
209-
210203
file = alloc_file_pseudo(inode, secretmem_mnt, "secretmem",
211204
O_RDWR, &secretmem_fops);
212205
if (IS_ERR(file))

0 commit comments

Comments
 (0)