Skip to contentSkip to conversation

Untrusted user data in kernel

Coverity New Bryan Fulton 15 participants 25 messages Dec 17, 2004 – Jan 7, 2005 2 messages not in the archive

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

Hi, recently we ran a security checker over linux and discovered some flaws in the Linux 2.6.9 kernel. I've inserted into this post a few examples of what we found. These functions copy in user data (copy_from_user) and use it as an array index, loop bound or memory function argument without proper bounds checking.

This posting just involves bugs in /fs, /net and /drivers/net. There will be more postings for similar flaws in the drivers, as well as network exploitable bugs and bugs in system calls.

Some can be viewed as minor as they might involve directly passing an unsigned tainted scalar to kmalloc. I was under the impression that kmalloc has an implicit bounds check as it returns null if attempting to allocate >64kb (or at least it used to). Can someone confirm/disconfirm that?

Suggestions for other security properties to check are welcome. We appreciate your feedback as a method to improve and expand our security checkers.

Thanks,
.:Bryan Fulton and Ted Unangst of Coverity, Inc.

Quick location summary:

/fs/coda/pioctl.c::coda_pioctl /fs/xfs/linux-2.6/xfs_ioctl.c::xfs_attrmulti_by_handle /net/ipv6/netfilter/ip6_tables.c::do_replace /net/bridge/br_ioctl.c::old_deviceless /net/rose/rose_route.c::rose_rt_ioctl /drivers/net/wan/sdla.c::sdla_xfer

/////////////////////////////////////////////////////
// 1: /fs/coda/pioctl.c::coda_pioctl //
/////////////////////////////////////////////////////
- tainted scalars (signed shorts) data->vi.in_size and data->vi.out_size are used to copy memory from and to user space
- neither are properly upper/lower bounds checked (in_size only upper-bound checked, out_size only lower-bound checked)

Call to function "copy_from_user" TAINTS argument "data"

61 if (copy_from_user(&data, (void __user *)user_data, sizeof(data)))
{
62 return -EINVAL;
63 }
64

...

TAINTED variable "(data).vi" was passed to a tainted sink.

90 error = venus_pioctl(inode->i_sb, &(cnp->c_fid), cmd, &data);
91
92 path_release(&nd);
93 return error;
94 }
95
96

inside linux-2.6.9/fs/coda/upcall.c::venus_pioctl

Checked upper bounds of signed scalar "((data)->vi).in_size"

                                 by "((data)->vi).in_size > 8192"

553 if (data->vi.in_size > VC_MAXDATASIZE) {
554 error = -EINVAL;
555 goto exit;
556 }
557

...

Assigned TAINTED variable "((data)->vi).in_size" to variable "((inp)->coda_ioctl).len"

568 inp->coda_ioctl.len = data->vi.in_size;

...

TAINTED variable "((data)->vi).in_size" passed to tainted data sink "copy_from_user"

572 if ( copy_from_user((char*)inp + (long)inp->coda_ioctl.data,
573 data->vi.in, data->vi.in_size) ) {
574 error = -EINVAL;
575 goto exit;
576 }

...

Checked lower bounds of signed scalar "((data)->vi).out_size" by

                            "((outp)->coda_ioctl).len >

((data)->vi).out_size"

