Skip to contentSkip to conversation

kernel: auditfilter: looping issue, memory leak if has 2 or more AUDIT_FILTERKEYs

PATCH In review Chen Gang 2 participants 22 messages Apr 10, 2013 – Apr 17, 2013 Audit

lore
Share & tools
lore

Chen Gang

author
#0 lore raw

PATCH 1/1 kernel: auditfilter: looping issue, memory leak if has 2 or more AUDIT_FILTERKEYs

in the 'fcount' looping,

    if 'new->fields[*].type" has 2 or more AUDIT_FILTERKEYs
    need judge new->filterkey whether has value, or memory leak.
---
 kernel/auditfilter.c |    2 ++
 1 files changed, 2 insertions(+), 0 deletions(-)
kernel/auditfilter.c +2 -0
index f9fc54b..936ac79 100644
@@ -859,6 +859,8 @@ struct audit_entry *audit_dupe_rule(struct audit_krule *old)
 						       &old->fields[i]);
 			break;
 		case AUDIT_FILTERKEY:
+			if (new->filterkey)
+				break;
 			fk = kstrdup(old->filterkey, GFP_KERNEL);
 			if (unlikely(!fk))
 				err = -ENOMEM;
Signature · 1 line
1.7.7.6
author
#1 lore raw

in another function: audit_data_to_entry:

a. has the same issue for case AUDIT_WATCH.

b. has an new issue for AUDIT_DIR:

       after AUDIT_DIR succeed, it will set rule->tree.
       next, the other case fail, then will call audit_free_rule.
       but audit_free_rule will not free rule->tree.

I find them only by reading code, not test them. and I also do not know about the related features. so please help check my 2 opinions whether are correct.

welcome any suggestion or completions.

thanks.

:-)

gchen.

Quoted 24 lines · Chen Gang

in the 'fcount' looping,

    if 'new->fields[*].type" has 2 or more AUDIT_FILTERKEYs
    need judge new->filterkey whether has value, or memory leak.
---
 kernel/auditfilter.c |    2 ++
 1 files changed, 2 insertions(+), 0 deletions(-)
kernel/auditfilter.c +2 -0
index f9fc54b..936ac79 100644
@@ -859,6 +859,8 @@ struct audit_entry *audit_dupe_rule(struct audit_krule *old)
 						       &old->fields[i]);
 			break;
 		case AUDIT_FILTERKEY:
+			if (new->filterkey)
+				break;
 			fk = kstrdup(old->filterkey, GFP_KERNEL);
 			if (unlikely(!fk))
 				err = -ENOMEM;
Signature · 3 lines
Chen Gang

Asianux Corporation
author
#2 lore raw

also for function audit_list:
when call audit_make_reply fails (will return NULL).
we need free all its related variables instead of only kfree rull.
(such as call autit_free_rule)

please help check, thanks.

:-)

gchen.

Quoted 54 lines · Chen Gang

in another function: audit_data_to_entry:

a. has the same issue for case AUDIT_WATCH.

b. has an new issue for AUDIT_DIR:

       after AUDIT_DIR succeed, it will set rule->tree.
       next, the other case fail, then will call audit_free_rule.
       but audit_free_rule will not free rule->tree.

I find them only by reading code, not test them. and I also do not know about the related features. so please help check my 2 opinions whether are correct.

welcome any suggestion or completions.

thanks.

:-)

gchen.

Quoted 24 lines · Chen Gang

in the 'fcount' looping,

    if 'new->fields[*].type" has 2 or more AUDIT_FILTERKEYs
    need judge new->filterkey whether has value, or memory leak.
---
 kernel/auditfilter.c |    2 ++
 1 files changed, 2 insertions(+), 0 deletions(-)
kernel/auditfilter.c +2 -0
index f9fc54b..936ac79 100644
@@ -859,6 +859,8 @@ struct audit_entry *audit_dupe_rule(struct audit_krule *old)
 						       &old->fields[i]);
 			break;
 		case AUDIT_FILTERKEY:
+			if (new->filterkey)
+				break;
 			fk = kstrdup(old->filterkey, GFP_KERNEL);
 			if (unlikely(!fk))
 				err = -ENOMEM;
Signature · 3 lines
Chen Gang

Asianux Corporation
author
#3 lore raw

also for function audit_list_rules:
when call audit_make_reply fails (will return NULL). we also need process data->buf, not only data itself.

please help check, thanks.

