]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
lsblk: escape unsafe chars in parsable output
authorKarel Zak <kzak@redhat.com>
Thu, 8 Mar 2012 15:04:45 +0000 (16:04 +0100)
committerKarel Zak <kzak@redhat.com>
Thu, 8 Mar 2012 15:04:45 +0000 (16:04 +0100)
Signed-off-by: Karel Zak <kzak@redhat.com>
misc-utils/lsblk.8
misc-utils/lsblk.c

index 028b19f10818408a2dcc72946ce9c727e6b8eea6..cd63948dbac01af196ea965cab73bf65bce4104f 100644 (file)
@@ -54,15 +54,20 @@ Specify which output columns to print.  Use
 .B "--help"
 to get a list of all supported columns.
 .IP "\fB\-P, \-\-pairs\fP"
-Use key="value" output format.
+Use key="value" output format. All potentially unsafe characters are hex-escaped (\\x<code>).
 .IP "\fB\-r, \-\-raw\fP"
-Use the raw output format.
+Use the raw output format. All potentially unsafe characters are hex-escaped (\\x<code>).
 .IP "\fB\-t, \-\-topology\fP"
 Output info about block device topology.
 This option is equivalent to "-o NAME,ALIGNMENT,MIN-IO,OPT-IO,PHY-SEC,LOG-SEC,ROTA,SCHED".
 .SH NOTES
 For the partitions are some information (e.g. queue attributes) inherited from
 parental device.
+
+The
+.B lsblk
+reads filesystem information (FSTYPE, LABEL and UUID) from udev database if
+not executed by root.
 .PP
 The
 .B lsblk
index 2eacc10d297c160aa12fbf6dd3ee364c18a9624d..2eb2b72370e37e12060be8123adb865d91f754ff 100644 (file)
@@ -339,7 +339,7 @@ static void probe_device(struct blkdev_cxt *cxt)
                return;
 
        /* try udev DB */
-       if (probe_device_by_udev(cxt) == 0)
+       if (getuid() != 0 && probe_device_by_udev(cxt) == 0)
                return;                         /* success */
 
        /* try libblkid */
@@ -468,6 +468,29 @@ static char *get_type(struct blkdev_cxt *cxt)
        return res;
 }
 
+#define is_parsable(_l)        (((_l)->tt->flags & TT_FL_RAW) || \
+                        ((_l)->tt->flags & TT_FL_EXPORT))
+
+static char *encode_str(const char *str)
+{
+       size_t sz;
+       char *enc = NULL;
+
+       if (!str)
+               goto err;
+
+       sz = strlen(str) * 4;
+       enc = xmalloc(sz);
+
+       if (blkid_encode_string(str, enc, sz) != 0)
+               goto err;
+
+       return enc;
+err:
+       free(enc);
+       return xstrdup("");
+}
+
 static void set_tt_data(struct blkdev_cxt *cxt, int col, int id, struct tt_line *ln)
 {
        char buf[1024];
@@ -481,9 +504,9 @@ static void set_tt_data(struct blkdev_cxt *cxt, int col, int id, struct tt_line
        switch(id) {
        case COL_NAME:
                if (cxt->dm_name) {
-                       if ((lsblk->tt->flags & TT_FL_RAW) ||
-                           (lsblk->tt->flags & TT_FL_EXPORT))
-                               tt_line_set_data(ln, col, xstrdup(cxt->dm_name));
+                       if (is_parsable(lsblk))
+                               tt_line_set_data(ln, col,
+                                       encode_str(cxt->dm_name));
                        else {
                                snprintf(buf, sizeof(buf), "%s (%s)",
                                        cxt->dm_name, cxt->name);
@@ -492,7 +515,10 @@ static void set_tt_data(struct blkdev_cxt *cxt, int col, int id, struct tt_line
                        break;
                }
        case COL_KNAME:
-               tt_line_set_data(ln, col, xstrdup(cxt->name));
+               if (is_parsable(lsblk))
+                       tt_line_set_data(ln, col, encode_str(cxt->name));
+               else
+                       tt_line_set_data(ln, col, xstrdup(cxt->name));
                break;
        case COL_OWNER:
        {
@@ -519,8 +545,7 @@ static void set_tt_data(struct blkdev_cxt *cxt, int col, int id, struct tt_line
                break;
        }
        case COL_MAJMIN:
-               if ((lsblk->tt->flags & TT_FL_RAW) ||
-                   (lsblk->tt->flags & TT_FL_EXPORT))
+               if (is_parsable(lsblk))
                        snprintf(buf, sizeof(buf), "%u:%u", cxt->maj, cxt->min);
                else
                        snprintf(buf, sizeof(buf), "%3u:%-3u", cxt->maj, cxt->min);
@@ -540,7 +565,12 @@ static void set_tt_data(struct blkdev_cxt *cxt, int col, int id, struct tt_line
                break;
        case COL_LABEL:
                probe_device(cxt);
-               if (cxt->label)
+               if (!cxt->label)
+                       break;
+
+               if (is_parsable(lsblk))
+                       tt_line_set_data(ln, col, encode_str(cxt->label));
+               else
                        tt_line_set_data(ln, col, xstrdup(cxt->label));
                break;
        case COL_UUID: