From nobody Sun Feb 8 11:44:30 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 48693C83F3E for ; Thu, 31 Aug 2023 20:37:31 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1347431AbjHaUhc (ORCPT ); Thu, 31 Aug 2023 16:37:32 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38502 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1347432AbjHaUh3 (ORCPT ); Thu, 31 Aug 2023 16:37:29 -0400 Received: from mail-yb1-xb4a.google.com (mail-yb1-xb4a.google.com [IPv6:2607:f8b0:4864:20::b4a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id ACCEB10C0 for ; Thu, 31 Aug 2023 13:37:25 -0700 (PDT) Received: by mail-yb1-xb4a.google.com with SMTP id 3f1490d57ef6-d745094c496so1030931276.1 for ; Thu, 31 Aug 2023 13:37:25 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1693514245; x=1694119045; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=yBbx4b8+p9YSAXqpoHoy5g7vSJRSgSl+lhiyxSfb/eU=; b=0ZPTG/K7z5/cT3ShYOz0vb8RRH4Nw5JXxa7KNOL84iPd1CZJTHmMWpm+xmKxB9Vedi o6croEnyFXHRTciz3Ezy+AR6G9NjYBw/vR2C12GNnJbH9rG6IxMXq7cU+A/KN30iWzo0 C3kmfe//Nq5Upsh9aBAVG16S3pEbGPBhnd2VcTeSnecYNGtP0waNq6wqZhBL1Mrl3GgV exqBkHKhdfVkahdff25HfoKIw99yBYeqZbnLDr1ofsMz8R8AgCzoXG+wlrl6e83I3qnt WrAnR8VQUb5EsgG9+ZTMMgQOFUFLNAcDHIbyMmbDg+nmGxrxAKF0aoCHA/ey26lhxVZz OfYg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1693514245; x=1694119045; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=yBbx4b8+p9YSAXqpoHoy5g7vSJRSgSl+lhiyxSfb/eU=; b=WIyVjc6NMABng3+8EPU9zgMn2JbXFX5RKzwjFO4wKHOoadD3zxzyZ7Zm4jO1eMNYZC Q8SNE/0ir1/k8Vi8MjVzutFeYeEnmgCcn2wnBh/csm8l8beOZTrjJJ86YJq5oeaoFJZV C2RyTs2YSETPyHCuEXsZF2mMfzJxj9Pl9iaB+CcjMjzu8gNSuEadqkjOQVQJb5D8gA8Z Rn5jVA2oKmptfih3N/CuqXq9DPdnuEgWyFVUCweUn2ANYeQCFnggx03YU07xdCMSU0UA bYpg3UVtF2hBEBwemFCPyxY+q41cY75r246ONB8r+/5U0QSe2Eu6SyK53V1dJxfThYsR iwgQ== X-Gm-Message-State: AOJu0YyqhLqcYcg9vLVHSDbkQO2yZtq9/HZ2x2yYhczNg7Jkj7Ex91dm wp1lOc0oLFX6q0vPxZEvJaoTX+gtcSL+MhCQ X-Google-Smtp-Source: AGHT+IF1LWmDxsSgnrtvI1kzQAdmqG9kkHzoMRjXO6gEE8jOd/mbGmywsGSMUbTy0xnAonh3GTBUSjDNwaJZ1HZV X-Received: from mclapinski.waw.corp.google.com ([2a00:79e0:9b:0:36f8:f0a:6df2:a7d5]) (user=mclapinski job=sendgmr) by 2002:a05:6902:11c9:b0:d20:7752:e384 with SMTP id n9-20020a05690211c900b00d207752e384mr22037ybu.3.1693514245015; Thu, 31 Aug 2023 13:37:25 -0700 (PDT) Date: Thu, 31 Aug 2023 22:36:46 +0200 In-Reply-To: <20230831203647.558079-1-mclapinski@google.com> Mime-Version: 1.0 References: <20230831203647.558079-1-mclapinski@google.com> X-Mailer: git-send-email 2.42.0.283.g2d96d420d3-goog Message-ID: <20230831203647.558079-2-mclapinski@google.com> Subject: [PATCH 1/2] fcntl: add fcntl(F_CHECK_ORIGINAL_MEMFD) From: Michal Clapinski To: Jeff Layton , Chuck Lever , Alexander Viro , Christian Brauner , Shuah Khan , Andrew Morton , Jeff Xu , Aleksa Sarai , Daniel Verkamp , Kees Cook , Marc Dionne , linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org Cc: Michal Clapinski Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Add a way to check if an fd points to the memfd's original open fd (the one created by memfd_create). Useful because only the original open fd can be both writable and executable. Signed-off-by: Michal Clapinski --- fs/fcntl.c | 3 +++ include/uapi/linux/fcntl.h | 9 +++++++++ 2 files changed, 12 insertions(+) diff --git a/fs/fcntl.c b/fs/fcntl.c index e871009f6c88..301527e07a4d 100644 --- a/fs/fcntl.c +++ b/fs/fcntl.c @@ -419,6 +419,9 @@ static long do_fcntl(int fd, unsigned int cmd, unsigned= long arg, case F_SET_RW_HINT: err =3D fcntl_rw_hint(filp, cmd, arg); break; + case F_CHECK_ORIGINAL_MEMFD: + err =3D !(filp->f_mode & FMODE_WRITER); + break; default: break; } diff --git a/include/uapi/linux/fcntl.h b/include/uapi/linux/fcntl.h index 6c80f96049bd..9a10fe3aafa7 100644 --- a/include/uapi/linux/fcntl.h +++ b/include/uapi/linux/fcntl.h @@ -56,6 +56,15 @@ #define F_GET_FILE_RW_HINT (F_LINUX_SPECIFIC_BASE + 13) #define F_SET_FILE_RW_HINT (F_LINUX_SPECIFIC_BASE + 14) =20 +/* + * Check if the fd points to the memfd's original open fd (the one created= by + * memfd_create). Returns 1 if yes, 0 if no. + * If the fd doesn't point to a memfd, the value should not be interpreted. + * Useful because only the original open fd can be both writable and + * executable. + */ +#define F_CHECK_ORIGINAL_MEMFD (F_LINUX_SPECIFIC_BASE + 15) + /* * Valid hint values for F_{GET,SET}_RW_HINT. 0 is "not set", or can be * used to clear any hints previously set. --=20 2.42.0.283.g2d96d420d3-goog