DSUtilSocket: set permission to sockets 97/242097/1
authorjeon <jhyuni.kang@samsung.com>
Fri, 21 Aug 2020 11:49:05 +0000 (20:49 +0900)
committerSung-Jin Park <sj76.park@samsung.com>
Mon, 24 Aug 2020 06:32:03 +0000 (15:32 +0900)
Change-Id: I3328fa30d8a55b2f11accc01a28821b3e2d2ecc9

src/DSBuffer/DSBufferManager.cpp
src/DSDisplayDevice/DSDisplayDeviceTDMImpl.cpp
src/DSUtil/DSUtilSocket.cpp
src/DSUtil/DSUtilSocket.h
src/DSWaylandServer/DSWaylandCompositor.cpp

index 3c3cc14..3e2b7ae 100644 (file)
@@ -25,6 +25,7 @@
 #include "DSBufferManagerPrivate.h"
 #include "DSDebugLog.h"
 #include "DSBufferTBMImpl.h"
+#include "DSUtilSocket.h"
 
 namespace display_server
 {
@@ -137,6 +138,10 @@ DSBufferManagerPrivate::DSBufferManagerPrivate(DSBufferManager *p_ptr)
        if (__waylandCompositor && bufmgr)
                tbm_bufmgr_bind_native_display(bufmgr, __waylandCompositor->display());
 
+       DSUtilSocket *utilSocket = DSUtilSocket::getInstance();
+       utilSocket->initSocket("tbm-drm-auth");
+       utilSocket->releaseInstance();
+
 }
 
 std::shared_ptr<IDSBuffer> DSBufferManagerPrivate::getDSBuffer(struct ::wl_resource *bufferResource)
index ecf0415..b2c4339 100644 (file)
@@ -25,6 +25,7 @@
 #include "DSDisplayDeviceOutputTDMImpl.h"
 #include "DSDisplayDeviceHWCTDMImpl.h"
 #include "DSDebugLog.h"
+#include "DSUtilSocket.h"
 
 namespace display_server
 {
@@ -55,6 +56,10 @@ DSDisplayDeviceTDMImpl::DSDisplayDeviceTDMImpl()
 
                __outputList.emplace_back(std::make_shared<DSDisplayDeviceOutputTDMImpl>(toutput));
        }
+
+       DSUtilSocket *utilSocket = DSUtilSocket::getInstance();
+       utilSocket->initSocket("tdm-socket");
+       utilSocket->releaseInstance();
 }
 
 DSDisplayDeviceTDMImpl::~DSDisplayDeviceTDMImpl()
index 4307a75..9c0e0ff 100644 (file)
 */
 
 #include "DSUtilSocket.h"
+#include <malloc.h>
+#include <sys/xattr.h>
+#include <sys/stat.h>
+#include <errno.h>
+#include <unistd.h>
+#include <string.h>
+#include <sys/types.h>
+#include <pwd.h>
+#include <grp.h>
 
 namespace display_server
 {
@@ -80,5 +89,146 @@ void DSUtilSocket::releaseInstance()
        }
 }
 
