Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

libselinux: Do not clobber errno of the world #445

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

crrodriguez
Copy link

libselinux clobbers errno of all consumers (systemd, sshd you name it) because its constructors do not properly save and restore errno. The following program demonstrates it.

int main(void)
{
assert(errno == 0);
/* Just a test, it doesn't matter already clobbered */
if(is_selinux_enabled() < 0) {
perror("not enabled");
return 1;
}
}

if any function sets errno, it is not switched back to the original value and standards DO NOT require errno to the set to zero before entering main

libselinux clobbers errno of all consumers (systemd, sshd you name it)
because its constructors do not properly save and restore errno.
The following program demonstrates it.

int main(void)
{
        assert(errno == 0);
	/* Just a test, it doesn't matter already clobbered */
        if(is_selinux_enabled() < 0) {
                perror("not enabled");
                return 1;
        }
}

if any function sets errno, it is not switched back to the original
value and standards DO NOT require errno to the set to zero before
entering main

Signed-off-by: Cristian Rodríguez <cristian@rodriguez.im>
crrodriguez added a commit to crrodriguez/systemd that referenced this pull request Sep 28, 2024
standards do not require errno to be reset to zero before entering
main, third party libraries like openssl and libcap(1) use ELF
constructors that if buggy (1) will clobber errno value to everybody
else.

Libselinux also clobbers errno in most tests cases (2) ensure that is
set to zero before starting.

(1) https://git.kernel.org/pub/scm/libs/libcap/libcap.git/commit/?id=f25a1b7e69f7b33e6afb58b3e38f3450b7d2d9a0
(2) SELinuxProject/selinux#445
@cgzones
Copy link
Contributor

cgzones commented Nov 15, 2024

Seems fine, just a nit: the identifier __selinux_saved_errno is reserved (due to the leading underscore), maybe use selinux_saved_errno__?

Also the main way of contribution is to send patches via git send-email to selinux@vger.kernel.org.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants