Skip to contentSkip to conversation

BPF LSM prevent program unload

DISCUSSION Frederick Lawler 7 participants 16 messages Dec 5, 2023 – Dec 8, 2023

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

Hi,

IIUC, LSMs are supposed to give us the ability to design policy around unprivileged users and in addition to privileged users. As we expand our usage of BPF LSM's, there are cases where we want to restrict privileged users from unloading our progs. For instance, any privileged user that wants to remove restrictions we've placed on privileged users.

We currently have a loader application doesn't leverage BPF skeletons. We instead load BPF object files, and then pin the progs to a mount point that is a bpf filesystem. On next run, if we have new policies, load in new policies, and finally unload the old.

Here are some conditions a privileged user may unload programs:

umount /sys/fs/bpf
rm -rf /sys/fs/bpf/lsm
rm /sys/fs/bpf/lsm/some_prog
unlink /sys/fs/bpf/lsm/some_prog

This works because once we remove the last reference, the programs and pinned maps are cleaned up.

Moving individual pins or moving the mount entirely with mount --move do not perform any clean up operations. Lastly, bpftool doesn't currently have the ability to unload LSM's AFAIK.

The few ideas I have floating around are:

1. Leverage some LSM hooks (BPF or otherwise) to restrict on the functions security_sb_umount(), security_path_unlink(), security_inode_unlink().

Both security_path_unlink() and security_inode_unlink() handle the unlink/remove case, but not the umount case.

3. Leverage SELinux/Apparmor to possibly handle these cases.

4. Introduce a security_bpf_prog_unload() to target hopefully the umount and unlink cases at the same time.

5. Possible moonshot idea: introduce a interface to pin _specifically_ BPF LSM's to the kernel, and avoid the bpf sysfs problems all together.

We're making the assumption this problem has been thought about before, and are wondering if there's anything obvious we're missing here.

Fred

#1 lore raw
Quoted 33 lines · Frederick Lawler

Hi,

IIUC, LSMs are supposed to give us the ability to design policy around unprivileged users and in addition to privileged users. As we expand our usage of BPF LSM's, there are cases where we want to restrict privileged users from unloading our progs. For instance, any privileged user that wants to remove restrictions we've placed on privileged users.

We currently have a loader application doesn't leverage BPF skeletons. We instead load BPF object files, and then pin the progs to a mount point that is a bpf filesystem. On next run, if we have new policies, load in new policies, and finally unload the old.

Here are some conditions a privileged user may unload programs:

umount /sys/fs/bpf
rm -rf /sys/fs/bpf/lsm
rm /sys/fs/bpf/lsm/some_prog
unlink /sys/fs/bpf/lsm/some_prog

This works because once we remove the last reference, the programs and pinned maps are cleaned up.

Moving individual pins or moving the mount entirely with mount --move do not perform any clean up operations. Lastly, bpftool doesn't currently have the ability to unload LSM's AFAIK.

The few ideas I have floating around are:

1. Leverage some LSM hooks (BPF or otherwise) to restrict on the functions security_sb_umount(), security_path_unlink(), security_inode_unlink().

Quoted 8 lines

Both security_path_unlink() and security_inode_unlink() handle the unlink/remove case, but not the umount case.

3. Leverage SELinux/Apparmor to possibly handle these cases.

4. Introduce a security_bpf_prog_unload() to target hopefully the umount and unlink cases at the same time.

All the above programs can also be removed by privileged users.

Quoted 3 lines

5. Possible moonshot idea: introduce a interface to pin _specifically_ BPF LSM's to the kernel, and avoid the bpf sysfs problems all together.

Introducing non-auto-detachable lsm programs seems like a workable solution. That said, we can't remove the lsm program before it has been detached explicitly by the task which attaches it.

Quoted 6 lines

We're making the assumption this problem has been thought about before, and are wondering if there's anything obvious we're missing here.

Fred

Signature · 2 lines
Regards
Yafang
#2 lore raw
Quoted 37 lines · Yafang Shao
Quoted 36 lines · Frederick Lawler

Hi,

IIUC, LSMs are supposed to give us the ability to design policy around unprivileged users and in addition to privileged users. As we expand our usage of BPF LSM's, there are cases where we want to restrict privileged users from unloading our progs. For instance, any privileged user that wants to remove restrictions we've placed on privileged users.

We currently have a loader application doesn't leverage BPF skeletons. We instead load BPF object files, and then pin the progs to a mount point that is a bpf filesystem. On next run, if we have new policies, load in new policies, and finally unload the old.

Here are some conditions a privileged user may unload programs:

umount /sys/fs/bpf
rm -rf /sys/fs/bpf/lsm
rm /sys/fs/bpf/lsm/some_prog
unlink /sys/fs/bpf/lsm/some_prog

This works because once we remove the last reference, the programs and pinned maps are cleaned up.

