Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1755424AbcKYQKA (ORCPT ); Fri, 25 Nov 2016 11:10:00 -0500 Received: from mail-lf0-f42.google.com ([209.85.215.42]:35040 "EHLO mail-lf0-f42.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1754873AbcKYQJj (ORCPT ); Fri, 25 Nov 2016 11:09:39 -0500 From: Nikita Yushchenko To: Andrew Lunn , Vivien Didelot , Florian Fainelli , "David S. Miller" , John Crispin , Wei Yongjun , netdev@vger.kernel.org, linux-kernel@vger.kernel.org Cc: Chris Healy , Nikita Yushchenko Subject: [PATCH] net: dsa: fix unbalanced dsa_switch_tree reference counting Date: Fri, 25 Nov 2016 19:08:53 +0300 Message-Id: <1480090133-30412-1-git-send-email-nikita.yoush@cogentembedded.com> X-Mailer: git-send-email 2.1.4 Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Length: 1279 Lines: 39 _dsa_register_switch() gets a dsa_switch_tree object either via dsa_get_dst() or via dsa_add_dst(). Former path does not increase kref in returned object (resulting into caller not owning a reference), while later path does create a new object (resulting into caller owning a reference). The rest of _dsa_register_switch() assumes that it owns a reference, and calls dsa_put_dst(). This causes a memory breakage if first switch in the tree initialized successfully, but second failed to initialize. In particular, freed dsa_swith_tree object is left referenced by switch that was initialized, and later access to sysfs attributes of that switch cause OOPS. To fix, need to add kref_get() call to dsa_get_dst(). Signed-off-by: Nikita Yushchenko --- net/dsa/dsa2.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/net/dsa/dsa2.c b/net/dsa/dsa2.c index f8a7d9aab437..5fff951a0a49 100644 --- a/net/dsa/dsa2.c +++ b/net/dsa/dsa2.c @@ -28,8 +28,10 @@ static struct dsa_switch_tree *dsa_get_dst(u32 tree) struct dsa_switch_tree *dst; list_for_each_entry(dst, &dsa_switch_trees, list) - if (dst->tree == tree) + if (dst->tree == tree) { + kref_get(&dst->refcount); return dst; + } return NULL; } -- 2.1.4