Skip to content

Commit b784a33

Browse files
LUU QUANG MINHLUU QUANG MINH
LUU QUANG MINH
authored and
LUU QUANG MINH
committed
daemon: Prevent new daemon created on same machine
+ Only one daemon on the machine listening to fifo + fifo will not be unlinked by the new daemon init + Open fd and work with fchown avoiding TOCTOU Signed-off-by: LUU QUANG MINH <Minh.LuuQuang@vn.bosch.com> Signed-off-by: andv <fixed-term.An.DuVan@vn.bosch.com>
1 parent 8fc4e8e commit b784a33

File tree

2 files changed

+16
-11
lines changed

2 files changed

+16
-11
lines changed

CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -380,6 +380,7 @@ if(WITH_GIT_SUBMODULE)
380380
endif()
381381

382382
if(WITH_DLT_UNIT_TESTS)
383+
set(DLT_IPC "UNIX_SOCKET")
383384
find_package(GTest)
384385
if(GTEST_FOUND)
385386
find_package(PkgConfig REQUIRED)

src/daemon/dlt-daemon.c

+15-11
Original file line numberDiff line numberDiff line change
@@ -1590,9 +1590,13 @@ static int dlt_daemon_init_fifo(DltDaemonLocal *daemon_local)
15901590
/* open named pipe(FIFO) to receive DLT messages from users */
15911591
umask(0);
15921592

1593-
/* Try to delete existing pipe, ignore result of unlink */
1593+
/* Valid fifo means there is a daemon running, stop init phase of the new */
15941594
const char *tmpFifo = daemon_local->flags.daemonFifoName;
1595-
unlink(tmpFifo);
1595+
if (access(tmpFifo, F_OK) == 0) {
1596+
dlt_vlog(LOG_WARNING, "FIFO user %s is in use (%s)!\n",
1597+
tmpFifo, strerror(errno));
1598+
return -1;
1599+
}
15961600

15971601
ret = mkfifo(tmpFifo, S_IRUSR | S_IWUSR | S_IWGRP);
15981602

@@ -1602,13 +1606,21 @@ static int dlt_daemon_init_fifo(DltDaemonLocal *daemon_local)
16021606
return -1;
16031607
} /* if */
16041608

1609+
fd = open(tmpFifo, O_RDWR);
1610+
1611+
if (fd == -1) {
1612+
dlt_vlog(LOG_WARNING, "FIFO user %s cannot be opened (%s)!\n",
1613+
tmpFifo, strerror(errno));
1614+
return -1;
1615+
} /* if */
1616+
16051617
/* Set group of daemon FIFO */
16061618
if (daemon_local->flags.daemonFifoGroup[0] != 0) {
16071619
errno = 0;
16081620
struct group *group_dlt = getgrnam(daemon_local->flags.daemonFifoGroup);
16091621

16101622
if (group_dlt) {
1611-
ret = chown(tmpFifo, -1, group_dlt->gr_gid);
1623+
ret = fchown(fd, -1, group_dlt->gr_gid);
16121624

16131625
if (ret == -1)
16141626
dlt_vlog(LOG_ERR, "FIFO user %s cannot be chowned to group %s (%s)\n",
@@ -1628,14 +1640,6 @@ static int dlt_daemon_init_fifo(DltDaemonLocal *daemon_local)
16281640
}
16291641
}
16301642

1631-
fd = open(tmpFifo, O_RDWR);
1632-
1633-
if (fd == -1) {
1634-
dlt_vlog(LOG_WARNING, "FIFO user %s cannot be opened (%s)!\n",
1635-
tmpFifo, strerror(errno));
1636-
return -1;
1637-
} /* if */
1638-
16391643
#ifdef __linux__
16401644
/* F_SETPIPE_SZ and F_GETPIPE_SZ are only supported for Linux.
16411645
* For other OSes it depends on its system e.g. pipe manager.

0 commit comments

Comments
 (0)