apparmor: Fix use-after-free in aa_audit_rule_init
Subsystems (3)
PATCH 1/1 apparmor: Fix use-after-free in aa_audit_rule_init
In the implementation of aa_audit_rule_init(), when aa_label_parse() fails the allocated memory for rule is released using aa_audit_rule_free(). But after this release the the return statement tries to access the label field of the rule which results in use-after-free. Before releaseing the rule, copy errNo and return it after releasing rule.
--- security/apparmor/audit.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)
security/apparmor/audit.c +3 -1
@@ -178,6 +178,7 @@ void aa_audit_rule_free(void *vrule)int aa_audit_rule_init (u32 field,u32 op,char *rulestr,void **vrule) {struct aa_audit_rule *rule; +int err;switch (field) {case AUDIT_SUBJ_ROLE :
@@ -197,8 +198,9 @@ int aa_audit_rule_init(u32 field, u32 op, char *rulestr, void **vrule) rule->label =aa_label_parse (&root_ns->unconfined->label, rulestr,GFP_KERNEL , true, false);if (IS_ERR (rule->label)) { + err = rule->label;aa_audit_rule_free (rule); -return PTR_ERR (rule->label); +return PTR_ERR (err); } *vrule = rule;
Signature · 1 line
2.17.1
Quoted 4 lines
… But after this release the the return statement
tries to access the label field of the rule which results in use-after-free. Before releaseing the rule, copy errNo and return it after releasing rule.
Please avoid a duplicate word and a typo in this change description.
…
Quoted 1 line
+++
b/security/apparmor/audit.c
…
security/apparmor/audit.c int aa_audit_rule_init(u32 field, u32 op, char *rulestr, void **vrule) · line 201
Quoted 5 lines
security/apparmor/audit.c +1
-0@@ -197,8 +198,9 @@ int aa_audit_rule_init(u32 field, u32 op, char *rulestr, void **vrule) rule->label =aa_label_parse (&root_ns->unconfined->label, rulestr,GFP_KERNEL , true, false);if (IS_ERR (rule->label)) { + err = rule->label;
How do you think about to define the added local variable in this if branch directly?
+ int err = rule->label;
security/apparmor/audit.c int aa_audit_rule_init(u32 field, u32 op, char *rulestr, void **vrule) · line 206
Quoted 6 lines
security/apparmor/audit.c +1
-1aa_audit_rule_free (rule); -return PTR_ERR (rule->label); +return PTR_ERR (err); } *vrule = rule;
Regards,
Markus
John Johansen
maintainerQuoted 5 lines · Markus Elfring
Quoted 4 lines
… But after this release the the return statement
tries to access the label field of the rule which results in use-after-free. Before releaseing the rule, copy errNo and return it after releasing rule.
Navid thanks for finding this, and Markus thanks for the review
Quoted 2 lines
Please avoid a duplicate word and a typo in this change description. My preference would be a v2 version of the patch with the small clean-ups
that Markus has pointed out.
If I don't see a v2 this week I can pull this one in and do the revisions myself adding a little fix-up note.
Quoted 13 lines
…
Quoted 1 line
+++
b/security/apparmor/audit.c…
security/apparmor/audit.c int aa_audit_rule_init(u32 field, u32 op, char *rulestr, void **vrule) · line 201
Quoted 5 lines
security/apparmor/audit.c +1
-0@@ -197,8 +198,9 @@ int aa_audit_rule_init(u32 field, u32 op, char *rulestr, void **vrule) rule->label =aa_label_parse (&root_ns->unconfined->label, rulestr,GFP_KERNEL , true, false);if (IS_ERR (rule->label)) { + err = rule->label;How do you think about to define the added local variable in this if branch directly?
Review
Quoted 1 line
(quoted diff) +1
-0+ int err = rule->label;
yes, since err isn't defined or in use else where this would be preferable
Quoted 11 lines
security/apparmor/audit.c int aa_audit_rule_init(u32 field, u32 op, char *rulestr, void **vrule) · line 206
Quoted 6 lines
security/apparmor/audit.c +1
-1aa_audit_rule_free (rule); -return PTR_ERR (rule->label); +return PTR_ERR (err); } *vrule = rule;Regards,
Markus
PATCH v2 1/1 apparmor: Fix use-after-free in aa_audit_rule_init
In the implementation of aa_audit_rule_init(), when aa_label_parse() fails the allocated memory for rule is released using aa_audit_rule_free(). But after this release, the return statement tries to access the label field of the rule which results in use-after-free. Before releasing the rule, copy errNo and return it after release.
---
Changes in v2:
-- Fix typo in description
-- move err definition inside the if statement.
security/apparmor/audit.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
security/apparmor/audit.c +2 -1
@@ -197,8 +197,9 @@ int aa_audit_rule_init(u32 field, u32 op, char *rulestr, void **vrule) rule->label =aa_label_parse (&root_ns->unconfined->label, rulestr,GFP_KERNEL , true, false);if (IS_ERR (rule->label)) { +int err = rule->label;aa_audit_rule_free (rule); -return PTR_ERR (rule->label); +return PTR_ERR (err); } *vrule = rule;
Signature · 1 line
2.17.1
Quoted 8 lines · Navid Emamdoost
In the implementation of
aa_audit_rule_init(), whenaa_label_parse()fails the allocated memory for rule is released usingaa_audit_rule_free(). But after this release, the return statement tries to access the label field of the rule which results in use-after-free. Before releasing the rule, copy errNo and return it after release.Fixes 52e8c38001d8 ("apparmor: Fix memory leak of rule on error exit path")
Ugh! I'm not sure what I was thinking when I authored that patch. :/
Quoted 14 lines
Signed-off-by Navid Emamdoost <[email protected]>---
Changes in v2:
-- Fix typo in description
-- move err definition inside the if statement.security/apparmor/audit.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)security/apparmor/audit.c +0
-0
security/apparmor/audit.c int aa_audit_rule_init(u32 field, u32 op, char *rulestr, void **vrule) · line 200
Quoted 4 lines
security/apparmor/audit.c +1
-0@@ -197,8 +197,9 @@ int aa_audit_rule_init(u32 field, u32 op, char *rulestr, void **vrule) rule->label =aa_label_parse (&root_ns->unconfined->label, rulestr,GFP_KERNEL , true, false);if (IS_ERR (rule->label)) { +int err = rule->label;
Since rule->label is a pointer, I'd like to see this:
int err = PTR_ERR(rule->label);
security/apparmor/audit.c int aa_audit_rule_init(u32 field, u32 op, char *rulestr, void **vrule) · line 202
Quoted 3 lines
security/apparmor/audit.c +1
-1aa_audit_rule_free (rule); -return PTR_ERR (rule->label); +return PTR_ERR (err);
This line would change to:
return err;
Tyler
Quoted 6 lines
security/apparmor/audit.c +0
-0} *vrule = rule;Signature · 1 line
2.17.1
PATCH v3 1/1 apparmor: Fix use-after-free in aa_audit_rule_init
In the implementation of aa_audit_rule_init(), when aa_label_parse() fails the allocated memory for rule is released using aa_audit_rule_free(). But after this release, the return statement tries to access the label field of the rule which results in use-after-free. Before releasing the rule, copy errNo and return it after release.
---
Changes in v3:
-- applied Tyler Hicks recommendation on err initialization.
security/apparmor/audit.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
security/apparmor/audit.c +2 -1
@@ -197,8 +197,9 @@ int aa_audit_rule_init(u32 field, u32 op, char *rulestr, void **vrule) rule->label =aa_label_parse (&root_ns->unconfined->label, rulestr,GFP_KERNEL , true, false);if (IS_ERR (rule->label)) { +int err =PTR_ERR (rule->label);aa_audit_rule_free (rule); -return PTR_ERR (rule->label); +return err; } *vrule = rule;
Signature · 1 line
2.17.1
Quoted 9 lines · Navid Emamdoost
In the implementation of
aa_audit_rule_init(), whenaa_label_parse()fails the allocated memory for rule is released usingaa_audit_rule_free(). But after this release, the return statement tries to access the label field of the rule which results in use-after-free. Before releasing the rule, copy errNo and return it after release.Fixes 52e8c38001d8 ("apparmor: Fix memory leak of rule on error exit path")Signed-off-by Navid Emamdoost <[email protected]>
Thanks!
Tyler
Quoted 25 lines
---
Changes in v3:
-- applied Tyler Hicks recommendation on err initialization.security/apparmor/audit.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)security/apparmor/audit.c +2
-1@@ -197,8 +197,9 @@ int aa_audit_rule_init(u32 field, u32 op, char *rulestr, void **vrule) rule->label =aa_label_parse (&root_ns->unconfined->label, rulestr,GFP_KERNEL , true, false);if (IS_ERR (rule->label)) { +int err =PTR_ERR (rule->label);aa_audit_rule_free (rule); -return PTR_ERR (rule->label); +return err; } *vrule = rule;Signature · 1 line
2.17.1
Quoted 36 lines · Tyler Hicks
Quoted 8 lines · Navid Emamdoost
In the implementation of
aa_audit_rule_init(), whenaa_label_parse()fails the allocated memory for rule is released usingaa_audit_rule_free(). But after this release, the return statement tries to access the label field of the rule which results in use-after-free. Before releasing the rule, copy errNo and return it after release.Fixes 52e8c38001d8 ("apparmor: Fix memory leak of rule on error exit path")Ugh! I'm not sure what I was thinking when I authored that patch. :/
Quoted 14 lines
Signed-off-by Navid Emamdoost <[email protected]>---
Changes in v2:
-- Fix typo in description
-- move err definition inside the if statement.security/apparmor/audit.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)security/apparmor/audit.c +0
-0security/apparmor/audit.c int aa_audit_rule_init(u32 field, u32 op, char *rulestr, void **vrule) · line 200
Quoted 4 lines
security/apparmor/audit.c +1
-0@@ -197,8 +197,9 @@ int aa_audit_rule_init(u32 field, u32 op, char *rulestr, void **vrule) rule->label =aa_label_parse (&root_ns->unconfined->label, rulestr,GFP_KERNEL , true, false);if (IS_ERR (rule->label)) { +int err = rule->label;Since
rule->labelis a pointer, I'd like to see this:int err =
PTR_ERR(rule->label);
Quoted 8 lines
security/apparmor/audit.c int aa_audit_rule_init(u32 field, u32 op, char *rulestr, void **vrule) · line 202
Quoted 3 lines
security/apparmor/audit.c +1
-1aa_audit_rule_free (rule); -return PTR_ERR (rule->label); +return PTR_ERR (err);This line would change to:
return err;
Tyler, I made the changes and sent v3.
Quoted 9 lines
Tyler
Quoted 6 lines
security/apparmor/audit.c +0
-1} *vrule = rule; --2.17.1
Signature · 1 line
Navid.
kbuild test robot
build botThank you for the patch! Perhaps something to improve:
Hi Navid,
Thank you for the patch! Perhaps something to improve:
[auto build test WARNING on security/next-testing]
[cannot apply to v5.4-rc4 next-20191023]
[if your patch is applied to the wrong git tree, please drop us a note to help improve the system. BTW, we also suggest to use '--base' option to specify the base tree in git format-patch, please see https://stackoverflow.com/a/37406982]
url: https://github.com/0day-ci/linux/commits/Navid-Emamdoost/apparmor-Fix-use-after-free-in-aa_audit_rule_init/20191024-123239 base: https://git.kernel.org/pub/scm/linux/kernel/git/jmorris/linux-security.git next-testing config: nds32-allyesconfig (attached as .config) compiler: nds32le-linux-gcc (GCC) 8.1.0
reproduce:
wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross chmod +x ~/bin/make.cross
# save the attached .config to linux build tree
GCC_VERSION=8.1.0 make.cross ARCH=nds32
If you fix the issue, kindly add following tag
All warnings (new ones prefixed by >>):
security/apparmor/audit.c: In function 'aa_audit_rule_init':
>> security/apparmor/audit.c:200:13: warning: initialization of 'int' from 'struct aa_label *' makes integer from pointer without a cast [-Wint-conversion]
int err = rule->label;
^~~~
security/apparmor/audit.c:202:18: warning: passing argument 1 of 'PTR_ERR' makes pointer from integer without a cast [-Wint-conversion]
return PTR_ERR(err);
^~~ In file included from include/linux/rwsem.h:18,
from include/linux/key.h:18,
from include/linux/cred.h:13,
from include/linux/sched/signal.h:10,
from include/linux/ptrace.h:7,
from include/linux/audit.h:13,
from security/apparmor/audit.c:11:
include/linux/err.h:29:61: note: expected 'const void *' but argument is of type 'int'
static inline long __must_check PTR_ERR(__force const void *ptr)
~~~~~~~~~~~~^~~
vim +200 security/apparmor/audit.c
177
178 int aa_audit_rule_init(u32 field, u32 op, char *rulestr, void **vrule)
179 {
180 struct aa_audit_rule *rule;
181
182 switch (field) {
183 case AUDIT_SUBJ_ROLE:
184 if (op != Audit_equal && op != Audit_not_equal)
185 return -EINVAL;
186 break;
187 default:
188 return -EINVAL;
189 }
190
191 rule = kzalloc(sizeof(struct aa_audit_rule), GFP_KERNEL);
192
193 if (!rule)
194 return -ENOMEM;
195
196 /* Currently rules are treated as coming from the root ns */
197 rule->label = aa_label_parse(&root_ns->unconfined->label, rulestr,
198 GFP_KERNEL, true, false);
199 if (IS_ERR(rule->label)) {
> 200 int err = rule->label;
201 aa_audit_rule_free(rule);
202 return PTR_ERR(err);
203 }
204
205 *vrule = rule;
206 return 0;
207 }
208
---
0-DAY kernel test infrastructure Open Source Technology Center
https://lists.01.org/pipermail/kbuild-all Intel Corporation
kbuild test robot
build botThank you for the patch! Perhaps something to improve:
Hi Navid,
Thank you for the patch! Perhaps something to improve:
[auto build test WARNING on security/next-testing]
[cannot apply to v5.4-rc4 next-20191023]
[if your patch is applied to the wrong git tree, please drop us a note to help improve the system. BTW, we also suggest to use '--base' option to specify the base tree in git format-patch, please see https://stackoverflow.com/a/37406982]
url: https://github.com/0day-ci/linux/commits/Navid-Emamdoost/apparmor-Fix-use-after-free-in-aa_audit_rule_init/20191024-123239 base: https://git.kernel.org/pub/scm/linux/kernel/git/jmorris/linux-security.git next-testing config: ia64-allmodconfig (attached as .config) compiler: ia64-linux-gcc (GCC) 7.4.0
reproduce:
wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross chmod +x ~/bin/make.cross
# save the attached .config to linux build tree
GCC_VERSION=7.4.0 make.cross ARCH=ia64
If you fix the issue, kindly add following tag
All warnings (new ones prefixed by >>):
security/apparmor/audit.c: In function 'aa_audit_rule_init':
>> security/apparmor/audit.c:200:13: warning: initialization makes integer from pointer without a cast [-Wint-conversion]
int err = rule->label;
^~~~
>> security/apparmor/audit.c:202:18: warning: passing argument 1 of 'PTR_ERR' makes pointer from integer without a cast [-Wint-conversion]
return PTR_ERR(err);
^~~ In file included from include/linux/rwsem.h:18:0,
from include/linux/key.h:18,
from include/linux/cred.h:13,
from include/linux/sched/signal.h:10,
from include/linux/ptrace.h:7,
from include/linux/audit.h:13,
from security/apparmor/audit.c:11:
include/linux/err.h:29:33: note: expected 'const void *' but argument is of type 'int'
static inline long __must_check PTR_ERR(__force const void *ptr)
^~~~~~~
vim +200 security/apparmor/audit.c
177
178 int aa_audit_rule_init(u32 field, u32 op, char *rulestr, void **vrule)
179 {
180 struct aa_audit_rule *rule;
181
182 switch (field) {
183 case AUDIT_SUBJ_ROLE:
184 if (op != Audit_equal && op != Audit_not_equal)
185 return -EINVAL;
186 break;
187 default:
188 return -EINVAL;
189 }
190
191 rule = kzalloc(sizeof(struct aa_audit_rule), GFP_KERNEL);
192
193 if (!rule)
194 return -ENOMEM;
195
196 /* Currently rules are treated as coming from the root ns */
197 rule->label = aa_label_parse(&root_ns->unconfined->label, rulestr,
198 GFP_KERNEL, true, false);
199 if (IS_ERR(rule->label)) {
> 200 int err = rule->label; 201 aa_audit_rule_free(rule); > 202 return PTR_ERR(err);
203 }
204
205 *vrule = rule;
206 return 0;
207 }
208
---
0-DAY kernel test infrastructure Open Source Technology Center
https://lists.01.org/pipermail/kbuild-all Intel Corporation
Quoted 7 lines · John Johansen
Quoted 5 lines · Markus Elfring
Quoted 4 lines
… But after this release the the return statement
tries to access the label field of the rule which results in use-after-free. Before releaseing the rule, copy errNo and return it after releasing rule.
Quoted 5 lines
Navid thanks for finding this, and Markus thanks for the review
Quoted 2 lines
Please avoid a duplicate word and a typo in this change description. My preference would be a v2 version of the patch with the small clean-ups
that Markus has pointed out.
John and Markus, I updated and submitted v2.
Quoted 33 lines
If I don't see a v2 this week I can pull this one in and do the revisions myself adding a little fix-up note.
Quoted 12 lines
…
Quoted 1 line
+++
b/security/apparmor/audit.c…
security/apparmor/audit.c int aa_audit_rule_init(u32 field, u32 op, char *rulestr, void **vrule) · line 201
Quoted 5 lines
security/apparmor/audit.c +1
-0@@ -197,8 +198,9 @@ int aa_audit_rule_init(u32 field, u32 op, char *rulestr, void **vrule) rule->label =aa_label_parse (&root_ns->unconfined->label, rulestr,GFP_KERNEL , true, false);if (IS_ERR (rule->label)) { + err = rule->label;How do you think about to define the added local variable in this if branch directly?
Quoted 2 lines
+ int err =
rule->label;yes, since err isn't defined or in use else where this would be preferable
Quoted 11 lines
security/apparmor/audit.c int aa_audit_rule_init(u32 field, u32 op, char *rulestr, void **vrule) · line 206
Quoted 6 lines
security/apparmor/audit.c +1
-1aa_audit_rule_free (rule); -return PTR_ERR (rule->label); +return PTR_ERR (err); } *vrule = rule;Regards,
Markus
Signature · 2 lines
Thanks, Navid.