Do not pass internal pointers to clients 02/240702/4 accepted/tizen/unified/20200812.143927 submit/tizen/20200811.122142
authorKarol Lewandowski <k.lewandowsk@samsung.com>
Mon, 10 Aug 2020 14:02:32 +0000 (16:02 +0200)
committerKarol Lewandowski <k.lewandowsk@samsung.com>
Tue, 11 Aug 2020 11:23:55 +0000 (13:23 +0200)
Before this commit peripheral-bus would pass pointers to
local structures to clients via dbus.  This scheme broke
on aarch64 as the pointers were converted to 32-bit uint
dbus type.

This commits introduces random-number based handle ids
that are mapped from/to internal pointers.

Change-Id: Id12156e251526b79f1b9207c85f6b618871e9d38

16 files changed:
include/gdbus/peripheral_gdbus_adc.h
include/gdbus/peripheral_gdbus_gpio.h
include/gdbus/peripheral_gdbus_i2c.h
include/gdbus/peripheral_gdbus_pwm.h
include/gdbus/peripheral_gdbus_spi.h
include/gdbus/peripheral_gdbus_uart.h
include/handle/peripheral_handle.h
include/handle/peripheral_handle_common.h
packaging/peripheral-bus.spec
src/gdbus/peripheral_gdbus_adc.c
src/gdbus/peripheral_gdbus_gpio.c
src/gdbus/peripheral_gdbus_i2c.c
src/gdbus/peripheral_gdbus_pwm.c
src/gdbus/peripheral_gdbus_spi.c
src/gdbus/peripheral_gdbus_uart.c
src/handle/peripheral_handle_common.c

