Skip to content

Commit cbe4134

Browse files
shivankgarg98brauner
authored andcommitted
fs: export anon_inode_make_secure_inode() and fix secretmem LSM bypass
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 <[email protected]> Suggested-by: Mike Rapoport <[email protected]> Signed-off-by: Shivank Garg <[email protected]> Link: https://lore.kernel.org/[email protected] Acked-by: "Mike Rapoport (Microsoft)" <[email protected]> Signed-off-by: Christian Brauner <[email protected]>
1 parent 6a68d28 commit cbe4134

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
@@ -98,14 +98,25 @@ static struct file_system_type anon_inode_fs_type = {
9898
.kill_sb = kill_anon_super,
9999
};
100100

101-
static struct inode *anon_inode_make_secure_inode(
102-
const char *name,
103-
const struct inode *context_inode)
101+
/**
102+
* anon_inode_make_secure_inode - allocate an anonymous inode with security context
103+
* @sb: [in] Superblock to allocate from
104+
* @name: [in] Name of the class of the newfile (e.g., "secretmem")
105+
* @context_inode:
106+
* [in] Optional parent inode for security inheritance
107+
*
108+
* The function ensures proper security initialization through the LSM hook
109+
* security_inode_init_security_anon().
110+
*
111+
* Return: Pointer to new inode on success, ERR_PTR on failure.
112+
*/
113+
struct inode *anon_inode_make_secure_inode(struct super_block *sb, const char *name,
114+
const struct inode *context_inode)
104115
{
105116
struct inode *inode;
106117
int error;
107118

108-
inode = alloc_anon_inode(anon_inode_mnt->mnt_sb);
119+
inode = alloc_anon_inode(sb);
109120
if (IS_ERR(inode))
110121
return inode;
111122
inode->i_flags &= ~S_PRIVATE;
@@ -118,6 +129,7 @@ static struct inode *anon_inode_make_secure_inode(
118129
}
119130
return inode;
120131
}
132+
EXPORT_SYMBOL_GPL_FOR_MODULES(anon_inode_make_secure_inode, "kvm");
121133

122134
static struct file *__anon_inode_getfile(const char *name,
123135
const struct file_operations *fops,
@@ -132,7 +144,8 @@ static struct file *__anon_inode_getfile(const char *name,
132144
return ERR_PTR(-ENOENT);
133145

134146
if (make_inode) {
135-
inode = anon_inode_make_secure_inode(name, context_inode);
147+
inode = anon_inode_make_secure_inode(anon_inode_mnt->mnt_sb,
148+
name, context_inode);
136149
if (IS_ERR(inode)) {
137150
file = ERR_CAST(inode);
138151
goto err;

include/linux/fs.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3606,6 +3606,8 @@ extern int simple_write_begin(struct file *file, struct address_space *mapping,
36063606
extern const struct address_space_operations ram_aops;
36073607
extern int always_delete_dentry(const struct dentry *);
36083608
extern struct inode *alloc_anon_inode(struct super_block *);
3609+
struct inode *anon_inode_make_secure_inode(struct super_block *sb, const char *name,
3610+
const struct inode *context_inode);
36093611
extern int simple_nosetlease(struct file *, int, struct file_lease **, void **);
36103612
extern const struct dentry_operations simple_dentry_operations;
36113613

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)