:-)

gchen.

Quoted 69 lines · Chen Gang

also for function audit_list:
when call audit_make_reply fails (will return NULL).
we need free all its related variables instead of only kfree rull.
(such as call autit_free_rule)

please help check, thanks.

:-)

gchen.

Quoted 54 lines · Chen Gang

in another function: audit_data_to_entry:

a. has the same issue for case AUDIT_WATCH.

b. has an new issue for AUDIT_DIR:

       after AUDIT_DIR succeed, it will set rule->tree.
       next, the other case fail, then will call audit_free_rule.
       but audit_free_rule will not free rule->tree.

I find them only by reading code, not test them. and I also do not know about the related features. so please help check my 2 opinions whether are correct.

welcome any suggestion or completions.

thanks.

:-)

gchen.

Quoted 24 lines · Chen Gang

in the 'fcount' looping,

    if 'new->fields[*].type" has 2 or more AUDIT_FILTERKEYs
    need judge new->filterkey whether has value, or memory leak.
---
 kernel/auditfilter.c |    2 ++
 1 files changed, 2 insertions(+), 0 deletions(-)
kernel/auditfilter.c +2 -0
index f9fc54b..936ac79 100644
@@ -859,6 +859,8 @@ struct audit_entry *audit_dupe_rule(struct audit_krule *old)
 						       &old->fields[i]);
 			break;
 		case AUDIT_FILTERKEY:
+			if (new->filterkey)
+				break;
 			fk = kstrdup(old->filterkey, GFP_KERNEL);
 			if (unlikely(!fk))
 				err = -ENOMEM;
Signature · 3 lines
Chen Gang

Asianux Corporation
maintainer
#20 lore raw
In reply to Chen Gang

We only allow one filter key per rule. So we should never be able to get into this situation. See audit_data_to_entry()

-Eric

----- Original Message -----

Quoted 27 lines

in the 'fcount' looping,

    if 'new->fields[*].type" has 2 or more AUDIT_FILTERKEYs
    need judge new->filterkey whether has value, or memory leak.
---
 kernel/auditfilter.c |    2 ++
 1 files changed, 2 insertions(+), 0 deletions(-)
kernel/auditfilter.c +2 -0
index f9fc54b..936ac79 100644
@@ -859,6 +859,8 @@ struct audit_entry *audit_dupe_rule(struct audit_krule
 *old)
 						       &old->fields[i]);
 			break;
 		case AUDIT_FILTERKEY:
+			if (new->filterkey)
+				break;
 			fk = kstrdup(old->filterkey, GFP_KERNEL);
 			if (unlikely(!fk))

err = -ENOMEM;
--
1.7.7.6

maintainer
#8 lore raw
In reply to Chen Gang

----- Original Message -----

Quoted 5 lines

in another function: audit_data_to_entry:

a. has the same issue for case AUDIT_WATCH.

You are saying if there were 2 of them it will leak the old one? No. If you have 2 AUDIT_WATCH entries the first one will set entry->rule->watch and the second will bomb with EINVAL in audit_to_watch()

maintainer
#10 lore raw
In reply to Chen Gang

----- Original Message -----

Quoted 4 lines

b. has an new issue for AUDIT_DIR:

       after AUDIT_DIR succeed, it will set rule->tree.
       next, the other case fail, then will call audit_free_rule.
       but audit_free_rule will not free rule->tree.

Definitely a couple of leaks here...

I'm seeing leaks on size 8, 64, and 128.

Al, what do you think? Should I be calling audit_put_tree() in the error case if entry->tree != NULL? The audit trees are some of the most complex code in the kernel I think.

maintainer
#6 lore raw
In reply to Chen Gang

----- Original Message -----

Quoted 7 lines

also for function audit_list:
when call audit_make_reply fails (will return NULL).
we need free all its related variables instead of only kfree rull.
(such as call autit_free_rule)

please help check, thanks.

audit_free_rule() takes a struct audit_entry, not an audit_rule. struct audit_rule does not have additional things which need to be freed...

maintainer
#4 lore raw
In reply to Chen Gang

----- Original Message -----

Quoted 6 lines

also for function audit_list_rules:
when call audit_make_reply fails (will return NULL). we also need process data->buf, not only data itself.

please help check, thanks.

struct audit_rule_data {
[...]
char buf[0]; /* string fields buffer */
};

The last element in the struct is 0 length. But the allocation in audit_krule_to_data() looks like:

data = kmalloc(sizeof(*data) + krule->buflen, GFP_KERNEL);

So now data->buf appears as an allocation of size krule->buflen.

We do not need to free it separately. This is a pretty common C trick.

-Eric

author
#5 lore raw
Quoted 12 lines · Eric Paris

----- Original Message -----

Quoted 6 lines
Quoted 6 lines

also for function audit_list_rules:
when call audit_make_reply fails (will return NULL). we also need process data->buf, not only data itself.

please help check, thanks.

struct audit_rule_data {
[...]
char buf[0]; /* string fields buffer */
};

Quoted 7 lines

The last element in the struct is 0 length. But the allocation in audit_krule_to_data() looks like:

data = kmalloc(sizeof(*data) + krule->buflen, GFP_KERNEL);

So now data->buf appears as an allocation of size krule->buflen.

We do not need to free it separately. This is a pretty common C trick.

ok, thanks

it is my fault.

:-)

Signature · 3 lines
Chen Gang

Asianux Corporation
author
#7 lore raw
In reply to Eric Paris
Quoted 9 lines · Eric Paris

----- Original Message -----

Quoted 7 lines
Quoted 7 lines

also for function audit_list:
when call audit_make_reply fails (will return NULL).
we need free all its related variables instead of only kfree rull.
(such as call autit_free_rule)

please help check, thanks.

audit_free_rule() takes a struct audit_entry, not an audit_rule.

yes. but maybe you misunderstand what I said (excuse me, my English is
not quite weill)
I said: "need we process the rule just like audit_free_rule has done ?"

Quoted 3 lines

struct audit_rule does not have additional things which need to be freed...

oh, it is my fault:
(I did not notice: rule is struct audit_rule, not struct audit_krule).

thanks.

:-)

Signature · 3 lines
Chen Gang

Asianux Corporation
author
#9 lore raw
In reply to Eric Paris
Quoted 9 lines · Eric Paris

----- Original Message -----

Quoted 5 lines
Quoted 5 lines

in another function: audit_data_to_entry:

a. has the same issue for case AUDIT_WATCH.

You are saying if there were 2 of them it will leak the old one? No. If you have 2 AUDIT_WATCH entries the first one will set entry->rule->watch and the second will bomb with EINVAL in audit_to_watch()

thanks, really it is. it is my fault.

:-)

Signature · 3 lines
Chen Gang

Asianux Corporation
author
#21 lore raw
In reply to Eric Paris
Quoted 1 line · Eric Paris

We only allow one filter key per rule. So we should never be able to get into this situation. See audit_data_to_entry()

really it is, thanks.

:-)

Signature · 3 lines
Chen Gang

Asianux Corporation
author
#11 lore raw
In reply to Eric Paris
Quoted 6 lines · Eric Paris

----- Original Message -----

Quoted 4 lines
Quoted 4 lines

b. has an new issue for AUDIT_DIR:

       after AUDIT_DIR succeed, it will set rule->tree.
       next, the other case fail, then will call audit_free_rule.
       but audit_free_rule will not free rule->tree.
Quoted 7 lines

Definitely a couple of leaks here...

I'm seeing leaks on size 8, 64, and 128.

Al, what do you think? Should I be calling audit_put_tree() in the error case if entry->tree != NULL? The audit trees are some of the most complex code in the kernel I think.

can we add it in audit_free_rule ?

maybe like this:

@@ -75,6 +75,8 @@ static inline void audit_free_rule(struct audit_entry *e)
/* some rules don't have associated watches */
if (erule->watch)
audit_put_watch(erule->watch);
+ if (erule->tree)
+ audit_put_tree(erule->tree);
if (erule->fields)
for (i = 0; i < erule->field_count; i++) {
struct audit_field *f = &erule->fields[i];

thanks.

:-)

Signature · 3 lines
Chen Gang

Asianux Corporation
maintainer
#12 lore raw

----- Original Message -----

Quoted 22 lines · Eric Paris
Quoted 6 lines

----- Original Message -----

Quoted 4 lines
Quoted 4 lines

b. has an new issue for AUDIT_DIR:

       after AUDIT_DIR succeed, it will set rule->tree.
       next, the other case fail, then will call audit_free_rule.
       but audit_free_rule will not free rule->tree.
Quoted 9 lines

Definitely a couple of leaks here...

I'm seeing leaks on size 8, 64, and 128.