Moving individual pins or moving the mount entirely with mount --move do not perform any clean up operations. Lastly, bpftool doesn't currently have the ability to unload LSM's AFAIK.

The few ideas I have floating around are:

1. Leverage some LSM hooks (BPF or otherwise) to restrict on the functions security_sb_umount(), security_path_unlink(), security_inode_unlink().

Both security_path_unlink() and security_inode_unlink() handle the unlink/remove case, but not the umount case.

Quoted 8 lines
Quoted 5 lines

3. Leverage SELinux/Apparmor to possibly handle these cases.

4. Introduce a security_bpf_prog_unload() to target hopefully the umount and unlink cases at the same time.

All the above programs can also be removed by privileged users.

I should probably clarify the "BPF or otherwise" a bit better. Even a compiled in LSM module? If so, where can I find a bit more information about that?

We are aware of some of the shortcomings of policy cfg for the AppArmor & SELinux case.

Quoted 9 lines
Quoted 3 lines

5. Possible moonshot idea: introduce a interface to pin _specifically_ BPF LSM's to the kernel, and avoid the bpf sysfs problems all together.

Introducing non-auto-detachable lsm programs seems like a workable solution. That said, we can't remove the lsm program before it has been detached explicitly by the task which attaches it.

Quoted 1 line
Quoted 10 lines
Quoted 5 lines

We're making the assumption this problem has been thought about before, and are wondering if there's anything obvious we're missing here.

Fred

Signature · 2 lines
Regards
Yafang

Fred

#3 lore raw
Quoted 45 lines · Frederick Lawler
Quoted 43 lines · Yafang Shao
Quoted 41 lines · Frederick Lawler

Hi,

IIUC, LSMs are supposed to give us the ability to design policy around unprivileged users and in addition to privileged users. As we expand our usage of BPF LSM's, there are cases where we want to restrict privileged users from unloading our progs. For instance, any privileged user that wants to remove restrictions we've placed on privileged users.

We currently have a loader application doesn't leverage BPF skeletons. We instead load BPF object files, and then pin the progs to a mount point that is a bpf filesystem. On next run, if we have new policies, load in new policies, and finally unload the old.

Here are some conditions a privileged user may unload programs:

umount /sys/fs/bpf
rm -rf /sys/fs/bpf/lsm
rm /sys/fs/bpf/lsm/some_prog
unlink /sys/fs/bpf/lsm/some_prog

This works because once we remove the last reference, the programs and pinned maps are cleaned up.

Moving individual pins or moving the mount entirely with mount --move do not perform any clean up operations. Lastly, bpftool doesn't currently have the ability to unload LSM's AFAIK.

The few ideas I have floating around are:

1. Leverage some LSM hooks (BPF or otherwise) to restrict on the functions security_sb_umount(), security_path_unlink(), security_inode_unlink().

Both security_path_unlink() and security_inode_unlink() handle the unlink/remove case, but not the umount case.

3. Leverage SELinux/Apparmor to possibly handle these cases.

4. Introduce a security_bpf_prog_unload() to target hopefully the umount and unlink cases at the same time.

Quoted 6 lines
Quoted 2 lines

All the above programs can also be removed by privileged users.

I should probably clarify the "BPF or otherwise" a bit better. Even a compiled in LSM module? If so, where can I find a bit more information about that?

Uncertain if it's feasible using the LSM module.
+security exports for help.

Quoted 25 lines

We are aware of some of the shortcomings of policy cfg for the AppArmor & SELinux case.

Quoted 12 lines
Quoted 3 lines

5. Possible moonshot idea: introduce a interface to pin _specifically_ BPF LSM's to the kernel, and avoid the bpf sysfs problems all together.

Introducing non-auto-detachable lsm programs seems like a workable solution. That said, we can't remove the lsm program before it has been detached explicitly by the task which attaches it.

Quoted 4 lines

We're making the assumption this problem has been thought about before, and are wondering if there's anything obvious we're missing here.

Quoted 7 lines
Quoted 2 lines

Fred

--
Regards
Yafang

Fred

Signature · 2 lines
Regards
Yafang
#4 lore raw
Quoted 11 lines · Yafang Shao
Quoted 11 lines
Quoted 11 lines
Quoted 11 lines

Moving individual pins or moving the mount entirely with mount --move do not perform any clean up operations. Lastly, bpftool doesn't currently have the ability to unload LSM's AFAIK.

The few ideas I have floating around are:

1. Leverage some LSM hooks (BPF or otherwise) to restrict on the functions security_sb_umount(), security_path_unlink(), security_inode_unlink().

Both security_path_unlink() and security_inode_unlink() handle the unlink/remove case, but not the umount case.

That is what I thought at
https://lkml.kernel.org/r/[email protected] , though I didn't try it because the conclusion was that trying to re-implement TOMOYO LSM module using BPF is not realistic.

