From 6306c757a2d04ed671eaa4ccc31661e1099acc1a Mon Sep 17 00:00:00 2001 From: Joachim Nilsson Date: Wed, 22 Apr 2020 14:23:04 +0200 Subject: [PATCH] src: svc: Use realpath() to check if untangled file exists The svc_find_by_pidfile() function may be called with "/run/./foo.pid", or similar, which must be sanitized before being compared to the PID file of each svc_t. This patch introduce realpath() to untangle the paths and check if the file actually exists. Signed-off-by: Joachim Nilsson --- src/svc.c | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/src/svc.c b/src/svc.c index 230011d3..230573d4 100644 --- a/src/svc.c +++ b/src/svc.c @@ -390,13 +390,28 @@ svc_t *svc_find_by_nameid(char *name, char *id) svc_t *svc_find_by_pidfile(char *fn) { svc_t *svc, *iter = NULL; + char adjpath[strlen(fn) + 10]; /* + sizeof("/var/run/") */ + char fnpath[PATH_MAX]; + + pid_runpath(fn, adjpath, sizeof(adjpath)); + if (!realpath(adjpath, fnpath)) { + _pe("adjpath: %s, errno %d", adjpath, errno); + return NULL; + } for (svc = svc_iterator(&iter, 1); svc; svc = svc_iterator(&iter, 0)) { - char path[MAX_ARG_LEN]; + char path[PATH_MAX]; + char *pidfn; pid_t pid; - pid_runpath(fn, path, sizeof(path)); - if (!string_compare(path, pid_file(svc))) + pidfn = pid_file(svc); + if (!pidfn || !realpath(pidfn, path)) { + if (errno != ENOENT) + _pe("pidfn: %s, errno %d", pidfn ?: "", errno); + continue; + } + + if (!string_compare(fnpath, path)) continue; pid = pid_file_read(path);