From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.210.72 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1795723266; b=SP9ZK/ACfUZtD++tKPyJHID8sCah2WayEkznooOkt9X97K1rn5IRCTbZzx0urajojbb7 VvjNLQxOHsnAbFb6cjW5sIWsdTHc3PFADKyT6Yu9ScaqgG06HC3Jy6bekl1UhLgFXYwj7 HGBbyT7hdAKxLO+kgwt4y9s694VurvDqxYKg3roLmk/zxHkmPFZXXrPc6PEWX1vdB/zvF IET8mSa6K1PM64BE30xoExftIus3rKUGIZ8oOQfluKno/BL7fS2IeKJtTS8aSmCtQlkxB YpIDj4eNm2OeueF7DAdYwNev37JC5R4Za4qAKWsiIWDRb9XsQYb3DlmT1gUdu+isU5g== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795723266; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Forwarded-Encrypted:X-Gm-Message-State:X-Received:Date:Mime-Version: X-Mailer:Message-ID:Subject:From:To:Cc:Content-Type; bh=Lsmu86na7LP1yXQ9d2E+j8k+hfLm8TriJbzArNXJkMM=; b=XSIROFSvCfq6dcXKZTS/9D3moNJA0WZ/+ZDCpQl8cgSMjw55z5tS6Cn3LHeadvC9ae6T cYAUwGMslDXqJeC/v9AVf3bmxe0B3PAbDBYOUZOYDXHyoYVO9kTXgi32p3QuI5s9xDhx6 YbJzexr4tEcksP7n/namTMfmbIzJIWdGsJ3iS9qmgdQrNPMkHt7YN5sepPStNDNCi+2fe pi8SYUsXBh1Zcf7+FFNos/tFbRG+GPW4bNMVa8LjRn/2Fynb9wHpeb4vJfzGK0a5BXvkT HoEdsyDac3NxPl/5Xl3jU76o2E5kVkZqShomuLggfcKCxL9uuiFbvczICSMMOo9Knig== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=google.com; spf=pass smtp.mailfrom=flex--zhenwenliang.bounces.google.com; dkim=pass header.d=google.com header.i=@google.com header.a=rsa-sha256 header.s=20260707 header.b="QxbdqK/L"; arc=none smtp.remote-ip=209.85.210.72 Received: from mail-ot1-f72.google.com (mail-ot1-f72.google.com [209.85.210.72]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 584AD606A32A for ; Thu, 26 Nov 2026 20:01:06 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 584AD606A32A Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--zhenwenliang.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.a=rsa-sha256 header.s=20260707 header.b=QxbdqK/L Received: by mail-ot1-f72.google.com with SMTP id 46e09a7af769-86313f7f4faso7351448a34.1 for ; Thu, 26 Nov 2026 12:01:06 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20260707; t=1795723265; x=1796328065; darn=vger.kernel.org; h=content-type:cc:to:from:subject:message-id:mime-version:date:from :to:cc:subject:date:message-id:reply-to:content-type; bh=Lsmu86na7LP1yXQ9d2E+j8k+hfLm8TriJbzArNXJkMM=; b=QxbdqK/LnysLZNk3giLpFCC6miNSsgKre4H+NTcH1r+SzCQAHgLQjJPSGK2ObW/qzN GWtcny/72I4JywhjriFo845hSsRIgBL8Qn61amypgK7xmosCZbyAMNGh9+wNqkeQAS/3 e3yB9Em15DwqFj7E6sJ+kanQZEH6Zj+3w87sJUaABY/62hZYCS7LPZOZN8GzYybXkyiK ITCRd3uCHyNC3U9n3jrN5z41tP5KIGzy11YUBY3dzvJBqqLf+aeVTvHV5wYx6p4LEatc Mu/1V27sS6i1N/fPPvoWuyTpe+jPZ1ldCSUHCe8jYC8zZuoJvuDctYZAsRcZNrw8xa+R f7dQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1795723265; x=1796328065; h=content-type:cc:to:from:subject:message-id:mime-version:date :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to :content-type; bh=Lsmu86na7LP1yXQ9d2E+j8k+hfLm8TriJbzArNXJkMM=; b=yK23g0ygw7vCVN6xUhyWCd2/NGNcZPQhAHkuje+mQq8/T07WV2J/zpHdIKhUnkTtKe m55sq5PHCMbsoKY7a3RW+hAGfSiI3KgysitiqK7nNSAKAOae6DGTfFACFJkjIn29tC4N U5n8zeEYw0LxbbHlwKr3dhCg4+PqzQtF7EAO3jE/ay8J4PEfBVYvXwr8nedO2xDbFfmL miLtE3ZMkpInj+stBiJF41K0P6pdvqgb72sQbz7Yi8qgex+fA+60El/l94mk7s2ljYIe fyvX0j8O+3mm+Ztf34/MdFaP6lknvAdntlvvzJX6dKDZjHUDbWXHguxoYf/mKzOKmR3T s0Yg== X-Forwarded-Encrypted: i=1; AL+hErHEOOWzgIHehjy5eAbV/xb3ligwrGFmUic7dJKoY0GwDyWAsBdzj6J3V7DMnQcl1OF/SyCSiVDpvmTFmFqW9Ms=@vger.kernel.org X-Gm-Message-State: APuU4Z8UclMn32O823MqViA98inTK5LD8oF3Czj0Y3HCXyO0kcHmLe/0 Ve263DJ/X1t8Gu9LpheRZ/xSd0bdYAZqpdrFf4Mh2F1KSe01OhLVsIJAI5gshIwyZMIvj4RUf2D zIXG2XHzaoUBvhXzZvkK4jzxYk23bhw== X-Received: from ilei11-n2.prod.google.com ([2002:a05:6e02:61cb:20b0:54e:86af:fe5d]) (user=zhenwenliang job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6808:4f2c:b0:4d6:90f4:48b4 with SMTP id 5614622812f47-57a0cae5223mr24227154b6e.54.1795723264869; Thu, 26 Nov 2026 12:01:04 -0800 (PST) Date: Thu, 26 Nov 2026 20:01:02 +0000 X-Mailing-List: linux-integrity@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list Mime-Version: 1.0 X-Mailer: git-send-email 2.56.1.839.g22a8290db9-goog Message-ID: <20261126200103.2806716-1-zhenwenliang@google.com> Subject: [PATCH 1/2] KEYS: encrypted: Wait for RCU callbacks before module unload From: Zhenwen Liang To: Jarkko Sakkinen , Mimi Zohar , David Howells , James Bottomley Cc: Paul Moore , James Morris , "Serge E. Hallyn" , keyrings@vger.kernel.org, linux-integrity@vger.kernel.org, linux-security-module@vger.kernel.org, stable@vger.kernel.org Content-Type: text/plain; charset="UTF-8" encrypted_update() frees the old payload with call_rcu(), and the callback, encrypted_rcu_free(), is part of encrypted-keys.ko. cleanup_encrypted() only calls unregister_key_type(), which does not wait for callbacks that are already queued. If the module is removed shortly after a KEYCTL_UPDATE, the RCU core later calls encrypted_rcu_free() in freed module memory. No new callback can be queued once unregister_key_type() has returned: key_create_or_update() holds key_types_sem across __key_update(), key_update() checks key->type->update with key->sem held, and the garbage collector switches every remaining key of the type to key_type_dead under key->sem before unregister_key_type() returns. An rcu_barrier() at that point therefore waits for all pending encrypted_rcu_free() callbacks, as Documentation/RCU/rcubarrier.rst describes for modules that use call_rcu(). Tested in QEMU (x86_64, TCG) on mainline (7.4-rc4+) with KASAN, ENCRYPTED_KEYS=m and CONFIG_RCU_LAZY=y, so that callbacks are deferred: add an encrypted key, update it 20 times, then call delete_module() right away. Without this patch, the rcuop kthread hit int3 at encrypted_rcu_free+0x1 in the freed module text in 5 of 5 runs. With it, 5 of 5 runs were clean, and the unload took about 30 ms longer. The problem was found, and this patch prepared, with the help of a formal analysis tool we are developing that uses AI assistance; I have reviewed the analysis and the patch. Fixes: 7e70cb497850 ("keys: add new key-type encrypted") Link: https://lore.kernel.org/all/20261126023538.4160950-1-zhenwenliang@google.com/ Cc: stable@vger.kernel.org Signed-off-by: Zhenwen Liang --- security/keys/encrypted-keys/encrypted.c | 1 + 1 file changed, 1 insertion(+) diff --git a/security/keys/encrypted-keys/encrypted.c b/security/keys/encrypted-keys/encrypted.c index 63f0245e6f74..fca809819f8e 100644 --- a/security/keys/encrypted-keys/encrypted.c +++ b/security/keys/encrypted-keys/encrypted.c @@ -1000,6 +1000,7 @@ static int __init init_encrypted(void) static void __exit cleanup_encrypted(void) { unregister_key_type(&key_type_encrypted); + rcu_barrier(); } late_initcall(init_encrypted);