From patchwork Tue Oct 17 09:08:11 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jeff Xu X-Patchwork-Id: 13424813 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 A2347CDB482 for ; Tue, 17 Oct 2023 09:08:29 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 0CA808D0105; Tue, 17 Oct 2023 05:08:26 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 054B88D0101; Tue, 17 Oct 2023 05:08:25 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id D99948D0105; Tue, 17 Oct 2023 05:08:25 -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 C0E908D0101 for ; Tue, 17 Oct 2023 05:08:25 -0400 (EDT) Received: from smtpin01.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id A11E6B6222 for ; Tue, 17 Oct 2023 09:08:25 +0000 (UTC) X-FDA: 81354377370.01.B97E4C0 Received: from mail-pj1-f44.google.com (mail-pj1-f44.google.com [209.85.216.44]) by imf21.hostedemail.com (Postfix) with ESMTP id D25271C0017 for ; Tue, 17 Oct 2023 09:08:23 +0000 (UTC) Authentication-Results: imf21.hostedemail.com; dkim=pass header.d=chromium.org header.s=google header.b=m1BjMJzS; dmarc=pass (policy=none) header.from=chromium.org; spf=pass (imf21.hostedemail.com: domain of jeffxu@chromium.org designates 209.85.216.44 as permitted sender) smtp.mailfrom=jeffxu@chromium.org ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1697533703; 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=4llBoLvRHSRPhxaAVX+Xew1tpI9xPqxP9ssy5QguIl8=; b=sI8jN4MkbtLr1s6NqtXN1LepmpDO5oxVLetjJomLts5JhiQ+IPIucuxtKgrDmnIgUfZCa4 W1mpwY31yNlxmxPlGy0r+5mL3MiBWC0GVRLMVW3nA4+JH1ErBKNJ4pSGZez+3JojkbEFbD voADtiKzJIbCin8fo6G15aN5DEGwKYQ= ARC-Authentication-Results: i=1; imf21.hostedemail.com; dkim=pass header.d=chromium.org header.s=google header.b=m1BjMJzS; dmarc=pass (policy=none) header.from=chromium.org; spf=pass (imf21.hostedemail.com: domain of jeffxu@chromium.org designates 209.85.216.44 as permitted sender) smtp.mailfrom=jeffxu@chromium.org ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1697533703; a=rsa-sha256; cv=none; b=2ySRqM3lve/fx32Zg+JiVdNpgqrhM3O3SLTx3iRILZpe8OPhQO1P7TeCmrQ6gV0ft/agNg I4r26ZESt4+KFta6gi12t/uNvQQ426Hw+Hlkqe4+Yaglc1N/JkQm2e0GdYstaXa3iXiFur 5F7Tp/40SJRJ01kq9pL5+a9pmZ3RZ/g= Received: by mail-pj1-f44.google.com with SMTP id 98e67ed59e1d1-27d1b48e20dso3051651a91.0 for ; Tue, 17 Oct 2023 02:08:23 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1697533702; x=1698138502; darn=kvack.org; 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=4llBoLvRHSRPhxaAVX+Xew1tpI9xPqxP9ssy5QguIl8=; b=m1BjMJzS88ivL7tBhxOtFFOEeE75xogMDCs+QMwmDebgxB3stxW49uJj6vrMCAhX4o LQcQS21QzwN/28mENOjVzB9TMpTJBCQkRMCnOY15vDIW+iajuJPTUGqFw+SI7nOQFZAY 59aNSrAnY4USGLRPTCDKz4QaKqYTgKXk2WYYQ= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1697533702; x=1698138502; 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=4llBoLvRHSRPhxaAVX+Xew1tpI9xPqxP9ssy5QguIl8=; b=lLxjazakm27AYbQ8CJ8q9Kmom9uBga0ysOlpr/Rou7xVcJbejaZmKavsypDvucdIAJ Pkt6THhzki5YLsDfA2O5kgdV/12HC+hUnnzvS56napvN4ldh46oYxw3tDntKigP3Fca0 vNWcUg6bFCiLU4oGvpTeMfpEQDz1/MMrV0sYxEmlQI/r5zF5pfXZmp+OBTn0cKJ6qgCO ambd8hR0XVWbulL9B/QKKiSy0UdFCKQYZmvqI6jp82/XEPStb9KSmO1h/Hg5a8SHd6YS hABZx0NfaL43bDXtp6erDj2heYk52TtoI1EwfnzPTf30qiZBtpGezjUNBS3/b4DM+n1x 6rNw== X-Gm-Message-State: AOJu0YwM8NXZM7crmrAsuCJsufMQNc1C0zZs6BVlztAbABrFNJCJgtjC UBY4ijU2k/Q2rjahTmhrfSs2bw== X-Google-Smtp-Source: AGHT+IGY8Ka5xTN+9aN+5JwGnYHZ3aFuu39VQWy4bG7nyYFZ0P/8XqDQIp2idGjzoHOlr8sq9RvLbg== X-Received: by 2002:a17:90b:124a:b0:27c:f21b:6342 with SMTP id gx10-20020a17090b124a00b0027cf21b6342mr1443714pjb.22.1697533702687; Tue, 17 Oct 2023 02:08:22 -0700 (PDT) Received: from localhost (9.184.168.34.bc.googleusercontent.com. [34.168.184.9]) by smtp.gmail.com with UTF8SMTPSA id t13-20020a17090ae50d00b00277326038dasm868586pjy.39.2023.10.17.02.08.22 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Tue, 17 Oct 2023 02:08:22 -0700 (PDT) From: jeffxu@chromium.org To: akpm@linux-foundation.org, keescook@chromium.org, jannh@google.com, sroettger@google.com, willy@infradead.org, gregkh@linuxfoundation.org, torvalds@linux-foundation.org Cc: jeffxu@google.com, jorgelo@chromium.org, groeck@chromium.org, linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, linux-mm@kvack.org, surenb@google.com, alex.sierra@amd.com, apopple@nvidia.com, aneesh.kumar@linux.ibm.com, axelrasmussen@google.com, ben@decadent.org.uk, catalin.marinas@arm.com, david@redhat.com, dwmw@amazon.co.uk, ying.huang@intel.com, hughd@google.com, joey.gouly@arm.com, corbet@lwn.net, wangkefeng.wang@huawei.com, Liam.Howlett@oracle.com, lstoakes@gmail.com, mawupeng1@huawei.com, linmiaohe@huawei.com, namit@vmware.com, peterx@redhat.com, peterz@infradead.org, ryan.roberts@arm.com, shr@devkernel.io, vbabka@suse.cz, xiujianfeng@huawei.com, yu.ma@intel.com, zhangpeng362@huawei.com, dave.hansen@intel.com, luto@kernel.org, linux-hardening@vger.kernel.org Subject: [RFC PATCH v2 4/8] mseal: Check seal flag for mprotect(2) Date: Tue, 17 Oct 2023 09:08:11 +0000 Message-ID: <20231017090815.1067790-5-jeffxu@chromium.org> X-Mailer: git-send-email 2.42.0.655.g421f12c284-goog In-Reply-To: <20231017090815.1067790-1-jeffxu@chromium.org> References: <20231017090815.1067790-1-jeffxu@chromium.org> MIME-Version: 1.0 X-Rspam-User: X-Rspamd-Server: rspam12 X-Rspamd-Queue-Id: D25271C0017 X-Stat-Signature: ewinumyjboibemrdb5jpj5czas87qdp3 X-HE-Tag: 1697533703-610533 X-HE-Meta: U2FsdGVkX19xbVk+EW31G1Bk2lamXl4I6/v7AMcZaRIlo82HUN9ezLoXqMMqj5Fb4zZtJQ/wAansYjPHTrDQhFXcg3eggXwxnj5bP3SYXtPwQ5o0DNbDuwBRDaN2zNVv67W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VhkzfQfkboSqvs2JF378NK6Sp/JGxJqH9xQVKsSW8RZW7HivZs6++b9xP5UvEtcqXkfcCzmc85pngb6FTCacLW52AJ9K4kpkeeoM4H26dodayJoRhnUg/Ob/LHTyztHPIyDPNRYBy+zxTqvlw= 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 mprotect(2) changes protection of VMAs in the given address range. Sealing will prevent unintended mprotect call. What this patch does: When a mprotect(2) is invoked, if one of its VMAs has MM_SEAL_MPROTECT set from previous mseal(2) call, this mprotect(2) will fail, without any VMA modified. This patch is based on following: 1. do_mprotect_pkey() currently called in two places: SYSCALL_DEFINE3(mprotect,...) SYSCALL_DEFINE4(pkey_mprotect, ...) and not in any other places, therefore omit changing the signature of do_mprotect_pkey(), i.e. not passing checkSeals flag. 2. In do_mprotect_pkey(), calls can_modify_mm() before any update is made on the VMAs. Signed-off-by: Jeff Xu --- mm/mprotect.c | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/mm/mprotect.c b/mm/mprotect.c index 130db91d3a8c..6321c4d0aa3f 100644 --- a/mm/mprotect.c +++ b/mm/mprotect.c @@ -32,6 +32,7 @@ #include #include #include +#include #include #include #include @@ -753,6 +754,20 @@ static int do_mprotect_pkey(unsigned long start, size_t len, } } + /* + * do_mprotect_pkey() currently called from two places: + * SYSCALL_DEFINE3(mprotect,...) + * SYSCALL_DEFINE4(pkey_mprotect, ...) + * and not from other places. + * Therefore, omit changing the signature of do_mprotect_pkey(). + * Otherwise, we might need to add checkSeals and pass it + * from all callers of do_mprotect_pkey(). + */ + if (!can_modify_mm(current->mm, start, end, MM_SEAL_MPROTECT)) { + error = -EACCES; + goto out; + } + prev = vma_prev(&vmi); if (start > vma->vm_start) prev = vma;