Skip to contentSkip to conversation

fs: Fix use of incorrect flags with splice() on pipe from/to memfd

PATCH In review Jan Polensky 3 participants 4 messages Jul 8, 2025 – Jul 11, 2025

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 fs: Fix use of incorrect flags with splice() on pipe from/to memfd

Fix use of incorrect flags when using splice() with pipe ends and memfd secret. Ensure that pipe and memfd file descriptors are properly recognized and handled to prevent unintended EACCES errors in scenarios where EBADF or EINVAL are expected.

This resolves failures in LTP's splice07 test case:

    ./ltp-bin/testcases/bin/splice07
    [skip]
    splice07.c:54: TFAIL: splice() on pipe read end -> memfd secret expected EBADF, EINVAL: EACCES (13)
    [skip]
    splice07.c:54: TFAIL: splice() on memfd secret -> pipe write end expected EBADF, EINVAL: EACCES (13)
    [skip]
Fixes cbe4134ea4bc ("fs: export anon_inode_make_secure_inode() and fix secretmem LSM bypass")
---
 fs/anon_inodes.c   | 11 +++++++----
 include/linux/fs.h |  2 +-
 mm/secretmem.c     |  2 +-
 3 files changed, 9 insertions(+), 6 deletions(-)
fs/anon_inodes.c +7 -4
index 1d847a939f29..f4eade76273b 100644
@@ -104,6 +104,7 @@ static struct file_system_type anon_inode_fs_type = {
  * @name:	[in]	Name of the class of the newfile (e.g., "secretmem")
  * @context_inode:
  *		[in]	Optional parent inode for security inheritance
+ * @secmem	[in]	Indicates wheather the inode should be threaded as secretmem
  *
  * The function ensures proper security initialization through the LSM hook
  * security_inode_init_security_anon().
@@ -111,7 +112,7 @@ static struct file_system_type anon_inode_fs_type = {
  * Return:	Pointer to new inode on success, ERR_PTR on failure.
  */
 struct inode *anon_inode_make_secure_inode(struct super_block *sb, const char *name,
-					   const struct inode *context_inode)
+					   const struct inode *context_inode, bool secmem)
 {
 	struct inode *inode;
 	int error;
@@ -119,8 +120,10 @@ struct inode *anon_inode_make_secure_inode(struct super_block *sb, const char *n
 	inode = alloc_anon_inode(sb);
 	if (IS_ERR(inode))
 		return inode;
-	inode->i_flags &= ~S_PRIVATE;
-	inode->i_op = &anon_inode_operations;
+	if (!secmem) {
+		inode->i_flags &= ~S_PRIVATE;
+		inode->i_op = &anon_inode_operations;
+	}
 	error =	security_inode_init_security_anon(inode, &QSTR(name),
 						  context_inode);
 	if (error) {
@@ -145,7 +148,7 @@ static struct file *__anon_inode_getfile(const char *name,
 
 	if (make_inode) {
 		inode =	anon_inode_make_secure_inode(anon_inode_mnt->mnt_sb,
-						     name, context_inode);
+						     name, context_inode, false);
 		if (IS_ERR(inode)) {
 			file = ERR_CAST(inode);
 			goto err;
include/linux/fs.h +1 -1
index 040c0036320f..50a32bee366e 100644
@@ -3609,7 +3609,7 @@ extern const struct address_space_operations ram_aops;
 extern int always_delete_dentry(const struct dentry *);
 extern struct inode *alloc_anon_inode(struct super_block *);
 struct inode *anon_inode_make_secure_inode(struct super_block *sb, const char *name,
-					   const struct inode *context_inode);
+					   const struct inode *context_inode, bool secmem);
 extern int simple_nosetlease(struct file *, int, struct file_lease **, void **);
 extern const struct dentry_operations simple_dentry_operations;
 
mm/secretmem.c +1 -1
index 9a11a38a6770..d28e1caa8847 100644
@@ -196,7 +196,7 @@ static struct file *secretmem_file_create(unsigned long flags)
 	struct inode *inode;
 	const char *anon_name = "[secretmem]";
 
-	inode = anon_inode_make_secure_inode(secretmem_mnt->mnt_sb, anon_name, NULL);
+	inode = anon_inode_make_secure_inode(secretmem_mnt->mnt_sb, anon_name, NULL, true);
 	if (IS_ERR(inode))
 		return ERR_CAST(inode);
 

--
2.48.1

#1 lore raw
kernel test robot noticed the following build warnings:

Hi Jan,

kernel test robot noticed the following build warnings:

[auto build test WARNING on brauner-vfs/vfs.all]
[also build test WARNING on akpm-mm/mm-everything linus/master v6.16-rc5 next-20250708] [If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]

url:    https://github.com/intel-lab-lkp/linux/commits/Jan-Polensky/fs-Fix-use-of-incorrect-flags-with-splice-on-pipe-from-to-memfd/20250708-234803
base:   https://git.kernel.org/pub/scm/linux/kernel/git/vfs/vfs.git vfs.all
patch link:    https://lore.kernel.org/r/20250708154352.3913726-1-japo%40linux.ibm.com
patch subject: [PATCH v1 1/1] fs: Fix use of incorrect flags with splice() on pipe from/to memfd
config: x86_64-buildonly-randconfig-001-20250709 (https://download.01.org/0day-ci/archive/20250709/[email protected]/config)
compiler: gcc-12 (Debian 12.2.0-14+deb12u1) 12.2.0
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20250709/[email protected]/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/

All warnings (new ones prefixed by >>):

>> Warning: fs/anon_inodes.c:115 function parameter 'secmem' not described in 'anon_inode_make_secure_inode'
Signature · 2 lines
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
#2 lore raw
In reply to Jan Polensky
Quoted 45 lines · Jan Polensky

Fix use of incorrect flags when using splice() with pipe ends and memfd secret. Ensure that pipe and memfd file descriptors are properly recognized and handled to prevent unintended EACCES errors in scenarios where EBADF or EINVAL are expected.

This resolves failures in LTP's splice07 test case:

    ./ltp-bin/testcases/bin/splice07
    [skip]
    splice07.c:54: TFAIL: splice() on pipe read end -> memfd secret expected EBADF, EINVAL: EACCES (13)
    [skip]
    splice07.c:54: TFAIL: splice() on memfd secret -> pipe write end expected EBADF, EINVAL: EACCES (13)
    [skip]
Fixes cbe4134ea4bc ("fs: export anon_inode_make_secure_inode() and fix secretmem LSM bypass")
---
 fs/anon_inodes.c   | 11 +++++++----
 include/linux/fs.h |  2 +-
 mm/secretmem.c     |  2 +-
 3 files changed, 9 insertions(+), 6 deletions(-)
fs/anon_inodes.c +2 -1
index 1d847a939f29..f4eade76273b 100644
@@ -104,6 +104,7 @@ static struct file_system_type anon_inode_fs_type = {
  * @name:	[in]	Name of the class of the newfile (e.g., "secretmem")
  * @context_inode:
  *		[in]	Optional parent inode for security inheritance
+ * @secmem	[in]	Indicates wheather the inode should be threaded as secretmem
  *
  * The function ensures proper security initialization through the LSM hook
  * security_inode_init_security_anon().
@@ -111,7 +112,7 @@ static struct file_system_type anon_inode_fs_type = {
  * Return:	Pointer to new inode on success, ERR_PTR on failure.
  */
 struct inode *anon_inode_make_secure_inode(struct super_block *sb, const char *name,
-					   const struct inode *context_inode)
+					   const struct inode *context_inode, bool secmem)
 {
 	struct inode *inode;
 	int error;

fs/anon_inodes.c struct inode *anon_inode_make_secure_inode(struct super_block *sb, const char *n · line 126

Quoted 9 lines
fs/anon_inodes.c +4 -2
@@ -119,8 +120,10 @@ struct inode *anon_inode_make_secure_inode(struct super_block *sb, const char *n
 	inode = alloc_anon_inode(sb);
 	if (IS_ERR(inode))
 		return inode;
-	inode->i_flags &= ~S_PRIVATE;
-	inode->i_op = &anon_inode_operations;
+	if (!secmem) {
+		inode->i_flags &= ~S_PRIVATE;
+		inode->i_op = &anon_inode_operations;
+	}

That hides secret memory inodes from LSMs which is the exact opposite of what the original commit was there to fix. I'm pretty sure that the EACCES comes from the LSM layer because the relevant refpolicy or however that works hasn't been updated to allow secret memory files to use splice().

This is a chicken-and-egg problem withy anything that strips S_PRIVATE from things that were previously S_PRIVATE.

#3 lore raw
Quoted 2 lines · Christian Brauner
Quoted 1 line · Jan Polensky

Fix use of incorrect flags when using splice() with pipe ends and

[skip]

Quoted 10 lines

fs/anon_inodes.c struct inode *anon_inode_make_secure_inode(struct super_block *sb, const char *n · line 126

Quoted 1 line
fs/anon_inodes.c +1 -0
+	}

That hides secret memory inodes from LSMs which is the exact opposite of what the original commit was there to fix. I'm pretty sure that the EACCES comes from the LSM layer because the relevant refpolicy or however that works hasn't been updated to allow secret memory files to use splice().

This is a chicken-and-egg problem withy anything that strips S_PRIVATE from things that were previously S_PRIVATE.

Yes, agree. I've already send a fix to LTP.
Thank you for your reply.