All of lore.kernel.org
 help / color / mirror / Atom feed
From: Ian Jackson <ian.jackson@eu.citrix.com>
To: Andrew Cooper <andrew.cooper3@citrix.com>
Cc: Stefano Stabellini <sstabellini@kernel.org>,
	WeiLiu <wei.liu2@citrix.com>,
	George Dunlap <George.Dunlap@eu.citrix.com>,
	Tim Deegan <tim@xen.org>, Xen-devel <xen-devel@lists.xen.org>,
	Jan Beulich <JBeulich@suse.com>
Subject: Re: [PATCH] libelf: Fix div0 issues in elf_{shdr, phdr}_count()
Date: Thu, 8 Dec 2016 15:47:40 +0000	[thread overview]
Message-ID: <22601.32924.967352.367426@mariner.uk.xensource.com> (raw)
In-Reply-To: <1ca334cd-cda0-e870-b213-72a82447fe37@citrix.com>

Andrew Cooper writes ("Re: [PATCH] libelf: Fix div0 issues in elf_{shdr,phdr}_count()"):
> On 08/12/16 15:17, Jan Beulich wrote:
> > Oh, I see - elf_phdr_count() itself relies on the check that's
> > about to be done. But I think the correct thing then would be to
> > not use elf_phdr_count() here, but read the raw field instead.
> > You patch basically adds a weaker check there which is then
> > immediately being followed by the stronger check above.

There must be no "reading of raw fields".  You may use
elf_access_unsigned if you really want to.

> > And looking at it from that angle it's then questionable why
> > elf_{p,s}hdr_count() do any checking at all - this should all be
> > done once in elf_init(). IOW I did the adjustment in the wrong
> > way, and I really should have made elf_shdr_count() match
> > elf_phdr_count() (moving the checks into elf_init()).

The point of this checking is not to avoid overflow, but just to
ensure that the loops which rely on elf_*_count do not iterate "far
too much".

> > And looking at elf_init() again I also realize that I blindly
> > copied the range checks, calculation of which can overflow.

These possibly-faulty range checks are harmless because they all
operate on unsigned values.

Frankly I'm not sure what the point of a01b6d46 was ?

> > I think it would be better to do those checks such that
> > overflow results in an error return.

The design principle behind my fixes to XSA-55 is to write the bulk of
libelf in a subset of C which is always safe.

This means:

 * Always using unsigned integers (so no signed integer overflow).

 * Doing all pointer dereferences into the ELF block using an
   access function which does a bounds check.  (And this also
   means representing all pointers as unsigned offsets, so that we
   avoid calculating basilisk pointer values.)

 * Explicitly limiting the iteration count of every loop where
   necessayr (to avoid DOS attacks from bad images).

 * Not using unsafe operations like division by input-controlled
   values.

Sticking to these rules is a lot easier than writing explicit checks.
This is because these explicit checks are very easy to omit, or get
wrong.  The pointers are all encapsulated in a special type which
prevents uncontrolled dereference.

Admittedly the rule about iteration count is not so easy to remember,
and I had failed to anticipate that someone would introduce division.
But both of those kind of bugs are denial of service, rather than
privilege escalation.

Having stared at the commit message of a01b6d46 and at the
pre-a01b6d46 code, I still don't understand what motivated the
changes.

Ian.

_______________________________________________
Xen-devel mailing list
Xen-devel@lists.xen.org
https://lists.xen.org/xen-devel

  reply	other threads:[~2016-12-08 15:47 UTC|newest]

Thread overview: 50+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-12-08 14:18 [PATCH] libelf: Fix div0 issues in elf_{shdr, phdr}_count() Andrew Cooper
2016-12-08 14:41 ` Jan Beulich
2016-12-08 14:46   ` Andrew Cooper
2016-12-08 15:17     ` Jan Beulich
2016-12-08 15:23       ` Andrew Cooper
2016-12-08 15:47         ` Ian Jackson [this message]
2016-12-08 16:09           ` Jan Beulich
2016-12-08 17:28           ` Ian Jackson
2016-12-09  8:38             ` Jan Beulich
2016-12-09 11:54               ` Ian Jackson
2016-12-09 13:03                 ` Jan Beulich
2016-12-09 15:44                 ` [PATCH 0/8] libelf: safety enhancements Ian Jackson
2016-12-09 15:44                   ` [PATCH 1/8] libelf: loop safety: Introduce elf_iter_ok and elf_strcmp_safe Ian Jackson
2016-12-12 15:02                     ` Jan Beulich
2016-12-12 15:23                       ` Ian Jackson
2016-12-12 15:15                     ` Jan Beulich
2016-12-12 15:51                     ` Jan Beulich
2016-12-12 16:00                       ` Ian Jackson
2016-12-12 16:16                         ` Jan Beulich
2016-12-12 16:56                           ` Ian Jackson
2016-12-13  7:24                             ` Jan Beulich
2016-12-13 16:04                               ` Ian Jackson
2016-12-13 16:37                                 ` Jan Beulich
2016-12-09 15:44                   ` [PATCH 2/8] libelf: loop safety: Pass `elf' to elf_xen_parse_features Ian Jackson
2016-12-12 15:03                     ` Jan Beulich
2016-12-09 15:44                   ` [PATCH 3/8] libelf: loop safety: Call elf_iter_ok[_counted] in every loop Ian Jackson
2016-12-12 15:12                     ` Jan Beulich
2016-12-12 15:38                       ` Ian Jackson
2016-12-12 15:56                         ` Jan Beulich
2016-12-12 16:02                           ` Ian Jackson
2016-12-09 15:44                   ` [PATCH 4/8] libelf: loop safety: Call elf_iter_ok_counted at every *mem*_unsafe Ian Jackson
2016-12-12 15:19                     ` Jan Beulich
2016-12-12 15:54                       ` Ian Jackson
2016-12-12 15:58                         ` Jan Beulich
2016-12-12 16:03                           ` Ian Jackson
2016-12-09 15:44                   ` [PATCH 5/8] libelf: loop safety: Replace all calls to strcmp Ian Jackson
2016-12-12 15:22                     ` Jan Beulich
2016-12-12 15:44                       ` Ian Jackson
2016-12-09 15:44                   ` [PATCH 6/8] libelf: loop safety cleanup: Remove obsolete check in elf_shdr_count Ian Jackson
2016-12-12 15:41                     ` Jan Beulich
2016-12-09 15:44                   ` [PATCH 7/8] libelf: loop safety cleanup: Remove superseded image size copy check Ian Jackson
2016-12-12 16:26                     ` Jan Beulich
2016-12-09 15:44                   ` [PATCH 8/8] libelf: safety: Document safety principles in header file Ian Jackson
2016-12-15 16:43                     ` Jan Beulich
2016-12-16  4:28                       ` George Dunlap
2016-12-16 11:33                         ` Ian Jackson
2016-12-16 11:58                           ` Jan Beulich
2016-12-16 11:43                       ` Ian Jackson
2016-12-16 12:31                         ` Jan Beulich
2016-12-08 14:48   ` [PATCH] libelf: Fix div0 issues in elf_{shdr, phdr}_count() Ian Jackson

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=22601.32924.967352.367426@mariner.uk.xensource.com \
    --to=ian.jackson@eu.citrix.com \
    --cc=George.Dunlap@eu.citrix.com \
    --cc=JBeulich@suse.com \
    --cc=andrew.cooper3@citrix.com \
    --cc=sstabellini@kernel.org \
    --cc=tim@xen.org \
    --cc=wei.liu2@citrix.com \
    --cc=xen-devel@lists.xen.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.