From e7e4c92650892cf67210be5ea89ffba967427cbf Mon Sep 17 00:00:00 2001 From: =?utf8?q?=C3=89rico=20Rolim?= Date: Tue, 27 Oct 2020 16:19:58 -0300 Subject: [PATCH] unstrip: Stop using strndupa. MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit This functon has inherent safety issues, since a long enough path can lead to memory clobbering. Due to the recursive nature of make_directories(), multiple calls could also stack overflow. Instead, the string can be allocated in the heap. As a bonus, this improves musl compatibility, since musl doesn't include the strndupa macro for now. Also add braces around while loop. Signed-off-by: Érico Rolim --- src/ChangeLog | 4 ++++ src/unstrip.c | 16 +++++++++++----- 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/src/ChangeLog b/src/ChangeLog index 112b2242a..7f923c5d6 100644 --- a/src/ChangeLog +++ b/src/ChangeLog @@ -1,3 +1,7 @@ +2020-10-27 Érico N. Rolim + + * unstrip.c (make_directories): Use strndup, not strndupa. + 2020-09-28 Mark Wielaard * readelf.c (next_listptr_offset): Take idx as pointer, to be updated diff --git a/src/unstrip.c b/src/unstrip.c index a855038af..0257d9cc3 100644 --- a/src/unstrip.c +++ b/src/unstrip.c @@ -311,12 +311,18 @@ make_directories (const char *path) if (lastslash == path) return; - char *dir = strndupa (path, lastslash - path); + char *dir = strndup (path, lastslash - path); + if (dir == NULL) + error(EXIT_FAILURE, errno, _("memory exhausted")); + while (mkdir (dir, 0777) < 0 && errno != EEXIST) - if (errno == ENOENT) - make_directories (dir); - else - error (EXIT_FAILURE, errno, _("cannot create directory '%s'"), dir); + { + if (errno == ENOENT) + make_directories (dir); + else + error (EXIT_FAILURE, errno, _("cannot create directory '%s'"), dir); + } + free (dir); } /* Keep track of new section data we are creating, so we can free it -- 2.47.3