From patchwork Thu Jul 13 14:33:47 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Aleksa Sarai X-Patchwork-Id: 13312299 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 kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 6CC84C001DE for ; Thu, 13 Jul 2023 14:34:54 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 0C85F90002E; Thu, 13 Jul 2023 10:34:54 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 078CC90001C; Thu, 13 Jul 2023 10:34:54 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id E82CF90002E; Thu, 13 Jul 2023 10:34:53 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id D8CFD90001C for ; Thu, 13 Jul 2023 10:34:53 -0400 (EDT) Received: from smtpin24.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id AE04EB038C for ; Thu, 13 Jul 2023 14:34:53 +0000 (UTC) X-FDA: 81006835266.24.598ADD0 Received: from mout-p-101.mailbox.org (mout-p-101.mailbox.org [80.241.56.151]) by imf14.hostedemail.com (Postfix) with ESMTP id 53359100014 for ; Thu, 13 Jul 2023 14:34:49 +0000 (UTC) Authentication-Results: imf14.hostedemail.com; dkim=pass header.d=cyphar.com header.s=MBO0001 header.b=ZX0Frhby; dmarc=pass (policy=reject) header.from=cyphar.com; spf=pass (imf14.hostedemail.com: domain of cyphar@cyphar.com designates 80.241.56.151 as permitted sender) smtp.mailfrom=cyphar@cyphar.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1689258890; a=rsa-sha256; cv=none; b=g3KC8wfw7OAZ97juQa/9BvZ1RXtwFaTpRHBhqFRaW9D4HmGvdA+VFMFktDL0EkWfUceYux Tx2W08S0VJ2x8ebPbyGhKELH6JiodchsCgVlieCzI3MDPlJqKL+xOJzaHuI0W+OvqbK8oj /bPMIyrnFHKg2d1vnr/uKXDlMX05RDw= ARC-Authentication-Results: i=1; imf14.hostedemail.com; dkim=pass header.d=cyphar.com header.s=MBO0001 header.b=ZX0Frhby; dmarc=pass (policy=reject) header.from=cyphar.com; spf=pass (imf14.hostedemail.com: domain of cyphar@cyphar.com designates 80.241.56.151 as permitted sender) smtp.mailfrom=cyphar@cyphar.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1689258890; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=eOgLl0t5X3JmCAjENFRIGMn7YQO04jybNACbXkaCXBI=; b=m/aOz3k/lz6f3ri4PI9jIXXUSXM7J1Munr+D0XN+L/uQqWGhnCwQufD6SsKul0nWAyF6Hb uKK6jcfX/VMqfjEsivWno9igEw5TAePtFI5qEN+0U94bs4T0TTVW5tqtOaDyWMrJd1PWXT yUJIh9BgohKxUpMtRnZw7Qq0C+WUmfo= Received: from smtp2.mailbox.org (smtp2.mailbox.org [IPv6:2001:67c:2050:b231:465::2]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (P-384) server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by mout-p-101.mailbox.org (Postfix) with ESMTPS id 4R1xvp0ry7z9ss7; Thu, 13 Jul 2023 16:34:46 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cyphar.com; s=MBO0001; t=1689258886; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=eOgLl0t5X3JmCAjENFRIGMn7YQO04jybNACbXkaCXBI=; b=ZX0FrhbyjxOcpR/ogupcfZZez3Csj5uMZjBQwaoPVVOXSnOYLNd/bntd4dRoGklJZEU5kK PW8vmvLTIt1z7I8aSLzFqj5jLJOXEpS+zaUGhhU3T2NCUEQDn4vhuubgwN1BWXzL5aIM0E 8Qf2Rb6z8j+i/li/B+UrBWinLdMwdjE530baaub1LgxaQAFSwdBvxB1IQcWdukJyB7vyvT D1W6RYN2Uj1F2gAsa6XfYVlAP9CUdUVsHKohEA59w0bZwLVTpC8FuBOgtebO1xH5ld8RfV ABy/DwAHv54WUzp5auRVPzPzFZTmdq+7hjUauXLk3WzPfN4mRNDUn18j9fu1hA== From: Aleksa Sarai To: Andrew Morton , Jeff Xu , Aleksa Sarai , YueHaibing , Luis Chamberlain , Kees Cook , Daniel Verkamp Cc: linux-mm@kvack.org, Dominique Martinet , Christian Brauner , stable@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [RFC PATCH 2/3] memfd: remove racheting feature from vm.memfd_noexec Date: Fri, 14 Jul 2023 00:33:47 +1000 Message-ID: <20230713143406.14342-3-cyphar@cyphar.com> In-Reply-To: <20230713143406.14342-1-cyphar@cyphar.com> References: <20230713143406.14342-1-cyphar@cyphar.com> MIME-Version: 1.0 X-Rspam-User: X-Rspamd-Server: rspam06 X-Rspamd-Queue-Id: 53359100014 X-Stat-Signature: 4iph94rtfb1bc5qghud3pbhegijpmwfe X-HE-Tag: 1689258889-744465 X-HE-Meta: U2FsdGVkX1//TIkkswQQkAt+CaIVt7wQaWr8UkefyDteEuvN/d20PgxUsfkkGZ0eCbVNAlb7lQ3BxtaXQh3Dkz+O0CkdnqpAasTZ5DMeTvPbKUQ+sJrOPvj64bgOIdfSn8vjYqtnd9KDcaLAP2+PKw938e59rCA8YjCopJg+exXy3bmhLddzGVy1C5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dkGtKD6DvvHrWghepxc++Rliv7ZYHtmTs2ZQbx4pDqSE//QSpAFuoCiylVwsxfKxfwSYbzndFV4Vb9jKBe+yhCfmXb1lDf9WgANuJNqem+jHPi2jI/873vAjCVaIO5ste9Qcnz8FlftPkIeFP8xuuu3C X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: This sysctl has the very unusal behaviour of not allowing any user (even CAP_SYS_ADMIN) to reduce the restriction setting, meaning that if you were to set this sysctl to a more restrictive option in the host pidns you would need to reboot your machine in order to reset it. The justification given in [1] is that this is a security feature and thus it should not be possible to disable. Aside from the fact that we have plenty of security-related sysctls that can be disabled after being enabled (fs.protected_symlinks for instance), the protection provided by the sysctl is to stop users from being able to create a binary and then execute it. A user with CAP_SYS_ADMIN can trivially do this without memfd_create(2): % cat mount-memfd.c #include #include #include #include #include #include #define SHELLCODE "#!/bin/echo this file was executed from this totally private tmpfs:" int main(void) { int fsfd = fsopen("tmpfs", FSOPEN_CLOEXEC); assert(fsfd >= 0); assert(!fsconfig(fsfd, FSCONFIG_CMD_CREATE, NULL, NULL, 2)); int dfd = fsmount(fsfd, FSMOUNT_CLOEXEC, 0); assert(dfd >= 0); int execfd = openat(dfd, "exe", O_CREAT | O_RDWR | O_CLOEXEC, 0782); assert(execfd >= 0); assert(write(execfd, SHELLCODE, strlen(SHELLCODE)) == strlen(SHELLCODE)); assert(!close(execfd)); char *execpath = NULL; char *argv[] = { "bad-exe", NULL }, *envp[] = { NULL }; execfd = openat(dfd, "exe", O_PATH | O_CLOEXEC); assert(execfd >= 0); assert(asprintf(&execpath, "/proc/self/fd/%d", execfd) > 0); assert(!execve(execpath, argv, envp)); } % ./mount-memfd this file was executed from this totally private tmpfs: /proc/self/fd/5 % Given that it is possible for CAP_SYS_ADMIN users to create executable binaries without memfd_create(2) and without touching the host filesystem (not to mention the many other things a CAP_SYS_ADMIN process would be able to do that would be equivalent or worse), it seems strange to cause a fair amount of headache to admins when there doesn't appear to be an actual security benefit to blocking this. It should be noted that with this change, programs that can do an unprivileged unshare(CLONE_NEWUSER) would be able to create an executable memfd even if their current pidns didn't allow it. However, the same sample program above can also be used in this scenario, meaning that even with this consideration, blocking CAP_SYS_ADMIN makes little sense: % unshare -rm ./mount-memfd this file was executed from this totally private tmpfs: /proc/self/fd/5 This simply further reinforces that locked-down environments need to disallow CLONE_NEWUSER for unprivileged users (as is already the case in most container environments). [1]: https://lore.kernel.org/all/CABi2SkWnAgHK1i6iqSqPMYuNEhtHBkO8jUuCvmG3RmUB5TKHJw@mail.gmail.com/ Cc: Dominique Martinet Cc: Christian Brauner Cc: stable@vger.kernel.org # v6.3+ Fixes: 105ff5339f49 ("mm/memfd: add MFD_NOEXEC_SEAL and MFD_EXEC") Signed-off-by: Aleksa Sarai --- kernel/pid_sysctl.h | 7 ------- 1 file changed, 7 deletions(-) diff --git a/kernel/pid_sysctl.h b/kernel/pid_sysctl.h index b26e027fc9cd..8a22bc29ebb4 100644 --- a/kernel/pid_sysctl.h +++ b/kernel/pid_sysctl.h @@ -24,13 +24,6 @@ static int pid_mfd_noexec_dointvec_minmax(struct ctl_table *table, if (ns != &init_pid_ns) table_copy.data = &ns->memfd_noexec_scope; - /* - * set minimum to current value, the effect is only bigger - * value is accepted. - */ - if (*(int *)table_copy.data > *(int *)table_copy.extra1) - table_copy.extra1 = table_copy.data; - return proc_dointvec_minmax(&table_copy, write, buf, lenp, ppos); }