user/kernel bug and memory leak in i2c-dev
Hi Greg,
As you may have seen on LKML (and this list) we have a patch ready to fix the two problems mentioned in topic. I will submit it for inclusion into Linux 2.4 tomorrow. I think I need to CC: Marcelo for it to work, right?
I checked wether the fix should be applied somewhere else. It doesn't apply to Linux 2.2 (there were no I2C subsystem there) but it *does* apply to 2.6.0-test2. The proposed patch won't apply cleanly because the code was reindented, but it *has* to be applied though. What do you want me to do? Prepare and submit a patch to the LKML and CC: Linus, as I plan to do for 2.4? Prepare the patch and send it to you? Leave you handle the problem alone?
See you,
Signature · 2 lines
Jean Delvare http://www.ensicaen.ismra.fr/~delvare/
Quoted 7 lines · Jean Delvare
Hi Greg,
As you may have seen on LKML (and this list) we have a patch ready to fix the two problems mentioned in topic. I will submit it for inclusion into Linux 2.4 tomorrow. I think I need to CC: Marcelo for it to work, right?
Yes. I'd be glad to send it to him if you want me to (with your attribution of course.)
I'm still looking at the patch you submitted, as it still seems strange
to me why it's needed... I'll write back to that in a bit.
Quoted 7 lines
I checked wether the fix should be applied somewhere else. It doesn't apply to Linux 2.2 (there were no I2C subsystem there) but it *does* apply to 2.6.0-test2. The proposed patch won't apply cleanly because the code was reindented, but it *has* to be applied though. What do you want me to do? Prepare and submit a patch to the LKML and CC: Linus, as I plan to do for 2.4? Prepare the patch and send it to you? Leave you handle the problem alone?
Hey, I don't want to handle anything alone here :)
If you send me a patch, I'll be glad to forward it on to Linus by putting it into a bk tree for him to pull from.
More in response to the patch...
Sorry for the delay, "Real Life" (tm) issues been keeping me busy...
thanks,
greg k-h
Quoted 8 lines
Quoted 5 lines
As you may have seen on LKML (and this list) we have a patch ready to fix the two problems mentioned in topic. I will submit it for inclusion into Linux 2.4 tomorrow. I think I need to CC: Marcelo for it to work, right?
Yes. I'd be glad to send it to him if you want me to (with your attribution of course.)
I think I'll do it for 2.4. The i2c subsystem in 2.4 is in sync with our CVS (well, it *should* be) so it's "legitimate" (or say logical) that the people maintaining i2c-CVS submit fixes for inclusion into the 2.4 kernel.
Quoted 2 lines
I'm still looking at the patch you submitted, as it still seems
strange to me why it's needed... I'll write back to that in a bit.
I had a really hard time figuring this out too. This kernel memory vs. user memory thing is tricky, to say the least. Sergey's post helped me a lot understanding how it works:
http://archives.andrew.net.au/lm-sensors/msg03874.html
And even now I'm not sure I understood it all, but at least I *accepted* the idea the patch was good ;) I think it's all in the fact that user-space memory areas can be unmapped at "any" time, and that they are mapped again by read_{from,to}_user, but it only works on direct kernel-space-pointer-to-user-space-memory access, not on more complex accesses such as the one we had. All these obviously rely on some obscure (to me at least) kernel magic, and if Robert and Sergey agree a fix is needed, I have to believe them.
BTW, I find Sergey's fix much clearer than Robert's one.
And the memory leak fix and the various optimizations are unarguably great.
Quoted 8 lines
Quoted 7 lines
I checked wether the fix should be applied somewhere else. It doesn't apply to Linux 2.2 (there were no I2C subsystem there) but it *does* apply to 2.6.0-test2. The proposed patch won't apply cleanly because the code was reindented, but it *has* to be applied though. What do you want me to do? Prepare and submit a patch to the LKML and CC: Linus, as I plan to do for 2.4? Prepare the patch and send it to you? Leave you handle the problem alone?
Quoted 4 lines
Hey, I don't want to handle anything alone here :)
If you send me a patch, I'll be glad to forward it on to Linus by putting it into a bk tree for him to pull from.
OK, will do so.
(working)
Here it is (warning: not even tested, but it's the same as 2.4's one, which I tested).
Since you're our interface guy between i2c-CVS development and the 2.6 kernel, I think it's far better that you submit the patch to Linus. It's probably easier for him if he has to deal with a single person representing our group.
One note though: When I say this patch is needed for 2.6, I just mean "the code in 2.6 is the same as the code in 2.4". Maybe the kernel magic has changed and copy_{from,to}_user is better than before and can handle this, how would I know. Still the optimizations, at least, will be welcome, and the rest can't hurt.
Quoted 1 line
Sorry for the delay, "Real Life" (tm) issues been keeping me busy...
Don't be sorry, sometimes it's great to realize we still have a real life :)
I just wanted to make sure you were OK with the patch (or at least were aware of it) before I send it to Marcelo.
Signature · 2 lines
Jean Delvare http://www.ensicaen.ismra.fr/~delvare/
Quoted 6 lines · Jean Delvare
Quoted 6 lines
Quoted 5 lines
As you may have seen on LKML (and this list) we have a patch ready to fix the two problems mentioned in topic. I will submit it for inclusion into Linux 2.4 tomorrow. I think I need to CC: Marcelo for it to work, right?
Quoted 7 lines
Quoted 2 lines
Yes. I'd be glad to send it to him if you want me to (with your attribution of course.)
I think I'll do it for 2.4. The i2c subsystem in 2.4 is in sync with our CVS (well, it *should* be) so it's "legitimate" (or say logical) that the people maintaining i2c-CVS submit fixes for inclusion into the 2.4 kernel.
Thanks, Marcelo said he applied your patch, so it will show up in the next release.
Quoted 4 lines
OK, will do so.
(working)
Here it is (warning: not even tested, but it's the same as 2.4's one, which I tested).
-ENOPATCH :)
thanks,
greg k-h
Quoted 3 lines
Thanks, Marcelo said he applied your patch, so it will show up in the next release.
Great.
Quoted 1 line
-ENOPATCH:)
-EOVERTEMP ;]
Here it is, sorry.
Signature · 101 lines
Jean Delvare
http://www.ensicaen.ismra.fr/~delvare/
-------------- next part --------------
diff -ru 2.6/drivers/i2c/i2c-dev.c 2.6/drivers/i2c/i2c-dev.c
--- 2.6/drivers/i2c/i2c-dev.c Sat Aug 2 10:53:19 2003
+++ 2.6/drivers/i2c/i2c-dev.c Wed Aug 6 09:26:55 2003
@@ -181,6 +181,7 @@
struct i2c_smbus_ioctl_data data_arg;
union i2c_smbus_data temp;
struct i2c_msg *rdwr_pa;
+ u8 **data_ptrs;
int i,datasize,res;
unsigned long funcs;
@@ -214,7 +215,7 @@
return (copy_to_user((unsigned long __user *)arg, &funcs,
sizeof(unsigned long)))?-EFAULT:0;
- case I2C_RDWR:
+ case I2C_RDWR:
if (copy_from_user(&rdwr_arg,
(struct i2c_rdwr_ioctl_data __user *)arg,
sizeof(rdwr_arg)))
@@ -231,28 +232,37 @@
if (rdwr_pa = NULL) return -ENOMEM;
+ if (copy_from_user(rdwr_pa, rdwr_arg.msgs,
+ rdwr_arg.nmsgs * sizeof(struct i2c_msg))) {
+ kfree(rdwr_pa);
+ return -EFAULT;
+ }
+
+ data_ptrs = (u8 **) kmalloc(rdwr_arg.nmsgs * sizeof(u8 *),
+ GFP_KERNEL);
+ if (data_ptrs = NULL) {
+ kfree(rdwr_pa);
+ return -ENOMEM;
+ }
+
res = 0;
for( i=0; i<rdwr_arg.nmsgs; i++ ) {
- if(copy_from_user(&(rdwr_pa[i]),
- &(rdwr_arg.msgs[i]),
- sizeof(rdwr_pa[i]))) {
- res = -EFAULT;
- break;
- }
/* Limit the size of the message to a sane amount */
if (rdwr_pa[i].len > 8192) {
res = -EINVAL;
break;
}
+ data_ptrs[i] = rdwr_pa[i].buf;
rdwr_pa[i].buf = kmalloc(rdwr_pa[i].len, GFP_KERNEL);
if(rdwr_pa[i].buf = NULL) {
res = -ENOMEM;
break;
}
if(copy_from_user(rdwr_pa[i].buf,
- rdwr_arg.msgs[i].buf,
+ data_ptrs[i],
rdwr_pa[i].len)) {
- res = -EFAULT;
+ ++i; /* Needs to be kfreed too */
+ res = -EFAULT;
break;
}
}
@@ -260,18 +270,18 @@
int j;
for (j = 0; j < i; ++j)
kfree(rdwr_pa[j].buf);
+ kfree(data_ptrs);
kfree(rdwr_pa);
return res;
}
- if (!res) {
- res = i2c_transfer(client->adapter,
- rdwr_pa,
- rdwr_arg.nmsgs);
- }
+
+ res = i2c_transfer(client->adapter,
+ rdwr_pa,
+ rdwr_arg.nmsgs);
while(i-- > 0) {
if( res>=0 && (rdwr_pa[i].flags & I2C_M_RD)) {
if(copy_to_user(
- rdwr_arg.msgs[i].buf,
+ data_ptrs[i],
rdwr_pa[i].buf,
rdwr_pa[i].len)) {
res = -EFAULT;
@@ -279,6 +289,7 @@
}
kfree(rdwr_pa[i].buf);
}
+ kfree(data_ptrs);
kfree(rdwr_pa);
return res;Greg KH
AppliedQuoted 11 lines · Jean Delvare
Quoted 3 lines
Thanks, Marcelo said he applied your patch, so it will show up in the next release.
Great.
Quoted 1 line
-ENOPATCH:)
-EOVERTEMP;]Here it is, sorry.
Applied, thanks.
greg k-h