diff mbox series

net: tun: set tun->dev->addr_len during TUNSETLINK processing

Message ID 20210405113555.9419-1-phil@philpotter.co.uk (mailing list archive)
State Superseded
Delegated to: Netdev Maintainers
Headers show
Series net: tun: set tun->dev->addr_len during TUNSETLINK processing | expand

Checks

Context Check Description
netdev/cover_letter success Link
netdev/fixes_present success Link
netdev/patch_count success Link
netdev/tree_selection success Guessed tree name to be net-next
netdev/subject_prefix warning Target tree name not specified in the subject
netdev/cc_maintainers success CCed 3 of 3 maintainers
netdev/source_inline fail Was 0 now: 1
netdev/verify_signedoff success Link
netdev/module_param success Was 0 now: 0
netdev/build_32bit success Errors and warnings before: 0 this patch: 0
netdev/kdoc success Errors and warnings before: 0 this patch: 0
netdev/verify_fixes success Link
netdev/checkpatch success total: 0 errors, 0 warnings, 0 checks, 66 lines checked
netdev/build_allmodconfig_warn success Errors and warnings before: 0 this patch: 0
netdev/header_inline success Link

Commit Message

Phillip Potter April 5, 2021, 11:35 a.m. UTC
When changing type with TUNSETLINK ioctl command, set tun->dev->addr_len
to match the appropriate type, using new tun_get_addr_len utility function
which returns appropriate address length for given type. Fixes a
KMSAN-found uninit-value bug reported by syzbot at:
https://syzkaller.appspot.com/bug?id=0766d38c656abeace60621896d705743aeefed51

Reported-by: syzbot+001516d86dbe88862cec@syzkaller.appspotmail.com
Signed-off-by: Phillip Potter <phil@philpotter.co.uk>
---
 drivers/net/tun.c | 48 +++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 48 insertions(+)

Comments

David Miller April 5, 2021, 9:59 p.m. UTC | #1
From: Phillip Potter <phil@philpotter.co.uk>
Date: Mon,  5 Apr 2021 12:35:55 +0100

