From patchwork Tue Dec 12 23:16:57 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jeff Xu X-Patchwork-Id: 13490086 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 CBBD0C4332F for ; Tue, 12 Dec 2023 23:17:22 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 1227A6B03D6; Tue, 12 Dec 2023 18:17:17 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 0AC4E6B03D7; Tue, 12 Dec 2023 18:17:16 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id DEE6E6B03D8; Tue, 12 Dec 2023 18:17:16 -0500 (EST) 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 C2FD36B03D6 for ; Tue, 12 Dec 2023 18:17:16 -0500 (EST) Received: from smtpin04.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id A023580B6B for ; Tue, 12 Dec 2023 23:17:16 +0000 (UTC) X-FDA: 81559729272.04.9DE0815 Received: from mail-pf1-f175.google.com (mail-pf1-f175.google.com [209.85.210.175]) by imf15.hostedemail.com (Postfix) with ESMTP id CD053A0006 for ; Tue, 12 Dec 2023 23:17:14 +0000 (UTC) Authentication-Results: imf15.hostedemail.com; dkim=pass header.d=chromium.org header.s=google header.b=n6hHw+dL; dmarc=pass (policy=none) header.from=chromium.org; spf=pass (imf15.hostedemail.com: domain of jeffxu@chromium.org designates 209.85.210.175 as permitted sender) smtp.mailfrom=jeffxu@chromium.org ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1702423034; a=rsa-sha256; cv=none; b=VG60KCptohLVQRL4NOtzxK4GxnvdR5epwh2SdrYpbdtdfd/502awhN75C6jIH1kyGTGfzA 9POI0sL96onEAuEv/eEGVesL+Yr4CAnG0xWc3RHyLqd+yRIlU1ZFDBxmgpg8266JqoZ4Y7 tHOpryxXuBgkLSBLcTjBw5kBgbuAJH0= ARC-Authentication-Results: i=1; imf15.hostedemail.com; dkim=pass header.d=chromium.org header.s=google header.b=n6hHw+dL; dmarc=pass (policy=none) header.from=chromium.org; spf=pass (imf15.hostedemail.com: domain of jeffxu@chromium.org designates 209.85.210.175 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=1702423034; 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=Vgg0J3qxY24SnHD9sqtPXJJls+X5lh0PMbmZ3jrDVoQ=; b=nBVfM1Y/iviQXYJyzJsyqOhM61ZlnbMVCSCOOWxVQQv0lRBbZqGFzngC6ERXJeHCQSuKG+ SqXLvatgXDxPO28IR3GdYU9UTsgstLbczLvJwYI9blQ85CfRFLyOODrNf7NbTiFog3lVyX o5Yq0IPithDHS9VljBGukv9gdkVk2Rg= Received: by mail-pf1-f175.google.com with SMTP id d2e1a72fcca58-6cea5548eb2so5582422b3a.0 for ; Tue, 12 Dec 2023 15:17:14 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1702423034; x=1703027834; 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=Vgg0J3qxY24SnHD9sqtPXJJls+X5lh0PMbmZ3jrDVoQ=; b=n6hHw+dLeey5REcloF1frimHNdQMnGM+CmN6GfSa9KsrTsyUuN++pdYJ/wNkJ+K31t Qle84lXZ7zYspnemR2+nEOfC2VKuCn1/J+2GxjjaSy5F/XHECeo/deJvU7P5gBcFyfHV TicYIq7kH2wrKFi10rnUbvnsMts+7qQ9cst+Y= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1702423034; x=1703027834; 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=Vgg0J3qxY24SnHD9sqtPXJJls+X5lh0PMbmZ3jrDVoQ=; b=Cc1XSvYB5R2+SrdIhFHaC/70LMTVld7guIKOKYT1Wm0ES2sZejsUMm7TzUO6C4siYd +G5aLaMVbqlfnvNR0WVJbrlZ2J5+0wf4B3EfFuHnH+MGuIsOUN8m76fox/UDGdLgront jkb0aeMI1uQwpVt5swHWOqZJsXzfaOO0v17KL5w1oR5JcWwPIK9t63QdVSbInRR9bS3D rYl/lBqsRgq65BiP0EMYWMHtTghaBldQZT7C6QF8MCIQGJW35qEmL3QMWR5THU6UwmzW dZZFqeawJc6UyXrUvfZ1mJsj7eAT9p4YqYLY8yp3EOQ4TRETrdEW6yAzNp6+5h3Wpkad aHkQ== X-Gm-Message-State: AOJu0YxqTLgZuSylWQ0Ss/Vmg5PUlntu2Ct9tbooWJcl/pdQbMX9KAuW zQZYJW2TAdZG83hIRtWWq5nrMg== X-Google-Smtp-Source: AGHT+IH0w5ya9vjJqVfLSwVuEpvbnpor4YM2jb0u3s8vpT0Fs9e2ntlip5J/9UPEmwW3uq6XLY2yzA== X-Received: by 2002:a05:6a20:429a:b0:190:50ec:e2e4 with SMTP id o26-20020a056a20429a00b0019050ece2e4mr9563392pzj.45.1702423033741; Tue, 12 Dec 2023 15:17:13 -0800 (PST) Received: from localhost (34.133.83.34.bc.googleusercontent.com. [34.83.133.34]) by smtp.gmail.com with UTF8SMTPSA id b4-20020aa78704000000b006ce41b1ba8csm8575780pfo.131.2023.12.12.15.17.13 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Tue, 12 Dec 2023 15:17:13 -0800 (PST) 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, pedro.falcato@gmail.com, dave.hansen@intel.com, linux-hardening@vger.kernel.org, deraadt@openbsd.org, Jeff Xu Subject: [RFC PATCH v3 03/11] mseal: add can_modify_mm and can_modify_vma Date: Tue, 12 Dec 2023 23:16:57 +0000 Message-ID: <20231212231706.2680890-4-jeffxu@chromium.org> X-Mailer: git-send-email 2.43.0.472.g3155946c3a-goog In-Reply-To: <20231212231706.2680890-1-jeffxu@chromium.org> References: <20231212231706.2680890-1-jeffxu@chromium.org> MIME-Version: 1.0 X-Rspam-User: X-Rspamd-Server: rspam06 X-Rspamd-Queue-Id: CD053A0006 X-Stat-Signature: z7icodiuw835jwswocmaznkscoybtiz3 X-HE-Tag: 1702423034-414387 X-HE-Meta: U2FsdGVkX1/84K1se94lmOgucMjcCJjp7HO/JdbSlPdJcoVCsa01q+N10hsM3fU7YANntTMzdwjin5MrUjyGAt43R9DWjqdS0Q96JDMit/I+L5aSyH89i4puVBu5oPAgcBcPtBbGmjdVZXrtX2aa7HUnMtq9HN5HEDF3NLUkL/+x1R/riqxL2jOB+bwdmk/vbjGVaMI1wDj4JYxtzB5swXs/blKBSUkvdtN9h/RRq0ZGTkyOy+wdjYC6YUjamMy1MbogVGcWVQZ1vqODoUEJooQsr1mhLwvma/mer9QfDTeJmZ6svwk9m3nLgZB82LaC2DAyhjBi1M/U90UqAv9ZvQlqu17rZG9hFZ66ma0Ss0zQv9sCbD3DkZ84ZJQh5KdwzAfn+HjFjFYB0EyEDHuFCi6tYaoes0PJhW4t1lnKXR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ity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: From: Jeff Xu Two utilities to be used later. 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 | 18 ++++++++++++++++++ mm/mseal.c | 38 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 56 insertions(+) diff --git a/include/linux/mm.h b/include/linux/mm.h index 3d1120570de5..2435acc1f44f 100644 --- a/include/linux/mm.h +++ b/include/linux/mm.h @@ -3339,6 +3339,12 @@ static inline unsigned long vma_seals(struct vm_area_struct *vma) return (vma->vm_seals & MM_SEAL_ALL); } +extern bool can_modify_mm(struct mm_struct *mm, unsigned long start, + unsigned long end, unsigned long checkSeals); + +extern bool can_modify_vma(struct vm_area_struct *vma, + unsigned long checkSeals); + #else static inline bool check_vma_seals_mergeable(unsigned long vm_seals1) { @@ -3349,6 +3355,18 @@ static inline unsigned long vma_seals(struct vm_area_struct *vma) { return 0; } + +static inline bool can_modify_mm(struct mm_struct *mm, unsigned long start, + unsigned long end, unsigned long checkSeals) +{ + return true; +} + +static inline bool can_modify_vma(struct vm_area_struct *vma, + unsigned long checkSeals) +{ + return true; +} #endif /* These take the mm semaphore themselves */ diff --git a/mm/mseal.c b/mm/mseal.c index 13bbe9ef5883..d12aa628ebdc 100644 --- a/mm/mseal.c +++ b/mm/mseal.c @@ -26,6 +26,44 @@ static bool can_do_mseal(unsigned long types, unsigned long 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, + unsigned long checkSeals) +{ + if (checkSeals & vma_seals(vma)) + return false; + + 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, + unsigned long checkSeals) +{ + struct vm_area_struct *vma; + + VMA_ITERATOR(vmi, mm, start); + + if (!checkSeals) + return true; + + /* going through each vma to check. */ + for_each_vma_range(vmi, vma, end) { + if (!can_modify_vma(vma, checkSeals)) + return false; + } + + /* Allow by default. */ + return true; +} + /* * Check if a seal type can be added to VMA. */