From patchwork Thu Aug 31 20:36:46 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michal Clapinski X-Patchwork-Id: 13371857 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 AAC3EC83F37 for ; Thu, 31 Aug 2023 20:37:29 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236779AbjHaUha (ORCPT ); Thu, 31 Aug 2023 16:37:30 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38540 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1347400AbjHaUh3 (ORCPT ); Thu, 31 Aug 2023 16:37:29 -0400 Received: from mail-yb1-xb49.google.com (mail-yb1-xb49.google.com [IPv6:2607:f8b0:4864:20::b49]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id AC990E7F for ; Thu, 31 Aug 2023 13:37:25 -0700 (PDT) Received: by mail-yb1-xb49.google.com with SMTP id 3f1490d57ef6-d7493fcd829so1019522276.3 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=G88I0B6rvTJxpOmsnp1OwNzheN2mWZYlb5cPpa5XZA7nxpby//jAxVtgwnhfrAx/1U Cm8RAAG84csfaNbAtd604mT7oF7h0ExEaL+99qKIRqQ+TbY1CkcEWdKH5pfpXs4xQPGc RTtKHqa4YoYIaO55blpVQPZBr0gcDTdNpjszPWoghPs/bJ0wfvgBm8X0DQpWs4ufwFMY uhwYK5uvTHwJMBACOS1GRk1csYdLfwdTBTxNvH1oVwRwRkFkqevJBWKVXVrNghSlOlom +ii37Cz/d725LYRPY8oxaxR551r809jwH/nX81TfrO3+hTCmAhghudEdj8SqxJ5ZgaS1 kd8g== X-Gm-Message-State: AOJu0YyrFSsbOGy79GzPXjYVEEZ9jzw6zhiWzoUcSFZPwUsg2qmkG/7x D0XgFVRty/k2b7ORe4+XbcaBqH/D802ut2he 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-kselftest@vger.kernel.org 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 = fcntl_rw_hint(filp, cmd, arg); break; + case F_CHECK_ORIGINAL_MEMFD: + err = !(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) +/* + * 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. From patchwork Thu Aug 31 20:36:47 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michal Clapinski X-Patchwork-Id: 13371858 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 6B28CC83F37 for ; Thu, 31 Aug 2023 20:37:44 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1347426AbjHaUhp (ORCPT ); Thu, 31 Aug 2023 16:37:45 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50454 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1347427AbjHaUho (ORCPT ); Thu, 31 Aug 2023 16:37:44 -0400 Received: from mail-yw1-x114a.google.com (mail-yw1-x114a.google.com [IPv6:2607:f8b0:4864:20::114a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C18D31B1 for ; Thu, 31 Aug 2023 13:37:38 -0700 (PDT) Received: by mail-yw1-x114a.google.com with SMTP id 00721157ae682-5925f39aa5cso21500567b3.1 for ; Thu, 31 Aug 2023 13:37:38 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1693514258; x=1694119058; 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=xGxCZUuxbWSlNwkUI6nwNutDuYApOwCOxRWfVwGAJ3o=; b=aPRx0fT3CkSVIUrV1PmRN5ysG5vkys23TxgntCvZjZmRN3QniZCggOCA9k7y7K3v9h FrI4K80Xv1u9VMu9jmSo4bhQacOu1p2oqbOhfUI5Kbv+wMBWBXBNSCLaduV+uI4DDtq8 Z1GFMFhr5LPG+g91Z98MRMwvrnqBy3wlV/pDdgdBz5TWQlbGqhR4FFCpUXOWQYz+CpWw X+YUHGmAhuIwkPHdJR5JhVYargvNqzLZlSAY2Y2BqOX/ritvItNqFvn4l5g0CiO58vz1 APGqJ0r93JUep3jiNB4pqvtZXvMXWBurUia7FZgC64riK11fv7j/bW9yGJinxYytA67E vhKg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1693514258; x=1694119058; 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=xGxCZUuxbWSlNwkUI6nwNutDuYApOwCOxRWfVwGAJ3o=; b=Kvu3X4dWRAaJ29Bv/wfdfGPgZtk9J2elL5bkWDh+mBviJo0X4Mm3lP5kWZYIfZk1l4 rSDkxMANzqWGUhb6KTsu7XP0eYW6Ucl0jaelUC9xpmnRqjBLQraCWN5Y4Dsyl4H/VG3f qD36SS9QhIxxJnSGGOqyFzE6dEIJCuLaR4iK8/cDOStikGo1nHpkSRmhyWo+m8MFZfIm oph82g2JcXevP9qoCjXYnOI4ADekDUbz5p/4hs5tmjV4TFrnfd+OP7tZYX+2atK5B0W4 rAmU1YUiscmorGzvMtsoK0xgnRDp5QZQXYKL+aBqgk60lVqfOWG66HU7CZcTaHGYfcfu fWOw== X-Gm-Message-State: AOJu0YwB6Gp5SpM9OzkrVIaMItoS9RtlUWOFD4/+Hnmz6GKp++kApyFq LrMDLSQS6qiath1wYIao4Z2DX5p6mgTaER3X X-Google-Smtp-Source: AGHT+IERJHbjR+c/ciTN5Esvviq3kVNk3iU0MzA8FlGygkmNX9iS6RTXYbmmUYknoi7aexCc3wAuxahV5uNuP5zC X-Received: from mclapinski.waw.corp.google.com ([2a00:79e0:9b:0:36f8:f0a:6df2:a7d5]) (user=mclapinski job=sendgmr) by 2002:a05:690c:2b8b:b0:594:f596:e232 with SMTP id en11-20020a05690c2b8b00b00594f596e232mr20144ywb.2.1693514257991; Thu, 31 Aug 2023 13:37:37 -0700 (PDT) Date: Thu, 31 Aug 2023 22:36:47 +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-3-mclapinski@google.com> Subject: [PATCH 2/2] selftests: test 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-kselftest@vger.kernel.org Signed-off-by: Michal Clapinski --- tools/testing/selftests/memfd/memfd_test.c | 32 ++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/tools/testing/selftests/memfd/memfd_test.c b/tools/testing/selftests/memfd/memfd_test.c index 3df008677239..4f3b7615ca87 100644 --- a/tools/testing/selftests/memfd/memfd_test.c +++ b/tools/testing/selftests/memfd/memfd_test.c @@ -39,6 +39,10 @@ #define MFD_NOEXEC_SEAL 0x0008U +#ifndef F_CHECK_ORIGINAL_MEMFD +#define F_CHECK_ORIGINAL_MEMFD (1024 + 15) +#endif + /* * Default is not to test hugetlbfs */ @@ -1567,6 +1571,32 @@ static void test_share_fork(char *banner, char *b_suffix) close(fd); } +static void test_fcntl_check_original(void) +{ + int fd, fd2; + char path[128]; + + printf("%s FCNTL-CHECK-ORIGINAL\n", memfd_str); + fd = sys_memfd_create("kern_memfd_exec_reopen", 0); + if (fd < 0) { + printf("memfd_create failed: %m\n"); + abort(); + } + if (fcntl(fd, F_CHECK_ORIGINAL_MEMFD) != 1) { + printf("fcntl(F_CHECK_ORIGINAL_MEMFD) failed\n"); + abort(); + } + + fd2 = mfd_assert_reopen_fd(fd); + if (fcntl(fd2, F_CHECK_ORIGINAL_MEMFD) != 0) { + printf("fcntl(F_CHECK_ORIGINAL_MEMFD) failed\n"); + abort(); + } + + close(fd); + close(fd2); +} + int main(int argc, char **argv) { pid_t pid; @@ -1609,6 +1639,8 @@ int main(int argc, char **argv) test_share_open("SHARE-OPEN", ""); test_share_fork("SHARE-FORK", ""); + test_fcntl_check_original(); + /* Run test-suite in a multi-threaded environment with a shared * file-table. */ pid = spawn_idle_thread(CLONE_FILES | CLONE_FS | CLONE_VM);