While hooking security_sb_umount() from LSM modules will be possible, unconditionally rejecting umount operation might confuse userspace programs (e.g. retry until umount operation succeeds). Therefore, maybe introducing a kernel thread who holds a refcount using a file descriptor ownded by that kernel thread is better than trying to manage individual mount namepsaces and inodes... Letting a kernel code to intentionally leak that refcount instead of storing into somewhere might be possible, but that is considered as a kernel bug.

maintainer
#5 lore raw
In reply to Yafang Shao
Quoted 24 lines · Yafang Shao
Quoted 23 lines · Frederick Lawler
Quoted 22 lines · Yafang Shao
Quoted 21 lines · Frederick Lawler

Hi,

IIUC, LSMs are supposed to give us the ability to design policy around unprivileged users and in addition to privileged users. As we expand our usage of BPF LSM's, there are cases where we want to restrict privileged users from unloading our progs. For instance, any privileged user that wants to remove restrictions we've placed on privileged users.

We currently have a loader application doesn't leverage BPF skeletons. We instead load BPF object files, and then pin the progs to a mount point that is a bpf filesystem. On next run, if we have new policies, load in new policies, and finally unload the old.

Here are some conditions a privileged user may unload programs:

umount /sys/fs/bpf
rm -rf /sys/fs/bpf/lsm
rm /sys/fs/bpf/lsm/some_prog
unlink /sys/fs/bpf/lsm/some_prog

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines

This works because once we remove the last reference, the programs and pinned maps are cleaned up.

Moving individual pins or moving the mount entirely with mount --move do not perform any clean up operations. Lastly, bpftool doesn't currently have the ability to unload LSM's AFAIK.

If you haven't already, I would suggest talking with KP Singh as he is the BPF LSM maintainer; I see him on the To/CC line so I'm sure he'll comment when he has the chance to do so.

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines

The few ideas I have floating around are:

1. Leverage some LSM hooks (BPF or otherwise) to restrict on the functions security_sb_umount(), security_path_unlink(), security_inode_unlink().

Both security_path_unlink() and security_inode_unlink() handle the unlink/remove case, but not the umount case.

I'm not a BPF expert, but this seems like the most obvious solution, although as Tetsuo already mentioned you probably don't want to block all unmount operations as that would be bad for obvious reasons. I'm guessing that a BPF LSM would have access to things like the current task credentials and enough of the mounted filesystem's state (BPF prog pinning?) to make a reasonable decision about granting or denying the umount operation request.

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

3. Leverage SELinux/Apparmor to possibly handle these cases.

SELinux has support for restricting unmount operations as well BPF program loading. I see that AppArmor also has controls around unmount, but I am less familiar with how that works. It is also worth mentioning that Tomoyo and Landlock provide unmount hook implementations although both LSMs are fairly unique so I can't say if they would be a good fit for your proposed use case.

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

4. Introduce a security_bpf_prog_unload() to target hopefully the umount and unlink cases at the same time.

At first glance that seems reasonable, but I guess we would need to see it discussed a bit before I could promise to commit to that.

As a FYI, we have some documented guidelines on creating new LSM hooks; it's worth a quick read if you haven't seen it already.

https://github.com/LinuxSecurityModule/kernel?tab=readme-ov-file#new-lsm-hook-guidelines

Quoted 6 lines
Quoted 6 lines
Quoted 2 lines

All the above programs can also be removed by privileged users.

I should probably clarify the "BPF or otherwise" a bit better. Even a compiled in LSM module? If so, where can I find a bit more information about that?

I'm not quite sure what you are asking about here, but we don't currently support "unloading" built-in LSM modules and I don't see us changing that anytime soon. The closest one could get would be with a LSM that supports runtime configuration of its security policy; one could go from a restrictive or an allow-all, permissive policy effectively disabling the LSM from an access control standpoint.

I don't want to speak for all the LSMs here, but at least SELinux has the ability to restrict policy loading so that one could prevent replacing a relatively strict policy with a more permissive policy. Although it is worth noting that enabling this restriction has a number of caveats, i.e. policy updates require a reboot, and isn't something I would recommend for a general purpose system.

Signature · 1 line
paul-moore.com
#6 lore raw
Quoted 79 lines · Paul Moore
Quoted 23 lines · Yafang Shao
Quoted 22 lines · Frederick Lawler
Quoted 21 lines · Yafang Shao
Quoted 20 lines · Frederick Lawler

Hi,

IIUC, LSMs are supposed to give us the ability to design policy around unprivileged users and in addition to privileged users. As we expand our usage of BPF LSM's, there are cases where we want to restrict privileged users from unloading our progs. For instance, any privileged user that wants to remove restrictions we've placed on privileged users.

We currently have a loader application doesn't leverage BPF skeletons. We instead load BPF object files, and then pin the progs to a mount point that is a bpf filesystem. On next run, if we have new policies, load in new policies, and finally unload the old.

