Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

can: af_can: can_pernet_init(): add missing error handling for kzalloc returning NULL

This patch adds the missing check and error handling for out-of-memory
situations, when kzalloc cannot allocate memory.

Fixes: cb5635a36776 ("can: complete initial namespace support")
Acked-by: Oliver Hartkopp <socketcan@hartkopp.net>
Cc: linux-stable <stable@vger.kernel.org>
Signed-off-by: Marc Kleine-Budde <mkl@pengutronix.de>

+13 -1
+13 -1
net/can/af_can.c
··· 875 875 spin_lock_init(&net->can.can_rcvlists_lock); 876 876 net->can.can_rx_alldev_list = 877 877 kzalloc(sizeof(struct dev_rcv_lists), GFP_KERNEL); 878 - 878 + if (!net->can.can_rx_alldev_list) 879 + goto out; 879 880 net->can.can_stats = kzalloc(sizeof(struct s_stats), GFP_KERNEL); 881 + if (!net->can.can_stats) 882 + goto out_free_alldev_list; 880 883 net->can.can_pstats = kzalloc(sizeof(struct s_pstats), GFP_KERNEL); 884 + if (!net->can.can_pstats) 885 + goto out_free_can_stats; 881 886 882 887 if (IS_ENABLED(CONFIG_PROC_FS)) { 883 888 /* the statistics are updated every second (timer triggered) */ ··· 897 892 } 898 893 899 894 return 0; 895 + 896 + out_free_can_stats: 897 + kfree(net->can.can_stats); 898 + out_free_alldev_list: 899 + kfree(net->can.can_rx_alldev_list); 900 + out: 901 + return -ENOMEM; 900 902 } 901 903 902 904 static void can_pernet_exit(struct net *net)