Skip to content

Commit

Permalink
Fix multiuser mode
Browse files Browse the repository at this point in the history
  • Loading branch information
topjohnwu committed Aug 6, 2017
1 parent 86a113a commit 8d8355d
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 17 deletions.
40 changes: 33 additions & 7 deletions db.c
Original file line number Diff line number Diff line change
Expand Up @@ -60,40 +60,66 @@ static int settings_callback(void *v, int argc, char **argv, char **azColName) {

void database_check(struct su_context *ctx) {
sqlite3 *db = NULL;
int ret;
char query[512], *err = NULL;

// Set default values
ctx->info->root_access = ROOT_ACCESS_APPS_AND_ADB;
ctx->info->multiuser_mode = MULTIUSER_MODE_OWNER_ONLY;
ctx->info->mnt_ns = NAMESPACE_MODE_REQUESTER;
ctx->info->policy = QUERY;

// First query the from app data
// Check if file is readable
if (access(ctx->user.database_path, R_OK) == -1)
if (access(APP_DATA_PATH REQUESTOR_DATABASE_PATH, R_OK) == -1)
return;

// Open database
int ret = sqlite3_open_v2(ctx->user.database_path, &db, SQLITE_OPEN_READONLY, NULL);
ret = sqlite3_open_v2(APP_DATA_PATH REQUESTOR_DATABASE_PATH, &db, SQLITE_OPEN_READONLY, NULL);
if (ret) {
LOGD("sqlite3 open failure: %s\n", sqlite3_errstr(ret));
sqlite3_close(db);
return;
}

char query[512], *err = NULL;

// Check multiuser mode settings
snprintf(query, sizeof(query), "SELECT key, value FROM settings WHERE key='%s'", MULTIUSER_MODE_ENTRY);
sqlite3_exec(db, query, settings_callback, ctx, &err);

err = NULL;

if (ctx->user.android_user_id != 0 && ctx->info->multiuser_mode == MULTIUSER_MODE_USER) {
sqlite3_close(db);
// Check if file is readable
if (access(ctx->user.database_path, R_OK) == -1)
return;

// Open database
ret = sqlite3_open_v2(ctx->user.database_path, &db, SQLITE_OPEN_READONLY, NULL);
if (ret) {
LOGD("sqlite3 open failure: %s\n", sqlite3_errstr(ret));
sqlite3_close(db);
return;
}
}

// Query for policy
snprintf(query, sizeof(query), "SELECT policy, until FROM policies WHERE uid=%d", ctx->info->uid % 100000);
sqlite3_exec(db, query, policy_callback, ctx, &err);
if (err != NULL)
if (err != NULL) {
LOGE("sqlite3_exec: %s\n", err);
return;
}

err = NULL;

// Query for settings
snprintf(query, sizeof(query), "SELECT key, value FROM settings");
snprintf(query, sizeof(query), "SELECT key, value FROM settings WHERE key!='%s'", MULTIUSER_MODE_ENTRY);
sqlite3_exec(db, query, settings_callback, ctx, &err);
if (err != NULL)
if (err != NULL) {
LOGE("sqlite3_exec: %s\n", err);
return;
}

sqlite3_close(db);
}
6 changes: 0 additions & 6 deletions su.h
Original file line number Diff line number Diff line change
Expand Up @@ -89,12 +89,6 @@ struct su_user_info {
// the user in android userspace (multiuser)
// that invoked this action.
unsigned android_user_id;
// path to superuser directory. this is populated according
// to the multiuser mode.
// this is used to check uid/gid for protecting socket.
// this is used instead of database, as it is more likely
// to exist. db will not exist if su has never launched.
char base_path[PATH_MAX];
// path to su database. this is populated according
// to the multiuser mode.
char database_path[PATH_MAX];
Expand Down
6 changes: 2 additions & 4 deletions su_daemon.c
Original file line number Diff line number Diff line change
Expand Up @@ -155,11 +155,9 @@ void su_daemon_receiver(int client) {

snprintf(su_ctx->user.database_path, PATH_MAX, "%s/%d/%s",
USER_DATA_PATH, su_ctx->user.android_user_id, REQUESTOR_DATABASE_PATH);
snprintf(su_ctx->user.base_path, PATH_MAX, "%s/%d/%s",
USER_DATA_PATH, su_ctx->user.android_user_id, REQUESTOR);

// verify if Magisk Manager is installed
xstat(su_ctx->user.base_path, &su_ctx->st);
xstat(APP_DATA_PATH REQUESTOR, &su_ctx->st);
// odd perms on superuser data dir
if (su_ctx->st.st_gid != su_ctx->st.st_uid) {
LOGE("Bad uid/gid %d/%d for Superuser Requestor application", su_ctx->st.st_uid, su_ctx->st.st_gid);
Expand Down Expand Up @@ -342,7 +340,7 @@ void su_daemon_receiver(int client) {
su_daemon_main(argc, argv);
}

/*
/*
* Connect daemon, send argc, argv, cwd, pts slave
*/
int su_client_main(int argc, char *argv[]) {
Expand Down

0 comments on commit 8d8355d

Please sign in to comment.