Browse Source

fl_wrapper: sanitise to prevent buffer overflow warnings

Signed-off-by: Alejandro Mery <amery@jpi.io>
master
Alejandro Mery 3 months ago
committed by Alejandro Mery
parent
commit
0e447e6d02
1 changed files with 34 additions and 23 deletions
  1. +34
    -23
      src/tools-source/fl_wrapper/fl_wrapper.c

+ 34
- 23
src/tools-source/fl_wrapper/fl_wrapper.c

@ -23,7 +23,6 @@
* - /usr/include/dlfcn.h
*/
/* Headers and prototypes */
#ifndef DEBUG
@ -110,16 +109,17 @@ static void * get_dl_symbol(char * symname)
static inline ssize_t readlink_from(char *buf, size_t buf_len, const char *tpl, ...)
{
char pathname[buf_len];
ssize_t l;
va_list ap;
va_start(ap, tpl);
vsnprintf(buf, buf_len, tpl, ap);
vsnprintf(pathname, buf_len, tpl, ap);
va_end(ap);
if ((l = readlink(buf, buf, buf_len)) < 0)
return -1;
buf[l] = '\0';
l = readlink(pathname, buf, buf_len);
if (l >= 0)
buf[l] = '\0';
return l;
}
@ -301,32 +301,43 @@ static void handle_fileat_access_before(const char * func, int dirfd, const char
}
}
static const char *make_file_absolute(char *absfile, const char *file)
static const char *make_file_absolute(char *buf, size_t buf_size, const char *file)
{
if (file[0] == '/')
if (file[0] == '/') {
return file;
else {
char cwd[PATH_MAX];
if (getcwd(cwd, PATH_MAX) == NULL)
return NULL;
snprintf(absfile, PATH_MAX, "%s/%s", cwd, file);
return absfile;
} else if (getcwd(buf, buf_size)) {
size_t l0 = strlen(buf);
size_t l1 = strlen(file);
if ((l0 + l1 + 1) < buf_size) {
buf[l0++] = '/';
memcpy(buf + l0, file, l1 + 1);
return buf;
}
}
return NULL;
}
static const char *make_fileat_absolute(char *absfile, int dirfd, const char *file)
static const char *make_fileat_absolute(char *buf, size_t buf_size,
int dirfd, const char *file)
{
if (file[0] == '/')
return file;
else if (dirfd == AT_FDCWD)
return make_file_absolute(absfile, file);
else {
char cwd[PATH_MAX];
if (readlink_from(cwd, PATH_MAX, "/proc/self/fd/%d", dirfd) < 0)
return NULL;
snprintf(absfile, PATH_MAX, "%s/%s", cwd, file);
return absfile;
return make_file_absolute(buf, buf_size, file);
else if (readlink_from(buf, buf_size, "/proc/self/fd/%d", dirfd) > 0) {
size_t l0 = strlen(buf);
size_t l1 = strlen(file);
if ((l0 + l1 + 1) < buf_size) {
buf[l0++] = '/';
memcpy(buf + l0, file, l1 + 1);
return buf;
}
}
return NULL;
}
/* sort of, private realpath, mostly not readlink() */
@ -435,7 +446,7 @@ static void handle_file_access_after(const char * func, const char * file,
else if (strcmp(file, rlog) == 0) return;
else if (lstat(file, &st) < 0) return;
file2 = make_file_absolute(absfile, file);
file2 = make_file_absolute(absfile, sizeof(absfile), file);
if (file2 == NULL) {
ERR("%s(\"%s\"): failed to make absolute!", func, file);
return;
@ -462,7 +473,7 @@ static void handle_fileat_access_after(const char * func, int dirfd, const char
else if (strcmp(file, rlog) == 0) return;
else if (fstatat(dirfd, file, &st, AT_SYMLINK_NOFOLLOW) < 0) return;
file2 = make_fileat_absolute(absfile, dirfd, file);
file2 = make_fileat_absolute(absfile, sizeof(absfile), dirfd, file);
if (file2 == NULL) {
ERR("%s(%d, \"%s\"): failed to make absolute!", func, dirfd, file);
return;

Loading…
Cancel
Save