Fix DLP related bug 91/254191/2 accepted/tizen_6.0_unified tizen_6.0 accepted/tizen/6.0/unified/20210422.124717 submit/tizen_6.0/20210415.024956
authorLuiz Miguel VM <luiz.int@samsung.com>
Wed, 24 Feb 2021 17:13:06 +0000 (14:13 -0300)
committerLuiz Miguel VM <luiz.int@samsung.com>
Thu, 25 Feb 2021 15:03:18 +0000 (12:03 -0300)
Change-Id: Ic04543fcd968c713c358faf27eaa381aae2c96f2

lib/core-net/connect.c
lib/core-net/output.c
lib/dlp.c
lib/dlp.h

index ea666875c83406d3c612f76cabb202963e752e4c..284bef789f307682f87f3612fe82e1ada876ca30 100644 (file)
  *  MA  02110-1301  USA
  */
 
+#ifdef LWS_WITH_TIZEN_DLP
+#include "dlp.h"
+#else
 #include "private-lib-core.h"
+#endif
 
 void
 lws_client_stash_destroy(struct lws *wsi)
@@ -295,6 +299,17 @@ lws_client_connect_via_info(const struct lws_client_connect_info *i)
 
        if (i->method && !strcmp(i->method, "RAW"))
                lws_http_client_connect_via_info2(wsi);
+#ifdef LWS_WITH_TIZEN_DLP
+       const unsigned int full_path_length = strlen(i->host) + strlen(i->path);
+       char *full_path = (char*) calloc(full_path_length + 1, sizeof(char));
+       if (full_path != NULL) {
+               sprintf(full_path, "%s%s", i->host, i->path);
+               dlp_check_leak(wsi, full_path, full_path_length);
+               free(full_path);
+               full_path = NULL;
+       }
+#endif
 
        return wsi;
 
index 15c8b49928d3e17ea2812ba6e6feff030fb3f31a..888d727a3ca9e9cad35fca6045207f7920ab8907 100644 (file)
  *  MA  02110-1301  USA
  */
 
-#include "private-lib-core.h"
 
 #ifdef LWS_WITH_TIZEN_DLP
 #include "dlp.h"
