Message ID | 20200726030855.q6dfjekazfzl5usw@pesu.pes.edu |
---|---|
State | Rejected |
Delegated to: | David Miller |
Headers | show |
Series | [v2] net: ipv6: fix use-after-free Read in __xfrm6_tunnel_spi_lookup | expand |
On Sat, Jul 25, 2020 at 8:09 PM B K Karthik <bkkarthik@pesu.pes.edu> wrote: > @@ -103,10 +103,10 @@ static int __xfrm6_tunnel_spi_check(struct net *net, u32 spi) > { > struct xfrm6_tunnel_net *xfrm6_tn = xfrm6_tunnel_pernet(net); > struct xfrm6_tunnel_spi *x6spi; > - int index = xfrm6_tunnel_spi_hash_byspi(spi); > + int index = xfrm6_tunnel_spi_hash_byaddr((const xfrm_address_t *)spi); > > hlist_for_each_entry(x6spi, > - &xfrm6_tn->spi_byspi[index], > + &xfrm6_tn->spi_byaddr[index], > list_byspi) { > if (x6spi->spi == spi) How did you convince yourself this is correct? This lookup is still using spi. :) More importantly, can you explain how UAF happens? Apparently the syzbot stack traces you quote make no sense at all. I also looked at other similar reports, none of them makes sense to me. Thanks.
On Sun, Jul 26, 2020 at 11:05 AM Cong Wang <xiyou.wangcong@gmail.com> wrote: > > On Sat, Jul 25, 2020 at 8:09 PM B K Karthik <bkkarthik@pesu.pes.edu> wrote: > > @@ -103,10 +103,10 @@ static int __xfrm6_tunnel_spi_check(struct net *net, u32 spi) > > { > > struct xfrm6_tunnel_net *xfrm6_tn = xfrm6_tunnel_pernet(net); > > struct xfrm6_tunnel_spi *x6spi; > > - int index = xfrm6_tunnel_spi_hash_byspi(spi); > > + int index = xfrm6_tunnel_spi_hash_byaddr((const xfrm_address_t *)spi); > > > > hlist_for_each_entry(x6spi, > > - &xfrm6_tn->spi_byspi[index], > > + &xfrm6_tn->spi_byaddr[index], > > list_byspi) { > > if (x6spi->spi == spi) > > How did you convince yourself this is correct? This lookup is still > using spi. :) I'm sorry, but my intention behind writing this patch was not to fix the UAF, but to fix a slab-out-of-bound. If required, I can definitely change the subject line and resend the patch, but I figured this was correct for https://syzkaller.appspot.com/bug?id=058d05f470583ab2843b1d6785fa8d0658ef66ae . since that particular report did not have a reproducer, Dmitry Vyukov <dvyukov@google.com> suggested that I test this patch on other reports for xfrm/spi . Forgive me if this was the wrong way to send a patch for that particular report, but I guessed since the reproducer did not trigger the crash for UAF, I would leave the subject line as 'fix UAF' :) xfrm6_spi_hash_by_hash seemed more convincing because I had to prevent a slab-out-of-bounds because it uses ipv6_addr_hash. It would be of great help if you could help me understand how this was able to fix a UAF. > > More importantly, can you explain how UAF happens? Apparently > the syzbot stack traces you quote make no sense at all. I also > looked at other similar reports, none of them makes sense to me. Forgive me, but I do not understand what you mean by the stack traces (this or other similar reports) "make no sense". I apologise if this message was hurtful / disrespectful in any manner. thanks, karthik
Hi K, Thank you for the patch! Perhaps something to improve: [auto build test WARNING on ipsec/master] [also build test WARNING on ipsec-next/master sparc-next/master net-next/master net/master v5.8-rc6 next-20200724] [If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in https://git-scm.com/docs/git-format-patch] url: https://github.com/0day-ci/linux/commits/B-K-Karthik/net-ipv6-fix-use-after-free-Read-in-__xfrm6_tunnel_spi_lookup/20200726-111019 base: https://git.kernel.org/pub/scm/linux/kernel/git/klassert/ipsec.git master config: alpha-allyesconfig (attached as .config) compiler: alpha-linux-gcc (GCC) 9.3.0 reproduce (this is a W=1 build): wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross chmod +x ~/bin/make.cross # save the attached .config to linux build tree COMPILER_INSTALL_PATH=$HOME/0day COMPILER=gcc-9.3.0 make.cross ARCH=alpha If you fix the issue, kindly add following tag as appropriate Reported-by: kernel test robot <lkp@intel.com> All warnings (new ones prefixed by >>): net/ipv6/xfrm6_tunnel.c: In function '__xfrm6_tunnel_spi_check': >> net/ipv6/xfrm6_tunnel.c:106:43: warning: cast to pointer from integer of different size [-Wint-to-pointer-cast] 106 | int index = xfrm6_tunnel_spi_hash_byaddr((const xfrm_address_t *)spi); | ^ vim +106 net/ipv6/xfrm6_tunnel.c 101 102 static int __xfrm6_tunnel_spi_check(struct net *net, u32 spi) 103 { 104 struct xfrm6_tunnel_net *xfrm6_tn = xfrm6_tunnel_pernet(net); 105 struct xfrm6_tunnel_spi *x6spi; > 106 int index = xfrm6_tunnel_spi_hash_byaddr((const xfrm_address_t *)spi); 107 108 hlist_for_each_entry(x6spi, 109 &xfrm6_tn->spi_byaddr[index], 110 list_byspi) { 111 if (x6spi->spi == spi) 112 return -1; 113 } 114 return index; 115 } 116 --- 0-DAY CI Kernel Test Service, Intel Corporation https://lists.01.org/hyperkitty/list/kbuild-all@lists.01.org
Hi K, Thank you for the patch! Perhaps something to improve: [auto build test WARNING on ipsec/master] [also build test WARNING on ipsec-next/master sparc-next/master net-next/master net/master v5.8-rc6 next-20200724] [If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in https://git-scm.com/docs/git-format-patch] url: https://github.com/0day-ci/linux/commits/B-K-Karthik/net-ipv6-fix-use-after-free-Read-in-__xfrm6_tunnel_spi_lookup/20200726-111019 base: https://git.kernel.org/pub/scm/linux/kernel/git/klassert/ipsec.git master config: x86_64-randconfig-a001-20200726 (attached as .config) compiler: clang version 12.0.0 (https://github.com/llvm/llvm-project 8bf4c1f4fb257774f66c8cda07adc6c5e8668326) reproduce (this is a W=1 build): wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross chmod +x ~/bin/make.cross # install x86_64 cross compiling tool for clang build # apt-get install binutils-x86-64-linux-gnu # save the attached .config to linux build tree COMPILER_INSTALL_PATH=$HOME/0day COMPILER=clang make.cross ARCH=x86_64 If you fix the issue, kindly add following tag as appropriate Reported-by: kernel test robot <lkp@intel.com> All warnings (new ones prefixed by >>): >> net/ipv6/xfrm6_tunnel.c:106:43: warning: cast to 'const xfrm_address_t *' from smaller integer type 'u32' (aka 'unsigned int') [-Wint-to-pointer-cast] int index = xfrm6_tunnel_spi_hash_byaddr((const xfrm_address_t *)spi); ^~~~~~~~~~~~~~~~~~~~~~~~~~~ net/ipv6/xfrm6_tunnel.c:69:28: warning: unused function 'xfrm6_tunnel_spi_hash_byspi' [-Wunused-function] static inline unsigned int xfrm6_tunnel_spi_hash_byspi(u32 spi) ^ 2 warnings generated. vim +106 net/ipv6/xfrm6_tunnel.c 101 102 static int __xfrm6_tunnel_spi_check(struct net *net, u32 spi) 103 { 104 struct xfrm6_tunnel_net *xfrm6_tn = xfrm6_tunnel_pernet(net); 105 struct xfrm6_tunnel_spi *x6spi; > 106 int index = xfrm6_tunnel_spi_hash_byaddr((const xfrm_address_t *)spi); 107 108 hlist_for_each_entry(x6spi, 109 &xfrm6_tn->spi_byaddr[index], 110 list_byspi) { 111 if (x6spi->spi == spi) 112 return -1; 113 } 114 return index; 115 } 116 --- 0-DAY CI Kernel Test Service, Intel Corporation https://lists.01.org/hyperkitty/list/kbuild-all@lists.01.org
On Sat, Jul 25, 2020 at 11:12 PM B K Karthik <bkkarthik@pesu.pes.edu> wrote: > > On Sun, Jul 26, 2020 at 11:05 AM Cong Wang <xiyou.wangcong@gmail.com> wrote: > > > > On Sat, Jul 25, 2020 at 8:09 PM B K Karthik <bkkarthik@pesu.pes.edu> wrote: > > > @@ -103,10 +103,10 @@ static int __xfrm6_tunnel_spi_check(struct net *net, u32 spi) > > > { > > > struct xfrm6_tunnel_net *xfrm6_tn = xfrm6_tunnel_pernet(net); > > > struct xfrm6_tunnel_spi *x6spi; > > > - int index = xfrm6_tunnel_spi_hash_byspi(spi); > > > + int index = xfrm6_tunnel_spi_hash_byaddr((const xfrm_address_t *)spi); > > > > > > hlist_for_each_entry(x6spi, > > > - &xfrm6_tn->spi_byspi[index], > > > + &xfrm6_tn->spi_byaddr[index], > > > list_byspi) { > > > if (x6spi->spi == spi) > > > > How did you convince yourself this is correct? This lookup is still > > using spi. :) > > I'm sorry, but my intention behind writing this patch was not to fix > the UAF, but to fix a slab-out-of-bound. Odd, your $subject is clearly UAF, so is the stack trace in your changelog. :) > If required, I can definitely change the subject line and resend the > patch, but I figured this was correct for > https://syzkaller.appspot.com/bug?id=058d05f470583ab2843b1d6785fa8d0658ef66ae > . since that particular report did not have a reproducer, > Dmitry Vyukov <dvyukov@google.com> suggested that I test this patch on > other reports for xfrm/spi . You have to change it to avoid misleading. > > Forgive me if this was the wrong way to send a patch for that > particular report, but I guessed since the reproducer did not trigger > the crash > for UAF, I would leave the subject line as 'fix UAF' :) > > xfrm6_spi_hash_by_hash seemed more convincing because I had to prevent > a slab-out-of-bounds because it uses ipv6_addr_hash. > It would be of great help if you could help me understand how this was > able to fix a UAF. Sure, you just avoid a pointer deref, which of course can fix the UAF, but I still don't think it is correct in any aspect. Even if it is a OOB, you still have to explain why it happened. Once again, I can't see how it could happen either. > > > > > More importantly, can you explain how UAF happens? Apparently > > the syzbot stack traces you quote make no sense at all. I also > > looked at other similar reports, none of them makes sense to me. > > Forgive me, but I do not understand what you mean by the stack traces > (this or other similar reports) "make no sense". Because the stack trace in your changelog clearly shows it is allocated in tomoyo_init_log(), which is a buffer in struct tomoyo_query, but none of xfrm paths uses it. Or do you see anything otherwise? Thanks.
On Mon, Jul 27, 2020 at 1:37 AM Cong Wang <xiyou.wangcong@gmail.com> wrote: > > On Sat, Jul 25, 2020 at 11:12 PM B K Karthik <bkkarthik@pesu.pes.edu> wrote: > > > > On Sun, Jul 26, 2020 at 11:05 AM Cong Wang <xiyou.wangcong@gmail.com> wrote: > > > > > > On Sat, Jul 25, 2020 at 8:09 PM B K Karthik <bkkarthik@pesu.pes.edu> wrote: > > > > @@ -103,10 +103,10 @@ static int __xfrm6_tunnel_spi_check(struct net *net, u32 spi) > > > > { > > > > struct xfrm6_tunnel_net *xfrm6_tn = xfrm6_tunnel_pernet(net); > > > > struct xfrm6_tunnel_spi *x6spi; > > > > - int index = xfrm6_tunnel_spi_hash_byspi(spi); > > > > + int index = xfrm6_tunnel_spi_hash_byaddr((const xfrm_address_t *)spi); > > > > > > > > hlist_for_each_entry(x6spi, > > > > - &xfrm6_tn->spi_byspi[index], > > > > + &xfrm6_tn->spi_byaddr[index], > > > > list_byspi) { > > > > if (x6spi->spi == spi) > > > > > > How did you convince yourself this is correct? This lookup is still > > > using spi. :) > > > > I'm sorry, but my intention behind writing this patch was not to fix > > the UAF, but to fix a slab-out-of-bound. > > Odd, your $subject is clearly UAF, so is the stack trace in your changelog. > :) > > > > If required, I can definitely change the subject line and resend the > > patch, but I figured this was correct for > > https://syzkaller.appspot.com/bug?id=058d05f470583ab2843b1d6785fa8d0658ef66ae > > . since that particular report did not have a reproducer, > > Dmitry Vyukov <dvyukov@google.com> suggested that I test this patch on > > other reports for xfrm/spi . > > You have to change it to avoid misleading. I will do that once somebody tells me this patch is reasonable to avoid wasting people's time. > > > > > Forgive me if this was the wrong way to send a patch for that > > particular report, but I guessed since the reproducer did not trigger > > the crash > > for UAF, I would leave the subject line as 'fix UAF' :) > > > > xfrm6_spi_hash_by_hash seemed more convincing because I had to prevent > > a slab-out-of-bounds because it uses ipv6_addr_hash. > > It would be of great help if you could help me understand how this was > > able to fix a UAF. > > Sure, you just avoid a pointer deref, which of course can fix the UAF, > but I still don't think it is correct in any aspect. I saw a function call being made to tomoyo_check_acl(). the next thing happening is a kfree(). Also, spi_hash_byspi just returns spi % XFRM6_TUNNEL_SPI_BYSPI_HSIZE . I'm a mentee, hence I would say my knowledge is very limited, please let me know if I am making a horrible mistake somewhere, but return (__force u32)(a->s6_addr32[0] ^ a->s6_addr32[1] ^ a->s6_addr32[2] ^ a->s6_addr32[3]); seems like a better because as David S. Miller <davem@davemloft.net> said "It is doing a XOR on all bits of an IPv6 address, it is doing more bit shifting which the existing hash was ignoring" . Please help me understand this better if I am going wrong. > > Even if it is a OOB, you still have to explain why it happened. Once > again, I can't see how it could happen either. > > > > > > > > > More importantly, can you explain how UAF happens? Apparently > > > the syzbot stack traces you quote make no sense at all. I also > > > looked at other similar reports, none of them makes sense to me. > > > > Forgive me, but I do not understand what you mean by the stack traces > > (this or other similar reports) "make no sense". > > Because the stack trace in your changelog clearly shows it is allocated > in tomoyo_init_log(), which is a buffer in struct tomoyo_query, but > none of xfrm paths uses it. Or do you see anything otherwise? Aren't there indirect inet calls and netfilter hooks? I'm sorry I do not see anything otherwise. Please help me understand. thanks, karthik
On Sat, Jul 25, 2020 at 10:35:12PM -0700, Cong Wang wrote: > On Sat, Jul 25, 2020 at 8:09 PM B K Karthik <bkkarthik@pesu.pes.edu> wrote: > > @@ -103,10 +103,10 @@ static int __xfrm6_tunnel_spi_check(struct net *net, u32 spi) > > { > > struct xfrm6_tunnel_net *xfrm6_tn = xfrm6_tunnel_pernet(net); > > struct xfrm6_tunnel_spi *x6spi; > > - int index = xfrm6_tunnel_spi_hash_byspi(spi); > > + int index = xfrm6_tunnel_spi_hash_byaddr((const xfrm_address_t *)spi); > > > > hlist_for_each_entry(x6spi, > > - &xfrm6_tn->spi_byspi[index], > > + &xfrm6_tn->spi_byaddr[index], > > list_byspi) { > > if (x6spi->spi == spi) > > How did you convince yourself this is correct? This lookup is still > using spi. :) This can not be correct, it takes a u32 spi value and casts it to a pointer to an IP address.
From: B K Karthik <bkkarthik@pesu.pes.edu> Date: Sun, 26 Jul 2020 08:38:55 +0530 > @@ -103,10 +103,10 @@ static int __xfrm6_tunnel_spi_check(struct net *net, u32 spi) > { > struct xfrm6_tunnel_net *xfrm6_tn = xfrm6_tunnel_pernet(net); > struct xfrm6_tunnel_spi *x6spi; > - int index = xfrm6_tunnel_spi_hash_byspi(spi); > + int index = xfrm6_tunnel_spi_hash_byaddr((const xfrm_address_t *)spi); We can cast this a thousand times to make the compiler quiet, but the fact is that this function does not expect an integer SPI as an argument. It expects a protocol address. Please stop forcing this fix, I fear you don't understand how this code works. Come back to us when you do. Thank you.
diff --git a/net/ipv6/xfrm6_tunnel.c b/net/ipv6/xfrm6_tunnel.c index 25b7ebda2fab..a0e18be2145f 100644 --- a/net/ipv6/xfrm6_tunnel.c +++ b/net/ipv6/xfrm6_tunnel.c @@ -103,10 +103,10 @@ static int __xfrm6_tunnel_spi_check(struct net *net, u32 spi) { struct xfrm6_tunnel_net *xfrm6_tn = xfrm6_tunnel_pernet(net); struct xfrm6_tunnel_spi *x6spi; - int index = xfrm6_tunnel_spi_hash_byspi(spi); + int index = xfrm6_tunnel_spi_hash_byaddr((const xfrm_address_t *)spi); hlist_for_each_entry(x6spi, - &xfrm6_tn->spi_byspi[index], + &xfrm6_tn->spi_byaddr[index], list_byspi) { if (x6spi->spi == spi) return -1;