From patchwork Mon Oct 16 14:38:22 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jeff Xu X-Patchwork-Id: 13423495 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 4DC42CDB485 for ; Mon, 16 Oct 2023 14:39:07 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 536748D00AB; Mon, 16 Oct 2023 10:39:06 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 4A3E38D00AA; Mon, 16 Oct 2023 10:39:06 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 27F8F8D00A9; Mon, 16 Oct 2023 10:39:06 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id 12E968D00A8 for ; Mon, 16 Oct 2023 10:39:06 -0400 (EDT) Received: from smtpin30.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id E33991CADBC for ; Mon, 16 Oct 2023 14:39:05 +0000 (UTC) X-FDA: 81351581850.30.E6A44D2 Received: from mail-pl1-f175.google.com (mail-pl1-f175.google.com [209.85.214.175]) by imf30.hostedemail.com (Postfix) with ESMTP id E703380013 for ; Mon, 16 Oct 2023 14:39:02 +0000 (UTC) Authentication-Results: imf30.hostedemail.com; dkim=pass header.d=chromium.org header.s=google header.b=FLTcqut4; spf=pass (imf30.hostedemail.com: domain of jeffxu@chromium.org designates 209.85.214.175 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=1697467143; 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=Xej43+CxxYh9a3blA/g52Ig4N3aDI9kSsEpUlToUGyY=; b=H0gjMm0d6Qs/Y+Wdcl8fMciZDjEJMQaaJOyp+WYAfkhE4LWtKUqSwbMo9aIdFfBzcQwjdB XJ+xgGD2Htn+uM09XwqSVLj81zjG/JM2ix0nlVXjj8WyZk/DyXHkmlOJdB+5uUNn/AiEe+ 16MiF1GcTUTgJl3bwKKkb0Oa5th6lKM= ARC-Authentication-Results: i=1; imf30.hostedemail.com; dkim=pass header.d=chromium.org header.s=google header.b=FLTcqut4; spf=pass (imf30.hostedemail.com: domain of jeffxu@chromium.org designates 209.85.214.175 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=1697467143; a=rsa-sha256; cv=none; b=l1NHGWVA+TG5Fm5XrigSQssz8bnlWGtTHye9WoLwWWCdBOj4h8nAtFEqHngiymwbVY2i+G paAJ1js6eJ+hczn5oUJqAb2hjVSfV+j6PqScgO2MzDZ4GseYzytvSaLehvsXmQi+w4Hswj htZydbanFN0rtzypAWsOVhwdkJiaucY= Received: by mail-pl1-f175.google.com with SMTP id d9443c01a7336-1ca816f868fso5547625ad.1 for ; Mon, 16 Oct 2023 07:39:02 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1697467142; x=1698071942; 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=Xej43+CxxYh9a3blA/g52Ig4N3aDI9kSsEpUlToUGyY=; b=FLTcqut4QHGTJihKIRDOeKaI6+9bcBQtekpXmfOuYd+TBb5NGMEVErR1dX2y15xOt8 44avYMcB1Xj2BN39oTiokleM7b9BNXTFnYRTxUc+8TtdUUCaPdI6MbZNHugkXWPwDWA1 UH0smrrTpI5LsgIjaA5wFwKKSv/lNzRTOzQCg= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1697467142; x=1698071942; 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=Xej43+CxxYh9a3blA/g52Ig4N3aDI9kSsEpUlToUGyY=; b=Fz1z47J4PeZMXIijcReIktKyNe54H5OdOi9JahIkbrYDiuBArnMRkBk56CwC03cDcv H69wHjcbP3SiSTENDg2tcPM/fJ+Z5XFuIZcDWLbN7Y+KxCeRsCTjUvOcaNHWNCwCUZx7 viFzxAiUdMKPCiPMVbmnW1R5DdaBWvbXhqSYKzO8P/uORiSQvXy2ZfSFu0TMyraoa9bF OS8P0r283t4O2RisJcPgfpjqEtC8pOQwSiB6VQIJ0cLjrcpThRp3KlaNKQLZj2rlIBCV L/5DxkHIAV3OahjVeiqXM6XFgp62h20doC2FvSBkak9FQvz4GBvXsykKio7QMpBVMYmB 153g== X-Gm-Message-State: AOJu0YwRhg/CSM7vPIUmBcl9ihcpSV2UC92aGC/742VZEOfttU0P42RO cWPtKED0ediJcTIWkbXQGU/iEw== X-Google-Smtp-Source: AGHT+IGUzUb5v0t/3MkBLk3yxGa4HPz1kX3xlLqN5hhkqcb5wVyzEHQXpSYW2jltVLkxXX+Emd4ILg== X-Received: by 2002:a17:903:294b:b0:1c9:d143:e9e with SMTP id li11-20020a170903294b00b001c9d1430e9emr10728383plb.18.1697467141766; Mon, 16 Oct 2023 07:39:01 -0700 (PDT) Received: from localhost (9.184.168.34.bc.googleusercontent.com. [34.168.184.9]) by smtp.gmail.com with UTF8SMTPSA id ju1-20020a170903428100b001b9d335223csm8556065plb.26.2023.10.16.07.39.01 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 16 Oct 2023 07:39:01 -0700 (PDT) From: jeffxu@chromium.org To: akpm@linux-foundation.org, keescook@chromium.org, sroettger@google.com Cc: jeffxu@google.com, jorgelo@chromium.org, groeck@chromium.org, linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, linux-mm@kvack.org, jannh@google.com, 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, torvalds@linux-foundation.org, lstoakes@gmail.com, willy@infradead.org, 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 v1 3/8] mseal: add can_modify_mm and can_modify_vma Date: Mon, 16 Oct 2023 14:38:22 +0000 Message-ID: <20231016143828.647848-4-jeffxu@chromium.org> X-Mailer: git-send-email 2.42.0.655.g421f12c284-goog In-Reply-To: <20231016143828.647848-1-jeffxu@chromium.org> References: <20231016143828.647848-1-jeffxu@chromium.org> MIME-Version: 1.0 X-Rspamd-Queue-Id: E703380013 X-Rspam-User: X-Stat-Signature: ykd1wwynjith6ikzkzpb78dzqiwwoma7 X-Rspamd-Server: rspam01 X-HE-Tag: 1697467142-256009 X-HE-Meta: U2FsdGVkX19RNtpbrrpSJIJLnfvwXG5rCFDrlzDcga1k7ywj2IcHGjhxDuePxQY3CkTVb9RFaDCTTfZs7RfZG5RkIi1vFxj+mD7JhwaJg4eD/2NAF0jKJpmeahFXx7aw827vgAFesNwFXIFScZ1U8th3qkJUgaJa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hnu/Cq7HKW6NUM9LZLLLpYWwfNpoT6zpq9fqcSiilcYbUujoT87LSpITVXVMfWOKgcEID+11MNzQ7iMKFmDIr8pwaYgR9z1/e2zjWR37QAnK/R9rhUIePgq2sA== 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 can_modify_mm: checks sealing flags for given memory range. can_modify_vma: checks sealing flags for given vma. Signed-off-by: Jeff Xu --- include/linux/mm.h | 34 ++++++++++++++++++++++++++ mm/mseal.c | 60 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 94 insertions(+) diff --git a/include/linux/mm.h b/include/linux/mm.h index e790b91a0cd4..aafdb68950f8 100644 --- a/include/linux/mm.h +++ b/include/linux/mm.h @@ -257,6 +257,18 @@ extern struct rw_semaphore nommu_region_sem; extern unsigned int kobjsize(const void *objp); #endif +enum caller_origin { + ON_BEHALF_OF_KERNEL = 0, + ON_BEHALF_OF_USERSPACE, +}; + +enum mm_action { + MM_ACTION_MPROTECT, + MM_ACTION_MUNMAP, + MM_ACTION_MREMAP, + MM_ACTION_MMAP, +}; + /* * vm_seals in vm_area_struct, see mm_types.h. */ @@ -3302,6 +3314,28 @@ static inline void mm_populate(unsigned long addr, unsigned long len) static inline void mm_populate(unsigned long addr, unsigned long len) {} #endif +#ifdef CONFIG_MSEAL +extern bool can_modify_mm(struct mm_struct *mm, unsigned long start, + unsigned long end, enum mm_action action, + enum caller_origin called); + +extern bool can_modify_vma(struct vm_area_struct *vma, enum mm_action action, + enum caller_origin called); +#else +static inline bool can_modify_mm(struct mm_struct *mm, unsigned long start, + unsigned long end, enum mm_action action, + enum caller_origin called) +{ + return true; +} + +static inline bool can_modify_vma(struct vm_area_struct *vma, enum mm_action action, + enum caller_origin called) +{ + return true; +} +#endif + /* These take the mm semaphore themselves */ extern int __must_check vm_brk(unsigned long, unsigned long); extern int __must_check vm_brk_flags(unsigned long, unsigned long, unsigned long); diff --git a/mm/mseal.c b/mm/mseal.c index 615b6e06ab44..3285ef6b95a6 100644 --- a/mm/mseal.c +++ b/mm/mseal.c @@ -36,6 +36,66 @@ static bool can_do_mseal(unsigned int types, unsigned int flags) return true; } +/* + * check if a vma is sealed for modification. + * return true, if modification is allowed. + */ +bool can_modify_vma(struct vm_area_struct *vma, enum mm_action action, + enum caller_origin called) +{ + if (called == ON_BEHALF_OF_KERNEL) + return true; + + switch (action) { + case MM_ACTION_MPROTECT: + if (vma->vm_seals & VM_SEAL_MPROTECT) + return false; + break; + + case MM_ACTION_MUNMAP: + if (vma->vm_seals & VM_SEAL_MUNMAP) + return false; + break; + + case MM_ACTION_MREMAP: + if (vma->vm_seals & VM_SEAL_MREMAP) + return false; + break; + + case MM_ACTION_MMAP: + if (vma->vm_seals & VM_SEAL_MMAP) + return false; + break; + } + + return true; +} + +/* + * Check if the vmas of a memory range are allowed to be modified. + * the memory ranger can have a gap (unallocated memory). + * return true, if it is allowed. + */ +bool can_modify_mm(struct mm_struct *mm, unsigned long start, unsigned long end, + enum mm_action action, enum caller_origin called) +{ + struct vm_area_struct *vma; + + VMA_ITERATOR(vmi, mm, start); + + if (called == ON_BEHALF_OF_KERNEL) + return true; + + /* going through each vma to check */ + for_each_vma_range(vmi, vma, end) { + if (!can_modify_vma(vma, action, called)) + return false; + } + + /* Allow by default. */ + return true; +} + /* * Check if a seal type can be added to VMA. */