From patchwork Mon May 15 13:05:49 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jeff Xu X-Patchwork-Id: 13241396 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 8BFBEC77B75 for ; Mon, 15 May 2023 13:06:12 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 8F2A2900003; Mon, 15 May 2023 09:06:11 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 8A2C2900002; Mon, 15 May 2023 09:06:11 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 76A38900003; Mon, 15 May 2023 09:06:11 -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 63519900002 for ; Mon, 15 May 2023 09:06:11 -0400 (EDT) Received: from smtpin04.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id A4BBB1C896B for ; Mon, 15 May 2023 13:06:09 +0000 (UTC) X-FDA: 80792512458.04.0FB5AEA Received: from mail-pl1-f182.google.com (mail-pl1-f182.google.com [209.85.214.182]) by imf24.hostedemail.com (Postfix) with ESMTP id 4B0B41800DB for ; Mon, 15 May 2023 13:06:03 +0000 (UTC) Authentication-Results: imf24.hostedemail.com; dkim=pass header.d=chromium.org header.s=google header.b=Rt5yYfm0; spf=pass (imf24.hostedemail.com: domain of jeffxu@chromium.org designates 209.85.214.182 as permitted sender) smtp.mailfrom=jeffxu@chromium.org; dmarc=pass (policy=none) header.from=chromium.org ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1684155964; a=rsa-sha256; cv=none; b=KfqFG3cniAPWKxDoY5yO0S41B8A2ZztauvqxbDV8QUP4Gmdg8FdQZdHZWsw2qrNSVr5Y0X 0RUmJs1MIVkEOha8rfTuJyNVftRq+2eIZqJo9M0m+jDm10kCg0DeCnK/qjdlwykLqa9eYo P5zBPE4gkdfhAuEQPKQ9ITFC6UKxfKE= ARC-Authentication-Results: i=1; imf24.hostedemail.com; dkim=pass header.d=chromium.org header.s=google header.b=Rt5yYfm0; spf=pass (imf24.hostedemail.com: domain of jeffxu@chromium.org designates 209.85.214.182 as permitted sender) smtp.mailfrom=jeffxu@chromium.org; dmarc=pass (policy=none) header.from=chromium.org ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1684155964; 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=+ujRp+fUzXaEUmgqKvUj1r8AuZXVhmdz7uVnxDSzpYA=; b=IbS2sCrQdgEtNU8AXmBjjk+9gTocBqliPDVlpC2sCKwnEsGJ6a2tJHxL2nsqP1MVYtA/4F fpAn2l0hggAg6z34W75pSNrxdx/t5u49smW2+FL76gZQMHDtLu+pbRHAOZ2NZ1+wG0flkK 7CizKnLNm1Plp0Z/lLedFsgFprn1zrM= Received: by mail-pl1-f182.google.com with SMTP id d9443c01a7336-1addac3de73so29114385ad.1 for ; Mon, 15 May 2023 06:06:03 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1684155963; x=1686747963; 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=+ujRp+fUzXaEUmgqKvUj1r8AuZXVhmdz7uVnxDSzpYA=; b=Rt5yYfm0CK8EUUQDrYdxuqMd3n+YNkZzNsjoe7Sh2TMU94iFWNQ9XbZV1szPVs7im8 +tqDWyCl21fUMShhLNa9cxw1wcdjPBpuZrvT2b9Xhnfk8Tltierjd6QqsJqZl2irCBNZ 75n9PT1i0yFsyihOnR4+Se6E/rdRMjFhTYgCA= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1684155963; x=1686747963; 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=+ujRp+fUzXaEUmgqKvUj1r8AuZXVhmdz7uVnxDSzpYA=; b=BskcpKH+sXesmgCpuN0HX+yHisX4w+i4pXw7yF9X1Ulnwn/iRAx6GANEv7+G10v7Z0 kjM4KSku2ViXuCZTtBFowh3EHT/FaXX1yCHoRkGw4C/3AXWD8BWJYKKHYE5ctcmReZNO sTfQ7bo7ATUz76KXr2hWDItogRRCT+11/QrBKnrsYCWEqsBKXdH7e7RTSak/N1p/5rlr wQRexno9Unpa/Ds5nUwNPO+4s9ImCDFVx6X+XCIKHi5BdXx/kxUHR67u67ZXUly91TUh 7YVo3Mc2Tez33RNNpv9tttNrr3jNj58d9uwu1rfkHrcp+FH5KUigOiT4VAq0wBCoIEmS 5ATw== X-Gm-Message-State: AC+VfDxDrvEZydqZoQXVZcz68UfklGbiVUDId8PwhhtMxpXaXQoZG5VB TrrRz9HX+6Yf/O+6qhcmTZlycA== X-Google-Smtp-Source: ACHHUZ6kGDKbByWmlumIW033viVHhT0CSCqBtVBlJ4yzye+I6JDMWWkia59K77zKHw/c/xUVQ4KIpw== X-Received: by 2002:a17:902:ce84:b0:1ac:a030:c30a with SMTP id f4-20020a170902ce8400b001aca030c30amr28883712plg.19.1684155962938; Mon, 15 May 2023 06:06:02 -0700 (PDT) Received: from localhost (183.43.230.35.bc.googleusercontent.com. [35.230.43.183]) by smtp.gmail.com with UTF8SMTPSA id i2-20020a170902c94200b001a988a71617sm13474160pla.192.2023.05.15.06.06.02 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 15 May 2023 06:06:02 -0700 (PDT) From: jeffxu@chromium.org To: dave.hansen@intel.com, luto@kernel.org, jorgelo@chromium.org, keescook@chromium.org, groeck@chromium.org, jannh@google.com, sroettger@google.com Cc: akpm@linux-foundation.org, jeffxu@google.com, linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, linux-mm@kvack.org, linux-hardening@vger.kernel.org Subject: [PATCH 3/6] PKEY: Apply PKEY_ENFORCE_API to mprotect Date: Mon, 15 May 2023 13:05:49 +0000 Message-ID: <20230515130553.2311248-4-jeffxu@chromium.org> X-Mailer: git-send-email 2.40.1.606.ga4b1b128d6-goog In-Reply-To: <20230515130553.2311248-1-jeffxu@chromium.org> References: <20230515130553.2311248-1-jeffxu@chromium.org> MIME-Version: 1.0 X-Rspam-User: X-Stat-Signature: dw3wig9ow5kkrssb7bgb9ey1spoaccm3 X-Rspamd-Server: rspam08 X-Rspamd-Queue-Id: 4B0B41800DB X-HE-Tag: 1684155963-956624 X-HE-Meta: U2FsdGVkX19y13SbLDKWYRUZOyv4bDmENvIxkg+Ay5OEUQ3x4a3f7Kt8QX/Yrcq0dq3pgPaXbG7Mu57xnQkNJISMa8eIiqz51Jg3/12T/HJOMAfNZwNz21B6CYnPBLE/udKfLCM4ZsRcjVIoInGDWhbtzh9hRtPltRKr6HdbVv7AvQVdub4jhJUxyoIDRRYQcPJtiFWhqlkVGP63pJ/6GH8SVVKxATjIavyzNsb/8Zr3Ukva+K7SbaQTwY18HUxrzDytgFeZCkb2YyHkwNaEPOLO6NbKFbXlmi1UXYTrwLMmLg+S3DXBUB4iDdLgdmkqOeFj/P421SrFZGntYTWFfAsJ/CD35108Ff+u2IfXrOYRYFDC7UD27IpZT+zfdt85FyL1ExlKEqCRLa2TuldCy4Ly5wKRcZIFMq4Lgi7H40dx5ZGbsBOit+oHD+5jxKJV9lnkwU9e9+y5si1CCpm20hVFdlDbo9KmfmT/iuQ3/3ZpxKds9m+l22pGWQRDK+Gjld3aFnWHbr+tW96HNs9s9xUlhJe+nmCkUgzP5wxyej/vAPt6YDTyB7FanZDKSZI2r/gumQsO8i+GpYQizNZxrcFHNxvfHPXhqJK8xJvBOsJ7r+oLVCURNM8mlMivRXKwIJT+s9em0/BxHnfpInkRB4DHqAAINsMjpFN+A3QekCCR8XQkoYT0SyuJ50AfAqVokdUVUKL/P25qsv7YGkpRsbQO3LHN5tWgdNuX2aM14rrZdqAVwkBxL8IAQ5fHleqX39tSqSbRcF9U+AQLYCLoyGWjO2ldmYLnkBl8c0JJUa7Q9qPs/HGVyblLbX8lathDQVtC/JkWPTCPhAL5PtKt5/gauXZYzFGnTNik1jVf73k1qRChOlFguOvoTFUPJbL0cXMTq2HTrOfG4styA6yx+VFj+Xm1sgfzFpz65KrvHyJg0c5RwL97l/BVD693kCAjVmiGCUubgA7coLhGZTH U53JOdXb m3w27FyRsmjT2/Lqmkv3A0RvseC8rwXM7RIS7LQfhX6fZ3C6W5PtmXjhhpNJi3l6LI+Gd5m4N01RMggEL9er2TAPTK2MFZQO+QNpi71P7bDzr95fSPYLjq1YkSMx7TKLH8yehZt8G+Okvpmu17Ouf/GuSYQdnygbzOSrOWZcw8IJn9bXc1XkuhAXccLptAoR6wkIgplDqjUDkerwryLTPqBW39xruND4/yQjotxEgQCtqj7/gpeOg5VAaJSNYQ8RqX+VlvukD4HxHq2RRCaMbHU3rMK3X8+Mip5tIiu/P7f6s61e1ChVtYsFxrEUAc2r3e14CbpOkUlFRuhoqeNCS1TwCw4C/U+iLt+M4l5EFggiRsaX7WJirmv4nmIeiER1vEtM6rfE/rLSfQ1+jlAU4qyGsx7oTccuIeH94 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: From: Jeff Xu This patch enables PKEY_ENFORCE_API for the mprotect and mprotect_pkey syscalls. Signed-off-by: Jeff Xu --- mm/mprotect.c | 25 ++++++++++++++++++++++--- 1 file changed, 22 insertions(+), 3 deletions(-) diff --git a/mm/mprotect.c b/mm/mprotect.c index 8a68fdca8487..1378be50567d 100644 --- a/mm/mprotect.c +++ b/mm/mprotect.c @@ -727,9 +727,13 @@ mprotect_fixup(struct vma_iterator *vmi, struct mmu_gather *tlb, /* * pkey==-1 when doing a legacy mprotect() + * syscall==true if this is called by syscall from userspace. + * Note: this is always true for now, added as a reminder in case that + * do_mprotect_pkey is called directly by kernel in the future. + * Also it is consistent with __do_munmap(). */ static int do_mprotect_pkey(unsigned long start, size_t len, - unsigned long prot, int pkey) + unsigned long prot, int pkey, bool syscall) { unsigned long nstart, end, tmp, reqprot; struct vm_area_struct *vma, *prev; @@ -794,6 +798,21 @@ static int do_mprotect_pkey(unsigned long start, size_t len, } } + /* + * When called by syscall from userspace, check if the calling + * thread has the PKEY permission to modify the memory mapping. + */ + if (syscall && + arch_check_pkey_enforce_api(current->mm, start, end) < 0) { + char comm[TASK_COMM_LEN]; + + pr_warn_ratelimited( + "munmap was denied on PKEY_ENFORCE_API memory, pid=%d '%s'\n", + task_pid_nr(current), get_task_comm(comm, current)); + error = -EACCES; + goto out; + } + prev = vma_prev(&vmi); if (start > vma->vm_start) prev = vma; @@ -878,7 +897,7 @@ static int do_mprotect_pkey(unsigned long start, size_t len, SYSCALL_DEFINE3(mprotect, unsigned long, start, size_t, len, unsigned long, prot) { - return do_mprotect_pkey(start, len, prot, -1); + return do_mprotect_pkey(start, len, prot, -1, true); } #ifdef CONFIG_ARCH_HAS_PKEYS @@ -886,7 +905,7 @@ SYSCALL_DEFINE3(mprotect, unsigned long, start, size_t, len, SYSCALL_DEFINE4(pkey_mprotect, unsigned long, start, size_t, len, unsigned long, prot, int, pkey) { - return do_mprotect_pkey(start, len, prot, pkey); + return do_mprotect_pkey(start, len, prot, pkey, true); } SYSCALL_DEFINE2(pkey_alloc, unsigned long, flags, unsigned long, init_val)