Skip to contentSkip to conversation

auditfilter.c cleanup/const-ification

PATCH In review Mitchell Blank Jr 4 participants 10 messages Apr 3, 2006 – Apr 6, 2006 Audit

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 auditfilter.c cleanup/const-ification

(Not sure if this will make it to the linux-audit mailing list or not; I tried subscribing to it via the web page a couple days ago but haven't heard back from mailman)

I noticed the gcc 4.X warning:
kernel/auditfilter.c: In function "audit_filter_user" kernel/auditfilter.c:588: warning: "state" may be used uninitialized in this function
...and thought I'd take a quick look.

The gcc warning isn't correct (since audit_filter_user() only looked at state if audit_filter_user_rules() returned non-zero, in which case 'state' would have been initialized) However the code was needlessly complex -- audit_filter_user_rules() carefully populated the "enum audit_state *state" with various value but it's only caller just cares if it's AUDIT_DISABLED or not. It's shorter and simpler to just let audit_filter_user_rules() modify its caller's return value more directly. As an added bonus this also removes the warning.

While I was looking at auditfilter.c I did some other minor cleanup

* const-ified pointers where possible

* both audit_data_to_entry() and audit_krule_to_data() had an unused variable called "void *bufp" which I removed

* [minor] I changed some variables from "int" to "unsigned int" if they can't be negative. Since ->field_count is unsigned I think it's a little cleaner to use an unsigned type to iterate through it

I'm not actually using the audit subsystem currently but this at least compiles and boots ok. I believe it should all be safe.

include/linux/audit.h +3 -2
index 1c47c59..c70049e 100644
@@ -363,10 +363,11 @@ extern void		    audit_log_d_path(struct
 					     struct dentry *dentry,
 					     struct vfsmount *vfsmnt);
 				/* Private API (for audit.c only) */
-extern int audit_filter_user(struct netlink_skb_parms *cb, int type);
+extern int audit_filter_user(const struct netlink_skb_parms *cb, int type);
 extern int audit_filter_type(int type);
 extern int  audit_receive_filter(int type, int pid, int uid, int seq,
-				 void *data, size_t datasz, uid_t loginuid);
+				 const void *data, size_t datasz,
+				 uid_t loginuid);
 #else
 #define audit_log(c,g,t,f,...) do { ; } while (0)
 #define audit_log_start(c,g,t) ({ NULL; })
kernel/auditfilter.c +37 -47

215 lines not on this page · raw message

#1 lore raw

Hi Mitchell,

Thanks for looking at this.

Quoted 6 lines · Mitchell Blank Jr

The gcc warning isn't correct (since audit_filter_user() only looked at state if audit_filter_user_rules() returned non-zero, in which case 'state' would have been initialized)  However the code was needlessly complex -- audit_filter_user_rules() carefully populated the "enum audit_state *state" with various value but it's only caller just cares if it's AUDIT_DISABLED or not.

IIRC, this was done to mirror the filtering of syscalls. I think we discussed this last June/July. Anyways it was a long time ago.

Quoted 3 lines

It's shorter and simpler to just let audit_filter_user_rules() modify its caller's return value more directly.  As an added bonus this also removes the warning.

Changes to the rule matcher have to be carefully tested just in case something obscure needs it. In this case, I don't think so since its a user space originating message.

Quoted 10 lines

While I was looking at auditfilter.c I did some other minor cleanup

  * const-ified pointers where possible

  * both audit_data_to_entry() and audit_krule_to_data() had an unused variable called "void *bufp" which I removed

  * [minor] I changed some variables from "int" to "unsigned int" if they can't be negative.  Since ->field_count is unsigned I think it's a little cleaner to use an unsigned type to iterate through it

These are good cleanups. In a way, I wished this was 2 patches instead of 1. I'd take all these cleanups immediately. The other one I'd probably want to put in the test kernel for a week or two just to make sure nothing relied on the state.

Thanks,
-Steve

#2 lore raw
Quoted 1 line · Steve Grubb

These are good cleanups. In a way, I wished this was 2 patches instead of 1.

Yeah, sorry. I got a little carried away I guess. I can split them if that helps.

Quoted 3 lines

The other one I'd probably want to
put in the test kernel for a week or two just to make sure nothing relied on the state.

It's pretty clear from inspection. Look:

| static int audit_filter_user_rules(struct netlink_skb_parms *cb,
|                                    struct audit_krule *rule,
|                                    enum audit_state *state)

OK, so it's a static function... and it has only one caller, where:

| enum audit_state state;
[...]

