diff options
author | José Bollo <jose.bollo@iot.bzh> | 2018-07-13 14:33:56 +0200 |
---|---|---|
committer | José Bollo <jose.bollo@iot.bzh> | 2018-07-13 15:24:14 +0200 |
commit | 65141ebcb134f670a87233a88b8d51e4a671272e (patch) | |
tree | 811127c56d21eb66ddd825bea082353ebbfe6c25 | |
parent | 1e1d181ac5226b2e1b26192d36e13b5a44148139 (diff) |
afb-hook & afb-trace: Fix usage of readlink
The function readlink doesn't add a terminating null.
This patch fixes that were needed.
Change-Id: Ie15309471fe8203c8e98b97315c4ea30de3ccd75
Signed-off-by: José Bollo <jose.bollo@iot.bzh>
-rw-r--r-- | src/afb-hook.c | 10 | ||||
-rw-r--r-- | src/afb-trace.c | 8 |
2 files changed, 14 insertions, 4 deletions
diff --git a/src/afb-hook.c b/src/afb-hook.c index d9fd1a37..48eb582a 100644 --- a/src/afb-hook.c +++ b/src/afb-hook.c @@ -736,11 +736,14 @@ static void hook_api_event_make_cb(void *closure, const struct afb_hookid *hooki static void hook_api_rootdir_get_fd_cb(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, int result) { char path[PATH_MAX], proc[100]; + ssize_t s; + if (result < 0) _hook_api_(export, "rootdir_get_fd() -> %d, %m", result); else { snprintf(proc, sizeof proc, "/proc/self/fd/%d", result); - readlink(proc, path, sizeof path); + s = readlink(proc, path, sizeof path); + path[s < 0 ? 0 : s >= sizeof path ? sizeof path - 1 : s] = 0; _hook_api_(export, "rootdir_get_fd() -> %d = %s", result, path); } } @@ -748,13 +751,16 @@ static void hook_api_rootdir_get_fd_cb(void *closure, const struct afb_hookid *h static void hook_api_rootdir_open_locale_cb(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, const char *filename, int flags, const char *locale, int result) { char path[PATH_MAX], proc[100]; + ssize_t s; + if (!locale) locale = "(null)"; if (result < 0) _hook_api_(export, "rootdir_open_locale(%s, %d, %s) -> %d, %m", filename, flags, locale, result); else { snprintf(proc, sizeof proc, "/proc/self/fd/%d", result); - readlink(proc, path, sizeof path); + s = readlink(proc, path, sizeof path); + path[s < 0 ? 0 : s >= sizeof path ? sizeof path - 1 : s] = 0; _hook_api_(export, "rootdir_open_locale(%s, %d, %s) -> %d = %s", filename, flags, locale, result, path); } } diff --git a/src/afb-trace.c b/src/afb-trace.c index dd6e863c..e7ae9fc0 100644 --- a/src/afb-trace.c +++ b/src/afb-trace.c @@ -687,10 +687,12 @@ static void hook_api_rootdir_get_fd(void *closure, const struct afb_hookid *hook { char path[PATH_MAX], proc[100]; const char *key, *val; + ssize_t s; if (result >= 0) { snprintf(proc, sizeof proc, "/proc/self/fd/%d", result); - readlink(proc, path, sizeof path); + s = readlink(proc, path, sizeof path); + path[s < 0 ? 0 : s >= sizeof path ? sizeof path - 1 : s] = 0; key = "path"; val = path; } else { @@ -705,10 +707,12 @@ static void hook_api_rootdir_open_locale(void *closure, const struct afb_hookid { char path[PATH_MAX], proc[100]; const char *key, *val; + ssize_t s; if (result >= 0) { snprintf(proc, sizeof proc, "/proc/self/fd/%d", result); - readlink(proc, path, sizeof path); + s = readlink(proc, path, sizeof path); + path[s < 0 ? 0 : s >= sizeof path ? sizeof path - 1 : s] = 0; key = "path"; val = path; } else { |