Skip to content

Commit 588c414

Browse files
committed
Revert "Use flat files for stored apps"
This reverts commit ec37525.
1 parent 15ea355 commit 588c414

File tree

3 files changed

+83
-31
lines changed

3 files changed

+83
-31
lines changed

Android.mk

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,11 @@ include $(CLEAR_VARS)
44
LOCAL_MODULE := su
55
LOCAL_SRC_FILES := su.c db.c activity.c utils.c
66

7+
LOCAL_C_INCLUDES += external/sqlite/dist
8+
79
LOCAL_STATIC_LIBRARIES := \
810
liblog \
11+
libsqlite \
912
libc \
1013

1114
LOCAL_MODULE_PATH := $(TARGET_OUT_OPTIONAL_EXECUTABLES)

db.c

Lines changed: 77 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -15,44 +15,93 @@
1515
*/
1616

1717
#include <stdlib.h>
18-
#include <stdio.h>
18+
#include <sys/stat.h>
1919
#include <limits.h>
2020
#include <cutils/log.h>
2121

22+
#include <sqlite3.h>
23+
2224
#include "su.h"
2325

24-
int database_check(const struct su_context *ctx)
26+
static sqlite3 *db_init(void)
27+
{
28+
sqlite3 *db;
29+
int rc;
30+
31+
rc = sqlite3_open_v2(REQUESTOR_DATABASE_PATH, &db, SQLITE_OPEN_READONLY, NULL);
32+
if ( rc ) {
33+
LOGE("Couldn't open database: %s", sqlite3_errmsg(db));
34+
return NULL;
35+
}
36+
37+
// Create an automatic busy handler in case the db is locked
38+
sqlite3_busy_timeout(db, 1000);
39+
return db;
40+
}
41+
42+
static int db_check(sqlite3 *db, const struct su_context *ctx)
2543
{
26-
FILE *fp;
27-
char allow = '-';
28-
char *filename = malloc(snprintf(NULL, 0, "%s/%u-%u", REQUESTOR_STORED_PATH, ctx->from.uid, ctx->to.uid) + 1);
29-
sprintf(filename, "%s/%u-%u", REQUESTOR_STORED_PATH, ctx->from.uid, ctx->to.uid);
30-
if ((fp = fopen(filename, "r"))) {
31-
LOGD("Found file");
32-
char cmd[PATH_MAX];
33-
fgets(cmd, sizeof(cmd), fp);
34-
int last = strlen(cmd) - 1;
35-
LOGD("this is the last character %u of the string", cmd[5]);
36-
if (cmd[last] == '\n') {
37-
cmd[last] = '\0';
44+
char sql[4096];
45+
char *zErrmsg;
46+
char **result;
47+
int nrow,ncol;
48+
int allow = DB_INTERACTIVE;
49+
50+
sqlite3_snprintf(
51+
sizeof(sql), sql,
52+
"SELECT _id,name,allow FROM apps WHERE uid=%u AND exec_uid=%u AND exec_cmd='%q';",
53+
ctx->from.uid, ctx->to.uid, get_command(&ctx->to)
54+
);
55+
56+
if (strlen(sql) >= sizeof(sql)-1)
57+
return DB_DENY;
58+
59+
int error = sqlite3_get_table(db, sql, &result, &nrow, &ncol, &zErrmsg);
60+
if (error != SQLITE_OK) {
61+
LOGE("Database check failed with error message %s", zErrmsg);
62+
if (error == SQLITE_BUSY) {
63+
LOGE("Specifically, the database is busy");
3864
}
39-
LOGD("Comparing %c %s, %u to %s", cmd[last - 2], cmd, last, get_command(&ctx->to));
40-
if (strcmp(cmd, get_command(&ctx->to)) == 0) {
41-
allow = fgetc(fp);
65+
return DB_DENY;
66+
}
67+
68+
if (nrow == 0 || ncol != 3)
69+
goto out;
70+
71+
if (strcmp(result[0], "_id") == 0 && strcmp(result[2], "allow") == 0) {
72+
if (strcmp(result[5], "1") == 0) {
73+
allow = DB_ALLOW;
74+
} else if (strcmp(result[5], "-1") == 0){
75+
allow = DB_INTERACTIVE;
76+
} else {
77+
allow = DB_DENY;
4278
}
43-
fclose(fp);
44-
} else if ((fp = fopen(REQUESTOR_STORED_DEFAULT, "r"))) {
45-
LOGD("Using default");
46-
allow = fgetc(fp);
47-
fclose(fp);
4879
}
49-
free(filename);
5080

51-
if (allow == '1') {
52-
return DB_ALLOW;
53-
} else if (allow == '0') {
54-
return DB_DENY;
55-
} else {
81+
out:
82+
sqlite3_free_table(result);
83+
84+
return allow;
85+
}
86+
87+
int database_check(const struct su_context *ctx)
88+
{
89+
sqlite3 *db;
90+
int dballow;
91+
92+
LOGE("sudb - Opening database");
93+
db = db_init();
94+
if (!db) {
95+
LOGE("sudb - Could not open database, prompt user");
96+
// if the database could not be opened, we can assume we need to
97+
// prompt the user
5698
return DB_INTERACTIVE;
5799
}
100+
101+
LOGE("sudb - Database opened");
102+
dballow = db_check(db, ctx);
103+
// Close the database, we're done with it. If it stays open, it will cause problems
104+
sqlite3_close(db);
105+
LOGE("sudb - Database closed");
106+
return dballow;
58107
}

su.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,10 @@
2525

2626
#define REQUESTOR "com.noshufou.android.su"
2727
#define REQUESTOR_DATA_PATH "/data/data/" REQUESTOR
28-
#define REQUESTOR_CACHE_PATH REQUESTOR_DATA_PATH "/cache"
28+
#define REQUESTOR_CACHE_PATH "/dev/" REQUESTOR
2929

30-
#define REQUESTOR_STORED_PATH REQUESTOR_DATA_PATH "/files/stored"
31-
#define REQUESTOR_STORED_DEFAULT REQUESTOR_STORED_PATH "/default"
30+
#define REQUESTOR_DATABASES_PATH REQUESTOR_DATA_PATH "/databases"
31+
#define REQUESTOR_DATABASE_PATH REQUESTOR_DATABASES_PATH "/permissions.sqlite"
3232

3333
/* intent actions */
3434
#define ACTION_REQUEST REQUESTOR ".REQUEST"

0 commit comments

Comments
 (0)