Skip to content

Commit 382d1e3

Browse files
committed
Use am to send intents rather than a binder transaction.
This commit is by koush and is mostly copied from the CyanogenMod fork of su. By using am instead of a binder transaction, we can guarantee compatibility with current and future versions of Android.
1 parent f39dbf0 commit 382d1e3

File tree

4 files changed

+76
-154
lines changed

4 files changed

+76
-154
lines changed

activity.c

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
/*
2+
** Copyright 2010, Adam Shanks (@ChainsDD)
3+
** Copyright 2008, Zinx Verituse (@zinxv)
4+
**
5+
** Licensed under the Apache License, Version 2.0 (the "License");
6+
** you may not use this file except in compliance with the License.
7+
** You may obtain a copy of the License at
8+
**
9+
** http://www.apache.org/licenses/LICENSE-2.0
10+
**
11+
** Unless required by applicable law or agreed to in writing, software
12+
** distributed under the License is distributed on an "AS IS" BASIS,
13+
** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
** See the License for the specific language governing permissions and
15+
** limitations under the License.
16+
*/
17+
18+
#include <unistd.h>
19+
20+
#include "su.h"
21+
22+
int send_intent(const struct su_context *ctx,
23+
const char *socket_path, int allow, const char *action)
24+
{
25+
char command[PATH_MAX];
26+
27+
sprintf(command, "/system/bin/am broadcast -a %s --es socket %s --ei version_code %d > /dev/null",
28+
action, socket_path, VERSION_CODE);
29+
30+
// before sending the intent, make sure the (uid and euid) and (gid and egid) match,
31+
// otherwise LD_LIBRARY_PATH is wiped in Android 4.0+.
32+
// Also, sanitize all secure environment variables (from linker_environ.c in linker).
33+
34+
/* The same list than GLibc at this point */
35+
static const char* const unsec_vars[] = {
36+
"GCONV_PATH",
37+
"GETCONF_DIR",
38+
"HOSTALIASES",
39+
"LD_AUDIT",
40+
"LD_DEBUG",
41+
"LD_DEBUG_OUTPUT",
42+
"LD_DYNAMIC_WEAK",
43+
"LD_LIBRARY_PATH",
44+
"LD_ORIGIN_PATH",
45+
"LD_PRELOAD",
46+
"LD_PROFILE",
47+
"LD_SHOW_AUXV",
48+
"LD_USE_LOAD_BIAS",
49+
"LOCALDOMAIN",
50+
"LOCPATH",
51+
"MALLOC_TRACE",
52+
"MALLOC_CHECK_",
53+
"NIS_PATH",
54+
"NLSPATH",
55+
"RESOLV_HOST_CONF",
56+
"RES_OPTIONS",
57+
"TMPDIR",
58+
"TZDIR",
59+
"LD_AOUT_LIBRARY_PATH",
60+
"LD_AOUT_PRELOAD",
61+
// not listed in linker, used due to system() call
62+
"IFS",
63+
};
64+
const char* const* cp = unsec_vars;
65+
const char* const* endp = cp + sizeof(unsec_vars)/sizeof(unsec_vars[0]);
66+
while (cp < endp) {
67+
unsetenv(*cp);
68+
cp++;
69+
}
70+
71+
// sane value so "am" works
72+
setenv("LD_LIBRARY_PATH", "/vendor/lib:/system/lib", 1);
73+
setegid(getgid());
74+
seteuid(getuid());
75+
return system(command);
76+
}

activity.cpp

Lines changed: 0 additions & 142 deletions
This file was deleted.

su.c

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -42,15 +42,6 @@
4242
/* Still lazt, will fix this */
4343
static char socket_path[PATH_MAX];
4444

45-
46-
static inline int get_sdk_version(void)
47-
{
48-
char sdk_version_prop[PROPERTY_VALUE_MAX];
49-
50-
property_get("ro.build.version.sdk", sdk_version_prop, "0");
51-
return atoi(sdk_version_prop);
52-
}
53-
5445
static int from_init(struct su_initiator *from)
5546
{
5647
char path[PATH_MAX], exe[PATH_MAX];
@@ -455,8 +446,6 @@ int main(int argc, char *argv[])
455446
}
456447
ctx.to.optind = optind;
457448

458-
ctx.sdk_version = get_sdk_version();
459-
460449
if (from_init(&ctx.from) < 0) {
461450
deny(&ctx);
462451
}

su.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,6 @@ struct su_context {
7070
struct su_initiator from;
7171
struct su_request to;
7272
mode_t umask;
73-
int sdk_version;
7473
};
7574

7675
enum {

0 commit comments

Comments
 (0)