Received: by 2002:a25:683:0:0:0:0:0 with SMTP id 125csp359034ybg; Fri, 12 Jun 2020 03:31:39 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwUTm4KsKb80akxWwFV8uTkDCuyOnafv1oYljHj3lOrd4ExHjU6pIFZyH9EeQkjy243QFGs X-Received: by 2002:a17:907:9d8:: with SMTP id bx24mr12275213ejc.517.1591957899353; Fri, 12 Jun 2020 03:31:39 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1591957899; cv=none; d=google.com; s=arc-20160816; b=JOQNKgcsNPd8lbvXzVdDN4Zft9B6ftw0xDoBLLEhat5Gw01J3IM52QLRCE9nSdn209 KBLXz5u5gUJTougz2kB7mQSqTTGKZLiTAoTMuAK9qsa1WeEmlacKDb+/bqU/DD8LG/qx 9sS1+PGepSxtwxX6ohEsT2qt/COZHGIL7Yt8p1C5RPB0uJSn/AFzPxif7/ShgnH5IDAz Ja/QtTOYOt9EAtH7ntpyD2qD2TY+ouelU3ry7mrULhAG0XUYY31x1/gD27JJHnDvVZOf Y0OVpbaZIZ+drGVz2vWk4sNAtLuTKce/CnJYx3EW0jpLYYO+oYEyipMdKTdyhK1ieXgW Qd2A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:mime-version:message-id:date:subject:cc :to:from; bh=Pg29DEJl9Ef8gtM3lYTJERVqd1nWBKYRNdIYY668IbU=; b=V5+duNijD9rGwnQiSutlqFFPxGXNvSdVNd0k/bdzUkDajexFFf/RkSQEXVsiqpYETO 4Bfr79wRnGQV39qvpg1Cc5Rioxw6fgUdcJVATwDjoS6L2Fk6bXCHsiQOu2TagPt9l7pQ G6yhQ5r437b6rN0NNV1iqprQ4MTpt09uDYhzUVtyHV5KzJP2ibDPK/BToJWNFd8q/wXs VAfB2u9xYgESWvhhnB2BBNU1fNWZVPiUFifig4Fv1dqjGGF69Dr/rbZGokTUXB175I5+ udUtp5B0+d6K/bUnwO4mVY9djtrUwjKbUtjbwMi+wl5HhSdksmv24xyi4VofQ2YRnN3F 5MUg== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id h3si4826414ejo.141.2020.06.12.03.31.16; Fri, 12 Jun 2020 03:31:39 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726259AbgFLK22 (ORCPT + 99 others); Fri, 12 Jun 2020 06:28:28 -0400 Received: from szxga07-in.huawei.com ([45.249.212.35]:40822 "EHLO huawei.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1725911AbgFLK21 (ORCPT ); Fri, 12 Jun 2020 06:28:27 -0400 Received: from DGGEMS413-HUB.china.huawei.com (unknown [172.30.72.60]) by Forcepoint Email with ESMTP id DC2D05EBC751DA91CC57; Fri, 12 Jun 2020 18:28:24 +0800 (CST) Received: from use12-sp2.huawei.com (10.67.189.174) by DGGEMS413-HUB.china.huawei.com (10.3.19.213) with Microsoft SMTP Server id 14.3.487.0; Fri, 12 Jun 2020 18:28:18 +0800 From: Xiaoming Ni To: , , , , , , , CC: , , , , Subject: [PATCH RFC] cred: Add WARN to detect wrong use of get/put_cred Date: Fri, 12 Jun 2020 18:28:15 +0800 Message-ID: <1591957695-118312-1-git-send-email-nixiaoming@huawei.com> X-Mailer: git-send-email 1.8.5.6 MIME-Version: 1.0 Content-Type: text/plain X-Originating-IP: [10.67.189.174] X-CFilter-Loop: Reflected Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Cred release and usage check code flow: 1. put_cred() if (atomic_dec_and_test(&(cred)->usage)) __put_cred(cred); 2. __put_cred() BUG_ON(atomic_read(&cred->usage) != 0); call_rcu(&cred->rcu, put_cred_rcu); 3. put_cred_rcu() if (atomic_read(&cred->usage) != 0) panic("CRED: put_cred_rcu() sees %p with usage %d\n", cred, atomic_read(&cred->usage)); kmem_cache_free(cred_jar, cred); If panic is triggered on put_cred_rcu(), there are two possibilities 1. Call get_cred() after __put_cred(), usage > 0 2. Call put_cred() after __put_cred(), usage < 0 Since put_cred_rcu is an asynchronous behavior, it is no longer the first scene when panic, there is no information about the murderer in the panic call stack... So, add WARN() in get_cred()/put_cred(), and pray to catch the murderer at the first scene. Signed-off-by: Xiaoming Ni --- include/linux/cred.h | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/include/linux/cred.h b/include/linux/cred.h index 18639c0..c00d5a1 100644 --- a/include/linux/cred.h +++ b/include/linux/cred.h @@ -224,11 +224,16 @@ static inline bool cap_ambient_invariant_ok(const struct cred *cred) * * Get a reference on the specified set of new credentials. The caller must * release the reference. + * + * Initialize usage to 1 during cred resource allocation, + * so when calling get_cred, usage cannot be 0. */ static inline struct cred *get_new_cred(struct cred *cred) { - atomic_inc(&cred->usage); - return cred; + if (atomic_inc_not_zero(&cred->usage)) + return cred; + WARN(1, "get_new_cred after __put_cred"); + return NULL; } /** @@ -280,11 +285,14 @@ static inline const struct cred *get_cred_rcu(const struct cred *cred) static inline void put_cred(const struct cred *_cred) { struct cred *cred = (struct cred *) _cred; + int usage; if (cred) { validate_creds(cred); - if (atomic_dec_and_test(&(cred)->usage)) + usage = atomic_dec_return(&(cred)->usage); + if (usage == 0) __put_cred(cred); + WARN(usage < 0, "put_cred after __put_cred"); } } -- 1.8.5.6