588 if (outp->coda_ioctl.len > data->vi.out_size) {
589 error = -EINVAL;
590 } else {

TAINTED variable "((data)->vi).out_size" passed to tainted data sink "copy_to_user"

591 if (copy_to_user(data->vi.out,
592 (char *)outp +
(long)outp->coda_ioctl.data,
593 data->vi.out_size)) {
594 error = -EFAULT;
595 goto exit;
596 }

////////////////////////////////////////////////////////////////////
// 2: /fs/xfs/linux-2.6/xfs_ioctl.c::xfs_attrmulti_by_handle //
////////////////////////////////////////////////////////////////////

- tainted unsigned scalar am_hreq.opcount multiplied and passed to kmalloc (512) and copy_from_user (518), and used as a loop bounds (524)
- this is fairly minor as there is a capable() call before the copy_from_user in xfs_vget_fsop_handlereq

Call to function "xfs_vget_fsop_handlereq" TAINTS argument "am_hreq"

504 error = xfs_vget_fsop_handlereq(mp, parinode, CAP_SYS_ADMIN, arg,
505
sizeof(xfs_fsop_attrmulti_handlereq_t),
506 (xfs_fsop_handlereq_t *)&am_hreq,
507 &vp, &inode);
508 if (error)
509 return -error;
510

Assign TAINTED variable "((am_hreq).opcount * 24)" to variable "size"

511 size = am_hreq.opcount * sizeof(attr_multiop_t);

TAINTED variable "size" was passed to a tainted sink.

512 ops = (xfs_attr_multiop_t *)kmalloc(size, GFP_KERNEL);

...

TAINTED variable "size" was passed to a tainted sink.

518 if (copy_from_user(ops, am_hreq.ops, size)) {

519           kfree(ops);
520           VN_RELE(vp);
521           return -XFS_ERROR(EFAULT);

522 }
523

TAINTED variable "(am_hreq).opcount" used as a loop boundary

524 for (i = 0; i < am_hreq.opcount; i++) {

////////////////////////////////////////////////////////
// 3: /net/ipv6/netfilter/ip6_tables.c::do_replace //
////////////////////////////////////////////////////////

- tainted unsigned scalar tmp.num_counters multiplied and passed to vmalloc (1161) and memset (1166) which could overflow or be too large

Call to function "copy_from_user" TAINTS argument "tmp"

1143 if (copy_from_user(&tmp, user, sizeof(tmp)) != 0)
1144 return -EFAULT;

...

TAINTED variable "((tmp).num_counters * 16)" was passed to a tainted sink.

1161 counters = vmalloc(tmp.num_counters * sizeof(struct
ip6t_counters));
1162 if (!counters) {
1163 ret = -ENOMEM;
1164 goto free_newinfo;
1165 }

TAINTED variable "((tmp).num_counters * 16)" was passed to a tainted sink.

1166 memset(counters, 0, tmp.num_counters * sizeof(struct
ip6t_counters));

//////////////////////////////////////////////////
// 4: /net/bridge/br_ioctl.c::old_deviceless //
//////////////////////////////////////////////////

- tainted unsigned scalar args[2] multiplied and passed to kmalloc (327) and memset (331) which could overflow
- same scalar then used as a boundary to array index to the alloc'd memory (inside get_bridge_ifindices)

Call to function "copy_from_user" TAINTS argument "args"

315 if (copy_from_user(args, uarg, sizeof(args)))
316 return -EFAULT;
317
317
318 switch (args[0]) {
319

...

TAINTED variable "(args[2] * 4)" was passed to a tainted sink.

327 indices = kmalloc(args[2]*sizeof(int),
GFP_KERNEL);

...

TAINTED variable "(args[2] * 4)" was passed to a tainted sink.

331 memset(indices, 0, args[2]*sizeof(int));
332 args[2] = get_bridge_ifindices(indices,
args[2]);
333

inside /net/bridge/br_ioctl.c::get_bridge_ifindices

24 static int get_bridge_ifindices(int *indices, int num)
25 {
26 struct net_device *dev;
27 int i = 0;
28
29 for (dev = dev_base; dev && i < num; dev = dev->next) {
30 if (dev->priv_flags & IFF_EBRIDGE)
31 indices[i++] = dev->ifindex;
32 }
33
34 return i;
35 }

//////////////////////////////////////////////////
// 5: /net/rose/rose_route.c::rose_rt_ioctl //
//////////////////////////////////////////////////

- tainted scalar (unsigned char) rose_route->ndigis used as a loop boundary (122) for indexing into rose_neigh->digipeat->calls[] of 8 structs

Call to function "copy_from_user" TAINTS argument "rose_route"

720 if (copy_from_user(&rose_route, arg,
sizeof(struct
rose_route_struct)))
721 return -EFAULT;

...mem.len

TAINTED variable "(rose_route).ndigis" was passed to a tainted sink. [model]

731 err = rose_add_node(&rose_route, dev);

inside /net/rose/rose_route.c::rose_add_node

112 if (rose_route->ndigis != 0) {
...

Tainted variable "(rose_route)->ndigis" used as a loop boundary

122 for (i = 0; i < rose_route->ndigis; i++)
{
123 rose_neigh->digipeat->calls[i] =
124 rose_route->digipeaters[i];
125 rose_neigh->digipeat->repeated[i] = 0;
126 }

//////////////////////////////////////////////
// 6: /drivers/net/wan/sdla.c::sdla_xfer //
//////////////////////////////////////////////

- tainted signed scalar mem.len passed to kmalloc and memset (1206 and 1211, or 1220 and 1223). Possibly minor because of kmalloc's implicit size check

Call to function "copy_from_user" TAINTS argument "mem"

1201 if(copy_from_user(&mem, info, sizeof(mem)))

...

TAINTED variable "(mem).len" was passed to a tainted sink.

1206 temp = kmalloc(mem.len, GFP_KERNEL);

...

TAINTED variable "(mem).len" was passed to a tainted sink.

1209 memset(temp, 0, mem.len);
1210 sdla_read(dev, mem.addr, temp, mem.len);

TAINTED variable "(mem).len" was passed to a tainted sink.

1211 if(copy_to_user(mem.data, temp, mem.len))

...

TAINTED variable "(mem).len" was passed to a tainted sink.

1220 temp = kmalloc(mem.len, GFP_KERNEL);
1221 if (!temp)
1222 return(-ENOMEM);

TAINTED variable "(mem).len" was passed to a tainted sink.

1223 if(copy_from_user(temp, mem.data, mem.len))
1224 {

Signature · 4 lines
Bryan J Fulton
Coverity, Inc.

Email: [email protected]
#1 lore raw

This at least needs CAP_NET_ADMIN.

Quoted 30 lines · Bryan Fulton

////////////////////////////////////////////////////////
// 3: /net/ipv6/netfilter/ip6_tables.c::do_replace //
////////////////////////////////////////////////////////

- tainted unsigned scalar tmp.num_counters multiplied and passed to vmalloc (1161) and memset (1166) which could overflow or be too large

Call to function "copy_from_user" TAINTS argument "tmp"

1143 if (copy_from_user(&tmp, user, sizeof(tmp)) != 0)
1144 return -EFAULT;

...

TAINTED variable "((tmp).num_counters * 16)" was passed to a tainted sink.

1161 counters = vmalloc(tmp.num_counters * sizeof(struct
ip6t_counters));
1162 if (!counters) {
1163 ret = -ENOMEM;
1164 goto free_newinfo;
1165 }

TAINTED variable "((tmp).num_counters * 16)" was passed to a tainted sink.

1166 memset(counters, 0, tmp.num_counters * sizeof(struct
ip6t_counters));

Signature · 2 lines
James Morris
<[email protected]>
#2 lore raw
Quoted 2 lines · James Morris

This at least needs CAP_NET_ADMIN.

It is already checked in do_ip6t_set_ctl(). Otherwise anyone could replace iptables rules :)

Regards
Patrick

Quoted 39 lines · Bryan Fulton
Quoted 34 lines

////////////////////////////////////////////////////////
// 3: /net/ipv6/netfilter/ip6_tables.c::do_replace //
////////////////////////////////////////////////////////

- tainted unsigned scalar tmp.num_counters multiplied and passed to vmalloc (1161) and memset (1166) which could overflow or be too large

Call to function "copy_from_user" TAINTS argument "tmp"

1143 if (copy_from_user(&tmp, user, sizeof(tmp)) != 0)
1144 return -EFAULT;

...

TAINTED variable "((tmp).num_counters * 16)" was passed to a tainted sink.

1161 counters = vmalloc(tmp.num_counters * sizeof(struct
ip6t_counters));
1162 if (!counters) {
1163 ret = -ENOMEM;
1164 goto free_newinfo;
1165 }

TAINTED variable "((tmp).num_counters * 16)" was passed to a tainted sink.

1166 memset(counters, 0, tmp.num_counters * sizeof(struct
ip6t_counters));

#3 lore raw
Quoted 6 lines · Patrick McHardy
Quoted 2 lines · James Morris

This at least needs CAP_NET_ADMIN.

It is already checked in do_ip6t_set_ctl(). Otherwise anyone could replace iptables rules :)

That's what I meant, you need the capability to do anything bad :-)

- James

Signature · 2 lines
James Morris
<[email protected]>
#4 lore raw
Quoted 2 lines · James Morris

That's what I meant, you need the capability to do anything bad :-)

But.. even if you have the 'permission' to do bad things, it shouldn't be possible.

It's a bug, and only because you can't exploit it if you haven't the right capabilities doesn't make the bug disappear.

IMHO such things (passing values between user/kernel space) should always be checked.

tom

maintainer
#5 lore raw
Quoted 2 lines · On Fri, 17 Dec 2004 14:18:52 +0100 Tomas Carnecky <[email protected]> wrote:

IMHO such things (passing values between user/kernel space) should always be checked.

As per Patrick's posting, which James was responding to, it is checked at the level above this function.

#6 lore raw
Quoted 9 lines · David S. Miller
Quoted 1 line · On Fri, 17 Dec 2004 14:18:52 +0100 Tomas Carnecky <[email protected]> wrote:

IMHO such things (passing values between user/kernel space) should always be checked.

As per Patrick's posting, which James was responding to, it is checked at the level above this function.

Is only the capability checked or also the data passed to the kernel? It's not clear from Patricks reply:

Quoted 2 lines

It is already checked in do_ip6t_set_ctl(). Otherwise anyone could replace iptables rules :)

For me it seems that only CAP_NET_ADMIN is checked and not the data.

tom

maintainer
#7 lore raw
Quoted 3 lines · On Fri, 17 Dec 2004 20:34:55 +0100 Tomas Carnecky <[email protected]> wrote:
Quoted 2 lines

It is already checked in do_ip6t_set_ctl(). Otherwise anyone could replace iptables rules :)

For me it seems that only CAP_NET_ADMIN is checked and not the data.

If that's the case then I agree with you Tomas.

#8 lore raw
In reply to James Morris
Quoted 6 lines · James Morris
Quoted 3 lines · Patrick McHardy

James Morris wrote:

Quoted 8 lines
Quoted 5 lines
Quoted 2 lines

This at least needs CAP_NET_ADMIN.

It is already checked in do_ip6t_set_ctl(). Otherwise anyone could replace iptables rules :)

