The buffer used for parsing the mount list is not large enough.
In container environments using overlay filesystems, the use of a
large number of layers can easily result in valid entries which
exceed the buffer size resulting in 'Line too long when parsing mounts'
errors and the mount list not being processed completely.
Signed-off-by: Thomas Abraham <
tabr...@suse.com>
---
hugeutils.c | 59 +++++++++++++----------------------------------------
1 file changed, 14 insertions(+), 45 deletions(-)
diff --git a/hugeutils.c b/hugeutils.c
index 0230976..c8ba455 100644
--- a/hugeutils.c
+++ b/hugeutils.c
@@ -44,6 +44,7 @@
#include <linux/types.h>
#include <linux/unistd.h>
#include <dirent.h>
+#include <mntent.h>
#include "libhugetlbfs_internal.h"
#include "hugetlbfs.h"
@@ -617,62 +618,30 @@ void debug_show_page_sizes(void)
hpage_sizes[i].mount);
}
-#define LINE_MAXLEN 2048
static void find_mounts(void)
{
- int fd;
- char path[PATH_MAX+1];
- char line[LINE_MAXLEN + 1];
- char *eol;
- char *match;
- char *end;
- int bytes;
- off_t offset;
-
- fd = open("/proc/mounts", O_RDONLY);
- if (fd < 0) {
- fd = open("/etc/mtab", O_RDONLY);
- if (fd < 0) {
+ FILE *f;
+ struct mntent *mnt;
+ char *fstype = "hugetlbfs";
+
+ f = fopen("/proc/mounts", "r");
+ if (!f) {
+ f = fopen("/etc/mtab", "r");
+ if (!f) {
ERROR("Couldn't open /proc/mounts or /etc/mtab (%s)\n",
strerror(errno));
return;
}
}
- while ((bytes = read(fd, line, LINE_MAXLEN)) > 0) {
- line[LINE_MAXLEN] = '\0';
- eol = strchr(line, '\n');
- if (!eol) {
- ERROR("Line too long when parsing mounts\n");
- break;
- }
-
- /*
- * Truncate the string to just one line and reset the file
- * to begin reading at the start of the next line.
- */
- *eol = '\0';
- offset = bytes - (eol + 1 - line);
- lseek(fd, -offset, SEEK_CUR);
-
- /* Match only hugetlbfs filesystems. */
- match = strstr(line, " hugetlbfs ");
- if (match) {
- match = strchr(line, '/');
- if (!match)
- continue;
- end = strchr(match, ' ');
- if (!end)
- continue;
-
- strncpy(path, match, end - match);
- path[end - match] = '\0';
- if ((hugetlbfs_test_path(path) == 1) &&
+ while ((mnt = getmntent(f))) {
+ if (!strncmp(mnt->mnt_type, fstype, sizeof(fstype))) {
+ if ((hugetlbfs_test_path(mnt->mnt_dir) == 1) &&
!(access(path, R_OK | W_OK | X_OK)))
- add_hugetlbfs_mount(path, 0);
+ add_hugetlbfs_mount(mnt->mnt_dir, 0);
}
}
- close(fd);
+ fclose(f);
}
void setup_mounts(void)
--
2.35.3