Skip to content

Commit

Permalink
fix(dracut-install): tweaks to get_real_file()
Browse files Browse the repository at this point in the history
Fix potential memory leaks in two locations and use
_exit(EXIT_FAILURE) if asprintf(&abspath, ...) fails.

Signed-off-by: Zoltán Böszörményi <zboszor@gmail.com>
  • Loading branch information
zboszor authored and johannbg committed Jul 19, 2021
1 parent 6c71ba4 commit 1beeaf3
Showing 1 changed file with 18 additions and 9 deletions.
27 changes: 18 additions & 9 deletions src/install/dracut-install.c
Original file line number Diff line number Diff line change
Expand Up @@ -419,7 +419,7 @@ static char *get_real_file(const char *src, bool fullyresolve)
char linktarget[PATH_MAX + 1];
ssize_t linksz;
_cleanup_free_ char *fullsrcpath;
char *abspath = NULL;
char *abspath = NULL; /* this is returned from the function, don't _cleanup_free_ */
struct stat sb;

if (sysrootdirlen) {
Expand Down Expand Up @@ -455,30 +455,39 @@ static char *get_real_file(const char *src, bool fullyresolve)
log_debug("get_real_file: readlink('%s') returns '%s'", fullsrcpath, linktarget);

if (linktarget[0] == '/') {
if (asprintf(&abspath, "%s%s", (sysrootdirlen ? sysrootdir : ""), linktarget) < 0)
return NULL;
if (asprintf(&abspath, "%s%s", (sysrootdirlen ? sysrootdir : ""), linktarget) < 0) {
log_error("Out of memory!");
_exit(EXIT_FAILURE);
}
} else {
_cleanup_free_ char *fullsrcdir = strdup(fullsrcpath);

if (!fullsrcdir) {
log_error("Out of memory!");
return NULL;
_exit(EXIT_FAILURE);
}

fullsrcdir[dir_len(fullsrcdir)] = '\0';

if (asprintf(&abspath, "%s/%s", fullsrcdir, linktarget) < 0)
return NULL;
if (asprintf(&abspath, "%s/%s", fullsrcdir, linktarget) < 0) {
log_error("Out of memory!");
_exit(EXIT_FAILURE);
}
}

if (fullyresolve) {
struct stat st;
if (lstat(abspath, &st) < 0) {
if (errno != ENOENT)
if (errno != ENOENT) {
free(abspath);
return NULL;
}
}
if (S_ISLNK(st.st_mode)) {
char *tmp = get_real_file(abspath, fullyresolve);
free(abspath);
abspath = tmp;
}
if (S_ISLNK(st.st_mode))
return get_real_file(abspath, fullyresolve);
}

log_debug("get_real_file('%s') => '%s'", src, abspath);
Expand Down

0 comments on commit 1beeaf3

Please sign in to comment.