Received: by 2002:a05:6a10:7420:0:0:0:0 with SMTP id hk32csp4435726pxb; Mon, 21 Feb 2022 21:32:52 -0800 (PST) X-Google-Smtp-Source: ABdhPJw6MV9IQT/NkSZYT0cAf4TLtxbPJdR2orZu5wIn/E3y5IfA+IoPM0NU6w5AF/UDemg/aDlx X-Received: by 2002:a63:7b0e:0:b0:374:2525:d8c8 with SMTP id w14-20020a637b0e000000b003742525d8c8mr8839949pgc.42.1645507972261; Mon, 21 Feb 2022 21:32:52 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1645507972; cv=none; d=google.com; s=arc-20160816; b=HCmEhgBSTUqgTx1p7em0w8FUYgoxpci8910aROiv2WBiHF4Q1qmBYcXbg0DbDguBje rqG1MoDunGGPVGju0ChuQ30NEJliEJvM9x0eCXU64UsMHSGhZpHxiFYLXuCdA45V88PN nwQjXBIpHk7O6pTQhRCqLMG24x3vZh1eW3GDdZBLIKvzMZrgsO5OvqJrX0ott8KP51ra TGPLnqcPTW9JwfmKMTuOd1vEl5Mu6Pln3CXb1g/TBsfeRLGfJVHfith4q9ckj2vp1/ED WOYnpfTjV7jXLYE05gbeoI5EeZSNN0Y8CUExBa5qbBSTfhTEmKUAEBUzvdijiLTe70Y7 tgSg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:organization :mime-version:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=4xg7jwF/09tzab8aj8KjZr9xicQu8lJWfyIPnAZKonI=; b=bAbnVWHmVP5z2ENJR41z29f9F5mcH8VGknqXep1EZmvdeAKuxBfLmXpZN5d+v56XWL gnwPIOBWgnF3wwiZdt8mxdXlMyxdiI5IdcjxPLKxvOzFQOUms6A06Mmupf/MZO1tBgoE 4syHKcpHPCVmp/9SKvLmixW3z5xTp96HHK6Rw+gMATD5UGhgIhqWwBbi6OgisFa8DjDG /BU4SWZl5+zPcw4/QMVjPB+9/SUfwQGPeiQu4rvRGUHpsGP2nnRUmrDhkxo3YgtwqkHS qgrZHidllPVsi8moeqQ3jclVXYnKOgssBdsK5jZ6/FaOFRz5zzfGWicJITHwWrmmqpgy h08A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=XsTI6izU; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Return-Path: Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net. [2620:137:e000::1:18]) by mx.google.com with ESMTPS id y72si19307469pgd.419.2022.02.21.21.32.51 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 21 Feb 2022 21:32:52 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) client-ip=2620:137:e000::1:18; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=XsTI6izU; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id A3EF71168EA; Mon, 21 Feb 2022 21:01:00 -0800 (PST) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236607AbiBUX00 (ORCPT + 99 others); Mon, 21 Feb 2022 18:26:26 -0500 Received: from mxb-00190b01.gslb.pphosted.com ([23.128.96.19]:40606 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236520AbiBUX0W (ORCPT ); Mon, 21 Feb 2022 18:26:22 -0500 Received: from mga05.intel.com (mga05.intel.com [192.55.52.43]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 61A1624F06; Mon, 21 Feb 2022 15:25:58 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1645485958; x=1677021958; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=5NTVg/3b+n9yGWloqSIjoBVRuhweq0kc3TvI9tdbCGE=; b=XsTI6izUfQqIHjMfGYaFKCugfvFjWSKsp2Nbku1OjpqBxV5oyYabTVc+ vA283z6CTDE3YpGY+ZkJm5r8vBnB9OBFPvQ03UJ9eVl5oMTNMngxh+Up7 WDZXVGQdhXNF2zJoQZB6+5uoEjogzl4hJPBUhckNWFmb5qUDPFaYGOa0X i4c/RjgoeGBliXIDO+gaV+ugs+B5X8M7P3HhKfnP4PCOEBgMg7m7jjW8B XwrTEJC1F/4bgsA3waEOJW7WMyAjhev4m9frySXHYSwyTv+8B1HyBNtMr lUK7geRlsgvjl+Gf1RF7yTt4w7hBYVgB+ngaXL48YrRH0vbrMXF5xWEx8 w==; X-IronPort-AV: E=McAfee;i="6200,9189,10265"; a="338011869" X-IronPort-AV: E=Sophos;i="5.88,386,1635231600"; d="scan'208";a="338011869" Received: from orsmga005.jf.intel.com ([10.7.209.41]) by fmsmga105.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 21 Feb 2022 15:25:58 -0800 X-IronPort-AV: E=Sophos;i="5.88,386,1635231600"; d="scan'208";a="706396973" Received: from lab-ah.igk.intel.com ([10.91.215.196]) by orsmga005-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 21 Feb 2022 15:25:54 -0800 From: Andrzej Hajda To: linux-kernel@vger.kernel.org, intel-gfx@lists.freedesktop.org, dri-devel@lists.freedesktop.org, netdev Cc: Eric Dumazet , Jani Nikula , Daniel Vetter , Lucas De Marchi , Andrzej Hajda , Chris Wilson , Dmitry Vyukov , Jakub Kicinski , "David S . Miller" Subject: [PATCH v3 01/11] [DO NOT MERGE] ref_tracker: implement use-after-free detection Date: Tue, 22 Feb 2022 00:25:32 +0100 Message-Id: <20220221232542.1481315-2-andrzej.hajda@intel.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20220221232542.1481315-1-andrzej.hajda@intel.com> References: <20220221232542.1481315-1-andrzej.hajda@intel.com> MIME-Version: 1.0 Organization: Intel Technology Poland sp. z o.o. - ul. Slowackiego 173, 80-298 Gdansk - KRS 101882 - NIP 957-07-52-316 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-2.0 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,RDNS_NONE,SPF_HELO_NONE,T_SCC_BODY_TEXT_LINE autolearn=no autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Eric Dumazet Whenever ref_tracker_dir_init() is called, mark the struct ref_tracker_dir as dead. Test the dead status from ref_tracker_alloc() and ref_tracker_free() This should detect buggy dev_put()/dev_hold() happening too late in netdevice dismantle process. Signed-off-by: Eric Dumazet Signed-off-by: David S. Miller Signed-off-by: Andrzej Hajda --- include/linux/ref_tracker.h | 2 ++ lib/ref_tracker.c | 5 +++++ 2 files changed, 7 insertions(+) diff --git a/include/linux/ref_tracker.h b/include/linux/ref_tracker.h index 60f3453be23e6..a443abda937d8 100644 --- a/include/linux/ref_tracker.h +++ b/include/linux/ref_tracker.h @@ -13,6 +13,7 @@ struct ref_tracker_dir { spinlock_t lock; unsigned int quarantine_avail; refcount_t untracked; + bool dead; struct list_head list; /* List of active trackers */ struct list_head quarantine; /* List of dead trackers */ #endif @@ -26,6 +27,7 @@ static inline void ref_tracker_dir_init(struct ref_tracker_dir *dir, INIT_LIST_HEAD(&dir->quarantine); spin_lock_init(&dir->lock); dir->quarantine_avail = quarantine_count; + dir->dead = false; refcount_set(&dir->untracked, 1); stack_depot_init(); } diff --git a/lib/ref_tracker.c b/lib/ref_tracker.c index a6789c0c626b0..32ff6bd497f8e 100644 --- a/lib/ref_tracker.c +++ b/lib/ref_tracker.c @@ -20,6 +20,7 @@ void ref_tracker_dir_exit(struct ref_tracker_dir *dir) unsigned long flags; bool leak = false; + dir->dead = true; spin_lock_irqsave(&dir->lock, flags); list_for_each_entry_safe(tracker, n, &dir->quarantine, head) { list_del(&tracker->head); @@ -72,6 +73,8 @@ int ref_tracker_alloc(struct ref_tracker_dir *dir, gfp_t gfp_mask = gfp; unsigned long flags; + WARN_ON_ONCE(dir->dead); + if (gfp & __GFP_DIRECT_RECLAIM) gfp_mask |= __GFP_NOFAIL; *trackerp = tracker = kzalloc(sizeof(*tracker), gfp_mask); @@ -100,6 +103,8 @@ int ref_tracker_free(struct ref_tracker_dir *dir, unsigned int nr_entries; unsigned long flags; + WARN_ON_ONCE(dir->dead); + if (!tracker) { refcount_dec(&dir->untracked); return -EEXIST; -- 2.25.1