Skip to content

Commit f94c422

Browse files
shivankgarg98gregkh
authored andcommitted
fs: export anon_inode_make_secure_inode() and fix secretmem LSM bypass
[ Upstream commit cbe4134 ] 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]> Signed-off-by: Sasha Levin <[email protected]>
1 parent cdd9862 commit f94c422

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
@@ -55,14 +55,25 @@ static struct file_system_type anon_inode_fs_type = {
5555
.kill_sb = kill_anon_super,
5656
};
5757

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

65-
inode = alloc_anon_inode(anon_inode_mnt->mnt_sb);
76+
inode = alloc_anon_inode(sb);
6677
if (IS_ERR(inode))
6778
return inode;
6879
inode->i_flags &= ~S_PRIVATE;
@@ -74,6 +85,7 @@ static struct inode *anon_inode_make_secure_inode(
7485
}
7586
return inode;
7687
}
88+
EXPORT_SYMBOL_GPL_FOR_MODULES(anon_inode_make_secure_inode, "kvm");
7789

7890
static struct file *__anon_inode_getfile(const char *name,
7991
const struct file_operations *fops,
@@ -88,7 +100,8 @@ static struct file *__anon_inode_getfile(const char *name,
88100
return ERR_PTR(-ENOENT);
89101

90102
if (make_inode) {
91-
inode = anon_inode_make_secure_inode(name, context_inode);
103+
inode = anon_inode_make_secure_inode(anon_inode_mnt->mnt_sb,
104+
name, context_inode);
92105
if (IS_ERR(inode)) {
93106
file = ERR_CAST(inode);
94107
goto err;

include/linux/fs.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3407,6 +3407,8 @@ extern int simple_write_begin(struct file *file, struct address_space *mapping,
34073407
extern const struct address_space_operations ram_aops;
34083408
extern int always_delete_dentry(const struct dentry *);
34093409
extern struct inode *alloc_anon_inode(struct super_block *);
3410+
struct inode *anon_inode_make_secure_inode(struct super_block *sb, const char *name,
3411+
const struct inode *context_inode);
34103412
extern int simple_nosetlease(struct file *, int, struct file_lease **, void **);
34113413
extern const struct dentry_operations simple_dentry_operations;
34123414

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)