2020-07-25 12:48:44

by B K Karthik

[permalink] [raw]
Subject: [PATCH] net: ipv6: fix slab-out-of-bounda Read in xfrm6_tunnel_alloc_spi

fix slab-out-of-bounds Read in xfrm6_tunnel_alloc_spi
by checking for existance of head for the list spi_byspi

==================================================================
BUG: KASAN: slab-out-of-bounds in __xfrm6_tunnel_alloc_spi net/ipv6/xfrm6_tunnel.c:124 [inline]
BUG: KASAN: slab-out-of-bounds in xfrm6_tunnel_alloc_spi+0x779/0x8a0 net/ipv6/xfrm6_tunnel.c:174
Read of size 4 at addr ffff88809a3fe000 by task syz-executor597/6834
CPU: 1 PID: 6834 Comm: syz-executor597 Not tainted 5.8.0-rc5-next-20200716-syzkaller #0
Hardware name: Google Google Compute Engine/Google Compute Engine, BIOS Google 01/01/2011
Call Trace:
__dump_stack lib/dump_stack.c:77 [inline]
dump_stack+0x18f/0x20d lib/dump_stack.c:118
print_address_description.constprop.0.cold+0xae/0x497 mm/kasan/report.c:383
__kasan_report mm/kasan/report.c:513 [inline]
kasan_report.cold+0x1f/0x37 mm/kasan/report.c:530
__xfrm6_tunnel_alloc_spi net/ipv6/xfrm6_tunnel.c:124 [inline]
xfrm6_tunnel_alloc_spi+0x779/0x8a0 net/ipv6/xfrm6_tunnel.c:174
ipcomp6_tunnel_create net/ipv6/ipcomp6.c:84 [inline]
ipcomp6_tunnel_attach net/ipv6/ipcomp6.c:124 [inline]
ipcomp6_init_state net/ipv6/ipcomp6.c:159 [inline]
ipcomp6_init_state+0x2af/0x700 net/ipv6/ipcomp6.c:139
__xfrm_init_state+0x9a6/0x14b0 net/xfrm/xfrm_state.c:2498
xfrm_init_state+0x1a/0x70 net/xfrm/xfrm_state.c:2525
pfkey_msg2xfrm_state net/key/af_key.c:1291 [inline]
pfkey_add+0x1a10/0x2b70 net/key/af_key.c:1508
pfkey_process+0x66d/0x7a0 net/key/af_key.c:2834
pfkey_sendmsg+0x42d/0x800 net/key/af_key.c:3673
sock_sendmsg_nosec net/socket.c:651 [inline]
sock_sendmsg+0xcf/0x120 net/socket.c:671
____sys_sendmsg+0x331/0x810 net/socket.c:2362
___sys_sendmsg+0xf3/0x170 net/socket.c:2416
__sys_sendmmsg+0x195/0x480 net/socket.c:2506
__do_sys_sendmmsg net/socket.c:2535 [inline]
__se_sys_sendmmsg net/socket.c:2532 [inline]
__x64_sys_sendmmsg+0x99/0x100 net/socket.c:2532
do_syscall_64+0x60/0xe0 arch/x86/entry/common.c:384
entry_SYSCALL_64_after_hwframe+0x44/0xa9
RIP: 0033:0x440409
Code: Bad RIP value.
RSP: 002b:00007ffea3e50018 EFLAGS: 00000246 ORIG_RAX: 0000000000000133
RAX: ffffffffffffffda RBX: 00000000004002c8 RCX: 0000000000440409
RDX: 0400000000000282 RSI: 0000000020000180 RDI: 0000000000000003
RBP: 00000000006ca018 R08: 00000000004002c8 R09: 00000000004002c8
R10: 0000000000000000 R11: 0000000000000246 R12: 0000000000401c10
R13: 0000000000401ca0 R14: 0000000000000000 R15: 0000000000000000
Allocated by task 6731:
kasan_save_stack+0x1b/0x40 mm/kasan/common.c:48
kasan_set_track mm/kasan/common.c:56 [inline]
__kasan_kmalloc.constprop.0+0xbf/0xd0 mm/kasan/common.c:461
slab_post_alloc_hook mm/slab.h:535 [inline]
slab_alloc mm/slab.c:3312 [inline]
kmem_cache_alloc+0x138/0x3a0 mm/slab.c:3482
dup_fd+0x89/0xc90 fs/file.c:293
copy_files kernel/fork.c:1459 [inline]
copy_process+0x1dd0/0x6b70 kernel/fork.c:2064
_do_fork+0xe8/0xb10 kernel/fork.c:2434
__do_sys_clone+0xc8/0x110 kernel/fork.c:2551
do_syscall_64+0x60/0xe0 arch/x86/entry/common.c:384
entry_SYSCALL_64_after_hwframe+0x44/0xa9
The buggy address belongs to the object at ffff88809a3fe0c0
which belongs to the cache files_cache of size 832
The buggy address is located 192 bytes to the left of
832-byte region [ffff88809a3fe0c0, ffff88809a3fe400)
The buggy address belongs to the page:
page:000000007671797d refcount:1 mapcount:0 mapping:0000000000000000 index:0xffff88809a3fec00 pfn:0x9a3fe
flags: 0xfffe0000000200(slab)
raw: 00fffe0000000200 ffffea00027a5248 ffffea0002a3b648 ffff88821bc47600
raw: ffff88809a3fec00 ffff88809a3fe0c0 0000000100000003 0000000000000000
page dumped because: kasan: bad access detected
Memory state around the buggy address:
ffff88809a3fdf00: fc fc fc fc fc fc fc fc fc fc fc fc fc fc fc fc
ffff88809a3fdf80: fc fc fc fc fc fc fc fc fc fc fc fc fc fc fc fc
>ffff88809a3fe000: fc fc fc fc fc fc fc fc fc fc fc fc fc fc fc fc
^
ffff88809a3fe080: fc fc fc fc fc fc fc fc 00 00 00 00 00 00 00 00
ffff88809a3fe100: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
==================================================================