Al, what do you think? Should I be calling audit_put_tree() in the error case if entry->tree != NULL? The audit trees are some of the most complex code in the kernel I think.

can we add it in audit_free_rule ?

maybe like this:

Review static inline void audit_free_rule(struct audit_entry *e) · line 82

Quoted 8 lines
(quoted diff) +2 -0
@@ -75,6 +75,8 @@ static inline void audit_free_rule(struct audit_entry *e)
 	/* some rules don't have associated watches */
 	if (erule->watch)
 		audit_put_watch(erule->watch);
+	if (erule->tree)
+		audit_put_tree(erule->tree);
 	if (erule->fields)
 		for (i = 0; i < erule->field_count; i++) {
 			struct audit_field *f = &erule->fields[i];

Where does the tree information get freed normally? That's the code you need to run down. You don't want to start getting double frees on the non-error case. I'll try to dig into it if Al doesn't. It's easy to show the leak on current kernels.

while(1)
auditctl -a exit,always -w /etc -F auid=-1

author
#13 lore raw
Quoted 4 lines · Eric Paris
Quoted 4 lines
Quoted 4 lines

can we add it in audit_free_rule ?

maybe like this:

Quoted 11 lines
Quoted 9 lines

Review static inline void audit_free_rule(struct audit_entry *e) · line 82

Quoted 9 lines
(quoted diff) +2 -0
@@ -75,6 +75,8 @@ static inline void audit_free_rule(struct audit_entry *e)
 	/* some rules don't have associated watches */
 	if (erule->watch)
 		audit_put_watch(erule->watch);
+	if (erule->tree)
+		audit_put_tree(erule->tree);
 	if (erule->fields)
 		for (i = 0; i < erule->field_count; i++) {
 			struct audit_field *f = &erule->fields[i];

Where does the tree information get freed normally? That's the code you need to run down. You don't want to start getting double frees on the non-error case. I'll try to dig into it if Al doesn't. It's easy to show the leak on current kernels.

I think:
it is in function audit_del_rule. when del, also set NULL. so the deletion in audit_free_rule is safe.
the process of erule->watch and erule->tree are similar.

please check, thanks.

Quoted 5 lines

while(1)
auditctl -a exit,always -w /etc -F auid=-1

it is valuable to me, thanks.

Signature · 3 lines
Chen Gang

Asianux Corporation
author
#14 lore raw
Quoted 5 lines · Chen Gang
Quoted 4 lines · Eric Paris
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines

can we add it in audit_free_rule ?

maybe like this:

Quoted 11 lines
Quoted 11 lines
Quoted 11 lines
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines

Review static inline void audit_free_rule(struct audit_entry *e) · line 82

Quoted 9 lines
(quoted diff) +2 -0
@@ -75,6 +75,8 @@ static inline void audit_free_rule(struct audit_entry *e)
 	/* some rules don't have associated watches */
 	if (erule->watch)
 		audit_put_watch(erule->watch);
+	if (erule->tree)
+		audit_put_tree(erule->tree);
 	if (erule->fields)
 		for (i = 0; i < erule->field_count; i++) {
 			struct audit_field *f = &erule->fields[i];

Where does the tree information get freed normally? That's the code you need to run down. You don't want to start getting double frees on the non-error case. I'll try to dig into it if Al doesn't. It's easy to show the leak on current kernels.

oh.. it seems another issues need consider !!

a. in function audit_remove_watch_rule, it does not set NULL for krule->watch.

b. function audit_del_rule and audit_remove_watch_rule need lock protected.
it seems we should call audit_del_rule in audit_free_rule.
audit_del_rule will instead of audit_put_watch and audit_put_tree.
but we need consider whether will cause dead lock !

I will continue to see it.

Quoted 15 lines

I think:
it is in function audit_del_rule. when del, also set NULL. so the deletion in audit_free_rule is safe.
the process of erule->watch and erule->tree are similar.

please check, thanks.

Quoted 5 lines
Quoted 5 lines

while(1)
auditctl -a exit,always -w /etc -F auid=-1

it is valuable to me, thanks.

Signature · 3 lines
Chen Gang

Asianux Corporation
author
#15 lore raw
In reply to Chen Gang
Quoted 7 lines · Chen Gang
Quoted 6 lines · Eric Paris

----- Original Message -----

Quoted 4 lines
Quoted 4 lines

b. has an new issue for AUDIT_DIR:

       after AUDIT_DIR succeed, it will set rule->tree.
       next, the other case fail, then will call audit_free_rule.
       but audit_free_rule will not free rule->tree.
Quoted 7 lines
Quoted 7 lines

Definitely a couple of leaks here...

I'm seeing leaks on size 8, 64, and 128.

Al, what do you think? Should I be calling audit_put_tree() in the error case if entry->tree != NULL? The audit trees are some of the most complex code in the kernel I think.

it seems, your way is the only executable way (if not change code much). what my original idea is incorrect.

we need add related code at failure process area in audit_data_to_entry. and another functions need not add these code (should not add). 'watch' also need be processed, since audit_to_watch let ref count = 2.
(it just like the function audit_del_rule has done)

please help check thanks.

:-)

kernel/auditfilter.c +4 -0
index 81f63f9..f5327ce 100644
@@ -594,6 +594,10 @@ exit_nofree:
 	return entry;
 
 exit_free:
+	if (entry->rule.watch)
+		audit_put_watch(entry->rule.watch); /* matches initial get */
+	if (entry->rule.tree)
+		audit_put_tree(entry->rule.tree); /* that's the temporary one */
 	audit_free_rule(entry);
 	return ERR_PTR(err);
 }
Quoted 20 lines

can we add it in audit_free_rule ?

maybe like this:

(quoted diff) +2 -0
@@ -75,6 +75,8 @@ static inline void audit_free_rule(struct audit_entry *e)
 	/* some rules don't have associated watches */
 	if (erule->watch)
 		audit_put_watch(erule->watch);
+	if (erule->tree)
+		audit_put_tree(erule->tree);
 	if (erule->fields)
 		for (i = 0; i < erule->field_count; i++) {
 			struct audit_field *f = &erule->fields[i];

thanks.

:-)

Signature · 3 lines
Chen Gang

Asianux Corporation
author
#16 lore raw
Quoted 8 lines · Chen Gang
Quoted 7 lines · Chen Gang
Quoted 6 lines · Eric Paris

----- Original Message -----

Quoted 4 lines
Quoted 4 lines

b. has an new issue for AUDIT_DIR:

       after AUDIT_DIR succeed, it will set rule->tree.
       next, the other case fail, then will call audit_free_rule.
       but audit_free_rule will not free rule->tree.
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines

Definitely a couple of leaks here...

I'm seeing leaks on size 8, 64, and 128.

Al, what do you think? Should I be calling audit_put_tree() in the error case if entry->tree != NULL? The audit trees are some of the most complex code in the kernel I think.

I am just testing about it with:

---
while(1)
auditctl -a exit,always -w /etc -F auid=-1
---

under fedora 17, we need modify the auditctl source code:
a. let -w /etc can pass auditctl checking.
b. let loop infinitely in a process (if process quit, will free mem) c. need fix a bug for auditctl (under Fedora 17)
audit_open may open 2 times.
when loop infinitely, it will cause resource handle leak.

I have checked (by insert printf in kernel/auditfilter.c):
after modify the auditct, the work flow is just what we want to be.
(will alloc watch, alloc tree, then failure occurs)

I guess, we need 2-3 days to get a test result.

welcome any suggestions and completions.

thanks.

Quoted 54 lines

it seems, your way is the only executable way (if not change code much). what my original idea is incorrect.

we need add related code at failure process area in audit_data_to_entry. and another functions need not add these code (should not add). 'watch' also need be processed, since audit_to_watch let ref count = 2.
(it just like the function audit_del_rule has done)

please help check thanks.

:-)

