diff options
author | Karel Zak | 2014-11-27 13:39:35 +0100 |
---|---|---|
committer | Karel Zak | 2014-11-27 13:39:35 +0100 |
commit | 89e90ae7b2826110ea28c1c0eb8e7c56c3907bdc (patch) | |
tree | 1cd3e59664c5d32df7b51cff76b2bb7ce270334d /libblkid/src/save.c | |
parent | include/carefulputc: encode also ' and $ in fputs_quoted() output (diff) | |
download | kernel-qcow2-util-linux-89e90ae7b2826110ea28c1c0eb8e7c56c3907bdc.tar.gz kernel-qcow2-util-linux-89e90ae7b2826110ea28c1c0eb8e7c56c3907bdc.tar.xz kernel-qcow2-util-linux-89e90ae7b2826110ea28c1c0eb8e7c56c3907bdc.zip |
libblkid: care about unsafe chars in cache
The high-level libblkid API uses /run/blkid/blkid.tab cache to
store probing results. The cache format is
<device NAME="value" ...>devname</device>
and unfortunately the cache code does not escape quotation marks:
# mkfs.ext4 -L 'AAA"BBB'
# cat /run/blkid/blkid.tab
...
<device ... LABEL="AAA"BBB" ...>/dev/sdb1</device>
such string is later incorrectly parsed and blkid(8) returns
nonsenses. And for use-cases like
# eval $(blkid -o export /dev/sdb1)
it's also insecure.
Note that mount, udevd and blkid -p are based on low-level libblkid
API, it bypass the cache and directly read data from the devices.
The current udevd upstream does not depend on blkid(8) output at all,
it's directly linked with the library and all unsafe chars are encoded by
\x<hex> notation.
# mkfs.ext4 -L 'X"`/tmp/foo` "' /dev/sdb1
# udevadm info --export-db | grep LABEL
...
E: ID_FS_LABEL=X__/tmp/foo___
E: ID_FS_LABEL_ENC=X\x22\x60\x2ftmp\x2ffoo\x60\x20\x22
Signed-off-by: Karel Zak <kzak@redhat.com>
Diffstat (limited to 'libblkid/src/save.c')
-rw-r--r-- | libblkid/src/save.c | 22 |
1 files changed, 21 insertions, 1 deletions
diff --git a/libblkid/src/save.c b/libblkid/src/save.c index 8216f0917..5e8bbee8f 100644 --- a/libblkid/src/save.c +++ b/libblkid/src/save.c @@ -26,6 +26,21 @@ #include "blkidP.h" + +static void save_quoted(const char *data, FILE *file) +{ + const char *p; + + fputc('"', file); + for (p = data; p && *p; p++) { + if ((unsigned char) *p == 0x22 || /* " */ + (unsigned char) *p == 0x5c) /* \ */ + fputc('\\', file); + + fputc(*p, file); + } + fputc('"', file); +} static int save_dev(blkid_dev dev, FILE *file) { struct list_head *p; @@ -43,9 +58,14 @@ static int save_dev(blkid_dev dev, FILE *file) if (dev->bid_pri) fprintf(file, " PRI=\"%d\"", dev->bid_pri); + list_for_each(p, &dev->bid_tags) { blkid_tag tag = list_entry(p, struct blkid_struct_tag, bit_tags); - fprintf(file, " %s=\"%s\"", tag->bit_name,tag->bit_val); + + fputc(' ', file); /* space between tags */ + fputs(tag->bit_name, file); /* tag NAME */ + fputc('=', file); /* separator between NAME and VALUE */ + save_quoted(tag->bit_val, file); /* tag "VALUE" */ } fprintf(file, ">%s</device>\n", dev->bid_name); |