From patchwork Fri Dec 6 01:09:22 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Isaac J. Manjarres" X-Patchwork-Id: 13896199 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 D6409E77170 for ; Fri, 6 Dec 2024 01:09:42 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 4A55D6B0141; Thu, 5 Dec 2024 20:09:42 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 453FB6B0142; Thu, 5 Dec 2024 20:09:42 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 2F5216B0145; Thu, 5 Dec 2024 20:09:42 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id 0E24B6B0141 for ; Thu, 5 Dec 2024 20:09:42 -0500 (EST) Received: from smtpin21.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id C29B142CEA for ; Fri, 6 Dec 2024 01:09:41 +0000 (UTC) X-FDA: 82862751258.21.D9FF278 Received: from mail-ot1-f73.google.com (mail-ot1-f73.google.com [209.85.210.73]) by imf04.hostedemail.com (Postfix) with ESMTP id 42B9C40003 for ; Fri, 6 Dec 2024 01:09:22 +0000 (UTC) Authentication-Results: imf04.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=QY3ZGOJC; spf=pass (imf04.hostedemail.com: domain of 30k5SZw4KCPkjtbbdnbokbssfthpphmf.dpnmjovy-nnlwbdl.psh@flex--isaacmanjarres.bounces.google.com designates 209.85.210.73 as permitted sender) smtp.mailfrom=30k5SZw4KCPkjtbbdnbokbssfthpphmf.dpnmjovy-nnlwbdl.psh@flex--isaacmanjarres.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1733447372; 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-type:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=ZVe1l78oGg6d6XLgPQZsewnpRSOEBU8Pjn8H7vlg4Dw=; b=NiOKDB3fFO0b1DALM275fLh5fv/m5KzCbAJGD7i2N+jWYLXvyo/EVUWuBIHtXaxQ5y+tCM cftWJwexZ+bzqN6qE/lem6XIH5Azg+B3jAS2o0TvvFGXVQJn22qGpZGwaFtCQqiCH8TySZ QiF7SE0AY3pgBBXCdiIV8+Kd2xehsKY= ARC-Authentication-Results: i=1; imf04.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=QY3ZGOJC; spf=pass (imf04.hostedemail.com: domain of 30k5SZw4KCPkjtbbdnbokbssfthpphmf.dpnmjovy-nnlwbdl.psh@flex--isaacmanjarres.bounces.google.com designates 209.85.210.73 as permitted sender) smtp.mailfrom=30k5SZw4KCPkjtbbdnbokbssfthpphmf.dpnmjovy-nnlwbdl.psh@flex--isaacmanjarres.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1733447372; a=rsa-sha256; cv=none; b=SxlNUE5C2P9/fSu/OYsrfy5h8AtubpGhMu5tMsBtsKBjxzczaNpcweth2dBCoaz9V07aGn ZKJBJOGqsL5yaQPDhyRtNM/1BZtJ5b48ZGRW5WpQvRgjOqMdgrQhId29xEAjSxuEze009B T92r/LMx61gB8qAoejCbjzZigjzHNh8= Received: by mail-ot1-f73.google.com with SMTP id 46e09a7af769-71d418d1977so1274178a34.0 for ; Thu, 05 Dec 2024 17:09:39 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1733447379; x=1734052179; darn=kvack.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=ZVe1l78oGg6d6XLgPQZsewnpRSOEBU8Pjn8H7vlg4Dw=; b=QY3ZGOJCVb5Gb1hv2lolqVnCpbm6Q8JgQjIKddE+roz/xBr+punzCwHeVL+43Vk4+b 821qLV8HvjwBY8VnX/AIu/LGpVmHziEbhoea17opXx2dhwy46wcLDkzDkOteQiExivkW lz6ij5PHyhp5Yk11t+ROTL2Xz4em48dwBo9yYVPMqJpfUo6b4Pzsm4BfuiXfIt2Uv7TI ou/Ey4kMhscOxlYAvDgOfuR1r08qmfDKeAXKci2KDi2GIXBmt+a7d3M4TGLAfYxveJOX C9nJCvuA/5qQafRDFylDJ+Hgh2x8wH1QcKe5PrxWNp3DMVXhLNA7I+zEbzOUu52Wh3lk PQ7w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1733447379; x=1734052179; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=ZVe1l78oGg6d6XLgPQZsewnpRSOEBU8Pjn8H7vlg4Dw=; b=WERC6Ak/GBYo6jESPHj8PpWHejwGgq1KMVxDlJZxYMtab3j0da8t85UPEdDkE+pQSy ITLxswLVlPqf4hLMudDZAV+9DHjuRM6RUEOp/46jcUcDzyDvhs6RCMnClxFoiT329k1d 66PUgDhBuzBPqVJH39mnFAmibUm/u97cI3VQop5cXlDgQPrrHhP/5/RxnjZPa46zQ6Mc boB5opxJ/vfp4vE+Vk26GYW/701cBTuAQjgvyVoWa/UX6o41fqBtq7ZQ/hyUkEjWAs5p NRZXEL4yC47Cp4sBKrzCI7nSHTUGztEevKR/XLd0NPgGtw0TH03XaVAB1YnBw6ipwc0i fDHQ== X-Forwarded-Encrypted: i=1; AJvYcCU3HlL/AlFNRfYpvuDqWCaydVPbR1uXKn1J7RMG7g6LJ8CVVrfnL0na80LGVQepFazJFRcs90aKlw==@kvack.org X-Gm-Message-State: AOJu0YwybMwUv4NQM/ekm+o2hhlQIMDejNXYwyqV9hEliPgh8YueFhuR 2Rqkg06kOxYbSCoILXwMYXCX/VdGux3jbz6IOCNbFG3c09BWa0FFUId5yu+ZYY6+d+e2cE3gLi5 A7io0U6XXzJR58wG5WawuU4zkRB2qi9dPHQ== X-Google-Smtp-Source: AGHT+IEYpYAtNlzlC2hTmD5R4FH3FX1Gz1pWaXrJ2tHU6HF+bn7elD4ZxjnSK9HqdAVOAhJ2PH3MkkjOxrNoW9K1dfDvHQ== X-Received: from pgct14.prod.google.com ([2002:a05:6a02:528e:b0:7fd:113b:ea62]) (user=isaacmanjarres job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6830:2710:b0:71d:4624:3f2b with SMTP id 46e09a7af769-71dcf546d9fmr1201933a34.19.1733447378840; Thu, 05 Dec 2024 17:09:38 -0800 (PST) Date: Thu, 5 Dec 2024 17:09:22 -0800 In-Reply-To: <20241206010930.3871336-1-isaacmanjarres@google.com> Mime-Version: 1.0 References: <20241206010930.3871336-1-isaacmanjarres@google.com> X-Mailer: git-send-email 2.47.0.338.g60cca15819-goog Message-ID: <20241206010930.3871336-2-isaacmanjarres@google.com> Subject: [RFC PATCH v1 1/2] mm/memfd: Add support for F_SEAL_FUTURE_EXEC to memfd From: "Isaac J. Manjarres" To: Andrew Morton , Jeff Layton , Chuck Lever , Alexander Aring , "Liam R. Howlett" , Lorenzo Stoakes , Vlastimil Babka , Jann Horn , Shuah Khan Cc: "Isaac J. Manjarres" , kernel-team@android.com, linux-mm@kvack.org, linux-kernel@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-kselftest@vger.kernel.org, Suren Baghdasaryan , Kalesh Singh , John Stultz X-Rspamd-Queue-Id: 42B9C40003 X-Stat-Signature: f96xn4qt6cbntre6igz18um1pdc7j3bi X-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Gha3TnpsAjg3QOo/ydpAyAoPyod92FtNJn7aqrBkTEYn7OA1CzYmqNCrSQzW1fW6KQ5bLpCcp9O4GNACvcT2imfnLrrEOezJ5quOpb9o5FKNCy1Tjef7GwETdNqWlFueyWqyyNhGrX3HDY0SBt3l9Ze4TlCvF3XxddeIWGr0lAUlHicesqcHrzzOr7QfPM0WZHWtTzrvyGO9yP59UhB6G9HE9SFRz5TnLcm7WpVXKoe6aBw9jgay87qMZpjij2tJTxJ34H9DQEs2l3SPSPtCZH9WYOFGbUxft9mcT/1A2P8mvY+hD5FIhRLTd8uikvvJhr9wYGbm+9K5IrLiQIcrqJKt7VIbqyQN80XqHIBDmSXgYK+QKVCUJ+RGLoTAdANvMjXw8nvWlOfLy0EPlXtn2QD4XR/Af/pyxoZvbMYwqHL5OoIwndklXQe0SyiywhvHL3xomfXuH67ud0Cw2RhmHU4lS1Gsemg6SOgo/QHbqy14ETdOnXFQ/qGM8r7MIwHWtEf1YIjm0vLj7h5epRUTP2GYVQWj3pHRtPUtPbrqGI3qMSMF0lGn1Wq1YQv0ttmQ9W4RSAmO9+pIjDNvq1cCwP4AO29eEAtTwopwb5lUzkyE4d9JWrcC398+poIkEKWhSBd/NRV61hYjhu04wGiBdL6S7g5YOn5sapHX+ufOoM8oewB80m6BGown95wmRqhU59MjJQ== 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: Android currently uses the ashmem driver [1] for creating shared memory regions between processes. Ashmem buffers can initially be mapped with PROT_READ, PROT_WRITE, and PROT_EXEC. Processes can then use the ASHMEM_SET_PROT_MASK ioctl command to restrict--never add--the permissions that the buffer can be mapped with. Processes can remove the ability to map ashmem buffers as executable to ensure that those buffers cannot be exploited to run unintended code. We are currently trying to replace ashmem with memfd. However, memfd does not have a provision to permanently remove the ability to map a buffer as executable. Although, this should be something that can be achieved via a new file seal. There are known usecases (e.g. CursorWindow [2]) where a process maps a buffer with read/write permissions before restricting the buffer to being mapped as read-only for future mappings. The resulting VMA from the writable mapping has VM_MAYEXEC set, meaning that mprotect() can change the mapping to be executable. Therefore, implementing the seal similar to F_SEAL_WRITE would not be appropriate, since it would not work with the CursorWindow usecase. This is because the CursorWindow process restricts the mapping permissions to read-only after the writable mapping is created. So, adding a file seal for executable mappings that operates like F_SEAL_WRITE would fail. Therefore, add support for F_SEAL_FUTURE_EXEC, which is handled similarly to F_SEAL_FUTURE_WRITE. This ensures that CursorWindow can continue to create a writable mapping initially, and then restrict the permissions on the buffer to be mappable as read-only by using both F_SEAL_FUTURE_WRITE and F_SEAL_FUTURE_EXEC. After the seal is applied, any calls to mmap() with PROT_EXEC will fail. [1] https://cs.android.com/android/kernel/superproject/+/common-android-mainline:common/drivers/staging/android/ashmem.c [2] https://developer.android.com/reference/android/database/CursorWindow Cc: Suren Baghdasaryan Cc: Kalesh Singh Cc: John Stultz Signed-off-by: Isaac J. Manjarres --- include/linux/mm.h | 5 +++++ include/uapi/linux/fcntl.h | 1 + mm/memfd.c | 1 + mm/mmap.c | 11 +++++++++++ 4 files changed, 18 insertions(+) diff --git a/include/linux/mm.h b/include/linux/mm.h index 4eb8e62d5c67..40c03a491e45 100644 --- a/include/linux/mm.h +++ b/include/linux/mm.h @@ -4096,6 +4096,11 @@ static inline bool is_write_sealed(int seals) return seals & (F_SEAL_WRITE | F_SEAL_FUTURE_WRITE); } +static inline bool is_exec_sealed(int seals) +{ + return seals & F_SEAL_FUTURE_EXEC; +} + /** * is_readonly_sealed - Checks whether write-sealed but mapped read-only, * in which case writes should be disallowing moving diff --git a/include/uapi/linux/fcntl.h b/include/uapi/linux/fcntl.h index 6e6907e63bfc..ef066e524777 100644 --- a/include/uapi/linux/fcntl.h +++ b/include/uapi/linux/fcntl.h @@ -49,6 +49,7 @@ #define F_SEAL_WRITE 0x0008 /* prevent writes */ #define F_SEAL_FUTURE_WRITE 0x0010 /* prevent future writes while mapped */ #define F_SEAL_EXEC 0x0020 /* prevent chmod modifying exec bits */ +#define F_SEAL_FUTURE_EXEC 0x0040 /* prevent future executable mappings */ /* (1U << 31) is reserved for signed error codes */ /* diff --git a/mm/memfd.c b/mm/memfd.c index 35a370d75c9a..77b49995a044 100644 --- a/mm/memfd.c +++ b/mm/memfd.c @@ -184,6 +184,7 @@ unsigned int *memfd_file_seals_ptr(struct file *file) } #define F_ALL_SEALS (F_SEAL_SEAL | \ + F_SEAL_FUTURE_EXEC |\ F_SEAL_EXEC | \ F_SEAL_SHRINK | \ F_SEAL_GROW | \ diff --git a/mm/mmap.c b/mm/mmap.c index b1b2a24ef82e..c7b96b057fda 100644 --- a/mm/mmap.c +++ b/mm/mmap.c @@ -375,6 +375,17 @@ unsigned long do_mmap(struct file *file, unsigned long addr, if (!file_mmap_ok(file, inode, pgoff, len)) return -EOVERFLOW; + if (is_exec_sealed(seals)) { + /* No new executable mappings if the file is exec sealed. */ + if (prot & PROT_EXEC) + return -EACCES; + /* + * Prevent an initially non-executable mapping from + * later becoming executable via mprotect(). + */ + vm_flags &= ~VM_MAYEXEC; + } + flags_mask = LEGACY_MAP_MASK; if (file->f_op->fop_flags & FOP_MMAP_SYNC) flags_mask |= MAP_SYNC;