Add support for ambient capabilities#408
Conversation
|
You seem to be saying that pam_cap.so by itself isn't able to give you the pI you specify in capabilities.conf, and therefore you need to set KEEPCAPS in login.c. Is that what you are finding? I think it would be better to add ambient capability support to pam_cap.so and leave all that logic out of su and login, if possible. |
|
Leaving this to pam_cap.so entirely, will not work. Please, let me explain: Ambient capabilities will not survive the setuid() system call. To support ambient capabilities, one has to save the ambient caps before calling setuid() and restore them after that. Calling setuid() is the most important thing the login and su applications do. There is no way to delegate this to pam_cap.so. The keepcaps flag is different. This flag is thread specific and has to be set via prctl(). The flag must be set with root privileges and survives the setuid() call but it won't survive execve(). Without the keepcaps flag set it is not possible to restore the ambient caps after the setuid() call. So, specifying ambient capabilities in capabilities.conf, will grant these capabilities to PAM clients up to the moment they call setuid(). With ambient caps not surviving setuid() and the keepcaps flag not surviving execve(), supporting ambient capabilities naturally fall in the scope of login and su. |
|
Yes, I see. Ideally there would be a pam_* helper called after the setuid() in which pam_cap.so could re-set the pA. |
|
There seem like there are two aspects to this pull request:
For (1), is there some reason that the For (2), In the proof of concept example I worked up here: https://git.kernel.org/pub/scm/libs/libcap/libcap.git/tree/contrib/sucap I used the https://bugzilla.kernel.org/show_bug.cgi?id=214377 to explore if |
|
So this will be included in libcap-2.58 (plus any bug fixes I accumulate before then): Let me know if it works for you: |
|
As I like this approach very much to keep everything within the PAM layer, this does not work without further modifications:
This should not be changed, I think. The man page PAM_OPEN_SESSION(3) suggests that the calling application should have sufficient privileges to perform tasks like creating or mounting the user's home dir. Calling ¹Seems to have quite a paper trail. |
|
That is, indeed, a pity. I have one more idea before giving up on this. |
|
My work here should be done. Specifically, comment 3 here: Use the same instructions for downloading and building the module (or This attempt is based on the Linux-PAM documentation for application writers (I have some vague recollection of writing those words about 20 years ago...!), so if this doesn't work, I think there is some sort of problem with the application:
This latest If the app doesn't do this, then that's a deviation from that documentation, and that's a PAM API fix the app can make... |
|
Indeed. Looking at the missing call to |
|
Great idea to put it into the cleanup code path. Confirmed to work with So, thanks to Andrew's excellent work, my pull request basically collapses to: |
|
Taking into account that there's a commit that reverts another one I think that you should squash everything in one commit. |
|
How do I do that? Preferably |
|
I'd use For more information you can check: https://levelup.gitconnected.com/how-to-squash-git-commits-9a095c1bc1fc |
aeacf82 to
ac938b2
Compare
|
There might be some issue left to explore... Expanding the diff for that patch for I don't know how old that comment is, but clearly there was some concern that it didn't work reliably at the time it was written. I'd argue that this is some sort of distributed non-compliance problem if it still exists. I plan to stick with this support expectation in |
Looks like it was introduced in 2007 by commit 0fd1ed4 to address https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=412061 |
|
Does not look that well. pam_krb5 still does not implement |
|
FWIW I just released libcap-2.58. |
|
The pam-krb5 commit should have resolved this issue with that module. |
Awesome. Can you confirm (Björn) that, and explain why, the commented-out location is |
|
In fact, the added location of |
ac938b2 to
97f788f
Compare
|
Ok, I have used the patched version for some time now on my production workstation and laptop, and I explored a few different PAM setups at my university. Seems to work as intended. Anything left to do before merge? |
|
I was trying this out and it did not work for me. I took fedora version of libcap, patched it with keepcaps and defer commits from libcap upstream. Then I took shadow-utils patched with this PR and installed both these modified packages. I set /etc/security/capability.conf to @bjorn-fischer @AndrewGMorgan Am I doing something wrong? |
|
[...]
This usually happens if the application performing the login procedure misses the |
|
@ZoltanFridrich When tracing This was the call to fork(), now looking for PID 21167: After this, no |
|
@ZoltanFridrich I suspect your |
|
I dug in a bit more. On Fedora-34, It too is non-compliant and the needed patch looks something like this: I've forwarded a copy of that patch to the author of the most recent Changelog entry here: https://mirrors.edge.kernel.org/pub/linux/utils/util-linux/v2.37/v2.37-ChangeLog |
|
Just in case, informing @karelzak maintainer of util-linux |
|
Thanks for your info. It seems, the most important thing is PAM_DATA_SILENT -- I hope it's correctly implemented in PAM, because everywhere (in util-linux login and su) we keep the parent process running to clean up the session later. We have to be sure that after pam_end() in the child the PAM handler is still valid in the parent. AndrewGMorgan and bjorn-fischer, please, please, can you also prepare PR for util-linux for login-utils/su-common.c and login-utils/login.c. I guess it's not only Fedora who uses login and su from util-linux. It's a trivial patch, but I'd like to be sure it's tested by someone who has experience with ambient capabilities. Thanks! |
|
@AndrewGMorgan @bjorn-fischer so just to make sure, this patch is good to merge? |
|
This looks good to me. However, the tabbing for the added code might need some attention to be consistent with the code it is being added to. |
This conforms to PAM documentation and it is needed to support ambient capabilities with PAM + libcap-2.58+. Signed-off-by: Björn Fischer <bf@CeBiTec.Uni-Bielefeld.DE>
Thanks, I went ahead and made that change, so am merging now. |
Caveat: This is for discussion, mostly. Still need to have a policy for bounded capabilities.
Enhance login(1) and su(1) to support ambient capabilities
How things work without ambient capabilities
POSIX capabilities have been supported by Linux for quite some time now. Most distributions allow to set inheritable capabilities:
Activate
pam_cap.so(comes with PAM enabled libcap) in your PAM config......add capabilities to users in
/etc/security/capability.conf......and enable that capability on an executable:
Now joeuser can perform traceroutes that require raw sockets without using sudo, e.g.
While this already works on most Linux distributions, you have to set the inherited capabilities on every applicable executable, and you will have to set it again after that executable has been updated. It may be the case that joeuser is your network admin and should be allowed to use raw sockets in any application. This is where ambient capabilities come into play.
Ambient Capabilities: Granting privileges on any application
Users with ambient capabilities can use these without having them set on an executable with
setcap. If joeuser has the ambient capability cap_net_raw, he would be able to use raw sockets in any application, even in scripting languages.Unfortunately ambient capabilities are more or less unsupported in current distributions. The main reason for this is lacking support in applications that grant access to Linux systems (like login(1) and su(1)). Those applications usually run with root privileges and drop these with the setuid() system call which clears all ambient capabilities. Generally this is a good idea, because there are many applications out there that rely on setuid() to drop all privileges.
To include support for ambient capabilities into login and su, the application has to set the "keep capabilities" flag and save the set of ambient capabilities before calling setuid(), and restore them afterwards. Since version 2.50 libcap's pam_cap.so module supports the "keepcaps" argument, which takes care of the "keep capabilities" flag. So each PAM enabled application just needs to save and restore the capabilities during setuid(). Old and new behavior can be controlled at runtime by changing the PAM configuration.
Signed-off-by: Björn Fischer bf@CeBiTec.Uni-Bielefeld.DE