From 186bea594d970b4d1dda03a8ca288df26fbc76d3 Mon Sep 17 00:00:00 2001 From: Sooyoung Ha Date: Mon, 12 Jun 2017 19:50:04 +0900 Subject: [PATCH 01/16] source: avoid potential buffer overflow of scanf Change-Id: I853dac8153041dc1786bb58f2058f31cba0a56e3 Signed-off-by: Sooyoung Ha --- src/default_plugin_appcmd.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/default_plugin_appcmd.c b/src/default_plugin_appcmd.c index 7c0405e..2302386 100644 --- a/src/default_plugin_appcmd.c +++ b/src/default_plugin_appcmd.c @@ -632,9 +632,9 @@ static void appcmd_receiver_packageinfo(int fd_in, int fd_out) } if (!strncmp(buf, "mainappid : ", 12)) { - sscanf(buf, "mainappid : %s", mainapp_id); + sscanf(buf, "mainappid : %127s", mainapp_id); } else if (!strncmp(buf, "Type: ", 6)) { - sscanf(buf, "Type: %s", type); + sscanf(buf, "Type: %127s", type); } else if (!strncmp(buf, "Removable: ", 11)) { sscanf(buf, "Removable: %d", &is_removable); } else if (strstr(buf, " is Running") != NULL) { -- 2.7.4 From 981f183b8b4f9cf8aa6b8adc5c76f2589592dc9d Mon Sep 17 00:00:00 2001 From: Sooyoung Ha Date: Mon, 12 Jun 2017 20:17:09 +0900 Subject: [PATCH 02/16] package: update version (3.0.28) Change-Id: I040d55035076328d3317c47a92019f60fa86782b Signed-off-by: Sooyoung Ha --- packaging/sdbd.spec | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packaging/sdbd.spec b/packaging/sdbd.spec index 130e95c..7ecd2ff 100644 --- a/packaging/sdbd.spec +++ b/packaging/sdbd.spec @@ -2,7 +2,7 @@ Name: sdbd Summary: SDB daemon -Version: 3.0.27 +Version: 3.0.28 Release: 0 License: Apache-2.0 Summary: SDB daemon -- 2.7.4 From 16774a1a86387718aa971249a464af99fd3e0053 Mon Sep 17 00:00:00 2001 From: Jinhyung Jo Date: Wed, 21 Jun 2017 20:18:48 +0900 Subject: [PATCH 03/16] source: remove build warning change format specifier for 'size_t': %d -> %zu, %x -> %zx remove -Wint-to-pointer-cast, -Wpointer-to-int-cast apply indentation to 'if' statement Change-Id: Id58a213d6ce8a8b43d19c2789327ec7b2716ab93 Signed-off-by: Jinhyung Jo --- src/default_plugin_appcmd.c | 24 ++++++++++++------------ src/sdb.c | 4 ++-- src/services.c | 6 +++--- src/sockets.c | 2 +- src/transport.c | 12 ++++++------ src/transport_local.c | 6 +++--- src/usb_funcfs_client.c | 2 +- src/usb_linux_client.c | 2 +- 8 files changed, 29 insertions(+), 29 deletions(-) diff --git a/src/default_plugin_appcmd.c b/src/default_plugin_appcmd.c index 2302386..8ffb4e3 100644 --- a/src/default_plugin_appcmd.c +++ b/src/default_plugin_appcmd.c @@ -68,7 +68,7 @@ static int appcmd_install_gen_shellcmd(appcmd_info* p_info) { int len = sizeof(p_info->shell_cmd); if (p_info->args_cnt != 5) { - D("failed to parse appcmd.(cnt=%d)\n", p_info->args_cnt); + D("failed to parse appcmd.(cnt=%zu)\n", p_info->args_cnt); return -1; } @@ -101,7 +101,7 @@ static int appcmd_uninstall_gen_shellcmd(appcmd_info* p_info) { int len = sizeof(p_info->shell_cmd); if (p_info->args_cnt != 2) { - D("failed to parse appcmd.(cnt=%d)\n", p_info->args_cnt); + D("failed to parse appcmd.(cnt=%zu)\n", p_info->args_cnt); return -1; } @@ -120,7 +120,7 @@ static int appcmd_runapp_gen_shellcmd(appcmd_info* p_info) { int len = sizeof(p_info->shell_cmd); if (p_info->args_cnt != 2) { - D("failed to parse appcmd.(cnt=%d)\n", p_info->args_cnt); + D("failed to parse appcmd.(cnt=%zu)\n", p_info->args_cnt); return -1; } @@ -150,7 +150,7 @@ static int appcmd_rununittestapp_gen_shellcmd(appcmd_info* p_info) { p_service = strtok_r(p_info->raw_command, ":", &ptr); p_appid = strtok_r(NULL, ":", &ptr); if (p_service == NULL || p_appid == NULL) { - D("failed to parse appcmd.(cnt=%d)\n", p_info->args_cnt); + D("failed to parse appcmd.(cnt=%zu)\n", p_info->args_cnt); return -1; } @@ -175,7 +175,7 @@ static int appcmd_killapp_gen_shellcmd(appcmd_info* p_info) { int len = sizeof(p_info->shell_cmd); if (p_info->args_cnt != 2) { - D("failed to parse appcmd.(cnt=%d)\n", p_info->args_cnt); + D("failed to parse appcmd.(cnt=%zu)\n", p_info->args_cnt); return -1; } @@ -194,7 +194,7 @@ static int appcmd_packagelist_gen_shellcmd(appcmd_info* p_info) { int len = sizeof(p_info->shell_cmd); if (p_info->args_cnt != 2) { - D("failed to parse appcmd.(cnt=%d)\n", p_info->args_cnt); + D("failed to parse appcmd.(cnt=%zu)\n", p_info->args_cnt); return -1; } @@ -213,7 +213,7 @@ static int appcmd_debugwebapp_gen_shellcmd(appcmd_info* p_info) { int len = sizeof(p_info->shell_cmd); if (p_info->args_cnt != 2) { - D("failed to parse appcmd.(cnt=%d)\n", p_info->args_cnt); + D("failed to parse appcmd.(cnt=%zu)\n", p_info->args_cnt); return -1; } @@ -236,7 +236,7 @@ static int appcmd_debugnativeapp_gen_shellcmd(appcmd_info* p_info) { int len = sizeof(p_info->shell_cmd); if (p_info->args_cnt != 5) { - D("failed to parse appcmd.(cnt=%d)\n", p_info->args_cnt); + D("failed to parse appcmd.(cnt=%zu)\n", p_info->args_cnt); return -1; } @@ -264,7 +264,7 @@ static int appcmd_appinfo_gen_shellcmd(appcmd_info* p_info) { int len = sizeof(p_info->shell_cmd); if (p_info->args_cnt != 2) { - D("failed to parse appcmd.(cnt=%d)\n", p_info->args_cnt); + D("failed to parse appcmd.(cnt=%zu)\n", p_info->args_cnt); return -1; } @@ -571,7 +571,7 @@ static void run_appcmd_packageinfo(appcmd_info* p_info) { p_info->exitcode = -1; if (p_info->args_cnt != 3) { - D("failed to parse appcmd.(cnt=%d)\n", p_info->args_cnt); + D("failed to parse appcmd.(cnt=%zu)\n", p_info->args_cnt); return; } @@ -596,7 +596,7 @@ static int appcmd_packageinfo_gen_shellcmd(appcmd_info* p_info) { int len = sizeof(p_info->shell_cmd); if (p_info->args_cnt != 2) { - D("failed to parse appcmd.(cnt=%d)\n", p_info->args_cnt); + D("failed to parse appcmd.(cnt=%zu)\n", p_info->args_cnt); return -1; } @@ -714,7 +714,7 @@ int appcmd_service( parameters* in, int out_fd ) { /* appcmd parameter data map * "service name:arg1:arg2:...:argN" */ info.args_cnt = tokenize(command, ":", info.args, MAX_TOKENS); - D("args_cnt=%d\n", info.args_cnt); + D("args_cnt=%zu\n", info.args_cnt); if (info.args_cnt < 1) { D("failed to parse appcmd for install. (%s)\n", command); info.exitcode = -1; diff --git a/src/sdb.c b/src/sdb.c index fd9114c..a83c3f7 100644 --- a/src/sdb.c +++ b/src/sdb.c @@ -791,7 +791,7 @@ static void update_version(atransport *t, int version, size_t payload) #endif t->protocol_version = min(version, A_VERSION); t->max_payload = min(payload, max_payload); - D("update transport version. version=%x, max_payload=%d\n", t->protocol_version, t->max_payload); + D("update transport version. version=%x, max_payload=%zu\n", t->protocol_version, t->max_payload); } void handle_packet(apacket *p, atransport *t) @@ -1398,7 +1398,7 @@ static int sdbd_set_groups(const char *name, int gid, struct group_info default_ D("group list : ngroups = %d\n", ngroups); group_ids = malloc((ngroups + default_groups_size) * sizeof(gid_t)); if (group_ids == NULL) { - D("failed to allocate group_ids(%d)\n", (ngroups + default_groups_size) * sizeof(gid_t)); + D("failed to allocate group_ids(%zu)\n", (ngroups + default_groups_size) * sizeof(gid_t)); return -1; } if (getgrouplist(name, gid, group_ids, &ngroups) == -1) { diff --git a/src/services.c b/src/services.c index c12a8d7..6c9d1d6 100644 --- a/src/services.c +++ b/src/services.c @@ -578,7 +578,7 @@ int create_userprocess(const char *cmd, pid_t *pid, char * const argv[], char * static void subproc_waiter_service(int fd, void *cookie) { - pid_t pid = (pid_t)cookie; + pid_t pid = (pid_t)((intptr_t)cookie); D("entered. fd=%d of pid=%d\n", fd, pid); for (;;) { @@ -820,7 +820,7 @@ static int create_subproc_thread(const char *name, int lines, int columns) sti = malloc(sizeof(stinfo)); if(sti == 0) fatal("cannot allocate stinfo"); sti->func = subproc_waiter_service; - sti->cookie = (void*)pid; + sti->cookie = (void*)((intptr_t)pid); sti->fd = ret_fd; if(sdb_thread_create( &t, service_bootstrap_func, sti)){ @@ -866,7 +866,7 @@ static int create_sync_subprocess(void (*func)(int, void *), void* cookie) { sti = malloc(sizeof(stinfo)); if(sti == 0) fatal("cannot allocate stinfo"); sti->func = subproc_waiter_service; - sti->cookie = (void*)pid; + sti->cookie = (void*)((intptr_t)pid); sti->fd = s[0]; if(sdb_thread_create( &t, service_bootstrap_func, sti)){ diff --git a/src/sockets.c b/src/sockets.c index f27affa..b81d6a3 100644 --- a/src/sockets.c +++ b/src/sockets.c @@ -335,7 +335,7 @@ static void local_socket_event_func(int fd, unsigned ev, void *_s) while(avail > 0) { r = sdb_read(fd, x, avail); - D("LS(%d): post sdb_read(fd=%d,...) r=%d (errno=%d) avail=%d\n", s->id, s->fd, r, r<0?errno:0, avail); + D("LS(%d): post sdb_read(fd=%d,...) r=%d (errno=%d) avail=%zu\n", s->id, s->fd, r, r<0?errno:0, avail); if(r > 0 && r <= avail) { avail -= r; x += r; diff --git a/src/transport.c b/src/transport.c index 152a22a..855c02d 100644 --- a/src/transport.c +++ b/src/transport.c @@ -670,8 +670,8 @@ atransport *acquire_one_transport(int state, transport_type ttype, const char* s sdb_mutex_lock(&transport_lock); for (t = transport_list.next; t != &transport_list; t = t->next) { if (t->connection_state == CS_NOPERM) { - if (error_out) - *error_out = "insufficient permissions for device"; + if (error_out) + *error_out = "insufficient permissions for device"; continue; } @@ -875,7 +875,7 @@ int readx(int fd, void *ptr, size_t len) #if SDB_TRACE size_t len0 = len; #endif - D("readx: fd=%d wanted=%d\n", fd, len); + D("readx: fd=%d wanted=%zu\n", fd, len); while(len > 0) { r = sdb_read(fd, p, len); if(r > 0 && r <= len) { @@ -894,7 +894,7 @@ int readx(int fd, void *ptr, size_t len) } #if SDB_TRACE - D("readx: fd=%d wanted=%d got=%d\n", fd, len0, len0 - len); + D("readx: fd=%d wanted=%zu got=%zu\n", fd, len0, len0 - len); dump_hex( ptr, len0 ); #endif return 0; @@ -906,7 +906,7 @@ int writex(int fd, const void *ptr, size_t len) int r; #if SDB_TRACE - D("writex: fd=%d len=%d: ", fd, len); + D("writex: fd=%d len=%zu: ", fd, len); dump_hex( ptr, len ); #endif while(len > 0) { @@ -936,7 +936,7 @@ int check_header(apacket *p, atransport *t) } if(p->msg.data_length > t->max_payload) { - D("check_header(): %d > transport->max_payload(%d)\n", p->msg.data_length, t->max_payload); + D("check_header(): %d > transport->max_payload(%zu)\n", p->msg.data_length, t->max_payload); return -1; } diff --git a/src/transport_local.c b/src/transport_local.c index 01f5c6c..d1b569e 100644 --- a/src/transport_local.c +++ b/src/transport_local.c @@ -146,7 +146,7 @@ static void *server_socket_thread(void * arg) int serverfd, fd; struct sockaddr_in addr; socklen_t alen; - int port = (int)arg; + int port = (intptr_t)arg; D("transport: server_socket_thread() starting\n"); serverfd = -1; @@ -469,7 +469,7 @@ static void* notify_sdbd_startup_thread(void* ptr) { snprintf(request, sizeof request, "host:connect:%s:%d", guest_ip, DEFAULT_SDB_LOCAL_TRANSPORT_PORT); } D("[%s:%d] request:%s \n", __FUNCTION__, __LINE__, request); - snprintf(buffer, sizeof buffer, "%04x%s", strlen(request), request ); + snprintf(buffer, sizeof buffer, "%04zx%s", strlen(request), request ); if (send_msg_to_localhost_from_guest(host_ip, DEFAULT_SDB_PORT, buffer, 0) <0) { D("could not send sdbd noti request. it might sdb server has not been started yet.\n"); @@ -501,7 +501,7 @@ void local_init(int port) D("transport: local %s init\n", HOST ? "client" : "server"); - if(sdb_thread_create(&thr, func, (void *)port)) { + if(sdb_thread_create(&thr, func, (void *)((intptr_t)port))) { fatal_errno("cannot create local socket %s thread", HOST ? "client" : "server"); } diff --git a/src/usb_funcfs_client.c b/src/usb_funcfs_client.c index 241d368..3ca92a3 100644 --- a/src/usb_funcfs_client.c +++ b/src/usb_funcfs_client.c @@ -554,7 +554,7 @@ int ffs_usb_read(usb_handle *h, void *data, size_t len) { int n; - D("%d: about to read (fd=%d, len=%d)\n", getpid(), h->bulk_out, len); + D("%d: about to read (fd=%d, len=%zu)\n", getpid(), h->bulk_out, len); n = bulk_read(h->bulk_out, data, len); if(n != len) { D("ERROR: fd = %d, n = %d, errno = %d\n", diff --git a/src/usb_linux_client.c b/src/usb_linux_client.c index ecea71a..ea9d7e6 100644 --- a/src/usb_linux_client.c +++ b/src/usb_linux_client.c @@ -98,7 +98,7 @@ int linux_usb_write(usb_handle *h, const void *data, int len) int linux_usb_read(usb_handle *h, void *data, size_t len) { - D("about to read (fd=%d, len=%d)\n", h->fd, len); + D("about to read (fd=%d, len=%zu)\n", h->fd, len); while (len > 0) { /* The sdb_read does not support read larger than 4096 bytes at once. Read 4096 byte block repeatedly when reading data is larger than 4096 bytes. */ -- 2.7.4 From 5ae69c8554258b1ffd96ae597111209a6053c40c Mon Sep 17 00:00:00 2001 From: Jinhyung Jo Date: Wed, 21 Jun 2017 20:28:48 +0900 Subject: [PATCH 04/16] source: remove integer underflow Change-Id: Ie6987bb833fa1061409ccd3a8b4abddf4e281cc7 Signed-off-by: Jinhyung Jo --- src/sockets.c | 23 +++++++++++------------ src/transport.c | 17 ++++++++--------- src/usb_linux_client.c | 35 ++++++++++++++++++----------------- 3 files changed, 37 insertions(+), 38 deletions(-) diff --git a/src/sockets.c b/src/sockets.c index b81d6a3..9711613 100644 --- a/src/sockets.c +++ b/src/sockets.c @@ -324,37 +324,36 @@ static void local_socket_event_func(int fd, unsigned ev, void *_s) // sdbd에서 패킷 데이터를 MAX_PAYLOAD - 200로 잡아서 암호화 하게되면 // 최대 MAX_PAYLOAD - 100 크기의 패킷을 생성하게 됨. const size_t max_payload = asock_get_max_payload(s) - 200; - size_t avail = max_payload; #else const size_t max_payload = asock_get_max_payload(s); - size_t avail = max_payload; #endif - + int avail = max_payload; int r = 0; int is_eof = 0; - while(avail > 0) { + while (avail > 0) { r = sdb_read(fd, x, avail); - D("LS(%d): post sdb_read(fd=%d,...) r=%d (errno=%d) avail=%zu\n", s->id, s->fd, r, r<0?errno:0, avail); - if(r > 0 && r <= avail) { + D("LS(%d): post sdb_read(fd=%d,...) r=%d (errno=%d) avail=%d\n", + s->id, s->fd, r, r < 0 ? errno : 0, avail); + if (r > 0 && r <= avail) { avail -= r; x += r; continue; } - if(r < 0) { - if(errno == EAGAIN) break; - if(errno == EINTR) continue; + if (r < 0) { + if (errno == EAGAIN) break; + if (errno == EINTR) continue; } - /* r = 0 or unhandled error */ + /* r = 0 or unhandled error */ is_eof = 1; break; } D("LS(%d): fd=%d post avail loop. r=%d is_eof=%d forced_eof=%d\n", s->id, s->fd, r, is_eof, s->fde.force_eof); - //변경된 최대 패킷 크기로 코드 수정 - if((avail == max_payload) || (s->peer == 0)) { + //변경된 최대 패킷 크기로 코드 수정 + if ((avail == max_payload) || (s->peer == 0)) { put_apacket(p); } else { p->len = max_payload >= avail ? max_payload - avail : 0; diff --git a/src/transport.c b/src/transport.c index 855c02d..7eb9e95 100644 --- a/src/transport.c +++ b/src/transport.c @@ -434,7 +434,7 @@ static int transport_read_action(int fd, struct tmsg* m) { char *p = (char*)m; - size_t len = sizeof(*m); + int len = sizeof(*m); int r; while (len > 0) { @@ -872,14 +872,13 @@ int readx(int fd, void *ptr, size_t len) { char *p = ptr; int r; -#if SDB_TRACE - size_t len0 = len; -#endif + int l = len; + D("readx: fd=%d wanted=%zu\n", fd, len); - while(len > 0) { + while (l > 0) { r = sdb_read(fd, p, len); - if(r > 0 && r <= len) { - len -= r; + if (r > 0 && r <= l) { + l -= r; p += r; } else { if (r < 0) { @@ -894,8 +893,8 @@ int readx(int fd, void *ptr, size_t len) } #if SDB_TRACE - D("readx: fd=%d wanted=%zu got=%zu\n", fd, len0, len0 - len); - dump_hex( ptr, len0 ); + D("readx: fd=%d wanted=%zu got=%zu\n", fd, len, len - l); + dump_hex( ptr, len ); #endif return 0; } diff --git a/src/usb_linux_client.c b/src/usb_linux_client.c index ea9d7e6..a4840c9 100644 --- a/src/usb_linux_client.c +++ b/src/usb_linux_client.c @@ -98,25 +98,26 @@ int linux_usb_write(usb_handle *h, const void *data, int len) int linux_usb_read(usb_handle *h, void *data, size_t len) { - D("about to read (fd=%d, len=%zu)\n", h->fd, len); - while (len > 0) { - /* The sdb_read does not support read larger than 4096 bytes at once. - Read 4096 byte block repeatedly when reading data is larger than 4096 bytes. */ - int bytes_to_read = len < 4096 ? len : 4096; - int n = sdb_read(h->fd, data, bytes_to_read); - if(n < 0) { - if(errno == EINTR) { - continue; - } else { - D("ERROR: fd = %d, n = %d, errno = %d\n", h->fd, n, errno); - return -1; - } + D("about to read (fd=%d, len=%zu)\n", h->fd, len); + int l = len; + while (l > 0) { + /* The sdb_read does not support read larger than 4096 bytes at once. + Read 4096 byte block repeatedly when reading data is larger than 4096 bytes. */ + int bytes_to_read = l < 4096 ? l : 4096; + int n = sdb_read(h->fd, data, bytes_to_read); + if (n < 0) { + if (errno == EINTR) { + continue; + } else { + D("ERROR: fd = %d, n = %d, errno = %d\n", h->fd, n, errno); + return -1; } - len = len >= n ? len - n : 0; - data = ((char*) data) + n; } - D("[ done fd=%d ]\n", h->fd); - return 0; + l = (l >= n) ? l - n : 0; + data = ((char*)data) + n; + } + D("[ done fd=%d ]\n", h->fd); + return 0; } void linux_usb_init() -- 2.7.4 From 112c62399fc25c810de70bce171f54fefe5a0b51 Mon Sep 17 00:00:00 2001 From: Jinhyung Jo Date: Thu, 22 Jun 2017 17:15:06 +0900 Subject: [PATCH 05/16] package: update version (3.0.29) Change-Id: I99d417e1d2a513506f084b59c219466d14b98e4f Signed-off-by: Jinhyung Jo --- packaging/sdbd.spec | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packaging/sdbd.spec b/packaging/sdbd.spec index 7ecd2ff..8f5efed 100644 --- a/packaging/sdbd.spec +++ b/packaging/sdbd.spec @@ -2,7 +2,7 @@ Name: sdbd Summary: SDB daemon -Version: 3.0.28 +Version: 3.0.29 Release: 0 License: Apache-2.0 Summary: SDB daemon -- 2.7.4 From 26a2f3133488b8be6318540e5405a20d1c766ef9 Mon Sep 17 00:00:00 2001 From: Sooyoung Ha Date: Tue, 27 Jun 2017 22:31:06 +0900 Subject: [PATCH 06/16] dbus: add dbus_connection_unref Change-Id: Ic7c085a06da97f40c570b7be116a1236d5b222da Signed-off-by: Sooyoung Ha --- src/sdb.c | 1 + 1 file changed, 1 insertion(+) diff --git a/src/sdb.c b/src/sdb.c index a83c3f7..39ea547 100644 --- a/src/sdb.c +++ b/src/sdb.c @@ -1373,6 +1373,7 @@ static void *bootdone_cb(void *x) { g_main_loop_run(mainloop); D("dbus loop exited"); + dbus_connection_unref(bus); return NULL; } -- 2.7.4 From 01531ec45d3292cd266f41e3812e240d02d6c585 Mon Sep 17 00:00:00 2001 From: Sooyoung Ha Date: Tue, 27 Jun 2017 22:32:18 +0900 Subject: [PATCH 07/16] source: align indent I change the tabs to spaces and remove the redundant spaces. Change-Id: Iaed7c815f2d9e04582b1a68bc65d678261824378 Signed-off-by: Sooyoung Ha --- src/TizenConfig.h | 2 +- src/commandline_sdbd.c | 198 ++++++++--------- src/commandline_sdbd.h | 12 +- src/default_plugin_auth.c | 1 - src/fdevent.h | 3 +- src/file_sync_service.h | 4 +- src/init.c | 198 ++++++++--------- src/plugin_encrypt.c | 476 ++++++++++++++++++++--------------------- src/plugin_encrypt.h | 30 +-- src/sdb.c | 394 +++++++++++++++++----------------- src/sdb.h | 6 +- src/services.c | 2 +- src/socket_inaddr_any_server.c | 2 +- src/socket_local.h | 8 +- src/socket_local_server.c | 4 +- src/socket_loopback_server.c | 2 +- src/sockets.c | 10 +- src/sockets.h | 44 ++-- src/transport.c | 24 +-- src/transport_local.c | 8 +- src/transport_usb.c | 2 +- src/usb_funcfs_client.c | 78 +++---- src/utils.c | 152 ++++++------- 23 files changed, 829 insertions(+), 831 deletions(-) diff --git a/src/TizenConfig.h b/src/TizenConfig.h index 81d36f3..7da4f99 100644 --- a/src/TizenConfig.h +++ b/src/TizenConfig.h @@ -15,7 +15,7 @@ */ /* - * Android config -- "CYGWIN_NT-5.1". + * Android config -- "CYGWIN_NT-5.1". * * Cygwin has pthreads, but GDB seems to get confused if you use it to * create threads. By "confused", I mean it freezes up the first time the diff --git a/src/commandline_sdbd.c b/src/commandline_sdbd.c index 63850f3..eee6ac2 100644 --- a/src/commandline_sdbd.c +++ b/src/commandline_sdbd.c @@ -53,115 +53,115 @@ static void print_sdbd_command(FILE *stream, SdbdCommandlineArgs *sdbd_args) { } int parse_sdbd_commandline(SdbdCommandlineArgs *sdbd_args, int argc, char *argv[]) { - int split_retval; - - int opt; - int long_index = 0; - - static struct option long_options[] = { - { ARG_EMULATOR_VM_NAME, required_argument, NULL, ARG_S_EMULATOR_VM_NAME }, - { ARG_SENSORS, required_argument, NULL, ARG_S_SENSORS }, - { ARG_SDB, required_argument, NULL, ARG_S_SDB }, - { ARG_SDBD_LISTEN_PORT, required_argument, NULL, ARG_S_SDBD_LISTEN_PORT }, - { ARG_HELP, no_argument, NULL, ARG_S_HELP }, - { ARG_USAGE, no_argument, NULL, ARG_S_USAGE }, - { NULL, 0, NULL, 0 } - }; - - optind = 1; /* the index of the next element to be processed in argv */ - - while ((opt = getopt_long(argc, argv, "", long_options, &long_index)) != -1) { - switch (opt) { - case ARG_S_EMULATOR_VM_NAME: - split_retval = split_host_port(optarg, - &sdbd_args->emulator.host, - &sdbd_args->emulator.port); - if (split_retval != SDBD_COMMANDLINE_SUCCESS) { - return split_retval; - } - /* if we are on emulator we listen using local transport - * so we should set port to default value but this can - * be overwritten by command line options */ - if (sdbd_args->sdbd_port < 0) { - sdbd_args->sdbd_port = DEFAULT_SDB_LOCAL_TRANSPORT_PORT; - } - print_sdbd_command(stdout, sdbd_args); - break; - case ARG_S_SENSORS: - split_retval = split_host_port(optarg, - &sdbd_args->sensors.host, - &sdbd_args->sensors.port); - if (split_retval != SDBD_COMMANDLINE_SUCCESS) { - return split_retval; - } - print_sdbd_command(stdout, sdbd_args); - break; - case ARG_S_SDB: - split_retval = split_host_port(optarg, - &sdbd_args->sdb.host, - &sdbd_args->sdb.port); - if (split_retval != SDBD_COMMANDLINE_SUCCESS) { - return split_retval; - } - print_sdbd_command(stdout, sdbd_args); - break; - case ARG_S_SDBD_LISTEN_PORT: - if (sscanf(optarg, "%d", &sdbd_args->sdbd_port) < 1) { - return SDBD_COMMANDLINE_FAILURE; - } - print_sdbd_command(stdout, sdbd_args); - break; - case ARG_S_HELP: - return SDBD_COMMANDLINE_HELP; - case ARG_S_USAGE: - return SDBD_COMMANDLINE_USAGE; - case 1: - return SDBD_COMMANDLINE_FAILURE_UNKNOWN_OPT; - case '?': - return SDBD_COMMANDLINE_FAILURE_UNKNOWN_OPT; - default: - return SDBD_COMMANDLINE_FAILURE; - } - } - - print_sdbd_command(stdout, sdbd_args); - - return SDBD_COMMANDLINE_SUCCESS; + int split_retval; + + int opt; + int long_index = 0; + + static struct option long_options[] = { + { ARG_EMULATOR_VM_NAME, required_argument, NULL, ARG_S_EMULATOR_VM_NAME }, + { ARG_SENSORS, required_argument, NULL, ARG_S_SENSORS }, + { ARG_SDB, required_argument, NULL, ARG_S_SDB }, + { ARG_SDBD_LISTEN_PORT, required_argument, NULL, ARG_S_SDBD_LISTEN_PORT }, + { ARG_HELP, no_argument, NULL, ARG_S_HELP }, + { ARG_USAGE, no_argument, NULL, ARG_S_USAGE }, + { NULL, 0, NULL, 0 } + }; + + optind = 1; /* the index of the next element to be processed in argv */ + + while ((opt = getopt_long(argc, argv, "", long_options, &long_index)) != -1) { + switch (opt) { + case ARG_S_EMULATOR_VM_NAME: + split_retval = split_host_port(optarg, + &sdbd_args->emulator.host, + &sdbd_args->emulator.port); + if (split_retval != SDBD_COMMANDLINE_SUCCESS) { + return split_retval; + } + /* if we are on emulator we listen using local transport + * so we should set port to default value but this can + * be overwritten by command line options */ + if (sdbd_args->sdbd_port < 0) { + sdbd_args->sdbd_port = DEFAULT_SDB_LOCAL_TRANSPORT_PORT; + } + print_sdbd_command(stdout, sdbd_args); + break; + case ARG_S_SENSORS: + split_retval = split_host_port(optarg, + &sdbd_args->sensors.host, + &sdbd_args->sensors.port); + if (split_retval != SDBD_COMMANDLINE_SUCCESS) { + return split_retval; + } + print_sdbd_command(stdout, sdbd_args); + break; + case ARG_S_SDB: + split_retval = split_host_port(optarg, + &sdbd_args->sdb.host, + &sdbd_args->sdb.port); + if (split_retval != SDBD_COMMANDLINE_SUCCESS) { + return split_retval; + } + print_sdbd_command(stdout, sdbd_args); + break; + case ARG_S_SDBD_LISTEN_PORT: + if (sscanf(optarg, "%d", &sdbd_args->sdbd_port) < 1) { + return SDBD_COMMANDLINE_FAILURE; + } + print_sdbd_command(stdout, sdbd_args); + break; + case ARG_S_HELP: + return SDBD_COMMANDLINE_HELP; + case ARG_S_USAGE: + return SDBD_COMMANDLINE_USAGE; + case 1: + return SDBD_COMMANDLINE_FAILURE_UNKNOWN_OPT; + case '?': + return SDBD_COMMANDLINE_FAILURE_UNKNOWN_OPT; + default: + return SDBD_COMMANDLINE_FAILURE; + } + } + + print_sdbd_command(stdout, sdbd_args); + + return SDBD_COMMANDLINE_SUCCESS; } void apply_sdbd_commandline_defaults(SdbdCommandlineArgs *sdbd_args) { - sdbd_args->emulator.port = -1; + sdbd_args->emulator.port = -1; - sdbd_args->sensors.host = strdup(QEMU_FORWARD_IP); - sdbd_args->sensors.port = DEFAULT_SENSORS_LOCAL_TRANSPORT_PORT; + sdbd_args->sensors.host = strdup(QEMU_FORWARD_IP); + sdbd_args->sensors.port = DEFAULT_SENSORS_LOCAL_TRANSPORT_PORT; - sdbd_args->sdb.host = strdup(QEMU_FORWARD_IP); - sdbd_args->sdb.port = DEFAULT_SDB_PORT; + sdbd_args->sdb.host = strdup(QEMU_FORWARD_IP); + sdbd_args->sdb.port = DEFAULT_SDB_PORT; - // by default don't listen on local transport - sdbd_args->sdbd_port = -1; + // by default don't listen on local transport + sdbd_args->sdbd_port = -1; } int split_host_port(const char *optarg, char **host, int *port) { - const char *colon = strchr(optarg, ':'); - char *old_val = NULL; - - if (colon) { - old_val = *host; - *host = strndup(optarg, colon - optarg); - if (sscanf(colon + 1, "%d", port) < 1) { - return SDBD_COMMANDLINE_FAILURE; - } - } else { - return SDBD_COMMANDLINE_FAILURE; - } - - if (old_val) { - free(old_val); - } - return SDBD_COMMANDLINE_SUCCESS; + const char *colon = strchr(optarg, ':'); + char *old_val = NULL; + + if (colon) { + old_val = *host; + *host = strndup(optarg, colon - optarg); + if (sscanf(colon + 1, "%d", port) < 1) { + return SDBD_COMMANDLINE_FAILURE; + } + } else { + return SDBD_COMMANDLINE_FAILURE; + } + + if (old_val) { + free(old_val); + } + return SDBD_COMMANDLINE_SUCCESS; } @@ -175,7 +175,7 @@ void clear_sdbd_commandline_args(SdbdCommandlineArgs *sdbd_args) { free(sdbd_args->sensors.host); sdbd_args->sensors.host = NULL; - memset(sdbd_args, 0, sizeof(SdbdCommandlineArgs)); + memset(sdbd_args, 0, sizeof(SdbdCommandlineArgs)); } diff --git a/src/commandline_sdbd.h b/src/commandline_sdbd.h index 598b478..2b026ca 100644 --- a/src/commandline_sdbd.h +++ b/src/commandline_sdbd.h @@ -46,8 +46,8 @@ * @brief A simple host:port tuple */ typedef struct { - char *host; - int port; + char *host; + int port; } HostPort; /*! @@ -55,10 +55,10 @@ typedef struct { * @brief Contains all values, which are read from commandline. */ typedef struct { - HostPort emulator; ///< emulator name and forward port - HostPort sdb; ///< sdb address - HostPort sensors; ///< sensors address - int sdbd_port; ///< Port to listen on in tcp mode + HostPort emulator; ///< emulator name and forward port + HostPort sdb; ///< sdb address + HostPort sensors; ///< sensors address + int sdbd_port; ///< Port to listen on in tcp mode } SdbdCommandlineArgs; #include diff --git a/src/default_plugin_auth.c b/src/default_plugin_auth.c index 8fa743a..103603b 100644 --- a/src/default_plugin_auth.c +++ b/src/default_plugin_auth.c @@ -41,7 +41,6 @@ int auth_support ( parameters* in, parameters* out ) int auth_get_key_file_paths ( parameters* in, parameters* out ) { return PLUGIN_CMD_FAIL; - } int confirm_public_key( parameters* in, int out_fd ) diff --git a/src/fdevent.h b/src/fdevent.h index a6db9ea..d383c1f 100644 --- a/src/fdevent.h +++ b/src/fdevent.h @@ -64,8 +64,7 @@ void fdevent_set_timeout(fdevent *fde, int64_t timeout_ms); */ void fdevent_loop(); -struct fdevent -{ +struct fdevent { fdevent *next; fdevent *prev; diff --git a/src/file_sync_service.h b/src/file_sync_service.h index 94a52b6..089a212 100644 --- a/src/file_sync_service.h +++ b/src/file_sync_service.h @@ -18,7 +18,7 @@ #define _FILE_SYNC_SERVICE_H_ #ifdef HAVE_BIG_ENDIAN -static inline unsigned __swap_uint32(unsigned x) +static inline unsigned __swap_uint32(unsigned x) { return (((x) & 0xFF000000) >> 24) | (((x) & 0x00FF0000) >> 8) @@ -72,7 +72,7 @@ typedef union { struct { unsigned id; unsigned msglen; - } status; + } status; } syncmsg; void init_sdk_sync_permit_rule_regx(void); diff --git a/src/init.c b/src/init.c index bec59f2..7fcc70b 100644 --- a/src/init.c +++ b/src/init.c @@ -50,7 +50,7 @@ char *smack_mnt = NULL; void set_smackmnt(const char *mnt) { - smack_mnt = strdup(mnt); + smack_mnt = strdup(mnt); } /* Verify the mount point for smack file system has a smackfs. @@ -62,126 +62,126 @@ void set_smackmnt(const char *mnt) */ static int verify_smackmnt(const char *mnt) { - struct statfs sfbuf; - int rc; - - do { - rc = statfs(mnt, &sfbuf); - } while (rc < 0 && errno == EINTR); - - if (rc == 0) { - if ((uint32_t)sfbuf.f_type == (uint32_t)SMACK_MAGIC) { - struct statvfs vfsbuf; - rc = statvfs(mnt, &vfsbuf); - if (rc == 0) { - if (!(vfsbuf.f_flag & ST_RDONLY)) { - set_smackmnt(mnt); - } - return 0; - } - } - } - - return -1; + struct statfs sfbuf; + int rc; + + do { + rc = statfs(mnt, &sfbuf); + } while (rc < 0 && errno == EINTR); + + if (rc == 0) { + if ((uint32_t)sfbuf.f_type == (uint32_t)SMACK_MAGIC) { + struct statvfs vfsbuf; + rc = statvfs(mnt, &vfsbuf); + if (rc == 0) { + if (!(vfsbuf.f_flag & ST_RDONLY)) { + set_smackmnt(mnt); + } + return 0; + } + } + } + + return -1; } int smackfs_exists(void) { - int exists = 0; - FILE *fp = NULL; - char *buf = NULL; - size_t len; - ssize_t num; - - fp = fopen("/proc/filesystems", "r"); - if (!fp) - return 1; /* Fail as if it exists */ - - __fsetlocking(fp, FSETLOCKING_BYCALLER); - - num = getline(&buf, &len, fp); - while (num != -1) { - if (strstr(buf, SMACKFS)) { - exists = 1; - break; - } - num = getline(&buf, &len, fp); - } - - free(buf); - fclose(fp); - return exists; + int exists = 0; + FILE *fp = NULL; + char *buf = NULL; + size_t len; + ssize_t num; + + fp = fopen("/proc/filesystems", "r"); + if (!fp) + return 1; /* Fail as if it exists */ + + __fsetlocking(fp, FSETLOCKING_BYCALLER); + + num = getline(&buf, &len, fp); + while (num != -1) { + if (strstr(buf, SMACKFS)) { + exists = 1; + break; + } + num = getline(&buf, &len, fp); + } + + free(buf); + fclose(fp); + return exists; } static void init_smackmnt(void) { - char *buf=NULL, *p; - FILE *fp=NULL; - size_t len; - ssize_t num; - - if (smack_mnt) - return; - - if (verify_smackmnt(SMACKFSMNT) == 0) - return; - - if (verify_smackmnt(OLDSMACKFSMNT) == 0) - return; - - /* Drop back to detecting it the long way. */ - if (!smackfs_exists()) - goto out; - - /* At this point, the usual spot doesn't have an smackfs so - * we look around for it */ - fp = fopen("/proc/mounts", "r"); - if (!fp) - goto out; - - __fsetlocking(fp, FSETLOCKING_BYCALLER); - while ((num = getline(&buf, &len, fp)) != -1) { - char *tmp; - p = strchr(buf, ' '); - if (!p) - goto out; - p++; - - tmp = strchr(p, ' '); - if (!tmp) - goto out; - - if (!strncmp(tmp + 1, SMACKFS" ", strlen(SMACKFS)+1)) { - *tmp = '\0'; - break; - } - } - - /* If we found something, dup it */ - if (num > 0) - verify_smackmnt(p); + char *buf=NULL, *p; + FILE *fp=NULL; + size_t len; + ssize_t num; + + if (smack_mnt) + return; + + if (verify_smackmnt(SMACKFSMNT) == 0) + return; + + if (verify_smackmnt(OLDSMACKFSMNT) == 0) + return; + + /* Drop back to detecting it the long way. */ + if (!smackfs_exists()) + goto out; + + /* At this point, the usual spot doesn't have an smackfs so + * we look around for it */ + fp = fopen("/proc/mounts", "r"); + if (!fp) + goto out; + + __fsetlocking(fp, FSETLOCKING_BYCALLER); + while ((num = getline(&buf, &len, fp)) != -1) { + char *tmp; + p = strchr(buf, ' '); + if (!p) + goto out; + p++; + + tmp = strchr(p, ' '); + if (!tmp) + goto out; + + if (!strncmp(tmp + 1, SMACKFS" ", strlen(SMACKFS)+1)) { + *tmp = '\0'; + break; + } + } + + /* If we found something, dup it */ + if (num > 0) + verify_smackmnt(p); out: - free(buf); - if (fp) - fclose(fp); - return; + free(buf); + if (fp) + fclose(fp); + return; } void fini_smackmnt(void) { - free(smack_mnt); - smack_mnt = NULL; + free(smack_mnt); + smack_mnt = NULL; } static void init_lib(void) __attribute__ ((constructor)); static void init_lib(void) { - init_smackmnt(); + init_smackmnt(); } static void fini_lib(void) __attribute__ ((destructor)); static void fini_lib(void) { - fini_smackmnt(); + fini_smackmnt(); } diff --git a/src/plugin_encrypt.c b/src/plugin_encrypt.c index 51df016..b7fc3ab 100644 --- a/src/plugin_encrypt.c +++ b/src/plugin_encrypt.c @@ -1,238 +1,238 @@ - -#include - -//#define LOG_TAG "SDBD" -//#include -#define TRACE_TAG TRACE_SDB -#include "log.h" - -#include "plugin.h" -#include "plugin_encrypt.h" -#include "parameter.h" -#include "sdbd_plugin.h" - -#define SAKEP_AKE_MSG_RECORD_FIXED_LEN 36 -#define SAKEP_AES_ECB_ADDED_PADDING_SIZE 16 - -// return 1 if success -// return 0 otherwise -int security_init(const int nSessionID, const char* pUserID) -{ - int success = 0; - int ret; - parameters in, out; - - if (pUserID == NULL) { - in.number_of_parameter = 1; - in.array_of_parameter = ( parameter* ) malloc ( sizeof (parameter) ); - in.array_of_parameter[0].type = type_int32; - in.array_of_parameter[0].v_int32 = nSessionID; - } else { - in.number_of_parameter = 2; - in.array_of_parameter = ( parameter* ) malloc ( sizeof (parameter) * in.number_of_parameter ); - in.array_of_parameter[0].type = type_int32; - in.array_of_parameter[0].v_int32 = nSessionID; - in.array_of_parameter[1].type = type_string; - in.array_of_parameter[1].v_string.length = strlen(pUserID); - in.array_of_parameter[1].v_string.data = strdup(pUserID); - } - - ret = request_sync_cmd ( PLUGIN_SYNC_CMD_SEC_INIT, &in, &out ); - if ( ret == PLUGIN_CMD_SUCCESS ) { - success = 1; - release_parameters ( &out ); - } - - release_parameters ( &in ); - return success; -} - -// return 1 if success -// return 0 otherwise -int security_deinit(const int nSessionID) -{ - int success = 0; - int ret; - parameters in, out; - - in.number_of_parameter = 1; - in.array_of_parameter = ( parameter* ) malloc ( sizeof (parameter) ); - in.array_of_parameter[0].type = type_int32; - in.array_of_parameter[0].v_int32 = nSessionID; - - ret = request_sync_cmd ( PLUGIN_SYNC_CMD_SEC_DEINIT, &in, &out ); - if ( ret == PLUGIN_CMD_SUCCESS ) { - success = 1; - release_parameters ( &out ); - } - - release_parameters ( &in ); - return success; -} - -// return 1 if success -// return 0 otherwise -int security_parse_server_hello(const int nSessionID, apacket* pApacket) -{ - int success = 0; - int ret; - parameters in, out; - - in.number_of_parameter = 2; - in.array_of_parameter = ( parameter* ) malloc ( sizeof (parameter) * in.number_of_parameter ); - in.array_of_parameter[0].type = type_int32; - in.array_of_parameter[0].v_int32 = nSessionID; - in.array_of_parameter[1].type = type_chunk; - in.array_of_parameter[1].v_chunk.size = pApacket->msg.data_length; - in.array_of_parameter[1].v_chunk.data = pApacket->data; - - ret = request_sync_cmd ( PLUGIN_SYNC_CMD_SEC_PARSE_SERVER_HELLO, &in, &out ); - if ( ret == PLUGIN_CMD_SUCCESS ) { - success = 1; - release_parameters ( &out ); - } - - // avoid to free - in.array_of_parameter[1].v_chunk.data = NULL; - release_parameters ( &in ); - return success; -} - -// return 1 if success -// return 0 otherwise -int security_gen_client_hello(const int nSessionID, apacket* pApacket) -{ - int success = 0; - int ret; - parameters in, out; - - in.number_of_parameter = 1; - in.array_of_parameter = ( parameter* ) malloc ( sizeof (parameter) ); - in.array_of_parameter[0].type = type_int32; - in.array_of_parameter[0].v_int32 = nSessionID; - - ret = request_sync_cmd ( PLUGIN_SYNC_CMD_SEC_GEN_CLIENT_HELLO, &in, &out ); - if ( ret == PLUGIN_CMD_SUCCESS ) { - memcpy(pApacket->data, out.array_of_parameter[0].v_chunk.data, out.array_of_parameter[0].v_chunk.size); - pApacket->msg.data_length = out.array_of_parameter[0].v_chunk.size; - success = 1; - release_parameters ( &out ); - } - - release_parameters ( &in ); - return success; -} - -// return 1 if success -// return 0 otherwise -int security_parse_server_ack(const int nSessionID, apacket* pApacket) -{ - int success = 0; - int ret; - parameters in, out; - - in.number_of_parameter = 2; - in.array_of_parameter = ( parameter* ) malloc ( sizeof (parameter) * in.number_of_parameter ); - in.array_of_parameter[0].type = type_int32; - in.array_of_parameter[0].v_int32 = nSessionID; - in.array_of_parameter[1].type = type_chunk; - in.array_of_parameter[1].v_chunk.size = pApacket->msg.data_length; - in.array_of_parameter[1].v_chunk.data = pApacket->data; - - ret = request_sync_cmd ( PLUGIN_SYNC_CMD_SEC_PARSE_SERVER_ACK, &in, &out ); - if ( ret == PLUGIN_CMD_SUCCESS ) { - success = 1; - release_parameters ( &out ); - } - - // avoid to free - in.array_of_parameter[1].v_chunk.data = NULL; - release_parameters ( &in ); - return success; -} - -// return 1 if success -// return 0 otherwise -int security_gen_client_ack(const int nSessionID, apacket* pApacket) -{ - int success = 0; - int ret; - parameters in, out; - - in.number_of_parameter = 1; - in.array_of_parameter = ( parameter* ) malloc ( sizeof (parameter) ); - in.array_of_parameter[0].type = type_int32; - in.array_of_parameter[0].v_int32 = nSessionID; - - ret = request_sync_cmd ( PLUGIN_SYNC_CMD_SEC_GEN_CLIENT_ACK, &in, &out ); - if ( ret == PLUGIN_CMD_SUCCESS ) { - memcpy(pApacket->data, out.array_of_parameter[0].v_chunk.data, out.array_of_parameter[0].v_chunk.size); - pApacket->msg.data_length = out.array_of_parameter[0].v_chunk.size; - success = 1; - release_parameters ( &out ); - } - - release_parameters ( &in ); - return success; -} - -// return 1 if success -// return 0 otherwise -int security_encrypt(const int nSessionID, apacket* pApacket) -{ - int success = 0; - int ret; - parameters in, out; - - in.number_of_parameter = 2; - in.array_of_parameter = ( parameter* ) malloc ( sizeof (parameter) * in.number_of_parameter ); - in.array_of_parameter[0].type = type_int32; - in.array_of_parameter[0].v_int32 = nSessionID; - in.array_of_parameter[1].type = type_chunk; - in.array_of_parameter[1].v_chunk.size = pApacket->msg.data_length; - in.array_of_parameter[1].v_chunk.data = pApacket->data; - - ret = request_sync_cmd ( PLUGIN_SYNC_CMD_SEC_ENCRYPT, &in, &out ); - if ( ret == PLUGIN_CMD_SUCCESS ) { - memcpy(pApacket->data, out.array_of_parameter[0].v_chunk.data, out.array_of_parameter[0].v_chunk.size); - pApacket->msg.data_length = out.array_of_parameter[0].v_chunk.size; - success = 1; - release_parameters ( &out ); - } - - // avoid to free - in.array_of_parameter[1].v_chunk.data = NULL; - release_parameters ( &in ); - return success; -} - -// return 1 if success -// return 0 otherwise -int security_decrypt(const int nSessionID, apacket* pApacket) -{ - int success = 0; - int ret; - parameters in, out; - - in.number_of_parameter = 2; - in.array_of_parameter = ( parameter* ) malloc ( sizeof (parameter) * in.number_of_parameter ); - in.array_of_parameter[0].type = type_int32; - in.array_of_parameter[0].v_int32 = nSessionID; - in.array_of_parameter[1].type = type_chunk; - in.array_of_parameter[1].v_chunk.size = pApacket->msg.data_length; - in.array_of_parameter[1].v_chunk.data = pApacket->data; - - ret = request_sync_cmd ( PLUGIN_SYNC_CMD_SEC_DECRYPT, &in, &out ); - if ( ret == PLUGIN_CMD_SUCCESS ) { - memcpy(pApacket->data, out.array_of_parameter[0].v_chunk.data, out.array_of_parameter[0].v_chunk.size); - pApacket->msg.data_length = out.array_of_parameter[0].v_chunk.size; - success = 1; - release_parameters ( &out ); - } - - // avoid to free - in.array_of_parameter[1].v_chunk.data = NULL; - release_parameters ( &in ); - return success; -} - + +#include + +//#define LOG_TAG "SDBD" +//#include +#define TRACE_TAG TRACE_SDB +#include "log.h" + +#include "plugin.h" +#include "plugin_encrypt.h" +#include "parameter.h" +#include "sdbd_plugin.h" + +#define SAKEP_AKE_MSG_RECORD_FIXED_LEN 36 +#define SAKEP_AES_ECB_ADDED_PADDING_SIZE 16 + +// return 1 if success +// return 0 otherwise +int security_init(const int nSessionID, const char* pUserID) +{ + int success = 0; + int ret; + parameters in, out; + + if (pUserID == NULL) { + in.number_of_parameter = 1; + in.array_of_parameter = ( parameter* ) malloc ( sizeof (parameter) ); + in.array_of_parameter[0].type = type_int32; + in.array_of_parameter[0].v_int32 = nSessionID; + } else { + in.number_of_parameter = 2; + in.array_of_parameter = ( parameter* ) malloc ( sizeof (parameter) * in.number_of_parameter ); + in.array_of_parameter[0].type = type_int32; + in.array_of_parameter[0].v_int32 = nSessionID; + in.array_of_parameter[1].type = type_string; + in.array_of_parameter[1].v_string.length = strlen(pUserID); + in.array_of_parameter[1].v_string.data = strdup(pUserID); + } + + ret = request_sync_cmd ( PLUGIN_SYNC_CMD_SEC_INIT, &in, &out ); + if ( ret == PLUGIN_CMD_SUCCESS ) { + success = 1; + release_parameters ( &out ); + } + + release_parameters ( &in ); + return success; +} + +// return 1 if success +// return 0 otherwise +int security_deinit(const int nSessionID) +{ + int success = 0; + int ret; + parameters in, out; + + in.number_of_parameter = 1; + in.array_of_parameter = ( parameter* ) malloc ( sizeof (parameter) ); + in.array_of_parameter[0].type = type_int32; + in.array_of_parameter[0].v_int32 = nSessionID; + + ret = request_sync_cmd ( PLUGIN_SYNC_CMD_SEC_DEINIT, &in, &out ); + if ( ret == PLUGIN_CMD_SUCCESS ) { + success = 1; + release_parameters ( &out ); + } + + release_parameters ( &in ); + return success; +} + +// return 1 if success +// return 0 otherwise +int security_parse_server_hello(const int nSessionID, apacket* pApacket) +{ + int success = 0; + int ret; + parameters in, out; + + in.number_of_parameter = 2; + in.array_of_parameter = ( parameter* ) malloc ( sizeof (parameter) * in.number_of_parameter ); + in.array_of_parameter[0].type = type_int32; + in.array_of_parameter[0].v_int32 = nSessionID; + in.array_of_parameter[1].type = type_chunk; + in.array_of_parameter[1].v_chunk.size = pApacket->msg.data_length; + in.array_of_parameter[1].v_chunk.data = pApacket->data; + + ret = request_sync_cmd ( PLUGIN_SYNC_CMD_SEC_PARSE_SERVER_HELLO, &in, &out ); + if ( ret == PLUGIN_CMD_SUCCESS ) { + success = 1; + release_parameters ( &out ); + } + + // avoid to free + in.array_of_parameter[1].v_chunk.data = NULL; + release_parameters ( &in ); + return success; +} + +// return 1 if success +// return 0 otherwise +int security_gen_client_hello(const int nSessionID, apacket* pApacket) +{ + int success = 0; + int ret; + parameters in, out; + + in.number_of_parameter = 1; + in.array_of_parameter = ( parameter* ) malloc ( sizeof (parameter) ); + in.array_of_parameter[0].type = type_int32; + in.array_of_parameter[0].v_int32 = nSessionID; + + ret = request_sync_cmd ( PLUGIN_SYNC_CMD_SEC_GEN_CLIENT_HELLO, &in, &out ); + if ( ret == PLUGIN_CMD_SUCCESS ) { + memcpy(pApacket->data, out.array_of_parameter[0].v_chunk.data, out.array_of_parameter[0].v_chunk.size); + pApacket->msg.data_length = out.array_of_parameter[0].v_chunk.size; + success = 1; + release_parameters ( &out ); + } + + release_parameters ( &in ); + return success; +} + +// return 1 if success +// return 0 otherwise +int security_parse_server_ack(const int nSessionID, apacket* pApacket) +{ + int success = 0; + int ret; + parameters in, out; + + in.number_of_parameter = 2; + in.array_of_parameter = ( parameter* ) malloc ( sizeof (parameter) * in.number_of_parameter ); + in.array_of_parameter[0].type = type_int32; + in.array_of_parameter[0].v_int32 = nSessionID; + in.array_of_parameter[1].type = type_chunk; + in.array_of_parameter[1].v_chunk.size = pApacket->msg.data_length; + in.array_of_parameter[1].v_chunk.data = pApacket->data; + + ret = request_sync_cmd ( PLUGIN_SYNC_CMD_SEC_PARSE_SERVER_ACK, &in, &out ); + if ( ret == PLUGIN_CMD_SUCCESS ) { + success = 1; + release_parameters ( &out ); + } + + // avoid to free + in.array_of_parameter[1].v_chunk.data = NULL; + release_parameters ( &in ); + return success; +} + +// return 1 if success +// return 0 otherwise +int security_gen_client_ack(const int nSessionID, apacket* pApacket) +{ + int success = 0; + int ret; + parameters in, out; + + in.number_of_parameter = 1; + in.array_of_parameter = ( parameter* ) malloc ( sizeof (parameter) ); + in.array_of_parameter[0].type = type_int32; + in.array_of_parameter[0].v_int32 = nSessionID; + + ret = request_sync_cmd ( PLUGIN_SYNC_CMD_SEC_GEN_CLIENT_ACK, &in, &out ); + if ( ret == PLUGIN_CMD_SUCCESS ) { + memcpy(pApacket->data, out.array_of_parameter[0].v_chunk.data, out.array_of_parameter[0].v_chunk.size); + pApacket->msg.data_length = out.array_of_parameter[0].v_chunk.size; + success = 1; + release_parameters ( &out ); + } + + release_parameters ( &in ); + return success; +} + +// return 1 if success +// return 0 otherwise +int security_encrypt(const int nSessionID, apacket* pApacket) +{ + int success = 0; + int ret; + parameters in, out; + + in.number_of_parameter = 2; + in.array_of_parameter = ( parameter* ) malloc ( sizeof (parameter) * in.number_of_parameter ); + in.array_of_parameter[0].type = type_int32; + in.array_of_parameter[0].v_int32 = nSessionID; + in.array_of_parameter[1].type = type_chunk; + in.array_of_parameter[1].v_chunk.size = pApacket->msg.data_length; + in.array_of_parameter[1].v_chunk.data = pApacket->data; + + ret = request_sync_cmd ( PLUGIN_SYNC_CMD_SEC_ENCRYPT, &in, &out ); + if ( ret == PLUGIN_CMD_SUCCESS ) { + memcpy(pApacket->data, out.array_of_parameter[0].v_chunk.data, out.array_of_parameter[0].v_chunk.size); + pApacket->msg.data_length = out.array_of_parameter[0].v_chunk.size; + success = 1; + release_parameters ( &out ); + } + + // avoid to free + in.array_of_parameter[1].v_chunk.data = NULL; + release_parameters ( &in ); + return success; +} + +// return 1 if success +// return 0 otherwise +int security_decrypt(const int nSessionID, apacket* pApacket) +{ + int success = 0; + int ret; + parameters in, out; + + in.number_of_parameter = 2; + in.array_of_parameter = ( parameter* ) malloc ( sizeof (parameter) * in.number_of_parameter ); + in.array_of_parameter[0].type = type_int32; + in.array_of_parameter[0].v_int32 = nSessionID; + in.array_of_parameter[1].type = type_chunk; + in.array_of_parameter[1].v_chunk.size = pApacket->msg.data_length; + in.array_of_parameter[1].v_chunk.data = pApacket->data; + + ret = request_sync_cmd ( PLUGIN_SYNC_CMD_SEC_DECRYPT, &in, &out ); + if ( ret == PLUGIN_CMD_SUCCESS ) { + memcpy(pApacket->data, out.array_of_parameter[0].v_chunk.data, out.array_of_parameter[0].v_chunk.size); + pApacket->msg.data_length = out.array_of_parameter[0].v_chunk.size; + success = 1; + release_parameters ( &out ); + } + + // avoid to free + in.array_of_parameter[1].v_chunk.data = NULL; + release_parameters ( &in ); + return success; +} + diff --git a/src/plugin_encrypt.h b/src/plugin_encrypt.h index c54ff2b..93d2363 100644 --- a/src/plugin_encrypt.h +++ b/src/plugin_encrypt.h @@ -1,15 +1,15 @@ -#ifndef __TRANSPORT_SECURITY_H__ -#define __TRANSPORT_SECURITY_H__ - -#include "sdb.h" - -int security_init(const int nID, const char* pUserID); -int security_deinit(const int nSessionID); -int security_parse_server_hello(const int nSessionID, apacket* pApacket); -int security_gen_client_hello(const int nSessionID, apacket* pApacket); -int security_parse_server_ack(const int nSessionID, apacket* pApacket); -int security_gen_client_ack(const int nSessionID, apacket* pApacket); -int security_encrypt(const int nID, apacket* pApacket); -int security_decrypt(const int nID, apacket* pApacket); - -#endif +#ifndef __TRANSPORT_SECURITY_H__ +#define __TRANSPORT_SECURITY_H__ + +#include "sdb.h" + +int security_init(const int nID, const char* pUserID); +int security_deinit(const int nSessionID); +int security_parse_server_hello(const int nSessionID, apacket* pApacket); +int security_gen_client_hello(const int nSessionID, apacket* pApacket); +int security_parse_server_ack(const int nSessionID, apacket* pApacket); +int security_gen_client_ack(const int nSessionID, apacket* pApacket); +int security_encrypt(const int nID, apacket* pApacket); +int security_decrypt(const int nID, apacket* pApacket); + +#endif diff --git a/src/sdb.c b/src/sdb.c index 39ea547..21653e3 100644 --- a/src/sdb.c +++ b/src/sdb.c @@ -369,141 +369,141 @@ void print_packet(const char *label, apacket *p) /* desc. : 암호화 실패 메시지 전송 parameter : [in] apacket* p : sdbd로 들어온 메시지 - [in] atransport *t : 현재 연결에 대한 atransport - [in] unsigned failed_value : 실패 값 + [in] atransport *t : 현재 연결에 대한 atransport + [in] unsigned failed_value : 실패 값 */ void send_encr_fail(apacket* p, atransport *t, unsigned failed_value){ - apacket* enc_p; - enc_p = get_apacket(); - enc_p->msg.command = A_ENCR; // 암호화 메시지 - enc_p->msg.arg0 = failed_value; // 실패값 - enc_p->msg.arg1 = p->msg.arg1; - send_packet(enc_p, t); - //put_apacket(enc_p); + apacket* enc_p; + enc_p = get_apacket(); + enc_p->msg.command = A_ENCR; // 암호화 메시지 + enc_p->msg.arg0 = failed_value; // 실패값 + enc_p->msg.arg1 = p->msg.arg1; + send_packet(enc_p, t); + //put_apacket(enc_p); } /* desc. : 암호화 메시지 핸들링 parameter : [in] apacket* p : sdbd로 들어온 메시지 - [in/out] atransport *t : 현재 연결에 대한 atransport + [in/out] atransport *t : 현재 연결에 대한 atransport ret : 0 : 정상적으로 메시지 전송 - -1: 메시지 전송 실패 + -1: 메시지 전송 실패 */ int handle_encr_packet(apacket* p, atransport *t){ - static int sessionID = 0; - int retVal = 0; + static int sessionID = 0; + int retVal = 0; apacket* enc_p = NULL; - if(p->msg.arg0 == ENCR_SET_ON_REQ){ // hello 메시지인 경우 - t->sessionID = sessionID; - if((retVal = security_init(t->sessionID, NULL)) == 1){ // 암호화 handshaking을 위한 init - if(security_parse_server_hello(t->sessionID, p) == 1){ // hello 메시지 파싱 - D("security_parse_server_hello success\n"); + if(p->msg.arg0 == ENCR_SET_ON_REQ){ // hello 메시지인 경우 + t->sessionID = sessionID; + if((retVal = security_init(t->sessionID, NULL)) == 1){ // 암호화 handshaking을 위한 init + if(security_parse_server_hello(t->sessionID, p) == 1){ // hello 메시지 파싱 + D("security_parse_server_hello success\n"); enc_p = get_apacket(); - if(security_gen_client_hello(t->sessionID, enc_p) == 1){ // hello 메시지 생성 - D("security_gen_client_hello success\n"); - enc_p->msg.command = A_ENCR; - enc_p->msg.arg0 = ENCR_SET_ON_REQ; - enc_p->msg.arg1 = p->msg.arg1; - sessionID++; - send_packet(enc_p, t); - } - else { // hello 메시지 생성 실패 - D("security_gen_client_hello error\n"); - send_encr_fail(p, t, ENCR_ON_FAIL); // 암호화 on 실패 메시지 전송 - t->encryption = ENCR_OFF; // 암호화 모드는 off - security_deinit(t->sessionID); - return -1; - } - } - else{ // hello 메시지 파싱 실패 - D("security_parse_server_hello error\n"); - send_encr_fail(p, t, ENCR_ON_FAIL); - t->encryption = ENCR_OFF; - security_deinit(t->sessionID); - - return -1; - } - } else { // init 실패 - D("security_init error\n"); - send_encr_fail(p, t, ENCR_ON_FAIL); - t->encryption = ENCR_OFF; - if (retVal == -1) - { - security_deinit(t->sessionID); - } - //here!! do security_deinit(), but when plugin pointer is null -> not deinit - return -1; - } - } - else if(p->msg.arg0 == ENCR_SET_ON_OK){ // ack 메시지인 경우 - if(security_parse_server_ack(t->sessionID, p) == 1){ // ack 메시지 파싱 + if(security_gen_client_hello(t->sessionID, enc_p) == 1){ // hello 메시지 생성 + D("security_gen_client_hello success\n"); + enc_p->msg.command = A_ENCR; + enc_p->msg.arg0 = ENCR_SET_ON_REQ; + enc_p->msg.arg1 = p->msg.arg1; + sessionID++; + send_packet(enc_p, t); + } + else { // hello 메시지 생성 실패 + D("security_gen_client_hello error\n"); + send_encr_fail(p, t, ENCR_ON_FAIL); // 암호화 on 실패 메시지 전송 + t->encryption = ENCR_OFF; // 암호화 모드는 off + security_deinit(t->sessionID); + return -1; + } + } + else{ // hello 메시지 파싱 실패 + D("security_parse_server_hello error\n"); + send_encr_fail(p, t, ENCR_ON_FAIL); + t->encryption = ENCR_OFF; + security_deinit(t->sessionID); + + return -1; + } + } else { // init 실패 + D("security_init error\n"); + send_encr_fail(p, t, ENCR_ON_FAIL); + t->encryption = ENCR_OFF; + if (retVal == -1) + { + security_deinit(t->sessionID); + } + //here!! do security_deinit(), but when plugin pointer is null -> not deinit + return -1; + } + } + else if(p->msg.arg0 == ENCR_SET_ON_OK){ // ack 메시지인 경우 + if(security_parse_server_ack(t->sessionID, p) == 1){ // ack 메시지 파싱 enc_p = get_apacket(); - if(security_gen_client_ack(t->sessionID, enc_p) == 1){ // ack 메시지 생성 - D("security_gen_client_ack success\n"); - enc_p->msg.command = A_ENCR; - enc_p->msg.arg0 = ENCR_SET_ON_OK; - enc_p->msg.arg1 = p->msg.arg1; - t->encryption = ENCR_ON; - send_packet(enc_p, t); - } - else { // ack 메시지 생성에 실패한 경우 - D("security_gen_client_ack error\n"); - send_encr_fail(p, t, ENCR_ON_FAIL); - t->encryption = ENCR_OFF; - security_deinit(t->sessionID); - return -1; - } - } - else { // ack 메시지 파싱에 실패한 경우 - D("security_parse_server_ack error\n"); - send_encr_fail(p, t, ENCR_ON_FAIL); - t->encryption = ENCR_OFF; - security_deinit(t->sessionID); - return -1; - } - } - else if(p->msg.arg0 == ENCR_SET_OFF){ // 암호화 모드 off 요청 메시지 - if(t->encryption == ENCR_ON && security_deinit(t->sessionID) == 1){ // 현재 암호화 모드가 on 상태인 경우 - enc_p = get_apacket(); - t->encryption = ENCR_OFF; // 현재 연결에 대한 암호화 모드 off - enc_p->msg.command = A_ENCR; - enc_p->msg.arg0 = ENCR_SET_OFF; - enc_p->msg.arg1 = p->msg.arg1; - send_packet(enc_p, t); - } - else { // 암호화 모드 off에 실패한 경우 - D("security_deinit error\n"); - send_encr_fail(p, t, ENCR_OFF_FAIL); // 암호화 모드 off 실패 메시지 전송 - return -1; - } - } - else if(p->msg.arg0 == ENCR_GET){ // 암호화 모드의 상태 요청 메시지인 경우 - enc_p = get_apacket(); - enc_p->msg.command = A_ENCR; - enc_p->msg.arg0 = ENCR_GET; // 암호화 모드 status get메시지 - enc_p->msg.arg1 = p->msg.arg1; - if(t->encryption == ENCR_ON){ // 암호화 모드가 on인 경우 - enc_p->msg.data_length = 13; - strncpy((char*)enc_p->data, "encryption:on", enc_p->msg.data_length); // encryption:on 메시지 전송 - } else if(t->encryption == ENCR_OFF){ // 암호화 모드가 off인 경우 - enc_p->msg.data_length = 14; - strncpy((char*)enc_p->data, "encryption:off", enc_p->msg.data_length); // encryption:off 메시지 전송 - } - send_packet(enc_p, t); - } - else if (p->msg.arg0 == ENCR_ON_FAIL) // 암호화 모드를 on 하는 도중 실패한 경우 받는 메시지 - { - t->encryption = ENCR_OFF; // 암호화 모드를 다시 off - D("encryption on failed\n"); - } - else if (p->msg.arg0 == ENCR_OFF_FAIL) // 암호화 모드를 off하는 도중 실패한 경우 받는 메시지 - { - //t->encryption = ENCR_ON; - D("encryption off failed\n"); - } - //put_apacket(enc_p); - return 0; + if(security_gen_client_ack(t->sessionID, enc_p) == 1){ // ack 메시지 생성 + D("security_gen_client_ack success\n"); + enc_p->msg.command = A_ENCR; + enc_p->msg.arg0 = ENCR_SET_ON_OK; + enc_p->msg.arg1 = p->msg.arg1; + t->encryption = ENCR_ON; + send_packet(enc_p, t); + } + else { // ack 메시지 생성에 실패한 경우 + D("security_gen_client_ack error\n"); + send_encr_fail(p, t, ENCR_ON_FAIL); + t->encryption = ENCR_OFF; + security_deinit(t->sessionID); + return -1; + } + } + else { // ack 메시지 파싱에 실패한 경우 + D("security_parse_server_ack error\n"); + send_encr_fail(p, t, ENCR_ON_FAIL); + t->encryption = ENCR_OFF; + security_deinit(t->sessionID); + return -1; + } + } + else if(p->msg.arg0 == ENCR_SET_OFF){ // 암호화 모드 off 요청 메시지 + if(t->encryption == ENCR_ON && security_deinit(t->sessionID) == 1){ // 현재 암호화 모드가 on 상태인 경우 + enc_p = get_apacket(); + t->encryption = ENCR_OFF; // 현재 연결에 대한 암호화 모드 off + enc_p->msg.command = A_ENCR; + enc_p->msg.arg0 = ENCR_SET_OFF; + enc_p->msg.arg1 = p->msg.arg1; + send_packet(enc_p, t); + } + else { // 암호화 모드 off에 실패한 경우 + D("security_deinit error\n"); + send_encr_fail(p, t, ENCR_OFF_FAIL); // 암호화 모드 off 실패 메시지 전송 + return -1; + } + } + else if(p->msg.arg0 == ENCR_GET){ // 암호화 모드의 상태 요청 메시지인 경우 + enc_p = get_apacket(); + enc_p->msg.command = A_ENCR; + enc_p->msg.arg0 = ENCR_GET; // 암호화 모드 status get메시지 + enc_p->msg.arg1 = p->msg.arg1; + if(t->encryption == ENCR_ON){ // 암호화 모드가 on인 경우 + enc_p->msg.data_length = 13; + strncpy((char*)enc_p->data, "encryption:on", enc_p->msg.data_length); // encryption:on 메시지 전송 + } else if(t->encryption == ENCR_OFF){ // 암호화 모드가 off인 경우 + enc_p->msg.data_length = 14; + strncpy((char*)enc_p->data, "encryption:off", enc_p->msg.data_length); // encryption:off 메시지 전송 + } + send_packet(enc_p, t); + } + else if (p->msg.arg0 == ENCR_ON_FAIL) // 암호화 모드를 on 하는 도중 실패한 경우 받는 메시지 + { + t->encryption = ENCR_OFF; // 암호화 모드를 다시 off + D("encryption on failed\n"); + } + else if (p->msg.arg0 == ENCR_OFF_FAIL) // 암호화 모드를 off하는 도중 실패한 경우 받는 메시지 + { + //t->encryption = ENCR_ON; + D("encryption off failed\n"); + } + //put_apacket(enc_p); + return 0; } #endif @@ -892,11 +892,11 @@ void handle_packet(apacket *p, atransport *t) } break; #ifdef SUPPORT_ENCRYPT - case A_ENCR: // 암호화 메시지인 경우 - if(t->connection_state != CS_OFFLINE) { - handle_encr_packet(p, t); - } - break; + case A_ENCR: // 암호화 메시지인 경우 + if(t->connection_state != CS_OFFLINE) { + handle_encr_packet(p, t); + } + break; #endif default: @@ -1301,91 +1301,91 @@ int should_drop_privileges() { #define SDBD_BOOT_INFO_FILE "/tmp/sdbd_boot_info" static DBusHandlerResult __sdbd_dbus_signal_filter(DBusConnection *conn, - DBusMessage *message, void *user_data) { - D("got dbus message\n"); - const char *interface; - - DBusError error; - dbus_error_init(&error); - - interface = dbus_message_get_interface(message); - if (interface == NULL) { - D("reject by security issue - no interface\n"); - return DBUS_HANDLER_RESULT_NOT_YET_HANDLED; - } - - if (dbus_message_is_signal(message, DEVICED_CORE_INTERFACE, - BOOTING_DONE_SIGNAL)) { - booting_done = 1; - if (access(SDBD_BOOT_INFO_FILE, F_OK) == 0) { - D("booting is done before\n"); - } else { - FILE *f = fopen(SDBD_BOOT_INFO_FILE, "w"); - if (f != NULL) { - fprintf(f, "%d", 1); - fclose(f); - } - } - D("booting is done\n"); - } - - D("handled dbus message\n"); - return DBUS_HANDLER_RESULT_HANDLED; + DBusMessage *message, void *user_data) { + D("got dbus message\n"); + const char *interface; + + DBusError error; + dbus_error_init(&error); + + interface = dbus_message_get_interface(message); + if (interface == NULL) { + D("reject by security issue - no interface\n"); + return DBUS_HANDLER_RESULT_NOT_YET_HANDLED; + } + + if (dbus_message_is_signal(message, DEVICED_CORE_INTERFACE, + BOOTING_DONE_SIGNAL)) { + booting_done = 1; + if (access(SDBD_BOOT_INFO_FILE, F_OK) == 0) { + D("booting is done before\n"); + } else { + FILE *f = fopen(SDBD_BOOT_INFO_FILE, "w"); + if (f != NULL) { + fprintf(f, "%d", 1); + fclose(f); + } + } + D("booting is done\n"); + } + + D("handled dbus message\n"); + return DBUS_HANDLER_RESULT_HANDLED; } static void *bootdone_cb(void *x) { - int MAX_LOCAL_BUFSZ = 128; - DBusError error; - DBusConnection *bus; - char rule[MAX_LOCAL_BUFSZ]; - GMainLoop *mainloop; + int MAX_LOCAL_BUFSZ = 128; + DBusError error; + DBusConnection *bus; + char rule[MAX_LOCAL_BUFSZ]; + GMainLoop *mainloop; /* g_type_init() is deprecated for glib version 2.35.0 or greater, */ #if !GLIB_CHECK_VERSION(2,35,0) - g_type_init(); + g_type_init(); #endif - dbus_error_init(&error); - bus = dbus_bus_get(DBUS_BUS_SYSTEM, &error); - if (!bus) { - D("Failed to connect to the D-BUS daemon: %s", error.message); - dbus_error_free(&error); - return NULL; - } - dbus_connection_setup_with_g_main(bus, NULL); - - snprintf(rule, MAX_LOCAL_BUFSZ, "type='signal',interface='%s'", - DEVICED_CORE_INTERFACE); - /* listening to messages */ - dbus_bus_add_match(bus, rule, &error); - if (dbus_error_is_set(&error)) { - D("Fail to rule set: %s", error.message); - dbus_error_free(&error); - return NULL; - } - - if (dbus_connection_add_filter(bus, __sdbd_dbus_signal_filter, NULL, NULL) - == FALSE) - return NULL; - - D("booting signal initialized\n"); - mainloop = g_main_loop_new(NULL, FALSE); - g_main_loop_run(mainloop); - - D("dbus loop exited"); + dbus_error_init(&error); + bus = dbus_bus_get(DBUS_BUS_SYSTEM, &error); + if (!bus) { + D("Failed to connect to the D-BUS daemon: %s", error.message); + dbus_error_free(&error); + return NULL; + } + dbus_connection_setup_with_g_main(bus, NULL); + + snprintf(rule, MAX_LOCAL_BUFSZ, "type='signal',interface='%s'", + DEVICED_CORE_INTERFACE); + /* listening to messages */ + dbus_bus_add_match(bus, rule, &error); + if (dbus_error_is_set(&error)) { + D("Fail to rule set: %s", error.message); + dbus_error_free(&error); + return NULL; + } + + if (dbus_connection_add_filter(bus, __sdbd_dbus_signal_filter, NULL, NULL) + == FALSE) + return NULL; + + D("booting signal initialized\n"); + mainloop = g_main_loop_new(NULL, FALSE); + g_main_loop_run(mainloop); + + D("dbus loop exited"); dbus_connection_unref(bus); - return NULL; + return NULL; } void register_bootdone_cb() { - D("registerd bootdone callback\n"); + D("registerd bootdone callback\n"); - sdb_thread_t t; - if (sdb_thread_create(&t, bootdone_cb, NULL)) { - D("cannot create service thread\n"); - return; - } + sdb_thread_t t; + if (sdb_thread_create(&t, bootdone_cb, NULL)) { + D("cannot create service thread\n"); + return; + } } static int sdbd_set_groups(const char *name, int gid, struct group_info default_groups[], int default_groups_size) { diff --git a/src/sdb.h b/src/sdb.h index 12b3105..1516455 100644 --- a/src/sdb.h +++ b/src/sdb.h @@ -146,7 +146,7 @@ struct asocket { /* socket-type-specific extradata */ void *extra; - /* A socket is bound to atransport */ + /* A socket is bound to atransport */ atransport *transport; }; @@ -216,8 +216,8 @@ struct atransport size_t max_payload; #ifdef SUPPORT_ENCRYPT - unsigned encryption; // 해당 연결이 암호화 모드인지 확인하는 flag , 0 = no-encryption / 1 = encryption - int sessionID; // 암호화 세션 ID, 암호화 map에 대한 key + unsigned encryption; // 해당 연결이 암호화 모드인지 확인하는 flag , 0 = no-encryption / 1 = encryption + int sessionID; // 암호화 세션 ID, 암호화 map에 대한 key #endif }; diff --git a/src/services.c b/src/services.c index 6c9d1d6..ea991c2 100644 --- a/src/services.c +++ b/src/services.c @@ -139,7 +139,7 @@ void rootshell_service(int fd, void *cookie) writex(fd, buf, strlen(buf)); } } else { - snprintf(buf, sizeof(buf), "Unknown command option : %s\n", mode); + snprintf(buf, sizeof(buf), "Unknown command option : %s\n", mode); writex(fd, buf, strlen(buf)); } D("set rootshell to %s\n", rootshell_mode == 1 ? "root" : SDK_USER_NAME); diff --git a/src/socket_inaddr_any_server.c b/src/socket_inaddr_any_server.c index 10a84e6..00aa19b 100644 --- a/src/socket_inaddr_any_server.c +++ b/src/socket_inaddr_any_server.c @@ -64,7 +64,7 @@ int socket_inaddr_any_server(int port, int type) if (ret < 0) { close(s); - return -1; + return -1; } } diff --git a/src/socket_local.h b/src/socket_local.h index dc274ac..056d090 100644 --- a/src/socket_local.h +++ b/src/socket_local.h @@ -17,7 +17,7 @@ #ifndef __SOCKET_LOCAL_H #define __SOCKET_LOCAL_H -#define FILESYSTEM_SOCKET_PREFIX "/tmp/" +#define FILESYSTEM_SOCKET_PREFIX "/tmp/" #define ANDROID_RESERVED_SOCKET_PREFIX "/dev/socket/" /* @@ -30,10 +30,10 @@ * socklen_t is set to indicate the final length. This function * will fail if the namespace is invalid (not one of the indicated * constants) or if the name is too long. - * + * * @return 0 on success or -1 on failure - */ -int socket_make_sockaddr_un(const char *name, int namespaceId, + */ +int socket_make_sockaddr_un(const char *name, int namespaceId, struct sockaddr_un *p_addr, socklen_t *alen); #endif diff --git a/src/socket_local_server.c b/src/socket_local_server.c index 8f2ed9f..5cba92d 100644 --- a/src/socket_local_server.c +++ b/src/socket_local_server.c @@ -88,7 +88,7 @@ int socket_local_server_bind(int s, const char *name, int namespaceId) } -/** Open a server-side UNIX domain datagram socket in the Linux non-filesystem +/** Open a server-side UNIX domain datagram socket in the Linux non-filesystem * namespace * * Returns fd on success, -1 on fail @@ -98,7 +98,7 @@ int socket_local_server(const char *name, int namespace, int type) { int err; int s; - + s = socket(AF_LOCAL, type, 0); if (s < 0) return -1; diff --git a/src/socket_loopback_server.c b/src/socket_loopback_server.c index 73e5670..f397a3e 100644 --- a/src/socket_loopback_server.c +++ b/src/socket_loopback_server.c @@ -133,7 +133,7 @@ int socket_loopback_server(int port, int type) if (ret < 0) { sdb_close(s); - return -1; + return -1; } } diff --git a/src/sockets.c b/src/sockets.c index 9711613..12baade 100644 --- a/src/sockets.c +++ b/src/sockets.c @@ -319,11 +319,11 @@ static void local_socket_event_func(int fd, unsigned ev, void *_s) apacket *p = get_apacket(); unsigned char *x = p->data; #ifdef SUPPORT_ENCRYPT - // sdb.c:536에서 sdb server의 패킷은 MAX_PAYLOAD-100으로 정하여서, - // sdb server에서 패킷 데이터의 크기를 MAX_PAYLOAD-100보다 작은 지를 체크함. - // sdbd에서 패킷 데이터를 MAX_PAYLOAD - 200로 잡아서 암호화 하게되면 - // 최대 MAX_PAYLOAD - 100 크기의 패킷을 생성하게 됨. - const size_t max_payload = asock_get_max_payload(s) - 200; + // sdb.c:536에서 sdb server의 패킷은 MAX_PAYLOAD-100으로 정하여서, + // sdb server에서 패킷 데이터의 크기를 MAX_PAYLOAD-100보다 작은 지를 체크함. + // sdbd에서 패킷 데이터를 MAX_PAYLOAD - 200로 잡아서 암호화 하게되면 + // 최대 MAX_PAYLOAD - 100 크기의 패킷을 생성하게 됨. + const size_t max_payload = asock_get_max_payload(s) - 200; #else const size_t max_payload = asock_get_max_payload(s); #endif diff --git a/src/sockets.h b/src/sockets.h index de2269f..e358a19 100644 --- a/src/sockets.h +++ b/src/sockets.h @@ -28,8 +28,8 @@ typedef int socklen_t; #include #endif -#define ANDROID_SOCKET_ENV_PREFIX "ANDROID_SOCKET_" -#define ANDROID_SOCKET_DIR "/dev/socket" +#define ANDROID_SOCKET_ENV_PREFIX "ANDROID_SOCKET_" +#define ANDROID_SOCKET_DIR "/dev/socket" #ifdef __cplusplus extern "C" { @@ -45,32 +45,32 @@ extern "C" { */ static inline int android_get_control_socket(const char *name) { - char key[64] = ANDROID_SOCKET_ENV_PREFIX; - const char *val; - int fd; + char key[64] = ANDROID_SOCKET_ENV_PREFIX; + const char *val; + int fd; - /* build our environment variable, counting cycles like a wolf ... */ + /* build our environment variable, counting cycles like a wolf ... */ #if HAVE_STRLCPY - strlcpy(key + sizeof(ANDROID_SOCKET_ENV_PREFIX) - 1, - name, - sizeof(key) - sizeof(ANDROID_SOCKET_ENV_PREFIX)); -#else /* for the host, which may lack the almightly strncpy ... */ - strncpy(key + sizeof(ANDROID_SOCKET_ENV_PREFIX) - 1, - name, - sizeof(key) - sizeof(ANDROID_SOCKET_ENV_PREFIX)); - key[sizeof(key)-1] = '\0'; + strlcpy(key + sizeof(ANDROID_SOCKET_ENV_PREFIX) - 1, + name, + sizeof(key) - sizeof(ANDROID_SOCKET_ENV_PREFIX)); +#else /* for the host, which may lack the almightly strncpy ... */ + strncpy(key + sizeof(ANDROID_SOCKET_ENV_PREFIX) - 1, + name, + sizeof(key) - sizeof(ANDROID_SOCKET_ENV_PREFIX)); + key[sizeof(key)-1] = '\0'; #endif - val = getenv(key); - if (!val) - return -1; + val = getenv(key); + if (!val) + return -1; - errno = 0; - fd = strtol(val, NULL, 10); - if (errno) - return -1; + errno = 0; + fd = strtol(val, NULL, 10); + if (errno) + return -1; - return fd; + return fd; } /* diff --git a/src/transport.c b/src/transport.c index 7eb9e95..faf5482 100644 --- a/src/transport.c +++ b/src/transport.c @@ -304,10 +304,10 @@ static void *output_thread(void *_t) t->serial); #ifdef SUPPORT_ENCRYPT - if (t->encryption == ENCR_ON && p->msg.command != A_ENCR) // 현재 연결이 암호화 모드이고, 암호화 관련 메시지가 아닌 경우, 메시지 복호화 - { - security_decrypt(t->sessionID, p); - } + if (t->encryption == ENCR_ON && p->msg.command != A_ENCR) // 현재 연결이 암호화 모드이고, 암호화 관련 메시지가 아닌 경우, 메시지 복호화 + { + security_decrypt(t->sessionID, p); + } #endif @@ -382,14 +382,14 @@ static void *input_thread(void *_t) D("%s: transport got packet, sending to remote, state(%s)\n", t->serial, statename(t)); #ifdef SUPPORT_ENCRYPT - if (t->encryption == ENCR_ON && p->msg.command != A_ENCR) // 현재 연결이 암호화 모드이고, 암호화 관련 메시지가 아닌 경우, 메시지를 암호화 - { - security_encrypt(t->sessionID, p); - } - else if(t->encryption == ENCR_OFF) - { + if (t->encryption == ENCR_ON && p->msg.command != A_ENCR) // 현재 연결이 암호화 모드이고, 암호화 관련 메시지가 아닌 경우, 메시지를 암호화 + { + security_encrypt(t->sessionID, p); + } + else if(t->encryption == ENCR_OFF) + { - } + } #endif @@ -812,7 +812,7 @@ void broadcast_transport(apacket *p) apacket* ap = get_apacket(); copy_packet(ap, p); send_packet(ap, t); - + if (ap->msg.command == A_STAT && ap->msg.arg1 == 0) { // lock state message if (ap->msg.arg0 == 0) { diff --git a/src/transport_local.c b/src/transport_local.c index d1b569e..58a07a4 100644 --- a/src/transport_local.c +++ b/src/transport_local.c @@ -41,7 +41,7 @@ #include "plugin.h" #ifdef HAVE_BIG_ENDIAN -#define H4(x) (((x) & 0xFF000000) >> 24) | (((x) & 0x00FF0000) >> 8) | (((x) & 0x0000FF00) << 8) | (((x) & 0x000000FF) << 24) +#define H4(x) (((x) & 0xFF000000) >> 24) | (((x) & 0x00FF0000) >> 8) | (((x) & 0x0000FF00) << 8) | (((x) & 0x000000FF) << 24) static inline void fix_endians(apacket *p) { p->msg.command = H4(p->msg.command); @@ -450,9 +450,9 @@ static void* notify_sdbd_startup_thread(void* ptr) { } if (get_emulator_guestip(guest_ip, sizeof guest_ip) == -1) { - D("failed to get emulator guest ip\n"); - goto sleep_and_continue; - } + D("failed to get emulator guest ip\n"); + goto sleep_and_continue; + } // tell qemu sdbd is just started with udp if (send_msg_to_localhost_from_guest(host_ip, sensors_port, "2\n", 1) < 0) { diff --git a/src/transport_usb.c b/src/transport_usb.c index 32789ec..471aa87 100644 --- a/src/transport_usb.c +++ b/src/transport_usb.c @@ -26,7 +26,7 @@ #include "sdb.h" #ifdef HAVE_BIG_ENDIAN -#define H4(x) (((x) & 0xFF000000) >> 24) | (((x) & 0x00FF0000) >> 8) | (((x) & 0x0000FF00) << 8) | (((x) & 0x000000FF) << 24) +#define H4(x) (((x) & 0xFF000000) >> 24) | (((x) & 0x00FF0000) >> 8) | (((x) & 0x0000FF00) << 8) | (((x) & 0x000000FF) << 24) static inline void fix_endians(apacket *p) { p->msg.command = H4(p->msg.command); diff --git a/src/usb_funcfs_client.c b/src/usb_funcfs_client.c index 3ca92a3..00b25c3 100644 --- a/src/usb_funcfs_client.c +++ b/src/usb_funcfs_client.c @@ -35,8 +35,8 @@ #include "sdb.h" -#define MAX_PACKET_SIZE_FS 64 -#define MAX_PACKET_SIZE_HS 512 +#define MAX_PACKET_SIZE_FS 64 +#define MAX_PACKET_SIZE_HS 512 #define cpu_to_le16(x) htole16(x) #define cpu_to_le32(x) htole32(x) @@ -250,7 +250,7 @@ static void *usb_open_thread(void *x) sdb_mutex_unlock(&usb->lock); D("[ usb_thread - registering device ]\n"); - register_usb_transport(usb, NULL, 1); /* writable transport */ + register_usb_transport(usb, NULL, 1); /* writable transport */ while (1) { /* wait until the USB device needs reset */ @@ -269,7 +269,7 @@ static void *usb_open_thread(void *x) sdb_mutex_unlock(&usb->lock); D("[ usb_thread - registering device ]\n"); - register_usb_transport(usb, NULL, 1); /* writable transport */ + register_usb_transport(usb, NULL, 1); /* writable transport */ } /* never gets here */ @@ -312,34 +312,34 @@ static int read_control(struct usb_handle *usb) } /* dispatch read event */ - switch (read_event.type) { - case FUNCTIONFS_RESUME: - case FUNCTIONFS_ENABLE: - D("FFSEvent %s\n", names[read_event.type]); - sdb_mutex_lock(&usb->lock); - usb->ffs_enabled = 1; - sdb_cond_signal(&usb->notify); - sdb_mutex_unlock(&usb->lock); - break; - - case FUNCTIONFS_SUSPEND: - case FUNCTIONFS_DISABLE: - D("FFSEvent %s\n", names[read_event.type]); - sdb_mutex_lock(&usb->lock); - usb->ffs_enabled = 0; - sdb_mutex_unlock(&usb->lock); - break; - - case FUNCTIONFS_BIND: - case FUNCTIONFS_UNBIND: - case FUNCTIONFS_SETUP: - D("FFSEvent %s\n", names[read_event.type]); - break; - - default: - D("FFSEvent event (type=%d) is unknown -- ignored\n", read_event.type); - break; - } + switch (read_event.type) { + case FUNCTIONFS_RESUME: + case FUNCTIONFS_ENABLE: + D("FFSEvent %s\n", names[read_event.type]); + sdb_mutex_lock(&usb->lock); + usb->ffs_enabled = 1; + sdb_cond_signal(&usb->notify); + sdb_mutex_unlock(&usb->lock); + break; + + case FUNCTIONFS_SUSPEND: + case FUNCTIONFS_DISABLE: + D("FFSEvent %s\n", names[read_event.type]); + sdb_mutex_lock(&usb->lock); + usb->ffs_enabled = 0; + sdb_mutex_unlock(&usb->lock); + break; + + case FUNCTIONFS_BIND: + case FUNCTIONFS_UNBIND: + case FUNCTIONFS_SETUP: + D("FFSEvent %s\n", names[read_event.type]); + break; + + default: + D("FFSEvent event (type=%d) is unknown -- ignored\n", read_event.type); + break; + } return ret; } @@ -431,9 +431,9 @@ static int bulk_read(int bulkout_fd, void *buf, size_t length) if (errno != EINTR) { return ret; } - } else { + } else { count += ret; - } + } } while (count < length); @@ -446,8 +446,8 @@ static int bulk_read(int bulkout_fd, void *buf, size_t length) */ static int ep0_exists() { - struct stat statb; - return stat(ep0_path, &statb) == 0; + struct stat statb; + return stat(ep0_path, &statb) == 0; } @@ -464,9 +464,9 @@ static int autoconfig(struct usb_handle *h) return -ENODEV; } - h->EP0_NAME = ep0_path; - h->EP_OUT_NAME = ep1_path; - h->EP_IN_NAME = ep2_path; + h->EP0_NAME = ep0_path; + h->EP_OUT_NAME = ep1_path; + h->EP_IN_NAME = ep2_path; return 0; } diff --git a/src/utils.c b/src/utils.c index 6f48ba2..3a27e16 100644 --- a/src/utils.c +++ b/src/utils.c @@ -117,41 +117,41 @@ buff_add (char* buff, char* buffEnd, const char* format, ... ) } char *str_trim(char *str) { - size_t len = 0; - char *frontp = str; - char *endp = NULL; - - if (str == NULL) { - return NULL; - } - if (str[0] == '\0') { - return str; - } - - len = strlen(str); - endp = str + len; - - while (isspace(*frontp)) { - ++frontp; - } - if (endp != frontp) { - while (isspace(*(--endp)) && endp != frontp) { - } - } - - if (str + len - 1 != endp) - *(endp + 1) = '\0'; - else if (frontp != str && endp == frontp) - *str = '\0'; - - endp = str; - if (frontp != str) { - while (*frontp) { - *endp++ = *frontp++; - } - *endp = '\0'; - } - return str; + size_t len = 0; + char *frontp = str; + char *endp = NULL; + + if (str == NULL) { + return NULL; + } + if (str[0] == '\0') { + return str; + } + + len = strlen(str); + endp = str + len; + + while (isspace(*frontp)) { + ++frontp; + } + if (endp != frontp) { + while (isspace(*(--endp)) && endp != frontp) { + } + } + + if (str + len - 1 != endp) + *(endp + 1) = '\0'; + else if (frontp != str && endp == frontp) + *str = '\0'; + + endp = str; + if (frontp != str) { + while (*frontp) { + *endp++ = *frontp++; + } + *endp = '\0'; + } + return str; } int spawn(const char* program, char* const arg_list[]) @@ -182,47 +182,47 @@ int spawn(const char* program, char* const arg_list[]) } char** str_split(char* a_str, const char a_delim) { - char** result = 0; - size_t count = 0; - char* tmp = a_str; - char* last_comma = 0; - char delim[2]; - delim[0] = a_delim; - delim[1] = 0; - char *ptr; - - /* Count how many elements will be extracted. */ - while (*tmp) { - if (a_delim == *tmp) { - count++; - last_comma = tmp; - } - tmp++; - } - - /* Add space for trailing token. */ - count += last_comma < (a_str + strlen(a_str) - 1); - - /* Add space for terminating null string so caller - knows where the list of returned strings ends. */ - count++; - - result = malloc(sizeof(char*) * count); - - if (result) { - size_t idx = 0; - char* token = strtok_r(a_str, delim, &ptr); - - while (token) { - //assert(idx < count); - *(result + idx++) = strdup(token); - token = strtok_r(0, delim, &ptr); - } - //assert(idx == count - 1); - *(result + idx) = 0; - } - - return result; + char** result = 0; + size_t count = 0; + char* tmp = a_str; + char* last_comma = 0; + char delim[2]; + delim[0] = a_delim; + delim[1] = 0; + char *ptr; + + /* Count how many elements will be extracted. */ + while (*tmp) { + if (a_delim == *tmp) { + count++; + last_comma = tmp; + } + tmp++; + } + + /* Add space for trailing token. */ + count += last_comma < (a_str + strlen(a_str) - 1); + + /* Add space for terminating null string so caller + knows where the list of returned strings ends. */ + count++; + + result = malloc(sizeof(char*) * count); + + if (result) { + size_t idx = 0; + char* token = strtok_r(a_str, delim, &ptr); + + while (token) { + //assert(idx < count); + *(result + idx++) = strdup(token); + token = strtok_r(0, delim, &ptr); + } + //assert(idx == count - 1); + *(result + idx) = 0; + } + + return result; } int keep_alive(int fd, int onoff, int cnt, int idle, int interval) -- 2.7.4 From 74a263c7743e5f92b5898eace2f85b98b33e566b Mon Sep 17 00:00:00 2001 From: Sooyoung Ha Date: Tue, 27 Jun 2017 22:49:24 +0900 Subject: [PATCH 08/16] package: update version (3.0.30) Change-Id: I29f10364d289fbf07f1811a22d693c71ef30b842 Signed-off-by: Sooyoung Ha --- packaging/sdbd.spec | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packaging/sdbd.spec b/packaging/sdbd.spec index 8f5efed..a251f8c 100644 --- a/packaging/sdbd.spec +++ b/packaging/sdbd.spec @@ -2,7 +2,7 @@ Name: sdbd Summary: SDB daemon -Version: 3.0.29 +Version: 3.0.30 Release: 0 License: Apache-2.0 Summary: SDB daemon -- 2.7.4 From 6de1b632732c5fec08d29ed715428ff2b110b8e6 Mon Sep 17 00:00:00 2001 From: Sooyoung Ha Date: Wed, 28 Jun 2017 16:51:21 +0900 Subject: [PATCH 09/16] source: fix typo Change-Id: I7c68c0689e3e62361491285ce8107879c24b0209 Signed-off-by: Sooyoung Ha --- src/transport.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/transport.c b/src/transport.c index faf5482..d6ecd66 100644 --- a/src/transport.c +++ b/src/transport.c @@ -876,7 +876,7 @@ int readx(int fd, void *ptr, size_t len) D("readx: fd=%d wanted=%zu\n", fd, len); while (l > 0) { - r = sdb_read(fd, p, len); + r = sdb_read(fd, p, l); if (r > 0 && r <= l) { l -= r; p += r; -- 2.7.4 From 1dabf8de4277ef1bb4c5113cac7931b8e7d88642 Mon Sep 17 00:00:00 2001 From: Sooyoung Ha Date: Wed, 28 Jun 2017 16:54:11 +0900 Subject: [PATCH 10/16] package: update version (3.0.31) Change-Id: Ie8d745d492170e326cf86d4c1e20afd12f1def36 Signed-off-by: Sooyoung Ha --- packaging/sdbd.spec | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packaging/sdbd.spec b/packaging/sdbd.spec index a251f8c..3bfc0ec 100644 --- a/packaging/sdbd.spec +++ b/packaging/sdbd.spec @@ -2,7 +2,7 @@ Name: sdbd Summary: SDB daemon -Version: 3.0.30 +Version: 3.0.31 Release: 0 License: Apache-2.0 Summary: SDB daemon -- 2.7.4 From f5cd214d92d63433a829ca64f3a6d711c92bc00d Mon Sep 17 00:00:00 2001 From: Sooyoung Ha Date: Fri, 30 Jun 2017 14:11:37 +0900 Subject: [PATCH 11/16] packaging: modify user and group of tv service file The 'sdk' user group causes TV smack denying. This almost revert of commit a16797abdc35b105d38beaf9aaccab12b616933d. Change-Id: Id0a4beb8efc9e986cfd9f7eb56041de0d05b1105 Signed-off-by: Sooyoung Ha --- packaging/sdbd_device_tv.service | 2 -- packaging/sdbd_emulator_tv.service | 2 -- 2 files changed, 4 deletions(-) diff --git a/packaging/sdbd_device_tv.service b/packaging/sdbd_device_tv.service index b683d96..fe3c965 100644 --- a/packaging/sdbd_device_tv.service +++ b/packaging/sdbd_device_tv.service @@ -4,8 +4,6 @@ Requires=tizen-system-env.service After=tmp.mount [Service] -User=sdk -Group=sdk Type=forking #location of SDBD log file #Environment=SDBD_LOG_PATH=/tmp diff --git a/packaging/sdbd_emulator_tv.service b/packaging/sdbd_emulator_tv.service index 0590499..4d81fd2 100644 --- a/packaging/sdbd_emulator_tv.service +++ b/packaging/sdbd_emulator_tv.service @@ -5,8 +5,6 @@ After=tmp.mount dbus.service #DefaultDependencies=false [Service] -User=sdk -Group=sdk Type=forking #location of SDBD log file #Environment=SDBD_LOG_PATH=/tmp -- 2.7.4 From ff571418f7992e29f3d5f8f329ea67d4640eca51 Mon Sep 17 00:00:00 2001 From: Sooyoung Ha Date: Fri, 30 Jun 2017 14:15:11 +0900 Subject: [PATCH 12/16] package: update version (3.0.32) Change-Id: I44433704c5f7267dee1aa18573e760bed2cbc56e Signed-off-by: Sooyoung Ha --- packaging/sdbd.spec | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packaging/sdbd.spec b/packaging/sdbd.spec index 3bfc0ec..c3cdb7a 100644 --- a/packaging/sdbd.spec +++ b/packaging/sdbd.spec @@ -2,7 +2,7 @@ Name: sdbd Summary: SDB daemon -Version: 3.0.31 +Version: 3.0.32 Release: 0 License: Apache-2.0 Summary: SDB daemon -- 2.7.4 From a3f4720d14b5ff542bea08801d6ae54b2f9a849a Mon Sep 17 00:00:00 2001 From: Slava Barinov Date: Fri, 26 May 2017 12:01:38 +0300 Subject: [PATCH 13/16] package: Add libpthread explicitly for ASan build Change-Id: I39508a5c77a41f5ce3b3dfcfe295cb24bde4a367 Signed-off-by: Slava Barinov --- packaging/sdbd.spec | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/packaging/sdbd.spec b/packaging/sdbd.spec index c3cdb7a..5ad9833 100644 --- a/packaging/sdbd.spec +++ b/packaging/sdbd.spec @@ -68,8 +68,7 @@ SDBD plugin API library cp %{SOURCE1003} . %build - -cmake . -DCMAKE_INSTALL_PREFIX=%{_prefix} \ +cmake . -DCMAKE_INSTALL_PREFIX=%{_prefix} %{?asan:-DCMAKE_EXE_LINKER_FLAGS="-pthread"} make %{?jobs:-j%jobs} -- 2.7.4 From 1584bbf77ce59b2d5987b01a8bd327376ba44b8e Mon Sep 17 00:00:00 2001 From: Sooyoung Ha Date: Tue, 25 Jul 2017 15:29:36 +0900 Subject: [PATCH 14/16] service: apply capabilities for security Change-Id: If8ea4bba3476acf2d2043f17f6f8b63538fd9f8f Signed-off-by: Sooyoung Ha --- packaging/sdbd_device.service | 2 ++ packaging/sdbd_device_tv.service | 2 ++ packaging/sdbd_emulator.service | 2 ++ packaging/sdbd_emulator_tv.service | 2 ++ packaging/sdbd_tcp.service | 2 ++ 5 files changed, 10 insertions(+) diff --git a/packaging/sdbd_device.service b/packaging/sdbd_device.service index b47e8f3..779e42e 100644 --- a/packaging/sdbd_device.service +++ b/packaging/sdbd_device.service @@ -12,6 +12,8 @@ EnvironmentFile=-/run/tizen-system-env PIDFile=/tmp/.sdbd.pid Restart=on-failure SmackProcessLabel=System +Capabilities=cap_dac_override,cap_setgid,cap_setuid,cap_sys_admin=i +SecureBits=keep-caps ExecStart=/usr/sbin/sdbd [Install] diff --git a/packaging/sdbd_device_tv.service b/packaging/sdbd_device_tv.service index fe3c965..0ea497d 100644 --- a/packaging/sdbd_device_tv.service +++ b/packaging/sdbd_device_tv.service @@ -11,6 +11,8 @@ EnvironmentFile=-/run/tizen-system-env OOMScoreAdjust=-1000 PIDFile=/tmp/.sdbd.pid Restart=on-failure +Capabilities=cap_dac_override,cap_setgid,cap_setuid,cap_sys_admin=i +SecureBits=keep-caps ExecStart=/usr/sbin/sdbd [Install] diff --git a/packaging/sdbd_emulator.service b/packaging/sdbd_emulator.service index abd1605..74c5d9b 100644 --- a/packaging/sdbd_emulator.service +++ b/packaging/sdbd_emulator.service @@ -13,6 +13,8 @@ PIDFile=/tmp/.sdbd.pid RemainAfterExit=yes #ExecStartPre=/bin/bash -c "/bin/echo '10.0.2.15/32 system::debugging_network' >> /smack/netlabel" SmackProcessLabel=System +Capabilities=cap_dac_override,cap_setgid,cap_setuid,cap_sys_admin=i +SecureBits=keep-caps ExecStart=/bin/sh -c "/usr/sbin/sdbd `/usr/bin/awk '{match($0, /sdb_port=([0-9]+)/,port_match); match($0, /vm_name=([^, ]*)/,vm_match); print \"--emulator=\" vm_match[1] \":\" port_match[1] \" --connect-to=10.0.2.2:26099\" \" --sensors=10.0.2.2:\"port_match[1]+3 }' /proc/cmdline`" [Install] diff --git a/packaging/sdbd_emulator_tv.service b/packaging/sdbd_emulator_tv.service index 4d81fd2..3627ded 100644 --- a/packaging/sdbd_emulator_tv.service +++ b/packaging/sdbd_emulator_tv.service @@ -12,6 +12,8 @@ Environment=DISPLAY=:0 PIDFile=/tmp/.sdbd.pid RemainAfterExit=yes OOMScoreAdjust=-1000 +Capabilities=cap_dac_override,cap_setgid,cap_setuid,cap_sys_admin=i +SecureBits=keep-caps #ExecStartPre=/bin/bash -c "/bin/echo '10.0.2.15/32 system::debugging_network' >> /smack/netlabel" ExecStart=/bin/sh -c "/usr/sbin/sdbd `/usr/bin/awk '{match($0, /sdb_port=([0-9]+)/,port_match); match($0, /vm_name=([^, ]*)/,vm_match); print \"--emulator=\" vm_match[1] \":\" port_match[1] \" --connect-to=10.0.2.2:26099\" \" --sensors=10.0.2.2:\"port_match[1]+3 }' /proc/cmdline`" diff --git a/packaging/sdbd_tcp.service b/packaging/sdbd_tcp.service index ade025c..5269cfe 100644 --- a/packaging/sdbd_tcp.service +++ b/packaging/sdbd_tcp.service @@ -8,4 +8,6 @@ Environment=DISPLAY=:0 PIDFile=/tmp/.sdbd.pid RemainAfterExit=yes SmackProcessLabel=System +Capabilities=cap_dac_override,cap_setgid,cap_setuid,cap_sys_admin=i +SecureBits=keep-caps ExecStart=/usr/sbin/sdbd --listen-port=26101 -- 2.7.4 From 1c6c0ba027a22eb3913613b07e387f517ecb5c17 Mon Sep 17 00:00:00 2001 From: Sooyoung Ha Date: Tue, 25 Jul 2017 15:30:19 +0900 Subject: [PATCH 15/16] package: update version (3.0.33) Change-Id: Ie125643663764582fee9458810531eabf0baf208 Signed-off-by: Sooyoung Ha --- packaging/sdbd.spec | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packaging/sdbd.spec b/packaging/sdbd.spec index 5ad9833..988fcd6 100644 --- a/packaging/sdbd.spec +++ b/packaging/sdbd.spec @@ -2,7 +2,7 @@ Name: sdbd Summary: SDB daemon -Version: 3.0.32 +Version: 3.0.33 Release: 0 License: Apache-2.0 Summary: SDB daemon -- 2.7.4 From 9aa94491aeda2e6e3b236fd18ae4ac30bc26bb14 Mon Sep 17 00:00:00 2001 From: Jinhyung Jo Date: Wed, 9 Aug 2017 16:50:43 +0900 Subject: [PATCH 16/16] source: add code to check for null pointer Change-Id: Ie73726a837cdc6d28468845c91388644f0c5a853 Signed-off-by: Jinhyung Jo --- src/default_plugin_auth.c | 4 ++++ src/default_plugin_basic.c | 28 ++++++++++++++++++++++++++++ src/default_plugin_event.c | 14 ++++++++++++++ src/plugin.c | 44 ++++++++++++++++++++++++++++++-------------- src/plugin_encrypt.c | 36 ++++++++++++++++++++++++++++++++++++ src/usb_funcfs_client.c | 4 ++++ 6 files changed, 116 insertions(+), 14 deletions(-) diff --git a/src/default_plugin_auth.c b/src/default_plugin_auth.c index 103603b..5021827 100644 --- a/src/default_plugin_auth.c +++ b/src/default_plugin_auth.c @@ -32,6 +32,10 @@ int auth_support ( parameters* in, parameters* out ) out->number_of_parameter = 1; out->array_of_parameter = ( parameter* ) malloc ( sizeof ( parameter ) ); + if (out->array_of_parameter == NULL) { + D("failed to allocate memory for the parameter\n"); + return PLUGIN_CMD_FAIL; + } out->array_of_parameter[0].type = type_int32; out->array_of_parameter[0].v_int32 = PLUGIN_RET_INVALID; diff --git a/src/default_plugin_basic.c b/src/default_plugin_basic.c index 6078e7e..0354377 100644 --- a/src/default_plugin_basic.c +++ b/src/default_plugin_basic.c @@ -49,6 +49,10 @@ int get_plugin_capability ( parameters* in, parameters* out ) out->number_of_parameter = 1; out->array_of_parameter = ( parameter* ) malloc ( sizeof ( parameter ) ); + if (out->array_of_parameter == NULL) { + D("failed to allocate memory for the parameter\n"); + return PLUGIN_CMD_FAIL; + } capability = in->array_of_parameter[0].v_int32; @@ -114,6 +118,10 @@ int verify_shell_cmd ( parameters* in, parameters* out ) out->number_of_parameter = 1; out->array_of_parameter = ( parameter* ) malloc ( sizeof ( parameter ) ); + if (out->array_of_parameter == NULL) { + D("failed to allocate memory for the parameter\n"); + return PLUGIN_CMD_FAIL; + } out->array_of_parameter[0].type = type_int32; out->array_of_parameter[0].v_int32 = PLUGIN_RET_VALID; @@ -137,6 +145,10 @@ int convert_shell_cmd ( parameters* in, parameters* out ) out->number_of_parameter = 1; out->array_of_parameter = ( parameter* ) malloc ( sizeof ( parameter ) ); + if (out->array_of_parameter == NULL) { + D("failed to allocate memory for the parameter\n"); + return PLUGIN_CMD_FAIL; + } make_string_parameter ( & ( out->array_of_parameter[0] ), "%s", in->array_of_parameter[0].v_string.data ); return PLUGIN_CMD_SUCCESS; @@ -159,6 +171,10 @@ int verify_peer_ip ( parameters* in, parameters* out ) out->number_of_parameter = 1; out->array_of_parameter = ( parameter* ) malloc ( sizeof ( parameter ) ); + if (out->array_of_parameter == NULL) { + D("failed to allocate memory for the parameter\n"); + return PLUGIN_CMD_FAIL; + } out->array_of_parameter[0].type = type_int32; out->array_of_parameter[0].v_int32 = PLUGIN_RET_VALID; @@ -174,6 +190,10 @@ int verify_sdbd_launch ( parameters* in, parameters* out ) out->number_of_parameter = 1; out->array_of_parameter = ( parameter* ) malloc ( sizeof ( parameter ) ); + if (out->array_of_parameter == NULL) { + D("failed to allocate memory for the parameter\n"); + return PLUGIN_CMD_FAIL; + } out->array_of_parameter[0].type = type_int32; out->array_of_parameter[0].v_int32 = PLUGIN_RET_VALID; @@ -197,6 +217,10 @@ int verify_root_cmd ( parameters* in, parameters* out ) out->number_of_parameter = 1; out->array_of_parameter = ( parameter* ) malloc ( sizeof ( parameter ) ); + if (out->array_of_parameter == NULL) { + D("failed to allocate memory for the parameter\n"); + return PLUGIN_CMD_FAIL; + } out->array_of_parameter[0].type = type_int32; if ( verify_root_commands ( in->array_of_parameter[0].v_string.data ) ) { @@ -217,6 +241,10 @@ int get_shell_env ( parameters* in, parameters* out ) out->number_of_parameter = 1; out->array_of_parameter = ( parameter* ) malloc ( sizeof ( parameter ) ); + if (out->array_of_parameter == NULL) { + D("failed to allocate memory for the parameter\n"); + return PLUGIN_CMD_FAIL; + } make_string_parameter ( & ( out->array_of_parameter[0] ), "%s", "" ); return PLUGIN_CMD_SUCCESS; diff --git a/src/default_plugin_event.c b/src/default_plugin_event.c index 787c3f4..37aa39a 100644 --- a/src/default_plugin_event.c +++ b/src/default_plugin_event.c @@ -120,6 +120,11 @@ int get_lock_state ( parameters* in, parameters* out ) out->number_of_parameter = 1; out->array_of_parameter = ( parameter* ) malloc ( sizeof ( parameter ) ); + if (out->array_of_parameter == NULL) { + out->number_of_parameter = 0; + PLUGIN_LOG("failed to allocate memory for the parameter\n"); + return PLUGIN_CMD_FAIL; + } out->array_of_parameter[0].type = type_int32; out->array_of_parameter[0].v_int32 = ( plugin_pwlocked() == 1 ) ? PLUGIN_RET_ON : PLUGIN_RET_OFF; @@ -132,8 +137,17 @@ static void pwlock_cb ( keynode_t *key, void* data ) int pwlocked = plugin_pwlocked(); parameters* out = ( parameters* ) malloc ( sizeof ( parameters ) ); + if (out == NULL) { + PLUGIN_LOG("failed to allocate memory for the parameter\n"); + return; + } out->number_of_parameter = 1; out->array_of_parameter = ( parameter* ) malloc ( sizeof ( parameter ) ); + if (out->array_of_parameter == NULL) { + PLUGIN_LOG("failed to allocate memory for the parameter\n"); + free(out); + return; + } out->array_of_parameter[0].type = type_int32; out->array_of_parameter[0].v_int32 = ( pwlocked == 1 ) ? PLUGIN_RET_ON : PLUGIN_RET_OFF; diff --git a/src/plugin.c b/src/plugin.c index 394c863..fd6ceb8 100644 --- a/src/plugin.c +++ b/src/plugin.c @@ -212,10 +212,6 @@ static void request_async_cmd ( int cmd, parameters* in, int out_fd ) ret = default_plugin_async_proc ( cmd, in, out_fd ); } - release_parameters ( in ); - if ( in != NULL ) { - free( in ); - } sdb_close(out_fd); } @@ -266,6 +262,10 @@ int request_capability_to_plugin ( int cap, char* out_buf, unsigned int out_len in.number_of_parameter = 1; in.array_of_parameter = ( parameter* ) malloc ( sizeof ( parameter ) ); + if (in.array_of_parameter == NULL) { + D("failed to allocate memory for the parameter\n"); + return success; + } in.array_of_parameter[0].type = type_int32; in.array_of_parameter[0].v_int32 = cap; @@ -296,6 +296,10 @@ int request_validity_to_plugin ( int cmd, const char* in_buf ) if ( in_buf != NULL ) { in.number_of_parameter = 1; in.array_of_parameter = ( parameter* ) malloc ( sizeof ( parameter ) ); + if (in.array_of_parameter == NULL) { + D("failed to allocate memory for the parameter\n"); + return success; + } in.array_of_parameter[0].type = type_string; in.array_of_parameter[0].v_string.length = strlen ( in_buf ); in.array_of_parameter[0].v_string.data = strdup ( in_buf ); @@ -329,6 +333,10 @@ int request_conversion_to_plugin ( int cmd, const char* in_buf, char* out_buf, u if ( in_buf != NULL ) { in.number_of_parameter = 1; in.array_of_parameter = ( parameter* ) malloc ( sizeof ( parameter ) ); + if (in.array_of_parameter == NULL) { + D("failed to allocate memory for the parameter\n"); + return success; + } in.array_of_parameter[0].type = type_string; in.array_of_parameter[0].v_string.length = strlen ( in_buf ); in.array_of_parameter[0].v_string.data = strdup ( in_buf ); @@ -360,6 +368,10 @@ int request_lock_state_to_plugin ( int lock_type ) in.number_of_parameter = 1; in.array_of_parameter = ( parameter* ) malloc ( sizeof ( parameter ) ); + if (in.array_of_parameter == NULL) { + D("failed to allocate memory for the parameter\n"); + return result; + } in.array_of_parameter[0].type = type_int32; in.array_of_parameter[0].v_int32 = lock_type; @@ -384,23 +396,27 @@ int request_lock_state_to_plugin ( int lock_type ) // return -1 if failed to create async proc thread int request_appcmd_to_plugin ( const char* in_buf ) { - parameters* in; + parameters in; int fd; - in = ( parameters* ) malloc ( sizeof ( parameters ) ); if ( in_buf != NULL ) { - in->number_of_parameter = 1; - in->array_of_parameter = ( parameter* ) malloc ( sizeof ( parameter ) ); - in->array_of_parameter[0].type = type_string; - in->array_of_parameter[0].v_string.length = strlen ( in_buf ); - in->array_of_parameter[0].v_string.data = strdup ( in_buf ); + in.number_of_parameter = 1; + in.array_of_parameter = ( parameter* ) malloc ( sizeof ( parameter ) ); + if (in.array_of_parameter == NULL) { + D("failed to allocate memory for the parameter\n"); + return -1; + } + in.array_of_parameter[0].type = type_string; + in.array_of_parameter[0].v_string.length = strlen ( in_buf ); + in.array_of_parameter[0].v_string.data = strdup ( in_buf ); } else { - in->number_of_parameter = 0; - in->array_of_parameter = NULL; + in.number_of_parameter = 0; + in.array_of_parameter = NULL; } - fd = create_async_proc_thread( PLUGIN_ASYNC_CMD_APPCMD_SERVICE, in ); + fd = create_async_proc_thread( PLUGIN_ASYNC_CMD_APPCMD_SERVICE, &in ); + release_parameters ( &in ); return fd; } diff --git a/src/plugin_encrypt.c b/src/plugin_encrypt.c index b7fc3ab..8bd9b9d 100644 --- a/src/plugin_encrypt.c +++ b/src/plugin_encrypt.c @@ -25,11 +25,19 @@ int security_init(const int nSessionID, const char* pUserID) if (pUserID == NULL) { in.number_of_parameter = 1; in.array_of_parameter = ( parameter* ) malloc ( sizeof (parameter) ); + if (in.array_of_parameter == NULL) { + D("failed to allocate memory for the parameter\n"); + return success; + } in.array_of_parameter[0].type = type_int32; in.array_of_parameter[0].v_int32 = nSessionID; } else { in.number_of_parameter = 2; in.array_of_parameter = ( parameter* ) malloc ( sizeof (parameter) * in.number_of_parameter ); + if (in.array_of_parameter == NULL) { + D("failed to allocate memory for the parameter\n"); + return success; + } in.array_of_parameter[0].type = type_int32; in.array_of_parameter[0].v_int32 = nSessionID; in.array_of_parameter[1].type = type_string; @@ -57,6 +65,10 @@ int security_deinit(const int nSessionID) in.number_of_parameter = 1; in.array_of_parameter = ( parameter* ) malloc ( sizeof (parameter) ); + if (in.array_of_parameter == NULL) { + D("failed to allocate memory for the parameter\n"); + return success; + } in.array_of_parameter[0].type = type_int32; in.array_of_parameter[0].v_int32 = nSessionID; @@ -80,6 +92,10 @@ int security_parse_server_hello(const int nSessionID, apacket* pApacket) in.number_of_parameter = 2; in.array_of_parameter = ( parameter* ) malloc ( sizeof (parameter) * in.number_of_parameter ); + if (in.array_of_parameter == NULL) { + D("failed to allocate memory for the parameter\n"); + return success; + } in.array_of_parameter[0].type = type_int32; in.array_of_parameter[0].v_int32 = nSessionID; in.array_of_parameter[1].type = type_chunk; @@ -108,6 +124,10 @@ int security_gen_client_hello(const int nSessionID, apacket* pApacket) in.number_of_parameter = 1; in.array_of_parameter = ( parameter* ) malloc ( sizeof (parameter) ); + if (in.array_of_parameter == NULL) { + D("failed to allocate memory for the parameter\n"); + return success; + } in.array_of_parameter[0].type = type_int32; in.array_of_parameter[0].v_int32 = nSessionID; @@ -133,6 +153,10 @@ int security_parse_server_ack(const int nSessionID, apacket* pApacket) in.number_of_parameter = 2; in.array_of_parameter = ( parameter* ) malloc ( sizeof (parameter) * in.number_of_parameter ); + if (in.array_of_parameter == NULL) { + D("failed to allocate memory for the parameter\n"); + return success; + } in.array_of_parameter[0].type = type_int32; in.array_of_parameter[0].v_int32 = nSessionID; in.array_of_parameter[1].type = type_chunk; @@ -161,6 +185,10 @@ int security_gen_client_ack(const int nSessionID, apacket* pApacket) in.number_of_parameter = 1; in.array_of_parameter = ( parameter* ) malloc ( sizeof (parameter) ); + if (in.array_of_parameter == NULL) { + D("failed to allocate memory for the parameter\n"); + return success; + } in.array_of_parameter[0].type = type_int32; in.array_of_parameter[0].v_int32 = nSessionID; @@ -186,6 +214,10 @@ int security_encrypt(const int nSessionID, apacket* pApacket) in.number_of_parameter = 2; in.array_of_parameter = ( parameter* ) malloc ( sizeof (parameter) * in.number_of_parameter ); + if (in.array_of_parameter == NULL) { + D("failed to allocate memory for the parameter\n"); + return success; + } in.array_of_parameter[0].type = type_int32; in.array_of_parameter[0].v_int32 = nSessionID; in.array_of_parameter[1].type = type_chunk; @@ -216,6 +248,10 @@ int security_decrypt(const int nSessionID, apacket* pApacket) in.number_of_parameter = 2; in.array_of_parameter = ( parameter* ) malloc ( sizeof (parameter) * in.number_of_parameter ); + if (in.array_of_parameter == NULL) { + D("failed to allocate memory for the parameter\n"); + return success; + } in.array_of_parameter[0].type = type_int32; in.array_of_parameter[0].v_int32 = nSessionID; in.array_of_parameter[1].type = type_chunk; diff --git a/src/usb_funcfs_client.c b/src/usb_funcfs_client.c index 00b25c3..7522c11 100644 --- a/src/usb_funcfs_client.c +++ b/src/usb_funcfs_client.c @@ -487,6 +487,10 @@ void ffs_usb_init() D("[ usb_init - using FunctionFS ]\n"); h = calloc(1, sizeof(usb_handle)); + if (h == NULL) { + perror("[ failed to allocate memory for usb FunctionFS bulk device ]\n"); + return; + } if (autoconfig(h) < 0) { perror("[ can't recognize usb FunctionFS bulk device ]\n"); free(h); -- 2.7.4