lsm: put the copy up creds when an LSM hook fails
Subsystems (3)
PATCH RESEND 1/1 lsm: put the copy up creds when an LSM hook fails
From: Chris Mason <[email protected]>
security_inode_copy_up() leaks a struct cred when one LSM allocates new creds for an overlayfs copy up and a later LSM then denies the copy up. One such pair is SELinux and a BPF LSM program that denies inode_copy_up. Enabling both LSMs and loading the program requires root. After that, each denied copy up leaks one struct cred, no matter which user triggers it.
The hook calls each enabled LSM in turn. SELinux or Smack allocates the creds and returns them through a pointer argument of the hook. A later LSM can still return an error, and the hook then returns that error with the creds still allocated. Its kernel-doc does not say that the caller owns them on error, and the only caller, overlayfs, does not put them.
Reproduced with SELinux and such a BPF LSM program: 36000 failed opens and chmods by an unprivileged user on an overlayfs mount left 36000 struct cred objects allocated, which kmemleak reported.
Fix this by putting the creds in security_inode_copy_up() when the hook fails, so the caller gets either creds that it owns or none.
---
Hi Paul,
Apologies for missed the LSM list and maintainers in first posting [1]. Resending with proper Cc list.
I think this LSM hook was introduced with confusing contact for chained LSMs and this change makes the hook contract more sane in this case.
Thanks,
Amir.
[1] https://lore.kernel.org/r/[email protected]/
security/security.c | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-)
security/security.c +11 -3
@@ -2314,18 +2314,26 @@ void security_inode_getlsmprop(struct inode *inode, struct lsm_prop *prop)/** * security_inode_copy_up() - Create new creds for an overlayfs copy-up op * @src: union dentry of copy-up file -* @new: newly created creds +* @new: newly created creds, must point to NULL on entry * * A file is about to be copied up from lower layer to upper layer of overlay * filesystem. Security module can prepare a set of new creds and modify as * need be and return new creds. Caller will switch to new creds temporarily to * create new file and release newly allocated creds. * -* Return: Returns 0 on success or a negative error code on error. +* Return: Returns 0 on success or a negative error code on error, in which +* case @new is left pointing to NULL. */ int security_inode_copy_up (struct dentry *src,struct cred **new) { -return call_int_hook (inode_copy_up, src, new); +int rc =call_int_hook (inode_copy_up, src, new); + +/* An earlier LSM may have prepared creds before a later one failed */ +if (unlikely (rc <0 ) && *new) { +put_cred (*new); + *new =NULL ; + } +return rc; }EXPORT_SYMBOL (security_inode_copy_up);
Signature · 1 line
2.55.0
[email protected]
AI reviewSashiko has reviewed this patch and found no issues.
Quoted 19 lines
From: Chris Mason <[email protected]>
security_inode_copy_up()leaks a struct cred when one LSM allocates new creds for an overlayfs copy up and a later LSM then denies the copy up. One such pair is SELinux and a BPF LSM program that deniesinode_copy_up. Enabling both LSMs and loading the program requires root. After that, each denied copy up leaks one struct cred, no matter which user triggers it.The hook calls each enabled LSM in turn. SELinux or Smack allocates the creds and returns them through a pointer argument of the hook. A later LSM can still return an error, and the hook then returns that error with the creds still allocated. Its kernel-doc does not say that the caller owns them on error, and the only caller, overlayfs, does not put them.
Reproduced with SELinux and such a BPF LSM program: 36000 failed opens and chmods by an unprivileged user on an overlayfs mount left 36000 struct cred objects allocated, which kmemleak reported.
Quoted 8 lines
Fix this by putting the creds in
security_inode_copy_up()when the hook fails, so the caller gets either creds that it owns or none.Fixes d8ad8b496184 ("security, overlayfs: provide copy up security hook for unioned files")Signed-off-by Chris Mason <[email protected]>[amir: put the creds in security_inode_copy_up() instead of in overlayfs]Signed-off-by Amir Goldstein <[email protected]>
Sashiko has reviewed this patch and found no issues.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Paul Moore
maintainerQuoted 35 lines · On Nov 20, 2026 [email protected] wrote:
security_inode_copy_up()leaks a struct cred when one LSM allocates new creds for an overlayfs copy up and a later LSM then denies the copy up. One such pair is SELinux and a BPF LSM program that deniesinode_copy_up. Enabling both LSMs and loading the program requires root. After that, each denied copy up leaks one struct cred, no matter which user triggers it.The hook calls each enabled LSM in turn. SELinux or Smack allocates the creds and returns them through a pointer argument of the hook. A later LSM can still return an error, and the hook then returns that error with the creds still allocated. Its kernel-doc does not say that the caller owns them on error, and the only caller, overlayfs, does not put them.
Reproduced with SELinux and such a BPF LSM program: 36000 failed opens and chmods by an unprivileged user on an overlayfs mount left 36000 struct cred objects allocated, which kmemleak reported.
Fix this by putting the creds in
security_inode_copy_up()when the hook fails, so the caller gets either creds that it owns or none.Fixes d8ad8b496184 ("security, overlayfs: provide copy up security hook for unioned files")Signed-off-by Chris Mason <[email protected]>[amir: put the creds in security_inode_copy_up() instead of in overlayfs]Signed-off-by Amir Goldstein <[email protected]>---
Hi Paul,Apologies for missed the LSM list and maintainers in first posting [1]. Resending with proper Cc list.
I think this LSM hook was introduced with confusing contact for chained LSMs and this change makes the hook contract more sane in this case.
Quoted 7 lines
Thanks,
Amir.[1] https://lore.kernel.org/r/[email protected]/
security/security.c | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-)
Thanks Amir, Chris - this looks good to me, I'm going to merge this into lsm/stable-7.4 and I'll send it up to Linus after it has had a few days in linux-next.
--
paul-moore.com