Le 14/12/2013 19:18, Ted Unangst a écrit :
> On Sat, Dec 14, 2013 at 19:06, Maxime Villard wrote:
>> Hi,
>> I was reading linux_socket.c when I came across a bug in
>> linux_sendmsg().
>
>> At l.1252, if control == NULL, the function jumps to 'done' and 'level'
>> is checked while it hasn't been initialized. As 'control' is NULL,
>> copyout() tries to write to NULL->cmsg_level.
>
> Agree with the patch. Can you do one other change? Change
> &control->level to control + offsetof(level). (Maybe with a second
> variable.) I think that would make it more clear that we're not
> dereferencing the pointer in the kernel and would reduce the number of
> casts.
>
ah, something like that? (not tested)
Index: linux_socket.c
===================================================================
RCS file: /cvs/src/sys/compat/linux/linux_socket.c,v
retrieving revision 1.46
diff -u -r1.46 linux_socket.c
--- linux_socket.c 26 Jun 2012 10:18:08 -0000 1.46
+++ linux_socket.c 14 Dec 2013 19:30:03 -0000
@@ -1214,7 +1214,8 @@
struct msghdr msg, *nmsg = NULL;
int error;
caddr_t control;
- int level;
+ int level = -1;
+ void *ulevel;
if ((error = copyin((caddr_t) uap, (caddr_t) &lla, sizeof lla)))
return error;
@@ -1251,8 +1252,8 @@
return error;
if (control == NULL)
goto done;
- error = copyin(&((struct cmsghdr *)control)->cmsg_level,
- &level, sizeof(int));
+ ulevel = control + offsetof(struct cmsghdr, cmsg_level);
+ error = copyin(ulevel, &level, sizeof(int));
if (error)
return error;
if (level == 1) {
@@ -1266,8 +1267,7 @@
* We don't because the control header is variable length
* up to 2048 bytes, and there's only 512 bytes of gap.
*/
- error = copyout(&level, &((struct cmsghdr *)control)->
- cmsg_level, sizeof(int));
+ error = copyout(&level, ulevel, sizeof(int));
if (error)
return error;
}
@@ -1277,8 +1277,7 @@
if (level == SOL_SOCKET) {
level = 1;
/* don't worry about the error */
- copyout(&level, &((struct cmsghdr *)control)->cmsg_level,
- sizeof(int));
+ copyout(&level, ulevel, sizeof(int));
}
return (error);
}