From nobody Sun Feb 8 23:25:43 2026 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 6D11318BC36 for ; Wed, 21 Aug 2024 12:36:30 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1724243792; cv=none; b=kRPm1NGXo+zyJ4FraW6jneBBxGQklY23hHCcjYmgwE0M32d+c9r2aqMTN701le0EDSbfjlT2vGTzNq5ggv17SaBSlZ4YKv9hngPvljk0eipCTLvCuWBWVb+ttryQAjA3d9+8iszlhZ1KSpP5uLGcliMRUue8FE0uVUqNiKLSQwg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1724243792; c=relaxed/simple; bh=vxY1xD4PYBOudJSOqSNC2Vu4G+cS92ZDHn4k+kBjiTE=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=NmzrlSLGRbP8AYhTDW9kdmjuxktSuey1I28xIpYjlkTjEdgQIYavwhKdGOFfxdomxgG4IFMTxfs66x8NNGr7Bq8FiToqar0AoB0skrVkdNdT59rZn9Go3vSABkGWLZ/X9J25TrcRkD5bqjUSzxcMcvs3+dx6XunTd8950E6zUSE= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=aFx6YXaU; arc=none smtp.client-ip=170.10.129.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="aFx6YXaU" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1724243789; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=x9bQJ2co3elsRL5KhC8W7d+SgjU49zr7xREsgM6Vv2Q=; b=aFx6YXaUhIVCkKh07v7m6fv7Vz661wFWruwx0Lkv8QL5paBkPHha4Gfpgqm3e1Aw2FIQ8m inQowFlhfigqlSsUlkkjGBcQ/CX3y2zAf4ONLBA/7uUaTPrkEzwU7WuPKqRhpHx9Xkj790 53SUbjdSQi6bl3EBXYo4TiJA7fX3B1U= Received: from mx-prod-mc-05.mail-002.prod.us-west-2.aws.redhat.com (ec2-54-186-198-63.us-west-2.compute.amazonaws.com [54.186.198.63]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-259-l2A2HIDUPR-LvjBZbaP7-Q-1; Wed, 21 Aug 2024 08:36:28 -0400 X-MC-Unique: l2A2HIDUPR-LvjBZbaP7-Q-1 Received: from mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.12]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-05.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 30E6E195608A; Wed, 21 Aug 2024 12:36:27 +0000 (UTC) Received: from warthog.procyon.org.uk.com (unknown [10.42.28.30]) by mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id A1ACB19560AE; Wed, 21 Aug 2024 12:36:25 +0000 (UTC) From: David Howells To: Jarkko Sakkinen Cc: David Howells , keyrings@vger.kernel.org, linux-security-module@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH 1/7] keys: Out of line key_is_dead() so it can have tracepoints added in Date: Wed, 21 Aug 2024 13:36:09 +0100 Message-ID: <20240821123616.60401-2-dhowells@redhat.com> In-Reply-To: <20240821123616.60401-1-dhowells@redhat.com> References: <20240821123616.60401-1-dhowells@redhat.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.0 on 10.30.177.12 Content-Type: text/plain; charset="utf-8" Move key_is_dead() out of line so that tracepoints can be added in to it without incurring circular #includes. Also, it is only used from the file it is moved into. Signed-off-by: David Howells cc: Jarkko Sakkinen cc: keyrings@vger.kernel.org cc: linux-security-module@vger.kernel.org Reviewed-by: Jarkko Sakkinen --- security/keys/internal.h | 20 -------------------- security/keys/keyring.c | 20 ++++++++++++++++++++ 2 files changed, 20 insertions(+), 20 deletions(-) diff --git a/security/keys/internal.h b/security/keys/internal.h index 2cffa6dc8255..8ba87127e446 100644 --- a/security/keys/internal.h +++ b/security/keys/internal.h @@ -211,26 +211,6 @@ extern struct key *request_key_auth_new(struct key *ta= rget, =20 extern struct key *key_get_instantiation_authkey(key_serial_t target_id); =20 -/* - * Determine whether a key is dead. - */ -static inline bool key_is_dead(const struct key *key, time64_t limit) -{ - time64_t expiry =3D key->expiry; - - if (expiry !=3D TIME64_MAX) { - if (!(key->type->flags & KEY_TYPE_INSTANT_REAP)) - expiry +=3D key_gc_delay; - if (expiry <=3D limit) - return true; - } - - return - key->flags & ((1 << KEY_FLAG_DEAD) | - (1 << KEY_FLAG_INVALIDATED)) || - key->domain_tag->removed; -} - /* * keyctl() functions */ diff --git a/security/keys/keyring.c b/security/keys/keyring.c index 4448758f643a..0eed018448cb 100644 --- a/security/keys/keyring.c +++ b/security/keys/keyring.c @@ -1687,6 +1687,26 @@ static void keyring_revoke(struct key *keyring) } } =20 +/* + * Determine whether a key is dead. + */ +static bool key_is_dead(const struct key *key, time64_t limit) +{ + time64_t expiry =3D key->expiry; + + if (expiry !=3D TIME64_MAX) { + if (!(key->type->flags & KEY_TYPE_INSTANT_REAP)) + expiry +=3D key_gc_delay; + if (expiry <=3D limit) + return true; + } + + return + key->flags & ((1 << KEY_FLAG_DEAD) | + (1 << KEY_FLAG_INVALIDATED)) || + key->domain_tag->removed; +} + static bool keyring_gc_select_iterator(void *object, void *iterator_data) { struct key *key =3D keyring_ptr_to_key(object); From nobody Sun Feb 8 23:25:43 2026 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 72224192D90 for ; Wed, 21 Aug 2024 12:36:35 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1724243797; cv=none; b=onJly/UXUbwlVUO1rpk8ifzyqJORy12Yh9aTKaQ/JsNVjWO8wq8WaL506FaJp/I9pSFZkoALkzvdFWn2JlAZ4wDzIokE3Drwc8wnlQ4zWdygDoeRR7MGW871MDUdcGBm2AgTVvCvHlcLDjWt5UdPzrd89kAyE2fvT1GHHx6wMv4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1724243797; c=relaxed/simple; bh=biAla2+tDuO620PqR8PPrT6nQ4xm9Caqb/5pyAP/muw=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=eg/678Yody34kCn+R/JSRu7S4CjTZQ5Uumkd3K6/8tqpUo+QSOKjXgoq6szKvkQ7r9XfILP2MDnwLCEauJjICeaJrUg0N0C7oEKrh8EIq6IatfcTPasNCZrJfwl3UHmiRBy5QMufKbIn/ywAir5xFCjfSPaAN2euDHWaHdOCmaY= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=Y3Ztb8yD; arc=none smtp.client-ip=170.10.133.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="Y3Ztb8yD" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1724243794; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=GCuFnR5X9H0OLLLaiWpXef0YVQ7dHPn4OY0ESihFasw=; b=Y3Ztb8yDpA7YjGDnMIJt0XT8jpI11/Xz3Z3TJfIs9ZJyM9/UsgCIeoSeDCMgBRm9BkEdTx TQBme77TgiSmVrE0dZCb3SYWrCy7uV2KOmz/RkM6XMfy6wtHSEDg2YDbpgNi7g1oBR33F9 ODdb1VlzVASM1PV3lzWkfX6gfFmAUTE= Received: from mx-prod-mc-05.mail-002.prod.us-west-2.aws.redhat.com (ec2-54-186-198-63.us-west-2.compute.amazonaws.com [54.186.198.63]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-644-MP2BVvUKPxi9CTMibNUXfg-1; Wed, 21 Aug 2024 08:36:31 -0400 X-MC-Unique: MP2BVvUKPxi9CTMibNUXfg-1 Received: from mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.17]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-05.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 3E10E1954B1B; Wed, 21 Aug 2024 12:36:30 +0000 (UTC) Received: from warthog.procyon.org.uk.com (unknown [10.42.28.30]) by mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 9589419560A3; Wed, 21 Aug 2024 12:36:28 +0000 (UTC) From: David Howells To: Jarkko Sakkinen Cc: David Howells , keyrings@vger.kernel.org, linux-security-module@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH 2/7] keys: Extract struct key_user to its own header for tracing purposes Date: Wed, 21 Aug 2024 13:36:10 +0100 Message-ID: <20240821123616.60401-3-dhowells@redhat.com> In-Reply-To: <20240821123616.60401-1-dhowells@redhat.com> References: <20240821123616.60401-1-dhowells@redhat.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.0 on 10.30.177.17 Content-Type: text/plain; charset="utf-8" Extract the key_user struct to its own header file to make it easier to access from tracepoints. Signed-off-by: David Howells cc: Jarkko Sakkinen cc: keyrings@vger.kernel.org cc: linux-security-module@vger.kernel.org Reviewed-by: Jarkko Sakkinen --- include/keys/key_user.h | 35 +++++++++++++++++++++++++++++++++++ security/keys/internal.h | 20 +------------------- 2 files changed, 36 insertions(+), 19 deletions(-) create mode 100644 include/keys/key_user.h diff --git a/include/keys/key_user.h b/include/keys/key_user.h new file mode 100644 index 000000000000..e9c383d8116e --- /dev/null +++ b/include/keys/key_user.h @@ -0,0 +1,35 @@ +/* User quota tracking for keys. + * + * Copyright (C) 2024 Red Hat, Inc. All Rights Reserved. + * Written by David Howells (dhowells@redhat.com) + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public Licence + * as published by the Free Software Foundation; either version + * 2 of the Licence, or (at your option) any later version. + */ + +#ifndef _KEYS_KEY_USER_H +#define _KEYS_KEY_USER_H + +/* + * Keep track of keys for a user. + * + * This needs to be separate to user_struct to avoid a refcount-loop + * (user_struct pins some keyrings which pin this struct). + * + * We also keep track of keys under request from userspace for this UID he= re. + */ +struct key_user { + struct rb_node node; + struct mutex cons_lock; /* construction initiation lock */ + spinlock_t lock; + refcount_t usage; /* for accessing qnkeys & qnbytes */ + atomic_t nkeys; /* number of keys */ + atomic_t nikeys; /* number of instantiated keys */ + kuid_t uid; + int qnkeys; /* number of keys allocated to this user */ + int qnbytes; /* number of bytes allocated to this user */ +}; + +#endif /* _KEYS_KEY_USER_H */ diff --git a/security/keys/internal.h b/security/keys/internal.h index 8ba87127e446..33c929a6bb97 100644 --- a/security/keys/internal.h +++ b/security/keys/internal.h @@ -19,6 +19,7 @@ #include #include #include +#include =20 struct iovec; =20 @@ -43,25 +44,6 @@ extern struct key_type key_type_user; extern struct key_type key_type_logon; =20 /*************************************************************************= ****/ -/* - * Keep track of keys for a user. - * - * This needs to be separate to user_struct to avoid a refcount-loop - * (user_struct pins some keyrings which pin this struct). - * - * We also keep track of keys under request from userspace for this UID he= re. - */ -struct key_user { - struct rb_node node; - struct mutex cons_lock; /* construction initiation lock */ - spinlock_t lock; - refcount_t usage; /* for accessing qnkeys & qnbytes */ - atomic_t nkeys; /* number of keys */ - atomic_t nikeys; /* number of instantiated keys */ - kuid_t uid; - int qnkeys; /* number of keys allocated to this user */ - int qnbytes; /* number of bytes allocated to this user */ -}; =20 extern struct rb_root key_user_tree; extern spinlock_t key_user_lock; From nobody Sun Feb 8 23:25:43 2026 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 4F336192D90 for ; Wed, 21 Aug 2024 12:36:40 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1724243801; cv=none; b=KJzTOrqfE1v8K6EC3YJuqh8UeNWJnaUE+XbxkTl5KAlqZ7Oy5l6eatXCldLfBKkg91Lfuz4HgUZeD+EnYe+Hab33etnhPcdS0ROAqgh56fI2drFLFh6++8C47UBB5uNXvupEd/6Qq4dtGlT8XvSuJPjKYzrl+iqsP8wUiPGUme0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1724243801; c=relaxed/simple; bh=Yautcy9ck/HanBfjk9RDHnxNHXSiPTSG9X+XqexUOe8=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=Pu3y4ytfxf+KcuyEghpHGfsj6o51S+kf9e69wSWJ8e397S5L4GWNXVBdg4yVJk3nGBOq6aE2bz0iJehwuEBIkAMD5dHtPSKEwmiXRoNqI9Az/ZGhxUSXH3VW/Xlyds/5iKZoMFPGV2WBKj5ai9EH2EtJMUYrYnXvJZ/OpvrDLB4= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=YSZzqH2d; arc=none smtp.client-ip=170.10.129.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="YSZzqH2d" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1724243799; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=s+EPM0BxZa29yRtUIP6Bqy4vu3lGbdW23TC1wKLwsMs=; b=YSZzqH2dYiyCmUFg2PdpTyH2WloCJhwNrQqSA7brSLcbJkeiJE6Za1sAO0/6EmvucQLF48 CwVjITH1Go0ZS/fuzGnCWiAnGAPuLV8qR/FLSoOxddZpcw0T4SIx2mFkh86bH7qTpSAAR7 oO+dtuEq8V+2wIKa8b/UUEk3Y6FHL/A= Received: from mx-prod-mc-03.mail-002.prod.us-west-2.aws.redhat.com (ec2-54-186-198-63.us-west-2.compute.amazonaws.com [54.186.198.63]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-577-TGQLKAD3MFGxqxid0qWvaw-1; Wed, 21 Aug 2024 08:36:34 -0400 X-MC-Unique: TGQLKAD3MFGxqxid0qWvaw-1 Received: from mx-prod-int-04.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-04.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.40]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-03.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 1A9A31954B23; Wed, 21 Aug 2024 12:36:33 +0000 (UTC) Received: from warthog.procyon.org.uk.com (unknown [10.42.28.30]) by mx-prod-int-04.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 86D4319560AD; Wed, 21 Aug 2024 12:36:31 +0000 (UTC) From: David Howells To: Jarkko Sakkinen Cc: David Howells , keyrings@vger.kernel.org, linux-security-module@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH 3/7] keys: Move key_get() out of line so a tracepoint can be added Date: Wed, 21 Aug 2024 13:36:11 +0100 Message-ID: <20240821123616.60401-4-dhowells@redhat.com> In-Reply-To: <20240821123616.60401-1-dhowells@redhat.com> References: <20240821123616.60401-1-dhowells@redhat.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.0 on 10.30.177.40 Content-Type: text/plain; charset="utf-8" Move key_get() out of line so that a tracepoint can be added into it without incurring circular header dependencies or overly expanding the callers. Signed-off-by: David Howells cc: Jarkko Sakkinen cc: keyrings@vger.kernel.org cc: linux-security-module@vger.kernel.org Reviewed-by: Jarkko Sakkinen --- include/linux/key.h | 6 +----- security/keys/key.c | 14 ++++++++++++++ 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/include/linux/key.h b/include/linux/key.h index 943a432da3ae..08062b4f807c 100644 --- a/include/linux/key.h +++ b/include/linux/key.h @@ -299,6 +299,7 @@ extern struct key *key_alloc(struct key_type *type, =20 extern void key_revoke(struct key *key); extern void key_invalidate(struct key *key); +struct key *key_get(struct key *key); extern void key_put(struct key *key); extern bool key_put_tag(struct key_tag *tag); extern void key_remove_domain(struct key_tag *domain_tag); @@ -309,11 +310,6 @@ static inline struct key *__key_get(struct key *key) return key; } =20 -static inline struct key *key_get(struct key *key) -{ - return key ? __key_get(key) : key; -} - static inline void key_ref_put(key_ref_t key_ref) { key_put(key_ref_to_ptr(key_ref)); diff --git a/security/keys/key.c b/security/keys/key.c index 3d7d185019d3..14c7ee77ea15 100644 --- a/security/keys/key.c +++ b/security/keys/key.c @@ -635,6 +635,20 @@ int key_reject_and_link(struct key *key, } EXPORT_SYMBOL(key_reject_and_link); =20 +/** + * key_get - Get a reference on a key + * @key: The key to get a reference on. + * + * Get a reference on a key, if not NULL, and return the parameter. + */ +struct key *key_get(struct key *key) +{ + if (key) + refcount_inc(&key->usage); + return key; +} +EXPORT_SYMBOL(key_get); + /** * key_put - Discard a reference to a key. * @key: The key to discard a reference from. From nobody Sun Feb 8 23:25:43 2026 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 8117418C929 for ; Wed, 21 Aug 2024 12:36:41 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1724243803; cv=none; b=CaHfpVGKHg14dVusEPk1XxNptYg4gnhSrKJn2kMeTLiXh4Hrcie7NANr7qzlQj+SISXt2kth2/MojB6jQseegzMcuyyBcAAbNdIFr/2eGDP126fDPgc3NhGBi3TRfoZecNneSjO4bZipALlN2sf3KGPIpYkxGrGG0CLpaP5DH5E= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1724243803; c=relaxed/simple; bh=oivQtnOu9efmEnO/ADZ+WlTYAALrj3lzSKJQB2IwQIQ=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=CqAPMs3jdIF/gJQj6ZbH4788Wi2bQyxmvEsQs35MhvtKffm2plx2ESVVkdkgRCGD0s9R6eaOsK+EEriIO/gzhyRAoPpRNQ3LuNKToUDDIhvk/ZZiZG3mkcUQIXmwwyWKDpMZ0ODoWLnuzquXWzoq/2EqOBrM8Y5jVoraW0un4lw= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=iiPJDM3F; arc=none smtp.client-ip=170.10.133.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="iiPJDM3F" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1724243800; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=cXncICABB6w0gnMhh6f892KrTOfN/hhwAjfLKmFuv/M=; b=iiPJDM3F5K1rc9PfVlMguvGf7InTFCmK3TlQjmuxE2CnVKysUsnTPR294kyZ5Zq/RCtwh0 VrqL2kCbAteA/oNd6xYbw81qyRSOuBRpbzuB2u+FV4eiyRh848mI4475A4cBmAbPwJh7gm XDFFLdLFYDRwSvElD2/6LbAP8mVLmCs= Received: from mx-prod-mc-03.mail-002.prod.us-west-2.aws.redhat.com (ec2-54-186-198-63.us-west-2.compute.amazonaws.com [54.186.198.63]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-614-BBAWbVe6PLCVnuVLaV_XxA-1; Wed, 21 Aug 2024 08:36:37 -0400 X-MC-Unique: BBAWbVe6PLCVnuVLaV_XxA-1 Received: from mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.4]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-03.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 3CF581956048; Wed, 21 Aug 2024 12:36:36 +0000 (UTC) Received: from warthog.procyon.org.uk.com (unknown [10.42.28.30]) by mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 773AA300019C; Wed, 21 Aug 2024 12:36:34 +0000 (UTC) From: David Howells To: Jarkko Sakkinen Cc: David Howells , keyrings@vger.kernel.org, linux-security-module@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH 4/7] keys: Add a key_ref_get() wrapper Date: Wed, 21 Aug 2024 13:36:12 +0100 Message-ID: <20240821123616.60401-5-dhowells@redhat.com> In-Reply-To: <20240821123616.60401-1-dhowells@redhat.com> References: <20240821123616.60401-1-dhowells@redhat.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.4.1 on 10.30.177.4 Content-Type: text/plain; charset="utf-8" Add a key_ref_get() wrapper function to go with key_ref_put() and use it for the place that needs it. Signed-off-by: David Howells cc: Jarkko Sakkinen cc: keyrings@vger.kernel.org cc: linux-security-module@vger.kernel.org Reviewed-by: Jarkko Sakkinen --- include/linux/key.h | 5 +++++ security/keys/keyring.c | 2 +- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/include/linux/key.h b/include/linux/key.h index 08062b4f807c..50a19e5f9e45 100644 --- a/include/linux/key.h +++ b/include/linux/key.h @@ -315,6 +315,11 @@ static inline void key_ref_put(key_ref_t key_ref) key_put(key_ref_to_ptr(key_ref)); } =20 +static inline void key_ref_get(key_ref_t key_ref) +{ + key_get(key_ref_to_ptr(key_ref)); +} + extern struct key *request_key_tag(struct key_type *type, const char *description, struct key_tag *domain_tag, diff --git a/security/keys/keyring.c b/security/keys/keyring.c index 0eed018448cb..7f02b913c560 100644 --- a/security/keys/keyring.c +++ b/security/keys/keyring.c @@ -920,7 +920,7 @@ key_ref_t keyring_search_rcu(key_ref_t keyring_ref, =20 ctx->now =3D ktime_get_real_seconds(); if (search_nested_keyrings(keyring, ctx)) - __key_get(key_ref_to_ptr(ctx->result)); + key_ref_get(ctx->result); return ctx->result; } From nobody Sun Feb 8 23:25:43 2026 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id D9FAE196D90 for ; Wed, 21 Aug 2024 12:36:44 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1724243807; cv=none; b=WswiuVmUuGeGE4V13hx0TlekDdUn3yaDToVN6PEC3GIXNSNwj5Ly0+xGuZt5sTjg7yglx8OC75rBBxYpJlqvN+tXF+5iu1DJr+RsyFKEAaYs4MV34+4Ha8aJroEUZze2CJsBaSfwJVQeOzW5n7v7kyJhD0ZWP1Pz3qT+RhKBEKk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1724243807; c=relaxed/simple; bh=POSRcUENXI3EPk3+g/oKXBGninv57eRKGcZLPy9oZh0=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=rvEqvJinbkw2pk3nPYaFsdoDpqrh839lyMdiF3RSmrtBuf4tT5zggHOUWx3Sg8Hev1GAGtmg0L9Qc/1xedLgkngyYVWpl+AhaclhXD/nFDps23nvyuzdzfCGVTyKFU7YhNkZrwaUC8TG+VO8Tzvsm62CtVx74OuRk5Ga8jA3PFw= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=Z6hFqhqL; arc=none smtp.client-ip=170.10.133.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="Z6hFqhqL" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1724243804; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=aC1VMLaM7iGzjE0S+YKf7RFlhMIrzMBJdSI6+j4wcG0=; b=Z6hFqhqLC87X+7Qhlg6EeuxEFz8Plh+TygCfVn08rHerjS5dXNFb+3Sdgw4BGgnMuiEQ1+ 0KVk/RXeif03v4URR3QSO8lx6akD0i81ki9OXCAOfPiXHBkFBnJq67rVrO/1E4MRZTjAYx MdLQhNerTPhDijDIkSkxqa7lPC7oxrQ= Received: from mx-prod-mc-01.mail-002.prod.us-west-2.aws.redhat.com (ec2-54-186-198-63.us-west-2.compute.amazonaws.com [54.186.198.63]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-452-HZ4uwCiUMHe_IVN9rk_Ueg-1; Wed, 21 Aug 2024 08:36:40 -0400 X-MC-Unique: HZ4uwCiUMHe_IVN9rk_Ueg-1 Received: from mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.17]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-01.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id F2DC51955BF7; Wed, 21 Aug 2024 12:36:38 +0000 (UTC) Received: from warthog.procyon.org.uk.com (unknown [10.42.28.30]) by mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 5BD1C1956053; Wed, 21 Aug 2024 12:36:37 +0000 (UTC) From: David Howells To: Jarkko Sakkinen Cc: David Howells , keyrings@vger.kernel.org, linux-security-module@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH 5/7] keys: Use key_get() instead of __key_get() Date: Wed, 21 Aug 2024 13:36:13 +0100 Message-ID: <20240821123616.60401-6-dhowells@redhat.com> In-Reply-To: <20240821123616.60401-1-dhowells@redhat.com> References: <20240821123616.60401-1-dhowells@redhat.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.0 on 10.30.177.17 Content-Type: text/plain; charset="utf-8" Switch users of __key_get() over to key_get() so that they benefit from the future tracepointage thereof also and remove __key_get(). Signed-off-by: David Howells cc: Jarkko Sakkinen cc: keyrings@vger.kernel.org cc: linux-security-module@vger.kernel.org Reviewed-by: Jarkko Sakkinen --- Documentation/security/keys/core.rst | 1 - crypto/asymmetric_keys/restrict.c | 6 +++--- include/linux/key.h | 6 ------ security/keys/keyring.c | 4 ++-- security/keys/process_keys.c | 15 ++++++--------- 5 files changed, 11 insertions(+), 21 deletions(-) diff --git a/Documentation/security/keys/core.rst b/Documentation/security/= keys/core.rst index 326b8a973828..0b179540d885 100644 --- a/Documentation/security/keys/core.rst +++ b/Documentation/security/keys/core.rst @@ -1217,7 +1217,6 @@ payload contents" for more information. * Extra references can be made to a key by calling one of the following functions:: =20 - struct key *__key_get(struct key *key); struct key *key_get(struct key *key); =20 Keys so references will need to be disposed of by calling key_put() wh= en diff --git a/crypto/asymmetric_keys/restrict.c b/crypto/asymmetric_keys/res= trict.c index afcd4d101ac5..1ea7bfd4e5d7 100644 --- a/crypto/asymmetric_keys/restrict.c +++ b/crypto/asymmetric_keys/restrict.c @@ -267,20 +267,20 @@ static int key_or_keyring_common(struct key *dest_key= ring, if (!sig->auth_ids[0] && !sig->auth_ids[1]) { if (asymmetric_key_id_same(signer_ids[2], sig->auth_ids[2])) - key =3D __key_get(trusted); + key =3D key_get(trusted); =20 } else if (!sig->auth_ids[0] || !sig->auth_ids[1]) { const struct asymmetric_key_id *auth_id; =20 auth_id =3D sig->auth_ids[0] ?: sig->auth_ids[1]; if (match_either_id(signer_ids, auth_id)) - key =3D __key_get(trusted); + key =3D key_get(trusted); =20 } else if (asymmetric_key_id_same(signer_ids[1], sig->auth_ids[1]) && match_either_id(signer_ids, sig->auth_ids[0])) { - key =3D __key_get(trusted); + key =3D key_get(trusted); } } else { return -EOPNOTSUPP; diff --git a/include/linux/key.h b/include/linux/key.h index 50a19e5f9e45..80d736813b89 100644 --- a/include/linux/key.h +++ b/include/linux/key.h @@ -304,12 +304,6 @@ extern void key_put(struct key *key); extern bool key_put_tag(struct key_tag *tag); extern void key_remove_domain(struct key_tag *domain_tag); =20 -static inline struct key *__key_get(struct key *key) -{ - refcount_inc(&key->usage); - return key; -} - static inline void key_ref_put(key_ref_t key_ref) { key_put(key_ref_to_ptr(key_ref)); diff --git a/security/keys/keyring.c b/security/keys/keyring.c index 7f02b913c560..e77d927f1d4d 100644 --- a/security/keys/keyring.c +++ b/security/keys/keyring.c @@ -1122,7 +1122,7 @@ key_ref_t find_key_to_update(key_ref_t keyring_ref, kleave(" =3D NULL [x]"); return NULL; } - __key_get(key); + key_get(key); kleave(" =3D {%d}", key->serial); return make_key_ref(key, is_key_possessed(keyring_ref)); } @@ -1367,7 +1367,7 @@ int __key_link_check_live_key(struct key *keyring, st= ruct key *key) void __key_link(struct key *keyring, struct key *key, struct assoc_array_edit **_edit) { - __key_get(key); + key_get(key); assoc_array_insert_set_object(*_edit, keyring_key_to_ptr(key)); assoc_array_apply_edit(*_edit); *_edit =3D NULL; diff --git a/security/keys/process_keys.c b/security/keys/process_keys.c index b5d5333ab330..01291b2d0888 100644 --- a/security/keys/process_keys.c +++ b/security/keys/process_keys.c @@ -333,7 +333,7 @@ int install_session_keyring_to_cred(struct cred *cred, = struct key *keyring) if (IS_ERR(keyring)) return PTR_ERR(keyring); } else { - __key_get(keyring); + key_get(keyring); } =20 /* install the keyring */ @@ -641,7 +641,7 @@ key_ref_t lookup_user_key(key_serial_t id, unsigned lon= g lflags, } =20 key =3D ctx.cred->thread_keyring; - __key_get(key); + key_get(key); key_ref =3D make_key_ref(key, 1); break; =20 @@ -658,8 +658,7 @@ key_ref_t lookup_user_key(key_serial_t id, unsigned lon= g lflags, goto reget_creds; } =20 - key =3D ctx.cred->process_keyring; - __key_get(key); + key =3D key_get(ctx.cred->process_keyring); key_ref =3D make_key_ref(key, 1); break; =20 @@ -688,8 +687,7 @@ key_ref_t lookup_user_key(key_serial_t id, unsigned lon= g lflags, goto reget_creds; } =20 - key =3D ctx.cred->session_keyring; - __key_get(key); + key =3D key_get(ctx.cred->session_keyring); key_ref =3D make_key_ref(key, 1); break; =20 @@ -717,7 +715,7 @@ key_ref_t lookup_user_key(key_serial_t id, unsigned lon= g lflags, if (!key) goto error; =20 - __key_get(key); + key_get(key); key_ref =3D make_key_ref(key, 1); break; =20 @@ -732,8 +730,7 @@ key_ref_t lookup_user_key(key_serial_t id, unsigned lon= g lflags, key =3D NULL; } else { rka =3D ctx.cred->request_key_auth->payload.data[0]; - key =3D rka->dest_keyring; - __key_get(key); + key =3D key_get(rka->dest_keyring); } up_read(&ctx.cred->request_key_auth->sem); if (!key) From nobody Sun Feb 8 23:25:43 2026 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id F0754192586 for ; Wed, 21 Aug 2024 12:36:44 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1724243806; cv=none; b=huALZl5XXisFTij+Qzh50NWXB9dFv10YeRpfEf5qmLUR7BSdwS4wB0waM44cNonspZs4+dzgFAsAA8JlMhND9JK4qSB+QOmVx/eAiRlXROlYSQNoHZJQV6w35hyOZ78Dea3O+9Wt0jqz7hmbtXdT5Y2Y7vW1n60NeHv8dXs8ZDw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1724243806; c=relaxed/simple; bh=kb6yfCuD7s6K/AHQzrVFBVg8+XRRIKgNnPIONhfxYIk=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=kmV18JoDSZfEim3y2cD6wLZsIrkakW2PL6ya57lRpfxdnI5DxrLxaAu6/Oklvvp/dex9WCe0G111sMtfY3ECKTHk7OauQJ+kvDfToblFVZG4qECqYUNOsAzSFEdkNVrHpJR63mp1tXqqnASJxyXulRRtbtqTe2vUAyPLh/cdO24= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=WLxsr7ZY; arc=none smtp.client-ip=170.10.129.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="WLxsr7ZY" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1724243804; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=Oz5JS8PzZhZufjj9rN9JEylCZMH16sWmhd40JDN5AsI=; b=WLxsr7ZYZpsRUbt1Xdh53VUMnXvjQXOPwJybXfNrez6qDJe2y2m7yuoNfeqL2jLZt0UjOH utX+ToecYvnvf1nRhhclVq1O1Whyj5W6wUz+X/0ONfWW6p4dOc5yD4G11/QWnaZDpSxTkZ XJpl6AKW8aCdtPfSfyGCK/Oc+19DVrM= Received: from mx-prod-mc-01.mail-002.prod.us-west-2.aws.redhat.com (ec2-54-186-198-63.us-west-2.compute.amazonaws.com [54.186.198.63]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-111-nSzOlTbMOXqTvozKjZLZmQ-1; Wed, 21 Aug 2024 08:36:43 -0400 X-MC-Unique: nSzOlTbMOXqTvozKjZLZmQ-1 Received: from mx-prod-int-02.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-02.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.15]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-01.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id DE32A1954B15; Wed, 21 Aug 2024 12:36:41 +0000 (UTC) Received: from warthog.procyon.org.uk.com (unknown [10.42.28.30]) by mx-prod-int-02.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 63E5B1955DD8; Wed, 21 Aug 2024 12:36:40 +0000 (UTC) From: David Howells To: Jarkko Sakkinen Cc: David Howells , keyrings@vger.kernel.org, linux-security-module@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH 6/7] keys: Provide a key_try_get() function and use it Date: Wed, 21 Aug 2024 13:36:14 +0100 Message-ID: <20240821123616.60401-7-dhowells@redhat.com> In-Reply-To: <20240821123616.60401-1-dhowells@redhat.com> References: <20240821123616.60401-1-dhowells@redhat.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.0 on 10.30.177.15 Content-Type: text/plain; charset="utf-8" Add a key_try_get() function to try to get a ref on a key and switch code that's manipulating the key refcount directly to use it. This will allow a tracepoint to be emplaced there later. Signed-off-by: David Howells cc: Jarkko Sakkinen cc: keyrings@vger.kernel.org cc: linux-security-module@vger.kernel.org Reviewed-by: Jarkko Sakkinen --- include/linux/key.h | 1 + security/keys/key.c | 16 +++++++++++++++- security/keys/keyring.c | 2 +- 3 files changed, 17 insertions(+), 2 deletions(-) diff --git a/include/linux/key.h b/include/linux/key.h index 80d736813b89..4e5baf3e7286 100644 --- a/include/linux/key.h +++ b/include/linux/key.h @@ -300,6 +300,7 @@ extern struct key *key_alloc(struct key_type *type, extern void key_revoke(struct key *key); extern void key_invalidate(struct key *key); struct key *key_get(struct key *key); +struct key *key_try_get(struct key *key); extern void key_put(struct key *key); extern bool key_put_tag(struct key_tag *tag); extern void key_remove_domain(struct key_tag *domain_tag); diff --git a/security/keys/key.c b/security/keys/key.c index 14c7ee77ea15..59cffb6f9b94 100644 --- a/security/keys/key.c +++ b/security/keys/key.c @@ -649,6 +649,20 @@ struct key *key_get(struct key *key) } EXPORT_SYMBOL(key_get); =20 +/** + * key_try_get - Get a ref on a key if its refcount is not non-zero. + * @key: The key to get a reference on. + * + * Get a reference on a key unless it has no references and return true if + * successful. @key must not be NULL. + */ +struct key *key_try_get(struct key *key) +{ + if (!refcount_inc_not_zero(&key->usage)) + return NULL; + return key; +} + /** * key_put - Discard a reference to a key. * @key: The key to discard a reference from. @@ -709,7 +723,7 @@ struct key *key_lookup(key_serial_t id) /* A key is allowed to be looked up only if someone still owns a * reference to it - otherwise it's awaiting the gc. */ - if (!refcount_inc_not_zero(&key->usage)) + if (!key_try_get(key)) goto not_found; =20 error: diff --git a/security/keys/keyring.c b/security/keys/keyring.c index e77d927f1d4d..a09a4c2b1bcb 100644 --- a/security/keys/keyring.c +++ b/security/keys/keyring.c @@ -1174,7 +1174,7 @@ struct key *find_keyring_by_name(const char *name, bo= ol uid_keyring) /* we've got a match but we might end up racing with * key_cleanup() if the keyring is currently 'dead' * (ie. it has a zero usage count) */ - if (!refcount_inc_not_zero(&keyring->usage)) + if (!key_try_get(keyring)) continue; keyring->last_used_at =3D ktime_get_real_seconds(); goto out; From nobody Sun Feb 8 23:25:43 2026 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id C4FA8199FC3 for ; Wed, 21 Aug 2024 12:36:48 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1724243811; cv=none; b=eDOdGOfkdKyMw4QaXI1RMl2CMnywZNkReuG7xhtbwNTZvHmFqQaL5prJuxMFmBgPjgxETiWeA1x0zeGPE6sZ4RWOoyZW2qikvNJipL2DHfKIMDV/Y6WsMJmLTMCAyEUH/IPnDLz28XFeWJJ11qeMVA8uD7Mqw+O8S66J4//BWiM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1724243811; c=relaxed/simple; bh=X5nsFJTEE8s4DdMQ/RXYKMuPcMF2/FYLF2Uhe2bQ7W4=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=JiO20ky8T+DAbKNYEfjjJd03/L+RK3RE1DZBO3DBCoMH0258z4dSFm7dvTIzDLiV4ApZayWsviZvLXttkE+aJhXbhZzYK6QxdcPgF6PiyOicisJ52Dmb7TBdZa9sbjJ1OTvJyP7eYD2x78nUky6Pjm8BUGFwG9uPtxZzle97ck8= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=ZwyekCbQ; arc=none smtp.client-ip=170.10.129.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="ZwyekCbQ" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1724243807; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=80E3kMIdbPaX7pELb4jvn2qp6xAsau9eGcp/ZZnpqD0=; b=ZwyekCbQTPdOeuvuwzTqDMicxUg/9KuKGMZQHzpEYIxxgjL4AnrLH9FvcvA3i2SynEkMqp G5rW8TFdreqAFbTDBzHmTUbtYj0TQfBXC9F11ipss6Wgg30EGnijRjUij3tEgOzB2fbq0y sscO3gwF1SIw3Bhu7OvWgEFpsfP6K4Q= Received: from mx-prod-mc-02.mail-002.prod.us-west-2.aws.redhat.com (ec2-54-186-198-63.us-west-2.compute.amazonaws.com [54.186.198.63]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-177-lER6gba3Poe28hKS5aL1_A-1; Wed, 21 Aug 2024 08:36:46 -0400 X-MC-Unique: lER6gba3Poe28hKS5aL1_A-1 Received: from mx-prod-int-02.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-02.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.15]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-02.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 3AA1F1955D56; Wed, 21 Aug 2024 12:36:45 +0000 (UTC) Received: from warthog.procyon.org.uk.com (unknown [10.42.28.30]) by mx-prod-int-02.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 487801955E8C; Wed, 21 Aug 2024 12:36:43 +0000 (UTC) From: David Howells To: Jarkko Sakkinen Cc: David Howells , keyrings@vger.kernel.org, linux-security-module@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH 7/7] keys: Add tracepoints for the keyrings facility Date: Wed, 21 Aug 2024 13:36:15 +0100 Message-ID: <20240821123616.60401-8-dhowells@redhat.com> In-Reply-To: <20240821123616.60401-1-dhowells@redhat.com> References: <20240821123616.60401-1-dhowells@redhat.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.0 on 10.30.177.15 Content-Type: text/plain; charset="utf-8" Add some tracepoints to aid in debuggin the keyrings facility and applications that use it. A number of events and operations are traceable, including: - Allocation - Refcounting - Instantiation and negative instantiation/rejection - Update - Detection of key being dead - Key quota changes - Key quota failure - Link, unlink and move - Keyring clearance - Revocation and invalidation - Garbage collection Signed-off-by: David Howells cc: Jarkko Sakkinen cc: keyrings@vger.kernel.org cc: linux-security-module@vger.kernel.org Reviewed-by: Jarkko Sakkinen Tested-by: Jarkko Sakkinen --- include/trace/events/key.h | 401 +++++++++++++++++++++++++++++++++++++ security/keys/gc.c | 4 + security/keys/internal.h | 1 + security/keys/key.c | 50 ++++- security/keys/keyctl.c | 2 + security/keys/keyring.c | 27 ++- 6 files changed, 472 insertions(+), 13 deletions(-) create mode 100644 include/trace/events/key.h diff --git a/include/trace/events/key.h b/include/trace/events/key.h new file mode 100644 index 000000000000..b3f8c39cc0e8 --- /dev/null +++ b/include/trace/events/key.h @@ -0,0 +1,401 @@ +/* Keyrings tracepoints + * + * Copyright (C) 2024 Red Hat, Inc. All Rights Reserved. + * Written by David Howells (dhowells@redhat.com) + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public Licence + * as published by the Free Software Foundation; either version + * 2 of the Licence, or (at your option) any later version. + */ +#undef TRACE_SYSTEM +#define TRACE_SYSTEM key + +#if !defined(_TRACE_KEY_H) || defined(TRACE_HEADER_MULTI_READ) +#define _TRACE_KEY_H + +#include + +struct key; + +/* + * Declare tracing information enums and their string mappings for display. + */ +#define key_ref_traces \ + EM(key_trace_ref_alloc, "ALLOC") \ + EM(key_trace_ref_free, "FREE ") \ + EM(key_trace_ref_gc, "GC ") \ + EM(key_trace_ref_get, "GET ") \ + EM(key_trace_ref_put, "PUT ") \ + E_(key_trace_ref_try_get, "GET ") + +#define key_dead_traces \ + EM(key_trace_dead_type_removed, "TYPR") \ + EM(key_trace_dead_domain_removed, "DOMR") \ + EM(key_trace_dead_expired, "EXPD") \ + E_(key_trace_dead_invalidated, "INVL") + +/* + * Generate enums for tracing information. + */ +#ifndef __NETFS_DECLARE_TRACE_ENUMS_ONCE_ONLY +#define __NETFS_DECLARE_TRACE_ENUMS_ONCE_ONLY + +#undef EM +#undef E_ +#define EM(a, b) a, +#define E_(a, b) a + +enum key_dead_trace { key_dead_traces } __mode(byte); +enum key_ref_trace { key_ref_traces } __mode(byte); + +#endif /* end __RXRPC_DECLARE_TRACE_ENUMS_ONCE_ONLY */ + +/* + * Export enum symbols via userspace. + */ +#undef EM +#undef E_ +#define EM(a, b) TRACE_DEFINE_ENUM(a); +#define E_(a, b) TRACE_DEFINE_ENUM(a); + +key_dead_traces; +key_ref_traces; + +/* + * Now redefine the EM() and E_() macros to map the enums to the strings t= hat + * will be printed in the output. + */ +#undef EM +#undef E_ +#define EM(a, b) { a, b }, +#define E_(a, b) { a, b } + +TRACE_EVENT(key_alloc, + TP_PROTO(const struct key *key), + + TP_ARGS(key), + + TP_STRUCT__entry( + __field(key_serial_t, key) + __field(uid_t, uid) + __array(char, type, 8) + __array(char, desc, 24) + ), + + TP_fast_assign( + __entry->key =3D key->serial; + __entry->uid =3D from_kuid(&init_user_ns, key->uid); + strncpy(__entry->type, key->type->name, sizeof(__entry->type) - 1); + strncpy(__entry->desc, key->description ?: "", sizeof(__entry->desc)= - 1); + __entry->type[sizeof(__entry->type) - 1] =3D 0; + __entry->desc[sizeof(__entry->desc) - 1] =3D 0; + ), + + TP_printk("key=3D%08x uid=3D%08x t=3D%s d=3D%s", + __entry->key, + __entry->uid, + __entry->type, + __entry->desc) + ); + +TRACE_EVENT(key_ref, + TP_PROTO(key_serial_t key, unsigned int ref, enum key_ref_trace trace, + const void *where), + + TP_ARGS(key, ref, trace, where), + + TP_STRUCT__entry( + __field(key_serial_t, key) + __field(enum key_ref_trace, trace) + __field(unsigned int, ref) + __field(const void *, where) + ), + + TP_fast_assign( + __entry->key =3D key; + __entry->trace =3D trace; + __entry->ref =3D ref; + __entry->where =3D where; + ), + + TP_printk("key=3D%08x %s r=3D%d pc=3D%pSR", + __entry->key, + __print_symbolic(__entry->trace, key_ref_traces), + __entry->ref, + __entry->where) + ); + +TRACE_EVENT(key_instantiate, + TP_PROTO(const struct key *key, const struct key_preparsed_payload *p= rep), + + TP_ARGS(key, prep), + + TP_STRUCT__entry( + __field(key_serial_t, key) + __field(unsigned int, datalen) + __field(unsigned int, quotalen) + ), + + TP_fast_assign( + __entry->key =3D key->serial; + __entry->datalen =3D prep->datalen; + __entry->quotalen =3D prep->quotalen; + ), + + TP_printk("key=3D%08x dlen=3D%u qlen=3D%u", + __entry->key, + __entry->datalen, + __entry->quotalen) + ); + +TRACE_EVENT(key_reject, + TP_PROTO(const struct key *key, int error), + + TP_ARGS(key, error), + + TP_STRUCT__entry( + __field(key_serial_t, key) + __field(int, error) + ), + + TP_fast_assign( + __entry->key =3D key->serial; + __entry->error =3D error; + ), + + TP_printk("key=3D%08x err=3D%d", + __entry->key, + __entry->error) + ); + +TRACE_EVENT(key_update, + TP_PROTO(const struct key *key, const struct key_preparsed_payload *p= rep), + + TP_ARGS(key, prep), + + TP_STRUCT__entry( + __field(key_serial_t, key) + __field(unsigned int, datalen) + __field(unsigned int, quotalen) + ), + + TP_fast_assign( + __entry->key =3D key->serial; + __entry->datalen =3D prep->datalen; + __entry->quotalen =3D prep->quotalen; + ), + + TP_printk("key=3D%08x dlen=3D%u qlen=3D%u", + __entry->key, + __entry->datalen, + __entry->quotalen) + ); + +TRACE_EVENT(key_dead, + TP_PROTO(const struct key *key, enum key_dead_trace trace), + + TP_ARGS(key, trace), + + TP_STRUCT__entry( + __field(key_serial_t, key) + __field(enum key_dead_trace, trace) + ), + + TP_fast_assign( + __entry->key =3D key->serial; + __entry->trace =3D trace; + ), + + TP_printk("key=3D%08x %s", + __entry->key, + __print_symbolic(__entry->trace, key_dead_traces)) + ); + +TRACE_EVENT(key_quota, + TP_PROTO(const struct key_user *user, int change_keys, int change_byt= es), + + TP_ARGS(user, change_keys, change_bytes), + + TP_STRUCT__entry( + __field(uid_t, uid) + __field(unsigned int, nkeys) + __field(unsigned int, nikeys) + __field(unsigned int, qnkeys) + __field(unsigned int, qnbytes) + __field(int, change_keys) + __field(int, change_bytes) + ), + + TP_fast_assign( + __entry->uid =3D from_kuid(&init_user_ns, user->uid); + __entry->nkeys =3D atomic_read(&user->nkeys); + __entry->nikeys =3D atomic_read(&user->nikeys); + __entry->qnkeys =3D user->qnkeys; + __entry->qnbytes =3D user->qnbytes; + __entry->change_keys =3D change_keys; + __entry->change_bytes =3D change_bytes; + ), + + TP_printk("uid=3D%d nkeys=3D%u/%u qkeys=3D%u qpay=3D%u ckeys=3D%d cpa= y=3D%d", + __entry->uid, + __entry->nikeys, __entry->nkeys, + __entry->qnkeys, + __entry->qnbytes, + __entry->change_keys, __entry->change_bytes) + ); + +TRACE_EVENT(key_edquot, + TP_PROTO(const struct key_user *user, unsigned int maxkeys, + unsigned int maxbytes, unsigned int reqbytes), + + TP_ARGS(user, maxkeys, maxbytes, reqbytes), + + TP_STRUCT__entry( + __field(uid_t, uid) + __field(unsigned int, nkeys) + __field(unsigned int, nikeys) + __field(unsigned int, qnkeys) + __field(unsigned int, qnbytes) + __field(unsigned int, maxkeys) + __field(unsigned int, maxbytes) + __field(unsigned int, reqbytes) + ), + + TP_fast_assign( + __entry->uid =3D from_kuid(&init_user_ns, user->uid); + __entry->nkeys =3D atomic_read(&user->nkeys); + __entry->nikeys =3D atomic_read(&user->nikeys); + __entry->qnkeys =3D user->qnkeys; + __entry->qnbytes =3D user->qnbytes; + __entry->maxkeys =3D maxkeys; + __entry->maxbytes =3D maxbytes; + __entry->reqbytes =3D reqbytes; + ), + + TP_printk("u=3D%d nkeys=3D%u/%u qkeys=3D%u/%u qpay=3D%u/%u cpay=3D%u", + __entry->uid, + __entry->nikeys, __entry->nkeys, + __entry->qnkeys, __entry->maxkeys, + __entry->qnbytes, __entry->maxbytes, + __entry->reqbytes) + ); + +TRACE_EVENT(key_link, + TP_PROTO(const struct key *keyring, const struct key *key), + + TP_ARGS(keyring, key), + + TP_STRUCT__entry( + __field(key_serial_t, keyring) + __field(key_serial_t, key) + ), + + TP_fast_assign( + __entry->keyring =3D keyring->serial; + __entry->key =3D key->serial; + ), + + TP_printk("key=3D%08x to=3D%08x", + __entry->key, __entry->keyring) + ); + +TRACE_EVENT(key_unlink, + TP_PROTO(const struct key *keyring, const struct key *key), + + TP_ARGS(keyring, key), + + TP_STRUCT__entry( + __field(key_serial_t, keyring) + __field(key_serial_t, key) + ), + + TP_fast_assign( + __entry->keyring =3D keyring->serial; + __entry->key =3D key->serial; + ), + + TP_printk("key=3D%08x from=3D%08x", + __entry->key, __entry->keyring) + ); + +TRACE_EVENT(key_move, + TP_PROTO(const struct key *from, const struct key *to, + const struct key *key), + + TP_ARGS(from, to, key), + + TP_STRUCT__entry( + __field(key_serial_t, from) + __field(key_serial_t, to) + __field(key_serial_t, key) + ), + + TP_fast_assign( + __entry->from =3D from->serial; + __entry->to =3D to->serial; + __entry->key =3D key->serial; + ), + + TP_printk("key=3D%08x from=3D%08x to=3D%08x", + __entry->key, __entry->from, __entry->to) + ); + +TRACE_EVENT(key_clear, + TP_PROTO(const struct key *keyring), + TP_ARGS(keyring), + TP_STRUCT__entry( + __field(key_serial_t, keyring) + ), + TP_fast_assign( + __entry->keyring =3D keyring->serial; + ), + TP_printk("key=3D%08x", + __entry->keyring) + ); + +TRACE_EVENT(key_revoke, + TP_PROTO(const struct key *key), + TP_ARGS(key), + TP_STRUCT__entry( + __field(key_serial_t, key) + ), + TP_fast_assign( + __entry->key =3D key->serial; + ), + TP_printk("key=3D%08x", + __entry->key) + ); + +TRACE_EVENT(key_invalidate, + TP_PROTO(const struct key *key), + TP_ARGS(key), + TP_STRUCT__entry( + __field(key_serial_t, key) + ), + TP_fast_assign( + __entry->key =3D key->serial; + ), + TP_printk("key=3D%08x", + __entry->key) + ); + +TRACE_EVENT(key_gc, + TP_PROTO(const struct key *key), + TP_ARGS(key), + TP_STRUCT__entry( + __field(key_serial_t, key) + ), + TP_fast_assign( + __entry->key =3D key->serial; + ), + TP_printk("key=3D%08x", + __entry->key) + ); + +#undef EM +#undef E_ +#endif /* _TRACE_KEY_H */ + +/* This part must be outside protection */ +#include diff --git a/security/keys/gc.c b/security/keys/gc.c index 7d687b0962b1..47f857653854 100644 --- a/security/keys/gc.c +++ b/security/keys/gc.c @@ -141,6 +141,7 @@ static noinline void key_gc_unused_keys(struct list_hea= d *keys) =20 list_del(&key->graveyard_link); =20 + trace_key_gc(key); kdebug("- %u", key->serial); key_check(key); =20 @@ -163,6 +164,8 @@ static noinline void key_gc_unused_keys(struct list_hea= d *keys) key_put_tag(key->domain_tag); kfree(key->description); =20 + trace_key_ref(key->serial, refcount_read(&key->usage), + key_trace_ref_free, 0); memzero_explicit(key, sizeof(*key)); kmem_cache_free(key_jar, key); } @@ -331,6 +334,7 @@ static void key_garbage_collector(struct work_struct *w= ork) */ found_unreferenced_key: kdebug("unrefd key %d", key->serial); + trace_key_ref(key->serial, refcount_read(&key->usage), key_trace_ref_gc, = 0); rb_erase(&key->serial_node, &key_serial_tree); spin_unlock(&key_serial_lock); =20 diff --git a/security/keys/internal.h b/security/keys/internal.h index 33c929a6bb97..87600683a7f5 100644 --- a/security/keys/internal.h +++ b/security/keys/internal.h @@ -20,6 +20,7 @@ #include #include #include +#include =20 struct iovec; =20 diff --git a/security/keys/key.c b/security/keys/key.c index 59cffb6f9b94..abb8d60a0dc2 100644 --- a/security/keys/key.c +++ b/security/keys/key.c @@ -14,6 +14,7 @@ #include #include #include +#define CREATE_TRACE_POINTS #include "internal.h" =20 struct kmem_cache *key_jar; @@ -264,12 +265,15 @@ struct key *key_alloc(struct key_type *type, const ch= ar *desc, if (!(flags & KEY_ALLOC_QUOTA_OVERRUN)) { if (user->qnkeys + 1 > maxkeys || user->qnbytes + quotalen > maxbytes || - user->qnbytes + quotalen < user->qnbytes) + user->qnbytes + quotalen < user->qnbytes) { + trace_key_edquot(user, maxkeys, maxbytes, quotalen); goto no_quota; + } } =20 user->qnkeys++; user->qnbytes +=3D quotalen; + trace_key_quota(user, +1, quotalen); spin_unlock_irqrestore(&user->lock, irqflags); } =20 @@ -319,7 +323,10 @@ struct key *key_alloc(struct key_type *type, const cha= r *desc, /* publish the key by giving it a serial number */ refcount_inc(&key->domain_tag->usage); atomic_inc(&user->nkeys); + trace_key_quota(user, 0, 0); key_alloc_serial(key); + trace_key_alloc(key); + trace_key_ref(key->serial, 1, key_trace_ref_alloc, __builtin_return_addre= ss(0)); =20 error: return key; @@ -331,6 +338,7 @@ struct key *key_alloc(struct key_type *type, const char= *desc, spin_lock_irqsave(&user->lock, irqflags); user->qnkeys--; user->qnbytes -=3D quotalen; + trace_key_quota(user, -1, -quotalen); spin_unlock_irqrestore(&user->lock, irqflags); } key_user_put(user); @@ -344,6 +352,7 @@ struct key *key_alloc(struct key_type *type, const char= *desc, spin_lock_irqsave(&user->lock, irqflags); user->qnkeys--; user->qnbytes -=3D quotalen; + trace_key_quota(user, -1, quotalen); spin_unlock_irqrestore(&user->lock, irqflags); } key_user_put(user); @@ -388,11 +397,13 @@ int key_payload_reserve(struct key *key, size_t datal= en) if (delta > 0 && (key->user->qnbytes + delta > maxbytes || key->user->qnbytes + delta < key->user->qnbytes)) { + trace_key_edquot(key->user, 0, maxbytes, datalen); ret =3D -EDQUOT; } else { key->user->qnbytes +=3D delta; key->quotalen +=3D delta; + trace_key_quota(key->user, 0, delta); } spin_unlock_irqrestore(&key->user->lock, flags); } @@ -447,6 +458,7 @@ static int __key_instantiate_and_link(struct key *key, if (ret =3D=3D 0) { /* mark the key as being instantiated */ atomic_inc(&key->user->nikeys); + trace_key_instantiate(key, prep); mark_key_instantiated(key, 0); notify_key(key, NOTIFY_KEY_INSTANTIATED, 0); =20 @@ -604,6 +616,7 @@ int key_reject_and_link(struct key *key, if (key->state =3D=3D KEY_IS_UNINSTANTIATED) { /* mark the key as being negatively instantiated */ atomic_inc(&key->user->nikeys); + trace_key_reject(key, -error); mark_key_instantiated(key, -error); notify_key(key, NOTIFY_KEY_INSTANTIATED, -error); key_set_expiry(key, ktime_get_real_seconds() + timeout); @@ -641,10 +654,15 @@ EXPORT_SYMBOL(key_reject_and_link); * * Get a reference on a key, if not NULL, and return the parameter. */ -struct key *key_get(struct key *key) +noinline struct key *key_get(struct key *key) { - if (key) - refcount_inc(&key->usage); + if (key) { + int r; + + __refcount_inc(&key->usage, &r); + trace_key_ref(key->serial, r + 1, key_trace_ref_get, + __builtin_return_address(0)); + } return key; } EXPORT_SYMBOL(key_get); @@ -656,10 +674,14 @@ EXPORT_SYMBOL(key_get); * Get a reference on a key unless it has no references and return true if * successful. @key must not be NULL. */ -struct key *key_try_get(struct key *key) +noinline struct key *key_try_get(struct key *key) { - if (!refcount_inc_not_zero(&key->usage)) + int r; + + if (!__refcount_inc_not_zero(&key->usage, &r)) return NULL; + trace_key_ref(key->serial, r + 1, key_trace_ref_try_get, + __builtin_return_address(0)); return key; } =20 @@ -671,12 +693,19 @@ struct key *key_try_get(struct key *key) * schedule the cleanup task to come and pull it out of the tree in process * context at some later time. */ -void key_put(struct key *key) +noinline void key_put(struct key *key) { if (key) { + key_serial_t serial =3D key->serial; + bool zero; + int r; + key_check(key); =20 - if (refcount_dec_and_test(&key->usage)) { + zero =3D __refcount_dec_and_test(&key->usage, &r); + trace_key_ref(serial, r - 1, key_trace_ref_put, + __builtin_return_address(0)); + if (zero) { unsigned long flags; =20 /* deal with the user's key tracking and quota */ @@ -684,6 +713,7 @@ void key_put(struct key *key) spin_lock_irqsave(&key->user->lock, flags); key->user->qnkeys--; key->user->qnbytes -=3D key->quotalen; + trace_key_quota(key->user, -1, -key->quotalen); spin_unlock_irqrestore(&key->user->lock, flags); } schedule_work(&key_gc_work); @@ -807,6 +837,7 @@ static inline key_ref_t __key_update(key_ref_t key_ref, ret =3D key->type->update(key, prep); if (ret =3D=3D 0) { /* Updating a negative key positively instantiates it */ + trace_key_update(key, prep); mark_key_instantiated(key, 0); notify_key(key, NOTIFY_KEY_UPDATED, 0); } @@ -1131,6 +1162,7 @@ int key_update(key_ref_t key_ref, const void *payload= , size_t plen) ret =3D key->type->update(key, &prep); if (ret =3D=3D 0) { /* Updating a negative key positively instantiates it */ + trace_key_update(key, &prep); mark_key_instantiated(key, 0); notify_key(key, NOTIFY_KEY_UPDATED, 0); } @@ -1166,6 +1198,7 @@ void key_revoke(struct key *key) */ down_write_nested(&key->sem, 1); if (!test_and_set_bit(KEY_FLAG_REVOKED, &key->flags)) { + trace_key_revoke(key); notify_key(key, NOTIFY_KEY_REVOKED, 0); if (key->type->revoke) key->type->revoke(key); @@ -1198,6 +1231,7 @@ void key_invalidate(struct key *key) if (!test_bit(KEY_FLAG_INVALIDATED, &key->flags)) { down_write_nested(&key->sem, 1); if (!test_and_set_bit(KEY_FLAG_INVALIDATED, &key->flags)) { + trace_key_invalidate(key); notify_key(key, NOTIFY_KEY_INVALIDATED, 0); key_schedule_gc_links(); } diff --git a/security/keys/keyctl.c b/security/keys/keyctl.c index ab927a142f51..f2ef898209e6 100644 --- a/security/keys/keyctl.c +++ b/security/keys/keyctl.c @@ -1020,11 +1020,13 @@ long keyctl_chown_key(key_serial_t id, uid_t user, = gid_t group) =20 newowner->qnkeys++; newowner->qnbytes +=3D key->quotalen; + trace_key_quota(newowner, +1, key->quotalen); spin_unlock_irqrestore(&newowner->lock, flags); =20 spin_lock_irqsave(&key->user->lock, flags); key->user->qnkeys--; key->user->qnbytes -=3D key->quotalen; + trace_key_quota(newowner, -1, -key->quotalen); spin_unlock_irqrestore(&key->user->lock, flags); } =20 diff --git a/security/keys/keyring.c b/security/keys/keyring.c index a09a4c2b1bcb..d67a60142318 100644 --- a/security/keys/keyring.c +++ b/security/keys/keyring.c @@ -1368,6 +1368,7 @@ void __key_link(struct key *keyring, struct key *key, struct assoc_array_edit **_edit) { key_get(key); + trace_key_link(keyring, key); assoc_array_insert_set_object(*_edit, keyring_key_to_ptr(key)); assoc_array_apply_edit(*_edit); *_edit =3D NULL; @@ -1506,6 +1507,7 @@ static int __key_unlink_begin(struct key *keyring, st= ruct key *key, static void __key_unlink(struct key *keyring, struct key *key, struct assoc_array_edit **_edit) { + trace_key_unlink(keyring, key); assoc_array_apply_edit(*_edit); notify_key(keyring, NOTIFY_KEY_UNLINKED, key_serial(key)); *_edit =3D NULL; @@ -1625,6 +1627,7 @@ int key_move(struct key *key, if (ret < 0) goto error; =20 + trace_key_move(from_keyring, to_keyring, key); __key_unlink(from_keyring, key, &from_edit); __key_link(to_keyring, key, &to_edit); error: @@ -1654,6 +1657,7 @@ int keyring_clear(struct key *keyring) =20 down_write(&keyring->sem); =20 + trace_key_clear(keyring); edit =3D assoc_array_clear(&keyring->keys, &keyring_assoc_array_ops); if (IS_ERR(edit)) { ret =3D PTR_ERR(edit); @@ -1697,14 +1701,27 @@ static bool key_is_dead(const struct key *key, time= 64_t limit) if (expiry !=3D TIME64_MAX) { if (!(key->type->flags & KEY_TYPE_INSTANT_REAP)) expiry +=3D key_gc_delay; - if (expiry <=3D limit) + if (expiry <=3D limit) { + trace_key_dead(key, key_trace_dead_expired); return true; + } + } + + if (test_bit(KEY_FLAG_DEAD, &key->flags)) { + trace_key_dead(key, key_trace_dead_type_removed); + return true; + } + + if (test_bit(KEY_FLAG_INVALIDATED, &key->flags)) { + trace_key_dead(key, key_trace_dead_invalidated); + return true; } =20 - return - key->flags & ((1 << KEY_FLAG_DEAD) | - (1 << KEY_FLAG_INVALIDATED)) || - key->domain_tag->removed; + if (key->domain_tag->removed) { + trace_key_dead(key, key_trace_dead_domain_removed); + return true; + } + return false; } =20 static bool keyring_gc_select_iterator(void *object, void *iterator_data)