]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
libmount: be robust for empty target/source strings
authorKarel Zak <kzak@redhat.com>
Mon, 17 Jun 2013 11:02:15 +0000 (13:02 +0200)
committerKarel Zak <kzak@redhat.com>
Mon, 17 Jun 2013 11:02:15 +0000 (13:02 +0200)
 * lib/canonicalize.c: don't interpret empty strings as relative paths
 * libmount: more robust libmnt_table find function and debug messages

References: https://bugzilla.novell.com/show_bug.cgi?id=825150
Signed-off-by: Karel Zak <kzak@redhat.com>
Signed-off-by: Karel Zak <kzak@redhat.com>
lib/canonicalize.c
libmount/src/context_umount.c
libmount/src/tab.c
sys-utils/umount.c

index 727806e1ece645f404bcfd3f156d7534491c67a0..548e29b75e30f10f76196d8f0edc1e7f7bea3ffc 100644 (file)
@@ -151,6 +151,9 @@ canonicalize_dm_name(const char *ptname)
        size_t  sz;
        char    path[256], name[256], *res = NULL;
 
+       if (!ptname || !*ptname)
+               return NULL;
+
        snprintf(path, sizeof(path), "/sys/block/%s/dm/name", ptname);
        if (!(f = fopen(path, "r" UL_CLOEXECSTR)))
                return NULL;
@@ -173,7 +176,7 @@ canonicalize_path(const char *path)
        char canonical[PATH_MAX+2];
        char *p;
 
-       if (path == NULL)
+       if (!path || !*path)
                return NULL;
 
        if (!myrealpath(path, canonical, PATH_MAX+1))
@@ -199,7 +202,7 @@ canonicalize_path_restricted(const char *path)
        uid_t euid;
        gid_t egid;
 
-       if (path == NULL)
+       if (!path || !*path)
                return NULL;
 
        euid = geteuid();
