Fix to get process identifier from cmdline
authorSangwan Kwon <sangwan.kwon@samsung.com>
Tue, 25 Feb 2020 06:53:41 +0000 (15:53 +0900)
committer권상완/Security 2Lab(SR)/Engineer/삼성전자 <sangwan.kwon@samsung.com>
Wed, 26 Feb 2020 04:22:05 +0000 (13:22 +0900)
Signed-off-by: Sangwan Kwon <sangwan.kwon@samsung.com>
src/vist/common/tests/process.cpp
src/vist/policy/api.cpp
src/vist/process.hpp
src/vist/service/tests/core.cpp

index fea8f1f4bca57759cbbc91d15cf121c18835ddae..a7b79d49293f60edb1bb66718c71e27dc0e1ae47 100644 (file)
@@ -24,15 +24,15 @@ using namespace vist;
 
 TEST(ProcessTests, path_positive)
 {
-       auto name = Process::GetPath(Process::GetPid());
-       EXPECT_EQ("/usr/bin/vist-test", name);
+       auto name = Process::GetIdentifier(Process::GetPid());
+       EXPECT_EQ("vist-test", name);
 }
 
 TEST(ProcessTests, path_negative)
 {
        bool raised = false;
        try {
-               Process::GetPath(-1);
+               Process::GetIdentifier(-1);
        } catch(...) {
                raised = true;
        }
index 61ff6764c78316af08fc756c5aa15f179b6fb8a7..98e6b6b6baf37a224f618f92759ebf3b8d098212 100644 (file)
@@ -39,9 +39,9 @@ void API::Admin::Set(const std::string& policy, const PolicyValue& value)
        std::string admin;
        auto peer = rmi::Gateway::GetPeerCredentials();
        if (peer == nullptr)
-               admin = Process::GetPath(Process::GetPid());
+               admin = Process::GetIdentifier(Process::GetPid());
        else
-               admin = Process::GetPath(peer->pid);
+               admin = Process::GetIdentifier(peer->pid);
 
        PolicyManager::Instance().set(policy, value, admin);
 }
index 29e4ebc66fd3bcbe9793a6b2dbb249898ef7fd69..ff7548d2c25b9647b4b9ef97346e3bccc79b2915 100644 (file)
@@ -39,34 +39,24 @@ struct Process {
                return ::getpid();
        }
 
-       /// TODO(Sangwan): Unify the method which get process identifier
-       static std::string GetPath(pid_t pid)
+       static std::string GetIdentifier(pid_t pid)
        {
-               std::string exe = "/proc/" + std::to_string(pid) + "/exe";
-
-               /// c++17 std::filesystem::read_symlink
-               std::vector<char> buf(1024);
                errno = 0;
-               auto size = ::readlink(exe.c_str(), buf.data(), buf.size());
-               if (size == -1) {
-                       WARN(VIST) << "Failed to get process path by exe: " << exe
-                                          << ", errno: " << errno;
-
-                       std::string cmdline = "/proc/" + std::to_string(pid) + "/cmdline";
-                       int fd = ::open(cmdline.c_str(), O_RDONLY);
-                       if (fd == -1)
-                               THROW(ErrCode::RuntimeError) << "Failed to get process path: " << cmdline;
+               std::string cmdline = "/proc/" + std::to_string(pid) + "/cmdline";
+               int fd = ::open(cmdline.c_str(), O_RDONLY);
+               if (fd == -1)
+                       THROW(ErrCode::RuntimeError) << "Failed to get process path: " << cmdline;
 
-                       errno = 0;
-                       size = ::read(fd, buf.data(), buf.size());
-                       ::close(fd);
+               errno = 0;
+               std::vector<char> buf(1024);
+               auto size = ::read(fd, buf.data(), buf.size());
+               ::close(fd);
 
-                       if (size == -1)
-                               THROW(ErrCode::RuntimeError) << "Failed to get process path: " << cmdline
-                                                                                        << ", errno: " << errno;
+               if (size == -1)
+                       THROW(ErrCode::RuntimeError) << "Failed to get process path: " << cmdline
+                                                                                << ", errno: " << errno;
 
-                       buf[size - 1] = '\0';
-               }
+               buf[size - 1] = '\0';
 
                return canonicalize(std::string(buf.begin(), buf.begin() + size));
        }
@@ -74,9 +64,18 @@ struct Process {
 private:
        static std::string canonicalize(std::string&& s)
        {
-               auto predicate = [](unsigned char c){ return std::isspace(c) || c == '\0'; };
-               auto base = std::find_if(s.begin(), s.end(), predicate);
-               s.erase(base, s.end());
+               { /// rtrim
+                       auto predicate = [](unsigned char c){ return std::isspace(c) || c == '\0'; };
+                       auto base = std::find_if(s.begin(), s.end(), predicate);
+                       s.erase(base, s.end());
+               }
+
+               { /// ltrim
+                       auto predicate = [](unsigned char c){ return c == '/'; };
+                       auto base = std::find_if(s.rbegin(), s.rend(), predicate).base();
+                       s.erase(s.begin(), base);
+               }
+
                return s;
        }
 };
index c7fc56486c82c64b82fc0a8b6c7640b32db4cf27..83d6f730293b5b762cf9ca04dbf89f54063fe73a 100644 (file)
@@ -41,7 +41,7 @@ TEST_F(CoreTests, query_select)
 
 TEST_F(CoreTests, query_update)
 {
-       policy::API::Admin::Enroll("/usr/bin/vist-test");
+       policy::API::Admin::Enroll("vist-test");
 
        std::string statement = "SELECT * FROM policy WHERE name = 'sample-int-policy'";
        auto rows = Vistd::Query(statement);
@@ -56,5 +56,5 @@ TEST_F(CoreTests, query_update)
        rows = Vistd::Query(statement);
        EXPECT_EQ(rows[0]["value"], "I/10");
 
-       policy::API::Admin::Disenroll("/usr/bin/vist-test");
+       policy::API::Admin::Disenroll("vist-test");
 }