[PATCH] ovl: put the copy-up credentials when the copy_up hook fails

Anton Protopopov a.s.protopopov at gmail.com
Thu Dec 3 10:42:43 UTC 2026


The security_inode_copy_up() function calls the inode_copy_up hook of
every LSM in turn and stops at the first error. SELinux and Smack
allocate new credentials with prepare_creds() and return them in
*new. If an LSM which runs after them fails the hook, for example a
BPF LSM program which denies the copy-up, ovl_prepare_copy_up_creds()
returns the error and the credentials leak. Nobody puts them: the
cleanup of the copy_up_creds class skips error pointers.

Put the credentials before returning the error. put_cred() accepts
NULL, so this is also right when no LSM allocated them.

Fixes: d8ad8b496184 ("security, overlayfs: provide copy up security hook for unioned files")
Signed-off-by: Anton Protopopov <a.s.protopopov at gmail.com>
---
 fs/overlayfs/copy_up.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/fs/overlayfs/copy_up.c b/fs/overlayfs/copy_up.c
index c147ad155b79..6195e5d47174 100644
--- a/fs/overlayfs/copy_up.c
+++ b/fs/overlayfs/copy_up.c
@@ -732,8 +732,10 @@ static const struct cred *ovl_prepare_copy_up_creds(struct dentry *dentry)
 	int err;
 
 	err = security_inode_copy_up(dentry, &copy_up_cred);
-	if (err < 0)
+	if (err < 0) {
+		put_cred(copy_up_cred);
 		return ERR_PTR(err);
+	}
 
 	if (!copy_up_cred)
 		return NULL;

base-commit: 7475ea8b58616b33c04afb976cf0fbaad9dd63fb



More information about the Linux-security-module-archive mailing list