From bdf76d0705550617e09996cf84ec832a99a9bb7c Mon Sep 17 00:00:00 2001 From: Hwankyu Jhun Date: Tue, 7 Jun 2016 17:29:49 +0900 Subject: [PATCH 01/16] Adjust coding rules - Remove unnecessary initializations - Remove using assignment in if condition - Remove void function return statements - Remove else condition after a return Change-Id: I910f9f4b7113b2e20cf58de89f0085d39984a2ba Signed-off-by: Hwankyu Jhun --- include/perf.h | 12 ++++++++---- src/common.c | 8 ++++---- src/debug-launchpad.c | 5 ++--- src/debug_util.c | 4 ++-- src/file_util.c | 3 ++- src/signal_util.c | 2 +- 6 files changed, 19 insertions(+), 15 deletions(-) diff --git a/include/perf.h b/include/perf.h index ec66620..aabcf5f 100755 --- a/include/perf.h +++ b/include/perf.h @@ -30,11 +30,15 @@ static struct timeval __g_base_time = { do { \ const char *tmp; \ struct timeval tv; \ + int ret; \ tmp = bundle_get_val(kb, AUL_K_STARTTIME); \ - if (tmp != NULL) \ - sscanf(tmp, "%ld/%ld", &tv.tv_sec, &tv.tv_usec); \ - else \ + if (tmp != NULL) { \ + ret = sscanf(tmp, "%ld/%ld", &tv.tv_sec, &tv.tv_usec); \ + if (ret != 2) \ + printf("Failed to convert format\n"); \ + } else { \ gettimeofday(&tv, NULL); \ + } \ __g_base_time.tv_sec = tv.tv_sec; \ __g_base_time.tv_usec = tv.tv_usec; \ } while (0) @@ -48,7 +52,7 @@ static struct timeval __g_base_time = { timersub(&cur, &__g_base_time, &res); \ printf("%c[1;31m[%s,%d] %ld sec %ld msec " \ fmt" %c[0m\n", \ - 27, __FUNCTION__, __LINE__, \ + 27, __func__, __LINE__, \ res.tv_sec, res.tv_usec/1000, \ ##arg, 27); \ } \ diff --git a/src/common.c b/src/common.c index 25e7aec..1f76ea4 100644 --- a/src/common.c +++ b/src/common.c @@ -156,8 +156,9 @@ app_pkt_t *_recv_pkt_raw(int fd, int *clifd, struct ucred *cr) sun_size = sizeof(struct sockaddr_un); - if ((*clifd = accept(fd, (struct sockaddr *)&aul_addr, - (socklen_t *)&sun_size)) == -1) { + *clifd = accept(fd, (struct sockaddr *)&aul_addr, + (socklen_t *)&sun_size); + if (*clifd == -1) { if (errno != EINTR) _E("accept error"); return NULL; @@ -747,10 +748,9 @@ static int __read_proc(const char *path, char *buf, int size) if (ret <= 0) { close(fd); return -1; - } else { - buf[ret] = 0; } + buf[ret] = 0; close(fd); return ret; diff --git a/src/debug-launchpad.c b/src/debug-launchpad.c index ed5d636..e38c4e7 100644 --- a/src/debug-launchpad.c +++ b/src/debug-launchpad.c @@ -88,8 +88,6 @@ static void __send_result_to_caller(int clifd, int ret) if (kill(ret, SIGKILL) == -1) _E("Failed to send SIGKILL: %d", errno); } - - return; } static int __prepare_exec(const char *appid, const char *app_path, @@ -106,7 +104,8 @@ static int __prepare_exec(const char *appid, const char *app_path, /* SET PRIVILEGES */ _D("appid: %s / pkg_type: %s / app_path: %s", appid, appinfo->pkg_type, app_path); - if ((ret = _set_access(appid)) != 0) { + ret = _set_access(appid); + if (ret != 0) { _E("Failed to set privileges " "- check your package's credential: %d", ret); return -1; diff --git a/src/debug_util.c b/src/debug_util.c index cc27f12..c71c22e 100644 --- a/src/debug_util.c +++ b/src/debug_util.c @@ -36,8 +36,8 @@ static int gdbserver_pid = -1; static int gdbserver_app_pid = -1; -static bool gdbserver = false; -static int valgrind_option = 0; +static bool gdbserver; +static int valgrind_option; bool _gdbserver_is_running(void) { diff --git a/src/file_util.c b/src/file_util.c index d7f2bea..c391409 100644 --- a/src/file_util.c +++ b/src/file_util.c @@ -29,6 +29,7 @@ static int recurse(const char *path, mode_t mode, { struct stat st; char dir[PATH_MAX]; + int n; if (path == NULL) return -1; @@ -37,7 +38,7 @@ static int recurse(const char *path, mode_t mode, return -1; if (strrchr(path, '/') != NULL) { - int n = strlen(path)-strlen(strrchr(path, '/')); + n = strlen(path) - strlen(strrchr(path, '/')); if (n >= PATH_MAX) return -1; diff --git a/src/signal_util.c b/src/signal_util.c index d761a79..f1cbc8e 100644 --- a/src/signal_util.c +++ b/src/signal_util.c @@ -35,7 +35,7 @@ #define AUL_DBUS_APPDEAD_SIGNAL "app_dead" #define AUL_DBUS_APPLAUNCH_SIGNAL "app_launch" -static GDBusConnection *bus = NULL; +static GDBusConnection *bus; static sigset_t oldmask; static void __socket_garbage_collector(void) -- 2.7.4 From f637f0184d998bf01d6353a7d352fe39e546dc9e Mon Sep 17 00:00:00 2001 From: Hwankyu Jhun Date: Tue, 14 Jun 2016 17:05:55 +0900 Subject: [PATCH 02/16] Add the root path to the environment Change-Id: If77de10a6932acf9be7cb055cdb4701f1800041a Signed-off-by: Hwankyu Jhun --- include/debug_util.h | 1 - include/defs.h | 1 + src/common.c | 4 ++++ 3 files changed, 5 insertions(+), 1 deletion(-) diff --git a/include/debug_util.h b/include/debug_util.h index b86e07d..f8d7a61 100644 --- a/include/debug_util.h +++ b/include/debug_util.h @@ -28,7 +28,6 @@ int _prepare_debug_tool(bundle *kb, const char *appid, const char **str_arr, int len); void _change_file(const char *path); void _wait_for_valgrind_output(void); -void _set_env(appinfo_t *app_info, bundle *kb); char **_create_argc_argv(bundle *kb, int *margc, const char *app_path); #endif /* __DEBUG_UTIL_H__ */ diff --git a/include/defs.h b/include/defs.h index 698c2a7..b298998 100644 --- a/include/defs.h +++ b/include/defs.h @@ -35,6 +35,7 @@ #define AUL_K_WAYLAND_DISPLAY "__AUL_WAYLAND_DISPLAY__" #define AUL_K_WAYLAND_WORKING_DIR "__AUL_WAYLAND_WORKING_DIR__" #define AUL_K_API_VERSION "__AUL_API_VERSION__" +#define AUL_K_ROOT_PATH "__AUL_ROOT_PATH__" #define SDK_CODE_COVERAGE "CODE_COVERAGE" #define SDK_DEBUG "DEBUG" diff --git a/src/common.c b/src/common.c index 1f76ea4..faed540 100644 --- a/src/common.c +++ b/src/common.c @@ -526,6 +526,10 @@ void _set_env(appinfo_t *appinfo, bundle *kb) if (str) setenv("TIZEN_API_VERSION", str, 1); + str = bundle_get_val(kb, AUL_K_ROOT_PATH); + if (str) + setenv("AUL_ROOT_PATH", str, 1); + libdir = __get_libdir(appinfo->app_path); if (libdir) { setenv("LD_LIBRARY_PATH", libdir, 1); -- 2.7.4 From 3138a868ab84feece69f4e8e12ab50e872fc1596 Mon Sep 17 00:00:00 2001 From: Hwankyu Jhun Date: Wed, 8 Jun 2016 21:38:28 +0900 Subject: [PATCH 03/16] Prepare a server socket of an application - The debug-launchpad socket path is changed to "/run/aul/daemons//.debug-launchpad-sock". - Requires [aul] https://review.tizen.org/gerrit/#/c/73534/ [amd] https://review.tizen.org/gerrit/#/c/73537/ Change-Id: Ied141d714591b2528559adcf64e67f0a3be558a4 Signed-off-by: Hwankyu Jhun --- include/common.h | 3 ++- packaging/debug-launchpad.service | 4 ++++ packaging/debug-launchpad.socket | 2 +- src/common.c | 36 ++++++++++++++++++++++++++++++------ src/debug-launchpad.c | 4 +++- src/signal_util.c | 6 +++--- 6 files changed, 43 insertions(+), 12 deletions(-) diff --git a/include/common.h b/include/common.h index b63e6c6..c01d142 100644 --- a/include/common.h +++ b/include/common.h @@ -34,7 +34,7 @@ #define _D(fmt, arg...) LOGD(fmt, ##arg) #define _W(fmt, arg...) LOGW(fmt, ##arg) -#define SOCKET_PATH "/run/user" +#define SOCKET_PATH "/run/aul" #define MAX_LOCAL_BUFSZ 128 #define AUL_SOCK_MAXBUFF 131071 @@ -68,6 +68,7 @@ void _modify_bundle(bundle *kb, int caller_pid, appinfo_t *appinfo, int cmd); void _set_env(appinfo_t *app_info, bundle *kb); char **_create_argc_argv(bundle *kb, int *margc, const char *app_path); int _proc_check_cmdline_bypid(int pid); +void _prepare_listen_sock(void); #endif /* __COMMON_H__ */ diff --git a/packaging/debug-launchpad.service b/packaging/debug-launchpad.service index 0b7d052..18af17f 100644 --- a/packaging/debug-launchpad.service +++ b/packaging/debug-launchpad.service @@ -3,4 +3,8 @@ Description=Start the debug preload/preinit daemon After=dbus.service ac.service [Service] +ExecStartPre=-/usr/bin/mkdir -p /run/aul/daemons/%U +ExecStartPre=-/usr/bin/chmod 0777 /run/aul/daemons/%U +ExecStartPre=-/usr/bin/mkdir -p /run/aul/apps/%U +ExecStartPre=-/usr/bin/chmod 0777 /run/aul/apps/%U ExecStart=/usr/bin/debug_launchpad_preloading_preinitializing_daemon diff --git a/packaging/debug-launchpad.socket b/packaging/debug-launchpad.socket index 4c5d81f..b1b7fd4 100644 --- a/packaging/debug-launchpad.socket +++ b/packaging/debug-launchpad.socket @@ -2,7 +2,7 @@ Description=The debug preload/preinit daemon Socket [Socket] -ListenStream=/run/user/%U/.debug-launchpad-sock +ListenStream=/run/aul/daemons/%U/.debug-launchpad-sock DirectoryMode=0777 [Install] diff --git a/src/common.c b/src/common.c index faed540..6b19d93 100644 --- a/src/common.c +++ b/src/common.c @@ -18,6 +18,7 @@ #include #include +#include #include #include #include @@ -72,12 +73,15 @@ static int __create_sock_activation(void) } #endif /* _APPFW_FEATURE_SOCKET_ACTIVATION */ -static int __create_server_socket(void) +static int __create_server_socket(bool is_app) { struct sockaddr_un saddr; int fd; - fd = socket(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0); + if (is_app) + fd = socket(AF_UNIX, SOCK_STREAM, 0); + else + fd = socket(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0); /* support above version 2.6.27 */ if (fd < 0) { if (errno == EINVAL) { @@ -94,9 +98,16 @@ static int __create_server_socket(void) memset(&saddr, 0, sizeof(saddr)); saddr.sun_family = AF_UNIX; - snprintf(saddr.sun_path, sizeof(saddr.sun_path), - "%s/%d/.debug-launchpad-sock", - SOCKET_PATH, getuid()); + + if (is_app) { + snprintf(saddr.sun_path, sizeof(saddr.sun_path), + "%s/apps/%d/%d", + SOCKET_PATH, getuid(), getpid()); + } else { + snprintf(saddr.sun_path, sizeof(saddr.sun_path), + "%s/daemons/%d/.debug-launchpad-sock", + SOCKET_PATH, getuid()); + } unlink(saddr.sun_path); if (bind(fd, (struct sockaddr *)&saddr, sizeof(saddr)) < 0) { @@ -131,7 +142,7 @@ int _create_server_sock(void) fd = __create_sock_activation(); #endif /* _APPFW_FEATURE_SOCKET_ACTIAVTION */ if (fd < 0) { - fd = __create_server_socket(); + fd = __create_server_socket(false); if (fd < 0) { _E("server sock error %d", fd); return -1; @@ -775,3 +786,16 @@ int _proc_check_cmdline_bypid(int pid) return 0; } +void _prepare_listen_sock(void) +{ + int fd; + char buf[12]; + + fd = __create_server_socket(true); + if (fd < 0) + return; + + snprintf(buf, sizeof(buf), "%d", fd); + setenv("AUL_LISTEN_SOCK", buf, 1); +} + diff --git a/src/debug-launchpad.c b/src/debug-launchpad.c index e38c4e7..4bf29ad 100644 --- a/src/debug-launchpad.c +++ b/src/debug-launchpad.c @@ -126,6 +126,8 @@ static int __prepare_exec(const char *appid, const char *app_path, return -1; } + _prepare_listen_sock(); + memset(process_name, '\0', AUL_PR_NAME); snprintf(process_name, AUL_PR_NAME, "%s", file_name); prctl(PR_SET_NAME, process_name); @@ -269,7 +271,7 @@ static int __start_process(const char *appid, const char *app_path, for (iter_fd = 3; iter_fd <= max_fd; iter_fd++) close(iter_fd); - snprintf(sock_path, sizeof(sock_path), "%s/%d/%d", + snprintf(sock_path, sizeof(sock_path), "%s/apps/%d/%d", SOCKET_PATH, getuid(), getpid()); unlink(sock_path); diff --git a/src/signal_util.c b/src/signal_util.c index f1cbc8e..8b1d62f 100644 --- a/src/signal_util.c +++ b/src/signal_util.c @@ -44,7 +44,7 @@ static void __socket_garbage_collector(void) struct dirent *dentry; char path[PATH_MAX]; - snprintf(path, sizeof(path), "%s/%d", SOCKET_PATH, getuid()); + snprintf(path, sizeof(path), "%s/apps/%d", SOCKET_PATH, getuid()); dp = opendir(path); if (dp == NULL) return; @@ -55,7 +55,7 @@ static void __socket_garbage_collector(void) snprintf(path, sizeof(path), "/proc/%s", dentry->d_name); if (access(path, F_OK) != 0) { /* Flawfinder: ignore */ - snprintf(path, sizeof(path), "%s/%d/%s", + snprintf(path, sizeof(path), "%s/apps/%d/%s", SOCKET_PATH, getuid(), dentry->d_name); unlink(path); continue; @@ -146,7 +146,7 @@ static int __sigchild_action(pid_t dead_pid) _send_app_dead_signal(dead_pid); - snprintf(buf, MAX_LOCAL_BUFSZ, "%s/%d/%d", + snprintf(buf, MAX_LOCAL_BUFSZ, "%s/apps/%d/%d", SOCKET_PATH, getuid(), dead_pid); unlink(buf); -- 2.7.4 From 098ec057c65063360674e6e9f46aa290cb81cc20 Mon Sep 17 00:00:00 2001 From: Hwankyu Jhun Date: Thu, 23 Jun 2016 14:55:34 +0900 Subject: [PATCH 04/16] Remove others permission about the user's directory Change-Id: I94dd0982485db160374090614659f2be955716a1 Signed-off-by: Hwankyu Jhun --- packaging/debug-launchpad.service | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packaging/debug-launchpad.service b/packaging/debug-launchpad.service index 18af17f..0fb5989 100644 --- a/packaging/debug-launchpad.service +++ b/packaging/debug-launchpad.service @@ -6,5 +6,5 @@ After=dbus.service ac.service ExecStartPre=-/usr/bin/mkdir -p /run/aul/daemons/%U ExecStartPre=-/usr/bin/chmod 0777 /run/aul/daemons/%U ExecStartPre=-/usr/bin/mkdir -p /run/aul/apps/%U -ExecStartPre=-/usr/bin/chmod 0777 /run/aul/apps/%U +ExecStartPre=-/usr/bin/chmod 0700 /run/aul/apps/%U ExecStart=/usr/bin/debug_launchpad_preloading_preinitializing_daemon -- 2.7.4 From c0bbda2fb8bbfa29181c7636471ba16583e79809 Mon Sep 17 00:00:00 2001 From: Hwankyu Jhun Date: Thu, 23 Jun 2016 16:12:29 +0900 Subject: [PATCH 05/16] Prevent app sockets from being deleted by attacker - Requires: https://review.tizen.org/gerrit/#/c/76214/ Change-Id: I761a86f219f0c9c7513930430c38c699d3fd1625 Signed-off-by: Hwankyu Jhun --- include/common.h | 1 + src/common.c | 71 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/signal_util.c | 9 +++---- 3 files changed, 75 insertions(+), 6 deletions(-) diff --git a/include/common.h b/include/common.h index c01d142..9210790 100644 --- a/include/common.h +++ b/include/common.h @@ -69,6 +69,7 @@ void _set_env(appinfo_t *app_info, bundle *kb); char **_create_argc_argv(bundle *kb, int *margc, const char *app_path); int _proc_check_cmdline_bypid(int pid); void _prepare_listen_sock(void); +int _delete_sock_path(int pid, uid_t uid); #endif /* __COMMON_H__ */ diff --git a/src/common.c b/src/common.c index 6b19d93..0f14356 100644 --- a/src/common.c +++ b/src/common.c @@ -28,6 +28,7 @@ #include #include #include +#include #include #include #ifdef _APPFW_FEATURE_SOCKET_ACTIVATION @@ -77,6 +78,7 @@ static int __create_server_socket(bool is_app) { struct sockaddr_un saddr; int fd; + int ret; if (is_app) fd = socket(AF_UNIX, SOCK_STREAM, 0); @@ -103,6 +105,25 @@ static int __create_server_socket(bool is_app) snprintf(saddr.sun_path, sizeof(saddr.sun_path), "%s/apps/%d/%d", SOCKET_PATH, getuid(), getpid()); + ret = mkdir(saddr.sun_path, 0700); + if (ret != 0) { + if (errno == EEXIST) { + if (access(saddr.sun_path, R_OK) != 0) { + _E("Failed to access %s directory - %d", + saddr.sun_path, errno); + close(fd); + return -1; + } + } else { + _E("Failed to create %s directory - %d", + saddr.sun_path, errno); + close(fd); + return -1; + } + } + snprintf(saddr.sun_path, sizeof(saddr.sun_path), + "%s/apps/%d/%d/.app-sock", + SOCKET_PATH, getuid(), getpid()); } else { snprintf(saddr.sun_path, sizeof(saddr.sun_path), "%s/daemons/%d/.debug-launchpad-sock", @@ -799,3 +820,53 @@ void _prepare_listen_sock(void) setenv("AUL_LISTEN_SOCK", buf, 1); } +static int __delete_dir(const char *path) +{ + DIR *dp; + struct dirent dentry; + struct dirent *result = NULL; + char buf[PATH_MAX]; + struct stat statbuf; + int ret; + + if (path == NULL) + return -1; + + dp = opendir(path); + if (dp == NULL) + return -1; + + while (readdir_r(dp, &dentry, &result) == 0 && result) { + if (!strcmp(dentry.d_name, ".") || !strcmp(dentry.d_name, "..")) + continue; + + snprintf(buf, sizeof(buf), "%s/%s", path, dentry.d_name); + ret = stat(buf, &statbuf); + if (ret == 0) { + if (S_ISDIR(statbuf.st_mode)) + __delete_dir(buf); + else + unlink(buf); + } + } + + rmdir(path); + closedir(dp); + + return 0; +} + +int _delete_sock_path(int pid, uid_t uid) +{ + char path[PATH_MAX]; + + snprintf(path, sizeof(path), "/run/aul/apps/%d/%d", uid, pid); + if (access(path, F_OK) == 0) + __delete_dir(path); + + if (access(path, F_OK) == 0) + return -1; + + return 0; +} + diff --git a/src/signal_util.c b/src/signal_util.c index 8b1d62f..f0e8dee 100644 --- a/src/signal_util.c +++ b/src/signal_util.c @@ -15,6 +15,7 @@ */ #include +#include #include #include #include @@ -55,9 +56,7 @@ static void __socket_garbage_collector(void) snprintf(path, sizeof(path), "/proc/%s", dentry->d_name); if (access(path, F_OK) != 0) { /* Flawfinder: ignore */ - snprintf(path, sizeof(path), "%s/apps/%d/%s", - SOCKET_PATH, getuid(), dentry->d_name); - unlink(path); + _delete_sock_path(atoi(dentry->d_name), getuid()); continue; } } @@ -146,9 +145,7 @@ static int __sigchild_action(pid_t dead_pid) _send_app_dead_signal(dead_pid); - snprintf(buf, MAX_LOCAL_BUFSZ, "%s/apps/%d/%d", - SOCKET_PATH, getuid(), dead_pid); - unlink(buf); + _delete_sock_path(dead_pid, getuid()); __socket_garbage_collector(); -- 2.7.4 From fc77ab4347469ef79674dc91436275873f97a7e5 Mon Sep 17 00:00:00 2001 From: Hwankyu Jhun Date: Wed, 29 Jun 2016 21:22:27 +0900 Subject: [PATCH 06/16] Remove unnecessary capability Change-Id: Ie6d5abe9adb03f6118c55bb9e78ad3541365e215 Signed-off-by: Hwankyu Jhun --- packaging/debug-launchpad.spec | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packaging/debug-launchpad.spec b/packaging/debug-launchpad.spec index 47517c8..2deff3e 100755 --- a/packaging/debug-launchpad.spec +++ b/packaging/debug-launchpad.spec @@ -75,7 +75,7 @@ rm -rf %{buildroot} %files %license LICENSE %manifest debug-launchpad.manifest -%caps(cap_mac_admin,cap_mac_override,cap_setgid=ei) %{_bindir}/debug_launchpad_preloading_preinitializing_daemon +%caps(cap_mac_admin,cap_setgid=ei) %{_bindir}/debug_launchpad_preloading_preinitializing_daemon %if 0%{?appfw_feature_socket_activation} %{_unitdir_user}/debug-launchpad.service %{_unitdir_user}/debug-launchpad.socket -- 2.7.4 From 9cbed95f80f830c5bf6f79f0eea54a7f8a1ddc48 Mon Sep 17 00:00:00 2001 From: Hwankyu Jhun Date: Wed, 29 Jun 2016 21:50:58 +0900 Subject: [PATCH 07/16] Remove unnecessary codes Change-Id: I6693a37e79b7ec134bcfd6bacb6131651914eaef Signed-off-by: Hwankyu Jhun --- CMakeLists.txt | 4 +- include/file_util.h | 25 ----------- include/security_util.h | 26 ------------ packaging/debug-launchpad.spec | 2 - src/debug-launchpad.c | 62 ++------------------------- src/debug_util.c | 85 ++----------------------------------- src/file_util.c | 96 ------------------------------------------ src/security_util.c | 67 ----------------------------- src/signal_util.c | 8 ---- 9 files changed, 7 insertions(+), 368 deletions(-) delete mode 100644 include/file_util.h delete mode 100644 include/security_util.h delete mode 100644 src/file_util.c delete mode 100644 src/security_util.c diff --git a/CMakeLists.txt b/CMakeLists.txt index ddb0713..4f68928 100755 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -19,7 +19,7 @@ MESSAGE("Build type: ${CMAKE_BUILD_TYPE}") # Set required packages INCLUDE(FindPkgConfig) -SET(pkgs_requires "dlog glib-2.0 gio-2.0 bundle vconf libsmack security-manager pkgmgr-info") +SET(pkgs_requires "dlog glib-2.0 gio-2.0 bundle vconf security-manager") IF(_APPFW_FEATURE_SOCKET_ACTIVATION) SET(pkgs_requires "${pkgs_requires} libsystemd-daemon") ADD_DEFINITIONS("-D_APPFW_FEATURE_SOCKET_ACTIVATION") @@ -55,9 +55,7 @@ SET(CMAKE_EXE_LINKER_FLAGS "-Wl,--as-needed") # debug_launchpad_preloading_preinitializing_daemon SET(DEBUG_LAUNCHPAD "debug_launchpad_preloading_preinitializing_daemon") add_executable(${DEBUG_LAUNCHPAD} - src/file_util.c src/debug_util.c - src/security_util.c src/signal_util.c src/common.c src/debug-launchpad.c diff --git a/include/file_util.h b/include/file_util.h deleted file mode 100644 index 1039401..0000000 --- a/include/file_util.h +++ /dev/null @@ -1,25 +0,0 @@ -/* - * Copyright (c) 2015 - 2016 Samsung Electronics Co., Ltd. All rights reserved. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef __FILE_UTIL_H__ -#define __FILE_UTIL_H__ - -#include - -int dlp_chmod(const char *path, mode_t mode, int recursive); - -#endif /* __FILE_UTIL_H__ */ - diff --git a/include/security_util.h b/include/security_util.h deleted file mode 100644 index 6029c59..0000000 --- a/include/security_util.h +++ /dev/null @@ -1,26 +0,0 @@ -/* - * Copyright (c) 2015 - 2016 Samsung Electronics Co., Ltd. All rights reserved. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef __SECURITY_UTIL_H__ -#define __SECURITY_UTIL_H__ - -int _set_smack_access_label(const char *path, const char *label); -int _apply_smack_rules(const char *subject, const char *object, - const char *access_type); -int _set_access(const char *appid); - -#endif /* __SECURITY_UTIL_H__ */ - diff --git a/packaging/debug-launchpad.spec b/packaging/debug-launchpad.spec index 2deff3e..2d3b388 100755 --- a/packaging/debug-launchpad.spec +++ b/packaging/debug-launchpad.spec @@ -21,8 +21,6 @@ BuildRequires: pkgconfig(gio-2.0) BuildRequires: pkgconfig(bundle) BuildRequires: pkgconfig(dlog) BuildRequires: pkgconfig(vconf) -BuildRequires: pkgconfig(libsmack) -BuildRequires: pkgconfig(pkgmgr-info) BuildRequires: pkgconfig(security-manager) %if "%{?profile}" == "wearable" diff --git a/src/debug-launchpad.c b/src/debug-launchpad.c index 4bf29ad..7e706f2 100644 --- a/src/debug-launchpad.c +++ b/src/debug-launchpad.c @@ -33,11 +33,10 @@ #include #include #include +#include #include "common.h" #include "signal_util.h" -#include "security_util.h" -#include "file_util.h" #include "debug_util.h" #include "perf.h" #include "defs.h" @@ -104,8 +103,8 @@ static int __prepare_exec(const char *appid, const char *app_path, /* SET PRIVILEGES */ _D("appid: %s / pkg_type: %s / app_path: %s", appid, appinfo->pkg_type, app_path); - ret = _set_access(appid); - if (ret != 0) { + ret = security_manager_prepare_app(appid); + if (ret != SECURITY_MANAGER_SUCCESS) { _E("Failed to set privileges " "- check your package's credential: %d", ret); return -1; @@ -161,58 +160,6 @@ static int __prepare_fork(bundle *kb, const char *appid) return 0; } -static int __get_caller_pid(bundle *kb) -{ - const char *str; - int pid; - - str = bundle_get_val(kb, AUL_K_ORG_CALLER_PID); - if (str) - goto end; - - str = bundle_get_val(kb, AUL_K_CALLER_PID); - if (str == NULL) - return -1; - -end: - pid = atoi(str); - if (pid <= 1) - return -1; - - return pid; -} - -static int __redirect_stdfds(int caller_pid) -{ - char buf[PATH_MAX]; - int fd; - int ret = 0; - - /* stdout */ - snprintf(buf, sizeof(buf), "/proc/%d/fd/1", caller_pid); - fd = open(buf, O_WRONLY); - if (fd < 0) { - _E("Failed to open caller(%d) stdout", caller_pid); - ret = 1; - } else { - dup2(fd, 1); - close(fd); - } - - /* stderr */ - snprintf(buf, sizeof(buf), "/proc/%d/fd/2", caller_pid); - fd = open(buf, O_WRONLY); - if (fd < 0) { - _E("Failed to open caller(%d) stderr", caller_pid); - ret += 2; - } else { - dup2(fd, 2); - close(fd); - } - - return ret; -} - static int __normal_fork_exec(int argc, char **argv) { _D("start real fork and exec\n"); @@ -275,9 +222,6 @@ static int __start_process(const char *appid, const char *app_path, SOCKET_PATH, getuid(), getpid()); unlink(sock_path); - if (__redirect_stdfds(__get_caller_pid(kb))) - _E("Failed to redirect caller fds"); - PERF("prepare exec - fisrt done"); _D("lock up test log(no error): prepare exec - first done"); diff --git a/src/debug_util.c b/src/debug_util.c index c71c22e..224de11 100644 --- a/src/debug_util.c +++ b/src/debug_util.c @@ -17,19 +17,13 @@ #include #include #include -#include #include #include #include "defs.h" #include "common.h" -#include "file_util.h" -#include "security_util.h" #include "debug_util.h" -#define LABEL_SDBD "sdbd" -#define LABEL_NETWORK "system::debugging_network" - #define POLL_VALGRIND_LOGFILE 0x00000001 #define POLL_VALGRIND_XMLFILE 0x00000002 #define POLL_VALGRIND_MASSIFFILE 0x00000004 @@ -59,67 +53,14 @@ int _get_valgrind_option(void) return valgrind_option; } -static int __check_pkginfo(const char *appid) -{ - int r; - bool preload = false; - char *storeclientid = NULL; - pkgmgrinfo_pkginfo_h handle; - - r = pkgmgrinfo_pkginfo_get_usr_pkginfo(appid, getuid(), &handle); - if (r != PMINFO_R_OK) { - _E("Failed to get pkginfo: %s", appid); - return -1; - } - - r = pkgmgrinfo_pkginfo_is_preload(handle, &preload); - if (r != PMINFO_R_OK) { - _E("Faield to check preload: %s", appid); - pkgmgrinfo_pkginfo_destroy_pkginfo(handle); - return -1; - } - - r = pkgmgrinfo_pkginfo_get_storeclientid(handle, &storeclientid); - if (r != PMINFO_R_OK) { - _E("Failed to get store client id: %s", appid); - pkgmgrinfo_pkginfo_destroy_pkginfo(handle); - return -1; - } - - if (preload == true || (storeclientid && storeclientid[0] != '\0')) { - _E("Debugging is not allowed"); - pkgmgrinfo_pkginfo_destroy_pkginfo(handle); - return -1; - } - - r = pkgmgrinfo_pkginfo_destroy_pkginfo(handle); - if (r != PMINFO_R_OK) { - _E("Failed to destroy pkginfo: %s", appid); - pkgmgrinfo_pkginfo_destroy_pkginfo(handle); - } - - return 0; -} - static int __prepare_gdbserver(bundle *kb, const char *appid) { - int r; const char *path; - r = __check_pkginfo(appid); - if (r < 0) - return -1; - path = bundle_get_val(kb, DLP_K_GDBSERVER_PATH); if (path == NULL) return -1; - r = dlp_chmod(path, S_IRUSR | S_IWUSR - | S_IXUSR | S_IRGRP | S_IXGRP - | S_IROTH | S_IXOTH, 1); - if (r != 0) - _W("Failed to set 755: %s", path); - gdbserver = true; return 0; @@ -209,44 +150,24 @@ int _prepare_debug_tool(bundle *kb, const char *appid, return 0; } -/* chmod and chsmack to read file without root privilege */ -void _change_file(const char *path) -{ - int r; - - r = dlp_chmod(path, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH, 0); - if (r) - _E("Failed to set 644: %s", path); - - r = _set_smack_access_label(path, "*"); - if (r) - _E("Failed to set smack label %s *", path); -} - void _wait_for_valgrind_output(void) { int wait_count = 1; do { if (valgrind_option & POLL_VALGRIND_LOGFILE) { - if (access(PATH_VALGRIND_LOGFILE, F_OK) == 0) { - _change_file(PATH_VALGRIND_LOGFILE); + if (access(PATH_VALGRIND_LOGFILE, F_OK) == 0) valgrind_option &= ~POLL_VALGRIND_LOGFILE; - } } if (valgrind_option & POLL_VALGRIND_XMLFILE) { - if (access(PATH_VALGRIND_XMLFILE, F_OK) == 0) { - _change_file(PATH_VALGRIND_XMLFILE); + if (access(PATH_VALGRIND_XMLFILE, F_OK) == 0) valgrind_option &= ~POLL_VALGRIND_XMLFILE; - } } if (valgrind_option & POLL_VALGRIND_MASSIFFILE) { - if (access(PATH_VALGRIND_MASSIFFILE, F_OK) == 0) { - _change_file(PATH_VALGRIND_MASSIFFILE); + if (access(PATH_VALGRIND_MASSIFFILE, F_OK) == 0) valgrind_option &= ~POLL_VALGRIND_MASSIFFILE; - } } usleep(50 * 1000); /* 50ms */ diff --git a/src/file_util.c b/src/file_util.c deleted file mode 100644 index c391409..0000000 --- a/src/file_util.c +++ /dev/null @@ -1,96 +0,0 @@ -/* - * Copyright (c) 2015 - 2016 Samsung Electronics Co., Ltd. All rights reserved. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include -#include -#include -#include -#include -#include -#include - -#include "file_util.h" - -static int recurse(const char *path, mode_t mode, - int (*fn)(const char *, mode_t, int)) -{ - struct stat st; - char dir[PATH_MAX]; - int n; - - if (path == NULL) - return -1; - - if (lstat(path, &st) == -1) - return -1; - - if (strrchr(path, '/') != NULL) { - n = strlen(path) - strlen(strrchr(path, '/')); - if (n >= PATH_MAX) - return -1; - - strncpy(dir, path, n); - dir[n] = '\0'; - fn(dir, mode, 1); - return 0; - } - - return -1; -} - -int dlp_chmod(const char *path, mode_t mode, int recursive) -{ - int fd; - struct stat lstat_info; - struct stat fstat_info; -#ifdef HAVE_WIN32_PROC - fprintf(stderr, "error: dlp_chmod not implemented on Win32 (%s)\n", - path); - return -1; -#else - - if (lstat(path, &lstat_info) == -1) - return -1; - - fd = open(path, O_WRONLY, S_IRWXU); - if (fd == -1) - return -1; - - if (fstat(fd, &fstat_info) == -1) { - close(fd); - return -1; - } - - /* this complex check is required because of 'chmod' security issue. */ - /* otherwise hacker can change other file's permission by using race condition and symbolic link. */ - if (lstat_info.st_mode == fstat_info.st_mode - && lstat_info.st_ino == fstat_info.st_ino - && lstat_info.st_dev == fstat_info.st_dev) { - if (fchmod(fd, mode) == -1) { - close(fd); - return -1; - } - } - - close(fd); - - if (recursive) - return recurse(path, mode, dlp_chmod); - - return 0; -#endif -} - diff --git a/src/security_util.c b/src/security_util.c deleted file mode 100644 index 0bc891c..0000000 --- a/src/security_util.c +++ /dev/null @@ -1,67 +0,0 @@ -/* - * Copyright (c) 2015 - 2016 Samsung Electronics Co., Ltd. All rights reserved. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include -#include -#include -#include - -#include "common.h" -#include "security_util.h" - -int _set_smack_access_label(const char *path, const char *label) -{ - return smack_setlabel(path, label, SMACK_LABEL_ACCESS); -} - -int _apply_smack_rules(const char *subject, const char *object, - const char *access_type) -{ - int r; - struct smack_accesses *rules = NULL; - - _D("%s %s %s", subject, object, access_type); - - r = smack_accesses_new(&rules); - if (r != 0) { - _E("smack_accesses_new() is failed."); - return -1; - } - - r = smack_accesses_add(rules, subject, object, access_type); - if (r != 0) { - _E("smack_accesses_add() is failed."); - smack_accesses_free(rules); - return -1; - } - - r = smack_accesses_apply(rules); - if (r != 0) { - _E("smack_accesses_apply() is failed."); - smack_accesses_free(rules); - return -1; - } - - smack_accesses_free(rules); - - return 0; -} - -int _set_access(const char *appid) -{ - return security_manager_prepare_app(appid); -} - diff --git a/src/signal_util.c b/src/signal_util.c index f0e8dee..3fd5158 100644 --- a/src/signal_util.c +++ b/src/signal_util.c @@ -17,7 +17,6 @@ #include #include #include -#include #include #include #include @@ -27,7 +26,6 @@ #include "defs.h" #include "common.h" -#include "file_util.h" #include "debug_util.h" #include "signal_util.h" @@ -130,8 +128,6 @@ int _send_app_launch_signal(int launch_pid, const char *app_id) static int __sigchild_action(pid_t dead_pid) { - char buf[MAX_LOCAL_BUFSZ]; - if (dead_pid <= 0) return -1; @@ -139,10 +135,6 @@ static int __sigchild_action(pid_t dead_pid) if (dead_pid == _get_gdbserver_pid()) dead_pid = _get_gdbserver_app_pid(); - /* valgrind xml file */ - if (access(PATH_VALGRIND_XMLFILE, F_OK) == 0) - _change_file(PATH_VALGRIND_XMLFILE); - _send_app_dead_signal(dead_pid); _delete_sock_path(dead_pid, getuid()); -- 2.7.4 From fd431cd3c34187606206f6baa612f0aaf5d279a4 Mon Sep 17 00:00:00 2001 From: Hwankyu Jhun Date: Thu, 7 Jul 2016 10:56:06 +0900 Subject: [PATCH 08/16] Modify closing all open fds Change-Id: I5bfc97b07e6d444860da66cadadf38f8729530e7 Signed-off-by: Hwankyu Jhun --- include/common.h | 1 + src/common.c | 36 ++++++++++++++++++++++++++++++++++++ src/debug-launchpad.c | 11 ++--------- 3 files changed, 39 insertions(+), 9 deletions(-) diff --git a/include/common.h b/include/common.h index 9210790..6488292 100644 --- a/include/common.h +++ b/include/common.h @@ -70,6 +70,7 @@ char **_create_argc_argv(bundle *kb, int *margc, const char *app_path); int _proc_check_cmdline_bypid(int pid); void _prepare_listen_sock(void); int _delete_sock_path(int pid, uid_t uid); +int _close_all_fds(void); #endif /* __COMMON_H__ */ diff --git a/src/common.c b/src/common.c index 0f14356..0df007d 100644 --- a/src/common.c +++ b/src/common.c @@ -870,3 +870,39 @@ int _delete_sock_path(int pid, uid_t uid) return 0; } +int _close_all_fds(void) +{ + DIR *dp; + struct dirent dentry; + struct dirent *result = NULL; + int fd; + int max_fd; + + dp = opendir("/proc/self/fd"); + if (dp == NULL) { + /* fallback */ + max_fd = sysconf(_SC_OPEN_MAX); + for (fd = 3; fd < max_fd; fd++) + close(fd); + + return 0; + } + + while (readdir_r(dp, &dentry, &result) == 0 && result) { + if (!isdigit(dentry.d_name[0])) + continue; + + fd = atoi(dentry.d_name); + if (fd < 3) + continue; + + if (fd == dirfd(dp)) + continue; + + close(fd); + } + closedir(dp); + + return 0; +} + diff --git a/src/debug-launchpad.c b/src/debug-launchpad.c index 7e706f2..9c58d1b 100644 --- a/src/debug-launchpad.c +++ b/src/debug-launchpad.c @@ -200,8 +200,6 @@ static int __start_process(const char *appid, const char *app_path, { char sock_path[PATH_MAX]; int pid; - int max_fd; - int iter_fd; if (__prepare_fork(kb, appinfo->debug_appid) < 0) return -1; @@ -214,13 +212,8 @@ static int __start_process(const char *appid, const char *app_path, _signal_unblock_sigchld(); _signal_fini(); - max_fd = sysconf(_SC_OPEN_MAX); - for (iter_fd = 3; iter_fd <= max_fd; iter_fd++) - close(iter_fd); - - snprintf(sock_path, sizeof(sock_path), "%s/apps/%d/%d", - SOCKET_PATH, getuid(), getpid()); - unlink(sock_path); + _close_all_fds(); + _delete_sock_path(getpid(), getuid()); PERF("prepare exec - fisrt done"); _D("lock up test log(no error): prepare exec - first done"); -- 2.7.4 From 87a3259fbb3a6878e5343aeba99384ffe1760e4a Mon Sep 17 00:00:00 2001 From: Hwankyu Jhun Date: Thu, 7 Jul 2016 21:35:28 +0900 Subject: [PATCH 09/16] Add the appid and the pkgid to the enviornment Change-Id: I2e2f53e2a77ff105c41be719b1ceb30836429771 Signed-off-by: Hwankyu Jhun --- include/common.h | 1 + src/common.c | 9 +++++++++ 2 files changed, 10 insertions(+) diff --git a/include/common.h b/include/common.h index 6488292..73dada5 100644 --- a/include/common.h +++ b/include/common.h @@ -56,6 +56,7 @@ typedef struct { char *hwacc; char *taskmanage; char *comp_type; + char *pkgid; } appinfo_t; struct ucred; diff --git a/src/common.c b/src/common.c index 0df007d..c98ad02 100644 --- a/src/common.c +++ b/src/common.c @@ -309,6 +309,9 @@ appinfo_t *_appinfo_create(bundle *kb) ptr = bundle_get_val(kb, AUL_K_COMP_TYPE); if (ptr) appinfo->comp_type = strdup(ptr); + ptr = bundle_get_val(kb, AUL_K_PKGID); + if (ptr) + appinfo->pkgid = strdup(ptr); ptr = bundle_get_val(kb, AUL_K_EXEC); if (ptr) appinfo->app_path = strdup(ptr); @@ -344,6 +347,8 @@ void _appinfo_free(appinfo_t *appinfo) free(appinfo->debug_appid); if (appinfo->comp_type) free(appinfo->comp_type); + if (appinfo->pkgid) + free(appinfo->pkgid); free(appinfo); } @@ -545,6 +550,10 @@ void _set_env(appinfo_t *appinfo, bundle *kb) setenv("HWACC", appinfo->hwacc, 1); if (appinfo->taskmanage) setenv("TASKMANAGE", appinfo->taskmanage, 1); + if (appinfo->appid) + setenv("AUL_APPID", appinfo->appid, 1); + if (appinfo->pkgid) + setenv("AUL_PKGID", appinfo->pkgid, 1); str = bundle_get_val(kb, AUL_K_WAYLAND_DISPLAY); if (str) -- 2.7.4 From 6297b39601a770d4ef84a9913cab471d7b8171c4 Mon Sep 17 00:00:00 2001 From: Hwankyu Jhun Date: Thu, 14 Jul 2016 11:18:19 +0900 Subject: [PATCH 10/16] Fix the exception about getting the file name Change-Id: Ib3ff3c69e01d7e8fbbbfc080078fbc122cce5070 Signed-off-by: Hwankyu Jhun --- src/debug-launchpad.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/debug-launchpad.c b/src/debug-launchpad.c index 9c58d1b..f7b7f46 100644 --- a/src/debug-launchpad.c +++ b/src/debug-launchpad.c @@ -119,8 +119,14 @@ static int __prepare_exec(const char *appid, const char *app_path, return -1; } - file_name = strrchr(app_path, '/') + 1; + file_name = strrchr(app_path, '/'); if (file_name == NULL) { + _D("file_name is NULL"); + return -1; + } + + file_name++; + if (*file_name == '\0') { _D("can't locate file name to execute"); return -1; } -- 2.7.4 From c02676bec6dfe4584f73f941d210a25b5707e12c Mon Sep 17 00:00:00 2001 From: Junghoon Park Date: Fri, 15 Jul 2016 17:46:11 +0900 Subject: [PATCH 11/16] Change exec label to System::Privileged Change-Id: I9a79ff4334ce4b4e7516a0b8449e2d125816c498 Signed-off-by: Junghoon Park --- debug-launchpad.manifest | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/debug-launchpad.manifest b/debug-launchpad.manifest index 5874aa8..0a54005 100644 --- a/debug-launchpad.manifest +++ b/debug-launchpad.manifest @@ -3,6 +3,6 @@ - + -- 2.7.4 From 2a526f1ad55774df1f056a4681932471022982e2 Mon Sep 17 00:00:00 2001 From: Hwankyu Jhun Date: Fri, 22 Jul 2016 14:32:10 +0900 Subject: [PATCH 12/16] Fix socket activation feature This feature is enabled for all profiles. Change-Id: Ib1db1f645da1f1c10f6cec5ed633db827c136315 Signed-off-by: Hwankyu Jhun --- packaging/debug-launchpad.spec | 13 +------------ 1 file changed, 1 insertion(+), 12 deletions(-) diff --git a/packaging/debug-launchpad.spec b/packaging/debug-launchpad.spec index 2d3b388..b3eab40 100755 --- a/packaging/debug-launchpad.spec +++ b/packaging/debug-launchpad.spec @@ -22,20 +22,9 @@ BuildRequires: pkgconfig(bundle) BuildRequires: pkgconfig(dlog) BuildRequires: pkgconfig(vconf) BuildRequires: pkgconfig(security-manager) - -%if "%{?profile}" == "wearable" -BuildRequires: pkgconfig(libsystemd-daemon) -%define appfw_feature_socket_activation 1 -%else -%if "%{?profile}" == "mobile" BuildRequires: pkgconfig(libsystemd-daemon) + %define appfw_feature_socket_activation 1 -%else -%if "%{?profile}" == "tv" -%define appfw_feature_socket_activation 0 -%endif -%endif -%endif %description Debug launchpad -- 2.7.4 From 1f8dd274ae4deee2835c6ba1356d3a0086fc0879 Mon Sep 17 00:00:00 2001 From: Hwankyu Jhun Date: Wed, 27 Jul 2016 15:25:09 +0900 Subject: [PATCH 13/16] Redirect stdout and stderr for debugging Change-Id: Ib9fc85cd9e6b4359e8e228c0bc2446705bb62d74 Signed-off-by: Hwankyu Jhun --- packaging/debug-launchpad.spec | 2 +- src/debug-launchpad.c | 54 +++++++++++++++++++++++++++++++++++++++++- 2 files changed, 54 insertions(+), 2 deletions(-) diff --git a/packaging/debug-launchpad.spec b/packaging/debug-launchpad.spec index b3eab40..491df68 100755 --- a/packaging/debug-launchpad.spec +++ b/packaging/debug-launchpad.spec @@ -62,7 +62,7 @@ rm -rf %{buildroot} %files %license LICENSE %manifest debug-launchpad.manifest -%caps(cap_mac_admin,cap_setgid=ei) %{_bindir}/debug_launchpad_preloading_preinitializing_daemon +%caps(cap_mac_admin,cap_setgid,cap_dac_override=ei) %{_bindir}/debug_launchpad_preloading_preinitializing_daemon %if 0%{?appfw_feature_socket_activation} %{_unitdir_user}/debug-launchpad.service %{_unitdir_user}/debug-launchpad.socket diff --git a/src/debug-launchpad.c b/src/debug-launchpad.c index f7b7f46..30d5385 100644 --- a/src/debug-launchpad.c +++ b/src/debug-launchpad.c @@ -166,6 +166,56 @@ static int __prepare_fork(bundle *kb, const char *appid) return 0; } +static int __stdout_stderr_redirection(int caller_pid) +{ + char path[PATH_MAX]; + int fd; + int ret = 0; + + /* stdout */ + snprintf(path, sizeof(path), "/proc/%d/fd/1", caller_pid); + fd = open(path, O_WRONLY); + if (fd < 0) { + _E("Failed to open %s [%s]", path, strerror(errno)); + ret++; + } else { + dup2(fd, 1); + close(fd); + } + + /* stderr */ + snprintf(path, sizeof(path), "/proc/%d/fd/2", caller_pid); + fd = open(path, O_WRONLY); + if (fd < 0) { + _E("Failed to open %s [%s]", path, strerror(errno)); + ret += 2; + } else { + dup2(fd, 2); + close(fd); + } + + return ret; +} + +static int __get_caller_pid(bundle *kb) +{ + const char *pid_str; + int pid; + + pid_str = bundle_get_val(kb, AUL_K_ORG_CALLER_PID); + if (pid_str == NULL) + pid_str = bundle_get_val(kb, AUL_K_CALLER_PID); + + if (pid_str == NULL) + return -1; + + pid = atoi(pid_str); + if (pid <= 1) + return -1; + + return pid; +} + static int __normal_fork_exec(int argc, char **argv) { _D("start real fork and exec\n"); @@ -204,7 +254,6 @@ static void __real_launch(const char *app_path, bundle *kb) static int __start_process(const char *appid, const char *app_path, bundle *kb, appinfo_t *appinfo) { - char sock_path[PATH_MAX]; int pid; if (__prepare_fork(kb, appinfo->debug_appid) < 0) @@ -221,6 +270,9 @@ static int __start_process(const char *appid, const char *app_path, _close_all_fds(); _delete_sock_path(getpid(), getuid()); + if (__stdout_stderr_redirection(__get_caller_pid(kb))) + _E("__stdout_stderr_redirection() failed"); + PERF("prepare exec - fisrt done"); _D("lock up test log(no error): prepare exec - first done"); -- 2.7.4 From eb77501ac10aec61f1a9898cc51fd104aa92bf73 Mon Sep 17 00:00:00 2001 From: Hwankyu Jhun Date: Fri, 29 Jul 2016 12:46:55 +0900 Subject: [PATCH 14/16] Wait socket creation for stdout & stderr redirection Change-Id: I18ec250046dca0a64446ca5828649f699277d1c0 Signed-off-by: Hwankyu Jhun --- src/debug-launchpad.c | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/src/debug-launchpad.c b/src/debug-launchpad.c index 30d5385..f1a872c 100644 --- a/src/debug-launchpad.c +++ b/src/debug-launchpad.c @@ -63,6 +63,8 @@ static int __real_send(int clifd, int ret) static void __send_result_to_caller(int clifd, int ret) { int res; + int count = 0; + char path[PATH_MAX]; _W("Check app launching"); @@ -75,6 +77,20 @@ static void __send_result_to_caller(int clifd, int ret) return; } + snprintf(path, sizeof(path), "/run/aul/apps/%d/%d/.app-sock", + getuid(), ret); + _D("socket path: %s", path); + do { + if (access(path, F_OK) == 0) { + _D("%s exists", path); + break; + } + + _D("-- now wait socket creation --"); + usleep(50 * 1000); + count++; + } while (count < 20); + res = _proc_check_cmdline_bypid(ret); if (res < 0) { _E("The app process might be terminated " @@ -264,15 +280,15 @@ static int __start_process(const char *appid, const char *app_path, PERF("fork done"); _D("lock up test log(no error): fork done"); + if (__stdout_stderr_redirection(__get_caller_pid(kb))) + _E("__stdout_stderr_redirection() failed"); + _signal_unblock_sigchld(); _signal_fini(); _close_all_fds(); _delete_sock_path(getpid(), getuid()); - if (__stdout_stderr_redirection(__get_caller_pid(kb))) - _E("__stdout_stderr_redirection() failed"); - PERF("prepare exec - fisrt done"); _D("lock up test log(no error): prepare exec - first done"); -- 2.7.4 From 56c25b836dc2b121c4c2c944484a7d0625593e85 Mon Sep 17 00:00:00 2001 From: Hwankyu Jhun Date: Fri, 29 Jul 2016 13:47:29 +0900 Subject: [PATCH 15/16] Add an exception handling Change-Id: I698ca846bfbf92bff906250020dba83de95b4763 Signed-off-by: Hwankyu Jhun --- src/common.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/common.c b/src/common.c index c98ad02..343e62d 100644 --- a/src/common.c +++ b/src/common.c @@ -498,6 +498,9 @@ static char *__get_libdir(const char *path) return NULL; ptr = strrchr(path_dup, '/'); + if (ptr == NULL) + return NULL; + *ptr = '\0'; snprintf(buf, sizeof(buf), "%s/../lib/", path_dup); -- 2.7.4 From 28fd55525dfaf497222cc3456feb35c10a8cdaca Mon Sep 17 00:00:00 2001 From: Hwankyu Jhun Date: Mon, 8 Aug 2016 14:46:09 +0900 Subject: [PATCH 16/16] Remove checking socket creation Change-Id: I9c84584498a4feae91249f13152f2a06314ebe5d Signed-off-by: Hwankyu Jhun --- include/common.h | 2 -- src/common.c | 52 --------------------------------------------------- src/debug-launchpad.c | 30 +---------------------------- 3 files changed, 1 insertion(+), 83 deletions(-) diff --git a/include/common.h b/include/common.h index 73dada5..d72241c 100644 --- a/include/common.h +++ b/include/common.h @@ -68,8 +68,6 @@ void _appinfo_free(appinfo_t *appinfo); void _modify_bundle(bundle *kb, int caller_pid, appinfo_t *appinfo, int cmd); void _set_env(appinfo_t *app_info, bundle *kb); char **_create_argc_argv(bundle *kb, int *margc, const char *app_path); -int _proc_check_cmdline_bypid(int pid); -void _prepare_listen_sock(void); int _delete_sock_path(int pid, uid_t uid); int _close_all_fds(void); diff --git a/src/common.c b/src/common.c index 343e62d..8912352 100644 --- a/src/common.c +++ b/src/common.c @@ -780,58 +780,6 @@ char **_create_argc_argv(bundle *kb, int *margc, const char *app_path) return argv; } -static int __read_proc(const char *path, char *buf, int size) -{ - int fd; - int ret; - - if (buf == NULL || path == NULL) - return -1; - - fd = open(path, O_RDONLY); - if (fd < 0) - return -1; - - ret = read(fd, buf, size - 1); - if (ret <= 0) { - close(fd); - return -1; - } - - buf[ret] = 0; - close(fd); - - return ret; -} - -int _proc_check_cmdline_bypid(int pid) -{ - char buf[MAX_CMD_BUFSZ]; - int ret; - - snprintf(buf, sizeof(buf), "/proc/%d/cmdline", pid); - ret = __read_proc(buf, buf, sizeof(buf)); - if (ret <= 0) - return -1; - - _D("cmdline: %s", buf); - - return 0; -} - -void _prepare_listen_sock(void) -{ - int fd; - char buf[12]; - - fd = __create_server_socket(true); - if (fd < 0) - return; - - snprintf(buf, sizeof(buf), "%d", fd); - setenv("AUL_LISTEN_SOCK", buf, 1); -} - static int __delete_dir(const char *path) { DIR *dp; diff --git a/src/debug-launchpad.c b/src/debug-launchpad.c index f1a872c..c67df3c 100644 --- a/src/debug-launchpad.c +++ b/src/debug-launchpad.c @@ -62,11 +62,7 @@ static int __real_send(int clifd, int ret) static void __send_result_to_caller(int clifd, int ret) { - int res; - int count = 0; - char path[PATH_MAX]; - - _W("Check app launching"); + _W("send result: %d", ret); if (clifd == -1) return; @@ -77,28 +73,6 @@ static void __send_result_to_caller(int clifd, int ret) return; } - snprintf(path, sizeof(path), "/run/aul/apps/%d/%d/.app-sock", - getuid(), ret); - _D("socket path: %s", path); - do { - if (access(path, F_OK) == 0) { - _D("%s exists", path); - break; - } - - _D("-- now wait socket creation --"); - usleep(50 * 1000); - count++; - } while (count < 20); - - res = _proc_check_cmdline_bypid(ret); - if (res < 0) { - _E("The app process might be terminated " - "while we are wating %d", ret); - __real_send(clifd, -1); /* abnormally launched */ - return; - } - if (__real_send(clifd, ret) < 0) { if (kill(ret, SIGKILL) == -1) _E("Failed to send SIGKILL: %d", errno); @@ -147,8 +121,6 @@ static int __prepare_exec(const char *appid, const char *app_path, return -1; } - _prepare_listen_sock(); - memset(process_name, '\0', AUL_PR_NAME); snprintf(process_name, AUL_PR_NAME, "%s", file_name); prctl(PR_SET_NAME, process_name); -- 2.7.4