AUDIT: copy_from_user is a deathtrap.
Linus,
Should I change copy_to/from_user to return -EFAULT, or
introduce a new copy_to/from_uspace which does and start moving everything across?
There are 5,500 uses of copy_to/from_user in 2.5.15. 52 of them use the return value in a way which would be broken by it returning -EFAULT. 51 of those don't need to (mainly cut & paste between serial drivers).
/* Returns amount which wasn't copied before EFAULT. Used by mount. */
static inline unsigned long
gradual_copy_from_user(void *to, const void *from, unsigned long n)
{
unsigned long i;
for (i = 0; i < n; i++, to++, from++) {
if (copy_from_user(from, to, 1) != 0)
break;
}
return n - i;
}There are 415 uses of copy_to/from_user which are wrong, despite an audit 12 months ago by the Stanford checker.
Tired of auditing the same bugs,
Rusty.
--
Anyone who quotes me in their sig is an idiot. -- Rusty Russell.
From: Rusty Russell <[email protected]>
Date: Fri, 17 May 2002 19:27:54 +1000
There are 415 uses of copy_to/from_user which are wrong, despite an audit 12 months ago by the Stanford checker.
I would much rather fix these instances than add yet another interface.
In message <[email protected]> you write:
Quoted 8 lines
From: Rusty Russell <[email protected]>
Date: Fri, 17 May 2002 19:27:54 +1000There are 415 uses of copy_to/from_user which are wrong, despite an audit 12 months ago by the Stanford checker.
I would much rather fix these instances than add yet another interface.
I'll accept that if someone's volunteering to audit the kernel for them every six months.
Sorry I wasn't clear: I'm saying *replace*, not add, Rusty.
--
Anyone who quotes me in their sig is an idiot. -- Rusty Russell.
From: Rusty Russell <[email protected]>
Date: Fri, 17 May 2002 19:49:40 +1000
Sorry I wasn't clear: I'm saying *replace*, not add,
I don't understand what you are proposing then. There are some instances that do want to know how many bytes did make it before the -EFAULT event.
You have to add a new version of the interface to handle both the case that cares about the length copied successfully and the case that only cares about -EFAULT vs. !-EFAULT
In message <[email protected]> you write:
Quoted 8 lines
From: Rusty Russell <[email protected]>
Date: Fri, 17 May 2002 19:49:40 +1000Sorry I wasn't clear: I'm saying *replace*, not add,
I don't understand what you are proposing then. There are some instances that do want to know how many bytes did make it before the
-EFAULTevent.
Yes. There are 52 places which care. Most of these are unneccessary attempts to return eg. number of bytes written in read call before we hit the fault, instead of -EFAULT.
The one case I found which obviously needed it was the mount options code, and I proposed a simple (slow) gradually_copy_from_user for this case:
static inline unsigned long
gradual_copy_from_user(void *to, const void *from, unsigned long n)
{
unsigned long i;
for (i = 0; i < n; i++, to++, from++) {
if (copy_from_user(from, to, 1) != 0)
break;
}
return n - i;
}Here is the list of places in 2.5.15 which actually use the return values other than zero:
./fs/proc/generic.c:108: n -= copy_to_user(buf, start < page ? page : start, n);./fs/hfs/file.c:263: i = copy_from_user(data, buf, count);./fs/hfs/file.c:390: chars -= copy_to_user(buf, p, chars);./fs/hfs/file.c:472: copy_from_user(p, buf, c);./fs/hfs/file_cap.c:162: memcount -= copy_to_user(buf, ((char *)&meta) + pos, memcount);./fs/hfs/file_cap.c:234: mem_count -= copy_from_user(((char *)&meta) + pos, buf, mem_count);./fs/hfs/file_hdr.c:422: left -= copy_to_user(buf, ((char *)&meta) + pos, left);./fs/hfs/file_hdr.c:592: left -= copy_to_user(buf, p + offset, left);./fs/hfs/file_hdr.c:671: left -= copy_from_user(((char *)&meta) + pos, buf, left);./fs/hfs/file_hdr.c:703: left -= copy_from_user(((char *)&meta) + pos, buf, left);./fs/hfs/file_hdr.c:868: left -= copy_from_user(p + offset, buf, left);./fs/namespace.c:670: i = size - copy_from_user((void *)page, data, size);./mm/filemap.c:1189: left = __copy_to_user(desc->buf, kaddr + offset, size);./drivers/char/pty.c:158: n -= copy_from_user(temp_buffer, buf, n);./drivers/char/esp.c:1332: c -= copy_from_user(tmp_buf, buf, c);./drivers/char/serial.c:1876: c -= copy_from_user(tmp_buf, buf, c);./drivers/char/n_tty.c:922: retval = copy_to_user(*b, &tty->read_buf[tty->read_tail], n);./drivers/char/vc_screen.c:251: ret = copy_to_user(buf, con_buf_start, orig_count);./drivers/char/vc_screen.c:325: ret = copy_from_user(con_buf, buf, this_round);./drivers/char/rocket.c:1606: c -= copy_from_user(tmp_buf, buf, c);./drivers/char/rocket.c:1643: c -= copy_from_user(tmp_buf, buf, c);./drivers/char/random.c:1362: i -= copy_to_user(buf, (__u8 const *)tmp, i);./drivers/char/random.c:1589: bytes -= copy_from_user(&buf, p, bytes);./drivers/char/riscom8.c:1247: c -= copy_from_user(tmp_buf, buf, c);./drivers/char/specialix.c:1626: c -= copy_from_user(tmp_buf, buf, c);./drivers/char/serial_amba.c:888: c -= copy_from_user(tmp_buf, buf, c);./drivers/char/dz.c:706: c -= copy_from_user (tmp_buf, buf, c);./drivers/char/mxser.c:931: c -= copy_from_user(mxvar_tmp_buf, buf, c);./drivers/char/generic_serial.c:237: c -= copy_from_user(tmp_buf, buf, c);./drivers/char/serial167.c:1257: c -= copy_from_user(tmp_buf, buf, c);./drivers/char/amiserial.c:958: c -= copy_from_user(tmp_buf, buf, c);./drivers/sbus/char/zs.c:1112: c -= copy_from_user(tmp_buf, buf, c);./drivers/sbus/char/sab82532.c:1127: c -= copy_from_user(tmp_buf, buf, c);./drivers/sbus/char/su.c:1234: c -= copy_from_user(tmp_buf, buf, c);./drivers/sbus/char/aurora.c:1627: c -= copy_from_user(tmp_buf, buf, c);./drivers/video/fbmem.c:386: count -= copy_to_user(buf, base_addr+p, count);./drivers/video/fbmem.c:422: count -= copy_from_user(base_addr+p, buf, count);./drivers/macintosh/macserial.c:1551: c -= copy_from_user(tmp_buf, buf, c);./drivers/s390/char/con3215.c:596: c -= copy_from_user(raw->buffer + raw->head,./drivers/s390/char/tuball.c:329: len2 -= copy_from_user(ob->bc_buf + ob->bc_wr,./arch/i386/kernel/vm86.c:79: tmp = copy_to_user(¤t->thread.vm86_info->regs,regs, VM86_REGS_SIZE1);./arch/i386/kernel/vm86.c:80: tmp += copy_to_user(¤t->thread.vm86_info->regs.VM86_REGS_PART2,./arch/i386/kernel/vm86.c:147: tmp = copy_from_user(&info, v86, VM86_REGS_SIZE1);./arch/i386/kernel/vm86.c:148: tmp += copy_from_user(&info.regs.VM86_REGS_PART2, &v86->regs.VM86_REGS_PART2,./arch/i386/kernel/vm86.c:148: tmp += copy_from_user(&info.regs.VM86_REGS_PART2, &v86->regs.VM86_REGS_PART2,./arch/i386/kernel/vm86.c:195: tmp = copy_from_user(&info, v86, VM86_REGS_SIZE1);./arch/i386/kernel/vm86.c:196: tmp += copy_from_user(&info.regs.VM86_REGS_PART2, &v86->regs.VM86_REGS_PART2,./arch/mips/baget/vacserial.c:1085: c -= copy_from_user(tmp_buf, buf, c);./arch/mips/au1000/common/serial.c:1212: c -= copy_from_user(tmp_buf, buf, c);./arch/ppc/4xx_io/serial_sicc.c:988: c -= copy_from_user(tmp_buf, buf, c);./arch/ia64/hp/sim/simserial.c:328: c -= copy_from_user(tmp_buf, buf, c);./arch/cris/drivers/serial.c:2355: c -= copy_from_user(tmp_buf, buf, c);
--
Anyone who quotes me in their sig is an idiot. -- Rusty Russell.
Quoted 15 lines · Rusty Russell
Quoted 6 lines
Sorry I wasn't clear: I'm saying *replace*, not add,
I don't understand what you are proposing then. There are some instances that do want to know how many bytes did make it before the
-EFAULTevent.Yes. There are 52 places which care. Most of these are unneccessary attempts to return eg. number of bytes written in read call before we hit the fault, instead of
-EFAULT.The one case I found which obviously needed it was the mount options code, and I proposed a simple (slow)
gradually_copy_from_userfor this case:
Quoted 11 lines
static inline unsigned long
gradual_copy_from_user(void *to, const void *from, unsigned long n)
{
unsigned long i;
for (i = 0; i < n; i++, to++, from++) {
if (copy_from_user(from, to, 1) != 0)
break;
}
return n - i;
}It looks simple to my uncomplicated mind to make copy_{to,from}_user() return number of bytes copied. If they != bytes requested, there was -EFAULT.
This can be easily wrapped by inline which returns only 0 on success or -EFAULT on failure.
Then use appropriate version for each case.
--
vda
Quoted 7 lines
Quoted 2 lines
I would much rather fix these instances than add yet another interface.
I'll accept that if someone's volunteering to audit the kernel for them every six months.
Sorry I wasn't clear: I'm saying *replace*, not add,
Replace requires you audit every single use, and then work out how to handle those that do care about the length and the point it faulted. From what I've seen of the stuff that has been fixed we have a mix of the following
1. Misports of ancient verify_* code - eg the serial ones
2. Not checking the return code - 100% legal and standards compliant
I've seen very few that have other screwups. In fact I've seen far more incorrect uses of kmalloc with a user passed input field, kmalloc with maths overflows, copy*user with maths overflows and the like
Alan
In message <[email protected]> you write:
Quoted 10 lines
Quoted 7 lines
Quoted 2 lines
I would much rather fix these instances than add yet another interface.
I'll accept that if someone's volunteering to audit the kernel for them every six months.
Sorry I wasn't clear: I'm saying *replace*, not add,
Replace requires you audit every single use, and then work out how to handle those that do care about the length and the point it faulted.
Read my original post. I have done this.
Quoted 5 lines
From what I've seen of the stuff that has been fixed we have a mix of the following
1. Misports of ancient verify_* code - eg the serial ones
2. Not checking the return code - 100% legal and standards compliant
No, the 400+ are all of form:
/* of course this returns 0 or -EFAULT! */ return copy_from_user(xxx);
Rusty.
--
Anyone who quotes me in their sig is an idiot. -- Rusty Russell.
Quoted 4 lines
No, the 400+ are all of form:
/* of course this returns 0 or -EFAULT! */ return copy_from_user(xxx);
So lets verify and fix them. Post the list to the kenrel janitors
In message <[email protected]> you write:
Quoted 6 lines
Quoted 4 lines
No, the 400+ are all of form:
/* of course this returns 0 or -EFAULT! */ return copy_from_user(xxx);So lets verify and fix them. Post the list to the kenrel janitors
Again, like we did 12 months ago you mean?
We could do that, or, we could fix the actual problem, which is the HUGE FUCKING BEARTRAP WHICH CATCHES EVERY SINGLE NEW PROGRAMMER ON THE WAY THROUGH.
Not fixing earlier was criminal, not fixing it today is insane. Rusty.
--
Anyone who quotes me in their sig is an idiot. -- Rusty Russell.
Quoted 10 lines · Rusty Russell
Quoted 4 lines
Quoted 2 lines
/* of course this returns 0 or -EFAULT! */ return copy_from_user(xxx);So lets verify and fix them. Post the list to the kenrel janitors
Again, like we did 12 months ago you mean?
We could do that, or, we could fix the actual problem, which is the HUGE FUCKING BEARTRAP WHICH CATCHES EVERY SINGLE NEW PROGRAMMER ON THE WAY THROUGH.
Looks like it is waiting for me yet (if I'll ever do something useful for lk).
Quoted 1 line
Not fixing earlier was criminal, not fixing it today is insane.
What's the problem? People don't understand what copy_x_user() returns and how to check return value properly?
Maybe good function name(s) will help?
copy_{from,to}_user_and_count()
Quoted 2 lines
There are 415 uses of copy_to/from_user which are wrong, despite an audit 12 months ago by the Stanford checker.
What are typical errors?
--
vda
Quoted 1 line
Again, like we did 12 months ago you mean?
We didnt fix them 12 months ago
Quoted 3 lines
We could do that, or, we could fix the actual problem, which is the HUGE FUCKING BEARTRAP WHICH CATCHES EVERY SINGLE NEW PROGRAMMER ON THE WAY THROUGH.
Capital letters versus content. I'd prefer content
All the cases I looked at where replications of existing bugs copied from old drivers. That doesn't say copy_*_user is wrong, it says lots of examples people keep using are wrong
In message <[email protected]> you write:
Quoted 5 lines
Quoted 3 lines
We could do that, or, we could fix the actual problem, which is the HUGE FUCKING BEARTRAP WHICH CATCHES EVERY SINGLE NEW PROGRAMMER ON THE WAY THROUGH.
Capital letters versus content. I'd prefer content
1) Returning 0 on success, and -errno on error is a common kernel convention.
2) Following kernel conventions makes it easier for other programmers to use your code.
3) You should only violate kernel conventions when there is a compelling reason.
1a) If you're going to break a convention, do it in a way that
breaks compile, or
1b) If you can't do that, make it reliably break at runtime.
4) The single case which requires this information can be fixed by a simple 10-line wrapper function.
I do not believe this is a compelling reason to violate kernel convention in a way which is almost impossible to notice. I furthur believe that it speaks very poorly about the thought put into kernel interface design.
Quoted 2 lines
All the cases I looked at where replications of existing bugs copied from old drivers.
Try looking at intermezzo, or the s390 and s390x ports. New code, new coders, same trap.
Rusty.
--
Anyone who quotes me in their sig is an idiot. -- Rusty Russell.
Quoted 1 line · Rusty Russell
Again, like we did 12 months ago you mean?
Adding some comments on the interface to uaccess.h might help. It's way more convenient than looking it up in your guide
regards
john
Signature · 3 lines
"It is very difficult to prophesy, especially when it pertains to the future." - Patrick Kurzawe
Quoted 2 lines · Rusty Russell
Sorry I wasn't clear: I'm saying *replace*, not add,
Ok, let _me_ be clear: replacing them with an inferior product that cannot tell you partial copies is not going to happen. Not now, not ever. You would break all the users who _require_ knowing about a read() that only gave you 5 out of 50 bytes.
Linus
Maybe what is needed is a copy_from_user debug mode, like slab poisoning.
john alvord
Quoted 19 lines · Linus Torvalds
Quoted 2 lines · Rusty Russell
Sorry I wasn't clear: I'm saying *replace*, not add,
Ok, let _me_ be clear: replacing them with an inferior product that cannot tell you partial copies is not going to happen. Not now, not ever. You would break all the users who _require_ knowing about a
read()that only gave you 5 out of 50 bytes.Linus
-
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/
Quoted 5 lines · Rusty Russell
Linus,
Should I change copy_to/from_user to return
-EFAULT, or
introduce a new copy_to/from_uspace which does and start moving everything across?
copyin/copyout! :)