Skip to content

Commit f79e166

Browse files
committed
Merge master-dev into master
1 parent 91cd65c commit f79e166

File tree

5 files changed

+257
-185
lines changed

5 files changed

+257
-185
lines changed

Android.mk

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,19 @@ LOCAL_PATH := $(call my-dir)
22
include $(CLEAR_VARS)
33

44
LOCAL_MODULE := su
5-
LOCAL_SRC_FILES := su.c activity.cpp
5+
LOCAL_SRC_FILES := su.c db.c activity.cpp
66

77

88
LOCAL_C_INCLUDES += external/sqlite/dist
9+
910
LOCAL_SHARED_LIBRARIES := \
1011
liblog \
1112
libsqlite \
1213
libcutils \
1314
libbinder \
14-
libutils
15+
libutils \
16+
1517
LOCAL_MODULE_PATH := $(TARGET_OUT_OPTIONAL_EXECUTABLES)
16-
LOCAL_MODULE_TAGS := eng,debug
18+
LOCAL_MODULE_TAGS := debug,eng
1719

1820
include $(BUILD_EXECUTABLE)

activity.cpp

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,20 @@
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+
118
#include <unistd.h>
219
#include <android_runtime/ActivityManager.h>
320
#include <binder/IBinder.h>
@@ -23,7 +40,7 @@ static const int VAL_INTEGER = 1;
2340

2441
static const int START_SUCCESS = 0;
2542

26-
int send_intent(struct su_initiator *from, struct su_request *to, const char *socket_path, int type)
43+
int send_intent(struct su_initiator *from, struct su_request *to, const char *socket_path, int allow, int type)
2744
{
2845
char sdk_version_prop[PROPERTY_VALUE_MAX] = "0";
2946
property_get("ro.build.version.sdk", sdk_version_prop, "0");
@@ -43,7 +60,7 @@ int send_intent(struct su_initiator *from, struct su_request *to, const char *so
4360
if (type == 0) {
4461
data.writeString16(String16("com.noshufou.android.su.REQUEST")); /* action */
4562
} else {
46-
data.writeString16(String16("com.noshufou.android.su.NOTIFICATION")); /* action */
63+
data.writeString16(String16("com.noshufou.android.su.RESULT")); /* action */
4764
}
4865
data.writeInt32(NULL_TYPE_ID); /* Uri - data */
4966
data.writeString16(NULL, 0); /* type */
@@ -63,13 +80,18 @@ int send_intent(struct su_initiator *from, struct su_request *to, const char *so
6380
int oldPos = data.dataPosition();
6481
data.writeInt32(0x4C444E42); // 'B' 'N' 'D' 'L'
6582
{ /* writeMapInternal */
66-
data.writeInt32(4); /* writeMapInternal - size */
83+
data.writeInt32(7); /* writeMapInternal - size */
6784

6885
data.writeInt32(VAL_STRING);
6986
data.writeString16(String16("caller_uid"));
7087
data.writeInt32(VAL_INTEGER);
7188
data.writeInt32(from->uid);
7289

90+
data.writeInt32(VAL_STRING);
91+
data.writeString16(String16("caller_bin"));
92+
data.writeInt32(VAL_STRING);
93+
data.writeString16(String16(from->bin));
94+
7395
data.writeInt32(VAL_STRING);
7496
data.writeString16(String16("desired_uid"));
7597
data.writeInt32(VAL_INTEGER);
@@ -84,6 +106,16 @@ int send_intent(struct su_initiator *from, struct su_request *to, const char *so
84106
data.writeString16(String16("socket"));
85107
data.writeInt32(VAL_STRING);
86108
data.writeString16(String16(socket_path));
109+
110+
data.writeInt32(VAL_STRING);
111+
data.writeString16(String16("allow"));
112+
data.writeInt32(VAL_INTEGER);
113+
data.writeInt32(allow);
114+
115+
data.writeInt32(VAL_STRING);
116+
data.writeString16(String16("version_code"));
117+
data.writeInt32(VAL_INTEGER);
118+
data.writeInt32(VERSION_CODE);
87119
}
88120
int newPos = data.dataPosition();
89121
data.setDataPosition(oldPos - 4);

db.c

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
/*
2+
** Copyright 2010, Adam Shanks (@ChainsDD)
3+
**
4+
** Licensed under the Apache License, Version 2.0 (the "License");
5+
** you may not use this file except in compliance with the License.
6+
** You may obtain a copy of the License at
7+
**
8+
** http://www.apache.org/licenses/LICENSE-2.0
9+
**
10+
** Unless required by applicable law or agreed to in writing, software
11+
** distributed under the License is distributed on an "AS IS" BASIS,
12+
** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
** See the License for the specific language governing permissions and
14+
** limitations under the License.
15+
*/
16+
17+
#include <stdlib.h>
18+
#include <sys/stat.h>
19+
#include <limits.h>
20+
#include <cutils/log.h>
21+
22+
#include <sqlite3.h>
23+
24+
#include "su.h"
25+
26+
// { int* pint; pint=(int*)data; ++(*pint); }
27+
28+
sqlite3 *database_init()
29+
{
30+
sqlite3 *db;
31+
int version, rc, databaseStatus = 0;
32+
char *zErrMsg = 0;
33+
34+
rc = sqlite3_open_v2(REQUESTOR_DATABASE_PATH, &db, SQLITE_OPEN_READONLY, NULL);
35+
if ( rc ) {
36+
LOGE("Couldn't open database: %s", sqlite3_errmsg(db));
37+
return NULL;
38+
}
39+
40+
// Create an automatic busy handler in case the db is locked
41+
sqlite3_busy_timeout(db, 1000);
42+
return db;
43+
}
44+
45+
int database_check(sqlite3 *db, struct su_initiator *from, struct su_request *to)
46+
{
47+
char sql[4096];
48+
char *zErrmsg;
49+
char **result;
50+
int nrow,ncol;
51+
int allow;
52+
struct timeval tv;
53+
54+
sqlite3_snprintf(
55+
sizeof(sql), sql,
56+
"SELECT _id,name,allow FROM apps WHERE uid=%u AND exec_uid=%u AND exec_cmd='%q';",
57+
(unsigned)from->uid, to->uid, to->command
58+
);
59+
60+
if (strlen(sql) >= sizeof(sql)-1)
61+
return DB_DENY;
62+
63+
int error = sqlite3_get_table(db, sql, &result, &nrow, &ncol, &zErrmsg);
64+
if (error != SQLITE_OK) {
65+
LOGE("Database check failed with error message %s", zErrmsg);
66+
if (error == SQLITE_BUSY) {
67+
LOGE("Specifically, the database is busy");
68+
}
69+
return DB_DENY;
70+
}
71+
72+
if (nrow == 0 || ncol != 3)
73+
return DB_INTERACTIVE;
74+
75+
if (strcmp(result[0], "_id") == 0 && strcmp(result[2], "allow") == 0) {
76+
if (strcmp(result[5], "1") == 0) {
77+
allow = DB_ALLOW;
78+
} else if (strcmp(result[5], "-1") == 0){
79+
allow = DB_INTERACTIVE;
80+
} else {
81+
allow = DB_DENY;
82+
}
83+
return allow;
84+
}
85+
86+
sqlite3_free_table(result);
87+
88+
return DB_INTERACTIVE;
89+
}

0 commit comments

Comments
 (0)