Here are some conditions a privileged user may unload programs:

umount /sys/fs/bpf
rm -rf /sys/fs/bpf/lsm
rm /sys/fs/bpf/lsm/some_prog
unlink /sys/fs/bpf/lsm/some_prog

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines

This works because once we remove the last reference, the programs and pinned maps are cleaned up.

Moving individual pins or moving the mount entirely with mount --move do not perform any clean up operations. Lastly, bpftool doesn't currently have the ability to unload LSM's AFAIK.

If you haven't already, I would suggest talking with KP Singh as he is the BPF LSM maintainer; I see him on the To/CC line so I'm sure he'll comment when he has the chance to do so.

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines

The few ideas I have floating around are:

1. Leverage some LSM hooks (BPF or otherwise) to restrict on the functions security_sb_umount(), security_path_unlink(), security_inode_unlink().

Both security_path_unlink() and security_inode_unlink() handle the unlink/remove case, but not the umount case.

I'm not a BPF expert, but this seems like the most obvious solution, although as Tetsuo already mentioned you probably don't want to block all unmount operations as that would be bad for obvious reasons. I'm guessing that a BPF LSM would have access to things like the current task credentials and enough of the mounted filesystem's state (BPF prog pinning?) to make a reasonable decision about granting or denying the umount operation request.

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

3. Leverage SELinux/Apparmor to possibly handle these cases.

SELinux has support for restricting unmount operations as well BPF program loading. I see that AppArmor also has controls around unmount, but I am less familiar with how that works. It is also worth mentioning that Tomoyo and Landlock provide unmount hook implementations although both LSMs are fairly unique so I can't say if they would be a good fit for your proposed use case.

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

4. Introduce a security_bpf_prog_unload() to target hopefully the umount and unlink cases at the same time.

At first glance that seems reasonable, but I guess we would need to see it discussed a bit before I could promise to commit to that.

As a FYI, we have some documented guidelines on creating new LSM hooks; it's worth a quick read if you haven't seen it already.

https://github.com/LinuxSecurityModule/kernel?tab=readme-ov-file#new-lsm-hook-guidelines

Quoted 5 lines
Quoted 5 lines
Quoted 2 lines

All the above programs can also be removed by privileged users.

I should probably clarify the "BPF or otherwise" a bit better. Even a compiled in LSM module? If so, where can I find a bit more information about that?

I'm not quite sure what you are asking about here, but we don't currently support "unloading" built-in LSM modules and I don't see us changing that anytime soon. The closest one could get would be with a LSM that supports runtime configuration of its security policy; one could go from a restrictive or an allow-all, permissive policy effectively disabling the LSM from an access control standpoint.

Quoted 6 lines

I don't want to speak for all the LSMs here, but at least SELinux has the ability to restrict policy loading so that one could prevent replacing a relatively strict policy with a more permissive policy. Although it is worth noting that enabling this restriction has a number of caveats, i.e. policy updates require a reboot, and isn't something I would recommend for a general purpose system.

You can prevent Smack from making policy changes using the "onlycap" mechanism. It restricts what process labels can use CAP_MAC_ADMIN, which is required to update Smack rules. As with SELinux, it isn't recommended for general use.

#7 lore raw
In reply to Frederick Lawler
Quoted 28 lines · Frederick Lawler

Hi,

IIUC, LSMs are supposed to give us the ability to design policy around unprivileged users and in addition to privileged users. As we expand our usage of BPF LSM's, there are cases where we want to restrict privileged users from unloading our progs. For instance, any privileged user that wants to remove restrictions we've placed on privileged users.

We currently have a loader application doesn't leverage BPF skeletons. We instead load BPF object files, and then pin the progs to a mount point that is a bpf filesystem. On next run, if we have new policies, load in new policies, and finally unload the old.

Here are some conditions a privileged user may unload programs:

umount /sys/fs/bpf
rm -rf /sys/fs/bpf/lsm
rm /sys/fs/bpf/lsm/some_prog
unlink /sys/fs/bpf/lsm/some_prog

This works because once we remove the last reference, the programs and pinned maps are cleaned up.

Moving individual pins or moving the mount entirely with mount --move do not perform any clean up operations. Lastly, bpftool doesn't currently have the ability to unload LSM's AFAIK.

Quoted 7 lines

The few ideas I have floating around are:

1. Leverage some LSM hooks (BPF or otherwise) to restrict on the functions security_sb_umount(), security_path_unlink(), security_inode_unlink().

Both security_path_unlink() and security_inode_unlink() handle the unlink/remove case, but not the umount case.

IMHO this is the best option. Here:

* BPF LSM Program = MAC Policy
* Removing / detaching / updating programs = Updating MAC policy

