qemu-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Qemu-devel] Image probing: how it can be insecure, and what we coul


From: Jeff Cody
Subject: Re: [Qemu-devel] Image probing: how it can be insecure, and what we could do about it
Date: Tue, 4 Nov 2014 15:33:23 -0500
User-agent: Mutt/1.5.21 (2010-09-15)

On Tue, Nov 04, 2014 at 07:45:38PM +0100, Markus Armbruster wrote:
> I'll try to explain all solutions fairly.  Isn't easy when you're as
> biased towards one of them as I am.  Please bear with me.
> 
> 
> = The trust boundary between image contents and meta-data =
> 
> A disk image consists of image contents and meta-data.
> 
> Example: all of a raw image's contents is image contents.  Leaves just
> file name and attributes for meta-data.
> 
> Example: QCOW2 meta-data includes header, header extensions, L1 table,
> L2 tables, ...  The meta-data defines where in the image the actual
> contents is stored.
> 
> A guest can access the image contents, not the meta-data.
> 
> Image contents you've let an untrusted guest write is untrusted.
> 
> Therefore, there's a trust boundary between image contents and
> meta-data.  QEMU has to trust image meta-data, but shouldn't trust image
> contents.  The exact location of the trust boundary depends on the image
> format.
> 
> 
> = How we instruct QEMU what to trust =
> 
> By configuring QEMU to use an image, the user instructs QEMU to trust
> the image's meta-data.
> 
> When the user's configuration specifies the image format explicitly, the
> trust boundary is clear.
> 
> Else, the trust boundary is ambigous when more than one format is
> possible.
> 
> QEMU resolves this ambiguity by picking the first format with the
> highest "score".  Raw format is always possible, and always has the
> lowest score.
> 
> 
> = How this lets the guest escape isolation =
> 
> Unfortunately, this lets the guest shift the trust boundary and escape
> isolation, as follows:
> 
> * Expose a raw image to the guest (whether you specify the format=raw or
>   let QEMU guess it doesn't matter).  The complete contents becomes
>   untrusted.
> 
> * Reuse the image *without* specifying the raw format.  QEMU guesses the
>   format based on untrusted image contents.  Now QEMU guesses a format
>   chosen by the guest, with meta-data chosen by the guest.  By
>   controlling image meta-data, the malicious guest can access arbitrary
>   files as QEMU, enlarge its storage, and more.  A non-malicious guest
>   can accidentally DoS itself, by writing a pattern probing recognizes.
> 
> This is CVE-2008-2004.
> 
> 
> = Aside: other trust boundaries =
> 
> Of course, this is not the only trust boundary that matters.  For
> instance, there's normally one between your host and somebody else's
> computers.  Telling QEMU to trust meta-data of some image you got "from
> the internet" violates it.  There's nothing QEMU can do about that.
> 
> 
> = Insecure usage is easy, secure usage is hard =
> 
> The oldest stratum of user interfaces doesn't let you specify the image
> format.  Use of raw images with these is insecure by design.  These
> interfaces are still recommended for human users.
> 
> Example of insecure usage: -hda foo.img, where foo.img is raw.
> 
> With the next generation of interfaces, specifying the image format is
> optional.  Use of raw images with these is insecure by default.
> 
> Example of insecure usage: -drive file=foo.img,index=0,media=cdrom,
> where foo.img is raw.  The -hda above is actually sugar for this.
> 
> Equivalent secure usage: add format=raw.
> 
> Note that specifying just the top image's format is not enough, you also
> have to specify any backing images' formats.  QCOW2 can optionally store
> the backing image format in the image.  The other COW formats can't.
> 
> Example of insecure usage: -hda bar.vmdk, where bar.vmdk is a VMDK image
> with a raw backing file.
> 
> Equivalent secure usage: Beats me.  Maybe there's a funky -drive
> backing.whatever to specify the backing image's format.
> 
> With the latest interface blockdev-add, specifying the format is
> mandatory.  Secure, but not really suitable for humans.
> 
> Example of secure usage:
> 
>     { "execute": "blockdev-add",
>       "arguments": {'options': {
>           'driver': 'raw', 'id':'foo',
>           'file': { 'driver': 'file', 'filename': 'foo.img' } } } }
> 
> Insecure usage is easy, secure usage is *hard*.  Even for sophisticated
> users like libvirt developers.  Evidence: libvirt CVE-2010-2237,
> CVE-2010-2238, CVE-2010-2239, and more that didn't get a CVE, like the
> recent accidental probing in drive-mirror.
> 
> 
> = How can we better guard the trust boundary in QEMU? =
> 
> The guest can violate the trust boundary only because
> 
> (a) QEMU supports both raw images and image formats, and
> 
> (b) QEMU guesses image format from raw image contents, and
> 
> (c) given a raw image, guests can change its contents and control a
> future QEMU's format guess.
> 
> We can attack one ore more of these three conditions:
> 
> (a) Outlaw raw images
> 
> (b) Don't guess format from untrusted image contents
> 
> (c) Prevent "bad" guest writes
> 
> Nobody is seriously suggesting we do (a).  It's clearly too late for
> that.  Let's explore the other two in more detail.
> 
> == Don't guess format from untrusted image contents ==
> 
> Several variations of the theme.
> 
> Guessing only happens when the user doesn't specify a format, so the
> simplest way to avoid it would be requiring users to always specify the
> format.
> 
> PRO: Simple, plugs the hole.
> 
> CON: Breaks a lot of existing usage.  Bye -hda, hello extra typing.
> 
> Variation: command line option to opt out of probing completely.
> 
> PRO: Simple, plugs the hole.
> 
> CON: Insecure by default.
> 
> CON: In addition to opting out, you have to update your usage
> accordingly.  I guess libvirt would do it anyway, to guard against
> accidental probing once and for all.
> 
> Variation: Stefan proposed to make format= mandatory just for -drive.  I
> guess he rather meant mandatory for anything but -hda and other selected
> convenience interfaces.
> 
> PRO: No extra typing in the cases where it makes the most difference.
> 
> CON: Breaks existing usage in the other cases, extra typing.
> 
> CON: Doesn't fully plug the hole.  Users of convenience interfaces may
>     remain insecure out of ignorance.  We could add a warning to guide
>     them to more secure usage, but then that warning would annoy users
>     who don't care for security (sometimes with reason), and we can't
>     have that.  So we silently leave the users who would care if they
>     knew insecure.
> 
> I proposed something less radical, namely to keep guessing the image
> format, but base the guess on trusted meta-data only: file name and
> attributes.  Block and character special files are raw.  For other
> files, find the file name extension, and look up the format claiming it.
> 
> PRO: Plugs the hole.
> 
> CON: Breaks existing usage when the new guess differs from the old
>     guess.  Common usage should be fine:
> 
>     * -hda test.qcow2
> 
>       Fine as long as test.qcow2 is really QCOW2 (as it should!), and
>       either specifies a backing format (as it arguably should), or the
>       backing file name is sane.
> 
>     * -hda disk.img
> 
>       Fine as long as disk.img is really a disk image (as it should).
> 
>     * -hda /dev/mapper/vg0-virtdisk
> 
>       Fine as long as the logical volume is raw.
> 
>     Less common usage can break:
> 
>     * -hda nbd://localhost
> 
>       Socket provides no clue, so no guess.
> 
>     Weird usage can conceivably break hard:
> 
>     * -hdd disk.img
> 
>       Breaks hard when disk.img is actually QCOW2, the guest boots
>       anyway from another drive, then proceeds to overwrite this one.
> 
> Mitigation: lengthy transition period where we warn "this usage is
> insecure, and we'll eventually break it; here's a hint on secure usage".
> 
> CON: We delay plugging the hole one more time.  But at least we no
> longer expose our users to it silently.
> 
> Jeff pointed out that we want to retain probing in things like qemu-img
> info.
> 
> Richard asked for a way for users to ask for insecure probing, say
> format=insecure-probe.  I have no problem with giving users rope when
> they ask for it.
> 
> Variation: if file name and attributes are unavailable or provide no
> clue, guess raw.  Same PRO and CON as above, only it avoids breaking a
> few more cases.  For instance, "-hda nbd://localhost" keeps working as
> long as the server serves a raw image.  Smells a bit like too much magic
> to me.
>

How about another variation: probe as we do currently, but a
result of 'raw' is a hard failure ("Unable to determine image format,
it may be 'raw'.  You must specify the image format").

This makes sense, because of a probe result of raw currently really
means "I don't recognize this image", so maybe we should just treat it
as such.

Imagine two different QEMU binaries, with different image formats
compiled in.  Currently, by detecting "raw" we could cause data
corruption in a guest, if one QEMU version doesn't recognize 'foobar'
image format.

PRO: 
* Simple, and non-raw format probes will continue to behave the same

* We stop "guessing" raw

CON: 
* User could specify format of "raw", have a guest write an image
header to sector 0, and then in subsequent usage go back to probing

* Will break some scripts




> My proposal replaces probing wholesale.  I like that because it results
> in simple, predictable guessing.  Here's a hybrid approach: first guess
> raw vs. non-raw based on trusted meta-data only, and if non-raw, probe.
> 
> Nothing the guest writes can affect the raw vs. non-raw decision.  Once
> an image is raw, only the user can make it non-raw, by changing its name
> or attributes.
> 
> Two variations: 1. guess raw without a clue, and 2. guess non-raw then.
> 
> Again, same PRO and CON as above, only it doesn't break when users give
> their non-raw images weird names.
> 
> == Prevent "bad" guest writes ==
> 
> Again, several variations, but this time, only the last one is serious,
> the others are just for illustration.
> 
> Fail guest writes to those parts of the image that probing may examine
> Can fail only writes to the first few sectors (at worst) of raw images.
> 
> PRO: Plugs the hole.
> 
> CON: The virtual hardware is defective.  Breaks common guest software
> that writes to the first few sectors, such as boot loaders and
> partitioning tools.  Breaks guest software using the whole device, which
> isn't common, but certainly not unheard of.
> 
> Variation: fail only writes of patterns that actually can make probing
> guess something other than raw.
> 
> PRO: Still plugs the hole.
> 
> CON: Except when you upgrade to a version that recognizes more patterns.
> 
> CON: The virtual hardware is still defective, but the defects are
> minimized.  We can hope that partition tables, boot sectors and such
> won't match the patterns, so common guest software hopefully works.
> Guest software using the whole device still breaks, only now it breaks
> later rather than sooner.
> 
> Variation: fail writes only on *probed* raw images.
> 
> CON: Doesn't fully plug the hole: mixing probed usage (user doesn't
> specify format) with non-probed usage (user specifies format) remains
> insecure.  The guest's write succeeds in non-probed usage, and the guest
> escapes isolation in the next probed usage.
> 
> CON: The virtual hardware is still defective, but it now comes with a
> "defective on/off" switch, factory default "defective on".  We could add
> a warning to guide users to switch defective off but then that warning
> would annoy people who don't care to switch it off (sometimes with
> reason), and we can't have that.  So we leave users who would care if
> they knew in the dark.
> 
> The two variations can be combined.  This is Kevin's proposal.
> 
> CON: Doesn't fully plug the hole: union of both variations' flaws.
> 
> CON: The virtual hardware is still defective: interesection of both
> variations' defects.
> 
> 
> = Conclusion =
> 
> This is *my* conclusion.  Yours may be different, and that's okay.  It's
> business, not personal ;)
> 
> Secure usage should not be hard.
> 
> If we permit insecure usage for convenience or compatibility, we should
> warn the user, unless he clearly asked for it.  Even if that warning
> annoys Kevin and Max.  If you want to suppress it with configure
> --reckless or something, no objections.
> 
> Same for defective virtual hardware.
> 
> Kevin's proposal to prevent "bad" guest writes has relatively small
> compatibility issues.  It improves things from "insecure by default" to
> "slightly defective by default" as long as you use raw images either
> always probed or always non-probed.  It doesn't help at all when you
> alternate probed and non-probed usage.  Improvement of sorts, but it
> still fails my "secure usage should not be hard" requirement, and that
> requirement is a hard one for me.
> 
> My proposal to ditch image contents probing entirely has more serious
> compatibility issues.  In particular, we'd have to forgo sugared
> convenience syntax for a number of less common things.  It definitely
> needs a grace period where all usage we're going to break warns.  On the
> up side, it will actually be secure by default when it's done.
> 
> If this is not acceptable, my second choice is actually the command line
> option to opt out of probing completely.  This doesn't address "insecure
> by default" (sadly), but it does at least satisfy my "secure usage
> should not be hard" requirement.
> 
> If we should adopt Kevin's proposal against my objections, then I very
> badly want the opt out option on top of it, opting out of both probing
> and "bad" write prevention.
> 
> Thanks for hearing me out.



reply via email to

[Prev in Thread] Current Thread [Next in Thread]