<div dir="auto">Also, we would need to have a separate rule for open and openat family with something like</div><div dir="auto"><br></div><div dir="auto">-S open,openat -F dir=/etc -F perm=wa -k ETC_WATCH.</div><div dir="auto"><br></div><div dir="auto">So 2 syscall rules instead of 1 watch rule but we replace -S all with specific syscalls.</div><div dir="auto"><br></div><div dir="auto">Or we could combine all these open,openat,write family syscalls into a single syscall rule but with the permissions flag.</div><div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Wed, Mar 15, 2023 at 3:29 PM Amjad Gabbar <<a href="mailto:amjadgabbar11@gmail.com">amjadgabbar11@gmail.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr">Yup....I was able to find the dummy check you are referring to and the audit_reset_context() that is called next(which immediately exits in case of dummy).<div>Thanks for the help folks....have a much better understanding of how the audit context is allocated on enabling syscall auditing and the whole flow post that.</div><div><br></div><div>Had just 1 question wrt watches. IIUC, for watch rules we evaluate all syscalls (Snippet from audit-userspace: <a href="https://github.com/linux-audit/audit-userspace/blob/1482cec74f2d9472f81dd4f0533484bd0c26decd/lib/libaudit.c#L805" target="_blank">https://github.com/linux-audit/audit-userspace/blob/1482cec74f2d9472f81dd4f0533484bd0c26decd/lib/libaudit.c#L805</a>).</div><div>But based on the permissions in the rule, we evaluate if the syscall belongs to a specific Audit Class using audit_match_class() and only log if the syscalls match/ are part of the class. This also explains why I see audit_filter_rules() called for watches even if the syscall being performed is not at all related to file system auditing.</div><div><br></div><div><ol><li>I was wondering why do we not automatically identify if the syscall is of interest or not in audit_in_mask() itself based on the rule permissions of the watch? In this way we would avoid the additional overhead of each syscall going into audit_filter_rules() and then evaluating on the AUDIT_PERM case as well.</li></ol></div><div><br></div><div>Currently a watch rule for "wa" permissions for /etc is similar to :</div><div>-a always,exit -F arch=b64 -S all -F dir=/etc -F perm=wa -k ETC_WATCH</div><div><br></div><div>We only log if the syscall is part of the WRITE and ATTR permissions set. Instead what I was suggesting was something like this:<br><br></div><div>-a always,exit -F arch=b64 -S <all syscalls part of the write and attr classes> -F dir=/etc  -k ETC_WATCH</div><div><br></div><div>Please correct me if my understanding in any of the above is incorrect.</div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Fri, Mar 10, 2023 at 3:54 PM Richard Guy Briggs <<a href="mailto:rgb@redhat.com" target="_blank">rgb@redhat.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">On 2023-02-17 16:50, Steve Grubb wrote:<br>
> Hello,<br>
> <br>
> On Tuesday, February 14, 2023 3:55:58 PM EST Amjad Gabbar wrote:<br>
> > Thanks for the reply.<br>
> > I was trying to evaluate the same via Flamegraphs and what I noticed was<br>
> > that :<br>
> > <br>
> > 1. Despite deleting all rules (auditctl -D), there were still calls to<br>
> > audit_filter_syscall() on each syscall. I assume this is because syscall<br>
> > auditing is enabled and despite no rules, there still will be some<br>
> > performance impact and calls to syscall filtering functions on each<br>
> > syscall.<br>
> <br>
> Yes.<br>
> <br>
> > 2. For a single watch rule as well without any syscall rules, I could see<br>
> > calls to audit_filter_syscall() followed by audit_filter_rules() for<br>
> > unrelated syscalls such as futex() and recvmsg() - not present in<br>
> > include/asm-generic/audit_*.h<br>
> > Why would these functions be called for a single watch rule for syscalls<br>
> > unrelated to the permissions?<br>
> <br>
> If auditing is enabled, it will go into the syscall filter for *any* syscall. <br>
> It will go into __audit_syscall_exit for every syscall. If there is an audit <br>
> context, it will go into audit_filter_syscall. The documentation in the <br>
> comments above these functions is informative.<br>
> <br>
> My guess is that this code path might benefit from adding a list_empty check. <br>
> A long time ago, I think we kept a variable that denoted if there were any <br>
> rules and short-circuited if none.<br>
<br>
There is essentially an empty list check in __audit_syscall_exit() with<br>
the dummy check, based on the number of syscall (or io_uring) rules in<br>
place tracked in audit_n_rules.  Unfortunately, we can't bail from<br>
__audit_syscall_entry() right after setting dummy because other<br>
hardwired records can cancel the dummy flag.<br>
<br>
> -Steve<br>
> <br>
> > On Tue, Feb 14, 2023 at 8:29 AM Steve Grubb <<a href="mailto:sgrubb@redhat.com" target="_blank">sgrubb@redhat.com</a>> wrote:<br>
> > > Hello,<br>
> > > <br>
> > > On Monday, February 13, 2023 4:24:02 PM EST Amjad Gabbar wrote:<br>
> > > > I wanted some help in better understanding the workflow of file system<br>
> > > > auditing(watch rules) vs Syscall Auditing(syscall rules). I know in<br>
> > > <br>
> > > general<br>
> > > <br>
> > > > file system auditing does not have the same performance impact as<br>
> > > > syscall<br>
> > > > auditing, even though both make use of syscall exits for their<br>
> > > <br>
> > > evaluation.<br>
> > > <br>
> > > > From the manpage - "Unlike most syscall auditing rules, watches do not<br>
> > > > impact performance based on the number of rules sent to the kernel."<br>
> > > > <br>
> > > > From a previous thread, I found this excerpt regarding file watch rules<br>
> > > <br>
> > > vs<br>
> > > <br>
> > > > sycall rules -<br>
> > > > <br>
> > > > "The reason it doesn't have performance impact like normal syscall<br>
> > > > rules<br>
> > > <br>
> > > is<br>
> > > <br>
> > > > because it gets moved to a list that is not evaluated every syscall. A<br>
> > > > normal syscall rule will get evaluated for every syscall because it has<br>
> > > <br>
> > > to<br>
> > > <br>
> > > > see if the syscall number is of interest and then it checks the next<br>
> > > > rule."<br>
> > > > <br>
> > > > Based on this I had a couple of questions:<br>
> > > > <br>
> > > > For normal syscall rules, the evaluation happens as<br>
> > > > __audit_syscall_exit<br>
> > > > <<a href="https://elixir.bootlin.com/linux/v6.1.10/C/ident/__audit_syscall_exit" rel="noreferrer" target="_blank">https://elixir.bootlin.com/linux/v6.1.10/C/ident/__audit_syscall_exit</a>><br>
> > > > calls audit_filter_syscall<br>
> > > > (<a href="https://elixir.bootlin.com/linux/v6.1.10/source/kernel/auditsc.c#L841" rel="noreferrer" target="_blank">https://elixir.bootlin.com/linux/v6.1.10/source/kernel/auditsc.c#L841</a>)<br>
> > > > <br>
> > > > Here, we check if the syscall is of interest or not in the<br>
> > > > audit_in_mask<br>
> > > > <<a href="https://elixir.bootlin.com/linux/v6.1.10/C/ident/audit_in_mask" rel="noreferrer" target="_blank">https://elixir.bootlin.com/linux/v6.1.10/C/ident/audit_in_mask</a>><br>
> > > <br>
> > > function.<br>
> > > <br>
> > > > Only if the syscall is of interest do we proceed with examining the<br>
> > > > task<br>
> > > > and return on the first rule match.<br>
> > > > <br>
> > > > 1. What is the process or code path for watch rules?<br>
> > > > audit_filter_syscall<br>
> > > > <<a href="https://elixir.bootlin.com/linux/v6.1.10/C/ident/audit_filter_syscall" rel="noreferrer" target="_blank">https://elixir.bootlin.com/linux/v6.1.10/C/ident/audit_filter_syscall</a>><br>
> > > <br>
> > > is<br>
> > > <br>
> > > > called for watch rules as well. Then how is it that these are not<br>
> > > > called<br>
> > > > for every syscall? Could you point me to the code where the evaluation<br>
> > > > happens only once?<br>
> > > <br>
> > > There is a file, kernel/audit_watch.c, that implements the interface<br>
> > > between<br>
> > > audit and fsnotify. You would want to learn how fsnotify works to<br>
> > > understand<br>
> > > how it avoids the syscall filter.<br>
> > > <br>
> > > > 2. Also, do file watches only involve the open system call family<br>
> > > > (open,<br>
> > > > openat etc). The man page implies the same, so just wanted to confirm.<br>
> > > > <br>
> > > > I assume -w /etc -p wa is the same as -a always,exit -S open -S openat<br>
> > > > -F<br>
> > > > dir=/etc?<br>
> > > <br>
> > > It depends on the flag passed for perm as to what syscall it wants. See:<br>
> > > <br>
> > > include/asm-generic/audit_*.h<br>
> > > <br>
> > > -Steve<br>
> <br>
> <br>
> <br>
> <br>
> --<br>
> Linux-audit mailing list<br>
> <a href="mailto:Linux-audit@redhat.com" target="_blank">Linux-audit@redhat.com</a><br>
> <a href="https://listman.redhat.com/mailman/listinfo/linux-audit" rel="noreferrer" target="_blank">https://listman.redhat.com/mailman/listinfo/linux-audit</a><br>
> <br>
<br>
- RGB<br>
<br>
--<br>
Richard Guy Briggs <<a href="mailto:rgb@redhat.com" target="_blank">rgb@redhat.com</a>><br>
Sr. S/W Engineer, Kernel Security, Base Operating Systems<br>
Remote, Ottawa, Red Hat Canada<br>
IRC: rgb, SunRaycer<br>
Voice: +1.647.777.2635, Internal: (81) 32635<br>
<br>
</blockquote></div>
</blockquote></div></div>