The decision around who can update MAC policy can be governed by the policy itself a.k.a. implemented with BPF LSM programs. So we can update hooks (as suggested here inode_unlink, sb_unmount, path_unlink) to only allow this action for a subset of users (e.g. CAP_MAC_ADMIN or even further restricted)

While, I think this may be doable with existing LSM hooks but we need to probably have to cover multiple hook points needed to prevent one action which makes a good case for another LSM hook, perhaps something in the link->ops->detach path like
https://elixir.bootlin.com/linux/latest/source/kernel/bpf/syscall.c#L5074

What do you think?

- KP

Quoted 18 lines

3. Leverage SELinux/Apparmor to possibly handle these cases.

4. Introduce a security_bpf_prog_unload() to target hopefully the umount and unlink cases at the same time.

5. Possible moonshot idea: introduce a interface to pin _specifically_ BPF LSM's to the kernel, and avoid the bpf sysfs problems all together.

We're making the assumption this problem has been thought about before, and are wondering if there's anything obvious we're missing here.

Fred

#8 lore raw
Quoted 35 lines · KP Singh
Quoted 33 lines · Frederick Lawler

Hi,

IIUC, LSMs are supposed to give us the ability to design policy around unprivileged users and in addition to privileged users. As we expand our usage of BPF LSM's, there are cases where we want to restrict privileged users from unloading our progs. For instance, any privileged user that wants to remove restrictions we've placed on privileged users.

We currently have a loader application doesn't leverage BPF skeletons. We instead load BPF object files, and then pin the progs to a mount point that is a bpf filesystem. On next run, if we have new policies, load in new policies, and finally unload the old.

Here are some conditions a privileged user may unload programs:

umount /sys/fs/bpf
rm -rf /sys/fs/bpf/lsm
rm /sys/fs/bpf/lsm/some_prog
unlink /sys/fs/bpf/lsm/some_prog

This works because once we remove the last reference, the programs and pinned maps are cleaned up.

Moving individual pins or moving the mount entirely with mount --move do not perform any clean up operations. Lastly, bpftool doesn't currently have the ability to unload LSM's AFAIK.

The few ideas I have floating around are:

1. Leverage some LSM hooks (BPF or otherwise) to restrict on the functions security_sb_umount(), security_path_unlink(), security_inode_unlink().

Quoted 7 lines
Quoted 2 lines

Both security_path_unlink() and security_inode_unlink() handle the unlink/remove case, but not the umount case.

IMHO this is the best option. Here:

* BPF LSM Program = MAC Policy
* Removing / detaching / updating programs = Updating MAC policy

What happens if a privileged user terminates the BPF LSM task and deletes any pinned BPF files that might exist?
We can apply specific capabilities to restrict access, but it's important to note that privileged users might also possess these capabilities.

Quoted 36 lines

The decision around who can update MAC policy can be governed by the policy itself a.k.a. implemented with BPF LSM programs. So we can update hooks (as suggested here inode_unlink, sb_unmount, path_unlink) to only allow this action for a subset of users (e.g. CAP_MAC_ADMIN or even further restricted)

While, I think this may be doable with existing LSM hooks but we need to probably have to cover multiple hook points needed to prevent one action which makes a good case for another LSM hook, perhaps something in the link->ops->detach path like
https://elixir.bootlin.com/linux/latest/source/kernel/bpf/syscall.c#L5074

What do you think?

- KP

Quoted 18 lines

3. Leverage SELinux/Apparmor to possibly handle these cases.

4. Introduce a security_bpf_prog_unload() to target hopefully the umount and unlink cases at the same time.

5. Possible moonshot idea: introduce a interface to pin _specifically_ BPF LSM's to the kernel, and avoid the bpf sysfs problems all together.

We're making the assumption this problem has been thought about before, and are wondering if there's anything obvious we're missing here.

Fred

Signature · 2 lines
Regards
Yafang
#9 lore raw
Quoted 8 lines
Quoted 5 lines

IMHO this is the best option. Here:

* BPF LSM Program = MAC Policy
* Removing / detaching / updating programs = Updating MAC policy

What happens if a privileged user terminates the BPF LSM task and deletes any pinned BPF files that might exist?

The LSM program is pinned, so it does not matter if the task is terminated.

Quoted 2 lines

We can apply specific capabilities to restrict access, but it's important to note that privileged users might also possess these

That depends on how you implement your restriction logic. If your LSM program says, check CAP_MAC_ADMIN -> Allow removal, then your logic explicitly grants the privilege. If your LSM hook denies all privileged users the ability to remove the program, then no privileged user can remove the LSM program.

The whole point here is to restrict privileged users from doing stuff.

- KP

Quoted 24 lines

capabilities.

Quoted 13 lines

The decision around who can update MAC policy can be governed by the policy itself a.k.a. implemented with BPF LSM programs. So we can update hooks (as suggested here inode_unlink, sb_unmount, path_unlink) to only allow this action for a subset of users (e.g. CAP_MAC_ADMIN or even further restricted)

