mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Linus Torvalds <torvalds@osdl.org>
To: Timothy Miller <miller@techsource.com>
Cc: "Robert T. Johnson" <rtjohnso@eecs.berkeley.edu>,
	Al Viro <viro@math.psu.edu>,
	Linux Kernel <linux-kernel@vger.kernel.org>
Subject: Re: Finding user/kernel pointer bugs [no html]
Date: Thu, 10 Jun 2004 08:04:23 -0700 (PDT)	[thread overview]
Message-ID: <Pine.LNX.4.58.0406100754270.2050@ppc970.osdl.org> (raw)
In-Reply-To: <40C87934.2060505@techsource.com>



On Thu, 10 Jun 2004, Timothy Miller wrote:
> 
> Are user pointers actual pointers?

Yes. They have to be. We could make them "unsigned long" or something, but 
the fact is, they do have all the pointer attributes: they are pointers to 
structures, they have _meaning_.

> That's much too tempting to dereference.

Absolutely. Which is why sparse extends the C type system so that you can 
be a pointer yet _also_ not be something you can directly dereference.

The code

        int __user * a;
        *a = 0;
        return *a;

complains about

	test.c:6:3: warning: dereference of noderef expression
	test.c:7:10: warning: dereference of noderef expression

in sparse.

> If you really want to force user space accesses to follow certain rules, 
> make them longs or structs (or at least void *) (depending on 
> architecture) so that only the proper user-space-access functions can 
> interpret them.

.. and this would be a total disaster.

Think about it. The user pointer isn't just a "value". It has a type it 
points to. We want to do

	if (get_user(len, &uiov32->iov_len) ||
		...

and yes, the above is a real example. In fact, if you grep for "get_user" 
in linux/*/*.c _most_ of the uses seem to be of this type.

In other words: user pointers _are_ pointers. You have to be able to 
access member names through them etc. It's just that you can't dereference 
them directly - but they definitely have all the other attributes of a 
pointer.

> Now, if this "handle" corresponds directly to a user space pointer, 
> someone might cast it and dereference it, but that would be easy to 
> detect, and such patches would be easy to reject.
> 
> Bad idea?

Yes. Handles are a bad idea. They make the code unreadable and totally 
type-less.

Realize that if you use just one type (the "handle") for user pointers, 
you also totally lose all C type-checking. You can't see the difference 
between a "pointer to an old-style 'struct stat'" and a "pointer to a 
new-style 'struct stat64'". So then you'd have to add your own crud to do 
type verifiations (add magic words to the handle that describe the type 
etc). 

It's a nightmare.

You want strict _static_ type analysis. Static type analysis has zero 
run-time costs, and means that you get the "right" answer at compile-time. 

And that's exactly what sparse is all about. Static type analysis.

		Linus


  reply	other threads:[~2004-06-10 15:04 UTC|newest]

Thread overview: 25+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2004-06-10  3:31 Robert T. Johnson
2004-06-10  4:10 ` Linus Torvalds
2004-06-10  4:48   ` Robert T. Johnson
2004-06-10 14:46     ` Linus Torvalds
2004-06-10 16:57       ` viro
2004-06-10 15:07   ` Timothy Miller
2004-06-10 15:04     ` Linus Torvalds [this message]
2004-06-10 15:26       ` Timothy Miller
2004-06-10  4:49 ` viro
2004-06-10  5:20   ` Robert T. Johnson
2004-06-10 16:58   ` Greg KH
2004-06-10 17:27     ` David Brownell
2004-06-10 17:35       ` Greg KH
2004-06-10 17:54     ` Thomas Sailer
2004-06-10 18:34     ` Greg KH
2004-06-10 18:45       ` viro
2004-06-10 18:54         ` Greg KH
2004-06-10 19:10     ` Greg KH
2004-06-10 19:14       ` viro
2004-06-10 19:32         ` Greg KH
2004-06-10 19:38           ` viro
2004-06-10 20:28           ` Sam Ravnborg
2004-06-10 20:48             ` Randy.Dunlap
2004-06-11 17:21       ` Jean Delvare
2004-06-11 17:59         ` Greg KH

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=Pine.LNX.4.58.0406100754270.2050@ppc970.osdl.org \
    --to=torvalds@osdl.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=miller@techsource.com \
    --cc=rtjohnso@eecs.berkeley.edu \
    --cc=viro@math.psu.edu \
    /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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®