Received: by 2002:a05:6358:d09b:b0:dc:cd0c:909e with SMTP id jc27csp2945552rwb; Fri, 9 Dec 2022 08:08:43 -0800 (PST) X-Google-Smtp-Source: AA0mqf4OzI6HDK1fSsgJupV06caowPLDy3Owq5Tt8SI7rkXA46DXJbJswnsjHiUhryXk5csFhvZG X-Received: by 2002:a17:902:8215:b0:185:441e:2342 with SMTP id x21-20020a170902821500b00185441e2342mr5711790pln.56.1670602123326; Fri, 09 Dec 2022 08:08:43 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1670602123; cv=none; d=google.com; s=arc-20160816; b=DvgAUCZVMEL0hgkfvBVRxy9s3LXSY6WXTwNHNwaBpuLvkhstJ0MSA8yCF0gQHYlSx/ RcwCWBjMtop/8pnxEmTaR1iu6wAcirjvncwV4l9HKABsN5MpT4+9JOiJJ+4CDHwZUvsK DKGavYsRi+u94HS595S6sbXu9IxXK1hj+8cphiN5CPWVCZvEBgzY+VTzWHiYfg8oVWts LV18oDIooAt9MoVtu8C8rfogswHdfCpjH/yNYAoc/hWIqPmiyYnknhz20Wtc6LnMuyIr E3PVPgROin4LDrdpZfA73jlvXl7FfBKwyLOh0TKK6MPrmnToXOWUmpfidNo0GRL8rzwA yggQ== 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=vFajmLWzEheOspCEYhY0eo6ZhecGZeOzelViubfUiKU=; b=U9Zj8WJFZW4/4Wv2JdSzwsDGiTz5Wu/hCRre5FTGIbrFEiB3p9qxlTykbs663iigBQ p9pcaMx1s5XWpGNcJz4o38I9fgBMGMKo0L/I1ep5XIJTioLnOrha3XGsos4U20afVrgk yLKhmzyDKST0UtCnPIk5KHuIcdLIrOJdATC/b14Ndi5CKOn/peDp18YU0NRIZKBd46m+ 8NTErOR5M0hZTqC26HCHpZEHH4tuun7CvTJPB//kNMEPYrw+nSW1Tyw0LbxJoj9ZySjc DuJKc6EHYFZ/4ln1JHz6TcNyVyTwhK3ipBqdoMc1VvY9+tdGOykaLQfUmFGk20bSKnS9 tZBA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=dptKtOCd; 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 i184-20020a6387c1000000b00478d1230674si1638181pge.736.2022.12.09.08.08.33; Fri, 09 Dec 2022 08:08:43 -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=dptKtOCd; 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 S229734AbiLIQF1 (ORCPT + 75 others); Fri, 9 Dec 2022 11:05:27 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49784 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230077AbiLIQFE (ORCPT ); Fri, 9 Dec 2022 11:05:04 -0500 Received: from mail-pf1-x42c.google.com (mail-pf1-x42c.google.com [IPv6:2607:f8b0:4864:20::42c]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 834C8554F6 for ; Fri, 9 Dec 2022 08:05:02 -0800 (PST) Received: by mail-pf1-x42c.google.com with SMTP id t18so3978686pfq.13 for ; Fri, 09 Dec 2022 08:05:02 -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=vFajmLWzEheOspCEYhY0eo6ZhecGZeOzelViubfUiKU=; b=dptKtOCdTYDRw1zV2LczU6Mn1xxfY3K7c4rFaFud5vbOoJLbaV53h863pAiKxktADU hrt1ext9eD2TGdU12+Ynlfi3y5U15x8duzI8R0NoHOvfww3B8KJhixGy5q8AKCjevVIp bMEZAl3CbIzNJxQFT7JG1kmnsDBR/bSc/JGm0= 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=vFajmLWzEheOspCEYhY0eo6ZhecGZeOzelViubfUiKU=; b=CiFJjVbpX30oV+BQmuFIxkXNNJYRwQGG+l+eb5P030KEFep/5TyfdVFwjBlxwXk805 HcYJWdb9VNfT/AxqGOvhyy69NlIlkRiuM/SQot+6QqM25LlXxY0X6bshOOSgrIV7hOJV 9n2CyCQhjoPxvRrk96YXTW+wx7+BsIdpUAWxnEiTHzU4w89+v/6c8LU40Q4f4xtYU20x Y2KsoyRSazCg0o3isQo5Ja83oFX4UYT/rPtLTLLd+tsoZteenGhj6TL3I32AlL/UyX1P hYMntKkzghGj42Ra6ZGnNu71+ZlVlvyP2Oyt9Jaf325j58gBNcYmHFCECloc4M1oOoQt psig== X-Gm-Message-State: ANoB5pkuTF48ntkkBtldo1dtYUsmvtoVnpfaxqRtBY2knY5p7A3Gfi30 BRn6+JGsG1x5gxe/7TBVagmdPA== X-Received: by 2002:a05:6a00:1d14:b0:578:1708:6416 with SMTP id a20-20020a056a001d1400b0057817086416mr113562pfx.11.1670601901687; Fri, 09 Dec 2022 08:05:01 -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 a15-20020aa795af000000b00576670cc170sm1460504pfk.93.2022.12.09.08.05.00 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 09 Dec 2022 08:05:01 -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, linux-security-module@vger.kernel.org Subject: [PATCH v7 2/6] selftests/memfd: add tests for F_SEAL_EXEC Date: Fri, 9 Dec 2022 16:04:49 +0000 Message-Id: <20221209160453.3246150-3-jeffxu@google.com> X-Mailer: git-send-email 2.39.0.rc1.256.g54fd8350bd-goog In-Reply-To: <20221209160453.3246150-1-jeffxu@google.com> References: <20221209160453.3246150-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 | 123 ++++++++++++++++++++- 1 file changed, 122 insertions(+), 1 deletion(-) diff --git a/tools/testing/selftests/memfd/memfd_test.c b/tools/testing/selftests/memfd/memfd_test.c index 94df2692e6e4..f18a15a1f275 100644 --- a/tools/testing/selftests/memfd/memfd_test.c +++ b/tools/testing/selftests/memfd/memfd_test.c @@ -28,12 +28,38 @@ #define MFD_DEF_SIZE 8192 #define STACK_SIZE 65536 +#define F_SEAL_EXEC 0x0020 + /* * 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[PATH_MAX]; + int size; + ssize_t nbytes; + + size = snprintf(buf1, PATH_MAX, "/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 +124,14 @@ static unsigned int mfd_assert_get_seals(int fd) static void mfd_assert_has_seals(int fd, unsigned int seals) { + char buf[PATH_MAX]; + int nbytes; unsigned int s; + fd2name(fd, buf, PATH_MAX); 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 +623,64 @@ static void mfd_fail_grow_write(int fd) } } +static void mfd_assert_mode(int fd, int mode) +{ + struct stat st; + char buf[PATH_MAX]; + int nbytes; + + fd2name(fd, buf, PATH_MAX); + + 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[PATH_MAX]; + int nbytes; + + fd2name(fd, buf, PATH_MAX); + + 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[PATH_MAX]; + int nbytes; + + fd2name(fd, buf, PATH_MAX); + + 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 +967,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 +1179,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.rc1.256.g54fd8350bd-goog