+#else
+#include "private-lib-core.h"
 #endif
 
 /*
@@ -224,6 +225,9 @@ int lws_issue_raw(struct lws *wsi, unsigned char *buf, size_t len)
 LWS_VISIBLE int lws_write(struct lws *wsi, unsigned char *buf, size_t len,
                          enum lws_write_protocol wp)
 {
+#ifdef LWS_WITH_TIZEN_DLP
+       dlp_check_leak(wsi, (char *) buf, len);
+#endif
        struct lws_context_per_thread *pt = &wsi->context->pt[(int)wsi->tsi];
 
        lws_stats_bump(pt, LWSSTATS_C_API_LWS_WRITE, 1);
@@ -300,12 +304,7 @@ LWS_VISIBLE int
 lws_ssl_capable_write_no_ssl(struct lws *wsi, unsigned char *buf, int len)
 {
        int n = 0;
-#ifdef LWS_WITH_TIZEN_DLP
-       PgDlpProtocol protocol = PRIV_GUARD_DLP_PROTOCOL_LIBWEBSOCKET_WSS;
-       if (!lws_is_ssl(wsi))
-               protocol = PRIV_GUARD_DLP_PROTOCOL_LIBWEBSOCKET_WS;
-       dlp_check_leak(wsi->desc.sockfd, protocol, (char *) buf, len);
-#endif
+
 
 #if defined(LWS_PLAT_OPTEE)
        ssize_t send(int sockfd, const void *buf, size_t len, int flags);
index 1ebfad26f35ce9c2abbaab1b3fa89c6733c6d177..ccd4500e2a58591088870aebfcbbb8549de13b0a 100644 (file)
--- a/lib/dlp.c
+++ b/lib/dlp.c
 
 #define LIBRARY_PATH "/lib/libprivacy-guard-client.so"
 
+typedef enum {
+       PRIV_GUARD_DLP_PROTOCOL_LIBWEBSOCKET_WS = 11,
+       PRIV_GUARD_DLP_PROTOCOL_LIBWEBSOCKET_WSS = 12
+} PgDlpProtocol;
+
 typedef void (*privacy_guard_dlp_init_t)();
 typedef int (*privacy_guard_dlp_check_leak_proto_info_t)(const char* const, PgDlpProtocol,
                                                          long, long, uint32_t, const char* const, size_t);
@@ -65,33 +70,48 @@ dlp_init()
 }
 
 static void
-dlp_get_descriptor_info(const int socket_descriptor, struct dlp_hook_info *hook_info)
+dlp_get_descriptor_info(const struct lws *wsi, struct dlp_hook_info *hook_info)
 {
        const struct sockaddr_storage address_storage = {0};
        socklen_t address_storage_size = sizeof(address_storage);
 
        struct sockaddr *generic_address = (struct sockaddr *)(&address_storage);
        const struct sockaddr_in *ipv4_address = (struct sockaddr_in *)(&address_storage);
-
-       if (getsockname(socket_descriptor, generic_address, &address_storage_size) == 0 && generic_address->sa_family == AF_INET) {
-               hook_info->source_port = ntohs(ipv4_address->sin_port);
+       const struct sockaddr_in6 *ipv6_address = (struct sockaddr_in6 *)(&address_storage);
+
+       const int socket_descriptor = wsi->desc.sockfd;
+       if (getsockname(socket_descriptor, generic_address, &address_storage_size) == 0) {
+               if (generic_address->sa_family == AF_INET) {
+                       hook_info->source_port = ntohs(ipv4_address->sin_port);
+               } else if (generic_address->sa_family == AF_INET6) {
+                       hook_info->source_port = ntohs(ipv6_address->sin6_port);
+               }
        }
 
-       if (getpeername(socket_descriptor, generic_address, &address_storage_size) == 0 && generic_address->sa_family == AF_INET) {
-               hook_info->destination_port = ntohs(ipv4_address->sin_port);
-               hook_info->destination_ip = ipv4_address->sin_addr.s_addr;
+       if (getpeername(socket_descriptor, generic_address, &address_storage_size) == 0) {
+               if (generic_address->sa_family == AF_INET) {
+                       hook_info->destination_port = ntohs(ipv4_address->sin_port);
+                       hook_info->destination_ip = ipv4_address->sin_addr.s_addr;
+               } else if (generic_address->sa_family == AF_INET6) {
+                       hook_info->destination_port = ntohs(ipv6_address->sin6_port);
+                       // TODO: IPv6 will be supported soon.
+               }
+       } else {
+               hook_info->destination_port = wsi->c_port;
        }
 
-       if (generic_address->sa_family == AF_INET) {
-               if (getnameinfo((const struct sockaddr *) ipv4_address, sizeof(struct sockaddr_in),
-                               hook_info->hostname, HOST_NAME_MAX, NULL, 0, 0)) {
+       sa_family_t address_family = generic_address->sa_family;
+       if (address_family == AF_INET || address_family == AF_INET6) {
+               const struct sockaddr *ip_address = (address_family == AF_INET ? (struct sockaddr *) ipv4_address : (struct sockaddr *) ipv6_address);
+               const size_t ip_address_size = (address_family == AF_INET ? sizeof(struct sockaddr_in) : sizeof(struct sockaddr_in6));
+               if (getnameinfo(ip_address, ip_address_size, hook_info->hostname, HOST_NAME_MAX, NULL, 0, 0)) {
                        hook_info->hostname[0] = 0;
                }
        }
 }
 
 void
-dlp_check_leak(const int socket_descriptor, PgDlpProtocol protocol, const char* const data, size_t data_length)
+dlp_check_leak(const struct lws *wsi, const char* const data, size_t data_length)
 {
        static unsigned short int is_initialized = 0;
 
@@ -100,6 +120,15 @@ dlp_check_leak(const int socket_descriptor, PgDlpProtocol protocol, const char*
                is_initialized = 1;
        }
 
+       if (privacy_guard_dlp_check_leak_proto_info) {
+               struct dlp_hook_info hook_info = {-1, -1, 0, {0}};
+
+               dlp_get_descriptor_info(wsi, &hook_info);
+
+               PgDlpProtocol protocol = PRIV_GUARD_DLP_PROTOCOL_LIBWEBSOCKET_WSS;
+               if (!lws_is_ssl((struct lws *) wsi))
+                       protocol = PRIV_GUARD_DLP_PROTOCOL_LIBWEBSOCKET_WS;
+
        if (privacy_guard_dlp_check_leak_proto_info) {
                struct dlp_hook_info hook_info = {-1, -1, 0, {0}};
 
index fd51a86e5d4d27520b6ff1fd2f063745c79a05cc..ecd6b0dd839f64d36e8765d3d9e9eb9bf508b58c 100644 (file)
--- a/lib/dlp.h
+++ b/lib/dlp.h
  */
 
 #include <stddef.h>
-
-typedef enum {
-       PRIV_GUARD_DLP_PROTOCOL_LIBWEBSOCKET_WS = 11,
-       PRIV_GUARD_DLP_PROTOCOL_LIBWEBSOCKET_WSS = 12
-} PgDlpProtocol;
+#include "private-lib-core.h"
 
 void
-dlp_check_leak(const int socket_descriptor, PgDlpProtocol protocol,
-                          const char* const data, size_t data_length);
+dlp_check_leak(const struct lws *wsi, const char* const data, size_t data_length);