fopen: add realpath before fopen 95/161895/2
authorSooyoung Ha <yoosah.ha@samsung.com>
Tue, 28 Nov 2017 06:04:48 +0000 (15:04 +0900)
committerSooyoung Ha <yoosah.ha@samsung.com>
Tue, 28 Nov 2017 06:35:51 +0000 (15:35 +0900)
To prevent path-modification attack using symbolic link.

Change-Id: Id5133df718b1e14a22849920082ff1717bc417e6
Signed-off-by: Sooyoung Ha <yoosah.ha@samsung.com>
src/sdb.c

index cd9bada..51f6b7e 100644 (file)
--- a/src/sdb.c
+++ b/src/sdb.c
@@ -1239,11 +1239,18 @@ int daemonize(void) {
         _exit(0);
     }
 #ifdef SDB_PIDPATH
-    FILE *f = fopen(SDB_PIDPATH, "w");
-
-    if (f != NULL) {
-        fprintf(f, "%d\n", getpid());
-        fclose(f);
+    char* tmppath = NULL;
+    tmppath = realpath(SDB_PIDPATH, NULL);
+    if (tmppath == NULL && errno == ENOENT) {
+        FILE *f = fopen(SDB_PIDPATH, "w");
+
+        if (f != NULL) {
+            fprintf(f, "%d\n", getpid());
+            fclose(f);
+        }
+    } else {
+        D("sdbd: %s file is existed. It might not work properly.\n", SDB_PIDPATH);
+        free(tmppath);
     }
 #endif
     if (setsid() == -1)