While, I think this may be doable with existing LSM hooks but we need to probably have to cover multiple hook points needed to prevent one action which makes a good case for another LSM hook, perhaps something in the link->ops->detach path like
https://elixir.bootlin.com/linux/latest/source/kernel/bpf/syscall.c#L5074

Quoted 3 lines

What do you think?

- KP

--
Regards
Yafang

#10 lore raw
Quoted 12 lines · KP Singh
Quoted 9 lines
Quoted 6 lines

IMHO this is the best option. Here:

* BPF LSM Program = MAC Policy
* Removing / detaching / updating programs = Updating MAC policy

What happens if a privileged user terminates the BPF LSM task and deletes any pinned BPF files that might exist?

The LSM program is pinned, so it does not matter if the task is terminated.

Quoted 8 lines
Quoted 2 lines

We can apply specific capabilities to restrict access, but it's important to note that privileged users might also possess these

That depends on how you implement your restriction logic. If your LSM program says, check CAP_MAC_ADMIN -> Allow removal, then your logic explicitly grants the privilege. If your LSM hook denies all privileged users the ability to remove the program, then no privileged user can remove the LSM program.

If it's impossible for any privileged user to remove the LSM program, this brings up another question: how can we stop this program? However, if a privileged user does have the capability to remove it, then the individual capable of doing so might possess these privileges.

Quoted 29 lines

The whole point here is to restrict privileged users from doing stuff.

- KP

Quoted 24 lines

capabilities.

Quoted 13 lines

The decision around who can update MAC policy can be governed by the policy itself a.k.a. implemented with BPF LSM programs. So we can update hooks (as suggested here inode_unlink, sb_unmount, path_unlink) to only allow this action for a subset of users (e.g. CAP_MAC_ADMIN or even further restricted)

While, I think this may be doable with existing LSM hooks but we need to probably have to cover multiple hook points needed to prevent one action which makes a good case for another LSM hook, perhaps something in the link->ops->detach path like
https://elixir.bootlin.com/linux/latest/source/kernel/bpf/syscall.c#L5074

Quoted 3 lines

What do you think?

- KP

--
Regards
Yafang

Signature · 2 lines
Regards
Yafang
#11 lore raw
Quoted 17 lines · Yafang Shao
Quoted 15 lines · KP Singh
Quoted 9 lines
Quoted 6 lines

IMHO this is the best option. Here:

* BPF LSM Program = MAC Policy
* Removing / detaching / updating programs = Updating MAC policy

What happens if a privileged user terminates the BPF LSM task and deletes any pinned BPF files that might exist?

The LSM program is pinned, so it does not matter if the task is terminated.

Quoted 2 lines

We can apply specific capabilities to restrict access, but it's important to note that privileged users might also possess these

Quoted 8 lines
Quoted 5 lines

That depends on how you implement your restriction logic. If your LSM program says, check CAP_MAC_ADMIN -> Allow removal, then your logic explicitly grants the privilege. If your LSM hook denies all privileged users the ability to remove the program, then no privileged user can remove the LSM program.

If it's impossible for any privileged user to remove the LSM program, this brings up another question: how can we stop this program?

Again, it depends on how you implement the logic in the LSM hook, few ideas:

* Layout xattr based policy that allows one particular binary running as one particular user to remove the program
* Come up with your own rules that, e.g. say allows the system to boot in a "privileged / un-enforced mode" which allows CAP_MAC_ADMIN to update the policy.
* A really terrible way. All odd user IDs with MAC_ADMIN are allowed to remove the program.

The point here is to further fine grain the concept of administrative "privilege" for accessing specific resources, LSM programs being a resource and BPF LSM programs is one way.

What I would encourage you to look at is how other LSM programs allow dynamic updates to their LSM policy and the same mechanisms should be usable in BPF.

Quoted 39 lines

However, if a privileged user does have the capability to remove it, then the individual capable of doing so might possess these privileges.

Quoted 29 lines

The whole point here is to restrict privileged users from doing stuff.

- KP

Quoted 24 lines

capabilities.

Quoted 13 lines

The decision around who can update MAC policy can be governed by the policy itself a.k.a. implemented with BPF LSM programs. So we can update hooks (as suggested here inode_unlink, sb_unmount, path_unlink) to only allow this action for a subset of users (e.g. CAP_MAC_ADMIN or even further restricted)

While, I think this may be doable with existing LSM hooks but we need to probably have to cover multiple hook points needed to prevent one action which makes a good case for another LSM hook, perhaps something in the link->ops->detach path like
https://elixir.bootlin.com/linux/latest/source/kernel/bpf/syscall.c#L5074

Quoted 3 lines

What do you think?

- KP

--
Regards
Yafang

--
Regards
Yafang

