Re: [PATCH 3/3] net: cleanly handle kernel vs user buffers for ->msg_control
From: Eric Dumazet
Date: Wed May 13 2020 - 11:42:03 EST
On 5/11/20 4:59 AM, Christoph Hellwig wrote:
> The msg_control field in struct msghdr can either contain a user
> pointer when used with the recvmsg system call, or a kernel pointer
> when used with sendmsg. To complicate things further kernel_recvmsg
> can stuff a kernel pointer in and then use set_fs to make the uaccess
> helpers accept it.
>
> Replace it with a union of a kernel pointer msg_control field, and
> a user pointer msg_control_user one, and allow kernel_recvmsg operate
> on a proper kernel pointer using a bitfield to override the normal
> choice of a user pointer for recvmsg.
>
> Signed-off-by: Christoph Hellwig <hch@xxxxxx>
> ---
> include/linux/socket.h | 12 ++++++++++-
> net/compat.c | 5 +++--
> net/core/scm.c | 49 ++++++++++++++++++++++++------------------
> net/ipv4/ip_sockglue.c | 3 ++-
> net/socket.c | 22 ++++++-------------
> 5 files changed, 50 insertions(+), 41 deletions(-)
>
> diff --git a/include/linux/socket.h b/include/linux/socket.h
> index 4cc64d611cf49..04d2bc97f497d 100644
> --- a/include/linux/socket.h
> +++ b/include/linux/socket.h
> @@ -50,7 +50,17 @@ struct msghdr {
> void *msg_name; /* ptr to socket address structure */
> int msg_namelen; /* size of socket address structure */
> struct iov_iter msg_iter; /* data */
> - void *msg_control; /* ancillary data */
> +
> + /*
> + * Ancillary data. msg_control_user is the user buffer used for the
> + * recv* side when msg_control_is_user is set, msg_control is the kernel
> + * buffer used for all other cases.
> + */
> + union {
> + void *msg_control;
> + void __user *msg_control_user;
> + };
> + bool msg_control_is_user : 1;
Adding a field in this structure seems dangerous.
Some users of 'struct msghdr ' define their own struct on the stack,
and are unaware of this new mandatory field.
This bit contains garbage, crashes are likely to happen ?
Look at IPV6_2292PKTOPTIONS for example.