That's what I meant, you need the capability to do anything bad :-)

Are you saying that processes with capability don't make mistakes? This isn't a bug related to untrusted users doing privileged operations, it's a case of using unchecked user data.

Signature · 3 lines
   -bill davidsen ([email protected])
"The secret to procrastination is to put things off until the
 last possible moment - but no longer"  -me
#9 lore raw
Quoted 10 lines · Bill Davidsen
Quoted 9 lines · James Morris
Quoted 6 lines · Patrick McHardy
Quoted 2 lines · James Morris

This at least needs CAP_NET_ADMIN.

Quoted 8 lines
Quoted 5 lines
Quoted 2 lines

It is already checked in do_ip6t_set_ctl(). Otherwise anyone could replace iptables rules :)

That's what I meant, you need the capability to do anything bad :-)

Are you saying that processes with capability don't make mistakes? This isn't a bug related to untrusted users doing privileged operations, it's a case of using unchecked user data.

But isn't there always the possibility of "unchecked user data"? I can, as root, do cp /dev/zero /dev/mem and have the most spectacular crask you've evet seen. I can even make my file- systems unrecoverable.

Cheers,
Dick Johnson
Penguin : Linux version 2.6.9 on an i686 machine (5537.79 BogoMips).
Notice : All mail here is now cached for review by Dictator Bush.
98.36% of all statistics are fiction.

