Skip to content

Commit ce6c2d4

Browse files
committed
fix: allow su operation for SELinux Enforcing systems
This patch uses virtual resource names for IPC, which is currently not under the purview of SELinux, for now at least :)
1 parent d3707a4 commit ce6c2d4

File tree

3 files changed

+9
-46
lines changed

3 files changed

+9
-46
lines changed

Superuser/jni/su/daemon.c

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -431,29 +431,13 @@ int run_daemon() {
431431

432432
memset(&sun, 0, sizeof(sun));
433433
sun.sun_family = AF_LOCAL;
434-
sprintf(sun.sun_path, "%s/server", REQUESTOR_DAEMON_PATH);
435-
436-
/*
437-
* Delete the socket to protect from situations when
438-
* something bad occured previously and the kernel reused pid from that process.
439-
* Small probability, isn't it.
440-
*/
441-
unlink(sun.sun_path);
442-
unlink(REQUESTOR_DAEMON_PATH);
443-
444-
int previous_umask = umask(027);
445-
mkdir(REQUESTOR_DAEMON_PATH, 0777);
434+
sprintf(sun.sun_path, "%c%s/server", '\0', REQUESTOR_DAEMON_PATH);
446435

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

452-
chmod(REQUESTOR_DAEMON_PATH, 0755);
453-
chmod(sun.sun_path, 0777);
454-
455-
umask(previous_umask);
456-
457441
if (listen(fd, 10) < 0) {
458442
PLOGE("daemon listen");
459443
goto err;
@@ -549,7 +533,7 @@ int connect_daemon(int argc, char *argv[], int ppid) {
549533

550534
memset(&sun, 0, sizeof(sun));
551535
sun.sun_family = AF_LOCAL;
552-
sprintf(sun.sun_path, "%s/server", REQUESTOR_DAEMON_PATH);
536+
sprintf(sun.sun_path, "%c%s/server", '\0', REQUESTOR_DAEMON_PATH);
553537

554538
if (0 != connect(socketfd, (struct sockaddr*)&sun, sizeof(sun))) {
555539
PLOGE("connect");

Superuser/jni/su/su.c

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -271,9 +271,7 @@ void set_identity(unsigned int uid) {
271271
}
272272

273273
static void socket_cleanup(struct su_context *ctx) {
274-
if (ctx && ctx->sock_path[0]) {
275-
if (unlink(ctx->sock_path))
276-
PLOGE("unlink (%s)", ctx->sock_path);
274+
if (ctx) {
277275
ctx->sock_path[0] = 0;
278276
}
279277
}
@@ -313,15 +311,12 @@ static int socket_create_temp(char *path, size_t len) {
313311
snprintf(path, len, "%s/.socket%d", REQUESTOR_CACHE_PATH, getpid());
314312
memset(sun.sun_path, 0, sizeof(sun.sun_path));
315313
snprintf(sun.sun_path, sizeof(sun.sun_path), "%s", path);
314+
/* use abstract namespace for socket path */
315+
sun.sun_path[0] = '\0';
316+
strcpy(&sun.sun_path[1], path);
317+
size_t size = offsetof(struct sockaddr_un, sun_path) + 1 + strlen(&sun.sun_path[1]);
316318

317-
/*
318-
* Delete the socket to protect from situations when
319-
* something bad occured previously and the kernel reused pid from that process.
320-
* Small probability, isn't it.
321-
*/
322-
unlink(sun.sun_path);
323-
324-
if (bind(fd, (struct sockaddr*)&sun, sizeof(sun)) < 0) {
319+
if (bind(fd, (struct sockaddr*)&sun, size) < 0) {
325320
PLOGE("bind");
326321
goto err;
327322
}

Superuser/src/com/koushikdutta/superuser/MultitaskSuRequestActivity.java

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,6 @@ protected void onDestroy() {
140140
}
141141
catch (Exception ex) {
142142
}
143-
new File(mSocketPath).delete();
144143
}
145144

146145
public static final String PERMISSION = "android.permission.ACCESS_SUPERUSER";
@@ -303,7 +302,7 @@ void manageSocket() {
303302
public void run() {
304303
try {
305304
mSocket = new LocalSocket();
306-
mSocket.connect(new LocalSocketAddress(mSocketPath, Namespace.FILESYSTEM));
305+
mSocket.connect(new LocalSocketAddress(mSocketPath, Namespace.ABSTRACT));
307306

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

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

393392
manageSocket();
394393

395-
396-
// watch for the socket disappearing. that means su died.
397-
new Runnable() {
398-
public void run() {
399-
if (isFinishing())
400-
return;
401-
if (!new File(mSocketPath).exists()) {
402-
finish();
403-
return;
404-
}
405-
406-
mHandler.postDelayed(this, 1000);
407-
};
408-
}.run();
409-
410394
mHandler.postDelayed(new Runnable() {
411395
@Override
412396
public void run() {

0 commit comments

Comments
 (0)