Skip to content

Commit

Permalink
fix: allow su operation for SELinux Enforcing systems
Browse files Browse the repository at this point in the history
This patch uses virtual resource names for IPC, which is currently not under the purview of
SELinux, for now at least :)
  • Loading branch information
mohankr committed Feb 26, 2014
1 parent d3707a4 commit ce6c2d4
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 46 deletions.
20 changes: 2 additions & 18 deletions Superuser/jni/su/daemon.c
Original file line number Diff line number Diff line change
Expand Up @@ -431,29 +431,13 @@ int run_daemon() {

memset(&sun, 0, sizeof(sun));
sun.sun_family = AF_LOCAL;
sprintf(sun.sun_path, "%s/server", REQUESTOR_DAEMON_PATH);

/*
* Delete the socket to protect from situations when
* something bad occured previously and the kernel reused pid from that process.
* Small probability, isn't it.
*/
unlink(sun.sun_path);
unlink(REQUESTOR_DAEMON_PATH);

int previous_umask = umask(027);
mkdir(REQUESTOR_DAEMON_PATH, 0777);
sprintf(sun.sun_path, "%c%s/server", '\0', REQUESTOR_DAEMON_PATH);

if (bind(fd, (struct sockaddr*)&sun, sizeof(sun)) < 0) {
PLOGE("daemon bind");
goto err;
}

chmod(REQUESTOR_DAEMON_PATH, 0755);
chmod(sun.sun_path, 0777);

umask(previous_umask);

if (listen(fd, 10) < 0) {
PLOGE("daemon listen");
goto err;
Expand Down Expand Up @@ -549,7 +533,7 @@ int connect_daemon(int argc, char *argv[], int ppid) {

memset(&sun, 0, sizeof(sun));
sun.sun_family = AF_LOCAL;
sprintf(sun.sun_path, "%s/server", REQUESTOR_DAEMON_PATH);
sprintf(sun.sun_path, "%c%s/server", '\0', REQUESTOR_DAEMON_PATH);

if (0 != connect(socketfd, (struct sockaddr*)&sun, sizeof(sun))) {
PLOGE("connect");
Expand Down
17 changes: 6 additions & 11 deletions Superuser/jni/su/su.c
Original file line number Diff line number Diff line change
Expand Up @@ -271,9 +271,7 @@ void set_identity(unsigned int uid) {
}

static void socket_cleanup(struct su_context *ctx) {
if (ctx && ctx->sock_path[0]) {
if (unlink(ctx->sock_path))
PLOGE("unlink (%s)", ctx->sock_path);
if (ctx) {
ctx->sock_path[0] = 0;
}
}
Expand Down Expand Up @@ -313,15 +311,12 @@ static int socket_create_temp(char *path, size_t len) {
snprintf(path, len, "%s/.socket%d", REQUESTOR_CACHE_PATH, getpid());
memset(sun.sun_path, 0, sizeof(sun.sun_path));
snprintf(sun.sun_path, sizeof(sun.sun_path), "%s", path);
/* use abstract namespace for socket path */
sun.sun_path[0] = '\0';
strcpy(&sun.sun_path[1], path);
size_t size = offsetof(struct sockaddr_un, sun_path) + 1 + strlen(&sun.sun_path[1]);

/*
* Delete the socket to protect from situations when
* something bad occured previously and the kernel reused pid from that process.
* Small probability, isn't it.
*/
unlink(sun.sun_path);

if (bind(fd, (struct sockaddr*)&sun, sizeof(sun)) < 0) {
if (bind(fd, (struct sockaddr*)&sun, size) < 0) {
PLOGE("bind");
goto err;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,6 @@ protected void onDestroy() {
}
catch (Exception ex) {
}
new File(mSocketPath).delete();
}

public static final String PERMISSION = "android.permission.ACCESS_SUPERUSER";
Expand Down Expand Up @@ -303,7 +302,7 @@ void manageSocket() {
public void run() {
try {
mSocket = new LocalSocket();
mSocket.connect(new LocalSocketAddress(mSocketPath, Namespace.FILESYSTEM));
mSocket.connect(new LocalSocketAddress(mSocketPath, Namespace.ABSTRACT));

DataInputStream is = new DataInputStream(mSocket.getInputStream());

Expand Down Expand Up @@ -392,21 +391,6 @@ protected void onCreate(Bundle savedInstanceState) {

manageSocket();


// watch for the socket disappearing. that means su died.
new Runnable() {
public void run() {
if (isFinishing())
return;
if (!new File(mSocketPath).exists()) {
finish();
return;
}

mHandler.postDelayed(this, 1000);
};
}.run();

mHandler.postDelayed(new Runnable() {
@Override
public void run() {
Expand Down

3 comments on commit ce6c2d4

@Djamal57baraja
Copy link

Choose a reason for hiding this comment

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

Thank you very much. Great appl

@malamel
Copy link

@malamel malamel commented on ce6c2d4 May 4, 2016

Choose a reason for hiding this comment

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

.

@marcowill
Copy link

Choose a reason for hiding this comment

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

Thanks!!!

Please sign in to comment.