#10 lore raw
Quoted 10 lines
Quoted 5 lines

Are you saying that processes with capability don't make mistakes? This isn't a bug related to untrusted users doing privileged operations, it's a case of using unchecked user data.

But isn't there always the possibility of "unchecked user data"? I can, as root, do cp /dev/zero /dev/mem and have the most spectacular crask you've evet seen. I can even make my file- systems unrecoverable.

Only if you have the capability for raw hardware access.
The same is true for the firmware interface. What other subsystems might be dangerous?

Regards
Oliver

#11 lore raw
In reply to linux-os
Quoted 21 lines · linux-os
Quoted 19 lines · Bill Davidsen
Quoted 15 lines · James Morris
Quoted 8 lines · Patrick McHardy
Quoted 2 lines · James Morris

This at least needs CAP_NET_ADMIN.

It is already checked in do_ip6t_set_ctl(). Otherwise anyone could replace iptables rules :)

That's what I meant, you need the capability to do anything bad :-)

Quoted 7 lines
Quoted 2 lines

Are you saying that processes with capability don't make mistakes? This isn't a bug related to untrusted users doing privileged operations, it's a case of using unchecked user data.

But isn't there always the possibility of "unchecked user data"? I can, as root, do cp /dev/zero /dev/mem and have the most spectacular crask you've evet seen. I can even make my file- systems unrecoverable.

But that's not the type of thing you would do by accident. The kernel can't protect against deliberate abuse by trusted users, nor should it. But the type of problem caused by an application program bug can, and I believe should, be caught.

The difference between "oops" and "take that!"

Signature · 3 lines
   -bill davidsen ([email protected])
"The secret to procrastination is to put things off until the
 last possible moment - but no longer"  -me
#12 lore raw
In reply to linux-os
Quoted 9 lines · linux-os
Quoted 7 lines · Bill Davidsen
Quoted 3 lines · James Morris

On Fri, 17 Dec 2004, Patrick McHardy wrote:

That's what I meant, you need the capability to do anything bad :-)

Quoted 8 lines
Quoted 2 lines

Are you saying that processes with capability don't make mistakes? This isn't a bug related to untrusted users doing privileged operations, it's a case of using unchecked user data.

But isn't there always the possibility of "unchecked user data"? I can, as root, do cp /dev/zero /dev/mem and have the most spectacular crask you've evet seen. I can even make my file- systems unrecoverable.

But the difference between you example (cp /dev/zero /dev/mem) and passing unchecked data to the kernel is... you _can_ check the data and do something about it if you discover that the data is not valid/within a range/whatever even if the user has full permissions. No same person would do a 'cp /dev/zero /dev/mem', but passing bad data is more likely to happen, badly written userspace configuration tools etc.

tom

#13 lore raw
Quoted 3 lines

But the difference between you example (cp /dev/zero /dev/mem) and passing unchecked data to the kernel is... you _can_ check the data and

This is the difference:
static int open_port(struct inode * inode, struct file * filp)

{
	return capable(CAP_SYS_RAWIO) ? 0 : -EPERM;
}

(from mem.c)

Regards
Oliver

#14 lore raw
Quoted 10 lines · Oliver Neukum
Quoted 1 line

But the difference between you example (cp /dev/zero /dev/mem) and passing unchecked data to the kernel is... you _can_ check the data and

This is the difference:
static int open_port(struct inode * inode, struct file * filp)

