diff options
-rw-r--r-- | src/tzip/tzip.c | 87 |
1 files changed, 87 insertions, 0 deletions
diff --git a/src/tzip/tzip.c b/src/tzip/tzip.c index cdd9cb78..d1654043 100644 --- a/src/tzip/tzip.c +++ b/src/tzip/tzip.c @@ -37,10 +37,14 @@ #include <sys/stat.h> #include <assert.h> #include <attr/xattr.h> +#include <linux/limits.h> +#include <tzplatform_config.h> #include "tzip.h" #include "tzip-utility.h" +#define APP_ATTR_PATH "/proc/%d/attr/current" + static pthread_t thread; static pthread_t mount_thread; static pthread_attr_t attr; @@ -48,6 +52,73 @@ static struct fuse *fuse_handle = NULL; static struct fuse_chan *channel = NULL; static GAsyncQueue *async_queue; +static int check_smack_label(pid_t pid) +{ + char path[PATH_MAX]; + char attr[64]; + size_t len; + FILE *fp; + + snprintf(path, sizeof(path), APP_ATTR_PATH, pid); + + fp = fopen(path, "r"); + if (!fp) + return 0; + + len = fread(attr, 1, sizeof(attr) - 1, fp); + fclose(fp); + if (len == 0) + return 0; + + attr[len] = '\0'; + + if (!strncmp("System", attr, len + 1)) + return 1; + + if (!strncmp("User", attr, len + 1)) + return 1; + + if (!strncmp("System::Privileged", attr, len + 1)) + return 1; + + return 0; +} + +static int check_path_available(char *mountpath) +{ + size_t len; + + if (!mountpath) + return 0; + + len = strlen(mountpath); + + if (!strncmp(mountpath, tzplatform_getenv(TZ_SYS_HOME), len)) + return 1; + + if (!strncmp(mountpath, tzplatform_getenv(TZ_SYS_RW_APP), len)) + return 1; + + return 0; +} + +static int is_app_privileged(pid_t pid, char *mountpath) +{ + int priv; + + priv = check_path_available(mountpath); + if (priv == 0) { + _E("TZIP mount path is invalid (%s)", mountpath); + return priv; + } + + priv = check_smack_label(pid); + if (priv == 0) + _E("PID (%d) cannot use TZIP due to smack label"); + + return priv; +} + static int tzip_getattr(const char *path, struct stat *stbuf) { int res = 0; @@ -615,6 +686,7 @@ static DBusMessage *edbus_request_mount_tzip(E_DBus_Object *obj, DBusMessage *ms char *smack; int ret; struct tzip_msg_data *msgdata = NULL; + pid_t pid; dbus_error_init(&err); @@ -634,6 +706,13 @@ static DBusMessage *edbus_request_mount_tzip(E_DBus_Object *obj, DBusMessage *ms goto out; } + pid = get_edbus_sender_pid(msg); + if (!is_app_privileged(pid, mountpath)) { + _E("PID (%d) is not privileged to use tzip", pid); + ret = -EPERM; + goto out; + } + if (!fuse_handle) tzip_server_init(); @@ -693,6 +772,7 @@ static DBusMessage *edbus_request_unmount_tzip(E_DBus_Object *obj, DBusMessage * char *mountpath; int ret; struct tzip_msg_data *msgdata = NULL; + pid_t pid; dbus_error_init(&err); @@ -709,6 +789,13 @@ static DBusMessage *edbus_request_unmount_tzip(E_DBus_Object *obj, DBusMessage * goto out; } + pid = get_edbus_sender_pid(msg); + if (!is_app_privileged(pid, mountpath)) { + _E("PID (%d) is not privileged to use tzip", pid); + ret = -EPERM; + goto out; + } + if (!fuse_handle) tzip_server_init(); |