2010-01-15 17:25:06

by Hartley Sweeten

[permalink] [raw]
Subject: [PATCH] ipv4/ip_sockglue.c: copy msg_control optval from user to kernel space

ipv4/ip_sockglue.c: copy msg_control optval from user to kernel space

In do_ip_getsockopt the char __user *optval is used directly in
IP_PKTOPTIONS for the msg.msg_control and not copied from
user to kernel address space. This produces a sparse warning:

warning: incorrect type in assignment (different address spaces)
expected void *msg_control
got char [noderef] <asn:1>*optval

Fix this by using copy _from_user to set msg.msg_control.

Signed-off-by: H Hartley Sweeten <[email protected]>
Cc: David S. Miller <[email protected]>
Cc: Alexey Kuznetsov <[email protected]>
Cc: "Pekka Savola (ipv6)" <[email protected]>
Cc: James Morris <[email protected]>
Cc: Hideaki YOSHIFUJI <[email protected]>
Cc: Patrick McHardy <[email protected]>

---

diff --git a/net/ipv4/ip_sockglue.c b/net/ipv4/ip_sockglue.c
index cafad9b..8065456 100644
--- a/net/ipv4/ip_sockglue.c
+++ b/net/ipv4/ip_sockglue.c
@@ -1173,7 +1173,8 @@ static int do_ip_getsockopt(struct sock *sk, int level, int optname,
if (sk->sk_type != SOCK_STREAM)
return -ENOPROTOOPT;

- msg.msg_control = optval;
+ if (copy_from_user(msg.msg_control, optval, len))
+ return -EFAULT;
msg.msg_controllen = len;
msg.msg_flags = 0;


2010-01-16 01:30:39

by David Miller

[permalink] [raw]
Subject: Re: [PATCH] ipv4/ip_sockglue.c: copy msg_control optval from user to kernel space

From: H Hartley Sweeten <[email protected]>
Date: Fri, 15 Jan 2010 10:24:59 -0700

> ipv4/ip_sockglue.c: copy msg_control optval from user to kernel space
>
> In do_ip_getsockopt the char __user *optval is used directly in
> IP_PKTOPTIONS for the msg.msg_control and not copied from
> user to kernel address space. This produces a sparse warning:
>
> warning: incorrect type in assignment (different address spaces)
> expected void *msg_control
> got char [noderef] <asn:1>*optval
>
> Fix this by using copy _from_user to set msg.msg_control.
>
> Signed-off-by: H Hartley Sweeten <[email protected]>

This isn't right.

We want the 'optval' pointer itself, not the data it points to, stored
in msg.msg_control

And 'msg_control' is, in this case a user pointer.

It just isn't annotated (along with the rest of struct msghdr) with
"__user" because we mix the usage of this object with kernel and user
pointers.

How did you test your change?

2010-01-16 08:50:18

by David Miller

[permalink] [raw]
Subject: Re: [PATCH] ipv4/ip_sockglue.c: copy msg_control optval from user to kernel space

From: "H Hartley Sweeten" <[email protected]>
Date: Sat, 16 Jan 2010 01:22:21 -0500

> On Fri 1/15/2010 8:30 PM, David Miller wrote:
>> How did you test your change?
>
> Hmm... I saw the sparse warning and tried this to fix it. The code compiled
> fine and the warning was gone. When I booted the resulting kernel I didn't
> see any issues. I must not have went down this code path in my testing.

I'm going to ask you a second time.

What was your test case? How did you test the change?

I don't think you tested your change at all besides seeing that gcc
would accept the code and sparse stopped spitting out a warning. And
you're vagueness about your testing methodology will only work to
confirm my suspicions.

I find it unlikely, at best, for you to have tested that code path, as
'msg' is an uninitilized stack variable at this point in the code, so
'msg->msg_control' is going to be a garbage pointer, and therefore
copying to it would result in a crash.

I don't even think you read and understood the code you are editing.

I suspect you just wanted to kill the sparse warning somehow, you
found a way that made the compiler and sparse eat it, and you simply
ran with it.

And that really upsets me.

Fixing sparse warnings should not be a mindless exercise. You should
understand the code you are changing.