Received: by 2002:a05:7412:3290:b0:fa:6e18:a558 with SMTP id ev16csp290620rdb; Thu, 25 Jan 2024 15:59:34 -0800 (PST) X-Google-Smtp-Source: AGHT+IHM6x6nOg8y2nLnN6t/dw7jytdYy8cSeueRFd3+8vuPRxB+ACV0EZYxwKn445MQmurfhn+R X-Received: by 2002:a05:6a00:4583:b0:6dd:c41e:3151 with SMTP id it3-20020a056a00458300b006ddc41e3151mr429742pfb.58.1706227174199; Thu, 25 Jan 2024 15:59:34 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1706227174; cv=pass; d=google.com; s=arc-20160816; b=wIaGSUJxJ22Qow7WWFKwjc58lyFp2eVKkbz1XxepyAlb/FhT3XS2BaywlOjp2jenwf WWjjN1MXpcFvM85TalChMXPGvGzyLniky1TjYG5P6d53dowLx/xJiGR6Hezi7FtzAGhm m+89clnnIJXhxvtTQkngpW+0mmq5Bh1iHACTUW/eCdLkGBrIoY/zgNrFwWdUf2e6IAYd e66Y0t44CVxEYmXkhU4T6oFTSyj5XYpenyGkk/fPfG60Ey6nnIAgtKxra4srQCo697IW YMe3OBJq2lu7fy5j0bqoegzMjAMKLCfkZdCCg2Y6U57KlxcrMt757/oOBFxn1DpLv01R dEYA== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from:dkim-signature; bh=6WIn40Y8JYVVNeXMpYpYkZ1/h6sE2JdD3Nzeb6tljs0=; fh=0u4WKmQXLbfFx7dTPMZn/CP58jbWkEI+fyqm6Dolj/A=; b=NHNYz4MvHfsQQBrPAzcoo33fvxzHxlmuHmtpDYdRZNOtnvTJZPxDMMmoYKB6wFviFi uOXsBOZTOXkay/EPFkGROUuDNvKKFVrMJ5wFCMGL7j7pNIvtrDqKQDvhNJ60D427zzwy jMmyX7qXz/ljsUOv2/V2cN78l0EbBoD20vvVzUZONXty5lDpVV2+i2LolnpKUFPnyAKN DeWkXG4h3HvJFCiv8eJO2X2wVZ2d+l8A9yLy7/9B4CcwcvRd579drvOcd1U4hLFOoBTe /FNQJK4J60ia8XkUTMIiByPlFmG/rSlyaR6NQsWQr2flMDimTYq6JXMJzieQiuk4PTn9 ksLg== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=EZE7fGnf; arc=pass (i=1 spf=pass spfdomain=intel.com dkim=pass dkdomain=intel.com dmarc=pass fromdomain=intel.com); spf=pass (google.com: domain of linux-kernel+bounces-39431-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) smtp.mailfrom="linux-kernel+bounces-39431-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Return-Path: Received: from sy.mirrors.kernel.org (sy.mirrors.kernel.org. [147.75.48.161]) by mx.google.com with ESMTPS id q16-20020a638c50000000b005cec871f30dsi91034pgn.271.2024.01.25.15.59.33 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 25 Jan 2024 15:59:34 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-39431-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) client-ip=147.75.48.161; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=EZE7fGnf; arc=pass (i=1 spf=pass spfdomain=intel.com dkim=pass dkdomain=intel.com dmarc=pass fromdomain=intel.com); spf=pass (google.com: domain of linux-kernel+bounces-39431-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) smtp.mailfrom="linux-kernel+bounces-39431-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sy.mirrors.kernel.org (Postfix) with ESMTPS id A308EB26299 for ; Thu, 25 Jan 2024 23:59:10 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id E89541B80A; Thu, 25 Jan 2024 23:57:40 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="EZE7fGnf" Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.8]) (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 E4E3B1A731; Thu, 25 Jan 2024 23:57:37 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=192.198.163.8 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706227059; cv=none; b=Suijg7OzZVS0+loNCeS5bmBKi/ShYpeeLEpH5KuZnP+dCFj+oUPTq7wAqQmVm1rKnV5WnO2nbBi+VN3OMVNsVTpZvnHPtVYzNmGhCtftoMi6dqRPn/kRCDifNrg4X/K76gye6MBNm0Cq1mk6kYvYiLG9Ckvzh8waLsLQXht6dfU= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706227059; c=relaxed/simple; bh=+5d3tyhMXlyo3iELNdYQuZU7PNVS/IBrZgPRKsrWJ48=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=TGxeXw3uG8JgV8TRLfS3zR8D1DiN3HlJ44vI+qCBC1URzKB6fD9kodJ/hMHJYcv7mC5ztlREhZvRRn61G0ZrCqAncqD1acrA/B/N8AAPzxAGf8yZqmD56k3uT4wpPXy6AhUeMd/QvM23rUge2BJRiR+BrLQYMXiqqFzk/VAPcK4= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com; spf=pass smtp.mailfrom=intel.com; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b=EZE7fGnf; arc=none smtp.client-ip=192.198.163.8 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=intel.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1706227058; x=1737763058; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=+5d3tyhMXlyo3iELNdYQuZU7PNVS/IBrZgPRKsrWJ48=; b=EZE7fGnfsoNgF9k6Pv/XgFpvgpV9i6DSLRmSXv4CtHsn0EjKXQYxq6s1 23ADwsV2lr+U1Od9FRlVkUf51x0K+FHbAGyhwszksS6Q6LozfXmJTarXD fOB++HmGNoCd22AlzElgEQVbTiYFApn6xw717FbwPItlovWn/HrL78pEQ 5a4/VxjwEJ9o9YQH4kEUE4+VrffsGBhl1EdDOXfRiYScUWvLdgZmDTd3y W3aGdTfJo99dm8YHjXKKvtTxhUgjNEG5UCm1f4HfWcfQtv6FdEbEb286/ kTjAN7k4p4Fim8XI6ieAtv5Mbrq9vwE77l7Lng4t3R8xAk9bHxIGOtOXF g==; X-IronPort-AV: E=McAfee;i="6600,9927,10964"; a="15867567" X-IronPort-AV: E=Sophos;i="6.05,216,1701158400"; d="scan'208";a="15867567" Received: from fmsmga001.fm.intel.com ([10.253.24.23]) by fmvoesa102.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 25 Jan 2024 15:57:36 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10964"; a="930191101" X-IronPort-AV: E=Sophos;i="6.05,216,1701158400"; d="scan'208";a="930191101" Received: from vcostago-mobl3.jf.intel.com ([10.24.14.99]) by fmsmga001-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 25 Jan 2024 15:57:35 -0800 From: Vinicius Costa Gomes To: brauner@kernel.org, amir73il@gmail.com, hu1.chen@intel.com Cc: miklos@szeredi.hu, malini.bhandaru@intel.com, tim.c.chen@intel.com, mikko.ylinen@intel.com, lizhen.you@intel.com, linux-unionfs@vger.kernel.org, linux-kernel@vger.kernel.org, Vinicius Costa Gomes Subject: [RFC v2 2/4] cred: Add a light version of override/revert_creds() Date: Thu, 25 Jan 2024 15:57:21 -0800 Message-ID: <20240125235723.39507-3-vinicius.gomes@intel.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240125235723.39507-1-vinicius.gomes@intel.com> References: <20240125235723.39507-1-vinicius.gomes@intel.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Add a light version of override/revert_creds(), this should only be used when the credentials in question will outlive the critical section and the critical section doesn't change the ->usage of the credentials. To make their usage less error prone, introduce cleanup guards asto be used like this: guard(cred)(credentials_to_override_and_restore); or this: scoped_guard(cred, credentials_to_override_and_restore) { /* with credentials overridden */ } Signed-off-by: Vinicius Costa Gomes --- include/linux/cred.h | 21 +++++++++++++++++++++ kernel/cred.c | 6 +++--- 2 files changed, 24 insertions(+), 3 deletions(-) diff --git a/include/linux/cred.h b/include/linux/cred.h index 2976f534a7a3..e9f2237e4bf8 100644 --- a/include/linux/cred.h +++ b/include/linux/cred.h @@ -172,6 +172,27 @@ static inline bool cap_ambient_invariant_ok(const struct cred *cred) cred->cap_inheritable)); } +/* + * Override creds without bumping reference count. Caller must ensure + * reference remains valid or has taken reference. Almost always not the + * interface you want. Use override_creds()/revert_creds() instead. + */ +static inline const struct cred *override_creds_light(const struct cred *override_cred) +{ + const struct cred *old = current->cred; + + rcu_assign_pointer(current->cred, override_cred); + return old; +} + +static inline void revert_creds_light(const struct cred *revert_cred) +{ + rcu_assign_pointer(current->cred, revert_cred); +} + +DEFINE_GUARD(cred, const struct cred *, _T = override_creds_light(_T), + revert_creds_light(_T)); + /** * get_new_cred_many - Get references on a new set of credentials * @cred: The new credentials to reference diff --git a/kernel/cred.c b/kernel/cred.c index c033a201c808..f95f71e3ac1d 100644 --- a/kernel/cred.c +++ b/kernel/cred.c @@ -485,7 +485,7 @@ EXPORT_SYMBOL(abort_creds); */ const struct cred *override_creds(const struct cred *new) { - const struct cred *old = current->cred; + const struct cred *old; kdebug("override_creds(%p{%ld})", new, atomic_long_read(&new->usage)); @@ -499,7 +499,7 @@ const struct cred *override_creds(const struct cred *new) * visible to other threads under RCU. */ get_new_cred((struct cred *)new); - rcu_assign_pointer(current->cred, new); + old = override_creds_light(new); kdebug("override_creds() = %p{%ld}", old, atomic_long_read(&old->usage)); @@ -521,7 +521,7 @@ void revert_creds(const struct cred *old) kdebug("revert_creds(%p{%ld})", old, atomic_long_read(&old->usage)); - rcu_assign_pointer(current->cred, old); + revert_creds_light(old); put_cred(override); } EXPORT_SYMBOL(revert_creds); -- 2.43.0