All of lore.kernel.org
 help / color / mirror / Atom feed
From: Vaishali Thakkar <vaishali.thakkar@oracle.com>
To: David Windsor <dave@progbits.org>, kernel-hardening@lists.openwall.com
Cc: Julia Lawall <julia.lawall@lip6.fr>
Subject: Re: [kernel-hardening] Use-after-free and management of reference counts
Date: Wed, 19 Oct 2016 09:00:54 +0530	[thread overview]
Message-ID: <5806E8EE.1060104@oracle.com> (raw)
In-Reply-To: <CAEXv5_gS-rS_31DoDFGu3YaknLpK654+S5-d=KXAHa8+ziPjYA@mail.gmail.com>



On Wednesday 19 October 2016 05:49 AM, David Windsor wrote:
> On Tue, Oct 18, 2016 at 6:56 PM, Sandy Harris <sandyinchina@gmail.com> wrote:
>> On Tue, Oct 18, 2016 at 9:13 AM, Jann Horn <jann@thejh.net> wrote:
>>
>>> Use-after-frees are really hard to deal with.
>>
>> I want to ask a possibly naive questions -- why can't we avoid the
>> whole class of bugs in the first place? I can think of only two main
>> sources for use-after-free bugs, both avoidable.
>>
>> One is allocating something within a function, then returning a
>> pointer to it. I know I've done that on occasion, got a warning from
>> lint(1) or the compiler, & fixed it; I have forgotten details. Surely
>> current static analysis tools can catch nearly all of these. If not,
>> writing one looks fairly straightforward. Sure, complex code with a
>> lot of indirection might fool such tools, but in general such code
>> should not be used anyway.
>>
> 
> The main use case in the kernel of reference counters is garbage
> collection: the kernel manages a particular object that has multiple
> users.  It cannot safely delete this object until all users of the
> object have finished using it.   Users acquire and release references
> to this object via a get()/put() API.  When all users have called
> put(), the reference counter becomes 0 (or -1, or some other
> sentinel), and the kernel frees the object.
> 
> Things go wrong when the reference counter gets overflowed.  If the
> reference counter is represented with a signed integer type,
> overflowing the reference counter causes it to go from INT_MAX to
> INT_MIN, then approach 0.  Depending on the logic, the transition to
> INT_MIN may be enough to trigger the bug, but when the reference
> counter becomes 0, the kernel will free the underlying object guarded
> by the reference counter while it still has valid users.

One of the other case is when users forget to call put() functions.
I think we have seen many such missing put() cases in the kernel.
Sometimes in some cases it is also possible that users may call put()
little early. 

>> The other is misusing malloc()/free() or their kernel code analogs, &
>> the obvious solution is to avoid using those wherever possible. There
>> are some kernel data structures that need to grow dynamically, but I
>> do not think there need to be a lot. Code the few very carefully and
>> refuse to accept patches elsewhere that use dynamic allocation; the
>> problem may not vanish, but it looks like this reduces it to
>> manageable size.
>>
>> I have the feeling I might be missing something here. Pointers welcome.
> 

-- 
Vaishali

  reply	other threads:[~2016-10-19  3:30 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-10-18 12:22 [kernel-hardening] Use-after-free and management of reference counts Vaishali Thakkar
2016-10-18 13:11 ` David Windsor
2016-10-18 15:04   ` Colin Vidal
2016-10-18 13:13 ` Jann Horn
2016-10-18 22:56   ` Sandy Harris
2016-10-18 23:58     ` Jann Horn
2016-10-24 22:02       ` Sandy Harris
2016-10-24 22:16       ` Sandy Harris
2016-10-19  0:19     ` David Windsor
2016-10-19  3:30       ` Vaishali Thakkar [this message]
2016-10-18 21:36 ` Kees Cook
2016-10-24  7:13   ` Vaishali Thakkar

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=5806E8EE.1060104@oracle.com \
    --to=vaishali.thakkar@oracle.com \
    --cc=dave@progbits.org \
    --cc=julia.lawall@lip6.fr \
    --cc=kernel-hardening@lists.openwall.com \
    /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.