+void DSUtilSocket::initSocket(std::string socketName)
+{
+       char *dir = nullptr;
+       int res;
+#undef STRERR_BUFSIZE
+#define STRERR_BUFSIZE 1024
+       char buf[STRERR_BUFSIZE];
+
+       if (socketName.empty()) return;
+
+       dir = getenv("XDG_RUNTIME_DIR");
+       if (!dir) return;
+
+       std::string xdgDir(dir);
+
+       /* check whether buffer size is less than concatenated string which
+        * is made of XDG_RUNTIME_DIR, '/', socket name and NULL.
+        */
+
+       int dirLength = xdgDir.length();
+       int nameLength = socketName.length();
+       if ((dirLength + nameLength + 2) > STRERR_BUFSIZE)
+     {
+        DSLOG_WRN("DSUtilSocket", "Size of buffer is not enough. dir:%s name:%s", xdgDir, socketName);
+        return;
+     }
+
+       //snprintf(socket_path, sizeof(socket_path), "%s/%s", dir, name);
+       std::string socketPath = xdgDir + "/" + socketName;
+
+       uid_t uid = __getpwnam("root");
+       gid_t gid = __getgrnam("display");
+
+       res = chmod(socketPath.c_str(), 509);
+       if (res < 0)
+       {
+               DSLOG_WRN("DSUtilSocket", "Could not change modes of socket file:%s (%s)", socketPath.c_str(), strerror_r(errno, buf, STRERR_BUFSIZE));
+               //PRCTL("[Winsys] Could not chane modes of socket file: %s", socketPath.c_str());
+               return;
+       }
+
+       res = chown(socketPath.c_str(), uid, gid);
+       if (res < 0)
+       {
+               DSLOG_WRN("DSUtilSocket", "Could not change owner of socket file:%s (%s)", socketPath.c_str(), strerror_r(errno, buf, STRERR_BUFSIZE));
+               //PRCTL("[Winsys] Could not change owner of socket file: %s", socketPath.c_str());
+               return;
+       }
+}
+
+int DSUtilSocket::__getpwnam(std::string name)
+{
+       struct ::passwd *u;
+       struct ::passwd *uRes;
+       char* buf;
+       size_t bufLen;
+       int ret;
+#undef BUFLEN
+#define BUFLEN 2048
+       bufLen = sysconf(_SC_GETPW_R_SIZE_MAX);
+       if ((int)bufLen == -1)          /* Value was indeterminate */
+               bufLen = BUFLEN;        /* Should be more than enough */
+#undef BUFLEN
+
+       buf = (char *)malloc(bufLen);
+       if (buf == NULL)
+       {
+               DSLOG_WRN("DSUtilSocket", "failed to create buffer");
+               return 0;
+       }
+
+       u = (struct ::passwd *)malloc(sizeof(struct passwd));
+       if (!u)
+       {
+               DSLOG_WRN("DSUtilSocket", "failed to create password struct");
+               free(buf);
+               return 0;
+       }
+
+       ret = getpwnam_r(name.c_str(), u, buf, bufLen, &uRes);
+       if (uRes == nullptr)
+       {
+               if (ret == 0) DSLOG_WRN("DSUtilSocket", "password not found");
+               else DSLOG_WRN("DSUtilSocket", "errno returned by getpwnam_r is %d", ret);
+               free(buf);
+               free(u);
+               return 0;
+       }
+       ret = u->pw_uid;
+       free(buf);
+       free(u);
+
+       return ret;
+}
+
+int DSUtilSocket::__getgrnam(std::string name)
+{
+       struct ::group *g;
+       struct ::group *grpRes;
+       char* buf;
+       size_t bufLen;
+       int ret;
+#undef BUFLEN
+#define BUFLEN 2048
+       bufLen = sysconf(_SC_GETGR_R_SIZE_MAX);
+       if ((int)bufLen == -1)          /* Value was indeterminate */
+               bufLen = BUFLEN;        /* Should be more than enough */
+#undef BUFLEN
+
+       buf = (char *)malloc(bufLen);
+       if (buf == nullptr)
+       {
+               DSLOG_WRN("DSUtilSocket", "failed to create buffer");
+               return 0;
+       }
+
+       g = (struct ::group *)malloc(sizeof(struct group));
+       if (!g)
+       {
+               DSLOG_WRN("DSUtilSocket", "failed to create group struct");
+               free(buf);
+               return 0;
+       }
+
+       ret = getgrnam_r(name.c_str(), g, buf, bufLen, &grpRes);
+       if (grpRes == NULL)
+       {
+               if (ret == 0) DSLOG_WRN("DSUtilSocket", "Group not found");
+               else DSLOG_WRN("DSUtilSocket", "errno returned by getpwnam_r is %d", ret);
+               free(buf);
+               free(g);
+               return 0;
+       }
+
+       ret = g->gr_gid;
+       free(buf);
+       free(g);
+       return ret;
+}
+
+
 
 }
index 309f1ef..dffdee4 100644 (file)
@@ -35,10 +35,14 @@ public:
        static DSUtilSocket *getInstance();
        static void releaseInstance();
 
+       void initSocket(std::string socketName);
+
 private:
        DSUtilSocket();
        ~DSUtilSocket();
        DSUtilSocket& operator=(const DSUtilSocket&) = delete;
+       int __getpwnam(std::string name);
+       int __getgrnam(std::string name);
 
        static std::mutex __mutex;
        static DSUtilSocket *__utilSocket;
index 9f823ee..1570727 100644 (file)
@@ -29,6 +29,7 @@
 #include "DSWaylandSurface.h"
 #include "DSWaylandRegion.h"
 #include "DSDebugLog.h"
+#include "DSUtilSocket.h"
 
 namespace display_server
 {
@@ -126,6 +127,10 @@ void DSWaylandCompositorPrivate::initSocket(std::string sName, std::string sDir)
 
        (void) socketPath;
 
+       DSUtilSocket *utilSocket = DSUtilSocket::getInstance();
+       utilSocket->initSocket(sName);
+       utilSocket->releaseInstance();
+
        //TODO : set permissions for Owner, Group and Other
        //TODO : set Smack attributes for socket
        //TODO : set symbolic link for socket