Fix: Tests: Segfault in `test_utils_expand_path()`
authorFrancis Deslauriers <francis.deslauriers@efficios.com>
Fri, 27 Sep 2019 19:14:17 +0000 (15:14 -0400)
committerJérémie Galarneau <jeremie.galarneau@efficios.com>
Tue, 1 Oct 2019 18:44:11 +0000 (14:44 -0400)
Background
==========
I have a file named "/a" on my file system (don't ask why).

Issue
=====
While running the `test_utils_expand_path` test case on my machine, I
get a Segfault. Here is the gdb backtrace:

  #0  __strlen_avx2 () at ../sysdeps/x86_64/multiarch/strlen-avx2.S:62
  #1  0x0000555555559eb7 in expand_double_slashes_dot_and_dotdot (path=0x0) at utils.c:223
  #2  0x000055555555a2d8 in _utils_expand_path (path=0x55555556b250 "/a/b/c/d/e", keep_symlink=true) at utils.c:384
  #3  0x000055555555a408 in utils_expand_path (path=0x55555556b250 "/a/b/c/d/e") at utils.c:423
  #4  0x000055555555859e in test_utils_expand_path () at test_utils_expand_path.c:291
  #5  0x00005555555589b0 in main (argc=1, argv=0x7fffffffe5e8) at test_utils_expand_path.c:352

I get this backtrace because the function `utils_partial_realpath()`
returns NULL when it tries to expand the "/a/b/c/d/e" path and realize
that it could not exist since "/a" is a file and not a directory.

Anyways, the returned NULL pointer is ignored and directly used in the
`expand_double_slashes_dot_and_dotdot()` function right after.

This configuration ("/a" being a file) is expected to fail but not to segfault.
It could be reproduce in a real scenario when creating directory
structures.

Solution
========
Return an error if `utils_partial_realpath()` returns NULL.

Signed-off-by: Francis Deslauriers <francis.deslauriers@efficios.com>
Signed-off-by: Jérémie Galarneau <jeremie.galarneau@efficios.com>
src/common/utils.c

index b0e5b63e34400c3f0e2e2df57b58fcdf0f875f32..17a313ee16f66efe8c574ddfddc8f94f156b9e11 100644 (file)
@@ -379,6 +379,9 @@ char *_utils_expand_path(const char *path, bool keep_symlink)
                /* Resolve partially our path */
                absolute_path = utils_partial_realpath(absolute_path,
                                absolute_path, LTTNG_PATH_MAX);
+               if (!absolute_path) {
+                       goto error;
+               }
        }
 
        ret = expand_double_slashes_dot_and_dotdot(absolute_path);
This page took 0.027936 seconds and 5 git commands to generate.