{
	return capable(CAP_SYS_RAWIO) ? 0 : -EPERM;
}

(from mem.c)

OK, but my point was, whenever you can check the 'contents' of the data passed to the kernel, do it. You can't check if the data someone writes to /dev/mem is valid or not, but you can check for out-of-range/etc. data in ioctl & friends.

tom

#15 lore raw
In reply to James Morris

Hi!

Quoted 1 line

This at least needs CAP_NET_ADMIN.

Hmm, but that means that CAP_NET_ADMIN implies all other capabilities, unless you fix this.

Pavel

Quoted 15 lines
Quoted 15 lines

TAINTED variable "((tmp).num_counters * 16)" was passed to a tainted sink.

1161 counters = vmalloc(tmp.num_counters * sizeof(struct
ip6t_counters));
1162 if (!counters) {
1163 ret = -ENOMEM;
1164 goto free_newinfo;
1165 }

TAINTED variable "((tmp).num_counters * 16)" was passed to a tainted sink.

1166 memset(counters, 0, tmp.num_counters * sizeof(struct
ip6t_counters));

Signature · 1 line
Boycott Kodak -- for their patent abuse against Java.
#16 lore raw
Quoted 6 lines · Pavel Machek

Hi!

Quoted 1 line

This at least needs CAP_NET_ADMIN.

Hmm, but that means that CAP_NET_ADMIN implies all other capabilities, unless you fix this.

I'm not saying it doesn't need to be fixed, but that it is not exploitable by unprivileged users.

- James

Signature · 2 lines
James Morris
<[email protected]>
#17 lore raw
In reply to Bryan Fulton

Hi Bryan,

First of all, thanks very much for this effort.

Davem: Please check the networking ones, there are several.

Quoted 9 lines · Bryan Fulton

Hi, recently we ran a security checker over linux and discovered some flaws in the Linux 2.6.9 kernel. I've inserted into this post a few examples of what we found. These functions copy in user data (copy_from_user) and use it as an array index, loop bound or memory function argument without proper bounds checking.

This posting just involves bugs in /fs, /net and /drivers/net. There will be more postings for similar flaws in the drivers, as well as network exploitable bugs and bugs in system calls.

We're anxious to see those.

Quoted 5 lines

Some can be viewed as minor as they might involve directly passing an unsigned tainted scalar to kmalloc. I was under the impression that kmalloc has an implicit bounds check as it returns null if attempting to allocate >64kb (or at least it used to). Can someone confirm/disconfirm that?

On v2.6 the kmalloc limit is 128k for most machines.

!CONFIG_MMU allows up to 1Mb and CONFIG_LARGE_ALLOCS allows up to 32Mb, so better not rely on kmalloc checking. ;)

Quoted 3 lines

Suggestions for other security properties to check are welcome. We appreciate your feedback as a method to improve and expand our security checkers.

Please run the checker on v2.4.29-pre3.

Would be really nice if you could do it periodically say on each new kernel release (v2.6 and v2.4) or a monthly basis.

Quoted 19 lines

Thanks,
.:Bryan Fulton and Ted Unangst of Coverity, Inc.

Quick location summary:

/fs/coda/pioctl.c::coda_pioctl /fs/xfs/linux-2.6/xfs_ioctl.c::xfs_attrmulti_by_handle /net/ipv6/netfilter/ip6_tables.c::do_replace /net/bridge/br_ioctl.c::old_deviceless /net/rose/rose_route.c::rose_rt_ioctl /drivers/net/wan/sdla.c::sdla_xfer

/////////////////////////////////////////////////////
// 1: /fs/coda/pioctl.c::coda_pioctl //
/////////////////////////////////////////////////////
- tainted scalars (signed shorts) data->vi.in_size and data->vi.out_size are used to copy memory from and to user space
- neither are properly upper/lower bounds checked (in_size only upper-bound checked, out_size only lower-bound checked)

<snip>

Quoted 22 lines

TAINTED variable "((data)->vi).in_size" passed to tainted data sink "copy_from_user"

572 if ( copy_from_user((char*)inp + (long)inp->coda_ioctl.data,
573 data->vi.in, data->vi.in_size) ) {
574 error = -EINVAL;
575 goto exit;
576 }

...

Checked lower bounds of signed scalar "((data)->vi).out_size" by

                            "((outp)->coda_ioctl).len >

((data)->vi).out_size"

