From e778642a9eb96975fcf3baa61dfa10add628af1a Mon Sep 17 00:00:00 2001 From: Karel Zak Date: Thu, 14 Apr 2016 14:26:54 +0200 Subject: [PATCH] libmount: don't support /etc/mtab by default The file mtab is evil and already unused by mainstream distributions. Now libmount is able to detect mtab->/proc/mounts and use /proc/self/mountinfo if necessary. This heuristic seems overkill in many cases. It's also dangerous on systems where mountinfo is strongly required (systemd based distros). This patch #ifdefs mtab code and forces libmount to always use /proc/self/mountinfo. The new configure option --enable-libmount-support-mtab is necessary to enable old behavior to support mtab. Signed-off-by: Karel Zak --- configure.ac | 10 +++++----- libmount/src/context.c | 13 +++++++++---- libmount/src/tab_parse.c | 18 +++++++++++------- libmount/src/tab_update.c | 2 ++ 4 files changed, 27 insertions(+), 16 deletions(-) diff --git a/configure.ac b/configure.ac index 48e994b10a..5a0040301d 100644 --- a/configure.ac +++ b/configure.ac @@ -962,13 +962,13 @@ AC_SUBST([LIBMOUNT_PATCH_VERSION], $PACKAGE_VERSION_RELEASE) AC_SUBST([LIBMOUNT_VERSION_INFO]) AC_DEFINE_UNQUOTED([LIBMOUNT_VERSION], ["$LIBMOUNT_VERSION"], [libmount version string]) -AC_ARG_ENABLE([libmount-force-mountinfo], - AS_HELP_STRING([--enable-libmount-force-mountinfo], [ignore /etc/mtab in all situations (recommended for systemd)]), - [], [enable_libmount_force_mountinfo=no] +AC_ARG_ENABLE([libmount-support-mtab], + AS_HELP_STRING([--enable-libmount-support-mtab], [support /etc/mtab (default is use /proc/self/mountinfo)]), + [], [enable_libmount_support_mtab=no] ) -AS_IF([test "x$enable_libmount_force_mountinfo" == xyes], [ - AC_DEFINE([USE_LIBMOUNT_FORCE_MOUNTINFO], [1], [Define to 1 if want to ignore mtab in all situations.]) +AS_IF([test "x$enable_libmount_support_mtab" == xyes], [ + AC_DEFINE([USE_LIBMOUNT_SUPPORT_MTAB], [1], [Define to 1 if want to support mtab.]) ]) diff --git a/libmount/src/context.c b/libmount/src/context.c index e26135fb0a..d9bda4375b 100644 --- a/libmount/src/context.c +++ b/libmount/src/context.c @@ -204,8 +204,10 @@ static int context_init_paths(struct libmnt_context *cxt, int writable) { assert(cxt); +#ifdef USE_LIBMOUNT_SUPPORT_MTAB if (!cxt->mtab_path) cxt->mtab_path = mnt_get_mtab_path(); +#endif if (!cxt->utab_path) cxt->utab_path = mnt_get_utab_path(); @@ -218,13 +220,12 @@ static int context_init_paths(struct libmnt_context *cxt, int writable) DBG(CXT, ul_debugobj(cxt, "checking for writable tab files")); -#ifdef USE_LIBMOUNT_FORCE_MOUNTINFO cxt->mtab_writable = 0; -#else - mnt_has_regular_mtab(&cxt->mtab_path, &cxt->mtab_writable); -#endif +#ifdef USE_LIBMOUNT_SUPPORT_MTAB + mnt_has_regular_mtab(&cxt->mtab_path, &cxt->mtab_writable); if (!cxt->mtab_writable) +#endif /* use /run/mount/utab if /etc/mtab is useless */ mnt_has_regular_utab(&cxt->utab_path, &cxt->utab_writable); @@ -1023,6 +1024,10 @@ int mnt_context_get_mtab(struct libmnt_context *cxt, struct libmnt_table **tb) cxt->table_fltrcb_data); mnt_table_set_cache(cxt->mtab, mnt_context_get_cache(cxt)); + + /* + * Note that mtab_path is NULL if mtab is useless or unsupported + */ if (cxt->utab) /* utab already parsed, don't parse it again */ rc = __mnt_table_parse_mtab(cxt->mtab, diff --git a/libmount/src/tab_parse.c b/libmount/src/tab_parse.c index 23076bfc0d..3253db7148 100644 --- a/libmount/src/tab_parse.c +++ b/libmount/src/tab_parse.c @@ -679,7 +679,7 @@ err: * @tb: tab pointer * @filename: file * - * Parses the whole table (e.g. /etc/mtab) and appends new records to the @tab. + * Parses the whole table (e.g. /etc/fstab) and appends new records to the @tab. * * The libmount parser ignores broken (syntax error) lines, these lines are * reported to the caller by the errcb() function (see mnt_table_set_parser_errcb()). @@ -1051,6 +1051,8 @@ static struct libmnt_fs *mnt_table_merge_user_fs(struct libmnt_table *tb, struct return fs; } +/* default filename is /proc/self/mountinfo + */ int __mnt_table_parse_mtab(struct libmnt_table *tb, const char *filename, struct libmnt_table *u_tb) { @@ -1058,9 +1060,7 @@ int __mnt_table_parse_mtab(struct libmnt_table *tb, const char *filename, assert(tb); -#ifdef USE_LIBMOUNT_FORCE_MOUNTINFO - DBG(TAB, ul_debugobj(tb, "mtab parse: ignore %s", filename ? filename : "mtab")); -#else +#ifdef USE_LIBMOUNT_SUPPORT_MTAB if (mnt_has_regular_mtab(&filename, NULL)) { DBG(TAB, ul_debugobj(tb, "force mtab usage [filename=%s]", filename)); @@ -1085,15 +1085,19 @@ int __mnt_table_parse_mtab(struct libmnt_table *tb, const char *filename, * useless /etc/mtab * -- read kernel information from /proc/self/mountinfo */ - tb->fmt = MNT_FMT_MOUNTINFO; - rc = mnt_table_parse_file(tb, _PATH_PROC_MOUNTINFO); + tb->fmt = filename ? MNT_FMT_GUESS : MNT_FMT_MOUNTINFO; + rc = mnt_table_parse_file(tb, filename ? filename : _PATH_PROC_MOUNTINFO); if (rc) { /* hmm, old kernel? ...try /proc/mounts */ tb->fmt = MNT_FMT_MTAB; return mnt_table_parse_file(tb, _PATH_PROC_MOUNTS); } + if (!is_mountinfo(tb)) + return 0; +#ifdef USE_LIBMOUNT_SUPPORT_MTAB read_utab: +#endif DBG(TAB, ul_debugobj(tb, "mtab parse: #2 read utab")); if (mnt_table_get_nents(tb) == 0) @@ -1139,7 +1143,7 @@ read_utab: /** * mnt_table_parse_mtab: * @tb: table - * @filename: overwrites default (/etc/mtab or $LIBMOUNT_MTAB) or NULL + * @filename: overwrites default or NULL * * This function parses /etc/mtab or /proc/self/mountinfo + * /run/mount/utabs or /proc/mounts. diff --git a/libmount/src/tab_update.c b/libmount/src/tab_update.c index 631e1cd9fd..d85cb89da5 100644 --- a/libmount/src/tab_update.c +++ b/libmount/src/tab_update.c @@ -105,7 +105,9 @@ int mnt_update_set_filename(struct libmnt_update *upd, const char *filename, /* detect tab filename -- /etc/mtab or /run/mount/utab */ +#ifdef USE_LIBMOUNT_SUPPORT_MTAB mnt_has_regular_mtab(&path, &rw); +#endif if (!rw) { path = NULL; mnt_has_regular_utab(&path, &rw); -- 2.39.5