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

Keep CAP_SYS_PTRACE with suid binary #1043

Merged
merged 2 commits into from
Jan 12, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions sway/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
#include <unistd.h>
#include <getopt.h>
#include <sys/capability.h>
#ifdef __linux__
#include <sys/prctl.h>
#endif
#include "sway/extensions.h"
#include "sway/layout.h"
#include "sway/config.h"
Expand Down Expand Up @@ -289,6 +292,18 @@ int main(int argc, char **argv) {
return 0;
}

#ifdef __linux__
bool suid = false;
if (getuid() != geteuid() || getgid() != getegid()) {
// Retain capabilities after setuid()
if (prctl(PR_SET_KEEPCAPS, 1, 0, 0, 0)) {
sway_log(L_ERROR, "Cannot keep caps after setuid()");
exit(EXIT_FAILURE);
}
suid = true;
}
#endif

// we need to setup logging before wlc_init in case it fails.
if (debug) {
init_log(L_DEBUG);
Expand All @@ -311,6 +326,20 @@ int main(int argc, char **argv) {
}
register_extensions();

#ifdef __linux__
if (suid) {
// Drop every cap except CAP_SYS_PTRACE
cap_t caps = cap_init();
cap_value_t keep = CAP_SYS_PTRACE;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't we also want that TTY one?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, it was required because a suid binary would not gain all caps as effective, and besides, all the privileged stuff is dealt with in a child process of wlc (forked in wlc_init, so it keeps everything), so it isn't.

sway_log(L_INFO, "Dropping extra capabilities");
if (cap_set_flag(caps, CAP_PERMITTED, 1, &keep, CAP_SET) ||
cap_set_flag(caps, CAP_EFFECTIVE, 1, &keep, CAP_SET) ||
cap_set_proc(caps)) {
sway_log(L_ERROR, "Failed to drop extra capabilities");
exit(EXIT_FAILURE);
}
}
#endif
// handle SIGTERM signals
signal(SIGTERM, sig_handler);

Expand Down