Add container request check logic 53/266353/1
authorWootak Jung <wootak.jung@samsung.com>
Wed, 10 Nov 2021 01:21:28 +0000 (10:21 +0900)
committerdh79pyun <dh79.pyun@samsung.com>
Thu, 11 Nov 2021 02:24:57 +0000 (11:24 +0900)
Change-Id: I8f7419f03e4937c9d41ece4a704b8f411ad04227

bt-service/services/bt-request-handler.c
packaging/bluetooth-frwk.spec

index 51f7249..7c54dd0 100644 (file)
 /* For maintaining Application Sync API call requests */
 static GSList *invocation_list = NULL;
 
+#ifdef TIZEN_FEATURE_BT_CONTAINER
+struct nspid_t {
+       char *unique_name;
+       gboolean is_container;
+};
+static GSList *nspid_list = NULL;
+#endif
+
 static GDBusConnection *bt_service_conn;
 static guint owner_id = 0;
 static guint owner_sig_id = 0;
@@ -5005,6 +5013,81 @@ static int __bt_service_get_requester_app_id(const char *unique_name, char *app_
        return BLUETOOTH_ERROR_NONE;
 }
 
+#ifdef TIZEN_FEATURE_BT_CONTAINER
+static void __bt_service_free_nspid(struct nspid_t *nspid)
+{
+       g_free(nspid->unique_name);
+       g_free(nspid);
+}
+
+static void __bt_service_cleanup_nspid_list(const char *unique_name)
+{
+       GSList *l;
+       struct nspid_t *nspid;
+
+       for (l = nspid_list; l; l = g_slist_next(l)) {
+               nspid = l->data;
+               if (!nspid)
+                       continue;
+               if (!g_strcmp0(nspid->unique_name, unique_name)) {
+                       nspid_list = g_slist_remove(nspid_list, nspid);
+                       __bt_service_free_nspid(nspid);
+                       return;
+               }
+       }
+}
+
+static gboolean __bt_service_is_container_request(const char *unique_name)
+{
+       int ret;
+       pid_t pid;
+       char path[1024], buf[1024];
+       FILE *fp;
+       gboolean is_container = FALSE;
+       GSList *l;
+       struct nspid_t *nspid;
+
+       for (l = nspid_list; l; l = g_slist_next(l)) {
+               nspid = l->data;
+               if (!nspid)
+                       continue;
+               if (!g_strcmp0(nspid->unique_name, unique_name))
+                       return nspid->is_container;
+       }
+
+       ret = __bt_service_get_sender_pid(unique_name, &pid);
+       if (ret != BLUETOOTH_ERROR_NONE)
+               return FALSE;
+
+       sprintf(path, "/proc/%d/status", pid);
+       fp = fopen(path, "r");
+       if (fp == NULL) {
+               BT_ERR("fopen() failed. %d(%s)", errno, strerror(errno));
+               return FALSE;
+       }
+
+       while (fgets(buf, sizeof(buf), fp)) {
+               if (strstr(buf, "NSpid:")) {
+                       char *p = strchr(buf + 7, '\t'); /* find 2nd '\t' */
+                       if (p) {
+                               BT_DBG("caller is container process");
+                               is_container = TRUE;
+                               goto out;
+                       }
+                       break;
+               }
+       }
+
+out:
+       nspid = g_malloc0(sizeof(struct nspid_t));
+       nspid->unique_name = g_strdup(unique_name);
+       nspid->is_container = is_container;
+       nspid_list = g_slist_append(nspid_list, nspid);
+       fclose(fp);
+       return is_container;
+}
+#endif
+
 gboolean __bt_service_check_privilege(int function_name,
                                        int service_type,
                                        const char *unique_name)
@@ -5021,6 +5104,14 @@ gboolean __bt_service_check_privilege(int function_name,
        retv_if(unique_name == NULL, FALSE);
        retv_if(bt_service_conn == NULL, FALSE);
 
+#ifdef TIZEN_FEATURE_BT_CONTAINER
+       /* Privilege will be checked in container if the request is from container */
+       if (__bt_service_is_container_request(unique_name)) {
+               BT_DBG("This request is from container, temporarily allow");
+               return TRUE;
+       }
+#endif
+
        ret_val = cynara_creds_get_default_client_method(&client_creds_method);
        if (ret_val != CYNARA_API_SUCCESS) {
                cynara_strerror(ret_val, err_msg, sizeof(err_msg));
@@ -5559,6 +5650,11 @@ static void __name_owner_changed(GDBusConnection *connection,
 
        /* Stop the Transport Discovery service */
        _bt_tds_stop_by_terminated_process(name);
+
+#ifdef TIZEN_FEATURE_BT_CONTAINER
+       /* Cleanup the NSpid list */
+       __bt_service_cleanup_nspid_list(name);
+#endif
 }
 
 static void __bt_service_bus_acquired_handler(GDBusConnection *connection,
@@ -5668,6 +5764,13 @@ void _bt_service_unregister(void)
 
                __bt_service_register_object(bt_service_conn, NULL, FALSE);
        }
+
+#ifdef TIZEN_FEATURE_BT_CONTAINER
+       if (nspid_list) {
+               g_slist_free_full(nspid_list, (GDestroyNotify)__bt_service_free_nspid);
+               nspid_list = NULL;
+       }
+#endif
 }
 
 int _bt_service_cynara_init(void)
index 9a544bc..c98c43a 100644 (file)
@@ -207,6 +207,7 @@ export CFLAGS="$CFLAGS -DTIZEN_DEBUG_ENABLE"
 export CFLAGS="$CFLAGS -DTIZEN_FEATURE_BT_OBEX"
 export CFLAGS="$CFLAGS -DTIZEN_FEATURE_BT_PAN_NAP"
 export CFLAGS="$CFLAGS -DTIZEN_FEATURE_BT_IPSP"
+export CFLAGS="$CFLAGS -DTIZEN_FEATURE_BT_CONTAINER"
 export CXXFLAGS="$CXXFLAGS -DTIZEN_DEBUG_ENABLE"
 export FFLAGS="$FFLAGS -DTIZEN_DEBUG_ENABLE"