Received: by 2002:a05:6a10:7420:0:0:0:0 with SMTP id hk32csp4435790pxb; Mon, 21 Feb 2022 21:32:58 -0800 (PST) X-Google-Smtp-Source: ABdhPJxjwqB6N5F5bWKTeIxSs232IYBuccL1r3ylR8HLH111B2kEKJ7fxNM1p2aQxitJE/rxY4TA X-Received: by 2002:a17:903:32d1:b0:14d:d40d:73fa with SMTP id i17-20020a17090332d100b0014dd40d73famr21444520plr.55.1645507978098; Mon, 21 Feb 2022 21:32:58 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1645507978; cv=none; d=google.com; s=arc-20160816; b=bn65ZlknGEyIHPt3FWcIUqoRRm9BQhncyj6x9j9EWB7lCI1gmLkbN+SDxfnsQ30b8E pRCrKF5LZiUGkp7H5h/pad9HNWmP3wzARjfDZAIpFg5fIzKzKeX1kbHWFauX53QC7FNn voVGItedZ2r4GQlOoSSHOBvusBI6yq+XI+U/ur3RMv3z5lp97BcHWwug1OYtpgwJYYN2 m8U70zH6Dn3EJYcp1C7hsHFV/9lsGWssNmCxKTFmqXF8cZpZFP7brHtjMCdmAwI8+wys MF+G6Ursa813zEwIauZQZdTTK9vMG4Dtn44tTr45An7mjgZ+GP9iedJjBJ6qadXdJ014 7CHA== 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=rDXMyekYAqB1Wqddk/Of6E5FP9WPfe1SvYLDDn+1mdDGMC2kj0TCeRC7Vq2Wu8tIM/ ogTdJWVWgd4mCMP5xDwE+GpEkeluPBAGfKo6O2o/PrYQ5dAug6nk61HpIvpXX8STAkRl NCLgtzHYeS+iMwZK3z2IdcPJ9o8P9C8I+wkBxn0tpJu2L5dBc5y6pbhs4vxGF6lCFu8A 8BVKIelOHIu3GVler3FZ7Ax6ZYcSn6bP80B1PiFeKysyh3U843tUuAnjO4qzSTUAS2dG 2PO0s+9hVe88NgTE+/vyvQDKgg+VJ6gJLox34wTfas5bi/0vieU319IkvgfdIkJ/Cf/K +2NA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b="WjhAFSS/"; spf=softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 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. [23.128.96.19]) by mx.google.com with ESMTPS id c5-20020a17090a8d0500b001bc4dc8500csi1134867pjo.98.2022.02.21.21.32.57 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 21 Feb 2022 21:32:58 -0800 (PST) Received-SPF: softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) client-ip=23.128.96.19; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b="WjhAFSS/"; spf=softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 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 22E2E1179A8; Mon, 21 Feb 2022 21:01:05 -0800 (PST) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236101AbiBUXR6 (ORCPT + 99 others); Mon, 21 Feb 2022 18:17:58 -0500 Received: from mxb-00190b01.gslb.pphosted.com ([23.128.96.19]:32932 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236051AbiBUXRv (ORCPT ); Mon, 21 Feb 2022 18:17:51 -0500 Received: from mga03.intel.com (mga03.intel.com [134.134.136.65]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 494C32459C; Mon, 21 Feb 2022 15:17:27 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1645485447; x=1677021447; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=5NTVg/3b+n9yGWloqSIjoBVRuhweq0kc3TvI9tdbCGE=; b=WjhAFSS//BAgw81ig+c5vCwM1p1hizaLB8hCwvvrCAYwXyJnGg+t2amH Nyy8FPPZAX7VN1umUlON2x7JH1weX8j179Yl5iKLCFeMKSytlW7GR5jnu kHvp+1Gh9JNyP3bLkhBXj2hlHwnW5nyVwp2uEuFFCm9k/ipQ2pBodNQPk puksrxSvTcq+Dg0WWLQBz5phaDu/zncUxUsDExjx59dBVQjt0xbKO89Fe Cq51JUDLBNbDPPHiHSFWSZ+dNaIWXrYV82imsD4tnWWBBiO49NLw2AWr7 BClugg7YhI8fjLBvJniZvu2mIoaozYDjVDu6kR0U7IaSyHxfXOnikPiVO A==; X-IronPort-AV: E=McAfee;i="6200,9189,10265"; a="251530344" X-IronPort-AV: E=Sophos;i="5.88,386,1635231600"; d="scan'208";a="251530344" Received: from orsmga004.jf.intel.com ([10.7.209.38]) by orsmga103.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 21 Feb 2022 15:17:27 -0800 X-IronPort-AV: E=Sophos;i="5.88,386,1635231600"; d="scan'208";a="638694376" Received: from lab-ah.igk.intel.com ([10.91.215.196]) by orsmga004-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 21 Feb 2022 15:17:24 -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 v2 01/11] ref_tracker: implement use-after-free detection Date: Tue, 22 Feb 2022 00:16:45 +0100 Message-Id: <20220221231705.1481059-4-andrzej.hajda@intel.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20220221231705.1481059-1-andrzej.hajda@intel.com> References: <20220221231705.1481059-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