Received: by 2002:a05:6358:d09b:b0:dc:cd0c:909e with SMTP id jc27csp1044400rwb; Wed, 7 Dec 2022 08:02:25 -0800 (PST) X-Google-Smtp-Source: AA0mqf7TkQ+r+ZetnJjQOFNEXROg99dIib1InOOWFvwUn7a7lMXUC+xWT/Yd3msPX15jQ282LqtP X-Received: by 2002:aa7:da48:0:b0:46b:25b9:3941 with SMTP id w8-20020aa7da48000000b0046b25b93941mr40604293eds.28.1670428945422; Wed, 07 Dec 2022 08:02:25 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1670428945; cv=none; d=google.com; s=arc-20160816; b=MS97BSYJcd0LbOM6F2AxxnEKGJp7MlZzXPkoHyo6USs0sYnXAfuVCgH3MeCmOYG1y/ sStTplioudz/vx8JLL11W71RaFFBBFb/kwJcLKDDb752TLzMsCyUY0w/1GRp4ct+XeXh u42sNGGiBcchTYWqxSkpy6IgwsO6t/kd2GxeffY/9CGMtSBd/4y5AGrhCBfoLiMXzgDx wygVKEq0wk7DyM1uoM3ISQjXqvj5XJapOBZJ1xia8Zo7SJMSkYvblpjoSmyh3+1dnTSu LtT0tW+Ww25nHpcj25PBT3ZzHekNRNZATH0+VheckIUfTt0GBILsezpHY8/RDM39IMdA A29g== 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=NbsvwgwPR3B757gRns7/stgcrkFvNaKcuShOuGKUmaw=; b=xjVrbDJOxp8YcSo4bmpZIf7V/G6U6UUmi6dDgEObejXmyuhl0fA+escD58rS9zhy8K l2vF6zH4+vWSpCKSghTJPitZljSPszKl5WpV1wtW57RkmvvDbkRX3idfdPHH1bfTlzkm eIts9k+c6h9YuVbAi09Rb+GKx3P9fNT+MqQBLwqEhZgeWauRGAWGFaVB7bwXdxJMm/dS jqC8KiaxwUbTfgd2NG/YZxYak2VtDOCjtM+tsHR7isXf6rrYRxvLQi0ipN0iC/qeLb5/ r91pzZZf8pcOzOPuSk67gY8Op5xR+G4Gxl/SSYCcLFYf3OH5yBICKrK9YQ0bZbT/9ILX 36Vw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=dWibBvBT; 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 Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id t1-20020a170906268100b007470b52f94esi13290321ejc.199.2022.12.07.08.02.05; Wed, 07 Dec 2022 08:02:25 -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=dWibBvBT; 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 S229602AbiLGPt6 (ORCPT + 77 others); Wed, 7 Dec 2022 10:49:58 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50590 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229753AbiLGPts (ORCPT ); Wed, 7 Dec 2022 10:49:48 -0500 Received: from mail-pl1-x62a.google.com (mail-pl1-x62a.google.com [IPv6:2607:f8b0:4864:20::62a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 2D3D95CD31 for ; Wed, 7 Dec 2022 07:49:47 -0800 (PST) Received: by mail-pl1-x62a.google.com with SMTP id w23so17374039ply.12 for ; Wed, 07 Dec 2022 07:49:47 -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=NbsvwgwPR3B757gRns7/stgcrkFvNaKcuShOuGKUmaw=; b=dWibBvBTiVA/84gDobjRgvxVixl764pEHsZqeOQjsQTYcSLe2AY2xlkKC1Imw+7IV2 PoFF7j9IoX6GzTzxhWS5x5nv3blYhAWDvnhY/ykRnnBQF3Y3rS372HPWrCicpAQYKQ4/ NY3okmtSv8f4TkrJddqps6OldgMeuk8K3hlFY= 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=NbsvwgwPR3B757gRns7/stgcrkFvNaKcuShOuGKUmaw=; b=PeHiIQPR1C2Yjef6b7jdLAlgbeWjCTQteEMIGgyN3qboMD5+GKyAgktIVjZhYIO3dT x+uivdT/yO5B/Ia7GKzzhmouAGkvaIlQJDdWaz8VfIhd8I1+TVJx8gF1fi6TO8uLJif/ TLlVcvIv/yqzwCnyCmO5gFrJ45BZJiyPU5KoF9I0tn7n/kvkCcaRMqc8v8V1eW6nsfEN cTfojJUmlaICdAYodOblTRskdIT8RSgVqGQrQ08DyiEQbys6XiXXdgbPO9S187E/rXEh Gk1oPE7NkWic/rGouTO6O0Je+wRpR3CvN5CDslnVFSmg3GVRVs2Y8W5Dhdx3HOJoPSGf +Ntg== X-Gm-Message-State: ANoB5plgnQOu2+jJDa+ejI4GEGeTTQuBtPr97+XMaBYnIeOehIAeCvGC bdoRAQzygvJNbnb6VgNX+4e1BvtWWuIqhZTx X-Received: by 2002:a17:903:40cb:b0:189:b4d0:aee with SMTP id t11-20020a17090340cb00b00189b4d00aeemr756372pld.67.1670428186688; Wed, 07 Dec 2022 07:49:46 -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 a9-20020a170902ecc900b0017f7628cbddsm14920934plh.30.2022.12.07.07.49.45 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 07 Dec 2022 07:49:46 -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, jannh@google.com, linux-hardening@vger.kernel.org Subject: [PATCH v6 2/6] selftests/memfd: add tests for F_SEAL_EXEC Date: Wed, 7 Dec 2022 15:49:35 +0000 Message-Id: <20221207154939.2532830-3-jeffxu@google.com> X-Mailer: git-send-email 2.39.0.rc0.267.gcb52ba06e7-goog In-Reply-To: <20221207154939.2532830-1-jeffxu@google.com> References: <20221207154939.2532830-1-jeffxu@google.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 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=unavailable 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 From: Daniel Verkamp Basic tests to ensure that user/group/other execute bits cannot be changed after applying F_SEAL_EXEC to a memfd. Signed-off-by: Daniel Verkamp Co-developed-by: Jeff Xu Signed-off-by: Jeff Xu --- 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", ""); -- 2.39.0.rc0.267.gcb52ba06e7-goog