index f13a0e8bb485a3dd19701ab22b8434d4ffe24ebe..37659912c342131302ccbe7242a0252e257f2c5e 100644 (file)
@@ -30,7 +30,7 @@ gboolean peripheral_gdbus_adc_open(
 gboolean peripheral_gdbus_adc_close(
                PeripheralIoGdbusAdc *adc,
                GDBusMethodInvocation *invocation,
-               gint handle,
+               guint handle,
                gpointer user_data);
 
 #endif /* __PERIPHERAL_GDBUS_ADC_H__ */
index 0fa281963a18834c23373c25d8acc8483d08b313..240632cb7e9c3570557defa7fb9db5db4b750c1c 100644 (file)
@@ -29,7 +29,7 @@ gboolean peripheral_gdbus_gpio_open(
 gboolean peripheral_gdbus_gpio_close(
                PeripheralIoGdbusGpio *gpio,
                GDBusMethodInvocation *invocation,
-               gint handle,
+               guint handle,
                gpointer user_data);
 
 #endif /* __PERIPHERAL_GDBUS_GPIO_H__ */
index 81b59fadae2528d2ee9dac746cf1c7e1ba6b2896..6fb3813452fc14339f0ad8facf244e32d1c9a144 100644 (file)
@@ -30,7 +30,7 @@ gboolean peripheral_gdbus_i2c_open(
 gboolean peripheral_gdbus_i2c_close(
                PeripheralIoGdbusI2c *i2c,
                GDBusMethodInvocation *invocation,
-               gint handle,
+               guint handle,
                gpointer user_data);
 
 #endif /* __PERIPHERAL_GDBUS_I2C_H__ */
index b61a8163a9ce1822052b3c53c80cd11785d8a113..f93e48694533cbbf6d6f0d92b37cdd460da828d4 100644 (file)
@@ -30,7 +30,7 @@ gboolean peripheral_gdbus_pwm_open(
 gboolean peripheral_gdbus_pwm_close(
                PeripheralIoGdbusPwm *pwm,
                GDBusMethodInvocation *invocation,
-               gint handle,
+               guint handle,
                gpointer user_data);
 
 #endif /* __PERIPHERAL_GDBUS_PWM_H__ */
index c3765085c67ce323b9c25554e5f90c5de856ceb0..c634501bb3ff8104507fbade1028e5b1789054d7 100644 (file)
@@ -30,7 +30,7 @@ gboolean peripheral_gdbus_spi_open(
 gboolean peripheral_gdbus_spi_close(
                PeripheralIoGdbusSpi *spi,
                GDBusMethodInvocation *invocation,
-               gint handle,
+               guint handle,
                gpointer user_data);
 
 #endif /* __PERIPHERAL_GDBUS_SPI_H__ */
index 393a7caf3db1f28a39f3b112ed040e23d1ca9eab..c3865163ec25ae6d017d330a772077ec2c80c3d7 100644 (file)
@@ -29,7 +29,7 @@ gboolean peripheral_gdbus_uart_open(
 gboolean peripheral_gdbus_uart_close(
                PeripheralIoGdbusUart *uart,
                GDBusMethodInvocation *invocation,
-               gint handle,
+               guint handle,
                gpointer user_data);
 
 #endif /* __PERIPHERAL_GDBUS_UART_H__ */
index 73e57c76a6f93bccfd7f7b3bbc385b34e6dcb9a7..2f1336df58cb6d5aa7081691ebc8c0c78a821e9e 100644 (file)
@@ -18,6 +18,7 @@
 #define __PERIPHERAL_HANDLE_H__
 
 #include <gio/gio.h>
+#include <stdint.h>
 
 #include "peripheral_board.h"
 #include "peripheral_io_gdbus.h"
@@ -71,6 +72,7 @@ typedef struct {
 
 typedef struct {
        uint watch_id;
+       uint32_t handle_id;
        GList **list;
        union {
                peripheral_handle_gpio_s gpio;
@@ -84,4 +86,7 @@ typedef struct {
 
 typedef peripheral_handle_s *peripheral_h;
 
+#define PERIPHERAL_H_TO_ID(ph) ((ph) ? (ph->handle_id) : 0)
+
+#include "peripheral_handle_common.h"
 #endif /* __PERIPHERAL_HANDLE_H__ */
index 20742ac5dfc056307077d92cc37e2693d585044b..ff2ff238232eb6d1a565aecebafc3ae2f51010b2 100644 (file)
@@ -19,6 +19,7 @@
 
 #include <stdio.h>
 #include <stdlib.h>
+#include <stdint.h>
 #include <gio/gio.h>
 #include <peripheral_io.h>
 
@@ -27,5 +28,6 @@
 
 peripheral_h peripheral_handle_new(GList **plist);
 int peripheral_handle_free(peripheral_h handle);
+peripheral_h peripheral_handle_find(GList *list, uint32_t _handle_id);
 
 #endif /* __PERIPHERAL_HANDLE_COMMON_H__ */
index 4285eb19f2aac53d9179b382aca35b871d5cbcbe..aa33384107cc4c2d77e1508e9bdec52458c915d6 100644 (file)
@@ -1,6 +1,6 @@
 Name:       peripheral-bus
 Summary:    Tizen Peripheral Input & Output Service Daemon
-Version:    0.1.2
+Version:    0.1.3
 Release:    0
 Group:      System & System Tools
 License:    Apache-2.0
index a88fa787d7e9ead1db52e8fdd0a001498ac1be26..73d3f417b1c9c89fa269d511c566e6d33a204e56 100644 (file)
@@ -83,7 +83,7 @@ gboolean peripheral_gdbus_adc_open(
                        NULL);
 
 out:
-       peripheral_io_gdbus_adc_complete_open(adc, invocation, adc_fd_list, GPOINTER_TO_UINT(adc_handle), ret);
+       peripheral_io_gdbus_adc_complete_open(adc, invocation, adc_fd_list, PERIPHERAL_H_TO_ID(adc_handle), ret);
        peripheral_interface_adc_fd_list_destroy(adc_fd_list);
 
        return true;
@@ -92,12 +92,18 @@ out:
 gboolean peripheral_gdbus_adc_close(
                PeripheralIoGdbusAdc *adc,
                GDBusMethodInvocation *invocation,
-               gint handle,
+               guint handle,
                gpointer user_data)
 {
        int ret = PERIPHERAL_ERROR_NONE;
 
-       peripheral_h adc_handle = GUINT_TO_POINTER(handle);
+       peripheral_info_s *info = (peripheral_info_s*)user_data;
+       peripheral_h adc_handle = peripheral_handle_find(info->adc_list, handle);
+       if (!adc_handle) {
+               _E("Unable to find handle referenced by client");
+               ret = PERIPHERAL_ERROR_INVALID_PARAMETER;
+               goto out;
+       }
 
        g_bus_unwatch_name(adc_handle->watch_id);
 
@@ -105,6 +111,7 @@ gboolean peripheral_gdbus_adc_close(
        if (ret != PERIPHERAL_ERROR_NONE)
                _E("Failed to destroy adc handle");
 
+out:
        peripheral_io_gdbus_adc_complete_close(adc, invocation, ret);
 
        return true;
index 0f52b7bfee23e6dd4cc4b684d40c155f09278692..e5952a2502f41b74ef42aa5e9bfe10292859c648 100644 (file)
@@ -97,7 +97,7 @@ gboolean peripheral_gdbus_gpio_open(
                        NULL);
 
 out:
-       peripheral_io_gdbus_gpio_complete_open(gpio, invocation, gpio_fd_list, GPOINTER_TO_UINT(gpio_handle), ret);
+       peripheral_io_gdbus_gpio_complete_open(gpio, invocation, gpio_fd_list, PERIPHERAL_H_TO_ID(gpio_handle), ret);
        peripheral_interface_gpio_fd_list_destroy(gpio_fd_list);
 
        return true;
@@ -106,12 +106,18 @@ out:
 gboolean peripheral_gdbus_gpio_close(
                PeripheralIoGdbusGpio *gpio,
                GDBusMethodInvocation *invocation,
-               gint handle,
+               guint handle,
                gpointer user_data)
 {
        int ret = PERIPHERAL_ERROR_NONE;
 
-       peripheral_h gpio_handle = GUINT_TO_POINTER(handle);
+       peripheral_info_s *info = (peripheral_info_s*)user_data;
+       peripheral_h gpio_handle = peripheral_handle_find(info->gpio_list, handle);
+       if (!gpio_handle) {
+               _E("Unable to find handle referenced by client");
+               ret = PERIPHERAL_ERROR_INVALID_PARAMETER;
+               goto out;
+       }
 
        g_bus_unwatch_name(gpio_handle->watch_id);
 
@@ -123,6 +129,7 @@ gboolean peripheral_gdbus_gpio_close(
        if (ret != PERIPHERAL_ERROR_NONE)
                _E("Failed to destroy gpio handle");
 
+out:
        peripheral_io_gdbus_gpio_complete_close(gpio, invocation, ret);
 
        return true;
index dab1d63ffedfebf81ae66b5670e4c8441601fd50..7003f373c3da1f1d8232053cd22af6236b1b2d7c 100644 (file)
@@ -83,7 +83,7 @@ gboolean peripheral_gdbus_i2c_open(
                        NULL);
 
 out:
-       peripheral_io_gdbus_i2c_complete_open(i2c, invocation, i2c_fd_list, GPOINTER_TO_UINT(i2c_handle), ret);
+       peripheral_io_gdbus_i2c_complete_open(i2c, invocation, i2c_fd_list, PERIPHERAL_H_TO_ID(i2c_handle), ret);
        peripheral_interface_i2c_fd_list_destroy(i2c_fd_list);
 
        return true;
@@ -92,12 +92,18 @@ out:
 gboolean peripheral_gdbus_i2c_close(
                PeripheralIoGdbusI2c *i2c,
                GDBusMethodInvocation *invocation,
-               gint handle,
+               guint handle,
                gpointer user_data)
 {
        int ret = PERIPHERAL_ERROR_NONE;
 
-       peripheral_h i2c_handle = GUINT_TO_POINTER(handle);
+       peripheral_info_s *info = (peripheral_info_s*)user_data;
+       peripheral_h i2c_handle = peripheral_handle_find(info->i2c_list, handle);
+       if (!i2c_handle) {
+               _E("Unable to find handle referenced by client");
+               ret = PERIPHERAL_ERROR_INVALID_PARAMETER;
+               goto out;
+       }
 
        g_bus_unwatch_name(i2c_handle->watch_id);
 
@@ -105,7 +111,8 @@ gboolean peripheral_gdbus_i2c_close(
        if (ret != PERIPHERAL_ERROR_NONE)
                _E("Failed to destroy i2c handle");
 
+out:
        peripheral_io_gdbus_i2c_complete_close(i2c, invocation, ret);
 
        return true;
-}
\ No newline at end of file
+}
index c2994a2130a2222b1b702d3d6ae3c069a5fc3973..51899bcaaf88a5976ca86b9fc1043c94d07b1d30 100644 (file)
@@ -95,7 +95,7 @@ gboolean peripheral_gdbus_pwm_open(
                        NULL);
 
 out:
-       peripheral_io_gdbus_pwm_complete_open(pwm, invocation, pwm_fd_list, GPOINTER_TO_UINT(pwm_handle), ret);
+       peripheral_io_gdbus_pwm_complete_open(pwm, invocation, pwm_fd_list, PERIPHERAL_H_TO_ID(pwm_handle), ret);
        peripheral_interface_pwm_fd_list_destroy(pwm_fd_list);
 
        return true;
@@ -104,12 +104,18 @@ out:
 gboolean peripheral_gdbus_pwm_close(
                PeripheralIoGdbusPwm *pwm,
                GDBusMethodInvocation *invocation,
-               gint handle,
+               guint handle,
                gpointer user_data)
 {
        int ret = PERIPHERAL_ERROR_NONE;
 
-       peripheral_h pwm_handle = GUINT_TO_POINTER(handle);
+       peripheral_info_s *info = (peripheral_info_s*)user_data;
+       peripheral_h pwm_handle = peripheral_handle_find(info->pwm_list, handle);
+       if (!pwm_handle) {
+               _E("Unable to find handle referenced by client");
+               ret = PERIPHERAL_ERROR_INVALID_PARAMETER;
+               goto out;
+       }
 
        g_bus_unwatch_name(pwm_handle->watch_id);
 
@@ -121,7 +127,8 @@ gboolean peripheral_gdbus_pwm_close(
        if (ret != PERIPHERAL_ERROR_NONE)
                _E("Failed to destroy pwm handle");
 
+out:
        peripheral_io_gdbus_pwm_complete_close(pwm, invocation, ret);
 
        return true;
-}
\ No newline at end of file
+}
index 96e0b9bbfcd4ba0ccfe803957f1d0702ebcea3cd..0ae835da03d07360295afc924ff948890d699ecf 100644 (file)
@@ -83,7 +83,7 @@ gboolean peripheral_gdbus_spi_open(
                        NULL);
 
 out:
-       peripheral_io_gdbus_spi_complete_open(spi, invocation, spi_fd_list, GPOINTER_TO_UINT(spi_handle), ret);
+       peripheral_io_gdbus_spi_complete_open(spi, invocation, spi_fd_list, PERIPHERAL_H_TO_ID(spi_handle), ret);
        peripheral_interface_spi_fd_list_destroy(spi_fd_list);
 
        return true;
@@ -92,12 +92,18 @@ out:
 gboolean peripheral_gdbus_spi_close(
                PeripheralIoGdbusSpi *spi,
                GDBusMethodInvocation *invocation,
-               gint handle,
+               guint handle,
                gpointer user_data)
 {
        int ret = PERIPHERAL_ERROR_NONE;
 
-       peripheral_h spi_handle = GUINT_TO_POINTER(handle);
+       peripheral_info_s *info = (peripheral_info_s*)user_data;
+       peripheral_h spi_handle = peripheral_handle_find(info->spi_list, handle);
+       if (!spi_handle) {
+               _E("Unable to find handle referenced by client");
+               ret = PERIPHERAL_ERROR_INVALID_PARAMETER;
+               goto out;
+       }
 
        g_bus_unwatch_name(spi_handle->watch_id);
 
@@ -105,7 +111,8 @@ gboolean peripheral_gdbus_spi_close(
        if (ret != PERIPHERAL_ERROR_NONE)
                _E("Failed to destroy spi handle");
 
+out:
        peripheral_io_gdbus_spi_complete_close(spi, invocation, ret);
 
        return true;
-}
\ No newline at end of file
+}
index fd6900660740096441a3f4e57ef6c209387b844d..9a4ecb6e13f057ecb24be8f3050bfe399df01c4b 100644 (file)
@@ -82,7 +82,7 @@ gboolean peripheral_gdbus_uart_open(
                        NULL);
 
 out:
-       peripheral_io_gdbus_uart_complete_open(uart, invocation, uart_fd_list, GPOINTER_TO_UINT(uart_handle), ret);
+       peripheral_io_gdbus_uart_complete_open(uart, invocation, uart_fd_list, PERIPHERAL_H_TO_ID(uart_handle), ret);
        peripheral_interface_uart_fd_list_destroy(uart_fd_list);
 
        return true;
@@ -91,12 +91,18 @@ out:
 gboolean peripheral_gdbus_uart_close(
                PeripheralIoGdbusUart *uart,
                GDBusMethodInvocation *invocation,
-               gint handle,
+               guint handle,
                gpointer user_data)
 {
        int ret = PERIPHERAL_ERROR_NONE;
 
-       peripheral_h uart_handle = GUINT_TO_POINTER(handle);
+       peripheral_info_s *info = (peripheral_info_s*)user_data;
+       peripheral_h uart_handle = peripheral_handle_find(info->uart_list, handle);
+       if (!uart_handle) {
+               _E("Unable to find handle referenced by client");
+               ret = PERIPHERAL_ERROR_INVALID_PARAMETER;
+               goto out;
+       }
 
        g_bus_unwatch_name(uart_handle->watch_id);
 
@@ -104,7 +110,8 @@ gboolean peripheral_gdbus_uart_close(
        if (ret != PERIPHERAL_ERROR_NONE)
                _E("Failed to destroy uart handle");
 
+out:
        peripheral_io_gdbus_uart_complete_close(uart, invocation, ret);
 
        return true;
-}
\ No newline at end of file
+}
index 4bb3b5eca1354350dd932b3520985f2af52b1817..e8c8598e90c222d91baf13e258cb71487f87d7ed 100644 (file)
 
 #include "peripheral_handle_common.h"
 
+gint cmp_handle(gconstpointer elem, gconstpointer _handle_id)
+{
+       peripheral_handle_s *ph = (peripheral_handle_s *)elem;
+       uint32_t handle_id = GPOINTER_TO_UINT(_handle_id);
+
+       return ph->handle_id != handle_id;
+}
+
 peripheral_h peripheral_handle_new(GList **plist)
 {
        GList *list = *plist;
@@ -28,12 +36,21 @@ peripheral_h peripheral_handle_new(GList **plist)
                _E("failed to allocate peripheral_handle_s");
                return NULL;
        }
+       do {
+               handle->handle_id = random();
+       } while (handle->handle_id == 0 || g_list_find_custom(list, GUINT_TO_POINTER(handle->handle_id), cmp_handle) != NULL);
 
        *plist = g_list_append(list, handle);
 
        return handle;
 }
 
+peripheral_h peripheral_handle_find(GList *list, uint32_t handle_id)
+{
+       GList *elem = g_list_find_custom(list, GUINT_TO_POINTER(handle_id), cmp_handle);
+       return elem ? elem->data : NULL;
+}
+
 int peripheral_handle_free(peripheral_h handle)
 {
        GList *list = *handle->list;
@@ -53,4 +70,4 @@ int peripheral_handle_free(peripheral_h handle)
        g_list_free(link);
 
        return 0;
-}
\ No newline at end of file
+}