From patchwork Mon Aug 14 08:40:57 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Aleksa Sarai X-Patchwork-Id: 13352563 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 10798EB64DD for ; Mon, 14 Aug 2023 08:41:37 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 8D9EF8E0003; Mon, 14 Aug 2023 04:41:37 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 88A9A8E0001; Mon, 14 Aug 2023 04:41:37 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 72BE98E0003; Mon, 14 Aug 2023 04:41:37 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id 647628E0001 for ; Mon, 14 Aug 2023 04:41:37 -0400 (EDT) Received: from smtpin03.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id 317B41A0886 for ; Mon, 14 Aug 2023 08:41:37 +0000 (UTC) X-FDA: 81122066634.03.6426837 Received: from mout-p-101.mailbox.org (mout-p-101.mailbox.org [80.241.56.151]) by imf21.hostedemail.com (Postfix) with ESMTP id 51C3C1C0011 for ; Mon, 14 Aug 2023 08:41:35 +0000 (UTC) Authentication-Results: imf21.hostedemail.com; dkim=pass header.d=cyphar.com header.s=MBO0001 header.b=PWInDqWM; spf=pass (imf21.hostedemail.com: domain of cyphar@cyphar.com designates 80.241.56.151 as permitted sender) smtp.mailfrom=cyphar@cyphar.com; dmarc=pass (policy=reject) header.from=cyphar.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1692002495; a=rsa-sha256; cv=none; b=Rg9Eu5gUBSmyHaBsYfjoSDHJd7BHp2bvGYRamaRSw/nWfevPTLd1hjYC4zHpXSeYiYNBZO vZ3XWxIZCNw7HNEJk4dKW+WUZoH64szU4Q+OigokSh7aonKc7nTL8IzfiDg4QeE4UCJzZd D73A8RPXGs2dbTt4QJnc4WwaZlzUNPs= ARC-Authentication-Results: i=1; imf21.hostedemail.com; dkim=pass header.d=cyphar.com header.s=MBO0001 header.b=PWInDqWM; spf=pass (imf21.hostedemail.com: domain of cyphar@cyphar.com designates 80.241.56.151 as permitted sender) smtp.mailfrom=cyphar@cyphar.com; dmarc=pass (policy=reject) header.from=cyphar.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1692002495; 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-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=7gQyeuP4S02FF5qxYTiiXRiidByA4CAzLTryBvJ7G7w=; b=CMXvfQdWadCqXS1AzEyEdMJlaqU7ip6zZi02+4y40SvnjN8zZcdMbS3oUMQLLysQXb3RFr 9K8HUIX6rc71SJkuGtBXt94tjTul0Fm4q8Q9bfxbEMDNh01mrm8yHpRdic/fU/5Gd9gc3s f0+Psn3r+TiwpwUP0A6HRxD9LFW6Vd8= Received: from smtp202.mailbox.org (smtp202.mailbox.org [10.196.197.202]) (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 4RPSYR2bs0z9sWB; Mon, 14 Aug 2023 10:41:31 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cyphar.com; s=MBO0001; t=1692002491; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=7gQyeuP4S02FF5qxYTiiXRiidByA4CAzLTryBvJ7G7w=; b=PWInDqWMMKzI/ONQxGwzOm9Nqjj0QWXmLPZUkhDGsxbUFUikujZWbEY1MqZPB1WBCvi77e kiKJ8pi/2PteW88+lQBK8ltXeZV9mb2bYVBIT+xToxGb7CsmZSjDBOtgWYsBzPZwis7wXQ RtHIduy6h2n0Y08EgBWVCaB3NB4595F6cDxfr1hTZLGJAVUq5dt4TkT/kmSYd5o/jIhMC4 ijPwqZ/3W9OYLr4Ca7Mt3QrZIxPfcKZbkuVIeM0k0uGsxknhulEqFewn6BGmOkqAjyH3EK i++D6TrN/b3E0iNQOwpKhc/n00nHwaN4m35+mYh5wEz06ygRPqfkMrBizyhlDw== From: Aleksa Sarai Date: Mon, 14 Aug 2023 18:40:57 +1000 Subject: [PATCH v2 1/5] selftests: memfd: error out test process when child test fails MIME-Version: 1.0 Message-Id: <20230814-memfd-vm-noexec-uapi-fixes-v2-1-7ff9e3e10ba6@cyphar.com> References: <20230814-memfd-vm-noexec-uapi-fixes-v2-0-7ff9e3e10ba6@cyphar.com> In-Reply-To: <20230814-memfd-vm-noexec-uapi-fixes-v2-0-7ff9e3e10ba6@cyphar.com> To: Andrew Morton , Shuah Khan , Jeff Xu , Kees Cook , Daniel Verkamp Cc: Christian Brauner , Dominique Martinet , stable@vger.kernel.org, linux-api@vger.kernel.org, linux-kernel@vger.kernel.org, linux-mm@kvack.org, linux-kselftest@vger.kernel.org, Aleksa Sarai X-Developer-Signature: v=1; a=openpgp-sha256; l=1390; i=cyphar@cyphar.com; h=from:subject:message-id; bh=+Up0ghrBW/nfRj4bC3B4yqL0dFwv9KTB+GgoZwyV0aw=; b=owGbwMvMwCWmMf3Xpe0vXfIZT6slMaTcfLFiw/TsfmWDxoNz8nnmrvi1iHnrP6m7AWasOo8m3 1wekyNc2FHKwiDGxSArpsiyzc8zdNP8xVeSP61kg5nDygQyhIGLUwAmkpfFyDBRc+qvi5+lrvwq Uu9s9nvj/PqGRbrzrqg2Y4FZ6bO7O9cw/NPdWbdxP9tDl7XxbgXfxdZNaXf791I7X+J3+4PgVBu 9RXwA X-Developer-Key: i=cyphar@cyphar.com; a=openpgp; fpr=C9C370B246B09F6DBCFC744C34401015D1D2D386 X-Rspamd-Server: rspam08 X-Rspamd-Queue-Id: 51C3C1C0011 X-Stat-Signature: mzswb4gh6dz4xzu1cy4wqkf7xr5oqrtf X-Rspam-User: X-HE-Tag: 1692002495-160332 X-HE-Meta: U2FsdGVkX18tqKZcTtY5UaK9qIH0vPjC9ESjjZfIlb/P6aaxNMDP6GWlzJESrd9jk4vy3SQdQh7C3O42r/AHbg9unphaR4S3HG498E05A9cimz62D82UxYhg7/9use/nAYaGE1+TpyzwWnBeQXmHiJ+LkEdxj11wFiItFo39vVDuQ+HlpZpTwBfMirSqJHRVmFoVnwFoqPu6cppe9pJT53uKfGaAvVH9spKTWP/M+SkOYnCbFqOWtDmi91LET0fKDkeuTdHLqHhPHN2SUyOIWDnJK7jGZYT+5eb+X+FnDNdtuzMUNH8YnTJzhUsJFrgahPWhkO7L/T/c22o6zsMC7STZivpxaFcV6fUNFHcQc3uSz8gCShGZ+TIgx9MZzrhiMlrHdh5YTyEYTzPgfbFKCJDovvo1oiWYK30FePn/LzWUwAv3MqMVZQnOflvHYxJuWHLDYZZaFKiqSjrjSU0MnPEdflMlUw9qIYkRsLYdcfC6pg9MR+WLvXBX9mSSYtgX6K7hTPYVUdJrziN3FW7tFkn6ZoRJXaZg5fptZS6JJC2trgr/LuoKrYoJRzBglTmOqEldeDQk5xPFXWOfc+fxSHHaZ7fCBu0PM9+1JjMB9Xt3HdxDfLbVQGv12geCPXoiEOyHXlmNpHq4gdRgxpvllV+3rxuyuNKkjhsJw852VK71kV6ovNwXp1zfv6v130naRkA1Ws8fWF03xdnsszvCqwZinadWQNgGvJrgQebMNnKMhzRVstqpFamV4bEXtBo9SxxTCAj3n2w/yki25EN1mZFJ//8Hgz9R6oCTPo4JfrwCIUpSJ6wSCGd045ayYQ5P+1bRVzms0Iblg9zK0jiFaaM2Jh+OdbjkLZYgF/GceFtz/utNpXmN7+uY3wA2S/KFJf55G2ydAMCjh9t9Wy1fCmVTgyZ+zY9vU7ZvaEIEuk59M0XG6YRD/vNKIIJIGWptkmcljU9e5wP5V915M0K AO5wcKu0 wAyEr8osDdXjhpdu65sAZTLm54j+zQ/0oUbs1L3icDDsmHAKj091lWN6OgeEM/OiE+3GaRcbLlb/pSkgwknKow6VHgsorWDyPXC/Mo0/3WI7+v1nC4qH/bXhnz/6tA8wLmpI9YWEOarUYbgzyMEoF1b/oYttaCX+iA3BuzhbeSi/WHmb5kBK4Ky2cOPPaiEc5cLKqNZ3pxI7oahTHawzPadFLLouiX7T5lUub 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: Before this change, a test runner using this self test would see a return code of 0 when the tests using a child process (namely the MFD_NOEXEC_SEAL and MFD_EXEC tests) failed, masking test failures. Fixes: 11f75a01448f ("selftests/memfd: add tests for MFD_NOEXEC_SEAL MFD_EXEC") Reviewed-by: Jeff Xu Signed-off-by: Aleksa Sarai --- tools/testing/selftests/memfd/memfd_test.c | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/tools/testing/selftests/memfd/memfd_test.c b/tools/testing/selftests/memfd/memfd_test.c index dbdd9ec5e397..8eb49204f9ea 100644 --- a/tools/testing/selftests/memfd/memfd_test.c +++ b/tools/testing/selftests/memfd/memfd_test.c @@ -1207,7 +1207,24 @@ static pid_t spawn_newpid_thread(unsigned int flags, int (*fn)(void *)) static void join_newpid_thread(pid_t pid) { - waitpid(pid, NULL, 0); + int wstatus; + + if (waitpid(pid, &wstatus, 0) < 0) { + printf("newpid thread: waitpid() failed: %m\n"); + abort(); + } + + if (WIFEXITED(wstatus) && WEXITSTATUS(wstatus) != 0) { + printf("newpid thread: exited with non-zero error code %d\n", + WEXITSTATUS(wstatus)); + abort(); + } + + if (WIFSIGNALED(wstatus)) { + printf("newpid thread: killed by signal %d\n", + WTERMSIG(wstatus)); + abort(); + } } /* From patchwork Mon Aug 14 08:40:58 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Aleksa Sarai X-Patchwork-Id: 13352564 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 12BD5EB64DD for ; Mon, 14 Aug 2023 08:41:44 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id A1211900002; Mon, 14 Aug 2023 04:41:43 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 9C22D8E0001; Mon, 14 Aug 2023 04:41:43 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 889D5900002; Mon, 14 Aug 2023 04:41:43 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id 77F9F8E0001 for ; Mon, 14 Aug 2023 04:41:43 -0400 (EDT) Received: from smtpin29.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id 4C3F61C9341 for ; Mon, 14 Aug 2023 08:41:43 +0000 (UTC) X-FDA: 81122066886.29.701F138 Received: from mout-p-202.mailbox.org (mout-p-202.mailbox.org [80.241.56.172]) by imf29.hostedemail.com (Postfix) with ESMTP id 63CF4120010 for ; Mon, 14 Aug 2023 08:41:40 +0000 (UTC) Authentication-Results: imf29.hostedemail.com; dkim=pass header.d=cyphar.com header.s=MBO0001 header.b=mkHgS1T1; spf=pass (imf29.hostedemail.com: domain of cyphar@cyphar.com designates 80.241.56.172 as permitted sender) smtp.mailfrom=cyphar@cyphar.com; dmarc=pass (policy=reject) header.from=cyphar.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1692002500; 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-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=8KHnXfVFe2HzozOZ++k3K0yZYQRi3T1iqdVW5ddEXr8=; b=sQVj69i50DMsp6bjx5NbIfxOYK1P8eJKsDlKUH4lo4T5nXHhhBr/F2P9slxc41ERbFh7Hu E4dth+DxpXSt0KX/BPmJL0zZKOOgOEOdsuzVE8Sygtf+zOmq5ftcPBJhJXtodQ9Bvu3Bjt LGfbNFwUXfxEPQiu4rY2dsLNR3tyq2I= ARC-Authentication-Results: i=1; imf29.hostedemail.com; dkim=pass header.d=cyphar.com header.s=MBO0001 header.b=mkHgS1T1; spf=pass (imf29.hostedemail.com: domain of cyphar@cyphar.com designates 80.241.56.172 as permitted sender) smtp.mailfrom=cyphar@cyphar.com; dmarc=pass (policy=reject) header.from=cyphar.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1692002500; a=rsa-sha256; cv=none; b=C53Xu9rmShhGgEW8qGrjCezh42pqwNHaF3mrhNTC2NF91Xcg39VkkIB9VHDhHFSqpRwouw gCh3U1fAvs9+e1uMZ91jd5S6VWEOKARghdklUF2/AYqnuvQCrLgA8zL0Wvl/fETx8Bbkm1 3yTK2lLUuxzvoDkw2Zc/5WN6Fhm6JCw= Received: from smtp202.mailbox.org (smtp202.mailbox.org [10.196.197.202]) (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-202.mailbox.org (Postfix) with ESMTPS id 4RPSYX6LYkz9sV8; Mon, 14 Aug 2023 10:41:36 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cyphar.com; s=MBO0001; t=1692002496; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=8KHnXfVFe2HzozOZ++k3K0yZYQRi3T1iqdVW5ddEXr8=; b=mkHgS1T1saMuQar2+hLvU869NXgYOqmVxqGItOAqDywdCYkf+YWmIvZvqXQ1SBOXWxGuri z93zrsoK0Cj8bIXSlfhGixvtUc41ZaUdmQ33jWm5GX/PhRIaYPBM2xOKWAjWjt7iKXOuSp cdkg+J36AMISU9xvCR/ymXcYfbqrBKjjqMe8CS/6BSKryd1PeDjLHqrqfxgJXnS6mOnuw7 VGa+VOLZHVdVZw4pftsvVsKbgEGQR2sKNlxkvQqUF2QLN7UEW+vpJ5ZLh6Mheo+OAE8yR3 MzgG0xTPbS5PMTklW09fnxmgJhThTcR47109IlAv79ze0UER9zp0IlFsBGOBmA== From: Aleksa Sarai Date: Mon, 14 Aug 2023 18:40:58 +1000 Subject: [PATCH v2 2/5] memfd: do not -EACCES old memfd_create() users with vm.memfd_noexec=2 MIME-Version: 1.0 Message-Id: <20230814-memfd-vm-noexec-uapi-fixes-v2-2-7ff9e3e10ba6@cyphar.com> References: <20230814-memfd-vm-noexec-uapi-fixes-v2-0-7ff9e3e10ba6@cyphar.com> In-Reply-To: <20230814-memfd-vm-noexec-uapi-fixes-v2-0-7ff9e3e10ba6@cyphar.com> To: Andrew Morton , Shuah Khan , Jeff Xu , Kees Cook , Daniel Verkamp Cc: Christian Brauner , Dominique Martinet , stable@vger.kernel.org, linux-api@vger.kernel.org, linux-kernel@vger.kernel.org, linux-mm@kvack.org, linux-kselftest@vger.kernel.org, Aleksa Sarai X-Developer-Signature: v=1; a=openpgp-sha256; l=6045; i=cyphar@cyphar.com; h=from:subject:message-id; bh=Oi08rzV4zlV5QA1O8wafoqXVIjsJnVHyeP4wDfF54wA=; b=owGbwMvMwCWmMf3Xpe0vXfIZT6slMaTcfLFS1FRjkR3j8lvbuN47TmXl+1bCoFiVbf3R3WTBC n3tA5dMO0pZGMS4GGTFFFm2+XmGbpq/+Eryp5VsMHNYmUCGMHBxCsBEbt9l+O/W9O/Pzh2L/Hdf l+5+En+Lk4cleX5P3688r/3Mkb/X56oz/Ga9VZCQltttufi1/uNLe312cCSlnN3yuVogwfjKV9F 34WwA X-Developer-Key: i=cyphar@cyphar.com; a=openpgp; fpr=C9C370B246B09F6DBCFC744C34401015D1D2D386 X-Rspamd-Queue-Id: 63CF4120010 X-Rspam-User: X-Stat-Signature: idrsn7h84rywj9mobryrqtagyzh89o7w X-Rspamd-Server: rspam01 X-HE-Tag: 1692002500-702157 X-HE-Meta: U2FsdGVkX1+ZGZZHRp1/spr+hp9q1pZ25e0SWQPDL6LIRvq6vgoSrJjI9nDYbKnY6k8k8eeAKo4taEVsS2oTt22OFfwWxSJmBor/yVa9rbK/GYFHepNXUdMum1+1EXKE6YNshdUcgXsdwPNgI5TLVy9JMGJ7FcSc5Nh5PqdqCL5uaAwVFrLPfCs7kcWVdmAlWKoSKU0gsY8J3vCrOO2MpBPL/kIz6LnGH2spqs+686Yps6YJRMeRgH0QXaGSMEAz1xMupB82KAXvGebt9NGlWGGlyr54aYDjUzSCvoqV+t6Z5Y6EbFNORlm8+X25G2vByBIp7hBa9VLnj/DzEaKHQp0rvHA7aqInz1DPyy8Wc2UlS/RXO/EP7Iuyv4ErrIskUNCn6X3/DR7on7q4NOZE13MfgsVa8Di5Qf/FnIXyyM0jDQIFWmSG2X9/0Z5S5UEqXcB10TQCFwVCKrZhArQkhEIhQjwaNkG1boyWDx0+2ydFtli/1PcHF75BK6O9WqqTv7tmT0xpz9k00dj6TWffc0Knvmomk4uhLTN3SUzeeVsNtZmQDD5Ib1XdnU99paJ4bFABYBm05Ps6/ElcwvCU3+cYgSwv4nf5Uue6r11iutffjUFX5zsPLENZr2ux3bRQO+Uca5fjJjD0CAw/wVhE/LI+lJ/oUMhV74jGOF6aBPZ/KhD72VY5o3YNmMd34Ii/Ru8vsVpnaagJ/H7K9NoAIvGw0bq+QgyNxZPWuFB64JZsXCtN6rCs/oLpg6EpltmIriXw3OSQcDi9AGi1pqzgqMKgceM3ApcQq+VcFtIL836hOi1wTFuFbGNvAvwuWOeNlBaueNT79oT+EonIm96n5bMaG3fsQVZhi5p0IHQMh6dq23kJMj7j7QdL0ciVap0MNJfD0isAda6YWDvKHGkUPgvLAz6TP0XFYSytjyTQbSzMMI83rRzwNL4O3ce2D1a0i3lH2l2Tu5teddtqEbk Eblj7rSK 7+NhHwjQkXuIMvHpWBf41/VoYGO++ehfSeuhAXSFgSzAZBfcyyJxZJiaOfBKSrReDJlR6tvzKJNx9pQMw0rIEN5WiC5Fl1zE22mX8jB6vi0RTI/5zntFTuME2Ju6YyLVtLWEHqSxkPRo5DUeU6Cnf+vaTmIQuwNy1FGhrCTIJC75HRgs2/sSk3hBLEuF5Kp8xZi4FUpPof1K50XDHjkDKGPXZ9PltHHxpb269g+RHB+TUuniJFqyrNqTJx250JnlcF7n+Sqs2nUCpvTq/ajyXv+BwAgxK0DBVkHWd+x36rOOm6bH2iOtEcAVTB7el1hOFF/0DbM6E/QNJwRMVOHiM9Nt9A0LG/FB+awGhDnXhdauO/x8LVSqADyCNqQ== 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: Given the difficulty of auditing all of userspace to figure out whether every memfd_create() user has switched to passing MFD_EXEC and MFD_NOEXEC_SEAL flags, it seems far less distruptive to make it possible for older programs that don't make use of executable memfds to run under vm.memfd_noexec=2. Otherwise, a small dependency change can result in spurious errors. For programs that don't use executable memfds, passing MFD_NOEXEC_SEAL is functionally a no-op and thus having the same In addition, every failure under vm.memfd_noexec=2 needs to print to the kernel log so that userspace can figure out where the error came from. The concerns about pr_warn_ratelimited() spam that caused the switch to pr_warn_once()[1,2] do not apply to the vm.memfd_noexec=2 case. This is a user-visible API change, but as it allows programs to do something that would be blocked before, and the sysctl itself was broken and recently released, it seems unlikely this will cause any issues. [1]: https://lore.kernel.org/Y5yS8wCnuYGLHMj4@x1n/ [2]: https://lore.kernel.org/202212161233.85C9783FB@keescook/ 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 --- include/linux/pid_namespace.h | 16 ++++------------ mm/memfd.c | 30 +++++++++++------------------- tools/testing/selftests/memfd/memfd_test.c | 22 +++++++++++++++++----- 3 files changed, 32 insertions(+), 36 deletions(-) diff --git a/include/linux/pid_namespace.h b/include/linux/pid_namespace.h index c758809d5bcf..53974d79d98e 100644 --- a/include/linux/pid_namespace.h +++ b/include/linux/pid_namespace.h @@ -17,18 +17,10 @@ struct fs_pin; #if defined(CONFIG_SYSCTL) && defined(CONFIG_MEMFD_CREATE) -/* - * sysctl for vm.memfd_noexec - * 0: memfd_create() without MFD_EXEC nor MFD_NOEXEC_SEAL - * acts like MFD_EXEC was set. - * 1: memfd_create() without MFD_EXEC nor MFD_NOEXEC_SEAL - * acts like MFD_NOEXEC_SEAL was set. - * 2: memfd_create() without MFD_NOEXEC_SEAL will be - * rejected. - */ -#define MEMFD_NOEXEC_SCOPE_EXEC 0 -#define MEMFD_NOEXEC_SCOPE_NOEXEC_SEAL 1 -#define MEMFD_NOEXEC_SCOPE_NOEXEC_ENFORCED 2 +/* modes for vm.memfd_noexec sysctl */ +#define MEMFD_NOEXEC_SCOPE_EXEC 0 /* MFD_EXEC implied if unset */ +#define MEMFD_NOEXEC_SCOPE_NOEXEC_SEAL 1 /* MFD_NOEXEC_SEAL implied if unset */ +#define MEMFD_NOEXEC_SCOPE_NOEXEC_ENFORCED 2 /* same as 1, except MFD_EXEC rejected */ #endif struct pid_namespace { diff --git a/mm/memfd.c b/mm/memfd.c index 0bdbd2335af7..d65485c762de 100644 --- a/mm/memfd.c +++ b/mm/memfd.c @@ -271,30 +271,22 @@ long memfd_fcntl(struct file *file, unsigned int cmd, unsigned int arg) static int check_sysctl_memfd_noexec(unsigned int *flags) { #ifdef CONFIG_SYSCTL - char comm[TASK_COMM_LEN]; - int sysctl = MEMFD_NOEXEC_SCOPE_EXEC; - struct pid_namespace *ns; - - ns = task_active_pid_ns(current); - if (ns) - sysctl = ns->memfd_noexec_scope; + int sysctl = task_active_pid_ns(current)->memfd_noexec_scope; if (!(*flags & (MFD_EXEC | MFD_NOEXEC_SEAL))) { - if (sysctl == MEMFD_NOEXEC_SCOPE_NOEXEC_SEAL) + if (sysctl >= MEMFD_NOEXEC_SCOPE_NOEXEC_SEAL) *flags |= MFD_NOEXEC_SEAL; else *flags |= MFD_EXEC; } - if (*flags & MFD_EXEC && sysctl >= MEMFD_NOEXEC_SCOPE_NOEXEC_ENFORCED) { - pr_warn_once( - "memfd_create(): MFD_NOEXEC_SEAL is enforced, pid=%d '%s'\n", - task_pid_nr(current), get_task_comm(comm, current)); - + if (!(*flags & MFD_NOEXEC_SEAL) && sysctl >= MEMFD_NOEXEC_SCOPE_NOEXEC_ENFORCED) { + pr_err_ratelimited( + "%s[%d]: memfd_create() requires MFD_NOEXEC_SEAL with vm.memfd_noexec=%d\n", + current->comm, task_pid_nr(current), sysctl); return -EACCES; } #endif - return 0; } @@ -302,7 +294,6 @@ SYSCALL_DEFINE2(memfd_create, const char __user *, uname, unsigned int, flags) { - char comm[TASK_COMM_LEN]; unsigned int *file_seals; struct file *file; int fd, error; @@ -325,12 +316,13 @@ SYSCALL_DEFINE2(memfd_create, if (!(flags & (MFD_EXEC | MFD_NOEXEC_SEAL))) { pr_warn_once( - "memfd_create() without MFD_EXEC nor MFD_NOEXEC_SEAL, pid=%d '%s'\n", - task_pid_nr(current), get_task_comm(comm, current)); + "%s[%d]: memfd_create() called without MFD_EXEC or MFD_NOEXEC_SEAL set\n", + current->comm, task_pid_nr(current)); } - if (check_sysctl_memfd_noexec(&flags) < 0) - return -EACCES; + error = check_sysctl_memfd_noexec(&flags); + if (error < 0) + return error; /* length includes terminating zero */ len = strnlen_user(uname, MFD_NAME_MAX_LEN + 1); diff --git a/tools/testing/selftests/memfd/memfd_test.c b/tools/testing/selftests/memfd/memfd_test.c index 8eb49204f9ea..8b7390ad81d1 100644 --- a/tools/testing/selftests/memfd/memfd_test.c +++ b/tools/testing/selftests/memfd/memfd_test.c @@ -1145,11 +1145,23 @@ static void test_sysctl_child(void) printf("%s sysctl 2\n", memfd_str); sysctl_assert_write("2"); - mfd_fail_new("kern_memfd_sysctl_2", - MFD_CLOEXEC | MFD_ALLOW_SEALING); - mfd_fail_new("kern_memfd_sysctl_2_MFD_EXEC", - MFD_CLOEXEC | MFD_EXEC); - fd = mfd_assert_new("", 0, MFD_NOEXEC_SEAL); + mfd_fail_new("kern_memfd_sysctl_2_exec", + MFD_EXEC | MFD_CLOEXEC | MFD_ALLOW_SEALING); + + fd = mfd_assert_new("kern_memfd_sysctl_2_dfl", + mfd_def_size, + MFD_CLOEXEC | MFD_ALLOW_SEALING); + mfd_assert_mode(fd, 0666); + mfd_assert_has_seals(fd, F_SEAL_EXEC); + mfd_fail_chmod(fd, 0777); + close(fd); + + fd = mfd_assert_new("kern_memfd_sysctl_2_noexec_seal", + mfd_def_size, + MFD_NOEXEC_SEAL | MFD_CLOEXEC | MFD_ALLOW_SEALING); + mfd_assert_mode(fd, 0666); + mfd_assert_has_seals(fd, F_SEAL_EXEC); + mfd_fail_chmod(fd, 0777); close(fd); sysctl_fail_write("0"); From patchwork Mon Aug 14 08:40:59 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Aleksa Sarai X-Patchwork-Id: 13352565 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 5165CEB64DD for ; Mon, 14 Aug 2023 08:41:48 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id E58A08E0001; Mon, 14 Aug 2023 04:41:47 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id E07E5900003; Mon, 14 Aug 2023 04:41:47 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id CA9FE8E0005; Mon, 14 Aug 2023 04:41:47 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id BB19C8E0001 for ; Mon, 14 Aug 2023 04:41:47 -0400 (EDT) Received: from smtpin08.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id 84F39C0B1D for ; Mon, 14 Aug 2023 08:41:47 +0000 (UTC) X-FDA: 81122067054.08.42AE994 Received: from mout-p-202.mailbox.org (mout-p-202.mailbox.org [80.241.56.172]) by imf15.hostedemail.com (Postfix) with ESMTP id 860E1A0004 for ; Mon, 14 Aug 2023 08:41:45 +0000 (UTC) Authentication-Results: imf15.hostedemail.com; dkim=pass header.d=cyphar.com header.s=MBO0001 header.b=p1mH3+eN; spf=pass (imf15.hostedemail.com: domain of cyphar@cyphar.com designates 80.241.56.172 as permitted sender) smtp.mailfrom=cyphar@cyphar.com; dmarc=pass (policy=reject) header.from=cyphar.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1692002505; a=rsa-sha256; cv=none; b=RsLyif+oPnrgo0W0zBr5OMl8f+vF5mJxMLUPeu3JCEIgosayirqhqXzKD8SP05Bi+fAgJk J1sJathNnbrFUrZ42WBKSPhi8QilJqTtCfCefGVLkk7u0bL0D6UYZCc+KVk5eNac2jS9ua fx7Ll1DK2Dp2GhQPhb8YQMi5q5Gy3Xc= ARC-Authentication-Results: i=1; imf15.hostedemail.com; dkim=pass header.d=cyphar.com header.s=MBO0001 header.b=p1mH3+eN; spf=pass (imf15.hostedemail.com: domain of cyphar@cyphar.com designates 80.241.56.172 as permitted sender) smtp.mailfrom=cyphar@cyphar.com; dmarc=pass (policy=reject) header.from=cyphar.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1692002505; 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-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=79RoJoR/fwEl868XiYEwCuZo1mWc1anAic2p8X8NglE=; b=p3ARRXIeb9yaou/+svn/oFByieVqx4+pwsOMx9Lrkh1h4TvUS0Z4cik6cQheV8LZZAY0JU FJ2GwvTEXA+gqiMHzic8O+cRyF7qrYJnxNaLa3cDwOwWkcZLokJ7BDUsRRpN12fP2xf+3K 2WOiGec8wzqMY2iOx+cruBLdlJF1BFE= Received: from smtp202.mailbox.org (smtp202.mailbox.org [IPv6:2001:67c:2050:b231:465::202]) (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-202.mailbox.org (Postfix) with ESMTPS id 4RPSYd70PJz9slh; Mon, 14 Aug 2023 10:41:41 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cyphar.com; s=MBO0001; t=1692002502; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=79RoJoR/fwEl868XiYEwCuZo1mWc1anAic2p8X8NglE=; b=p1mH3+eN9eCVXdi/TxEUdL6OIDyTpQYTMvtKaZZl79SDtqiMak9oJhVRBoeaG94oNA8btJ PXKhAtBfpm7RHIgirGHwamL0Dqr5iJzMUBPRM7gjZR47hIhN/BQmCcoL7ufFIkmwRd7HqN mVSznBoc06lgSX95l1aKE8O5Iqd0oGrgrweoODmiJ5QxpRealAixDosNM60Yo40IYyHmgg UYfjbMmbOD9Hz5BIR3vTbMmxgQcmoaVDW7Tiy9rJ0hv/5isMvaFDnVF17zljfHsxF5j7Ls UyjsKXdyA9wOQ660D6kuX8HSkD2lACAmd416Q3kl9z/0UdxSYT4GZVOm+puhqA== From: Aleksa Sarai Date: Mon, 14 Aug 2023 18:40:59 +1000 Subject: [PATCH v2 3/5] memfd: improve userspace warnings for missing exec-related flags MIME-Version: 1.0 Message-Id: <20230814-memfd-vm-noexec-uapi-fixes-v2-3-7ff9e3e10ba6@cyphar.com> References: <20230814-memfd-vm-noexec-uapi-fixes-v2-0-7ff9e3e10ba6@cyphar.com> In-Reply-To: <20230814-memfd-vm-noexec-uapi-fixes-v2-0-7ff9e3e10ba6@cyphar.com> To: Andrew Morton , Shuah Khan , Jeff Xu , Kees Cook , Daniel Verkamp Cc: Christian Brauner , Dominique Martinet , stable@vger.kernel.org, linux-api@vger.kernel.org, linux-kernel@vger.kernel.org, linux-mm@kvack.org, linux-kselftest@vger.kernel.org, Aleksa Sarai X-Developer-Signature: v=1; a=openpgp-sha256; l=2138; i=cyphar@cyphar.com; h=from:subject:message-id; bh=RhSvSYmzoCDTkzH6eBS7S4EFPaKzfTKNkGx9zoaFDXg=; b=owGbwMvMwCWmMf3Xpe0vXfIZT6slMaTcfLHKkqnzauO+Q1m9W/cV6QoIafZdM/7bxXw//nBq8 PzZ8dumdpSyMIhxMciKKbJs8/MM3TR/8ZXkTyvZYOawMoEMYeDiFICJPFNl+F/zy9EjaynTW+l7 ofv84tUT6+bn3924QbCv9tcRc+nsOj5GhjfxJfaqHXPWTy+PW/w4KtRJ8Llqn1Da/fm3D9eIrua pYQcA X-Developer-Key: i=cyphar@cyphar.com; a=openpgp; fpr=C9C370B246B09F6DBCFC744C34401015D1D2D386 X-Rspamd-Server: rspam08 X-Rspamd-Queue-Id: 860E1A0004 X-Stat-Signature: y7sh7m1t1pxgio66qm7htxa7sfggadqm X-Rspam-User: X-HE-Tag: 1692002505-919138 X-HE-Meta: U2FsdGVkX18lwm0ldfkt1YPCABTfi+qdL7FBiJFB6jWufI/5ZBe5LuRJ4m9c4SB1zPNrito1+y+E9KCf2nbl71ogKGxUghLowo2N5wzZcVYh1Qg4Gj6a/EXwgQUclXS9YXeq8KPKMEaBiR0CglBBvXBeHJIhJhSzaDd/2bz04NUQB9sNbTRZSS6osmBFNJrWEDvQKpPiQnDwPYhmJ312EnOG2x/DCwCUnGPxCpuyAPE65yU8kuG5MRnm9DpJ1KZKJMXduj4MB9yIrV+hNT3wejro5C4r8lft4cDbRkF/w3JTUWrgQFhi63ZZ8K8HbQUR6UjzTcvWSasjap+ivT1zaiIBMICR3jtzaoYR65QrS6/ESV/qH4dGixAN+Y5Lg71lb6NWTMyl2B9mH7EnccheTLhDJcKm3beKRbNZrs6j/IOZTSC8zvQ5mFxaI5+frAYI0UsU7FxvtQfjtaLIMwQf7u7ASfO68237y1sAQBusHf8VJE4YG0wuob9fpNFCXqnjDcIUupG0/xqtDDgzP2mCrxz8vT5H1YlOpm6SZsFbTC+n1FEfs3jrzvSQCuJjHcadx++5/ofIhzpWLQtAvQlTGRoSpo+N5VX5QT/shrktg4HVpeURvXbw9vB9seWcLyCcc2cQa18WQrTYhibg7xC6t+MnSHYtEVuWzolRe9Hfn/LDm/Huao+wgq2LNqLHgAzdD4bYmSP7urH4p2F1O+gzfSvlCH4PwYrc6gbRmPkDyYw9jXglO0gnSxHE+Hv/pnpy5vb9eqhJbXL/wR/getSclLkuf0nFmxYmvbW3DWOICd6ply8/2/SLJrac7UVyE2RMYVz/p9GbQdQ22NApx63tEMKZD/+rhQMF9lWqe1Iub/nvADkUY7nhFaVzw3G5xhXxoIxi4Pup2J5Ef95LMKut1XwdPDdLZPQ0vevRWH2HCp+AOzs6zyPPBfqa8tUm1SWkxk6pm3pZgF0Q42qdjhc QH/AnC42 EoXDn53VcQoD7PaBgDPdLZXdoFuKGrUxNYJVKvfjQqOdxalX3/YLG8379rLA4lhPzejpuKs1O5e5/TgfmFQ5q9IDpXdopkII6PznhlCYqJvhmHdxQHJukv1FGp1neZx3fNW1FXSOhtlQIBGS4EuDKMaZRLVd858CuX+hv/GeOu72XOYOkm7o8+Gle6GE31E5wY+JnPpcM8ss+rzD9UjQEXpdo6Hke0vdaETE4jH1mnYon5bNz8Y0ALcsfQAa3e62lnJcnSZ35BqPywRlKdFubB0Z/gdIMZtl0P62gh7eowKFpWhrnvK/w6Mx7i0oAx+cjMYTlfzBZT8X1dmh1EKf+KP8bpYs4zXzNlNPAQFfXhW+k7ALkftVGymx9hQ== 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: In order to incentivise userspace to switch to passing MFD_EXEC and MFD_NOEXEC_SEAL, we need to provide a warning on each attempt to call memfd_create() without the new flags. pr_warn_once() is not useful because on most systems the one warning is burned up during the boot process (on my system, systemd does this within the first second of boot) and thus userspace will in practice never see the warnings to push them to switch to the new flags. The original patchset[1] used pr_warn_ratelimited(), however there were concerns about the degree of spam in the kernel log[2,3]. The resulting inability to detect every case was flagged as an issue at the time[4]. While we could come up with an alternative rate-limiting scheme such as only outputting the message if vm.memfd_noexec has been modified, or only outputting the message once for a given task, these alternatives have downsides that don't make sense given how low-stakes a single kernel warning message is. Switching to pr_info_ratelimited() instead should be fine -- it's possible some monitoring tool will be unhappy with a stream of warning-level messages but there's already plenty of info-level message spam in dmesg. [1]: https://lore.kernel.org/20221215001205.51969-4-jeffxu@google.com/ [2]: https://lore.kernel.org/202212161233.85C9783FB@keescook/ [3]: https://lore.kernel.org/Y5yS8wCnuYGLHMj4@x1n/ [4]: https://lore.kernel.org/f185bb42-b29c-977e-312e-3349eea15383@linuxfoundation.org/ Cc: stable@vger.kernel.org # v6.3+ Fixes: 105ff5339f49 ("mm/memfd: add MFD_NOEXEC_SEAL and MFD_EXEC") Signed-off-by: Aleksa Sarai Reviewed-by: Christian Brauner --- mm/memfd.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mm/memfd.c b/mm/memfd.c index d65485c762de..aa46521057ab 100644 --- a/mm/memfd.c +++ b/mm/memfd.c @@ -315,7 +315,7 @@ SYSCALL_DEFINE2(memfd_create, return -EINVAL; if (!(flags & (MFD_EXEC | MFD_NOEXEC_SEAL))) { - pr_warn_once( + pr_info_ratelimited( "%s[%d]: memfd_create() called without MFD_EXEC or MFD_NOEXEC_SEAL set\n", current->comm, task_pid_nr(current)); } From patchwork Mon Aug 14 08:41:00 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Aleksa Sarai X-Patchwork-Id: 13352566 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 69BA3EB64DD for ; Mon, 14 Aug 2023 08:41:53 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 09D5A8E0003; Mon, 14 Aug 2023 04:41:53 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 02790900003; Mon, 14 Aug 2023 04:41:52 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id E08D68E0005; Mon, 14 Aug 2023 04:41:52 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id CDF738E0003 for ; Mon, 14 Aug 2023 04:41:52 -0400 (EDT) Received: from smtpin08.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id A12791C9264 for ; Mon, 14 Aug 2023 08:41:52 +0000 (UTC) X-FDA: 81122067264.08.C91FB2B Received: from mout-p-103.mailbox.org (mout-p-103.mailbox.org [80.241.56.161]) by imf20.hostedemail.com (Postfix) with ESMTP id B2F531C0005 for ; Mon, 14 Aug 2023 08:41:50 +0000 (UTC) Authentication-Results: imf20.hostedemail.com; dkim=pass header.d=cyphar.com header.s=MBO0001 header.b=FW4pAGCj; dmarc=pass (policy=reject) header.from=cyphar.com; spf=pass (imf20.hostedemail.com: domain of cyphar@cyphar.com designates 80.241.56.161 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=1692002511; 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-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=YYw3VsM//wPuCzVBeDBi0yiXj1DgwyxLCg5CNbEnouM=; b=IRpEUh8DSw+WjW9URX3cl7oUA0pfx/Bjs/TSkdgxFQVVNCw8fJBdPktg25yR/4iW6Tx+Sd 08e1xfqgM+CtywZFfA+Ui5X0jOJTkdnCw9FZF43DYvviI1ynQPG+BLaXTwrmjNgcGna2Dx U3PrQm9JLOn+TF7yeNzzGwkLdtc4SMU= ARC-Authentication-Results: i=1; imf20.hostedemail.com; dkim=pass header.d=cyphar.com header.s=MBO0001 header.b=FW4pAGCj; dmarc=pass (policy=reject) header.from=cyphar.com; spf=pass (imf20.hostedemail.com: domain of cyphar@cyphar.com designates 80.241.56.161 as permitted sender) smtp.mailfrom=cyphar@cyphar.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1692002511; a=rsa-sha256; cv=none; b=bvmjZ851g9nmiMuMO9zcmETW63fB++xTH69YG2Q+XPdAqpLBMXed4R9nSUrtKkeExVuLom rr/QgrIL+EIR1zvn6vo3L0JP2yStBYrT/sKnMVTrOw3PdziDzFo7Q5lbJGXFM0vxrn1N1S pQA2Lg+OM+jeE2FiJbp1gyAI5UXjFZ8= Received: from smtp202.mailbox.org (smtp202.mailbox.org [IPv6:2001:67c:2050:b231:465::202]) (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-103.mailbox.org (Postfix) with ESMTPS id 4RPSYl1sPZz9sSk; Mon, 14 Aug 2023 10:41:47 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cyphar.com; s=MBO0001; t=1692002507; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=YYw3VsM//wPuCzVBeDBi0yiXj1DgwyxLCg5CNbEnouM=; b=FW4pAGCjQrxS9kPO8UYvY2wfmvyzfNE3ar8tAhYT8AGZbuNMo3b1ODPydY1aGAH/x6LhX4 6jB1DEBjKA9mW8gjTHmKZDuQjEFV1Ld0MeCYsSdvcJ5jDvlZahmyqXlYJu+bB1cHTZjR3R 1RB+CQPRho3REB3s13nRV+Dh2sR6yKNBKA++UGvK3B+kN/Jju9zriOkLqiO9YUDU6qE3tV oZrb9a3lSzcun2V7BNPrWtpVzjfmlnHrx7smaVNTSHPWfJsG/9UNHSkf9XPZATyTsyurvc ST1H8jrxOmhNk3e4vW0vdgILUT3YKEsLxGdZIOKw0kJ23wcPum1m1VfCLeoF/g== From: Aleksa Sarai Date: Mon, 14 Aug 2023 18:41:00 +1000 Subject: [PATCH v2 4/5] memfd: replace ratcheting feature from vm.memfd_noexec with hierarchy MIME-Version: 1.0 Message-Id: <20230814-memfd-vm-noexec-uapi-fixes-v2-4-7ff9e3e10ba6@cyphar.com> References: <20230814-memfd-vm-noexec-uapi-fixes-v2-0-7ff9e3e10ba6@cyphar.com> In-Reply-To: <20230814-memfd-vm-noexec-uapi-fixes-v2-0-7ff9e3e10ba6@cyphar.com> To: Andrew Morton , Shuah Khan , Jeff Xu , Kees Cook , Daniel Verkamp Cc: Christian Brauner , Dominique Martinet , stable@vger.kernel.org, linux-api@vger.kernel.org, linux-kernel@vger.kernel.org, linux-mm@kvack.org, linux-kselftest@vger.kernel.org, Aleksa Sarai X-Developer-Signature: v=1; a=openpgp-sha256; l=10124; i=cyphar@cyphar.com; h=from:subject:message-id; bh=U7ufwXeR3BywMTVYzMKLH+ocS470Rxhe9PUxTLcsuDk=; b=owGbwMvMwCWmMf3Xpe0vXfIZT6slMaTcfLEmXWFyS8qGo5za91//3FAcP4OVm1d+JpNYzjUHD eYH+xjiOkpZGMS4GGTFFFm2+XmGbpq/+Eryp5VsMHNYmUCGMHBxCsBE/s5g+Gch39W/f80Jq8Qd K2Jb0u73q+yUWZJ6uXjCllft3Ze4gkQZGX49jo+etthCvmLRglfvPhh+Ot27+ojTmuMTlKqaKsz tfvMCAA== X-Developer-Key: i=cyphar@cyphar.com; a=openpgp; fpr=C9C370B246B09F6DBCFC744C34401015D1D2D386 X-Rspamd-Queue-Id: B2F531C0005 X-Rspam-User: X-Rspamd-Server: rspam05 X-Stat-Signature: wdew5k4u6umqy1yzw7is719fy1ref8e4 X-HE-Tag: 1692002510-452402 X-HE-Meta: U2FsdGVkX193Z3XWQVe3X68gsQLlUGJAG0c/yU+lCXxuFZHgGBVDKhrkM7U1Jg8NgbpYkWQQI+aXEbzO5tifar7M0sVDwuQ8PxyJ1LrYkLUyvNBSPbkHfFP8w8mx0jnKzWh2CkwN6A8Vx4xva3OErd2dZPXmo3V0omFlf8fZZVtELLv4OeM0FXWSe/pBn8/yHK1SB8yBKMEPiLuzrOonQwSU8lHGBRSC/Wc1Qw8+abIVLtW/KT6ANCsQQhSlIAgXze7XoHiWN+qaiZpRGLVMm3ojueB6mBmubpefTYbz6RLWlC618zdeHhoYobrrFWBVhZAJyWy/H3to9tE7/Trc0+v1Ln+myDHcK9ufBe3caBZPU0kzBWXu+i+CbJy88638ki2cj6MGlQQn8YXGNMRgVlsYFtRkD9MLnewsN5eTO3dBi/7t2IfLrnqm4XC83fcKNRHsT8qAYRn6h3bd+2BhOs9jDKp47wihycSBHHQtOwwbit+Vmy6F+2i2rd8RD5bBhAE5Sf33j1dOa+gRjHHToq6UtAUTnEj9RLeMt5+5miQMg8hhoFMs7rxIs309VmXmbERjY+YF1uzhUM7mYmFOs8OtX7UGiCdtvlSX1oGY8ng3hNiDjHtb6HyVkSKUYAWzi83cdjAgdGGDjaXvwSr2ZmQzjDUf50TozZQVVO4o7PRLgFofhuICpMpTLzL9Ye3t4dl6cA2e3Dyef2hHdVoaRsdBDgDn9fRJqZScDYrthln3f3e8xwJtW8M0rtERr4YyqkE9HeNtseOKKBnsPAB1yhIGXBWNWGxLDPkUcrmwqxbRsmSQwgOl6X9Z3+OSPGH3HIY5JwT9oxhsmuzmfrx3GNd1BB/ewKakSDNYzCMaSNkQSXP7Qw31pGRrx9CvnAEwGKAHglsjeRWrhujjEeZOQjPpoA10pAKvZSb6tpMEKoCV+OHyU9V+DIT4EZwVVrYg7T0rZa4bykINosls3wp VXMtIrgB +LyhM8wdLHXwItPfBQws0osxtfuoV8NcTISIKyHyFzP0XNDK4NJk5e6U1k6kRS2ZJj76q9eHnXVDyaA8I1x1N3Umw7VasX+D1q4USU7wR7E9EbxcpcypTSjhvFLatw8mJWASoSeHoJ+Sx6pWkc5m54YQxtYiNdCjEfu/F2JZUnjwm8o+/bYUSG7jSg4b6SgS9nRyW+tITXOWKAxMoSmxURs9sFzpvzgzVj0QCyvSiQDaOC8S3WEdcFQ3nrAsbol7V4SrpzxhmNZoQaqgMEic5IDggvJFBGOUMnXtW8wcTusSWhOOuxiM4s9dGrdq06h6A6rfs/8kWUUruThlwTIarOaAskSSjKGpzVayTPwCLrPr5jzb8QWyn+3LD8ahMCLCemSEs56NWibZ7Dx8XZ1OsUBynaMvIdV2OeZc1C436fqZ4PjU= 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 unusual 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. There appear to be concerns about confused-deputy-esque attacks[2] but a confused deputy that can write to arbitrary sysctls is a bigger security issue than executable memfds. /* New API */ The primary requirement from the original author appears to be more based on the need to be able to restrict an entire system in a hierarchical manner[3], such that child namespaces cannot re-enable executable memfds. So, implement that behaviour explicitly -- the vm.memfd_noexec scope is evaluated up the pidns tree to &init_pid_ns and you have the most restrictive value applied to you. The new lower limit you can set vm.memfd_noexec is whatever limit applies to your parent. Note that a pidns will inherit a copy of the parent pidns's effective vm.memfd_noexec setting at unshare() time. This matches the existing behaviour, and it also ensures that a pidns will never have its vm.memfd_noexec setting *lowered* behind its back (but it will be raised if the parent raises theirs). /* Backwards Compatibility */ As the previous version of the sysctl didn't allow you to lower the setting at all, there are no backwards compatibility issues with this aspect of the change. However it should be noted that now that the setting is completely hierarchical. Previously, a cloned pidns would just copy the current pidns setting, meaning that if the parent's vm.memfd_noexec was changed it wouldn't propoagate to existing pid namespaces. Now, the restriction applies recursively. This is a uAPI change, however: * The sysctl is very new, having been merged in 6.3. * Several aspects of the sysctl were broken up until this patchset and the other patchset by Jeff Xu last month. And thus it seems incredibly unlikely that any real users would run into this issue. In the worst case, if this causes userspace isues we could make it so that modifying the setting follows the hierarchical rules but the restriction checking uses the cached copy. [1]: https://lore.kernel.org/CABi2SkWnAgHK1i6iqSqPMYuNEhtHBkO8jUuCvmG3RmUB5TKHJw@mail.gmail.com/ [2]: https://lore.kernel.org/CALmYWFs_dNCzw_pW1yRAo4bGCPEtykroEQaowNULp7svwMLjOg@mail.gmail.com/ [3]: https://lore.kernel.org/CALmYWFuahdUF7cT4cm7_TGLqPanuHXJ-hVSfZt7vpTnc18DPrw@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 --- include/linux/pid_namespace.h | 23 ++++++++++++++++++++++- kernel/pid.c | 3 +++ kernel/pid_namespace.c | 6 +++--- kernel/pid_sysctl.h | 28 ++++++++++++---------------- mm/memfd.c | 3 ++- 5 files changed, 42 insertions(+), 21 deletions(-) diff --git a/include/linux/pid_namespace.h b/include/linux/pid_namespace.h index 53974d79d98e..f9f9931e02d6 100644 --- a/include/linux/pid_namespace.h +++ b/include/linux/pid_namespace.h @@ -39,7 +39,6 @@ struct pid_namespace { int reboot; /* group exit code if this pidns was rebooted */ struct ns_common ns; #if defined(CONFIG_SYSCTL) && defined(CONFIG_MEMFD_CREATE) - /* sysctl for vm.memfd_noexec */ int memfd_noexec_scope; #endif } __randomize_layout; @@ -56,6 +55,23 @@ static inline struct pid_namespace *get_pid_ns(struct pid_namespace *ns) return ns; } +#if defined(CONFIG_SYSCTL) && defined(CONFIG_MEMFD_CREATE) +static inline int pidns_memfd_noexec_scope(struct pid_namespace *ns) +{ + int scope = MEMFD_NOEXEC_SCOPE_EXEC; + + for (; ns; ns = ns->parent) + scope = max(scope, READ_ONCE(ns->memfd_noexec_scope)); + + return scope; +} +#else +static inline int pidns_memfd_noexec_scope(struct pid_namespace *ns) +{ + return 0; +} +#endif + extern struct pid_namespace *copy_pid_ns(unsigned long flags, struct user_namespace *user_ns, struct pid_namespace *ns); extern void zap_pid_ns_processes(struct pid_namespace *pid_ns); @@ -70,6 +86,11 @@ static inline struct pid_namespace *get_pid_ns(struct pid_namespace *ns) return ns; } +static inline int pidns_memfd_noexec_scope(struct pid_namespace *ns) +{ + return 0; +} + static inline struct pid_namespace *copy_pid_ns(unsigned long flags, struct user_namespace *user_ns, struct pid_namespace *ns) { diff --git a/kernel/pid.c b/kernel/pid.c index 6a1d23a11026..fee14a4486a3 100644 --- a/kernel/pid.c +++ b/kernel/pid.c @@ -83,6 +83,9 @@ struct pid_namespace init_pid_ns = { #ifdef CONFIG_PID_NS .ns.ops = &pidns_operations, #endif +#if defined(CONFIG_SYSCTL) && defined(CONFIG_MEMFD_CREATE) + .memfd_noexec_scope = MEMFD_NOEXEC_SCOPE_EXEC, +#endif }; EXPORT_SYMBOL_GPL(init_pid_ns); diff --git a/kernel/pid_namespace.c b/kernel/pid_namespace.c index 0bf44afe04dd..619972c78774 100644 --- a/kernel/pid_namespace.c +++ b/kernel/pid_namespace.c @@ -110,9 +110,9 @@ static struct pid_namespace *create_pid_namespace(struct user_namespace *user_ns ns->user_ns = get_user_ns(user_ns); ns->ucounts = ucounts; ns->pid_allocated = PIDNS_ADDING; - - initialize_memfd_noexec_scope(ns); - +#if defined(CONFIG_SYSCTL) && defined(CONFIG_MEMFD_CREATE) + ns->memfd_noexec_scope = pidns_memfd_noexec_scope(parent_pid_ns); +#endif return ns; out_free_idr: diff --git a/kernel/pid_sysctl.h b/kernel/pid_sysctl.h index b26e027fc9cd..2ee41a3a1dfd 100644 --- a/kernel/pid_sysctl.h +++ b/kernel/pid_sysctl.h @@ -5,33 +5,30 @@ #include #if defined(CONFIG_SYSCTL) && defined(CONFIG_MEMFD_CREATE) -static inline void initialize_memfd_noexec_scope(struct pid_namespace *ns) -{ - ns->memfd_noexec_scope = - task_active_pid_ns(current)->memfd_noexec_scope; -} - static int pid_mfd_noexec_dointvec_minmax(struct ctl_table *table, int write, void *buf, size_t *lenp, loff_t *ppos) { struct pid_namespace *ns = task_active_pid_ns(current); struct ctl_table table_copy; + int err, scope, parent_scope; if (write && !ns_capable(ns->user_ns, CAP_SYS_ADMIN)) return -EPERM; table_copy = *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; + /* You cannot set a lower enforcement value than your parent. */ + parent_scope = pidns_memfd_noexec_scope(ns->parent); + /* Equivalent to pidns_memfd_noexec_scope(ns). */ + scope = max(READ_ONCE(ns->memfd_noexec_scope), parent_scope); + + table_copy.data = &scope; + table_copy.extra1 = &parent_scope; - return proc_dointvec_minmax(&table_copy, write, buf, lenp, ppos); + err = proc_dointvec_minmax(&table_copy, write, buf, lenp, ppos); + if (!err && write) + WRITE_ONCE(ns->memfd_noexec_scope, scope); + return err; } static struct ctl_table pid_ns_ctl_table_vm[] = { @@ -51,7 +48,6 @@ static inline void register_pid_ns_sysctl_table_vm(void) register_sysctl("vm", pid_ns_ctl_table_vm); } #else -static inline void initialize_memfd_noexec_scope(struct pid_namespace *ns) {} static inline void register_pid_ns_sysctl_table_vm(void) {} #endif diff --git a/mm/memfd.c b/mm/memfd.c index aa46521057ab..1cad1904fc26 100644 --- a/mm/memfd.c +++ b/mm/memfd.c @@ -271,7 +271,8 @@ long memfd_fcntl(struct file *file, unsigned int cmd, unsigned int arg) static int check_sysctl_memfd_noexec(unsigned int *flags) { #ifdef CONFIG_SYSCTL - int sysctl = task_active_pid_ns(current)->memfd_noexec_scope; + struct pid_namespace *ns = task_active_pid_ns(current); + int sysctl = pidns_memfd_noexec_scope(ns); if (!(*flags & (MFD_EXEC | MFD_NOEXEC_SEAL))) { if (sysctl >= MEMFD_NOEXEC_SCOPE_NOEXEC_SEAL) From patchwork Mon Aug 14 08:41:01 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Aleksa Sarai X-Patchwork-Id: 13352567 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 540B0C001B0 for ; Mon, 14 Aug 2023 08:41:58 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id E58A4900004; Mon, 14 Aug 2023 04:41:57 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id E07D7900003; Mon, 14 Aug 2023 04:41:57 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id CA8EB900004; Mon, 14 Aug 2023 04:41:57 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id B9832900003 for ; Mon, 14 Aug 2023 04:41:57 -0400 (EDT) Received: from smtpin08.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id 991D51C926F for ; Mon, 14 Aug 2023 08:41:57 +0000 (UTC) X-FDA: 81122067474.08.D75482D Received: from mout-p-101.mailbox.org (mout-p-101.mailbox.org [80.241.56.151]) by imf22.hostedemail.com (Postfix) with ESMTP id 97D10C0022 for ; Mon, 14 Aug 2023 08:41:55 +0000 (UTC) Authentication-Results: imf22.hostedemail.com; dkim=pass header.d=cyphar.com header.s=MBO0001 header.b=RMj4CUJ8; spf=pass (imf22.hostedemail.com: domain of cyphar@cyphar.com designates 80.241.56.151 as permitted sender) smtp.mailfrom=cyphar@cyphar.com; dmarc=pass (policy=reject) header.from=cyphar.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1692002515; 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-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=W7cNopV9QtwDfWAl6SLw4jWogCD7aoRQLSKJtTeB2Z4=; b=uevjYasVj7bhoJV4Dj3qYJaM++77j5Q8Krg2raDvPRDDeX+FmmW710Ia4dD3Ti7JAFW/G5 INpswnjS6BpJ/DIWAFZ2ACp2fbDoH4J4NchO7OPU2kbq5NGdgdeTR/94h3kYauMJzSAEwD gv3mVu4tcbgembKlAsJw0aQlAWbl6XM= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1692002515; a=rsa-sha256; cv=none; b=I8t7ZBfl2CfQtEe/c6ohez+KalnuTsb1WzZATSN7fg8CKjrA+n9zywx3Y6BCK1L/IsE237 N9FzrRzw61Zuf7KO9CGlqeoDaJnwgG53l5YFVaqNZZpwQpXfwMl9djfnIpxMrWCtJpxnrm XtnmD//b8iTvX29zqWTBWxUGRpS/HI8= ARC-Authentication-Results: i=1; imf22.hostedemail.com; dkim=pass header.d=cyphar.com header.s=MBO0001 header.b=RMj4CUJ8; spf=pass (imf22.hostedemail.com: domain of cyphar@cyphar.com designates 80.241.56.151 as permitted sender) smtp.mailfrom=cyphar@cyphar.com; dmarc=pass (policy=reject) header.from=cyphar.com Received: from smtp202.mailbox.org (smtp202.mailbox.org [10.196.197.202]) (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 4RPSYr29qcz9sn2; Mon, 14 Aug 2023 10:41:52 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cyphar.com; s=MBO0001; t=1692002512; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=W7cNopV9QtwDfWAl6SLw4jWogCD7aoRQLSKJtTeB2Z4=; b=RMj4CUJ8Rvd/ZNQXo32QMV7F9lurzqTpsl02OwCu1jTFQ0n1d0fmf72WxHZ1ua+c7jCd9S D1E9rniYp4s34lLn7rTOmuQpmh3X6pRzrzURyHouUZnlH8th5qc4KVaHELYz6gQeWZpln9 Vgr+Uwo20A0DzCdzDAKx/PsWiZrylfeBS0edlCGMrrdZhKBQMuJCc+nW+saFy+eUxwGf0d /C3mvRd8NQMshleiI648XFnhOm+WbbnHPwtFAXk+GOcWLpgSGxvim4H+QKq7B+9A3O8LTY AMH1dOh1rk34EaAWtAIFNW6K9gvaZ1cf5MF5xsgbBghS5oC5H3C0dlRfJeTWbA== From: Aleksa Sarai Date: Mon, 14 Aug 2023 18:41:01 +1000 Subject: [PATCH v2 5/5] selftests: improve vm.memfd_noexec sysctl tests MIME-Version: 1.0 Message-Id: <20230814-memfd-vm-noexec-uapi-fixes-v2-5-7ff9e3e10ba6@cyphar.com> References: <20230814-memfd-vm-noexec-uapi-fixes-v2-0-7ff9e3e10ba6@cyphar.com> In-Reply-To: <20230814-memfd-vm-noexec-uapi-fixes-v2-0-7ff9e3e10ba6@cyphar.com> To: Andrew Morton , Shuah Khan , Jeff Xu , Kees Cook , Daniel Verkamp Cc: Christian Brauner , Dominique Martinet , stable@vger.kernel.org, linux-api@vger.kernel.org, linux-kernel@vger.kernel.org, linux-mm@kvack.org, linux-kselftest@vger.kernel.org, Aleksa Sarai X-Developer-Signature: v=1; a=openpgp-sha256; l=13000; i=cyphar@cyphar.com; h=from:subject:message-id; bh=0sdx3MhmB3vX/NM+/SK9/yhr9yRSi3Geq1KMMGDcLxI=; b=owGbwMvMwCWmMf3Xpe0vXfIZT6slMaTcfLG2x/3+jGwuzwQtTl/LxLKg180rat3ZkqJalPZ28 m4xPnS+o5SFQYyLQVZMkWWbn2fopvmLryR/WskGM4eVCWQIAxenAEzk2QFGhs4PXJ+fSNazrA34 9lOx7d2F0lMb0t2/9PwKXp7O0yOxYwkjwxtGGx9O5lVLjRPsOj1U3HqnnHOabjbXuDJo82yHIOl 6XgA= X-Developer-Key: i=cyphar@cyphar.com; a=openpgp; fpr=C9C370B246B09F6DBCFC744C34401015D1D2D386 X-Stat-Signature: s31dj9kbyj9g51mmgttfqk66neyjuq83 X-Rspamd-Server: rspam10 X-Rspamd-Queue-Id: 97D10C0022 X-Rspam-User: X-HE-Tag: 1692002515-47921 X-HE-Meta: U2FsdGVkX1+iNQtpFjvdAAVd3KHduz9Fuqa/4CxeqWf8Kqhpn6ftJKCg0XHo2OMWElYVZi5qNhk95Hv/QRUp7vXm6ymhpTaF/oiAWiXi57CEeabkjbS2j3q+7BTZJc5IV5hYLOJ3iN0jKGg2qFdQj+VAXcKEHtYUMizdb8xDAdnA6596AyS5vsOYTeYntum36VIY3VKPVAc/vabfZt5JHMH1gU3EKT0hze4RGpr1313itCxaBsSZe3VZYwsFXQbDfzx4TKo0ki0kWhTVUS9YjCFjwXPD/dnqiuy6z8sr5pp+K8COEh6PIINPFfBpFuAMbZoGGCVAGJA1ie2/Zy5eBFomAWWVtOjO0bpcmokF4h83gKUtflcu+ki0lktQowThMwbV51sjYSAIg9ulV705Q1F0XxHg7PgaPGi4jK2xo0D+l8eM2JHPl9ePM9ylhxvwTMENk/51dEn9Pv4WOE55F7vJNrxVyKEhRSJeWEU8b/DtvHHmBlfZ8z/cADcJ8Ewm2UTm0o3tuz2UAFCY0mg6a3zPpGNpyepwfOrXYAn9cqM8kWPnHbCdBSzvRevo7BG0GhwuvOkt/BTO6EYdUSN0pfosCntT8G4iwyrK5uJi5A+Z1TavNmPN9hIcsXyA8C7CHUIVuvza0UqWcgEBihCro1ilgyuJHQ1tJKp94LAsN7xS5O7UO9WbJ9D4NXyGlpzhEaPouNgoM30e4HK7kTCgJozumX6DtUmLZD31g+ksNsa2idUnarJzUZnA8K/UvUz/s2BH1LPNIK3cdg44T+1L4YQq5jQQ8G0L0OlhJ3zPegpX37eu3E+kRU5EJlr6YdGkzN3fs0QAuJk6/4ndHbTDsjxwb/UNX+dmWxkXqGuswBPArWzLIBmiG2XOkgh1mEIxan8ms7Gtn2Q2cOYojfMl4ifsE7zsTP31twdQfGAXen8UZll4ZivJE77bDC2X9fDbA6+mahksEKUwgM6zKAP suLslQr7 q23xAFxXO+jitng48Y5DdPjO8KJJu/rIuIxnk6jHFZbl8bPSYgLoLLhVCSGX992iIefHsAZnFmjCz3TxGzTvY3ApGsoBZUFg2FW0kUh+lnZb6iqQ1u3y3KZkFdxeR+/XIpjKevOxl1+jpRf/Z0wlfc70ciCRzE2wZFFMG2fCAsncUvrbMLEmfwIq+IRzHx85n8scIAf6E23DC+p4NZ4/I/fdZd0qgtV2OIDEe7opFtwoIS/HWyznfg12WngvFTNvlMIdo 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 adds proper tests for the nesting functionality of vm.memfd_noexec as well as some minor cleanups to spawn_*_thread(). Signed-off-by: Aleksa Sarai --- tools/testing/selftests/memfd/memfd_test.c | 339 +++++++++++++++++++++-------- 1 file changed, 254 insertions(+), 85 deletions(-) diff --git a/tools/testing/selftests/memfd/memfd_test.c b/tools/testing/selftests/memfd/memfd_test.c index 8b7390ad81d1..3df008677239 100644 --- a/tools/testing/selftests/memfd/memfd_test.c +++ b/tools/testing/selftests/memfd/memfd_test.c @@ -18,6 +18,7 @@ #include #include #include +#include #include "common.h" @@ -43,7 +44,6 @@ */ static size_t mfd_def_size = MFD_DEF_SIZE; static const char *memfd_str = MEMFD_STR; -static pid_t spawn_newpid_thread(unsigned int flags, int (*fn)(void *)); static int newpid_thread_fn2(void *arg); static void join_newpid_thread(pid_t pid); @@ -96,12 +96,12 @@ static void sysctl_assert_write(const char *val) int fd = open("/proc/sys/vm/memfd_noexec", O_WRONLY | O_CLOEXEC); if (fd < 0) { - printf("open sysctl failed\n"); + printf("open sysctl failed: %m\n"); abort(); } if (write(fd, val, strlen(val)) < 0) { - printf("write sysctl failed\n"); + printf("write sysctl %s failed: %m\n", val); abort(); } } @@ -111,7 +111,7 @@ static void sysctl_fail_write(const char *val) int fd = open("/proc/sys/vm/memfd_noexec", O_WRONLY | O_CLOEXEC); if (fd < 0) { - printf("open sysctl failed\n"); + printf("open sysctl failed: %m\n"); abort(); } @@ -122,6 +122,33 @@ static void sysctl_fail_write(const char *val) } } +static void sysctl_assert_equal(const char *val) +{ + char *p, buf[128] = {}; + int fd = open("/proc/sys/vm/memfd_noexec", O_RDONLY | O_CLOEXEC); + + if (fd < 0) { + printf("open sysctl failed: %m\n"); + abort(); + } + + if (read(fd, buf, sizeof(buf)) < 0) { + printf("read sysctl failed: %m\n"); + abort(); + } + + /* Strip trailing whitespace. */ + p = buf; + while (!isspace(*p)) + p++; + *p = '\0'; + + if (strcmp(buf, val) != 0) { + printf("unexpected sysctl value: expected %s, got %s\n", val, buf); + abort(); + } +} + static int mfd_assert_reopen_fd(int fd_in) { int fd; @@ -736,7 +763,7 @@ static int idle_thread_fn(void *arg) return 0; } -static pid_t spawn_idle_thread(unsigned int flags) +static pid_t spawn_thread(unsigned int flags, int (*fn)(void *), void *arg) { uint8_t *stack; pid_t pid; @@ -747,10 +774,7 @@ static pid_t spawn_idle_thread(unsigned int flags) abort(); } - pid = clone(idle_thread_fn, - stack + STACK_SIZE, - SIGCHLD | flags, - NULL); + pid = clone(fn, stack + STACK_SIZE, SIGCHLD | flags, arg); if (pid < 0) { printf("clone() failed: %m\n"); abort(); @@ -759,6 +783,33 @@ static pid_t spawn_idle_thread(unsigned int flags) return pid; } +static void join_thread(pid_t pid) +{ + int wstatus; + + if (waitpid(pid, &wstatus, 0) < 0) { + printf("newpid thread: waitpid() failed: %m\n"); + abort(); + } + + if (WIFEXITED(wstatus) && WEXITSTATUS(wstatus) != 0) { + printf("newpid thread: exited with non-zero error code %d\n", + WEXITSTATUS(wstatus)); + abort(); + } + + if (WIFSIGNALED(wstatus)) { + printf("newpid thread: killed by signal %d\n", + WTERMSIG(wstatus)); + abort(); + } +} + +static pid_t spawn_idle_thread(unsigned int flags) +{ + return spawn_thread(flags, idle_thread_fn, NULL); +} + static void join_idle_thread(pid_t pid) { kill(pid, SIGTERM); @@ -1111,42 +1162,69 @@ static void test_noexec_seal(void) close(fd); } -static void test_sysctl_child(void) +static void test_sysctl_sysctl0(void) { int fd; - int pid; - printf("%s sysctl 0\n", memfd_str); - sysctl_assert_write("0"); - fd = mfd_assert_new("kern_memfd_sysctl_0", + sysctl_assert_equal("0"); + + fd = mfd_assert_new("kern_memfd_sysctl_0_dfl", mfd_def_size, MFD_CLOEXEC | MFD_ALLOW_SEALING); - mfd_assert_mode(fd, 0777); mfd_assert_has_seals(fd, 0); mfd_assert_chmod(fd, 0644); close(fd); +} - printf("%s sysctl 1\n", memfd_str); - sysctl_assert_write("1"); - fd = mfd_assert_new("kern_memfd_sysctl_1", +static void test_sysctl_set_sysctl0(void) +{ + sysctl_assert_write("0"); + test_sysctl_sysctl0(); +} + +static void test_sysctl_sysctl1(void) +{ + int fd; + + sysctl_assert_equal("1"); + + fd = mfd_assert_new("kern_memfd_sysctl_1_dfl", mfd_def_size, MFD_CLOEXEC | MFD_ALLOW_SEALING); + mfd_assert_mode(fd, 0666); + mfd_assert_has_seals(fd, F_SEAL_EXEC); + mfd_fail_chmod(fd, 0777); + close(fd); - printf("%s child ns\n", memfd_str); - pid = spawn_newpid_thread(CLONE_NEWPID, newpid_thread_fn2); - join_newpid_thread(pid); + fd = mfd_assert_new("kern_memfd_sysctl_1_exec", + mfd_def_size, + MFD_CLOEXEC | MFD_EXEC | MFD_ALLOW_SEALING); + mfd_assert_mode(fd, 0777); + mfd_assert_has_seals(fd, 0); + mfd_assert_chmod(fd, 0644); + close(fd); + fd = mfd_assert_new("kern_memfd_sysctl_1_noexec", + mfd_def_size, + MFD_CLOEXEC | MFD_NOEXEC_SEAL | MFD_ALLOW_SEALING); mfd_assert_mode(fd, 0666); mfd_assert_has_seals(fd, F_SEAL_EXEC); mfd_fail_chmod(fd, 0777); - sysctl_fail_write("0"); close(fd); +} - printf("%s sysctl 2\n", memfd_str); - sysctl_assert_write("2"); - mfd_fail_new("kern_memfd_sysctl_2_exec", - MFD_EXEC | MFD_CLOEXEC | MFD_ALLOW_SEALING); +static void test_sysctl_set_sysctl1(void) +{ + sysctl_assert_write("1"); + test_sysctl_sysctl1(); +} + +static void test_sysctl_sysctl2(void) +{ + int fd; + + sysctl_assert_equal("2"); fd = mfd_assert_new("kern_memfd_sysctl_2_dfl", mfd_def_size, @@ -1156,98 +1234,188 @@ static void test_sysctl_child(void) mfd_fail_chmod(fd, 0777); close(fd); - fd = mfd_assert_new("kern_memfd_sysctl_2_noexec_seal", + mfd_fail_new("kern_memfd_sysctl_2_exec", + MFD_CLOEXEC | MFD_EXEC | MFD_ALLOW_SEALING); + + fd = mfd_assert_new("kern_memfd_sysctl_2_noexec", mfd_def_size, - MFD_NOEXEC_SEAL | MFD_CLOEXEC | MFD_ALLOW_SEALING); + MFD_CLOEXEC | MFD_NOEXEC_SEAL | MFD_ALLOW_SEALING); mfd_assert_mode(fd, 0666); mfd_assert_has_seals(fd, F_SEAL_EXEC); mfd_fail_chmod(fd, 0777); close(fd); - - sysctl_fail_write("0"); - sysctl_fail_write("1"); } -static int newpid_thread_fn(void *arg) +static void test_sysctl_set_sysctl2(void) { - test_sysctl_child(); - return 0; + sysctl_assert_write("2"); + test_sysctl_sysctl2(); } -static void test_sysctl_child2(void) +static int sysctl_simple_child(void *arg) { int fd; + int pid; - sysctl_fail_write("0"); - fd = mfd_assert_new("kern_memfd_sysctl_1", - mfd_def_size, - MFD_CLOEXEC | MFD_ALLOW_SEALING); + printf("%s sysctl 0\n", memfd_str); + test_sysctl_set_sysctl0(); - mfd_assert_mode(fd, 0666); - mfd_assert_has_seals(fd, F_SEAL_EXEC); - mfd_fail_chmod(fd, 0777); - close(fd); + printf("%s sysctl 1\n", memfd_str); + test_sysctl_set_sysctl1(); + + printf("%s sysctl 0\n", memfd_str); + test_sysctl_set_sysctl0(); + + printf("%s sysctl 2\n", memfd_str); + test_sysctl_set_sysctl2(); + + printf("%s sysctl 1\n", memfd_str); + test_sysctl_set_sysctl1(); + + printf("%s sysctl 0\n", memfd_str); + test_sysctl_set_sysctl0(); + + return 0; +} + +/* + * Test sysctl + * A very basic test to make sure the core sysctl semantics work. + */ +static void test_sysctl_simple(void) +{ + int pid = spawn_thread(CLONE_NEWPID, sysctl_simple_child, NULL); + + join_thread(pid); } -static int newpid_thread_fn2(void *arg) +static int sysctl_nested(void *arg) { - test_sysctl_child2(); + void (*fn)(void) = arg; + + fn(); return 0; } -static pid_t spawn_newpid_thread(unsigned int flags, int (*fn)(void *)) + +static int sysctl_nested_wait(void *arg) { - uint8_t *stack; - pid_t pid; + /* Wait for a SIGCONT. */ + kill(getpid(), SIGSTOP); + return sysctl_nested(arg); +} - stack = malloc(STACK_SIZE); - if (!stack) { - printf("malloc(STACK_SIZE) failed: %m\n"); - abort(); - } +static void test_sysctl_sysctl1_failset(void) +{ + sysctl_fail_write("0"); + test_sysctl_sysctl1(); +} - pid = clone(fn, - stack + STACK_SIZE, - SIGCHLD | flags, - NULL); - if (pid < 0) { - printf("clone() failed: %m\n"); - abort(); - } +static void test_sysctl_sysctl2_failset(void) +{ + sysctl_fail_write("1"); + test_sysctl_sysctl2(); - return pid; + sysctl_fail_write("0"); + test_sysctl_sysctl2(); } -static void join_newpid_thread(pid_t pid) +static int sysctl_nested_child(void *arg) { - int wstatus; + int fd; + int pid; - if (waitpid(pid, &wstatus, 0) < 0) { - printf("newpid thread: waitpid() failed: %m\n"); - abort(); - } + printf("%s nested sysctl 0\n", memfd_str); + sysctl_assert_write("0"); + /* A further nested pidns works the same. */ + pid = spawn_thread(CLONE_NEWPID, sysctl_simple_child, NULL); + join_thread(pid); - if (WIFEXITED(wstatus) && WEXITSTATUS(wstatus) != 0) { - printf("newpid thread: exited with non-zero error code %d\n", - WEXITSTATUS(wstatus)); - abort(); - } + printf("%s nested sysctl 1\n", memfd_str); + sysctl_assert_write("1"); + /* Child inherits our setting. */ + pid = spawn_thread(CLONE_NEWPID, sysctl_nested, test_sysctl_sysctl1); + join_thread(pid); + /* Child cannot raise the setting. */ + pid = spawn_thread(CLONE_NEWPID, sysctl_nested, + test_sysctl_sysctl1_failset); + join_thread(pid); + /* Child can lower the setting. */ + pid = spawn_thread(CLONE_NEWPID, sysctl_nested, + test_sysctl_set_sysctl2); + join_thread(pid); + /* Child lowering the setting has no effect on our setting. */ + test_sysctl_sysctl1(); + + printf("%s nested sysctl 2\n", memfd_str); + sysctl_assert_write("2"); + /* Child inherits our setting. */ + pid = spawn_thread(CLONE_NEWPID, sysctl_nested, test_sysctl_sysctl2); + join_thread(pid); + /* Child cannot raise the setting. */ + pid = spawn_thread(CLONE_NEWPID, sysctl_nested, + test_sysctl_sysctl2_failset); + join_thread(pid); + + /* Verify that the rules are actually inherited after fork. */ + printf("%s nested sysctl 0 -> 1 after fork\n", memfd_str); + sysctl_assert_write("0"); - if (WIFSIGNALED(wstatus)) { - printf("newpid thread: killed by signal %d\n", - WTERMSIG(wstatus)); - abort(); - } + pid = spawn_thread(CLONE_NEWPID, sysctl_nested_wait, + test_sysctl_sysctl1_failset); + sysctl_assert_write("1"); + kill(pid, SIGCONT); + join_thread(pid); + + printf("%s nested sysctl 0 -> 2 after fork\n", memfd_str); + sysctl_assert_write("0"); + + pid = spawn_thread(CLONE_NEWPID, sysctl_nested_wait, + test_sysctl_sysctl2_failset); + sysctl_assert_write("2"); + kill(pid, SIGCONT); + join_thread(pid); + + /* + * Verify that the current effective setting is saved on fork, meaning + * that the parent lowering the sysctl doesn't affect already-forked + * children. + */ + printf("%s nested sysctl 2 -> 1 after fork\n", memfd_str); + sysctl_assert_write("2"); + pid = spawn_thread(CLONE_NEWPID, sysctl_nested_wait, + test_sysctl_sysctl2); + sysctl_assert_write("1"); + kill(pid, SIGCONT); + join_thread(pid); + + printf("%s nested sysctl 2 -> 0 after fork\n", memfd_str); + sysctl_assert_write("2"); + pid = spawn_thread(CLONE_NEWPID, sysctl_nested_wait, + test_sysctl_sysctl2); + sysctl_assert_write("0"); + kill(pid, SIGCONT); + join_thread(pid); + + printf("%s nested sysctl 1 -> 0 after fork\n", memfd_str); + sysctl_assert_write("1"); + pid = spawn_thread(CLONE_NEWPID, sysctl_nested_wait, + test_sysctl_sysctl1); + sysctl_assert_write("0"); + kill(pid, SIGCONT); + join_thread(pid); + + return 0; } /* - * Test sysctl - * A very basic sealing test to see whether setting/retrieving seals works. + * Test sysctl with nested pid namespaces + * Make sure that the sysctl nesting semantics work correctly. */ -static void test_sysctl(void) +static void test_sysctl_nested(void) { - int pid = spawn_newpid_thread(CLONE_NEWPID, newpid_thread_fn); + int pid = spawn_thread(CLONE_NEWPID, sysctl_nested_child, NULL); - join_newpid_thread(pid); + join_thread(pid); } /* @@ -1433,6 +1601,9 @@ int main(int argc, char **argv) test_seal_grow(); test_seal_resize(); + test_sysctl_simple(); + test_sysctl_nested(); + test_share_dup("SHARE-DUP", ""); test_share_mmap("SHARE-MMAP", ""); test_share_open("SHARE-OPEN", ""); @@ -1447,8 +1618,6 @@ int main(int argc, char **argv) test_share_fork("SHARE-FORK", SHARED_FT_STR); join_idle_thread(pid); - test_sysctl(); - printf("memfd: DONE\n"); return 0;