kernel/auditfilter.c +4 -0
index 81f63f9..f5327ce 100644
@@ -594,6 +594,10 @@ exit_nofree:
 	return entry;
 
 exit_free:
+	if (entry->rule.watch)
+		audit_put_watch(entry->rule.watch); /* matches initial get */
+	if (entry->rule.tree)
+		audit_put_tree(entry->rule.tree); /* that's the temporary one */
 	audit_free_rule(entry);
 	return ERR_PTR(err);
 }
Quoted 23 lines

can we add it in audit_free_rule ?

maybe like this:

(quoted diff) +2 -0
@@ -75,6 +75,8 @@ static inline void audit_free_rule(struct audit_entry *e)
 	/* some rules don't have associated watches */
 	if (erule->watch)
 		audit_put_watch(erule->watch);
+	if (erule->tree)
+		audit_put_tree(erule->tree);
 	if (erule->fields)
 		for (i = 0; i < erule->field_count; i++) {
 			struct audit_field *f = &erule->fields[i];

thanks.

:-)

Signature · 3 lines
Chen Gang

Asianux Corporation
author
#17 lore raw
Quoted 9 lines · Chen Gang
Quoted 8 lines · Chen Gang
Quoted 7 lines · Chen Gang
Quoted 6 lines · Eric Paris