index 4a8659c9c06c7bb28b7d4bc2c21bd9f768d00501..7707991eb5949e9f6f7b09dca40c54ff0281e396 100644 (file)
@@ -71,11 +71,17 @@ int mnt_context_find_umount_fs(struct libmnt_context *cxt,
        struct libmnt_cache *cache = NULL;
        char *cn_tgt = NULL, *loopdev = NULL;
 
+       if (pfs)
+               *pfs = NULL;
+
        if (!cxt || !tgt || !pfs)
                return -EINVAL;
 
        DBG(CXT, mnt_debug_h(cxt, "umount: lookup FS for '%s'", tgt));
 
+       if (!*tgt)
+               return 1; /* empty string is not error */
+
        /*
         * The mtab file maybe huge and on systems with utab we have to merge
         * userspace mount options into /proc/self/mountinfo. This all is
@@ -169,9 +175,12 @@ try_loopdev:
                }
        }
 
-       *pfs = fs;
+       if (pfs)
+               *pfs = fs;
        free(loopdev);
 
+       DBG(CXT, mnt_debug_h(cxt, "umount fs: %s", fs ? mnt_fs_get_target(fs) :
+                                                       "<not found>"));
        return fs ? 0 : 1;
 err:
        free(loopdev);
@@ -197,7 +206,7 @@ static int lookup_umount_fs(struct libmnt_context *cxt)
        if (rc < 0)
                return rc;
        if (rc == 1 || !fs) {
-               DBG(CXT, mnt_debug_h(cxt, "umount: cannot find %s in mtab", tgt));
+               DBG(CXT, mnt_debug_h(cxt, "umount: cannot find '%s' in mtab", tgt));
                return 0;
        }
 
index 11a297814fd4c4b9fe0075269da1545a7833cbad..ef86627f8a11172fcea84ac77641c0b366de62b5 100644 (file)
@@ -274,7 +274,7 @@ int mnt_table_next_child_fs(struct libmnt_table *tb, struct libmnt_iter *itr,
        if (!tb || !itr || !parent)
                return -EINVAL;
 
-       DBG(TAB, mnt_debug_h(tb, "lookup next child of %s",
+       DBG(TAB, mnt_debug_h(tb, "lookup next child of '%s'",
                                mnt_fs_get_target(parent)));
 
        parent_id = mnt_fs_get_id(parent);
@@ -442,12 +442,12 @@ struct libmnt_fs *mnt_table_find_mountpoint(struct libmnt_table *tb,
 {
        char *mnt;
 
-       if (!tb || !path)
+       if (!tb || !path || !*path)
                return NULL;
        if (direction != MNT_ITER_FORWARD && direction != MNT_ITER_BACKWARD)
                return NULL;
 
-       DBG(TAB, mnt_debug_h(tb, "lookup MOUNTPOINT: %s", path));
+       DBG(TAB, mnt_debug_h(tb, "lookup MOUNTPOINT: '%s'", path));
 
        mnt = strdup(path);
        if (!mnt)
@@ -494,12 +494,12 @@ struct libmnt_fs *mnt_table_find_target(struct libmnt_table *tb, const char *pat
        assert(tb);
        assert(path);
 
-       if (!tb || !path)
+       if (!tb || !path || !*path)
                return NULL;
        if (direction != MNT_ITER_FORWARD && direction != MNT_ITER_BACKWARD)
                return NULL;
 
-       DBG(TAB, mnt_debug_h(tb, "lookup TARGET: %s", path));
+       DBG(TAB, mnt_debug_h(tb, "lookup TARGET: '%s'", path));
 
        /* native @target */
        mnt_reset_iter(&itr, direction);
@@ -510,6 +510,8 @@ struct libmnt_fs *mnt_table_find_target(struct libmnt_table *tb, const char *pat
        if (!tb->cache || !(cn = mnt_resolve_path(path, tb->cache)))
                return NULL;
 
+       DBG(TAB, mnt_debug_h(tb, "lookup canonical TARGET: '%s'", cn));
+
        /* canonicalized paths in struct libmnt_table */
        mnt_reset_iter(&itr, direction);
        while(mnt_table_next_fs(tb, &itr, &fs) == 0) {
@@ -566,12 +568,12 @@ struct libmnt_fs *mnt_table_find_srcpath(struct libmnt_table *tb, const char *pa
        const char *p;
 
        assert(tb);
-       if (!tb || !path)
+       if (!tb || !path || !*path)
                return NULL;
        if (direction != MNT_ITER_FORWARD && direction != MNT_ITER_BACKWARD)
                return NULL;
 
-       DBG(TAB, mnt_debug_h(tb, "lookup srcpath: %s", path));
+       DBG(TAB, mnt_debug_h(tb, "lookup SRCPATH: '%s'", path));
 
        /* native paths */
        mnt_reset_iter(&itr, direction);
@@ -585,6 +587,8 @@ struct libmnt_fs *mnt_table_find_srcpath(struct libmnt_table *tb, const char *pa
        if (!path || !tb->cache || !(cn = mnt_resolve_path(path, tb->cache)))
                return NULL;
 
+       DBG(TAB, mnt_debug_h(tb, "lookup canonical SRCPATH: '%s'", cn));
+
        /* canonicalized paths in struct libmnt_table */
        if (ntags < mnt_table_get_nents(tb)) {
                mnt_reset_iter(&itr, direction);
@@ -672,7 +676,7 @@ struct libmnt_fs *mnt_table_find_tag(struct libmnt_table *tb, const char *tag,
        assert(tag);
        assert(val);
 
-       if (!tb || !tag || !val)
+       if (!tb || !tag || !*tag || !val)
                return NULL;
        if (direction != MNT_ITER_FORWARD && direction != MNT_ITER_BACKWARD)
                return NULL;
@@ -721,9 +725,9 @@ struct libmnt_fs *mnt_table_find_source(struct libmnt_table *tb,
        if (direction != MNT_ITER_FORWARD && direction != MNT_ITER_BACKWARD)
                return NULL;
 
-       DBG(TAB, mnt_debug_h(tb, "lookup SOURCE: %s", source));
+       DBG(TAB, mnt_debug_h(tb, "lookup SOURCE: '%s'", source));
 
-       if (source && strchr(source, '=')) {
+       if (source && *source && strchr(source, '=')) {
                char *tag, *val;
 
                if (blkid_parse_tag_string(source, &tag, &val) == 0) {
@@ -761,7 +765,7 @@ struct libmnt_fs *mnt_table_find_pair(struct libmnt_table *tb, const char *sourc
        assert(tb);
        assert(target);
 
-       if (!tb || !target)
+       if (!tb || !target || !*target || !source || !*source)
                return NULL;
        if (direction != MNT_ITER_FORWARD && direction != MNT_ITER_BACKWARD)
                return NULL;
@@ -841,7 +845,7 @@ struct libmnt_fs *mnt_table_get_fs_root(struct libmnt_table *tb,
        assert(fs);
        assert(fsroot);
 
-       DBG(TAB, mnt_debug("lookup fs-root for %s", mnt_fs_get_source(fs)));
+       DBG(TAB, mnt_debug("lookup fs-root for '%s'", mnt_fs_get_source(fs)));
 
        fstype = mnt_fs_get_fstype(fs);
 
index e3f0e2dd0a17401b7e11903cede3495ba10c5be1..c3b8187cef5c36d33d0451b11ad7cefac809d24c 100644 (file)
@@ -236,7 +236,10 @@ static int mk_exit_code(struct libmnt_context *cxt, int rc)
                        tgt);
                break;
        case ENOENT:
-               warnx(_("%s: not found"), tgt);
+               if (tgt && *tgt)
+                       warnx(_("%s: mountpoint not found"), tgt);
+               else
+                       warnx(_("undefined mountpoint"));
                break;
        case EPERM:
                warnx(_("%s: must be superuser to unmount"), tgt);