From patchwork Thu Oct 17 00:51:04 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jeff Xu X-Patchwork-Id: 13839155 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 B4228D2F7D7 for ; Thu, 17 Oct 2024 00:51:16 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 408CE6B0083; Wed, 16 Oct 2024 20:51:16 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 3BA4D6B0088; Wed, 16 Oct 2024 20:51:16 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 20B886B0089; Wed, 16 Oct 2024 20:51:16 -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 00CC76B0083 for ; Wed, 16 Oct 2024 20:51:15 -0400 (EDT) Received: from smtpin03.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id 57E1EC0A24 for ; Thu, 17 Oct 2024 00:51:04 +0000 (UTC) X-FDA: 82681264932.03.079BD6F Received: from mail-pj1-f50.google.com (mail-pj1-f50.google.com [209.85.216.50]) by imf10.hostedemail.com (Postfix) with ESMTP id 8B3A7C000C for ; Thu, 17 Oct 2024 00:51:09 +0000 (UTC) Authentication-Results: imf10.hostedemail.com; dkim=pass header.d=chromium.org header.s=google header.b=Rs6PYFWQ; spf=pass (imf10.hostedemail.com: domain of jeffxu@chromium.org designates 209.85.216.50 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=1729126128; 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=qjKlw80hPOriNww9p0Da79oYC5cYurJRNqRcXEepGpg=; b=gI/6m+RZasVa6sBDG+Flj0o3GpGXZzf6A+JHvLBCeL+CoRHYIJ6jIe3j8PyzCkHWKvKrRk YI7lfa1qIfejmrBrVRmJp0yETTRhgOjGAVOUHtDFUgIRgRYbfJvWsgdr1Br2wCRJC+N+g0 Oe+JR+JW8VzdWTG/4DsymQMui4Uieyo= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1729126128; a=rsa-sha256; cv=none; b=wcp5vJUw2u6jAZJQLT+ONauEw99lvNnDjTs7UzjABCuJFKV+OfSeB9WXvfGGBD+0wSWX2E K9BcOglJOqrYRs//VGoj8TMlXYniUVeJa/6cX0d/Z9gFMutWxD0le8dORjue36xLLFmkKJ 38adv37q05sNo6ruXzRcU7966OmOjXc= ARC-Authentication-Results: i=1; imf10.hostedemail.com; dkim=pass header.d=chromium.org header.s=google header.b=Rs6PYFWQ; spf=pass (imf10.hostedemail.com: domain of jeffxu@chromium.org designates 209.85.216.50 as permitted sender) smtp.mailfrom=jeffxu@chromium.org; dmarc=pass (policy=none) header.from=chromium.org Received: by mail-pj1-f50.google.com with SMTP id 98e67ed59e1d1-2e2b93d2da6so47441a91.0 for ; Wed, 16 Oct 2024 17:51:13 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1729126272; x=1729731072; 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=qjKlw80hPOriNww9p0Da79oYC5cYurJRNqRcXEepGpg=; b=Rs6PYFWQ6HFT+v1MI5sqooSMn19tWlvPzN8gGxXEz+sC0U8v5IN9x1lHVIctUYzBeD 4WBd6wvHSLfXzBk8jg39BV3U8lqyYjqKPEoPiwHKyo56NuskKwyD0CjC2NPu8ktQ7c2D eGuf7d8XSln6pACaTQk+5JSn9ipTNjXJTxbRI= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1729126272; x=1729731072; 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=qjKlw80hPOriNww9p0Da79oYC5cYurJRNqRcXEepGpg=; b=hoSm/i96/Nj+yFG5ttSOEZuBwZ3VpModXddI2DKQWW4xTFgIxUVjUnqFll3XjD8RVG lGSoInaKY0tmyl3Zgd1t89I9GbwZfPx4DeIrPl+zNGjOVZWwf8LHLTrVcMUm48eEAobM PO+ZpVmFKVfjpeN1/7wSZRQJ56tlm4dBnqEpUuglD21wWJLmNgDg78dtgcPU4AAOh/YL DuRRNS1Xs58YPS95cDJz7HM23w7T2aqGDinx5BjujbZITQ2+e9s2uoEr0NGdJHiOu36D kj/acCfrdH3EACJkJrztQ7encH0S4xPNl+vZR57mCgqC5k5r3BBdF1NBodBK9peq7APQ m/wQ== X-Forwarded-Encrypted: i=1; AJvYcCWJaw92NwnzoX16kmG7zivGKAcs/ZJfGK1+FVZho7h4NFoSAGI+/Y51AW6XZ8+aC8nwnyX5i+YaWQ==@kvack.org X-Gm-Message-State: AOJu0YzdE/qFhEwta1wv1toWCPeb3F3McyjhR7MK+d7kkCiL31S/YwgL J2eqp87ULMAGHLTMB3FQaCxQTd6v9AdQJxcGAe4hADp5r27p7QuJGV6k5HIxsQ== X-Google-Smtp-Source: AGHT+IG+nZEmIFxEMebL8zJk/ulSdARWGAMWNOvXhzs7CcDij4L3o5BGAmBhyFvsZhnXUwj/YuMYiQ== X-Received: by 2002:a17:903:32c4:b0:205:723f:23d4 with SMTP id d9443c01a7336-20d47e69b07mr7460455ad.5.1729126270776; Wed, 16 Oct 2024 17:51:10 -0700 (PDT) Received: from localhost (56.4.82.34.bc.googleusercontent.com. [34.82.4.56]) by smtp.gmail.com with UTF8SMTPSA id d9443c01a7336-20d1804c050sm34102105ad.216.2024.10.16.17.51.10 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Wed, 16 Oct 2024 17:51:10 -0700 (PDT) From: jeffxu@chromium.org To: akpm@linux-foundation.org, keescook@chromium.org, torvalds@linux-foundation.org, usama.anjum@collabora.com, corbet@lwn.net, Liam.Howlett@oracle.com, lorenzo.stoakes@oracle.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, sroettger@google.com, pedro.falcato@gmail.com, linux-hardening@vger.kernel.org, willy@infradead.org, gregkh@linuxfoundation.org, deraadt@openbsd.org, surenb@google.com, merimus@google.com, rdunlap@infradead.org, Jeff Xu , stable@vger.kernel.org Subject: [PATCH v1 1/2] mseal: Two fixes for madvise(MADV_DONTNEED) when sealed Date: Thu, 17 Oct 2024 00:51:04 +0000 Message-ID: <20241017005105.3047458-2-jeffxu@chromium.org> X-Mailer: git-send-email 2.47.0.rc1.288.g06298d1525-goog In-Reply-To: <20241017005105.3047458-1-jeffxu@chromium.org> References: <20241017005105.3047458-1-jeffxu@chromium.org> MIME-Version: 1.0 X-Rspam-User: X-Rspamd-Server: rspam04 X-Rspamd-Queue-Id: 8B3A7C000C X-Stat-Signature: m17ksensdk58pdeq6wonmapp3yaj3gfi X-HE-Tag: 1729126269-290122 X-HE-Meta: U2FsdGVkX1/evGu3cNUO4nCpEGT6f3SLr7xgS1nVurMs057CbytOAIZ442Vctw/ZN2xr/pWu231sOloIeh7jcXtWyvgPtNH9hi9y79sY6bRk9JTtT37vak0NNjDUMSDwHDaa6N9u83ozOa+WsDqchW765Y04HA4086ZEy0jGlPenEBwWqR4u1306E1aeGdmZl8IcYa3QTdj73Lg6gc92CSA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AdlRCQpNt+tWMtJU+mEdaDbaYYqXRQLZuwOtlWkufkFu1oe0yy3r730LWv/lJFqTHZMWod39e0xbll5inJNmLCLmtp/qQ+DnWVZxYzNfOFwo9ESA= 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: List-Subscribe: List-Unsubscribe: From: Jeff Xu Two fixes for madvise(MADV_DONTNEED) when sealed. For PROT_NONE mappings, the previous blocking of madvise(MADV_DONTNEED) is unnecessary. As PROT_NONE already prohibits memory access, madvise(MADV_DONTNEED) should be allowed to proceed in order to free the page. For file-backed, private, read-only memory mappings, we previously did not block the madvise(MADV_DONTNEED). This was based on the assumption that the memory's content, being file-backed, could be retrieved from the file if accessed again. However, this assumption failed to consider scenarios where a mapping is initially created as read-write, modified, and subsequently changed to read-only. The newly introduced VM_WASWRITE flag addresses this oversight. Reported-by: Pedro Falcato Link:https://lore.kernel.org/all/CABi2SkW2XzuZ2-TunWOVzTEX1qc29LhjfNQ3hD4Nym8U-_f+ug@mail.gmail.com/ Fixes: 8be7258aad44 ("mseal: add mseal syscall") Cc: # 6.11.y: 4d1b3416659b: mm: move can_modify_vma to mm/vma.h Cc: # 6.11.y: 4a2dd02b0916: mm/mprotect: replace can_modify_mm with can_modify_vma Cc: # 6.11.y: 23c57d1fa2b9: mseal: replace can_modify_mm_madv with a vma variant Cc: # 6.11.y Signed-off-by: Jeff Xu Signed-off-by: Pedro Falcato --- include/linux/mm.h | 2 ++ mm/mprotect.c | 3 +++ mm/mseal.c | 42 ++++++++++++++++++++++++++++++++++++------ 3 files changed, 41 insertions(+), 6 deletions(-) diff --git a/include/linux/mm.h b/include/linux/mm.h index 4c32003c8404..b402eca2565a 100644 --- a/include/linux/mm.h +++ b/include/linux/mm.h @@ -430,6 +430,8 @@ extern unsigned int kobjsize(const void *objp); #ifdef CONFIG_64BIT /* VM is sealed, in vm_flags */ #define VM_SEALED _BITUL(63) +/* VM was writable */ +#define VM_WASWRITE _BITUL(62) #endif /* Bits set in the VMA until the stack is in its final location */ diff --git a/mm/mprotect.c b/mm/mprotect.c index 0c5d6d06107d..6397135ca526 100644 --- a/mm/mprotect.c +++ b/mm/mprotect.c @@ -821,6 +821,9 @@ static int do_mprotect_pkey(unsigned long start, size_t len, break; } + if ((vma->vm_flags & VM_WRITE) && !(newflags & VM_WRITE)) + newflags |= VM_WASWRITE; + error = security_file_mprotect(vma, reqprot, prot); if (error) break; diff --git a/mm/mseal.c b/mm/mseal.c index ece977bd21e1..28f28487be17 100644 --- a/mm/mseal.c +++ b/mm/mseal.c @@ -36,12 +36,8 @@ static bool is_madv_discard(int behavior) return false; } -static bool is_ro_anon(struct vm_area_struct *vma) +static bool anon_is_ro(struct vm_area_struct *vma) { - /* check anonymous mapping. */ - if (vma->vm_file || vma->vm_flags & VM_SHARED) - return false; - /* * check for non-writable: * PROT=RO or PKRU is not writeable. @@ -53,6 +49,22 @@ static bool is_ro_anon(struct vm_area_struct *vma) return false; } +static bool vma_is_prot_none(struct vm_area_struct *vma) +{ + if ((vma->vm_flags & VM_ACCESS_FLAGS) == VM_NONE) + return true; + + return false; +} + +static bool vma_was_writable_turn_readonly(struct vm_area_struct *vma) +{ + if (!(vma->vm_flags & VM_WRITE) && vma->vm_flags & VM_WASWRITE) + return true; + + return false; +} + /* * Check if a vma is allowed to be modified by madvise. */ @@ -61,7 +73,25 @@ bool can_modify_vma_madv(struct vm_area_struct *vma, int behavior) if (!is_madv_discard(behavior)) return true; - if (unlikely(!can_modify_vma(vma) && is_ro_anon(vma))) + /* not sealed */ + if (likely(can_modify_vma(vma))) + return true; + + /* PROT_NONE mapping */ + if (vma_is_prot_none(vma)) + return true; + + /* file-backed private mapping */ + if (vma->vm_file) { + /* read-only but was writeable */ + if (vma_was_writable_turn_readonly(vma)) + return false; + + return true; + } + + /* anonymous mapping is read-only */ + if (anon_is_ro(vma)) return false; /* Allow by default. */ From patchwork Thu Oct 17 00:51:05 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jeff Xu X-Patchwork-Id: 13839156 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 CE8D3D2F7D8 for ; Thu, 17 Oct 2024 00:51:18 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 2D09A6B0088; Wed, 16 Oct 2024 20:51:18 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 231C36B0089; Wed, 16 Oct 2024 20:51:18 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 037A46B008A; Wed, 16 Oct 2024 20:51:17 -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 D67DD6B0088 for ; Wed, 16 Oct 2024 20:51:17 -0400 (EDT) Received: from smtpin04.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id E279EAC1B1 for ; Thu, 17 Oct 2024 00:50:56 +0000 (UTC) X-FDA: 82681265226.04.D2942F0 Received: from mail-pg1-f171.google.com (mail-pg1-f171.google.com [209.85.215.171]) by imf11.hostedemail.com (Postfix) with ESMTP id E160940004 for ; Thu, 17 Oct 2024 00:51:04 +0000 (UTC) Authentication-Results: imf11.hostedemail.com; dkim=pass header.d=chromium.org header.s=google header.b=CKC9Qa21; spf=pass (imf11.hostedemail.com: domain of jeffxu@chromium.org designates 209.85.215.171 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=1729126132; 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=bkOjehUZhf9vTAiDfbTet38c6Gkgb8k55nqJSbDOZQY=; b=cZJ9V/UMKStIe0K7qjlizjtDn79fMPsKiGFd8rYFemUE+LYwhs8U0niCyPT8ve3Y7pvpab hht7qRxousejMyvvfGN+riG6eRHs2PomIIWoxyu7MyJ5L6qEipQtsQKVJZlXsv9mubhHFU l1v+sgkNu/ZTZcHy4+zf2dP+VfpQHhg= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1729126132; a=rsa-sha256; cv=none; b=WuoqN8HQToJnRPBSWS5uzStlsjV9lW4yINQMLOHL83zJcL3igJns2kcHum4Vb9rBDK36c5 NwQSKYMBfkjnuohR+rvSk1qaZnihfkWV1vNgj2KcXyZmfmn+WiNOkb61tCyHcTbKgWibBC xNmTNHVtZRR7Jhcm82AM2+aM2Diq+GA= ARC-Authentication-Results: i=1; imf11.hostedemail.com; dkim=pass header.d=chromium.org header.s=google header.b=CKC9Qa21; spf=pass (imf11.hostedemail.com: domain of jeffxu@chromium.org designates 209.85.215.171 as permitted sender) smtp.mailfrom=jeffxu@chromium.org; dmarc=pass (policy=none) header.from=chromium.org Received: by mail-pg1-f171.google.com with SMTP id 41be03b00d2f7-7ea535890e0so54759a12.2 for ; Wed, 16 Oct 2024 17:51:15 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1729126274; x=1729731074; 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=bkOjehUZhf9vTAiDfbTet38c6Gkgb8k55nqJSbDOZQY=; b=CKC9Qa21J1x9Wqu/yO5XXmJ6gGyk5UXST8AboX1E6dM7LUGnqBUwnh3pBrcKNXHnRY p/5oFf85Xl69FXYxuSt4ZRLa4ALtPZ94/AhZLhk16fRc1BP43aPPl0DCUHnrlpOUWPml xP4iB2Sz/FtOlHsdL6hT9cn7W7PEMhaWshiCA= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1729126274; x=1729731074; 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=bkOjehUZhf9vTAiDfbTet38c6Gkgb8k55nqJSbDOZQY=; b=CGyJr3waqWiKY2/acJg+MG6CTJ4TGcvxCr/RcE4XYJoIld7iIln6ZxOT/KH4Lasllf oTG7JwoFygNTR4ugrBIIt4iPp53JjkP3SUywRQ9SSaonQv9RsZhCGeHvCnEYQ0bgcSfR 3vVM15EwqEPBOmWZDQF9jRnMJfInhGybMUsx/jJ2l8KMmb3ip+YseVP+WCccZ9bIAaWM 9lLDx4avbDO8k2dkLn/0+VBb68yA17Sg0pWnPYeIK1fx2C1zrmxGZ52svuegs+9/qQsR eoZ3xZbq/KXdEkDZTIotag+au20mS8bkzpREBcsMJwkBYwWcLUOyMtyjV0iAotLzPuYM IcEg== X-Forwarded-Encrypted: i=1; AJvYcCVckJ8K/FRCxpvR0pWq8/fuCaXcBKrQn+Y6bXi2VcVl5Gb5sOFguTeZ/0mwNVO3WP7HdtTuj63jlg==@kvack.org X-Gm-Message-State: AOJu0Yzvp2RGY+aGggLSVoBE0iyqYUJUD69EA3nhnpSum9sUBAgsED0X mSZT9NzWfqdP9YuA9yt2hTDsi6XNfCI4Sqvc5sZQpy3ewja7SBBBeQE4B4D/aw== X-Google-Smtp-Source: AGHT+IHy9OceESdtYyqY6xLVtyPe+K9jcM7IV9Ttt1i23Ri9jkWXgMq2TJu6PP5Z70ZTIPLzN7qP/w== X-Received: by 2002:a17:902:dac7:b0:20c:a63e:b678 with SMTP id d9443c01a7336-20d47f829a5mr7401195ad.14.1729126274463; Wed, 16 Oct 2024 17:51:14 -0700 (PDT) Received: from localhost (56.4.82.34.bc.googleusercontent.com. [34.82.4.56]) by smtp.gmail.com with UTF8SMTPSA id d9443c01a7336-20d1804c68bsm34559025ad.185.2024.10.16.17.51.14 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Wed, 16 Oct 2024 17:51:14 -0700 (PDT) From: jeffxu@chromium.org To: akpm@linux-foundation.org, keescook@chromium.org, torvalds@linux-foundation.org, usama.anjum@collabora.com, corbet@lwn.net, Liam.Howlett@oracle.com, lorenzo.stoakes@oracle.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, sroettger@google.com, pedro.falcato@gmail.com, linux-hardening@vger.kernel.org, willy@infradead.org, gregkh@linuxfoundation.org, deraadt@openbsd.org, surenb@google.com, merimus@google.com, rdunlap@infradead.org, Jeff Xu , stable@vger.kernel.org Subject: [PATCH v1 2/2] selftest/mseal: Add tests for madvise fixes Date: Thu, 17 Oct 2024 00:51:05 +0000 Message-ID: <20241017005105.3047458-3-jeffxu@chromium.org> X-Mailer: git-send-email 2.47.0.rc1.288.g06298d1525-goog In-Reply-To: <20241017005105.3047458-1-jeffxu@chromium.org> References: <20241017005105.3047458-1-jeffxu@chromium.org> MIME-Version: 1.0 X-Rspamd-Queue-Id: E160940004 X-Stat-Signature: zy6xg4t4tak5mxsi3duepq8qomaenqki X-Rspamd-Server: rspam09 X-Rspam-User: X-HE-Tag: 1729126264-169947 X-HE-Meta: U2FsdGVkX1/h4ewuCDULcXS0X4qukqRgLvd57TQPKpkvw1AFyDs3WqUR2ZgKVUsml/MHtv/tUK6IC3xlZnD9mL0orDWmh6CvWe/QqSGyPsYeDcCmvijN22VZkcBMtJszpp7iJGnH2Kx4udzW65cUVEVYFRMN0YbaF6jACKEDYsUWTnJjtCJhXwPtdJIg8Ieb/GnK/k0mJAq9Muoo1g6MHHRlwI5RmYn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UwLLZlpCD3JK9PGHoRRSrmLsuvMPTbi6EUu3yL7PbM6dCX7/Y9PUhU0Mj 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: List-Subscribe: List-Unsubscribe: From: Jeff Xu Testcase for "Two fixes for madvise(MADV_DONTNEED) when sealed" test_seal_madvise_prot_none shall not block when mapping is PROT_NONE test_madvise_filebacked_writable shall not block writeable private filebacked mapping. test_madvise_filebacked_was_writable - shall block. shall block read-only private filebacked mapping which was previously writable. Fixes: 8be7258aad44 ("mseal: add mseal syscall") Cc: # 6.11.y: 67203f3f2a63: selftests/mm: add mseal test for no-discard madvise Cc: # 6.11.y: f28bdd1b17ec: selftests/mm: add more mseal traversal tests Cc: # 6.11.y Signed-off-by: Jeff Xu --- tools/testing/selftests/mm/mseal_test.c | 118 +++++++++++++++++++++++- 1 file changed, 116 insertions(+), 2 deletions(-) diff --git a/tools/testing/selftests/mm/mseal_test.c b/tools/testing/selftests/mm/mseal_test.c index 01675c412b2a..fa74dbe4a684 100644 --- a/tools/testing/selftests/mm/mseal_test.c +++ b/tools/testing/selftests/mm/mseal_test.c @@ -1728,7 +1728,7 @@ static void test_seal_discard_ro_anon_on_filebacked(bool seal) FAIL_TEST_IF_FALSE(!ret); } - /* sealing doesn't apply for file backed mapping. */ + /* read-only private file-backed mapping, allow always */ ret = sys_madvise(ptr, size, MADV_DONTNEED); FAIL_TEST_IF_FALSE(!ret); @@ -1864,6 +1864,111 @@ static void test_seal_madvise_nodiscard(bool seal) REPORT_TEST_PASS(); } +static void test_seal_madvise_prot_none(bool seal) +{ + void *ptr; + unsigned long page_size = getpagesize(); + unsigned long size = 4 * page_size; + int ret; + + setup_single_address(size, &ptr); + FAIL_TEST_IF_FALSE(ptr != (void *)-1); + + ret = sys_mprotect(ptr + page_size, page_size, PROT_NONE); + FAIL_TEST_IF_FALSE(!ret); + + if (seal) { + ret = seal_single_address(ptr + page_size, page_size); + FAIL_TEST_IF_FALSE(!ret); + } + + /* madvise(DONTNEED) should pass on PROT_NONE sealed VMA */ + ret = sys_madvise(ptr + page_size, page_size, MADV_DONTNEED); + FAIL_TEST_IF_FALSE(!ret); + + REPORT_TEST_PASS(); +} + +static void test_madvise_filebacked_writable(bool seal) +{ + void *ptr; + unsigned long page_size = getpagesize(); + unsigned long size = 4 * page_size; + int ret; + int fd; + unsigned long mapflags = MAP_PRIVATE; + + fd = memfd_create("test", 0); + FAIL_TEST_IF_FALSE(fd > 0); + + ret = fallocate(fd, 0, 0, size); + FAIL_TEST_IF_FALSE(!ret); + + ptr = mmap(NULL, size, PROT_READ|PROT_WRITE, mapflags, fd, 0); + FAIL_TEST_IF_FALSE(ptr != MAP_FAILED); + + if (seal) { + ret = sys_mseal(ptr, size); + FAIL_TEST_IF_FALSE(!ret); + } + + /* sealing doesn't apply for writeable file-backed mapping. */ + ret = sys_madvise(ptr, size, MADV_DONTNEED); + FAIL_TEST_IF_FALSE(!ret); + + ret = sys_munmap(ptr, size); + if (seal) + FAIL_TEST_IF_FALSE(ret < 0); + else + FAIL_TEST_IF_FALSE(!ret); + close(fd); + + REPORT_TEST_PASS(); +} + +static void test_madvise_filebacked_was_writable(bool seal) +{ + void *ptr; + unsigned long page_size = getpagesize(); + unsigned long size = 4 * page_size; + int ret; + int fd; + unsigned long mapflags = MAP_PRIVATE; + + fd = memfd_create("test", 0); + FAIL_TEST_IF_FALSE(fd > 0); + + ret = fallocate(fd, 0, 0, size); + FAIL_TEST_IF_FALSE(!ret); + + ptr = mmap(NULL, size, PROT_READ|PROT_WRITE, mapflags, fd, 0); + FAIL_TEST_IF_FALSE(ptr != MAP_FAILED); + + ret = sys_mprotect(ptr, size, PROT_READ); + FAIL_TEST_IF_FALSE(!ret); + + if (seal) { + ret = sys_mseal(ptr, size); + FAIL_TEST_IF_FALSE(!ret); + } + + /* read-only file-backed mapping, was writable. */ + ret = sys_madvise(ptr, size, MADV_DONTNEED); + if (seal) + FAIL_TEST_IF_FALSE(ret < 0); + else + FAIL_TEST_IF_FALSE(!ret); + + ret = sys_munmap(ptr, size); + if (seal) + FAIL_TEST_IF_FALSE(ret < 0); + else + FAIL_TEST_IF_FALSE(!ret); + close(fd); + + REPORT_TEST_PASS(); +} + int main(int argc, char **argv) { bool test_seal = seal_support(); @@ -1876,7 +1981,7 @@ int main(int argc, char **argv) if (!pkey_supported()) ksft_print_msg("PKEY not supported\n"); - ksft_set_plan(88); + ksft_set_plan(94); test_seal_addseal(); test_seal_unmapped_start(); @@ -1985,5 +2090,14 @@ int main(int argc, char **argv) test_seal_discard_ro_anon_on_pkey(false); test_seal_discard_ro_anon_on_pkey(true); + test_seal_madvise_prot_none(false); + test_seal_madvise_prot_none(true); + + test_madvise_filebacked_writable(false); + test_madvise_filebacked_writable(true); + + test_madvise_filebacked_was_writable(false); + test_madvise_filebacked_was_writable(true); + ksft_finished(); }