Received: by 2002:a05:6358:d09b:b0:dc:cd0c:909e with SMTP id jc27csp7589142rwb; Tue, 6 Dec 2022 07:34:13 -0800 (PST) X-Google-Smtp-Source: AA0mqf5k/Y7on2sDt0QVAuLk5u6WVN4ti7gDyu5Ybk5bZBbMgCch1/6oWgr88YA4Tbh9rkKHGLv0 X-Received: by 2002:aa7:dbc3:0:b0:461:6b61:81ae with SMTP id v3-20020aa7dbc3000000b004616b6181aemr77363375edt.62.1670340853393; Tue, 06 Dec 2022 07:34:13 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1670340853; cv=none; d=google.com; s=arc-20160816; b=0oQQjnZKbpdIgOf6wpMCMF3CjOYf0lbcJuwKNp3w+dG3CW17qrFlXNHzSXQKJz1UuN eDbDjNPfpKr6AaioAETJwsZncoJFzFuIgg8ELlufjfqm1jmIVY+fv6jla7G3qgvb1p+R cGUrPb5hRCqv/0sCnyu3mLG+RMCqqpFH2v5SrqUyYVs/DEdXSTqhk+Dv2cWucotWdBGL QT4GRn41CoOKKyrlrtHL7Dwq3E2FEkcvUau7HiBi0bHzX3+vg3mfk2VzY3ow5+NR6QGu prnpmqzojcLdAioAJVQU9tdEWfYEGXW7wPEO5DXPX9dXoQuVZhG6LbyGe7Z5pGG+entQ w7+Q== 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=kmJ6vAUUVWR2mQbJ18nVP5DOm/6S97wQS9z+LXJq08M=; b=B6jyFzVAZ+QnZfeUQitS9F5O1eEztzQb165v5mP76+PINQDrhDOCsq3+BCJlQo1fYm iy9CSk0F/XfbMjsDDgLfxmKsV95q0nlAFpoADENyX7Ns7CdA3Xt7q4+RKdMQ2yu2EoWa NTwwuJAjUndXc/FKz+AQufJwUFQV7gFY16K6aXLqLGEtJfgxnFplo8njgbXe/kj7hl9V Gcb3tf5GfcrGAFQBnrYxEX0FhL3sZgNsRM6JduleepBo6tv7fgqdenpVyA/ZlBcwFugw RaKwhPIjZAVvyZk7+VFTzUrpNRu0YKNlLqTMdMVqIH2DkC87/DpUw1HWgYMABXRRDuan lnmg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=NycKp0JI; 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 v9-20020aa7d9c9000000b004520b01a355si2056868eds.52.2022.12.06.07.33.52; Tue, 06 Dec 2022 07:34:13 -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=NycKp0JI; 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 S235014AbiLFPYQ (ORCPT + 78 others); Tue, 6 Dec 2022 10:24:16 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49474 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234347AbiLFPYG (ORCPT ); Tue, 6 Dec 2022 10:24:06 -0500 Received: from mail-pj1-x102e.google.com (mail-pj1-x102e.google.com [IPv6:2607:f8b0:4864:20::102e]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3AC7F63AE for ; Tue, 6 Dec 2022 07:24:05 -0800 (PST) Received: by mail-pj1-x102e.google.com with SMTP id w15-20020a17090a380f00b0021873113cb4so15135942pjb.0 for ; Tue, 06 Dec 2022 07:24:05 -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=kmJ6vAUUVWR2mQbJ18nVP5DOm/6S97wQS9z+LXJq08M=; b=NycKp0JIHUfaB7gQC1rQ4lT4Sr26kgUE3lM9tnnYJpitmIubv4/0CP5fdfEAJLpfZj gQYB6D/QKKUmeoY6twXhVr2IuJet3kp3/+/WL9I3VfXcmfUAy1TUbD/zlU/durISGorD SuOIZkKIlFUjTzMtAImsXseLwvjfH2YAwJ90o= 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=kmJ6vAUUVWR2mQbJ18nVP5DOm/6S97wQS9z+LXJq08M=; b=kMhSBMr8mxtcY3TW7o2Khl0tcjOojUVrIIivNDahmYXqLlN9EBPsN/iheDt+TikvR7 QUCGSvTiilEGR2Gu54hEyuUbQRMraB2SdGwucsdA3V3oI5+/e3qPmUC5WDHQtZpbQ5Nu faJydYaclYOJ2IJ3b8XCATScE95cVRBka+NfaO9wdhsuGxXadrCKx0NsWNN+n1i479i9 IvARKn1mLGhQcifh9lFXfn/Zq45BXlPWTR+1Dsf9dy+2BGp+exfOlwM1I3l9CHbL4kJb /0/pNr4pISp/eFC3DRYmI4eS6U13Qanjctg0SBTpf7V6hCQgsxoTBmdGIyAOilziOjTo qIMw== X-Gm-Message-State: ANoB5pmu+Hb6h4nr8MTeFSdqXTaQDNtuKPz9qvHfevLIYhDMWICwJ/F6 vLjSSpFAT/1xMIE0kmhDn1dMwA== X-Received: by 2002:a17:90a:ab08:b0:219:aea9:4487 with SMTP id m8-20020a17090aab0800b00219aea94487mr16683300pjq.161.1670340244734; Tue, 06 Dec 2022 07:24:04 -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 r25-20020a635d19000000b0046b1dabf9a8sm10004686pgb.70.2022.12.06.07.24.03 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 06 Dec 2022 07:24:04 -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, Jeff Xu Subject: [PATCH v5 2/6] selftests/memfd: add tests for F_SEAL_EXEC Date: Tue, 6 Dec 2022 15:23:54 +0000 Message-Id: <20221206152358.1966099-3-jeffxu@google.com> X-Mailer: git-send-email 2.39.0.rc0.267.gcb52ba06e7-goog In-Reply-To: <20221206152358.1966099-1-jeffxu@google.com> References: <20221206152358.1966099-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=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 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