588 if (outp->coda_ioctl.len > data->vi.out_size) {
589 error = -EINVAL;
590 } else {

TAINTED variable "((data)->vi).out_size" passed to tainted data sink "copy_to_user"

Quoted 7 lines

591 if (copy_to_user(data->vi.out,
592 (char *)outp +
(long)outp->coda_ioctl.data,
593 data->vi.out_size)) {
594 error = -EFAULT;
595 goto exit;
596 }

Correct, fix for both v2.4 and v2.6 attached. Adds bound checking:

Jan Harkes, please check correctness so we can apply it.

fs/coda/upcall.c renamed from fs/coda/upcall.c.orig +7 -2
@@ -550,10 +550,15 @@
 	UPARG(CODA_IOCTL);
 
         /* build packet for Venus */
-        if (data->vi.in_size > VC_MAXDATASIZE) {
+        if (data->vi.in_size > VC_MAXDATASIZE || data->vi.in_size < 0) {
 		error = -EINVAL;
 		goto exit;
-        }
+        } 
+
+	if (data->vi.out_size > VC_MAXDATASIZE || data->vi.out_size < 0) {
+		error = -EINVAL;
+		goto exit;
+	}
 
         inp->coda_ioctl.VFid = *fid;
     
Quoted 3 lines

////////////////////////////////////////////////////////////////////
// 2: /fs/xfs/linux-2.6/xfs_ioctl.c::xfs_attrmulti_by_handle //
////////////////////////////////////////////////////////////////////

XFS people, can you take care of this one please. Not a security threat, protected under ADMIN caps.

Quoted 3 lines

////////////////////////////////////////////////////////
// 3: /net/ipv6/netfilter/ip6_tables.c::do_replace //
////////////////////////////////////////////////////////

This one lacks bound checking as people discussed, but is not a security threat since region is protected under NET_ADMIN caps.

Quoted 3 lines

//////////////////////////////////////////////////
// 4: /net/bridge/br_ioctl.c::old_deviceless //
//////////////////////////////////////////////////

Lacks bound checking but is not a security threat since region is protectedunder NET_ADMIN caps.

Something similar to this should do it. Not sure if "65535" is a sane value, though.

br_ioctl.c 2005-01-05 11:02:30.552652112 -0200 renamed from br_ioctl.c.orig 2005-01-05 11:02:28.301994264 -0200 +3 -0
@@ -324,6 +324,9 @@
                int *indices;
                int ret = 0;
 
+               if (args[2] > 65535)
+                       return -EFAULT;
+
                indices = kmalloc(args[2]*sizeof(int), GFP_KERNEL);
                if (indices == NULL)
                        return -ENOMEM;
Quoted 3 lines

//////////////////////////////////////////////////
// 5: /net/rose/rose_route.c::rose_rt_ioctl //
//////////////////////////////////////////////////

Not a security threat because requires NET_ADMIN caps.

Alan, Arnaldo, proper bound checking is required nevertheless. Can you take a look at this please?

Quoted 7 lines

//////////////////////////////////////////////
// 6: /drivers/net/wan/sdla.c::sdla_xfer //
//////////////////////////////////////////////

- tainted signed scalar mem.len passed to kmalloc and memset (1206 and 1211, or 1220 and 1223). Possibly minor because of kmalloc's implicit size check

Protected by NET_ADMIN caps, but definately needs some bound checking.

Jan, I think SDLA_MAX_DATA is the correct bound to check for here, can you confirm please?

drivers/net/wan/sdla.c +3 -0
@@ -1195,6 +1195,9 @@
 
 	if(copy_from_user(&mem, info, sizeof(mem)))
 		return -EFAULT;
+
+	if (mem.len > SDLA_MAX_DATA || mem.len < 0)
+		return -EFAULT;
 		
 	if (read)
 	{	
fs/coda/upcall.c renamed from fs/coda/upcall.c.orig +6 -1
@@ -538,11 +538,16 @@
 	UPARG(CODA_IOCTL);
 
         /* build packet for Venus */
-        if (data->vi.in_size > VC_MAXDATASIZE) {
+        if (data->vi.in_size > VC_MAXDATASIZE || data->vi.in_size < 0) {
 		error = -EINVAL;
 		goto exit;
         }
 
+	if (data->vi.out_size > VC_MAXDATASIZE || data->vi.out_size < 0) {
+		error = -EINVAL;
+		goto exit;
+	}
+
         inp->coda_ioctl.VFid = *fid;
     
         /* the cmd field was mutated by increasing its size field to
fs/coda/upcall.c renamed from fs/coda/upcall.c.orig +7 -2
@@ -550,10 +550,15 @@
 	UPARG(CODA_IOCTL);
 
         /* build packet for Venus */
-        if (data->vi.in_size > VC_MAXDATASIZE) {
+        if (data->vi.in_size > VC_MAXDATASIZE || data->vi.in_size < 0) {
 		error = -EINVAL;
 		goto exit;
-        }
+        } 
+
+	if (data->vi.out_size > VC_MAXDATASIZE || data->vi.out_size < 0) {
+		error = -EINVAL;
+		goto exit;
+	}
 
         inp->coda_ioctl.VFid = *fid;
     
maintainer
#18 lore raw
Quoted 4 lines · Marcelo Tosatti

On Thu, Dec 16, 2004 at 05:33:32PM -0800, Bryan Fulton wrote:
Correct, fix for both v2.4 and v2.6 attached. Adds bound checking:

Jan Harkes, please check correctness so we can apply it.

I was looking at this and actually think that both in_size and out_size should just be changed to unsigned short instead of signed short (the values should never be negative, period).

That fixes the bounds checking on the in_size, but the out_size one is still questionable. I'm not even sure the code is actually testing the right thing there.

fs/coda/upcall.c line 556

Quoted 12 lines
fs/coda/upcall.c renamed from fs/coda/upcall.c.orig +2 -2
@@ -550,10 +550,15 @@
 	UPARG(CODA_IOCTL);
 
         /* build packet for Venus */
-        if (data->vi.in_size > VC_MAXDATASIZE) {
+        if (data->vi.in_size > VC_MAXDATASIZE || data->vi.in_size < 0) {
 		error = -EINVAL;
 		goto exit;
-        }
+        } 

This part would work, but changing to the variable to unsigned short in include/linux/coda.h works just as well.

fs/coda/upcall.c line 561

Quoted 4 lines
fs/coda/upcall.c renamed from fs/coda/upcall.c.orig +4 -0
+	if (data->vi.out_size > VC_MAXDATASIZE || data->vi.out_size < 0) {
+		error = -EINVAL;
+		goto exit;
+	}

We might be overwriting out_size when making the upcall to venus, so checking out_size here probably doesn't help all that much. I'm still looking at what exactly is going on with that. I should have a patch by the end of the week.

Jan

#19 lore raw
In reply to Marcelo Tosatti
Quoted 6 lines · Marcelo Tosatti
Quoted 3 lines

////////////////////////////////////////////////////////////////////
// 2: /fs/xfs/linux-2.6/xfs_ioctl.c::xfs_attrmulti_by_handle //
////////////////////////////////////////////////////////////////////

XFS people, can you take care of this one please. Not a security threat, protected under ADMIN caps.

Fixed in our local tree coupla weeks ago, it'll be in the next update.

cheers.

Signature · 1 line
Nathan

A message that is not in the archive (sent off-list or not yet received): [email protected]

A message that is not in the archive (sent off-list or not yet received): [email protected]

#22 lore raw

: //////////////////////////////////////////////
: // 6: /drivers/net/wan/sdla.c::sdla_xfer //
: //////////////////////////////////////////////
:
I am not a maintainer of sdla driver, but being Cc'd with this
mail, I'll try to look at it.

: - tainted signed scalar mem.len passed to kmalloc and memset (1206 and : 1211, or 1220 and 1223). Possibly minor because of kmalloc's : implicit size check

Yes. The value mem.len is passed to kmalloc and the code immediately
returns -ENOMEM when kmalloc fails.

: Protected by NET_ADMIN caps, but definately needs some bound checking. :
Depends on whether the kmalloc's internal checks are
sufficient or not.

: Jan, I think SDLA_MAX_DATA is the correct bound to check for here, can : you confirm please?
:
I cannot because I don't know or even have this hardware.
However: looking at the definitions in include/linux/sdla.h and the code itself it looks like SDLA_READMEM, SDLA_WRITEMEM, and SDLA_CLEAR ioctls are for reading/writing/clearing the card memory itself (maybe for debugging purposes or downloading a firmware or what). So no, SDLA_MAX_DATA is probably not a correct limit there.

The SDLA_CLEAR ioctl (the sdla_clear(dev) function) tries
to clear exactly 65536 bytes (hardcoded at sdla.c:sdla_clear() line 140). So the mem.len should be <= 65536 bytes, and even mem.addr + mem.len should be <= 65536 bytes.

So I propose the following patch (maybe the constant 65536 should
be defined in sdla.h and used both in sdla_xfer() and sdla_clear()):

drivers/net/wan/sdla.c renamed from drivers/net/wan/sdla.c.orig +4 -0
@@ -1195,6 +1195,10 @@
 
 	if(copy_from_user(&mem, info, sizeof(mem)))
 		return -EFAULT;
+
+	if (mem.len <= 0 || mem.addr < 0 || mem.len > 65536 || mem.addr > 65535
+		|| mem.addr + mem.len > 65536)
+		return -EFAULT;
 		
 	if (read)
 	{	
Signature · 3 lines
| Jan "Yenya" Kasprzak  <kas at {fi.muni.cz - work | yenya.net - private}> |
| GPG: ID 1024/D3498839      Fingerprint 0D99A7FB206605D7 8B35FCDE05B18A5E |
| http://www.fi.muni.cz/~kas/   Czech Linux Homepage: http://www.linux.cz/ |
Quoted 2 lines

Whatever the Java applications and desktop dances may lead to, Unix will <
still be pushing the packets around for a quite a while. --Rob Pike <

#23 lore raw

drivers/net/wan/sdla.c line 1201

Quoted 4 lines · Jan Kasprzak

[...]

drivers/net/wan/sdla.c renamed from drivers/net/wan/sdla.c.orig +3 -0
+	if (mem.len <= 0 || mem.addr < 0 || mem.len > 65536 || mem.addr > 65535
+		|| mem.addr + mem.len > 65536)
+		return -EFAULT;

Just an extremely small nitpick. The conditions

mem.len > 65536 || mem.addr > 65535

aren't needed, because if one of them is true, then

mem.addr + mem.len > 65536

must be true also, since we've already asserted that len>0 and addr>=0.

This would be even simpler if len and addr were unsigned as they should be, but that's probably not your fault :(

Signature · 4 lines
Paulo Marques - www.grupopie.com

"A journey of a thousand miles begins with a single step."
Lao-tzu, The Way of Lao-tzu
#24 lore raw
In reply to Jan Kasprzak
Quoted 8 lines

The SDLA_CLEAR ioctl (the sdla_clear(dev) function) tries
to clear exactly 65536 bytes (hardcoded at sdla.c:sdla_clear() line 140). So the mem.len should be <= 65536 bytes, and even mem.addr + mem.len should be <= 65536 bytes.

So I propose the following patch (maybe the constant 65536 should
be defined in sdla.h and used both in sdla_xfer() and sdla_clear()):

I'd propose they are set to CAP_SYS_RAWIO for those specific debugging/firmware load operations. They allow access to that level potentially anyway. That solves the problem cleanly.

maintainer
#25 lore raw

PATCH 1/1 fs/coda Re: [Coverity] Untrusted user data in kernel

In reply to Marcelo Tosatti

This patch adds bounds checking for tainted scalars. (reported by Brian Fulton and Ted Unangst, Coverity Inc.)

include/linux/coda.h +2 -2
Index: linux-2.4.29-pre3-bk4/include/linux/coda.h
===================================================================
@@ -767,8 +767,8 @@
 #define PIOCPARM_MASK 0x0000ffff
 struct ViceIoctl {
         caddr_t in, out;        /* Data to be transferred in, or out */
-        short in_size;          /* Size of input buffer <= 2K */
-        short out_size;         /* Maximum size of output buffer, <= 2K */
+        u_short in_size;        /* Size of input buffer <= 2K */
+        u_short out_size;       /* Maximum size of output buffer, <= 2K */
 };
 
 struct PioctlData {
fs/coda/upcall.c +25 -16

57 lines not on this page · raw message

maintainer
#26 lore raw

PATCH 1/1 fs/coda Re: [Coverity] Untrusted user data in kernel

In reply to Marcelo Tosatti

This patch adds bounds checks for tainted scalars (reported by Brian Fulton and Ted Unangst, Coverity Inc.).

include/linux/coda.h +2 -2
Index: linux-2.6.10-mm2/include/linux/coda.h
===================================================================
@@ -761,8 +761,8 @@
 struct ViceIoctl {
         void __user *in;        /* Data to be transferred in */
         void __user *out;       /* Data to be transferred out */
-        short in_size;          /* Size of input buffer <= 2K */
-        short out_size;         /* Maximum size of output buffer, <= 2K */
+        u_short in_size;        /* Size of input buffer <= 2K */
+        u_short out_size;       /* Maximum size of output buffer, <= 2K */
 };
 
 struct PioctlData {
fs/coda/upcall.c +19 -7
Index: linux-2.6.10-mm2/fs/coda/upcall.c
===================================================================
@@ -555,6 +555,11 @@
 		goto exit;
         }
 
+        if (data->vi.out_size > VC_MAXDATASIZE) {
+		error = -EINVAL;
+		goto exit;
+	}
+
         inp->coda_ioctl.VFid = *fid;
     
         /* the cmd field was mutated by increasing its size field to
@@ -583,19 +588,26 @@
 		       error, coda_f2s(fid));
 		goto exit; 
 	}
+
+	if (outsize < (long)outp->coda_ioctl.data + outp->coda_ioctl.len) {
+		error = -EINVAL;
+		goto exit;
+	}
         
 	/* Copy out the OUT buffer. */
         if (outp->coda_ioctl.len > data->vi.out_size) {
 		error = -EINVAL;
-        } else {
-		if (copy_to_user(data->vi.out, 
-				 (char *)outp + (long)outp->coda_ioctl.data, 
-				 data->vi.out_size)) {
-			error = -EFAULT;
-			goto exit;
-		}
+		goto exit;
         }
 
+	/* Copy out the OUT buffer. */
+	if (copy_to_user(data->vi.out, 
+			 (char *)outp + (long)outp->coda_ioctl.data, 
+			 outp->coda_ioctl.len)) {
+		error = -EFAULT;
+		goto exit;
+	}
+
  exit:
 	CODA_FREE(inp, insize);
 	return error;