From patchwork Fri Dec 2 01:34:01 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jeff Xu X-Patchwork-Id: 28651 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:f944:0:0:0:0:0 with SMTP id q4csp588285wrr; Thu, 1 Dec 2022 17:37:14 -0800 (PST) X-Google-Smtp-Source: AA0mqf6rp9r2BK6PK2E2m2G6VmF3IB/YvcBy1Eq5+clxZTcnHgY+af0VQsAa/R8VYaCPzrqQWVbs X-Received: by 2002:a17:902:dad2:b0:189:49ec:5f15 with SMTP id q18-20020a170902dad200b0018949ec5f15mr44449275plx.52.1669945034484; Thu, 01 Dec 2022 17:37:14 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1669945034; cv=none; d=google.com; s=arc-20160816; b=KaffYyrXaSmrZey+r/BW4QX+5XK7YyLDvPAlfO9VwaLMozxoKDs/+w0eLrFp7UMDOm Jwds4duNizTwEWyZrX4QR4ITAUdL8E/MacaVW9MyCtsZnBS+2SJx0lCTqVUG6GEt44xN fQARJomdQ4dGMin00SprDRxwo3qjOdcfQKlt7Wrl6NR4uR0aIqCyNMk8LHhXa6OZkRhX FXTWveLjeX6INjxhClifv9gxr8Ga/Grq9zvFYR4Jc0C7Y0fAIUkmq0aZGGGJFJ0f2D8T gqUXRMLigL4OKCuwAEjtI2s2vDgFgpsgl4ul/60Bfm01p7gQWD1Vd+duV69iDZb6pxbx mxig== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=I7MLjr3MjQu+vEYGPW7zVilB4153TCbmT2Qa9Is+kkA=; b=yYdx+cBHiEF0Ry4gO0VOyNp7rp9LE9XP5l349sX0BA5VpMFm4sTAzazHSmvmwjRiBw gx3XEDP+xEGmWXf//Rc8u1Zg102aqt3YBnABIqi3DHpLLL6WlDbNJsuNAxZ5ESyD6TWS 4GjvWJI+v4IneEAiFvDlf+UMBwsnNWwm0P6lztOAbAq/Ra+UKD+kjzsTKho5yYg337zh ih+v/+WZeJNVYYYirNH6pH0WLqpXGRxfKquPm3OCDN4nuBX2lxPcIwdhAvECEztEJW47 A3Mrx4/pa6EJpBCy+T1d0CGD1zuNV2MBCeNEtDPcSiXLL/e5CeonYVHpNwFXOaZ4FVTN C89Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=LxeM5dSe; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id o15-20020a170902d4cf00b001871d062940si6070504plg.400.2022.12.01.17.37.01; Thu, 01 Dec 2022 17:37:14 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=LxeM5dSe; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230429AbiLBBgD (ORCPT + 99 others); Thu, 1 Dec 2022 20:36:03 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52350 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231605AbiLBBfy (ORCPT ); Thu, 1 Dec 2022 20:35:54 -0500 Received: from mail-pg1-x531.google.com (mail-pg1-x531.google.com [IPv6:2607:f8b0:4864:20::531]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 717BFD2DB4 for ; Thu, 1 Dec 2022 17:35:52 -0800 (PST) Received: by mail-pg1-x531.google.com with SMTP id 6so3181276pgm.6 for ; Thu, 01 Dec 2022 17:35:52 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=I7MLjr3MjQu+vEYGPW7zVilB4153TCbmT2Qa9Is+kkA=; b=LxeM5dSegaw2mlu/73XHqy9gB1r8BqVrr8xehs4RWb4ERes96JpafPj6MFuHckmlH4 pbZbIrtq9O7AgzZnKPn6oPAlpLf2Wiiz6vezDvKYzpLMylysFplwAZkEM0wtWXegJkxQ t51Beph/LXZUj4c9ZZw6I/8POYLF+8FmharWw= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=I7MLjr3MjQu+vEYGPW7zVilB4153TCbmT2Qa9Is+kkA=; b=k359VLNgkDB4BlNcpFSTQMM/d816G5ztDqN+5+Q7USXiZtqb9jMAIyRK/An+kJe6q4 f49xyt8E/wgr3HXPfVhqxrp64g5xW8uRlRaRytxzfT/DNaN4wMYmmQ++jZa60UM0fCxs DlRvjkWu6r5W2L6jMVKxXy/ttUNFc2t7smO3YZh7Qaj9fu0fGCHPZS5PVuAGE96VZJ5u aXEOg8nKOLIB9oT/faKwWvejmTIFKViLYchda7eoYNdSh/4SI0orvOh0wfhBu6TMbJjB fObrwrguUtSVXnEzy5DQpFtf5wFE9+HfW4A967DWcgE1s0QJyTewCduoU18XiqUfMuIR Jhog== X-Gm-Message-State: ANoB5pnL5Yo1eACkwp3i7JgLBv3FEKOXjnjDVoZQcXjv768ZT8egym2Y 3LBmtR1gVfhFuid7We46SV0Vew== X-Received: by 2002:a63:d18:0:b0:46e:b0e3:ee46 with SMTP id c24-20020a630d18000000b0046eb0e3ee46mr42438144pgl.622.1669944952181; Thu, 01 Dec 2022 17:35:52 -0800 (PST) Received: from jeffxud.c.googlers.com.com (30.202.168.34.bc.googleusercontent.com. [34.168.202.30]) by smtp.gmail.com with ESMTPSA id s13-20020a65690d000000b00477fb27eaddsm3074241pgq.63.2022.12.01.17.35.51 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 01 Dec 2022 17:35:51 -0800 (PST) From: jeffxu@chromium.org To: skhan@linuxfoundation.org, keescook@chromium.org Cc: akpm@linux-foundation.org, dmitry.torokhov@gmail.com, dverkamp@chromium.org, hughd@google.com, jeffxu@google.com, jorgelo@chromium.org, linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, linux-mm@kvack.org, mnissler@chromium.org, jannh@google.com, linux-hardening@vger.kernel.org, Jeff Xu Subject: [PATCH v3] selftests/memfd: add tests for F_SEAL_EXEC Date: Fri, 2 Dec 2022 01:34:01 +0000 Message-Id: <20221202013404.163143-4-jeffxu@google.com> X-Mailer: git-send-email 2.39.0.rc0.267.gcb52ba06e7-goog In-Reply-To: <20221202013404.163143-1-jeffxu@google.com> References: <20221202013404.163143-1-jeffxu@google.com> MIME-Version: 1.0 X-Spam-Status: No, score=-2.0 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1751064284362326451?= X-GMAIL-MSGID: =?utf-8?q?1751064284362326451?= From: Daniel Verkamp Basic tests to ensure that user/group/other execute bits cannot be changed after applying F_SEAL_EXEC to a memfd. Co-developed-by: Jeff Xu Signed-off-by: Jeff Xu Signed-off-by: Daniel Verkamp --- tools/testing/selftests/memfd/memfd_test.c | 129 ++++++++++++++++++++- 1 file changed, 128 insertions(+), 1 deletion(-) diff --git a/tools/testing/selftests/memfd/memfd_test.c b/tools/testing/selftests/memfd/memfd_test.c index 94df2692e6e4..1d7e7b36bbdd 100644 --- a/tools/testing/selftests/memfd/memfd_test.c +++ b/tools/testing/selftests/memfd/memfd_test.c @@ -28,12 +28,44 @@ #define MFD_DEF_SIZE 8192 #define STACK_SIZE 65536 +#ifndef F_SEAL_EXEC +#define F_SEAL_EXEC 0x0020 +#endif + +#ifndef MAX_PATH +#define MAX_PATH 256 +#endif + /* * Default is not to test hugetlbfs */ static size_t mfd_def_size = MFD_DEF_SIZE; static const char *memfd_str = MEMFD_STR; +static ssize_t fd2name(int fd, char *buf, size_t bufsize) +{ + char buf1[MAX_PATH]; + int size; + ssize_t nbytes; + + size = snprintf(buf1, MAX_PATH, "/proc/self/fd/%d", fd); + if (size < 0) { + printf("snprintf(%d) failed on %m\n", fd); + abort(); + } + + /* + * reserver one byte for string termination. + */ + nbytes = readlink(buf1, buf, bufsize-1); + if (nbytes == -1) { + printf("readlink(%s) failed %m\n", buf1); + abort(); + } + buf[nbytes] = '\0'; + return nbytes; +} + static int mfd_assert_new(const char *name, loff_t sz, unsigned int flags) { int r, fd; @@ -98,11 +130,14 @@ static unsigned int mfd_assert_get_seals(int fd) static void mfd_assert_has_seals(int fd, unsigned int seals) { + char buf[MAX_PATH]; + int nbytes; unsigned int s; + fd2name(fd, buf, MAX_PATH); s = mfd_assert_get_seals(fd); if (s != seals) { - printf("%u != %u = GET_SEALS(%d)\n", seals, s, fd); + printf("%u != %u = GET_SEALS(%s)\n", seals, s, buf); abort(); } } @@ -594,6 +629,64 @@ static void mfd_fail_grow_write(int fd) } } +static void mfd_assert_mode(int fd, int mode) +{ + struct stat st; + char buf[MAX_PATH]; + int nbytes; + + fd2name(fd, buf, MAX_PATH); + + if (fstat(fd, &st) < 0) { + printf("fstat(%s) failed: %m\n", buf); + abort(); + } + + if ((st.st_mode & 07777) != mode) { + printf("fstat(%s) wrong file mode 0%04o, but expected 0%04o\n", + buf, (int)st.st_mode & 07777, mode); + abort(); + } +} + +static void mfd_assert_chmod(int fd, int mode) +{ + char buf[MAX_PATH]; + int nbytes; + + fd2name(fd, buf, MAX_PATH); + + if (fchmod(fd, mode) < 0) { + printf("fchmod(%s, 0%04o) failed: %m\n", buf, mode); + abort(); + } + + mfd_assert_mode(fd, mode); +} + +static void mfd_fail_chmod(int fd, int mode) +{ + struct stat st; + char buf[MAX_PATH]; + int nbytes; + + fd2name(fd, buf, MAX_PATH); + + if (fstat(fd, &st) < 0) { + printf("fstat(%s) failed: %m\n", buf); + abort(); + } + + if (fchmod(fd, mode) == 0) { + printf("fchmod(%s, 0%04o) didn't fail as expected\n", + buf, mode); + abort(); + } + + /* verify that file mode bits did not change */ + mfd_assert_mode(fd, st.st_mode & 07777); +} + static int idle_thread_fn(void *arg) { sigset_t set; @@ -880,6 +973,39 @@ static void test_seal_resize(void) close(fd); } +/* + * Test SEAL_EXEC + * Test that chmod() cannot change x bits after sealing + */ +static void test_seal_exec(void) +{ + int fd; + + printf("%s SEAL-EXEC\n", memfd_str); + + fd = mfd_assert_new("kern_memfd_seal_exec", + mfd_def_size, + MFD_CLOEXEC | MFD_ALLOW_SEALING); + + mfd_assert_mode(fd, 0777); + + mfd_assert_chmod(fd, 0644); + + mfd_assert_has_seals(fd, 0); + mfd_assert_add_seals(fd, F_SEAL_EXEC); + mfd_assert_has_seals(fd, F_SEAL_EXEC); + + mfd_assert_chmod(fd, 0600); + mfd_fail_chmod(fd, 0777); + mfd_fail_chmod(fd, 0670); + mfd_fail_chmod(fd, 0605); + mfd_fail_chmod(fd, 0700); + mfd_fail_chmod(fd, 0100); + mfd_assert_chmod(fd, 0666); + + close(fd); +} + /* * Test sharing via dup() * Test that seals are shared between dupped FDs and they're all equal. @@ -1059,6 +1185,7 @@ int main(int argc, char **argv) test_seal_shrink(); test_seal_grow(); test_seal_resize(); + test_seal_exec(); test_share_dup("SHARE-DUP", ""); test_share_mmap("SHARE-MMAP", "");