----- Original Message -----

Quoted 4 lines
Quoted 4 lines

b. has an new issue for AUDIT_DIR:

       after AUDIT_DIR succeed, it will set rule->tree.
       next, the other case fail, then will call audit_free_rule.
       but audit_free_rule will not free rule->tree.
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines

Definitely a couple of leaks here...

I'm seeing leaks on size 8, 64, and 128.

Al, what do you think? Should I be calling audit_put_tree() in the error case if entry->tree != NULL? The audit trees are some of the most complex code in the kernel I think.

oh, also need buffering optarg of auditctl under fedora 17. or "-F auid=-1" will be truncated to "-F auid".
it is ok if not looping again. but in our case, we need loop again.

to see memory usage, I think:
in top, really used memory = 'used' - 'cached' it is enough for us.

welcome any suggestions or completions.

thanks.

Quoted 85 lines

I am just testing about it with:

---
while(1)
auditctl -a exit,always -w /etc -F auid=-1
---

under fedora 17, we need modify the auditctl source code:
a. let -w /etc can pass auditctl checking.
b. let loop infinitely in a process (if process quit, will free mem) c. need fix a bug for auditctl (under Fedora 17)
audit_open may open 2 times.
when loop infinitely, it will cause resource handle leak.

I have checked (by insert printf in kernel/auditfilter.c):
after modify the auditct, the work flow is just what we want to be.
(will alloc watch, alloc tree, then failure occurs)

I guess, we need 2-3 days to get a test result.

welcome any suggestions and completions.

thanks.

Quoted 54 lines

it seems, your way is the only executable way (if not change code much). what my original idea is incorrect.

we need add related code at failure process area in audit_data_to_entry. and another functions need not add these code (should not add). 'watch' also need be processed, since audit_to_watch let ref count = 2.
(it just like the function audit_del_rule has done)

please help check thanks.

:-)

kernel/auditfilter.c +4 -0
index 81f63f9..f5327ce 100644
@@ -594,6 +594,10 @@ exit_nofree:
 	return entry;
 
 exit_free:
+	if (entry->rule.watch)
+		audit_put_watch(entry->rule.watch); /* matches initial get */
+	if (entry->rule.tree)
+		audit_put_tree(entry->rule.tree); /* that's the temporary one */
 	audit_free_rule(entry);
 	return ERR_PTR(err);
 }
Quoted 23 lines

can we add it in audit_free_rule ?

maybe like this:

(quoted diff) +2 -0
@@ -75,6 +75,8 @@ static inline void audit_free_rule(struct audit_entry *e)
 	/* some rules don't have associated watches */
 	if (erule->watch)
 		audit_put_watch(erule->watch);
+	if (erule->tree)
+		audit_put_tree(erule->tree);
 	if (erule->fields)
 		for (i = 0; i < erule->field_count; i++) {
 			struct audit_field *f = &erule->fields[i];

thanks.

:-)

Signature · 3 lines
Chen Gang

Asianux Corporation
author
#18 lore raw
Quoted 10 lines · Chen Gang
Quoted 9 lines · Chen Gang
Quoted 8 lines · Chen Gang
Quoted 7 lines · Chen Gang
Quoted 6 lines · Eric Paris

----- Original Message -----

Quoted 4 lines
Quoted 4 lines

b. has an new issue for AUDIT_DIR:

       after AUDIT_DIR succeed, it will set rule->tree.
       next, the other case fail, then will call audit_free_rule.
       but audit_free_rule will not free rule->tree.
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines

Definitely a couple of leaks here...

I'm seeing leaks on size 8, 64, and 128.

Al, what do you think? Should I be calling audit_put_tree() in the error case if entry->tree != NULL? The audit trees are some of the most complex code in the kernel I think.

after the test, the original version really has memory leak.

test:

    the related monitor command is:
      watch -d -n 1 "cat /proc/meminfo | awk '{print \$2}' \
        | head -n 4 | xargs \
        | awk '{print \"used \",\$1 - \$2 - \$3 - \$4}'"
    I run 15 processes of modified auditctl at the same time.