> When changing type with TUNSETLINK ioctl command, set tun->dev->addr_len
> to match the appropriate type, using new tun_get_addr_len utility function
> which returns appropriate address length for given type. Fixes a
> KMSAN-found uninit-value bug reported by syzbot at:
> https://syzkaller.appspot.com/bug?id=0766d38c656abeace60621896d705743aeefed51
> 
> Reported-by: syzbot+001516d86dbe88862cec@syzkaller.appspotmail.com
> Signed-off-by: Phillip Potter <phil@philpotter.co.uk>
> ---
>  drivers/net/tun.c | 48 +++++++++++++++++++++++++++++++++++++++++++++++
>  1 file changed, 48 insertions(+)
> 
> diff --git a/drivers/net/tun.c b/drivers/net/tun.c
> index 978ac0981d16..56c26339ee3b 100644
> --- a/drivers/net/tun.c
> +++ b/drivers/net/tun.c
> @@ -69,6 +69,14 @@
>  #include <linux/bpf.h>
>  #include <linux/bpf_trace.h>
>  #include <linux/mutex.h>
> +#include <linux/ieee802154.h>
> +#include <linux/if_ltalk.h>
> +#include <uapi/linux/if_fddi.h>
> +#include <uapi/linux/if_hippi.h>
> +#include <uapi/linux/if_fc.h>
> +#include <net/ax25.h>
> +#include <net/rose.h>
> +#include <net/6lowpan.h>
>  
>  #include <linux/uaccess.h>
>  #include <linux/proc_fs.h>
> @@ -2925,6 +2933,45 @@ static int tun_set_ebpf(struct tun_struct *tun, struct tun_prog __rcu **prog_p,
>  	return __tun_set_ebpf(tun, prog_p, prog);
>  }
>  
> +/* Return correct value for tun->dev->addr_len based on tun->dev->type. */
> +static inline unsigned char tun_get_addr_len(unsigned short type)
> +{

Please do not use inline in foo.c files, let the compiler decide.

Thanks.
Phillip Potter April 6, 2021, 10:18 a.m. UTC | #2
On Mon, Apr 05, 2021 at 02:59:21PM -0700, David Miller wrote:
> From: Phillip Potter <phil@philpotter.co.uk>
> Date: Mon,  5 Apr 2021 12:35:55 +0100
> 
> > When changing type with TUNSETLINK ioctl command, set tun->dev->addr_len
> > to match the appropriate type, using new tun_get_addr_len utility function
> > which returns appropriate address length for given type. Fixes a
> > KMSAN-found uninit-value bug reported by syzbot at:
> > https://syzkaller.appspot.com/bug?id=0766d38c656abeace60621896d705743aeefed51
> > 
> > Reported-by: syzbot+001516d86dbe88862cec@syzkaller.appspotmail.com
> > Signed-off-by: Phillip Potter <phil@philpotter.co.uk>
> > ---
> >  drivers/net/tun.c | 48 +++++++++++++++++++++++++++++++++++++++++++++++
> >  1 file changed, 48 insertions(+)
> > 
> > diff --git a/drivers/net/tun.c b/drivers/net/tun.c
> > index 978ac0981d16..56c26339ee3b 100644
> > --- a/drivers/net/tun.c
> > +++ b/drivers/net/tun.c
> > @@ -69,6 +69,14 @@
> >  #include <linux/bpf.h>
> >  #include <linux/bpf_trace.h>
> >  #include <linux/mutex.h>
> > +#include <linux/ieee802154.h>
> > +#include <linux/if_ltalk.h>
> > +#include <uapi/linux/if_fddi.h>
> > +#include <uapi/linux/if_hippi.h>
> > +#include <uapi/linux/if_fc.h>
> > +#include <net/ax25.h>
> > +#include <net/rose.h>
> > +#include <net/6lowpan.h>
> >  
> >  #include <linux/uaccess.h>
> >  #include <linux/proc_fs.h>
> > @@ -2925,6 +2933,45 @@ static int tun_set_ebpf(struct tun_struct *tun, struct tun_prog __rcu **prog_p,
> >  	return __tun_set_ebpf(tun, prog_p, prog);
> >  }
> >  
> > +/* Return correct value for tun->dev->addr_len based on tun->dev->type. */
> > +static inline unsigned char tun_get_addr_len(unsigned short type)
> > +{
> 
> Please do not use inline in foo.c files, let the compiler decide.
> 
> Thanks.

Dear David,

Thank you for the feedback, I will resend.

Regards,
Phil
Eric Dumazet April 6, 2021, 5:26 p.m. UTC | #3
On 4/5/21 1:35 PM, Phillip Potter wrote:
> When changing type with TUNSETLINK ioctl command, set tun->dev->addr_len
> to match the appropriate type, using new tun_get_addr_len utility function
> which returns appropriate address length for given type. Fixes a
> KMSAN-found uninit-value bug reported by syzbot at:
> https://syzkaller.appspot.com/bug?id=0766d38c656abeace60621896d705743aeefed51
> 
> Reported-by: syzbot+001516d86dbe88862cec@syzkaller.appspotmail.com
> Signed-off-by: Phillip Potter <phil@philpotter.co.uk>
> ---

Please give credits to people who helped.

You could have :

Suggested-by: Eric Dumazet <edumazet@google.com>

Or
Diagnosed-by: Eric Dumazet <edumazet@google.com>

Or at least CCed me :/
Phillip Potter April 6, 2021, 5:38 p.m. UTC | #4
On Tue, Apr 06, 2021 at 07:26:29PM +0200, Eric Dumazet wrote:
> 
> 
> On 4/5/21 1:35 PM, Phillip Potter wrote:
> > When changing type with TUNSETLINK ioctl command, set tun->dev->addr_len
> > to match the appropriate type, using new tun_get_addr_len utility function
> > which returns appropriate address length for given type. Fixes a
> > KMSAN-found uninit-value bug reported by syzbot at:
> > https://syzkaller.appspot.com/bug?id=0766d38c656abeace60621896d705743aeefed51
> > 
> > Reported-by: syzbot+001516d86dbe88862cec@syzkaller.appspotmail.com
> > Signed-off-by: Phillip Potter <phil@philpotter.co.uk>
> > ---
> 
> Please give credits to people who helped.
> 
> You could have :
> 
> Suggested-by: Eric Dumazet <edumazet@google.com>
> 
> Or
> Diagnosed-by: Eric Dumazet <edumazet@google.com>
> 
> Or at least CCed me :/
> 

Dear Eric,

Please accept my apology for this oversight. It certainly wasn't
intentional on my part, and entirely down to inexperience. I will send
the patch again with Diagnosed-by.

Regards,
Phil
diff mbox series

Patch

diff --git a/drivers/net/tun.c b/drivers/net/tun.c
index 978ac0981d16..56c26339ee3b 100644
--- a/drivers/net/tun.c
+++ b/drivers/net/tun.c
@@ -69,6 +69,14 @@ 
 #include <linux/bpf.h>
 #include <linux/bpf_trace.h>
 #include <linux/mutex.h>
+#include <linux/ieee802154.h>
+#include <linux/if_ltalk.h>
+#include <uapi/linux/if_fddi.h>
+#include <uapi/linux/if_hippi.h>
+#include <uapi/linux/if_fc.h>
+#include <net/ax25.h>
+#include <net/rose.h>
+#include <net/6lowpan.h>
 
 #include <linux/uaccess.h>
 #include <linux/proc_fs.h>
@@ -2925,6 +2933,45 @@  static int tun_set_ebpf(struct tun_struct *tun, struct tun_prog __rcu **prog_p,
 	return __tun_set_ebpf(tun, prog_p, prog);
 }
 
+/* Return correct value for tun->dev->addr_len based on tun->dev->type. */
+static inline unsigned char tun_get_addr_len(unsigned short type)
+{
+	switch (type) {
+	case ARPHRD_IP6GRE:
+	case ARPHRD_TUNNEL6:
+		return sizeof(struct in6_addr);
+	case ARPHRD_IPGRE:
+	case ARPHRD_TUNNEL:
+	case ARPHRD_SIT:
+		return 4;
+	case ARPHRD_ETHER:
+		return ETH_ALEN;
+	case ARPHRD_IEEE802154:
+	case ARPHRD_IEEE802154_MONITOR:
+		return IEEE802154_EXTENDED_ADDR_LEN;
+	case ARPHRD_PHONET_PIPE:
+	case ARPHRD_PPP:
+	case ARPHRD_NONE:
+		return 0;
+	case ARPHRD_6LOWPAN:
+		return EUI64_ADDR_LEN;
+	case ARPHRD_FDDI:
+		return FDDI_K_ALEN;
+	case ARPHRD_HIPPI:
+		return HIPPI_ALEN;
+	case ARPHRD_IEEE802:
+		return FC_ALEN;
+	case ARPHRD_ROSE:
+		return ROSE_ADDR_LEN;
+	case ARPHRD_NETROM:
+		return AX25_ADDR_LEN;
+	case ARPHRD_LOCALTLK:
+		return LTALK_ALEN;
+	default:
+		return 0;
+	}
+}
+
 static long __tun_chr_ioctl(struct file *file, unsigned int cmd,
 			    unsigned long arg, int ifreq_len)
 {
@@ -3088,6 +3135,7 @@  static long __tun_chr_ioctl(struct file *file, unsigned int cmd,
 				break;
 			}
 			tun->dev->type = (int) arg;
+			tun->dev->addr_len = tun_get_addr_len(tun->dev->type);
 			netif_info(tun, drv, tun->dev, "linktype set to %d\n",
 				   tun->dev->type);
 			call_netdevice_notifiers(NETDEV_POST_TYPE_CHANGE,