<div dir="ltr">Thanks for the explanation.<br><br>Goal: Converting root process to non-root process by enabling required capabilities for the process.<br><br>Scope: Am trying to find out required capabilities for the process which is going to run as "non-root"<br><br>How to use Apparmor to find the capabilities specific to process:<br><br>-> As per above discussion, Apparmor does not grant the required capabilities.<br><br>-> We had thought that, from apparmor logs, we would be able to find the required capabilities.<br><br>-> To find this, we would be following the steps to find the capabilities.<br><br>Process (A) will be running in "non-root" but with all enabled capabilities and check the apparmor logs.<br>Apparmor logs will show the required capabilities. <br>Capture these CAPS and use this CAPS in the process code to convert that process root to non-root by only enabling this CAPs observed in apparmor logs.<br><br>Is above understanding correct? please confirm the steps if anything is missing. <br><br><b>General Query:</b><br><br>In any event, AppArmor will usually see capability requests after the usual DAC permissions are handled.<br><br>Can you please explain this above statement with simple example?<br><br>For example, Process (x) tries to open a file (/etc/security) which is root permission but the process (x) runs in "non-root mode.<br>Pls note, process (x) does not have permission to open this file ((/etc/security) )<br><br>open => sys_open() => kernel further code for handing the code.<br><br>sys_open() => will return permission denied error due to permission issue.<br><br>Here, capable() check won't happen. Does DAC take care of this check without using capability (CAP_DAC_READ_SEARCH)?<br><br>In such a case, trying to understand when Kernel uses capable() to check CAP_DAC_READ_SEARCH/CAP_DAC_OVERRIDE before/after DAC.<br>Can you please explain the relation between DAC, apparmor and linux capability with this context?<br><br>Thanks<br>Murali.S<br></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Tue, Aug 4, 2020 at 6:08 AM John Johansen <<a href="mailto:john.johansen@canonical.com">john.johansen@canonical.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 8/3/20 8:02 PM, Murali Selvaraj wrote:<br>
> <br>
> Hi Seth,<br>
> <br>
> Thanks for the detailed explanation. Please go through below details and clarify further queries.<br>
> <br>
> I do not see a capability difference when this script runs in root (UID:0) and nobody (UID>0).<br>
> If we are observing the required capabilities when the script runs in root, that would be easy for us to find<br>
> the needed capabilities for this script. Then we will apply this capability when it runs in nobody user.<br>
> <br>
> #!/bin/sh<br>
> echo "Testing"<br>
> while [ 1 ]<br>
> do<br>
> cat /etc/foo =================> Ensure this file belongs to root permission<br>
> echo "TESTING" > /nvram/foo<br>
> killall <root_process_name><br>
> sleep 5<br>
> done<br>
> <br>
> ls -ltr /etc/foo<br>
> -rw-r--r-- 1 root root 8 Aug 3 20:31 /etc/foo<br>
> <br>
<br>
can you please add<br>
<br>
echo -n "Confinement:"<br>
cat /proc/self/attr/current || echo "Failed to obtain confinement" ; exit 1<br>
<br>
to your script after the killall or something similar, this will dump the confinement of the "cat" command but unless you have a transition for "cat" it should have the confinement of it parent or be denied.<br>
<br>
I should note that apparmor does have a dedup cache around logging capabilities. It is a single entry per processor (or virtual processor), and will prevent a previously seen cap from being logged IFF no other PROFILE has mediated a cap on that processor since the last time that profile previously logged the CAP in question.<br>
<br>
There is no manual switch to clear the cache, but it can be effectively cleared by replacing the profile but you need to actually make a change to the profile so that profile load dedup doesn't drop the replacement.<br>
<br>
<br>
> <br>
> Can you please check this script in your environment and share your observation. Please do the needful.<br>
> Please execute in root and non-root mode and find the capability list from apparmor log events.<br>
> <br>
<br>
not exactly your script but roughly equivalent<br>
<br>
unconfined non-root user killing root process<br>
<br>
$ kill 23579<br>
bash: kill: (23579) - Operation not permitted<br>
<br>
no apparmor log message.<br>
<br>
----<br>
<br>
unconfined root user killing root process<br>
<br>
$ sudo kill 23579<br>
<br>
success, no apparmor log message<br>
<br>
----<br>
<br>
confined non-root user without signal or CAP permissions killing root process<br>
<br>
$ aa-exec -p demo -- kill 23965<br>
kill: (23965): Operation not permitted<br>
<br>
no apparmor log message<br>
<br>
----<br>
<br>
confined root user without signal or CAP permissions killing root process<br>
<br>
$ sudo aa-exec -p demo -- kill 23965<br>
kill: (23965): Permission denied<br>
<br>
apparmor log messages<br>
<br>
[987021.379719] audit: type=1400 audit(1596533293.878:234): apparmor="DENIED" operation="signal" profile="demo" pid=24036 comm="kill" requested_mask="send" denied_mask="send" signal=term peer="/usr/bin/man"<br>
[987021.379727] audit: type=1400 audit(1596533293.878:235): apparmor="DENIED" operation="signal" profile="/usr/bin/man" pid=24036 comm="kill" requested_mask="receive" denied_mask="receive" signal=term peer="demo"<br>
<br>
notice no capabilities are needed to send the signal because its being sent from root to a root process<br>
<br>
----<br>
<br>
confined root user without signal or CAP permission kill a non-root process (different uids)<br>
<br>
$ sudo aa-exec -p demo -- kill 24690<br>
kill: (24690): Operation not permitted<br>
<br>
apparmor log message<br>
<br>
[989073.431936] audit: type=1400 audit(1596535345.981:238): apparmor="DENIED" operation="capable" profile="demo" pid=24717 comm="kill" capability=5 capname="kill"<br>
<br>
finally we get a CAP request for kill<br>
<br>
<br>
The reason for this is that the kernel applies DAC mediation before LSM (apparmor) mediation. AppArmor never sees the permission request unless DAC allow the operation.<br>
<br>
> *Need further clarifications:*<br>
> <br>
> My aim is to identify the required capabilities for the process when it runs in "non-root" user.<br>
> Currently, this process runs in root mode, so by default all CAPs are enabled in Effective/Permitted CAPs.<br>
> <br>
> Analysis:<br>
> <br>
> -> While the process runs in non-root mode, we are planning to apply the capabilities before switching to non-root from root.<br>
> So, we need to set appropriate capabilities in order to run the application successfully in "non-root".<br>
> <br>
yes<br>
<br>
> -> As per my assumption, we will find the required capabilities when the process runs in root-mode. To find the required CAPs list<br>
> we thought to use "apparmor" logs while the process runs in compliant mode.<br>
<br>
this doesn't quite work but you will be able to collect capabilities that don't rely on a uid check. This is because of how the kernel doesn't always apply a capability check, Eg. for kill CAPS are not always checked in the same way when subject uid == object uid vs. subject uid != object uid. The same can be said for DAC_OVERRIDE, DAC_READSEARCH, ...<br>
<br>
Some capabilities however aren't guarded by uid checks and you should be able to collect these caps when run as root. What you need to do to collect the full list of capabilities is give the non-root process all capabilities and run it. This should collect the full set of CAPs with how you are using uids.<br>
<br>
<br>
> Once we get the CAPs list from Apparmor logs, then we shall use the same required CAPS only ( NOT all the CAPs) for the<br>
> process when it runs in non-root mode.<br>
> <br>
> -> The idea is that we are trying to drop root privilege using this method.<br>
> <br>
> Can you please confirm , the above method is possible in apparmor event logs to find the required CAPs at least when run<br>
> in "root" mode.<br>
> <br>
<br>
close but see above, also you need to make sure exercise the application to get proper coverage<br>
<br>
> Thanks<br>
> Murali.S<br>
> <br>
> On Mon, Aug 3, 2020 at 8:21 PM Seth Arnold <<a href="mailto:seth.arnold@canonical.com" target="_blank">seth.arnold@canonical.com</a> <mailto:<a href="mailto:seth.arnold@canonical.com" target="_blank">seth.arnold@canonical.com</a>>> wrote:<br>
> <br>
> Hello Murali,<br>
> <br>
> On Mon, Aug 03, 2020 at 02:03:38PM -0400, Murali Selvaraj wrote:<br>
> > Query 1:<br>
> ><br>
> > - But I do not see CAP_DAC_OVERRIDE and CAP_KILL in apparmor event logs.<br>
> <br>
> AppArmor does not have a mechanism to grant capabilities that a process<br>
> does not already have. The kernel will query LSMs to see if a capability<br>
> is allowed to be used if the process already has the capability in<br>
> question. (There may be exceptions to this, there's hundreds of these<br>
> checks scattered throughout the kernel.)<br>
> <br>
> You'll only see these requests in AppArmor logs if the process had these<br>
> capabilities. By using su to switch to the 'nobody' account, you only have<br>
> access to whatever privileges the nobody account already has: additional<br>
> access to root_squashed files on NFS, any other processes mistakenly<br>
> running as user nobody, etc.<br>
> <br>
> Because this doesn't include any capabilities in the process's<br>
> capabilities sets, AppArmor won't even see the requests.<br>
> <br>
> > Query 2:<br>
> ><br>
> > - How apparmor identities how many capabilities are needed for the process?<br>
> <br>
> The kernel will call capable() in the codepaths as necessary; the security<br>
> module interface will get the calls, without context, after the rest of<br>
> the kernel's capabilities handling. It's difficult to follow because it's<br>
> been heavily optimized for performance.<br>
> <br>
> In any event, AppArmor will usually see capability requests after the<br>
> usual DAC permissions are handled.<br>
> <br>
> <br>
> > Query 3:<br>
> ><br>
> > - Does all system calls need capability when it runs in a non-root process,<br>
> > how does apparmor mapping the linux capabilities?<br>
> <br>
> "root processes" means both uid 0 as well as having capabilities in the<br>
> effective capability set. (Perhaps it'd also make sense to consider the<br>
> other capability sets in the process?) A uid 0 process with no<br>
> capabilities still has considerable power, since many important files like<br>
> /etc/sudoers are owned by uid 0, and modifying these files through DAC<br>
> permissions alone could be used to then gain capabilities. However, a uid<br>
> 0 process with no capabilities couldn't itself initiate a reboot in the<br>
> kernel, or override DAC restrictions on files, etc.<br>
> <br>
> A process with capabilities need not be uid 0 though I can't immediately<br>
> point any common examples.<br>
> <br>
> Linux's uid namespaces makes things a bit more complicated: a process can<br>
> have namespace-level capabilities that do not extend to capabilities in<br>
> the init namespace. (Consider something like an LXD guest: there may be<br>
> different users within the guest, and the 'root' user with the LXD guest<br>
> can do privileged operations over the namespace, eg manage the routing<br>
> table used for that network namespace, but not be able to manage the<br>
> routing table used by the network namespace for the init process.)<br>
> <br>
> See the user_namespaces(7) and unshare(2) manpages for some more information.<br>
> <br>
> > Can someone please clarify these queries?<br>
> <br>
> I'm afraid my description probably made things worse.<br>
> <br>
> Let me try one quick simple thing:<br>
> <br>
> Run your example with and without root privileges. You'll see the<br>
> difference in AppArmor log output. Hopefully that helps. :)<br>
> <br>
> Thanks<br>
> <br>
> <br>
<br>
</blockquote></div>