libmount: handle arbitrary line length for mounts
PATCH 2/2 libmount: handle arbitrary line length for mounts
The kernel's maximum path length is PATH_MAX (4096). The use of BUFSIZ (8192) would seem sufficient for reading mountinfo files, but it's not. Paths may contain escaped characters (requiring 4x as many bytes to read), and filesystem options are of unknown length. To avoid mounts being either intentionally or unintentionally hidden from libmount and its users, we must accept arbitrary length lines when parsing.
Long valid entries are currently ignored, with warnings like this:
mount: /proc/self/mountinfo: parse error: ignore entry at line 11.
mount: /proc/self/mountinfo: parse error: ignore entry at line 12.
Instead of using a malloc on every line parsed from mount files, do it once per mount file context, growing it as needed. The general case will never grow it.
--- libmount/src/mountP.h | 2 ++ libmount/src/tab.c | 8 ++++++ libmount/src/tab_parse.c | 66 ++++++++++++++++++++++++++++++++++++++++-------- 3 files changed, 65 insertions(+), 11 deletions(-)
libmount/src/mountP.h +2 -0
@@ -212,6 +212,8 @@ struct libmnt_table {int comms;/* enable/disable comment parsing */ char *comm_intro;/* First comment in file */ char *comm_tail;/* Last comment in file */ +char *buf;/* Memory for reading lines */ +size_t buf_size;/* Size of line-reading buffer */ struct libmnt_cache *cache;/* canonicalized paths/tags cache */
libmount/src/tab.c +8 -0
@@ -84,6 +84,13 @@ struct libmnt_table *mnt_new_table(void)DBG (TAB ,ul_debugobj (tb,"alloc" )); tb->refcount =1 ;INIT_LIST_HEAD (&tb->ents); + tb->buf_size =BUFSIZ ; + tb->buf =malloc (tb->buf_size); +if (!tb->buf) { +free (tb); +return NULL ; + } +return tb; }
@@ -166,6 +173,7 @@ void mnt_free_table(struct libmnt_table *tb)mnt_unref_cache (tb->cache);free (tb->comm_intro);free (tb->comm_tail); +free (tb->buf);free (tb); }
libmount/src/tab_parse.c +52 -10
103 lines not on this page · raw message
Signature · 6 lines
1.9.1 -- Kees Cook Chrome OS Security
Quoted 2 lines · Kees Cook
+ * Read a line with fgets. If it doesn't fit in the existing buffer,
+ * allocate more heap memory for storage until it does fit.
why not just use getline then ?
libmount/src/tab_parse.c static int append_comment(struct libmnt_table *tb, · line 433
Quoted 1 line
libmount/src/tab_parse.c +1
-0+if (fgets (current, remaining, f) ==NULL ) {
style omits space before the (. comes up a few times in this patch. -mike
Quoted 5 lines · Mike Frysinger
Quoted 2 lines · Kees Cook
+ * Read a line with fgets. If it doesn't fit in the existing buffer,
+ * allocate more heap memory for storage until it does fit.why not just use getline then ?
It's not often I get to discover new functions in libc. How did I ever miss this? If it behaves the same as fgets on EOF, it'll be perfect.
Quoted 3 lines
Quoted 1 line
+ if (fgets (current, remaining, f) == NULL) {
style omits space before the (. comes up a few times in this patch.
Noted. I saw the use of strchr and didn't look much more. :)
-Kees
Signature · 2 lines
Kees Cook Chrome OS Security