#12 lore raw
In reply to KP Singh
Quoted 48 lines · KP Singh
Quoted 28 lines · Frederick Lawler

Hi,

IIUC, LSMs are supposed to give us the ability to design policy around unprivileged users and in addition to privileged users. As we expand our usage of BPF LSM's, there are cases where we want to restrict privileged users from unloading our progs. For instance, any privileged user that wants to remove restrictions we've placed on privileged users.

We currently have a loader application doesn't leverage BPF skeletons. We instead load BPF object files, and then pin the progs to a mount point that is a bpf filesystem. On next run, if we have new policies, load in new policies, and finally unload the old.

Here are some conditions a privileged user may unload programs:

umount /sys/fs/bpf
rm -rf /sys/fs/bpf/lsm
rm /sys/fs/bpf/lsm/some_prog
unlink /sys/fs/bpf/lsm/some_prog

This works because once we remove the last reference, the programs and pinned maps are cleaned up.

Moving individual pins or moving the mount entirely with mount --move do not perform any clean up operations. Lastly, bpftool doesn't currently have the ability to unload LSM's AFAIK.

Quoted 7 lines

The few ideas I have floating around are:

1. Leverage some LSM hooks (BPF or otherwise) to restrict on the functions security_sb_umount(), security_path_unlink(), security_inode_unlink().

Both security_path_unlink() and security_inode_unlink() handle the unlink/remove case, but not the umount case.

IMHO this is the best option. Here:

* BPF LSM Program = MAC Policy
* Removing / detaching / updating programs = Updating MAC policy

The decision around who can update MAC policy can be governed by the policy itself a.k.a. implemented with BPF LSM programs. So we can update hooks (as suggested here inode_unlink, sb_unmount, path_unlink) to only allow this action for a subset of users (e.g. CAP_MAC_ADMIN or even further restricted)

Quoted 7 lines

While, I think this may be doable with existing LSM hooks but we need to probably have to cover multiple hook points needed to prevent one action which makes a good case for another LSM hook, perhaps something in the link->ops->detach path like
https://elixir.bootlin.com/linux/latest/source/kernel/bpf/syscall.c#L5074

What do you think?

That's what I was thinking for option (4) "introduce a security_bpf_prog_unload()". Anyway, I agree. Paul brought up a good point that he'd like to see more discussion around this idea [1]. Mucking with the mounts (see below) is a bit of a mess, and there could still exist other methods for unloading I'm not aware of yet.

Yesterday I whipped up a hack such that:

mkdir -p /run/fs/bpf-lsm
mount -t bpf none /run/fs/bpf-lsm
./load-policies /run/fs/bpf-lsm

Where the implemented hooks inode_unlink & sb_umount look for a "bpf" filesystem type on the struct super_block for the mount, and then check for a xattr or UUID to hone in on _just_ that special mount point. This is so we can still umount /sys/fs/bpf. This hack so far is working out quite well.

xattr is still under development, thus the UUID idea (we're still exploring unique-path-agnostic identifiers). I also didn't prove the UUID idea yet. For instance, I wouldn't want the UUID to change on mount --move or mount -o remount.

Apropos the reboot problem, the idea is to force a reboot for policy change for now. And then eventually leverage xattr + IMA, or something else to prove the loader binary + progs once these features are available. Policy versioning is also an idea we're floating around for handling the update case with the loader to avoid reboot.

This is all to say, I think there is a use-case here for a new hook to simplify the unload problem at least.

[1] https://lore.kernel.org/all/CAHC9VhSRdXLeJvS3tOmAAat+h8G7_cvAYnFvbrTwgG+sC+PRYg@mail.gmail.com/

+cc: discussion contributors

Quoted 21 lines

- KP

Quoted 18 lines

3. Leverage SELinux/Apparmor to possibly handle these cases.

4. Introduce a security_bpf_prog_unload() to target hopefully the umount and unlink cases at the same time.

5. Possible moonshot idea: introduce a interface to pin _specifically_ BPF LSM's to the kernel, and avoid the bpf sysfs problems all together.

We're making the assumption this problem has been thought about before, and are wondering if there's anything obvious we're missing here.

Fred

Fred

P.S. This is an awesome discussion :) Thanks everyone!

reviewer
#13 lore raw

Hi Frederick,

Quoted 1 line · Frederick Lawler

[...]

Quoted 14 lines
Quoted 7 lines

While, I think this may be doable with existing LSM hooks but we need to probably have to cover multiple hook points needed to prevent one action which makes a good case for another LSM hook, perhaps something in the link->ops->detach path like
https://elixir.bootlin.com/linux/latest/source/kernel/bpf/syscall.c#L5074

What do you think?

That's what I was thinking for option (4) "introduce a security_bpf_prog_unload()". Anyway, I agree. Paul brought up a good point that he'd like to see more discussion around this idea [1]. Mucking with the mounts (see below) is a bit of a mess, and there could still exist other methods for unloading I'm not aware of yet.