|	if (audit_filter_user_rules(cb, &e->rule, &state)) {
|                        if (state == AUDIT_DISABLED)
|	                                ret = 0;

That's all the code that looks at "state". It's never stored anywhere; it's just lost after that test.

The only other possibility where it could be used is if audit_filter_user_rules() were relying on it to be stable each time its run but that's clearly not the case because audit_filter_user_rules() only writes to "*state"; it never reads from it.

So I'm pretty confident that this is a safe cleanup.

-Mitch

#3 lore raw
In reply to Mitchell Blank Jr

Mitchell Blank Jr wrote: [Mon Apr 03 2006, 08:51:28AM EDT]

Quoted 6 lines

While I was looking at auditfilter.c I did some other minor cleanup

* const-ified pointers where possible

* both audit_data_to_entry() and audit_krule_to_data() had an unused variable called "void *bufp" which I removed

A patch in -mm is using 'bufp', so it might not be worthwhile to remove at this point.

Thanks,
Amy

#4 lore raw
Quoted 8 lines · Amy Griffis

Mitchell Blank Jr wrote: [Mon Apr 03 2006, 08:51:28AM EDT]

Quoted 1 line

While I was looking at auditfilter.c I did some other minor cleanup

[...]

Quoted 2 lines

* both audit_data_to_entry() and audit_krule_to_data() had an unused variable called "void *bufp" which I removed

A patch in -mm is using 'bufp', so it might not be worthwhile to remove at this point.

Do you have any info on this patch? The latest -mm (17-rc1-mm1) doesn't touch auditfilter.c at all.

-Mitch

#5 lore raw
Quoted 2 lines · Mitchell Blank Jr

Do you have any info on this patch?  The latest -mm (17-rc1-mm1) doesn't touch auditfilter.c at all.

We loosely sync to -mm as our working group's patches are ready for wider testing. There is a git tree at:

git://git.kernel.org/pub/scm/linux/kernel/git/viro/audit-current.git/

This is what we work against.

-Steve

#9 lore raw
In reply to Mitchell Blank Jr
Quoted 4 lines · Mitchell Blank Jr
Quoted 3 lines · Amy Griffis

Mitchell Blank Jr wrote: [Mon Apr 03 2006, 08:51:28AM EDT]

Quoted 1 line

While I was looking at auditfilter.c I did some other minor cleanup

[...]

Quoted 8 lines
Quoted 5 lines
Quoted 2 lines

* both audit_data_to_entry() and audit_krule_to_data() had an unused variable called "void *bufp" which I removed

A patch in -mm is using 'bufp', so it might not be worthwhile to remove at this point.

Do you have any info on this patch? The latest -mm (17-rc1-mm1) doesn't touch auditfilter.c at all.

Sorry about that, I thought it was in -mm at this point. Apparently the latest audit master branch wasn't pulled for 17-rc1-mm1, but AFAIKT it should be in the next -mm.

#6 lore raw
In reply to Steve Grubb
Quoted 4 lines · Steve Grubb

We loosely sync to -mm as our working group's patches are ready for wider testing. There is a git tree at:

git://git.kernel.org/pub/scm/linux/kernel/git/viro/audit-current.git/

OK, I cloned that. I'll try to rebase my changes against that when I get some spare moments.

I still don't see the change that Amy mentioned though: the only divergence in auditfilter.c from mainline is some added "#ifdef CONFIG_AUDIT_SYSCALL" stuff... nothing touches those "bufp" variables that I can see.

-Mitch

#7 lore raw
Quoted 6 lines · Mitchell Blank Jr
Quoted 1 line · Steve Grubb

git://git.kernel.org/pub/scm/linux/kernel/git/viro/audit-current.git/

I still don't see the change that Amy mentioned though: the only divergence in auditfilter.c from mainline is some added "#ifdef CONFIG_AUDIT_SYSCALL" stuff... nothing touches those "bufp" variables that I can see.

Ah, I see the problem I was having... the "git clone" left HEAD pointing at the "master.b2" branch. It seems that "audit.b4" is where the current action is, correct?

-Mitch

#8 lore raw
Quoted 11 lines · Mitchell Blank Jr
Quoted 6 lines · Mitchell Blank Jr
Quoted 1 line · Steve Grubb

git://git.kernel.org/pub/scm/linux/kernel/git/viro/audit-current.git/

I still don't see the change that Amy mentioned though: the only divergence in auditfilter.c from mainline is some added "#ifdef CONFIG_AUDIT_SYSCALL" stuff... nothing touches those "bufp" variables that I can see.

Ah, I see the problem I was having... the "git clone" left HEAD pointing at the "master.b2" branch. It seems that "audit.b4" is where the current action is, correct?

lspp.b5, soon to switch to lspp.b6