result:
for original version:
can see the memory leak, it will be more clear after 1 - 2 hours.

for new version (fix it):
can not see the memory leak after ran 12 - 14 hours.

I will use LTP (ltp-full-20130109) to test audit again under fedora 17
x86_64 for next-20130415, then send related patch.

welcome any suggestions or completions.

Quoted 101 lines

oh, also need buffering optarg of auditctl under fedora 17. or "-F auid=-1" will be truncated to "-F auid".
it is ok if not looping again. but in our case, we need loop again.

to see memory usage, I think:
in top, really used memory = 'used' - 'cached' it is enough for us.

welcome any suggestions or completions.

thanks.

Quoted 85 lines

I am just testing about it with:

---
while(1)
auditctl -a exit,always -w /etc -F auid=-1
---

under fedora 17, we need modify the auditctl source code:
a. let -w /etc can pass auditctl checking.
b. let loop infinitely in a process (if process quit, will free mem) c. need fix a bug for auditctl (under Fedora 17)
audit_open may open 2 times.
when loop infinitely, it will cause resource handle leak.

I have checked (by insert printf in kernel/auditfilter.c):
after modify the auditct, the work flow is just what we want to be.
(will alloc watch, alloc tree, then failure occurs)

I guess, we need 2-3 days to get a test result.

welcome any suggestions and completions.

thanks.

Quoted 54 lines

it seems, your way is the only executable way (if not change code much). what my original idea is incorrect.

we need add related code at failure process area in audit_data_to_entry. and another functions need not add these code (should not add). 'watch' also need be processed, since audit_to_watch let ref count = 2.
(it just like the function audit_del_rule has done)

please help check thanks.

:-)

kernel/auditfilter.c +4 -0
index 81f63f9..f5327ce 100644
@@ -594,6 +594,10 @@ exit_nofree:
 	return entry;
 
 exit_free:
+	if (entry->rule.watch)
+		audit_put_watch(entry->rule.watch); /* matches initial get */
+	if (entry->rule.tree)
+		audit_put_tree(entry->rule.tree); /* that's the temporary one */
 	audit_free_rule(entry);
 	return ERR_PTR(err);
 }
Quoted 23 lines

can we add it in audit_free_rule ?

maybe like this:

(quoted diff) +2 -0
@@ -75,6 +75,8 @@ static inline void audit_free_rule(struct audit_entry *e)
 	/* some rules don't have associated watches */
 	if (erule->watch)
 		audit_put_watch(erule->watch);
+	if (erule->tree)
+		audit_put_tree(erule->tree);
 	if (erule->fields)
 		for (i = 0; i < erule->field_count; i++) {
 			struct audit_field *f = &erule->fields[i];

thanks.

:-)

Signature · 3 lines
Chen Gang

Asianux Corporation
author
#19 lore raw

PATCH v2 1/1 kernel: auditfilter: resource management, tree and watch will memory leak when failure occurs

in function audit_data_to_entry:
when failure occurs, need check and free tree and watch. or memory leak.

test:
plan:
test command:
"auditctl -a exit,always -w /etc -F auid=-1"
(on fedora17, need modify auditctl to let "-w /etc" has effect)
running:
under fedora17 x86_64, 2 CPUs 3.20GHz, 2.5GB RAM. let 15 auditctl processes continue running at the same time.
monitor command:
watch -d -n 1 "cat /proc/meminfo | awk '{print \$2}' \
| head -n 4 | xargs \
| awk '{print \"used \",\$1 - \$2 - \$3 - \$4}'"

result:
for original version:
will use up all memory, within 3 hours. kill all auditctl, the memory still does not free.
for new version (apply this patch):
after 14 hours later, not find issues.

---
 kernel/auditfilter.c |    4 ++++
 1 files changed, 4 insertions(+), 0 deletions(-)
kernel/auditfilter.c +4 -0
index f9fc54b..2674368 100644
@@ -594,6 +594,10 @@ exit_nofree:
 	return entry;
 
 exit_free:
+	if (entry->rule.watch)
+		audit_put_watch(entry->rule.watch); /* matches initial get */
+	if (entry->rule.tree)
+		audit_put_tree(entry->rule.tree); /* that's the temporary one */
 	audit_free_rule(entry);
 	return ERR_PTR(err);
 }
Signature · 1 line
1.7.7.6