Quoted 5 lines

Yesterday I whipped up a hack such that:

mkdir -p /run/fs/bpf-lsm
mount -t bpf none /run/fs/bpf-lsm
./load-policies /run/fs/bpf-lsm

Trying to understand the solution here. Does load-policies add multiple policies to stop different ways to unload the LSM BPF program (unpin, umount, etc.)? So the only way to unload these policies is reboot. If this is the case, could you please share the list of hooks needed to achieve a secure result? If the list is really long, we should probably add an option to permanently load and attach a program (until reboot).

Thanks,
Song

#14 lore raw
Quoted 25 lines · Song Liu

Hi Frederick,

Quoted 1 line · Frederick Lawler

[...]

Quoted 14 lines
Quoted 7 lines

While, I think this may be doable with existing LSM hooks but we need to probably have to cover multiple hook points needed to prevent one action which makes a good case for another LSM hook, perhaps something in the link->ops->detach path like
https://elixir.bootlin.com/linux/latest/source/kernel/bpf/syscall.c#L5074

What do you think?

That's what I was thinking for option (4) "introduce a security_bpf_prog_unload()". Anyway, I agree. Paul brought up a good point that he'd like to see more discussion around this idea [1]. Mucking with the mounts (see below) is a bit of a mess, and there could still exist other methods for unloading I'm not aware of yet.

Quoted 5 lines

Yesterday I whipped up a hack such that:

mkdir -p /run/fs/bpf-lsm
mount -t bpf none /run/fs/bpf-lsm
./load-policies /run/fs/bpf-lsm

Quoted 5 lines

Trying to understand the solution here. Does load-policies add multiple policies to stop different ways to unload the LSM BPF program (unpin, umount, etc.)? So the only way to unload these policies is reboot. If this is the case, could you please share the list of hooks needed to achieve a secure result?

./load-policies loads multiple BPF object files (policy) each containing N programs. Then for each program, pin it to the bpffs and terminate. There's more there for atomic loads etc... but not relevant for answering the question. For this hack, I created a bpf object file with two programs:

- lsm/sb_umount
- lsm/inode_unlink

More could be added to this list as necessary depending on finding other ways to unload. I've only found the filesystem to be the most consistent way so far. libbpf's unpin functions seem to be also trapped by the inode_unlink program, but more exploration syscalls is on my TODO list.

And added the object file along with the rest to load in.

Quoted 2 lines

If the list is really long, we should probably add an option to permanently load and attach a program (until reboot).

This is an interesting thought as well. I think that would fall under idea (5) [1]. But the list isn't that long, and lonterm, I'd like the loader to have permission to load/unload BPF LSM progs. But, this won't help folks that leverage BPF's skeleton loading methods and users that rely on anon inodes tied to the task. I think KP offered some ideas there [2].

[1] https://lore.kernel.org/all/ZW+KYViDT3HWtKI1@CMGLRV3/
[2] https://lore.kernel.org/all/CACYkzJ4QpQZ8JmdNXKWeSh8oc=jAyRh4Zj98Z+TG37Ce=cfE0w@mail.gmail.com/

Quoted 3 lines

Thanks,
Song

reviewer
#15 lore raw

Hi Frederick,

Quoted 6 lines · Frederick Lawler
Quoted 4 lines · Song Liu

Hi Frederick,

Quoted 1 line · Frederick Lawler

[...]

Quoted 6 lines
Quoted 5 lines

Trying to understand the solution here. Does load-policies add multiple policies to stop different ways to unload the LSM BPF program (unpin, umount, etc.)? So the only way to unload these policies is reboot. If this is the case, could you please share the list of hooks needed to achieve a secure result?

Quoted 8 lines

./load-policies loads multiple BPF object files (policy) each containing N programs. Then for each program, pin it to the bpffs and terminate. There's more there for atomic loads etc... but not relevant for answering the question. For this hack, I created a bpf object file with two programs:

- lsm/sb_umount
- lsm/inode_unlink

Thanks for the information. This seems easy enough.

Quoted 6 lines

More could be added to this list as necessary depending on finding other ways to unload. I've only found the filesystem to be the most consistent way so far. libbpf's unpin functions seem to be also trapped by the inode_unlink program, but more exploration syscalls is on my TODO list.

Quoted 7 lines

And added the object file along with the rest to load in.

Quoted 2 lines

If the list is really long, we should probably add an option to permanently load and attach a program (until reboot).

This is an interesting thought as well. I think that would fall under idea (5) [1]. But the list isn't that long, and lonterm, I'd like the loader

Agreed this falls under idea (5).

Quoted 3 lines

to have permission to load/unload BPF LSM progs. But, this won't help folks that leverage BPF's skeleton loading methods and users that rely on anon inodes tied to the task. I think KP offered some ideas there [2].

Thanks,
Song