Skip to contentSkip to conversation

SELinux: Fix lsm_get_self_attr()

PATCH In review Mickaël Salaün 3 participants 17 messages Feb 23, 2024 – May 13, 2024

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

PATCH 1/2 SELinux: Fix lsm_get_self_attr()

selinux_lsm_getattr() may not initialize the value's pointer in some case. As for proc_pid_attr_read(), initialize this pointer to NULL in selinux_getselfattr() to avoid an UAF in the kfree() call.

Cc Casey Schaufler <[email protected]>
Cc Paul Moore <[email protected]>
Fixes 762c934317e6 ("SELinux: Add selfattr hooks")
---
 security/selinux/hooks.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
security/selinux/hooks.c +1 -1
index a6bf90ace84c..338b023a8c3e 100644
@@ -6559,7 +6559,7 @@ static int selinux_getselfattr(unsigned int attr, struct lsm_ctx __user *ctx,
 			       size_t *size, u32 flags)
 {
 	int rc;
-	char *val;
+	char *val = NULL;
 	int val_len;
 
 	val_len = selinux_lsm_getattr(attr, current, &val);
Signature · 1 line
2.43.0
#1 lore raw

PATCH 2/2 AppArmor: Fix lsm_get_self_attr()

aa_getprocattr() may not initialize the value's pointer in some case. As for proc_pid_attr_read(), initialize this pointer to NULL in apparmor_getselfattr() to avoid an UAF in the kfree() call.

Cc Casey Schaufler <[email protected]>
Cc John Johansen <[email protected]>
Cc Paul Moore <[email protected]>
Fixes 223981db9baf ("AppArmor: Add selfattr hooks")
---
 security/apparmor/lsm.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
security/apparmor/lsm.c +1 -1
index 98e1150bee9d..9a3dcaafb5b1 100644
@@ -784,7 +784,7 @@ static int apparmor_getselfattr(unsigned int attr, struct lsm_ctx __user *lx,
 	int error = -ENOENT;
 	struct aa_task_ctx *ctx = task_ctx(current);
 	struct aa_label *label = NULL;
-	char *value;
+	char *value = NULL;
 
 	switch (attr) {
 	case LSM_ATTR_CURRENT:
Signature · 1 line
2.43.0
maintainer
#2 lore raw

Re: [PATCH 2/2] AppArmor: Fix lsm_get_self_attr()

Quoted 11 lines · Mickaël Salaün

aa_getprocattr() may not initialize the value's pointer in some case. As for proc_pid_attr_read(), initialize this pointer to NULL in apparmor_getselfattr() to avoid an UAF in the kfree() call.

Cc Casey Schaufler <[email protected]>
Cc John Johansen <[email protected]>
Cc Paul Moore <[email protected]>
Fixes 223981db9baf ("AppArmor: Add selfattr hooks")
Quoted 3 lines
---
 security/apparmor/lsm.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

If you like John, I can send this up to Linus with the related SELinux fix, I would just need an ACK from you.

Quoted 15 lines
security/apparmor/lsm.c +1 -2
index 98e1150bee9d..9a3dcaafb5b1 100644
@@ -784,7 +784,7 @@ static int apparmor_getselfattr(unsigned int attr, struct lsm_ctx __user *lx,
        int error = -ENOENT;
        struct aa_task_ctx *ctx = task_ctx(current);
        struct aa_label *label = NULL;
-       char *value;
+       char *value = NULL;
 
        switch (attr) {
        case LSM_ATTR_CURRENT:
--

2.43.0

Signature · 1 line
paul-moore.com
maintainer Reviewed-by
#3 lore raw

Re: [PATCH 2/2] AppArmor: Fix lsm_get_self_attr()

Quoted 16 lines · Paul Moore
Quoted 11 lines · Mickaël Salaün

aa_getprocattr() may not initialize the value's pointer in some case. As for proc_pid_attr_read(), initialize this pointer to NULL in apparmor_getselfattr() to avoid an UAF in the kfree() call.

Cc Casey Schaufler <[email protected]>
Cc John Johansen <[email protected]>
Cc Paul Moore <[email protected]>
Fixes 223981db9baf ("AppArmor: Add selfattr hooks")
Quoted 3 lines
---
 security/apparmor/lsm.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
Quoted 2 lines

If you like John, I can send this up to Linus with the related SELinux fix, I would just need an ACK from you.

This patch looks good to me, and while we've still got at least two (maybe three?) more weeks before v6.8 is tagged, I think it would be good to get this up to Linus ASAP. I'll hold off for another day, but if we don't see any comment from John I'll go ahead and merge this and send it up to Linus with the SELinux fix; I'm sure John wouldn't be happy if v6.8 went out the door without this fix.

Quoted 15 lines
Quoted 15 lines
security/apparmor/lsm.c +1 -2
index 98e1150bee9d..9a3dcaafb5b1 100644
@@ -784,7 +784,7 @@ static int apparmor_getselfattr(unsigned int attr, struct lsm_ctx __user *lx,
        int error = -ENOENT;
        struct aa_task_ctx *ctx = task_ctx(current);
        struct aa_label *label = NULL;
-       char *value;
+       char *value = NULL;
 
        switch (attr) {
        case LSM_ATTR_CURRENT:
--

2.43.0

Signature · 1 line
paul-moore.com
maintainer
#4 lore raw

Re: [PATCH 2/2] AppArmor: Fix lsm_get_self_attr()

Quoted 20 lines · Paul Moore
Quoted 16 lines · Paul Moore
Quoted 11 lines · Mickaël Salaün

aa_getprocattr() may not initialize the value's pointer in some case. As for proc_pid_attr_read(), initialize this pointer to NULL in apparmor_getselfattr() to avoid an UAF in the kfree() call.

Cc Casey Schaufler <[email protected]>
Cc John Johansen <[email protected]>
Cc Paul Moore <[email protected]>
Fixes 223981db9baf ("AppArmor: Add selfattr hooks")
Quoted 3 lines
---
 security/apparmor/lsm.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
Quoted 2 lines

If you like John, I can send this up to Linus with the related SELinux fix, I would just need an ACK from you.

Quoted 8 lines

This patch looks good to me, and while we've still got at least two (maybe three?) more weeks before v6.8 is tagged, I think it would be good to get this up to Linus ASAP. I'll hold off for another day, but if we don't see any comment from John I'll go ahead and merge this and send it up to Linus with the SELinux fix; I'm sure John wouldn't be happy if v6.8 went out the door without this fix.

I just merged this into lsm/stable-6.8 and once the automated build/test has done it's thing and come back clean I'll send this, along with the associated SELinux fix, up to Linus. Thanks all.

John, if this commit is problematic please let me know and I'll send a fix or a revert.

Signature · 1 line
paul-moore.com
maintainer
#5 lore raw

Re: [PATCH 2/2] AppArmor: Fix lsm_get_self_attr()

Quoted 30 lines · Paul Moore
Quoted 20 lines · Paul Moore
Quoted 16 lines · Paul Moore
Quoted 11 lines · Mickaël Salaün

aa_getprocattr() may not initialize the value's pointer in some case. As for proc_pid_attr_read(), initialize this pointer to NULL in apparmor_getselfattr() to avoid an UAF in the kfree() call.

Cc Casey Schaufler <[email protected]>
Cc John Johansen <[email protected]>
Cc Paul Moore <[email protected]>
Fixes 223981db9baf ("AppArmor: Add selfattr hooks")
Quoted 3 lines
---
 security/apparmor/lsm.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
Quoted 2 lines

If you like John, I can send this up to Linus with the related SELinux fix, I would just need an ACK from you.

Quoted 8 lines

This patch looks good to me, and while we've still got at least two (maybe three?) more weeks before v6.8 is tagged, I think it would be good to get this up to Linus ASAP. I'll hold off for another day, but if we don't see any comment from John I'll go ahead and merge this and send it up to Linus with the SELinux fix; I'm sure John wouldn't be happy if v6.8 went out the door without this fix.

Quoted 3 lines

I just merged this into lsm/stable-6.8 and once the automated build/test has done it's thing and come back clean I'll send this, along with the associated SELinux fix, up to Linus. Thanks all.

In off-list discussions with Mickaël today it was noted that this patch also needs a fixup to the commit description so I've replaced it with the following:

"In apparmor_getselfattr() when an invalid AppArmor
attribute is requested, or a value hasn't been explicitly set for the requested attribute, the label passed to aa_put_label() is not properly initialized which can cause problems when the pointer value is non-NULL and AppArmor attempts to drop a reference on the bogus label object."

I've updated the commit in lsm/stable-6.8 and I'll be sending it to Linus shortly.

Quoted 2 lines

John, if this commit is problematic please let me know and I'll send a fix or a revert.

Signature · 1 line
paul-moore.com
maintainer
#6 lore raw

Re: [PATCH 2/2] AppArmor: Fix lsm_get_self_attr()

Quoted 46 lines · Paul Moore
Quoted 30 lines · Paul Moore
Quoted 20 lines · Paul Moore
Quoted 16 lines · Paul Moore
Quoted 11 lines · Mickaël Salaün

aa_getprocattr() may not initialize the value's pointer in some case. As for proc_pid_attr_read(), initialize this pointer to NULL in apparmor_getselfattr() to avoid an UAF in the kfree() call.

Cc Casey Schaufler <[email protected]>
Cc John Johansen <[email protected]>
Cc Paul Moore <[email protected]>
Fixes 223981db9baf ("AppArmor: Add selfattr hooks")
Quoted 3 lines
---
 security/apparmor/lsm.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
Quoted 2 lines

If you like John, I can send this up to Linus with the related SELinux fix, I would just need an ACK from you.

Quoted 8 lines

This patch looks good to me, and while we've still got at least two (maybe three?) more weeks before v6.8 is tagged, I think it would be good to get this up to Linus ASAP. I'll hold off for another day, but if we don't see any comment from John I'll go ahead and merge this and send it up to Linus with the SELinux fix; I'm sure John wouldn't be happy if v6.8 went out the door without this fix.

Quoted 3 lines

I just merged this into lsm/stable-6.8 and once the automated build/test has done it's thing and come back clean I'll send this, along with the associated SELinux fix, up to Linus. Thanks all.

In off-list discussions with Mickaël today it was noted that this patch also needs a fixup to the commit description so I've replaced it with the following:

"In apparmor_getselfattr() when an invalid AppArmor
attribute is requested, or a value hasn't been explicitly set for the requested attribute, the label passed to aa_put_label() is not properly initialized which can cause problems when the pointer value is non-NULL and AppArmor attempts to drop a reference on the bogus label object."

Quoted 5 lines

I've updated the commit in lsm/stable-6.8 and I'll be sending it to Linus shortly.

Quoted 2 lines

John, if this commit is problematic please let me know and I'll send a fix or a revert.

I also just realized that both this patch and the SELinux have the stable kernel marking which shouldn't be necessary as the LSM syscalls are only present in the v6.8-rcX kernels. I'm going to drop the stable tagging, but leave the 'Fixes:' tag of course.

Signature · 1 line
paul-moore.com
#7 lore raw

Re: [PATCH 2/2] AppArmor: Fix lsm_get_self_attr()

Quoted 50 lines · Paul Moore
Quoted 49 lines · Paul Moore
Quoted 30 lines · Paul Moore
Quoted 20 lines · Paul Moore
Quoted 16 lines · Paul Moore
Quoted 11 lines · Mickaël Salaün

aa_getprocattr() may not initialize the value's pointer in some case. As for proc_pid_attr_read(), initialize this pointer to NULL in apparmor_getselfattr() to avoid an UAF in the kfree() call.

Cc Casey Schaufler <[email protected]>
Cc John Johansen <[email protected]>
Cc Paul Moore <[email protected]>
Fixes 223981db9baf ("AppArmor: Add selfattr hooks")
Quoted 3 lines
---
 security/apparmor/lsm.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
Quoted 2 lines

If you like John, I can send this up to Linus with the related SELinux fix, I would just need an ACK from you.

Quoted 8 lines

This patch looks good to me, and while we've still got at least two (maybe three?) more weeks before v6.8 is tagged, I think it would be good to get this up to Linus ASAP. I'll hold off for another day, but if we don't see any comment from John I'll go ahead and merge this and send it up to Linus with the SELinux fix; I'm sure John wouldn't be happy if v6.8 went out the door without this fix.

Quoted 3 lines

I just merged this into lsm/stable-6.8 and once the automated build/test has done it's thing and come back clean I'll send this, along with the associated SELinux fix, up to Linus. Thanks all.

In off-list discussions with Mickaël today it was noted that this patch also needs a fixup to the commit description so I've replaced it with the following:

"In apparmor_getselfattr() when an invalid AppArmor
attribute is requested, or a value hasn't been explicitly set for the requested attribute, the label passed to aa_put_label() is not properly initialized which can cause problems when the pointer value is non-NULL and AppArmor attempts to drop a reference on the bogus label object."

I've updated the commit in lsm/stable-6.8 and I'll be sending it to Linus shortly.

Quoted 7 lines
Quoted 2 lines
Quoted 2 lines

John, if this commit is problematic please let me know and I'll send a fix or a revert.

I also just realized that both this patch and the SELinux have the stable kernel marking which shouldn't be necessary as the LSM syscalls are only present in the v6.8-rcX kernels. I'm going to drop the stable tagging, but leave the 'Fixes:' tag of course.

Looks good, thanks!

Quoted 4 lines
Signature · 2 lines
-- 
paul-moore.com
maintainer
#8 lore raw

Re: [PATCH 2/2] AppArmor: Fix lsm_get_self_attr()

In reply to Paul Moore
Quoted 30 lines · Paul Moore
Quoted 20 lines · Paul Moore
Quoted 16 lines · Paul Moore
Quoted 11 lines · Mickaël Salaün

aa_getprocattr() may not initialize the value's pointer in some case. As for proc_pid_attr_read(), initialize this pointer to NULL in apparmor_getselfattr() to avoid an UAF in the kfree() call.

Cc Casey Schaufler <[email protected]>
Cc John Johansen <[email protected]>
Cc Paul Moore <[email protected]>
Fixes 223981db9baf ("AppArmor: Add selfattr hooks")
Quoted 3 lines
---
  security/apparmor/lsm.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
Quoted 2 lines

If you like John, I can send this up to Linus with the related SELinux fix, I would just need an ACK from you.

Quoted 8 lines

This patch looks good to me, and while we've still got at least two (maybe three?) more weeks before v6.8 is tagged, I think it would be good to get this up to Linus ASAP. I'll hold off for another day, but if we don't see any comment from John I'll go ahead and merge this and send it up to Linus with the SELinux fix; I'm sure John wouldn't be happy if v6.8 went out the door without this fix.

Quoted 7 lines

I just merged this into lsm/stable-6.8 and once the automated build/test has done it's thing and come back clean I'll send this, along with the associated SELinux fix, up to Linus. Thanks all.

John, if this commit is problematic please let me know and I'll send a fix or a revert.

sorry, I am still trying to dig out of my backlog. This is good, you can certainly have my ACK, I know its already in tree so no point in adding it there but wanted to just make sure its on list

maintainer
#9 lore raw

Re: [PATCH 2/2] AppArmor: Fix lsm_get_self_attr()

Quoted 35 lines · John Johansen
Quoted 34 lines · Paul Moore
Quoted 20 lines · Paul Moore
Quoted 16 lines · Paul Moore
Quoted 11 lines · Mickaël Salaün

aa_getprocattr() may not initialize the value's pointer in some case. As for proc_pid_attr_read(), initialize this pointer to NULL in apparmor_getselfattr() to avoid an UAF in the kfree() call.

Cc Casey Schaufler <[email protected]>
Cc John Johansen <[email protected]>
Cc Paul Moore <[email protected]>
Fixes 223981db9baf ("AppArmor: Add selfattr hooks")
Quoted 3 lines
---
  security/apparmor/lsm.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
Quoted 2 lines

If you like John, I can send this up to Linus with the related SELinux fix, I would just need an ACK from you.

Quoted 8 lines

This patch looks good to me, and while we've still got at least two (maybe three?) more weeks before v6.8 is tagged, I think it would be good to get this up to Linus ASAP. I'll hold off for another day, but if we don't see any comment from John I'll go ahead and merge this and send it up to Linus with the SELinux fix; I'm sure John wouldn't be happy if v6.8 went out the door without this fix.

I just merged this into lsm/stable-6.8 and once the automated build/test has done it's thing and come back clean I'll send this, along with the associated SELinux fix, up to Linus. Thanks all.

Quoted 6 lines
Quoted 2 lines

John, if this commit is problematic please let me know and I'll send a fix or a revert.

sorry, I am still trying to dig out of my backlog. This is good, you can certainly have my ACK, I know its already in tree so no point in adding it there but wanted to just make sure its on list

No worries, reviews are still appreciated; just because a patch has made its way up to Linus is no guarantee there isn't something wrong with it ;)

Signature · 1 line
paul-moore.com
#10 lore raw
In reply to Mickaël Salaün

These bugs have been found with syzkaller. I just sent a PR to add support for the new LSM syscalls:
https://github.com/google/syzkaller/pull/4524

Quoted 29 lines · Mickaël Salaün

selinux_lsm_getattr() may not initialize the value's pointer in some case. As for proc_pid_attr_read(), initialize this pointer to NULL in selinux_getselfattr() to avoid an UAF in the kfree() call.

Cc Casey Schaufler <[email protected]>
Cc Paul Moore <[email protected]>
Fixes 762c934317e6 ("SELinux: Add selfattr hooks")
---
 security/selinux/hooks.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
security/selinux/hooks.c +1 -1
index a6bf90ace84c..338b023a8c3e 100644
@@ -6559,7 +6559,7 @@ static int selinux_getselfattr(unsigned int attr, struct lsm_ctx __user *ctx,
 			       size_t *size, u32 flags)
 {
 	int rc;
-	char *val;
+	char *val = NULL;
 	int val_len;
 
 	val_len = selinux_lsm_getattr(attr, current, &val);
Signature · 1 line
2.43.0
#12 lore raw
In reply to Mickaël Salaün
Quoted 3 lines · Mickaël Salaün

selinux_lsm_getattr() may not initialize the value's pointer in some case. As for proc_pid_attr_read(), initialize this pointer to NULL in selinux_getselfattr() to avoid an UAF in the kfree() call.

Not UAF but NULL pointer dereference (both patches)...

#13 lore raw
Quoted 6 lines · Mickaël Salaün
Quoted 3 lines · Mickaël Salaün

selinux_lsm_getattr() may not initialize the value's pointer in some case. As for proc_pid_attr_read(), initialize this pointer to NULL in selinux_getselfattr() to avoid an UAF in the kfree() call.

Not UAF but NULL pointer dereference (both patches)...

Well, that may be the result (as observed with the kfree() call), but the cause is obviously an uninitialized pointer.

maintainer
#14 lore raw
Quoted 11 lines · Mickaël Salaün
Quoted 6 lines · Mickaël Salaün
Quoted 3 lines · Mickaël Salaün

selinux_lsm_getattr() may not initialize the value's pointer in some case. As for proc_pid_attr_read(), initialize this pointer to NULL in selinux_getselfattr() to avoid an UAF in the kfree() call.

Not UAF but NULL pointer dereference (both patches)...

Well, that may be the result (as observed with the kfree() call), but the cause is obviously an uninitialized pointer.

Adding the SELinux list to the CC line; SELinux folks the original post is here:

* https://lore.kernel.org/all/[email protected]

Thanks for finding this and testing the patch, based on our off-list discussion, do you mind if I add a Suggested-by? Looking at this a bit more I think we'll want to make a few changes to selinux_lsm_getattr() later, but this patch is a good one for stable as it not only fixes the bug, but it is a trivial one-liner with very low risk.

I do think we need to tweak the commit description a bit, what do you think of the following?

"selinux_getselfattr() doesn't properly initialize the string
pointer it passes to selinux_lsm_getattr() which can cause a problem when an attribute hasn't been explicitly set; selinux_lsm_getattr() returns 0/success, but does not set or initialize the string label/attribute. Failure to properly initialize the string causes problems later in selinux_getselfattr() when the function attempts to kfree() the string."

Signature · 1 line
paul-moore.com
#15 lore raw
Quoted 13 lines · Paul Moore
Quoted 11 lines · Mickaël Salaün
Quoted 6 lines · Mickaël Salaün
Quoted 3 lines · Mickaël Salaün

selinux_lsm_getattr() may not initialize the value's pointer in some case. As for proc_pid_attr_read(), initialize this pointer to NULL in selinux_getselfattr() to avoid an UAF in the kfree() call.

Not UAF but NULL pointer dereference (both patches)...

Well, that may be the result (as observed with the kfree() call), but the cause is obviously an uninitialized pointer.

Quoted 6 lines

Adding the SELinux list to the CC line; SELinux folks the original post is here:

* https://lore.kernel.org/all/[email protected]

Thanks for finding this and testing the patch, based on our off-list discussion, do you mind if I add a Suggested-by? Looking at this a

Sure! I was in a hurry and didn't give it the attention it needed...

Quoted 8 lines

bit more I think we'll want to make a few changes to selinux_lsm_getattr() later, but this patch is a good one for stable as it not only fixes the bug, but it is a trivial one-liner with very low risk.

I do think we need to tweak the commit description a bit, what do you think of the following?

Quoted 8 lines

"selinux_getselfattr() doesn't properly initialize the string
pointer it passes to selinux_lsm_getattr() which can cause a problem when an attribute hasn't been explicitly set; selinux_lsm_getattr() returns 0/success, but does not set or initialize the string label/attribute. Failure to properly initialize the string causes problems later in selinux_getselfattr() when the function attempts to kfree() the string."

Much better!

maintainer
#16 lore raw
Quoted 40 lines · Mickaël Salaün
Quoted 13 lines · Paul Moore
Quoted 11 lines · Mickaël Salaün
Quoted 6 lines · Mickaël Salaün
Quoted 3 lines · Mickaël Salaün

selinux_lsm_getattr() may not initialize the value's pointer in some case. As for proc_pid_attr_read(), initialize this pointer to NULL in selinux_getselfattr() to avoid an UAF in the kfree() call.

Not UAF but NULL pointer dereference (both patches)...

Well, that may be the result (as observed with the kfree() call), but the cause is obviously an uninitialized pointer.

Quoted 6 lines

Adding the SELinux list to the CC line; SELinux folks the original post is here:

* https://lore.kernel.org/all/[email protected]

Thanks for finding this and testing the patch, based on our off-list discussion, do you mind if I add a Suggested-by? Looking at this a

Sure! I was in a hurry and didn't give it the attention it needed...

Quoted 8 lines

bit more I think we'll want to make a few changes to selinux_lsm_getattr() later, but this patch is a good one for stable as it not only fixes the bug, but it is a trivial one-liner with very low risk.

I do think we need to tweak the commit description a bit, what do you think of the following?

Quoted 8 lines

"selinux_getselfattr() doesn't properly initialize the string
pointer it passes to selinux_lsm_getattr() which can cause a problem when an attribute hasn't been explicitly set; selinux_lsm_getattr() returns 0/success, but does not set or initialize the string label/attribute. Failure to properly initialize the string causes problems later in selinux_getselfattr() when the function attempts to kfree() the string."

Quoted 1 line

Much better!

Great :) I just went ahead and merged this into the lsm/stable-6.8 branch to get this some testing in linux-next, although I'm going to be *shocked* if this commit causes a regression. I'll send this up to Linus early next week, and if John wants me to send the AppArmor patch I'll do that at the same time.

Signature · 1 line
paul-moore.com