From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-yw1-f175.google.com (mail-yw1-f175.google.com [209.85.128.175]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 6624E3542F6 for ; Sun, 23 Aug 2026 17:05:01 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.175 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1787504704; cv=none; b=r32qRSUdOky45iEhhFn9sT098ZR4FpWYQ/FBFYh3tDgkUVA63HZ8ytQWznmevvjIt5Bfs5FKFi6jKEJb05I9Hwr+e7J33xjz0BQ1iPmXUrCKU6Mo4V8z9FRD961hKVLTcwQvpidDDkobmH77M3UHHT1I7w70NPJH5lUk5WD11rk= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1787504704; c=relaxed/simple; bh=gWZEjagFLSEHCLiuRXV3m9OHpXG30kNMA/tQzzHkKfY=; h=From:To:Cc:Subject:Date:Message-ID:MIME-Version; b=DRsampf/PY+fm01E/a+28bBxW6cS7tQvgThJAzPdpMr/kTg3EnE9sSY64vEvRy1DhcB2FVvdwRC5tShifSr3gQkeUUpXvqrM21ohhy5CZMbiExF3oHSFT8SWkGsDilGOB5ljxFKDDYSedML8zrpubtD9eNYUpc1ZvltrnLWfEU8= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=IYlMljNb; arc=none smtp.client-ip=209.85.128.175 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="IYlMljNb" Received: by mail-yw1-f175.google.com with SMTP id 00721157ae682-836d228798aso1558617b3.2 for ; Sun, 23 Aug 2026 10:05:01 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1787504700; x=1788109500; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to:content-type; bh=7fp7SzY66XTj6zmh2H/e3A8N/ODfvtqZLUkZGsmrEbw=; b=IYlMljNbEZfglzy3U83D6qQhWDLHPfAZnO5R+b4emBXjS+xLb3prctm7I36iFvPIo1 595QCypQs4qwPrZJOdi2eSglflZfby8gLBMhjQBFDArjhF5wB9cxsnhET1Qg+iYStBH9 sqUduKs4wl6vsjP1y5lhphZlryQu5YusJvaUr7hcQeeQwRPt1o1pfsnwaFhnBif8PW17 +GRunEsh8XL9PFwKP0Ql9wTkhUhz28qblVr78G5SLVksNPfyYSSTGwHWEvcDKKmN+m2C EePXAa/7RX3ogc4Sql7v38RDYTkirAjZ6/A/Mx5s5NzzhjPfDw5gIo9wkeqTajB643Es lF9w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1787504700; x=1788109500; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-gg:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to:content-type; bh=7fp7SzY66XTj6zmh2H/e3A8N/ODfvtqZLUkZGsmrEbw=; b=LwbTrN2FYfFC37cKrbDG/xIz/Vz38xULi/JcTgH4hQCugjBwYdjwQb27uQDjavgBvB oRMD8Tq07Xo7z/GNngqM3oDTDTUtO7MEP5yNtpwjaFMFmxN6TZ9062nLdP8mAgAsaHPA F31fmg5eRa9uRAaJS29fzTOYb2zSQkYM2shiilXsTrr0nYnTxHLzX6a1Lt63yK3zX6Gz Pbe+68P7FSTKGVRrTKFCFbOgKpFxUTUn1/1nv3udQJ9ws46qjm+Et9ntMk6kXDxekbkI 4lxWIvTAHLMxWP9IxkZF77fJR6DRSJMvz/t0W0Gx95uvoElROtDVb9zYBCCP6PRNT9ed BaCQ== X-Forwarded-Encrypted: i=1; AHgh+RrkomWSgQIclAXJYttQmSJ3tg35BUjYy4JhDL1ninFlwYZMkWS8yAC6yVGBOeMDQiRKFwqNq9TZ0byaFBU=@vger.kernel.org X-Gm-Message-State: AFuF++kLUwVFA4cCoAP6022+8DYLNMe5ncnoP5zcYcaw5iR4NIM0/fTG C70D45YzX0iG9p0meE0G0DbfM40ce364mVGuAYhTSf+YensUnIQ0BByo X-Gm-Gg: AR+sD134Q064z1OU7cTGvoIrDF3la30FBbx10CjBljjE5UVz48FHK0Y5acJB+zN2Yxd EF0kFC+RCXOvuKWHycofZknmjcZZKprl9AZtX+llhzVfkAocDLNlOEALSyDLQZQQSd3Y3tzjD7T Nq7l4Vo4DHsgHrCsqcWjJ6ulNNgDEOLg8lIRYZgXGr2qDDvuSojns7Ve12LdFcSZE3T3QwTvwzG JT+aPGEY4x9EcCvVH3+4XJF2wuQdYIc+yZorwoChnTcMKORwrk6JRG/xP5trXmb2TRfvf7pEibM BvVwEUW7Yr0x00aHJppXadm8KSVM+AKIvVVO+8FaLi2GmXLeRKMxbPbZXACq0BIoCmd4b6wkn30 1QKo7k7JGm0I6Vqt98GxHL1WwAxWs72JY3I2uUYpY2eyt9e5TW7Z2kX6fVobJnMA4+4v9REpJZ4 fJAPJViKjVQGZ1JLbw1Vkv/P/FjcIR98IUY2hezFNUVh1+oGxFXU66cKtbSIuEAmaHQGklY1USf kPtYVn10axAOa5oi6DMIFhXUc3S2yCvZRdwOrTxdUjLTXv0yAljSm8= X-Received: by 2002:a05:690c:e013:20b0:81f:49e1:1f15 with SMTP id 00721157ae682-849ea01a334mr58148687b3.0.1787504699844; Sun, 23 Aug 2026 10:04:59 -0700 (PDT) Received: from localhost.localdomain (45.78.65.84.16clouds.com. [45.78.65.84]) by smtp.gmail.com with ESMTPSA id 00721157ae682-84ca68c25aasm22529197b3.22.2026.08.23.10.04.55 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 23 Aug 2026 10:04:58 -0700 (PDT) From: Chengfeng Ye To: David Howells , Jarkko Sakkinen , Paul Moore , James Morris , "Serge E. Hallyn" , Serge Hallyn Cc: keyrings@vger.kernel.org, linux-security-module@vger.kernel.org, linux-kernel@vger.kernel.org, Chengfeng Ye Subject: [PATCH] keys: Fix key_user use-after-free during ownership changes Date: Mon, 24 Aug 2026 01:04:48 +0800 Message-ID: <20260823170448.3856516-1-nicoyip.dev@gmail.com> X-Mailer: git-send-email 2.43.0 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit keyctl_chown_key() replaces key->user while holding key->sem and drops the old key_user reference after releasing the semaphore. The /proc/keys iterators and find_keyring_by_name() instead dereference key->user while holding unrelated locks. This allows the following interleaving: CPU 0 (/proc/keys) CPU 1 (KEYCTL_CHOWN) load old key->user replace key->user key_user_put(old) kfree(old) read old->uid Serialize the namespace-mapping reads and the pointer replacement with key_user_lock. This lock already protects final key_user removal, so the old object cannot be freed while its uid is being read. Keep reading the quota-owner UID rather than key->uid because those values can legitimately differ for thread keyrings. Fixes: 454804ab0302 ("keys: make procfiles per-user-namespace") Signed-off-by: Chengfeng Ye --- security/keys/internal.h | 10 ++++++++++ security/keys/keyctl.c | 2 ++ security/keys/keyring.c | 2 +- security/keys/proc.c | 4 ++-- 4 files changed, 15 insertions(+), 3 deletions(-) diff --git a/security/keys/internal.h b/security/keys/internal.h index b7b622bc36a1..741d547ba5c4 100644 --- a/security/keys/internal.h +++ b/security/keys/internal.h @@ -70,6 +70,16 @@ extern struct key_user root_key_user; extern struct key_user *key_user_lookup(kuid_t uid); extern void key_user_put(struct key_user *user); +static inline kuid_t key_user_uid(const struct key *key) +{ + kuid_t uid; + + spin_lock(&key_user_lock); + uid = key->user->uid; + spin_unlock(&key_user_lock); + return uid; +} + /* * Key quota limits. * - root has its own separate limits to everyone else diff --git a/security/keys/keyctl.c b/security/keys/keyctl.c index d14ace88e529..c17924609317 100644 --- a/security/keys/keyctl.c +++ b/security/keys/keyctl.c @@ -1036,8 +1036,10 @@ long keyctl_chown_key(key_serial_t id, uid_t user, gid_t group) atomic_inc(&newowner->nikeys); } + spin_lock(&key_user_lock); zapowner = key->user; key->user = newowner; + spin_unlock(&key_user_lock); key->uid = uid; } diff --git a/security/keys/keyring.c b/security/keys/keyring.c index 15bf4af8f282..49f4be934525 100644 --- a/security/keys/keyring.c +++ b/security/keys/keyring.c @@ -1158,7 +1158,7 @@ struct key *find_keyring_by_name(const char *name, bool uid_keyring) * grants Search permission and that hasn't been revoked */ list_for_each_entry(keyring, &ns->keyring_name_list, name_link) { - if (!kuid_has_mapping(ns, keyring->user->uid)) + if (!kuid_has_mapping(ns, key_user_uid(keyring))) continue; if (test_bit(KEY_FLAG_REVOKED, &keyring->flags)) diff --git a/security/keys/proc.c b/security/keys/proc.c index 4f4e2c1824f1..8d6d26652aab 100644 --- a/security/keys/proc.c +++ b/security/keys/proc.c @@ -68,7 +68,7 @@ static struct rb_node *key_serial_next(struct seq_file *p, struct rb_node *n) n = rb_next(n); while (n) { struct key *key = rb_entry(n, struct key, serial_node); - if (kuid_has_mapping(user_ns, key->user->uid)) + if (kuid_has_mapping(user_ns, key_user_uid(key))) break; n = rb_next(n); } @@ -100,7 +100,7 @@ static struct key *find_ge_key(struct seq_file *p, key_serial_t id) return NULL; for (;;) { - if (kuid_has_mapping(user_ns, minkey->user->uid)) + if (kuid_has_mapping(user_ns, key_user_uid(minkey))) return minkey; n = rb_next(&minkey->serial_node); if (!n) -- 2.43.0