Reported-and-testedby: [email protected]
Signed-off-by: B K Karthik <[email protected]>
---
net/ipv6/xfrm6_tunnel.c | 5 +++--
1 file changed, 3 insertions(+), 2 deletions(-)

diff --git a/net/ipv6/xfrm6_tunnel.c b/net/ipv6/xfrm6_tunnel.c
index 25b7ebda2fab..2d049244be81 100644
--- a/net/ipv6/xfrm6_tunnel.c
+++ b/net/ipv6/xfrm6_tunnel.c
@@ -121,8 +121,9 @@ static u32 __xfrm6_tunnel_alloc_spi(struct net *net, xfrm_address_t *saddr)
struct xfrm6_tunnel_spi *x6spi;
int index;

- if (xfrm6_tn->spi < XFRM6_TUNNEL_SPI_MIN ||
- xfrm6_tn->spi >= XFRM6_TUNNEL_SPI_MAX)
+ if ((xfrm6_tn->spi < XFRM6_TUNNEL_SPI_MIN ||
+ xfrm6_tn->spi >= XFRM6_TUNNEL_SPI_MAX) &&
+ xfrm6_tn->spi_byspi)
xfrm6_tn->spi = XFRM6_TUNNEL_SPI_MIN;
else
xfrm6_tn->spi++;
--
2.20.1


Attachments:
(No filename) (4.91 kB)
signature.asc (673.00 B)
Download all attachments

2020-07-26 10:29:53

by kernel test robot

[permalink] [raw]
Subject: Re: [PATCH] net: ipv6: fix slab-out-of-bounda Read in xfrm6_tunnel_alloc_spi

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-slab-out-of-bounda-Read-in-xfrm6_tunnel_alloc_spi/20200725-204631
base: https://git.kernel.org/pub/scm/linux/kernel/git/klassert/ipsec.git master
config: x86_64-randconfig-r032-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 <[email protected]>

All warnings (new ones prefixed by >>):

>> net/ipv6/xfrm6_tunnel.c:126:13: warning: address of array 'xfrm6_tn->spi_byspi' will always evaluate to 'true' [-Wpointer-bool-conversion]
xfrm6_tn->spi_byspi)
~~~~~~~~~~^~~~~~~~~
1 warning generated.

vim +126 net/ipv6/xfrm6_tunnel.c

116
117 static u32 __xfrm6_tunnel_alloc_spi(struct net *net, xfrm_address_t *saddr)
118 {
119 struct xfrm6_tunnel_net *xfrm6_tn = xfrm6_tunnel_pernet(net);
120 u32 spi;
121 struct xfrm6_tunnel_spi *x6spi;
122 int index;
123
124 if ((xfrm6_tn->spi < XFRM6_TUNNEL_SPI_MIN ||
125 xfrm6_tn->spi >= XFRM6_TUNNEL_SPI_MAX) &&
> 126 xfrm6_tn->spi_byspi)
127 xfrm6_tn->spi = XFRM6_TUNNEL_SPI_MIN;
128 else
129 xfrm6_tn->spi++;
130
131 for (spi = xfrm6_tn->spi; spi <= XFRM6_TUNNEL_SPI_MAX; spi++) {
132 index = __xfrm6_tunnel_spi_check(net, spi);
133 if (index >= 0)
134 goto alloc_spi;
135
136 if (spi == XFRM6_TUNNEL_SPI_MAX)
137 break;
138 }
139 for (spi = XFRM6_TUNNEL_SPI_MIN; spi < xfrm6_tn->spi; spi++) {
140 index = __xfrm6_tunnel_spi_check(net, spi);
141 if (index >= 0)
142 goto alloc_spi;
143 }
144 spi = 0;
145 goto out;
146 alloc_spi:
147 xfrm6_tn->spi = spi;
148 x6spi = kmem_cache_alloc(xfrm6_tunnel_spi_kmem, GFP_ATOMIC);
149 if (!x6spi)
150 goto out;
151
152 memcpy(&x6spi->addr, saddr, sizeof(x6spi->addr));
153 x6spi->spi = spi;
154 refcount_set(&x6spi->refcnt, 1);
155
156 hlist_add_head_rcu(&x6spi->list_byspi, &xfrm6_tn->spi_byspi[index]);
157
158 index = xfrm6_tunnel_spi_hash_byaddr(saddr);
159 hlist_add_head_rcu(&x6spi->list_byaddr, &xfrm6_tn->spi_byaddr[index]);
160 out:
161 return spi;
162 }
163

---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/[email protected]


Attachments:
(No filename) (3.30 kB)
.config.gz (32.31 kB)
Download all attachments

2020-07-29 00:31:55

by David Miller

[permalink] [raw]
Subject: Re: [PATCH] net: ipv6: fix slab-out-of-bounda Read in xfrm6_tunnel_alloc_spi

From: B K Karthik <[email protected]>
Date: Sat, 25 Jul 2020 18:15:53 +0530

> fix slab-out-of-bounds Read in xfrm6_tunnel_alloc_spi
> by checking for existance of head for the list spi_byspi

spi_byspi is an array, therefore as long as xfrm6_tn is not NULL
then spi_byspi will also not be NULL.